Class: Lispcalc::Lexer
- Inherits:
-
Object
- Object
- Lispcalc::Lexer
- 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
-
#initialize(input) ⇒ Lexer
constructor
A new instance of Lexer.
- #next_token ⇒ Object
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
Instance Method Details
#next_token ⇒ Object
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 |