Class: NexusParser::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_parser/lexer.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Lexer

Returns a new instance of Lexer.



5
6
7
8
9
10
# File 'lib/nexus_parser/lexer.rb', line 5

def initialize(input)
  @input = input
  # linefeed check the input here -
  @input.gsub!(/\x0D/,"") # get rid of possible dos carrige returns
  @next_token = nil
end

Instance Method Details

#peek(token_class) ⇒ Object

checks whether the next token is of the specified class.



13
14
15
16
# File 'lib/nexus_parser/lexer.rb', line 13

def peek(token_class)
  token = read_next_token(token_class)
  return token.class == token_class
end

#pop(token_class) ⇒ Object

return (and delete) the next token from the input stream, or raise an exception if the next token is not of the given class.



20
21
22
23
24
25
26
27
28
# File 'lib/nexus_parser/lexer.rb', line 20

def pop(token_class)
  token = read_next_token(token_class)
  @next_token = nil
  if token.class != token_class
    raise(NexusParser::ParseError,"expected #{token_class.to_s} but received #{token.class.to_s} at #{@input[0..40]}...", caller)
  else
    return token
  end
end