Class: Termular::Lexer
- Inherits:
-
Object
- Object
- Termular::Lexer
- Defined in:
- lib/termular/parser.rb
Constant Summary collapse
- TOKENS =
[ [ :WHITESPACE, /\s+/ ], [ :NUMBER, /\d+(\.\d+)?/, ->m { m[0].to_f } ], [ :BAREWORD, /[a-z][a-z0-9]*/, ->m { m[0] } ], [ :PLUS, /\+/ ], [ :MINUS, /-/ ], [ :TIMES, /\*/ ], [ :DOT, /\./ ], [ :DIVIDE, /\// ], [ :POWER, /\^/ ], [ :OPEN_PAREN, /\(/ ], [ :CLOSE_PAREN, /\)/ ], [ :OPEN_BRACKET, /\[/ ], [ :CLOSE_BRACKET, /\]/ ], [ :COMMA, /\,/ ], [ :COMMAND, /:([a-z][a-z0-9]*)/, ->m { m[1] } ], [ :LTE, /<=/ ], [ :LT, /</ ], [ :GTE, />=/ ], [ :GT, />/ ] ].map { |a| [a[0], Regexp.new("\\A#{a[1].source}", Regexp::MULTILINE), a[2]] }
Class Method Summary collapse
Class Method Details
.lex!(original_src) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/termular/parser.rb', line 100 def self.lex!(original_src) tokens = [] offset = 0 src = original_src until src.empty? match = nil tok, re, conv = TOKENS.find { |tok, re, conv| match = re.match(src, offset) } raise SyntaxError, "illegal character at position #{original_src.length - src.length}" unless match tokens << [tok, conv && conv[match]] unless tok == :WHITESPACE src = match.post_match end tokens << [:END] end |