Class: Lexer

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

Constant Summary collapse

TOKENS =
[
  /\(|\)/,          # parens
  /'/,              # quote
  /"(\\"|[^"])*"/,  # 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.



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

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

Class Method Details

.tokenize(program) ⇒ Object



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

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

Instance Method Details

#tokenizeObject



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

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