Class: Lispcalc::Lexer

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

Constant Summary collapse

WS =
/\A\s+/.freeze
OPEN_PAREN =
/\A\(/.freeze
CLOSE_PAREN =
/\A\)/.freeze
NUMBER =
/\A[+-]?([0-9]*[.])?[0-9]+/.freeze
SYMBOL =

TODO: review this regexp

/\A[+\-*\/>a-zA-Z][+\-*\/>a-zA-Z0-9]*/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Lexer

Returns a new instance of Lexer.



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

def initialize(input)
  @input = input
end

Class Method Details

.tokenize(input) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/lispcalc/lexer.rb', line 40

def self.tokenize(input)
  tokens = []
  lexer = Lexer.new(input)
  while (token = lexer.next_token)
    tokens << token
  end
  tokens
end

Instance Method Details

#next_tokenObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lispcalc/lexer.rb', line 13

def next_token
  if @input =~ WS
    advance(Regexp.last_match(0).length)
  end

  return nil if @input.empty?

  case @input
  when OPEN_PAREN
    advance
    [:open_paren]
  when CLOSE_PAREN
    advance
    [:close_paren]
  when NUMBER
    number = Regexp.last_match(0)
    advance(number.length)
    [:number, number]
  when SYMBOL
    symbol = Regexp.last_match(0)
    advance(symbol.length)
    [:symbol, symbol]
  else
    raise UnknownTokenError
  end
end