Class: Lexer

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

Constant Summary collapse

TOKENS =
[
  /\(|\)/,          # parens
  /'/,              # quote
  /\./,             # dot
  /"(\\"|[^"])*"/,  # string literal
  /[^\s)]+/,        # any non-whitespace character excluding )
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program) ⇒ Lexer

Returns a new instance of Lexer.



16
17
18
# File 'lib/lasp/lexer.rb', line 16

def initialize(program)
  @scanner = StringScanner.new(sanitize(program))
end

Class Method Details

.tokenize(program) ⇒ Object



12
13
14
# File 'lib/lasp/lexer.rb', line 12

def self.tokenize(program)
  new(program).tokenize
end

Instance Method Details

#tokenizeObject



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

def tokenize
  tokens = []
  while (token = read_token)
    tokens << token
  end
  tokens
end