Class: OboParser::Lexer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Lexer

Returns a new instance of Lexer.



3
4
5
6
# File 'lib/lexer.rb', line 3

def initialize(input)
  @input = input
  @next_token = nil
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



2
3
4
# File 'lib/lexer.rb', line 2

def input
  @input
end

Instance Method Details

#peek(token_class) ⇒ Object

checks whether the next token is of the specified class.



9
10
11
12
# File 'lib/lexer.rb', line 9

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.



16
17
18
19
20
21
22
23
24
# File 'lib/lexer.rb', line 16

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