Class: Aspen::CustomGrammar::Lexer

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tokenize(code, env = {}) ⇒ Object



6
7
8
# File 'lib/aspen/custom_grammar/lexer.rb', line 6

def self.tokenize(code, env={})
  new.tokenize(code, env)
end

Instance Method Details

#no_match(scanner, state) ⇒ Object

Raises:



69
70
71
72
# File 'lib/aspen/custom_grammar/lexer.rb', line 69

def no_match(scanner, state)
  raise Aspen::LexError,
          Aspen::Errors.messages(:unexpected_token, scanner, state)
end

#pop_stateObject



65
66
67
# File 'lib/aspen/custom_grammar/lexer.rb', line 65

def pop_state
  stack.pop
end

#push_state(state) ⇒ Object



61
62
63
# File 'lib/aspen/custom_grammar/lexer.rb', line 61

def push_state(state)
  stack.push(state)
end

#stackObject



53
54
55
# File 'lib/aspen/custom_grammar/lexer.rb', line 53

def stack
  @stack ||= []
end

#stateObject



57
58
59
# File 'lib/aspen/custom_grammar/lexer.rb', line 57

def state
  stack.last || :default
end

#tokenize(code, env = {}) ⇒ Object



10
11
12
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
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aspen/custom_grammar/lexer.rb', line 10

def tokenize(code, env={})
  scanner = StringScanner.new(code)
  tokens = []

  # puts "tokens: #{tokens} | state: #{state} | stack: #{stack}"

  until scanner.eos?
    case state
    when :default then
      if scanner.scan(/\(/)
        tokens << [:OPEN_PARENS]
        push_state :capture_segment
      elsif scanner.scan(/[[[:alnum:]][[:blank:]]\!"\#$%&'*+,\-.\/:;<=>?@\[\\\]^_‘\{\|\}~]+/)
        tokens << [:BARE, scanner.matched]
      else
        no_match(scanner, state)
      end

    when :capture_segment
      if scanner.scan(/\s+/)
        # NO OP
      elsif scanner.scan(/^(numeric|integer|float|string)/)
        tokens << [:TYPE, scanner.matched]
      elsif scanner.scan(Aspen::Lexer::PASCAL_CASE)
        tokens << [:TYPE, ["node", scanner.matched]]
      # TODO: This should only accept legal variable names, like `hello_01`
      elsif scanner.scan(/^\w+/)
        tokens << [:VAR_NAME, scanner.matched]
      elsif scanner.scan(/\)/)
        tokens << [:CLOSE_PARENS]
        pop_state
      else
        no_match(scanner, state)
      end

    else
      raise Aspen::LexError, "There is no matcher for state #{state.inspect}."
    end
  end

  tokens
end