Class: Contraction::TypeLexer

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/type_parser.rb

Overview

The lexer scans the input, creating a stack of tokens that can be used to then figure out our parse tree.

Constant Summary collapse

TOKENS =
[
  /^Hash/,
  /^=>/,
  /^\{/, /^\}/,
  /^\[/, /^\]/,
  /^\(/, /^\)/,
  /^</, /^>/,
  /^,/,
  /^#/,
  /([a-z_]+[a-z0-9_]*|(H(?!ash)))?[^=\{\[\(<>\)\]\},#]+/
]

Class Method Summary collapse

Class Method Details

.lex(text) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/parser/type_parser.rb', line 17

def self.lex(text)
  stack = []
  while text.length > 0
    changed = false

    TOKENS.each do |r|
      if m = text.match(r)
        if m[0].strip != ''
          stack << m[0].strip
        end
        text.sub! r, ''
        changed = true
        break
      end
    end

    raise "Unknown token found at #{text}" unless changed
  end

  stack.reverse
end