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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
# File 'lib/eggshell/expression-evaluator/parser.rb', line 69
def emit(type, data = nil, ts = nil, te = nil)
lst = @state[-1]
if type == :end
while @state[-1] == ST_OPERATOR || @state[-1] == ST_OPERATOR_TERN
@state.pop
end
return
end
word = data[ts...te].pack('c*')
@tokens << word
arg_check = true
if lst == ST_LABEL_CALL
arg_check = @ptr.length == 0 || @last_comma
elsif lst == ST_ARRAY
arg_check = @ptr.length == 1 || @last_comma
end
@last_comma = false if type != :space
expr_frag = ''
if @tokens.length > 5
expr_frag = @tokens[@tokens.length-5..-2].join('')
else
expr_frag = @tokens[0..-2].join('')
end
if type == :escape
raise Exception.new("expecting identifier after '#{@ptr[-1][1]}'") if @expect_label
raise Exception.new("escaping character outside of string") if lst != ST_STRING
word_unesc = ESCAPE_MAP[word]
raise Exception.new("invalid escape sequence: #{word}") if !word_unesc
@term_str += word_unesc
return
elsif type == :str_delim
raise Exception.new("expecting identifier after '#{@ptr[-1][1]}', not #{word}") if @expect_label
if !@quote_delim
@term_last.pop if lst != ST_INDEX_ACCESS
raise Exception.new("missing a comma after: #{expr_frag}") if !arg_check
if @hash_state[-1] == :key
@hash_state[-1] = :colon
elsif @hash_state[-1] == :value
@hash_state[-1] = :comma
elsif @hash_state[-1] == :comma
raise Exception.new("expecting comma after: #{expr_frag}")
end
@quote_delim = word
@state << ST_STRING
@term_str = ''
elsif @quote_delim == word
@quote_delim = nil
@state.pop
@ptr << @term_str
@term_str = nil
end
elsif lst == ST_STRING
@term_str += word
elsif type == :number_literal
@term_last.pop if lst != ST_INDEX_ACCESS
raise Exception.new("expecting identifier after: '#{@ptr[-1][1]}'") if @expect_label
raise Exception.new("missing a comma after: #{expr_frag}") if !arg_check
@ptr << (word.index('.') ? word.to_f : word.to_i)
if @hash_state[-1] == :key
@hash_state[-1] = :colon
elsif @hash_state[-1] == :value
@hash_state[-1] = :comma
end
elsif type == :identifier
if lst == ST_LABEL_MEMBER
@state.pop
@ptr << word
@last_ptr[-1][-1] << @ptr
@ptr = @last_ptr.pop
elsif @expect_label
@ptr[-1][1] += word
@expect_label = false
else
raise Exception.new("missing a comma after: #{expr_frag}") if !arg_check
if CONSTS.has_key?(word)
@ptr << CONSTS[word]
else
@ptr << [:var, word]
end
if @hash_state[-1] == :key
@hash_state[-1] = :colon
elsif @hash_state[-1] == :value
@hash_state[-1] = :comma
end
end
@term_last << ST_LABEL
elsif type == :logical_op
word = '==' if word == '='
@term_last.pop
raise Exception.new("expecting identifier after '#{@ptr[-1][1]}'") if @expect_label
if word == '?'
tern = [:op_tern, nil, [], nil]
tern[1] = @last_ptr[-1].pop
@last_ptr[-1] << tern
@ptr = tern[2]
@state[-1] = ST_OPERATOR_TERN
elsif lst == ST_OPERATOR
op = word.to_sym
prec_l = OPERATOR_MAP[@ptr[-2]]
prec_r = OPERATOR_MAP[op]
if prec_r > prec_l
nop = [:op, [@ptr.pop]]
@state << ST_OPERATOR
@ptr << nop
@last_ptr << @ptr
@ptr = nop[1]
elsif prec_l > prec_r
type = @last_ptr[-2].is_a?(Array) ? @last_ptr[-2][0][0] : nil
if type == :op
@state.pop
@last_ptr.pop
@ptr = @last_ptr[-1][0][1]
end
end
@ptr << op
else
lele = @ptr.pop
op = [:op, [lele, word.to_sym]]
@last_ptr << @ptr
@ptr << op
@ptr = op[1]
@state << ST_OPERATOR
end
elsif type == :paren_group
raise Exception.new("expecting identifier after '#{@ptr[-1][1]}'") if @expect_label
if word == '('
if @term_last[-1] == ST_LABEL
@state << ST_LABEL_CALL
@ptr[-1][0] = :func
@ptr[-1][2] = []
@last_ptr << @ptr
@ptr = @ptr[-1][2]
@expect_separator = ','
else
raise Exception.new("missing a comma after: #{expr_frag}") if !arg_check
@state << ST_GROUP
@last_ptr << @ptr
@ptr << [:group, []]
@ptr = @ptr[-1][1]
end
@term_last.pop
else
do_close = false
if lst == ST_OPERATOR
do_close = @state[-2] == ST_GROUP
if do_close
@state.pop
@ptr = @last_ptr.pop[1]
end
else
do_close = lst == ST_LABEL_CALL || lst == ST_GROUP
end
if do_close
s = @state.pop
@ptr = @last_ptr.pop
@expect_separator = nil
if @hash_state[-1] == :key
@hash_state[-1] = :colon
elsif @hash_state[-1] == :value
@hash_state[-1] = :comma
end
else
end
end
elsif type == :brace_group
raise Exception.new("missing a comma after: #{expr_frag}") if !arg_check
if word == '{'
if @hash_state[-1] && @hash_state[-1] != :value
raise Exception.new("invalid hash start")
end
@state << ST_HASH
@last_ptr << @ptr
@ptr = [:hash]
@hash_state << :key
else
if lst == ST_HASH
if @hash_state[-1] != :comma
msg = "missing value for key #{@ptr[-1]}"
msg = "missing ':' and a value for key #{@ptr[-1]}" if @hash_state[-1] == :key
msg = "missing a value for key #{@ptr[-1]}" if @hash_state[-1] == :value
raise Exception.new(msg)
end
@last_ptr[-1] << @ptr
@ptr = @last_ptr.pop
@state.pop
@hash_state.pop
if @hash_state[-1] == :key
@hash_state[-1] = :colon
elsif @hash_state[-1] == :value
@hash_state[-1] = :comma
end
else
end
end
elsif type == :index_group
if word == '['
if @term_last[-1] == ST_LABEL
@state << ST_INDEX_ACCESS
@last_ptr << @ptr
@ptr = [:index_access]
else
raise Exception.new("missing a comma after: #{expr_frag}") if !arg_check
if @hash_state[-1] && @hash_state[-1] != :value
raise Exception.new("invalid array start near: #{expr_frag}")
end
@ptr << [:array]
@state << ST_ARRAY
@last_ptr << @ptr
@ptr = @ptr[-1]
end
else
if lst == ST_INDEX_ACCESS
acc = @ptr
@ptr = @last_ptr.pop
@ptr[-1] << acc
@state.pop
elsif lst == ST_ARRAY
@ptr = @last_ptr.pop
@state.pop
if @hash_state[-1] == :key
@hash_state[-1] = :colon
elsif @hash_state[-1] == :value
@hash_state[-1] = :comma
end
else
end
end
elsif type == :separator
if word == '.'
if @term_last[-1] == ST_LABEL
@state << ST_LABEL_MEMBER
@last_ptr << @ptr
@ptr = [:member_access]
else
end
elsif word == ','
if lst == ST_HASH
if @hash_state[-1] == :comma
@hash_state[-1] = :key
else
raise Exception.new("misplaced comma near: #{expr_frag}")
end
elsif lst == ST_ARRAY
@last_comma = true
elsif lst == ST_LABEL_CALL
@last_comma = true
end
elsif word == ';'
end
elsif type == :modifier
if word == ':'
if lst == ST_HASH
if @hash_state[-1] == :colon
@hash_state[-1] = :value
else
raise Exception.new("misplaced colon near: #{expr_frag} -- #{@hash_state[-1]}")
end
elsif lst == ST_OPERATOR_TERN
tern = @last_ptr[-1][-1]
if tern[3] == nil
tern[3] = []
@ptr = tern[3]
else
end
elsif @term_last[-1] == ST_LABEL
@ptr[-1][1] += ':'
@expect_label = true
@term_last[-1] = nil
end
end
elsif type == :space
else
$stderr.write "! parser else: #{word}\n"
end
end
|