Class: Termular::Lexer

Inherits:
Object
  • Object
show all
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] } ]
].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



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/termular/parser.rb', line 84

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