Class: Aspen::Lexer

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

Constant Summary collapse

STRING_CAPTURE =
/(["'])(?:(?=(\\?))\2.)*?\1/
NUMBER_CAPTURE =
/([\d,]+\.?\d+)/
PASCAL_CASE =
/^([A-Z][a-zA-Z0-9]+)/
LABEL_PASCAL_CASE =
/^(:[A-Z][a-zA-Z0-9]+)/
CONTENT_REGEX =
/[[[:alnum:]][[:blank:]]"'&_,\-\–\—\!\.\/\\\?\$]+/
LABELED_NODE =
/^([A-Z][a-zA-Z0-9]+): (#{CONTENT_REGEX})\)/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



17
18
19
# File 'lib/aspen/lexer.rb', line 17

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

Instance Method Details

#pop_stateObject



183
184
185
# File 'lib/aspen/lexer.rb', line 183

def pop_state
  stack.pop
end

#push_state(state) ⇒ Object



179
180
181
# File 'lib/aspen/lexer.rb', line 179

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

#stackObject



171
172
173
# File 'lib/aspen/lexer.rb', line 171

def stack
  @stack ||= []
end

#stateObject



175
176
177
# File 'lib/aspen/lexer.rb', line 175

def state
  stack.last || :default
end

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



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/aspen/lexer.rb', line 21

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

  environment = Discourse.assert(env)
  grammar = environment.grammar

  until scanner.eos?
    # puts "tokens (6): #{tokens.last(6).inspect}"
    # puts "\n(#{state}) stack: #{stack}"
    # puts "grammar: #{grammar.inspect}"

    # Match custom grammars
    if grammar && scanner.beginning_of_line?
      line = scanner.scan(/^.*$/)
      if grammar.match?(line)
        tokens << [:CUSTOM_GRAMMAR_STATEMENT, line]
        next
      else
        scanner.unscan # reset pointer to beginning of line
      end
    end

    # If the line ends with :, it's starting a list.
    if scanner.beginning_of_line?
      line = scanner.scan(/^.*$/)
      if line.match? /:$/
        tokens << [:PREPARE_START_LIST]
      end
      scanner.unscan
    end

    # Standard Aspen syntax
    case state
    when :default then
      if scanner.scan(/\(/)
        tokens << [:OPEN_PARENS]
        push_state :node
      elsif scanner.scan(/\[/)
        tokens << [:OPEN_BRACKETS]
        push_state :edge
      elsif scanner.scan(/(:\s*\n)/) # Colon, any whitespace, newline
        tokens << [:START_LIST, scanner.matched]
        push_state :list
      elsif scanner.scan(/\./)
        tokens << [:END_STATEMENT, scanner.matched]
      elsif scanner.scan(/\s/)
        # NO OP
      elsif scanner.scan(/#.*$/)
        tokens << [:COMMENT, scanner.matched.gsub(/#\s*/, '')]
      else
        no_match(scanner, state)
      end

    when :node then
      # Removed Cypher form for now. Un   comment the next 3 lines
      # to start working on it.
      #
      # if scanner.scan(LABEL_PASCAL_CASE)
      #   tokens << [:LABEL, scanner.matched]
      #   push_state :hash
      if scanner.match?(LABELED_NODE)
        push_state :node_labeled
      elsif scanner.scan(/\n/) && stack == [:list, :node]
        # If we're inside a list node and we encounter a newline,
        # pop :node so we can return to the :list state.
        scanner.unscan
        pop_state
      elsif scanner.scan(CONTENT_REGEX)
        tokens << [:CONTENT, scanner.matched.strip]
      elsif scanner.scan(/[\:]/)
        tokens << [:SEPARATOR, scanner.matched]
      elsif scanner.scan(/\(/)
        tokens << [:OPEN_PARENS]
        push_state :label
      elsif scanner.scan(/\)/)
        tokens << [:CLOSE_PARENS]
        pop_state
      else
        no_match(scanner, state)
      end

    when :node_labeled
      if scanner.scan(PASCAL_CASE)
        tokens << [:LABEL, scanner.matched]
        pop_state # Back to Node
      else
        no_match(scanner, state)
      end

    when :edge then
      if scanner.scan(/[[[:alpha:]]\s]+/)
        tokens << [:CONTENT, scanner.matched.strip]
      elsif scanner.scan(/\]/)
        tokens << [:CLOSE_BRACKETS]
        pop_state
      else
        no_match(scanner, state)
      end

    when :hash then
      if scanner.scan(/\{/)
        tokens << [:OPEN_BRACES]
      elsif scanner.scan(/[[[:alpha:]]_]+/)
        tokens << [:IDENTIFIER, scanner.matched]
      elsif scanner.scan(/[\,\:]/)
        tokens << [:SEPARATOR, scanner.matched]
      elsif scanner.scan(STRING_CAPTURE)
        tokens << [:STRING, scanner.matched]
      elsif scanner.scan(NUMBER_CAPTURE)
        tokens << [:NUMBER, scanner.matched]
      elsif scanner.scan(/\}/)
        tokens << [:CLOSE_BRACES]
        pop_state
      elsif scanner.scan(/\s+/)
        # NO OP
      else
        no_match(scanner, state)
      end

    when :list then
      if scanner.scan(/([\-\*\+])/) # -, *, or + (any allowed by Markdown)
        tokens << [:BULLET, scanner.matched]
        push_state :node
      elsif scanner.scan(/\n\n/)
        tokens << [:END_LIST]
        pop_state
      elsif scanner.scan(/\s/)
        # NO OP
      else
        no_match(scanner, state)
      end

    when :label
      if scanner.scan(PASCAL_CASE)
        tokens << [:CONTENT, scanner.matched]
      elsif scanner.peek(1).match?(/\)/)
        pop_state # Go back to :node and let :node pop state
      else
        no_match(scanner, state)
      end

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

  tokens
end