Class: Nay::Lexer

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

Overview

Read in a string and generate Token instances.

Constant Summary collapse

EXPRESSION =

Stack States

:expression
EXPRESSION_LITERAL =
:expression_literal
CLOSE_EXPRESSION =

Token Types

:close_expression
CONTENT =
:content
END_EXPRESSION_LITERAL =
:end_expression_literal
IDENTIFIER =
:identifier
IGNORE =
:ignore
OPEN_EXPRESSION =
:open_expression
OPEN_EXPRESSION_LITERAL =
:open_expression_literal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Lexer

Returns a new instance of Lexer.



24
25
26
27
# File 'lib/nay/lexer.rb', line 24

def initialize(string)
  @scanner = StringScanner.new(string)
  @state   = State.new
end

Instance Attribute Details

#scannerObject (readonly)

Returns the value of attribute scanner.



22
23
24
# File 'lib/nay/lexer.rb', line 22

def scanner
  @scanner
end

#stateObject (readonly)

Returns the value of attribute state.



22
23
24
# File 'lib/nay/lexer.rb', line 22

def state
  @state
end

Instance Method Details

#next_tokenObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nay/lexer.rb', line 37

def next_token
  return nil if scanner.eos?

  case state.current
  when State::DEFAULT
    default
  when EXPRESSION
    expression
  when EXPRESSION_LITERAL
    expression_literal
  end
end

#restObject



29
30
31
32
33
34
35
# File 'lib/nay/lexer.rb', line 29

def rest
  [].tap do |tokens|
    while (token = next_token)
      tokens << token
    end
  end
end