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?
if grammar && scanner.beginning_of_line?
line = scanner.scan(/^.*$/)
if grammar.match?(line)
tokens << [:CUSTOM_GRAMMAR_STATEMENT, line]
next
else
scanner.unscan end
end
if scanner.beginning_of_line?
line = scanner.scan(/^.*$/)
if line.match? /:$/
tokens << [:PREPARE_START_LIST]
end
scanner.unscan
end
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)/) tokens << [:START_LIST, scanner.matched]
push_state :list
elsif scanner.scan(/\./)
tokens << [:END_STATEMENT, scanner.matched]
elsif scanner.scan(/\s/)
elsif scanner.scan(/#.*$/)
tokens << [:COMMENT, scanner.matched.gsub(/#\s*/, '')]
else
no_match(scanner, state)
end
when :node then
if scanner.match?(LABELED_NODE)
push_state :node_labeled
elsif scanner.scan(/\n/) && stack == [:list, :node]
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 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+/)
else
no_match(scanner, state)
end
when :list then
if scanner.scan(/([\-\*\+])/) tokens << [:BULLET, scanner.matched]
push_state :node
elsif scanner.scan(/\n\n/)
tokens << [:END_LIST]
pop_state
elsif scanner.scan(/\s/)
else
no_match(scanner, state)
end
when :label
if scanner.scan(PASCAL_CASE)
tokens << [:CONTENT, scanner.matched]
elsif scanner.peek(1).match?(/\)/)
pop_state else
no_match(scanner, state)
end
else raise Aspen::LexError, "There is no matcher for state #{state.inspect}."
end
end
tokens
end
|