cel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cel/parser.rb ADDED
@@ -0,0 +1,996 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.5.1
4
+ # from Racc grammar file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+ require 'strscan'
10
+ require 'bigdecimal'
11
+ require 'cel/ast/elements'
12
+ module Cel
13
+ class Parser < Racc::Parser
14
+
15
+ module_eval(<<'...end parser.ry/module_eval...', 'parser.ry', 97)
16
+
17
+
18
+
19
+ OPERATORS = if RUBY_VERSION < "2.7.0"
20
+ {
21
+ "&&" => :tANDOP,
22
+ "||" => :tOROP,
23
+ "+" => :tADDOP,
24
+ "-" => :tSUBOP,
25
+ }.merge(Hash[Cel::LOGICAL_OPERATORS.map{|op| [op, :tRELOP] }])
26
+ .merge(Hash[Cel::MULTI_OPERATORS.map{|op| [op, :tMULTIOP] }])
27
+ else
28
+ {
29
+ **Hash[Cel::LOGICAL_OPERATORS.map{|op| [op, :tRELOP] }],
30
+ **Hash[Cel::MULTI_OPERATORS.map{|op| [op, :tMULTIOP] }],
31
+ "&&" => :tANDOP,
32
+ "||" => :tOROP,
33
+ "+" => :tADDOP,
34
+ "-" => :tSUBOP,
35
+ }
36
+ end.freeze
37
+
38
+ OPERATORS_RE = Regexp.union(*OPERATORS.keys)
39
+
40
+ RESERVED = %W[
41
+ as break const continue else
42
+ for function if import let
43
+ loop package namespace return
44
+ var void while
45
+ ].freeze
46
+
47
+ IDENTIFIER_REGEX = /[a-zA-Z][_a-zA-Z0-9]*/
48
+
49
+ STRING_LIT_REGEX = Regexp.union(
50
+ /"""(?~""")*"""/,
51
+ /'''(?~''')*'''/,
52
+ /"(\\"|[^"])*"/,
53
+ /'(\\'|[^'])*'/,
54
+ )
55
+
56
+ NUMBER_REGEX = Regexp.union(
57
+ /(0x[0-9a-fA-F]+)[uU]?/, # hexadecimal
58
+ /(\d+)[uU]/, # uinteger
59
+ /((\d*\.\d+)|\d+)([eE][+\-]?\d+)?/, # integer, float, exponent
60
+ )
61
+
62
+ def parse(str)
63
+ tokenize(str)
64
+ do_parse
65
+ rescue Racc::ParseError => err
66
+ ex = Cel::ParseError.new(err.message)
67
+ ex.set_backtrace(err.backtrace)
68
+ raise ex
69
+ end
70
+
71
+ def tokenize(str)
72
+ str.force_encoding(Encoding::BINARY) unless str.valid_encoding?
73
+
74
+ scanner = StringScanner.new(str)
75
+
76
+ @q = []
77
+
78
+ until scanner.eos?
79
+ case
80
+ when scanner.scan(/\s+/)
81
+ # skip whitespace
82
+ when scanner.scan(/#(( *)|( ?(?<string>.*)))\n/)
83
+ # skip comment lines
84
+ when scanner.scan(NUMBER_REGEX)
85
+ @q << convert_to_number(scanner)
86
+ when scanner.scan(/true|false/)
87
+ @q << [:tBOOL, scanner.matched == "true"]
88
+ when scanner.scan(/null/)
89
+ @q << [:tNULL, nil]
90
+ when scanner.scan(/[bB]?[rR]?#{STRING_LIT_REGEX}/) # string
91
+ # s = scanner.matched.yield_self {|s| s[1, s.length - 2] }
92
+ # .gsub(DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE) do |match|
93
+ # case match
94
+ # when '\\a' then "\a"
95
+ # when '\\b' then "\b"
96
+ # when '\\e' then "\e"
97
+ # when '\\f' then "\f"
98
+ # when '\\n' then "\n"
99
+ # when '\\r' then "\r"
100
+ # when '\\s' then "\s"
101
+ # when '\\t' then "\t"
102
+ # when '\\v' then "\v"
103
+ # when '\\"' then '"'
104
+ # end
105
+ # end
106
+
107
+ # s = scanner.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\'/, "'")
108
+
109
+ str = scanner.matched
110
+
111
+ @q << if str.start_with?("b")
112
+ [:tBYTES, convert_to_bytes(str.byteslice(2..-2))]
113
+ else
114
+ [:tSTRING, convert_to_string(str)]
115
+ end
116
+ when scanner.scan(IDENTIFIER_REGEX)
117
+ word = scanner.matched
118
+ if RESERVED.include?(word)
119
+ @q << [:tRESERVED, scanner.matched]
120
+ elsif word == "in"
121
+ @q << [OPERATORS[scanner.matched], scanner.matched]
122
+ else
123
+ @q << [:tIDENTIFIER, scanner.matched]
124
+ end
125
+ when scanner.scan(OPERATORS_RE)
126
+ @q << [OPERATORS[scanner.matched], scanner.matched]
127
+ when scanner.scan(/\A.|\n/o)
128
+ s = scanner.matched
129
+ @q << [s, s]
130
+ else
131
+ raise ParseError, "unexpected value: #{scanner.string}"
132
+ end
133
+ end
134
+ @q << [:tEOF, false]
135
+ end
136
+
137
+ def next_token
138
+ @q.shift
139
+ end
140
+
141
+ def convert_to_number(scanner)
142
+ matched = scanner.matched
143
+ hexa, uint, number, floating, exp = scanner.captures
144
+
145
+ if !hexa.empty?
146
+ return [:tINT, hexa.to_i(16)]
147
+ end
148
+
149
+ if !uint.empty?
150
+ return [:tUINT, Integer(uint)]
151
+ end
152
+
153
+ if !exp.empty?
154
+ # third matched group, can only be a floating exponential, let's convert tout suite
155
+ [:tDOUBLE, BigDecimal(matched)]
156
+ elsif !floating.empty?
157
+ if number == floating || floating.start_with?(".")
158
+ [:tDOUBLE, Float(matched)]
159
+ elsif number.empty?
160
+ [:tDOUBLE, BigDecimal(matched)]
161
+ end
162
+ else
163
+ if matched[-1].downcase == "u"
164
+ [:tINT, Integer(matched[0..-2]).abs]
165
+ else
166
+ [:tINT, Integer(matched)]
167
+ end
168
+ end
169
+ end
170
+
171
+ TRIPE_DOUBLE_QUOTES = %Q{"""}
172
+ TRIPE_SINGLE_QUOTES = %Q{'''}
173
+
174
+ def convert_to_string(str)
175
+
176
+ if str.start_with?("r")
177
+ # If preceded by an r or R character, the string is a raw string
178
+ # and does not interpret escape sequences.
179
+ str = str.byteslice(2..-2).inspect.byteslice(1..-2)
180
+ return str
181
+ end
182
+
183
+ if str.size > 6 && (
184
+ (str.start_with?(TRIPE_DOUBLE_QUOTES) && str.end_with?(TRIPE_DOUBLE_QUOTES)) ||
185
+ (str.start_with?(TRIPE_SINGLE_QUOTES) && str.end_with?(TRIPE_SINGLE_QUOTES)))
186
+ str = str.byteslice(3..-4)
187
+ else
188
+ str = str.byteslice(1..-2)
189
+ end
190
+
191
+ cleanup_escape_sequences(str)
192
+ end
193
+
194
+ def convert_to_bytes(str)
195
+ str.unpack("C*")
196
+ end
197
+
198
+
199
+ BIN_ESCAPE = /\\([0-3][0-7][0-7])/
200
+ HEX_ESCAPE = /\\x([0-9a-fA-F]{2})/
201
+ BPM_ESCAPE = /\\u([0-9a-fA-F]{4})/
202
+ UNICODE_ESCAPE = /\\u([0-9a-fA-F]{4})/
203
+ WHITESPACE_ESCAPE = /\\([bfnrt"'\\])/
204
+ ESCAPE_UNION = Regexp.union(BIN_ESCAPE, HEX_ESCAPE, BPM_ESCAPE, WHITESPACE_ESCAPE)
205
+ # For the sake of a readable representation, the escape sequences (ESCAPE) are kept
206
+ # implicit in string tokens. This means that strings without the r or R (raw) prefix
207
+ # process ESCAPE sequences, while in strings with the raw prefix they stay uninterpreted.
208
+ # See documentation of string literals below.
209
+ def cleanup_escape_sequences(str)
210
+ str.gsub!(ESCAPE_UNION) do |match|
211
+ case match
212
+ when /\\"/
213
+ "\""
214
+ when BIN_ESCAPE
215
+ # For strings, it denotes the unicode code point.
216
+ Regexp.last_match(1).to_i.chr(Encoding::UTF_8)
217
+ when HEX_ESCAPE
218
+ # For strings, it denotes the unicode code point.
219
+ Regexp.last_match(1).hex.chr(Encoding::UTF_8)
220
+ when BPM_ESCAPE
221
+ Regexp.last_match(1).hex.chr(Encoding::BPM)
222
+ when UNICODE_ESCAPE
223
+ # encoding a Unicode code point. Valid only for string literals.
224
+ Regexp.last_match(1).hex.chr(Encoding::UTF_8)
225
+ when WHITESPACE_ESCAPE
226
+ Regexp.last_match(0)
227
+ end
228
+ end
229
+ str
230
+ end
231
+
232
+ ...end parser.ry/module_eval...
233
+ ##### State transition tables begin ###
234
+
235
+ racc_action_table = [
236
+ 22, 23, 24, 25, 26, 27, 28, 38, 20, 29,
237
+ 39, 13, 40, 38, 32, 4, 39, 31, 40, 12,
238
+ 21, 16, 4, 17, 33, 18, 22, 23, 24, 25,
239
+ 26, 27, 28, 38, 20, 32, 39, 34, 40, 78,
240
+ 35, 36, 35, 36, 37, 42, 21, 16, 47, 17,
241
+ 55, 18, 22, 23, 24, 25, 26, 27, 28, 56,
242
+ 20, 64, 68, 45, 72, 73, 74, 75, 76, 77,
243
+ 33, 34, 21, 16, 37, 17, 37, 18, 22, 23,
244
+ 24, 25, 26, 27, 28, 79, 20, 80, 81, 13,
245
+ 82, 83, 84, 90, 92, 93, 94, 12, 21, 16,
246
+ nil, 17, nil, 18, 22, 23, 24, 25, 26, 27,
247
+ 28, nil, 20, nil, nil, 13, nil, nil, nil, nil,
248
+ nil, nil, nil, 12, 21, 16, nil, 17, nil, 18,
249
+ 22, 23, 24, 25, 26, 27, 28, nil, 20, nil,
250
+ nil, 13, nil, nil, nil, nil, nil, nil, nil, 12,
251
+ 21, 16, nil, 17, nil, 18, 22, 23, 24, 25,
252
+ 26, 27, 28, nil, 20, nil, nil, 13, nil, nil,
253
+ nil, nil, nil, nil, nil, 12, 21, 16, nil, 17,
254
+ nil, 18, 22, 23, 24, 25, 26, 27, 28, nil,
255
+ 20, nil, nil, 13, nil, nil, nil, nil, nil, nil,
256
+ nil, 12, 21, 16, nil, 17, nil, 18, 22, 23,
257
+ 24, 25, 26, 27, 28, nil, 20, nil, nil, 13,
258
+ nil, nil, nil, nil, nil, nil, nil, 12, 21, 16,
259
+ nil, 17, nil, 18, 22, 23, 24, 25, 26, 27,
260
+ 28, nil, 20, nil, nil, 13, nil, nil, nil, nil,
261
+ nil, nil, nil, 12, 21, 16, nil, 17, nil, 18,
262
+ 22, 23, 24, 25, 26, 27, 28, nil, 20, nil,
263
+ nil, 13, nil, nil, nil, nil, nil, nil, nil, 12,
264
+ 21, 16, nil, 17, nil, 18, 22, 23, 24, 25,
265
+ 26, 27, 28, nil, 20, nil, nil, 13, nil, nil,
266
+ nil, nil, nil, nil, nil, 12, 21, 16, nil, 17,
267
+ nil, 18, 22, 23, 24, 25, 26, 27, 28, nil,
268
+ 20, nil, nil, 13, nil, nil, nil, nil, nil, nil,
269
+ nil, 12, 21, 16, nil, 17, nil, 18, 22, 23,
270
+ 24, 25, 26, 27, 28, nil, 20, nil, nil, 13,
271
+ nil, nil, nil, nil, nil, nil, nil, 12, 21, 16,
272
+ nil, 17, nil, 18, 22, 23, 24, 25, 26, 27,
273
+ 28, nil, 20, nil, nil, nil, nil, nil, nil, nil,
274
+ nil, nil, nil, 42, 21, 16, nil, 17, nil, 18,
275
+ 22, 23, 24, 25, 26, 27, 28, nil, 20, nil,
276
+ nil, 45, nil, nil, nil, nil, nil, nil, nil, nil,
277
+ 21, 16, nil, 17, nil, 18, 22, 23, 24, 25,
278
+ 26, 27, 28, nil, 20, nil, nil, 13, nil, nil,
279
+ nil, nil, nil, nil, nil, 12, 21, 16, nil, 17,
280
+ nil, 18, 22, 23, 24, 25, 26, 27, 28, nil,
281
+ 20, nil, nil, 13, nil, nil, nil, nil, nil, nil,
282
+ nil, 12, 21, 16, nil, 17, nil, 18, 22, 23,
283
+ 24, 25, 26, 27, 28, nil, 20, nil, nil, 13,
284
+ nil, nil, nil, nil, nil, nil, nil, 12, 21, 16,
285
+ nil, 17, nil, 18, 22, 23, 24, 25, 26, 27,
286
+ 28, nil, 20, nil, nil, 13, nil, nil, nil, nil,
287
+ nil, nil, nil, 12, 21, 16, nil, 17, nil, 18,
288
+ 22, 23, 24, 25, 26, 27, 28, nil, 20, nil,
289
+ nil, 13, nil, nil, nil, nil, nil, nil, nil, 12,
290
+ 21, 16, nil, 17, nil, 18, 22, 23, 24, 25,
291
+ 26, 27, 28, nil, 20, nil, nil, 13, nil, nil,
292
+ nil, nil, nil, nil, nil, 12, 21, 16, nil, 17,
293
+ nil, 18, 22, 23, 24, 25, 26, 27, 28, nil,
294
+ 20, nil, nil, 13, nil, nil, nil, nil, nil, nil,
295
+ nil, 12, 21, 16, nil, 17, nil, 18, 22, 23,
296
+ 24, 25, 26, 27, 28, nil, 20, nil, nil, 13,
297
+ nil, nil, nil, nil, nil, nil, nil, 12, 21, 16,
298
+ nil, 17, nil, 18, 22, 23, 24, 25, 26, 27,
299
+ 28, nil, 20, nil, nil, 13, nil, nil, nil, nil,
300
+ nil, nil, nil, 12, 21, 16, nil, 17, nil, 18 ]
301
+
302
+ racc_action_check = [
303
+ 0, 0, 0, 0, 0, 0, 0, 11, 0, 1,
304
+ 11, 0, 11, 43, 5, 0, 43, 5, 43, 0,
305
+ 0, 0, 2, 0, 6, 0, 12, 12, 12, 12,
306
+ 12, 12, 12, 46, 12, 57, 46, 7, 46, 57,
307
+ 8, 8, 60, 60, 9, 12, 12, 12, 15, 12,
308
+ 21, 12, 13, 13, 13, 13, 13, 13, 13, 29,
309
+ 13, 38, 40, 13, 48, 49, 50, 52, 53, 54,
310
+ 58, 59, 13, 13, 61, 13, 62, 13, 16, 16,
311
+ 16, 16, 16, 16, 16, 64, 16, 65, 66, 16,
312
+ 67, 68, 71, 82, 86, 89, 90, 16, 16, 16,
313
+ nil, 16, nil, 16, 17, 17, 17, 17, 17, 17,
314
+ 17, nil, 17, nil, nil, 17, nil, nil, nil, nil,
315
+ nil, nil, nil, 17, 17, 17, nil, 17, nil, 17,
316
+ 18, 18, 18, 18, 18, 18, 18, nil, 18, nil,
317
+ nil, 18, nil, nil, nil, nil, nil, nil, nil, 18,
318
+ 18, 18, nil, 18, nil, 18, 31, 31, 31, 31,
319
+ 31, 31, 31, nil, 31, nil, nil, 31, nil, nil,
320
+ nil, nil, nil, nil, nil, 31, 31, 31, nil, 31,
321
+ nil, 31, 32, 32, 32, 32, 32, 32, 32, nil,
322
+ 32, nil, nil, 32, nil, nil, nil, nil, nil, nil,
323
+ nil, 32, 32, 32, nil, 32, nil, 32, 33, 33,
324
+ 33, 33, 33, 33, 33, nil, 33, nil, nil, 33,
325
+ nil, nil, nil, nil, nil, nil, nil, 33, 33, 33,
326
+ nil, 33, nil, 33, 34, 34, 34, 34, 34, 34,
327
+ 34, nil, 34, nil, nil, 34, nil, nil, nil, nil,
328
+ nil, nil, nil, 34, 34, 34, nil, 34, nil, 34,
329
+ 35, 35, 35, 35, 35, 35, 35, nil, 35, nil,
330
+ nil, 35, nil, nil, nil, nil, nil, nil, nil, 35,
331
+ 35, 35, nil, 35, nil, 35, 36, 36, 36, 36,
332
+ 36, 36, 36, nil, 36, nil, nil, 36, nil, nil,
333
+ nil, nil, nil, nil, nil, 36, 36, 36, nil, 36,
334
+ nil, 36, 37, 37, 37, 37, 37, 37, 37, nil,
335
+ 37, nil, nil, 37, nil, nil, nil, nil, nil, nil,
336
+ nil, 37, 37, 37, nil, 37, nil, 37, 39, 39,
337
+ 39, 39, 39, 39, 39, nil, 39, nil, nil, 39,
338
+ nil, nil, nil, nil, nil, nil, nil, 39, 39, 39,
339
+ nil, 39, nil, 39, 42, 42, 42, 42, 42, 42,
340
+ 42, nil, 42, nil, nil, nil, nil, nil, nil, nil,
341
+ nil, nil, nil, 42, 42, 42, nil, 42, nil, 42,
342
+ 45, 45, 45, 45, 45, 45, 45, nil, 45, nil,
343
+ nil, 45, nil, nil, nil, nil, nil, nil, nil, nil,
344
+ 45, 45, nil, 45, nil, 45, 47, 47, 47, 47,
345
+ 47, 47, 47, nil, 47, nil, nil, 47, nil, nil,
346
+ nil, nil, nil, nil, nil, 47, 47, 47, nil, 47,
347
+ nil, 47, 74, 74, 74, 74, 74, 74, 74, nil,
348
+ 74, nil, nil, 74, nil, nil, nil, nil, nil, nil,
349
+ nil, 74, 74, 74, nil, 74, nil, 74, 76, 76,
350
+ 76, 76, 76, 76, 76, nil, 76, nil, nil, 76,
351
+ nil, nil, nil, nil, nil, nil, nil, 76, 76, 76,
352
+ nil, 76, nil, 76, 77, 77, 77, 77, 77, 77,
353
+ 77, nil, 77, nil, nil, 77, nil, nil, nil, nil,
354
+ nil, nil, nil, 77, 77, 77, nil, 77, nil, 77,
355
+ 78, 78, 78, 78, 78, 78, 78, nil, 78, nil,
356
+ nil, 78, nil, nil, nil, nil, nil, nil, nil, 78,
357
+ 78, 78, nil, 78, nil, 78, 79, 79, 79, 79,
358
+ 79, 79, 79, nil, 79, nil, nil, 79, nil, nil,
359
+ nil, nil, nil, nil, nil, 79, 79, 79, nil, 79,
360
+ nil, 79, 83, 83, 83, 83, 83, 83, 83, nil,
361
+ 83, nil, nil, 83, nil, nil, nil, nil, nil, nil,
362
+ nil, 83, 83, 83, nil, 83, nil, 83, 92, 92,
363
+ 92, 92, 92, 92, 92, nil, 92, nil, nil, 92,
364
+ nil, nil, nil, nil, nil, nil, nil, 92, 92, 92,
365
+ nil, 92, nil, 92, 94, 94, 94, 94, 94, 94,
366
+ 94, nil, 94, nil, nil, 94, nil, nil, nil, nil,
367
+ nil, nil, nil, 94, 94, 94, nil, 94, nil, 94 ]
368
+
369
+ racc_action_pointer = [
370
+ -2, 9, 5, nil, nil, -2, 9, 23, 28, 33,
371
+ nil, -15, 24, 50, nil, 25, 76, 102, 128, nil,
372
+ nil, 40, nil, nil, nil, nil, nil, nil, nil, 59,
373
+ nil, 154, 180, 206, 232, 258, 284, 310, 51, 336,
374
+ 52, nil, 362, -9, nil, 388, 11, 414, 40, 39,
375
+ 37, nil, 39, 39, 49, nil, nil, 19, 55, 57,
376
+ 30, 63, 65, nil, 62, 61, 60, 61, 71, nil,
377
+ nil, 68, nil, nil, 440, nil, 466, 492, 518, 544,
378
+ nil, nil, 83, 570, nil, nil, 74, nil, nil, 71,
379
+ 76, nil, 596, nil, 622, nil, nil ]
380
+
381
+ racc_action_default = [
382
+ -56, -56, -56, -2, -3, -5, -7, -9, -11, -14,
383
+ -16, -17, -56, -56, -24, -29, -56, -38, -39, -34,
384
+ -35, -56, -49, -50, -51, -52, -53, -54, -55, -56,
385
+ -1, -56, -56, -56, -56, -56, -56, -56, -56, -56,
386
+ -43, -18, -56, -21, -19, -56, -23, -38, -56, -56,
387
+ -37, -42, -56, -40, -56, -36, 97, -56, -6, -8,
388
+ -10, -12, -13, -15, -25, -56, -56, -44, -56, -20,
389
+ -22, -56, -31, -32, -56, -33, -56, -56, -56, -38,
390
+ -27, -28, -56, -56, -30, -41, -56, -48, -4, -56,
391
+ -56, -46, -56, -26, -56, -47, -45 ]
392
+
393
+ racc_goto_table = [
394
+ 2, 49, 43, 46, 44, 1, 3, 41, 30, 61,
395
+ 62, 57, 58, 59, 60, 63, 48, 66, 54, 52,
396
+ 53, 67, nil, nil, nil, nil, nil, nil, nil, nil,
397
+ nil, 71, 43, nil, nil, 46, 70, 69, nil, 65,
398
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
399
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
400
+ nil, nil, nil, 89, nil, nil, nil, nil, nil, nil,
401
+ nil, nil, nil, nil, 85, nil, 86, 87, 88, nil,
402
+ nil, nil, nil, 91, nil, nil, nil, nil, nil, nil,
403
+ nil, nil, 95, nil, 96 ]
404
+
405
+ racc_goto_check = [
406
+ 2, 14, 10, 10, 12, 1, 3, 11, 3, 8,
407
+ 8, 4, 5, 6, 7, 9, 2, 15, 2, 17,
408
+ 20, 21, nil, nil, nil, nil, nil, nil, nil, nil,
409
+ nil, 14, 10, nil, nil, 10, 12, 11, nil, 2,
410
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
411
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
412
+ nil, nil, nil, 14, nil, nil, nil, nil, nil, nil,
413
+ nil, nil, nil, nil, 2, nil, 2, 2, 2, nil,
414
+ nil, nil, nil, 2, nil, nil, nil, nil, nil, nil,
415
+ nil, nil, 2, nil, 2 ]
416
+
417
+ racc_goto_pointer = [
418
+ nil, 5, 0, 6, -20, -20, -20, -20, -26, -22,
419
+ -10, -5, -9, nil, -16, -23, nil, 1, nil, nil,
420
+ 2, -19 ]
421
+
422
+ racc_goto_default = [
423
+ nil, nil, 51, nil, 5, 6, 7, 8, 9, 10,
424
+ 11, nil, nil, 14, nil, nil, 15, nil, 19, 50,
425
+ nil, nil ]
426
+
427
+ racc_reduce_table = [
428
+ 0, 0, :racc_error,
429
+ 2, 31, :_reduce_none,
430
+ 1, 31, :_reduce_none,
431
+ 1, 33, :_reduce_none,
432
+ 5, 32, :_reduce_4,
433
+ 1, 32, :_reduce_none,
434
+ 3, 34, :_reduce_6,
435
+ 1, 34, :_reduce_none,
436
+ 3, 35, :_reduce_8,
437
+ 1, 35, :_reduce_none,
438
+ 3, 36, :_reduce_10,
439
+ 1, 36, :_reduce_none,
440
+ 3, 37, :_reduce_12,
441
+ 3, 37, :_reduce_13,
442
+ 1, 37, :_reduce_none,
443
+ 3, 38, :_reduce_15,
444
+ 1, 38, :_reduce_none,
445
+ 1, 39, :_reduce_none,
446
+ 2, 39, :_reduce_18,
447
+ 2, 39, :_reduce_19,
448
+ 2, 41, :_reduce_20,
449
+ 1, 41, :_reduce_none,
450
+ 2, 42, :_reduce_22,
451
+ 1, 42, :_reduce_none,
452
+ 1, 40, :_reduce_none,
453
+ 3, 40, :_reduce_25,
454
+ 6, 40, :_reduce_26,
455
+ 4, 40, :_reduce_27,
456
+ 4, 40, :_reduce_28,
457
+ 1, 43, :_reduce_29,
458
+ 4, 43, :_reduce_30,
459
+ 3, 43, :_reduce_31,
460
+ 3, 43, :_reduce_32,
461
+ 3, 43, :_reduce_33,
462
+ 1, 43, :_reduce_none,
463
+ 1, 46, :_reduce_none,
464
+ 2, 46, :_reduce_none,
465
+ 1, 44, :_reduce_none,
466
+ 0, 44, :_reduce_38,
467
+ 0, 47, :_reduce_39,
468
+ 1, 47, :_reduce_none,
469
+ 3, 49, :_reduce_41,
470
+ 1, 49, :_reduce_42,
471
+ 0, 45, :_reduce_43,
472
+ 1, 45, :_reduce_none,
473
+ 5, 51, :_reduce_none,
474
+ 3, 51, :_reduce_46,
475
+ 5, 50, :_reduce_47,
476
+ 3, 50, :_reduce_48,
477
+ 1, 48, :_reduce_49,
478
+ 1, 48, :_reduce_50,
479
+ 1, 48, :_reduce_51,
480
+ 1, 48, :_reduce_52,
481
+ 1, 48, :_reduce_53,
482
+ 1, 48, :_reduce_54,
483
+ 1, 48, :_reduce_55 ]
484
+
485
+ racc_reduce_n = 56
486
+
487
+ racc_shift_n = 97
488
+
489
+ racc_token_table = {
490
+ false => 0,
491
+ :error => 1,
492
+ :tINT => 2,
493
+ :tUINT => 3,
494
+ :tDOUBLE => 4,
495
+ :tBOOL => 5,
496
+ :tNULL => 6,
497
+ :tSTRING => 7,
498
+ :tBYTES => 8,
499
+ :tRESERVED => 9,
500
+ :tIDENTIFIER => 10,
501
+ :tMULTIOP => 11,
502
+ :tADDOP => 12,
503
+ :tSUBOP => 13,
504
+ :tRELOP => 14,
505
+ :tANDOP => 15,
506
+ :tOROP => 16,
507
+ :tEOF => 17,
508
+ :UMINUS => 18,
509
+ "?" => 19,
510
+ ":" => 20,
511
+ "!" => 21,
512
+ "." => 22,
513
+ "(" => 23,
514
+ ")" => 24,
515
+ "[" => 25,
516
+ "]" => 26,
517
+ "{" => 27,
518
+ "}" => 28,
519
+ "," => 29 }
520
+
521
+ racc_nt_base = 30
522
+
523
+ racc_use_result_var = true
524
+
525
+ Racc_arg = [
526
+ racc_action_table,
527
+ racc_action_check,
528
+ racc_action_default,
529
+ racc_action_pointer,
530
+ racc_goto_table,
531
+ racc_goto_check,
532
+ racc_goto_default,
533
+ racc_goto_pointer,
534
+ racc_nt_base,
535
+ racc_reduce_table,
536
+ racc_token_table,
537
+ racc_shift_n,
538
+ racc_reduce_n,
539
+ racc_use_result_var ]
540
+
541
+ Racc_token_to_s_table = [
542
+ "$end",
543
+ "error",
544
+ "tINT",
545
+ "tUINT",
546
+ "tDOUBLE",
547
+ "tBOOL",
548
+ "tNULL",
549
+ "tSTRING",
550
+ "tBYTES",
551
+ "tRESERVED",
552
+ "tIDENTIFIER",
553
+ "tMULTIOP",
554
+ "tADDOP",
555
+ "tSUBOP",
556
+ "tRELOP",
557
+ "tANDOP",
558
+ "tOROP",
559
+ "tEOF",
560
+ "UMINUS",
561
+ "\"?\"",
562
+ "\":\"",
563
+ "\"!\"",
564
+ "\".\"",
565
+ "\"(\"",
566
+ "\")\"",
567
+ "\"[\"",
568
+ "\"]\"",
569
+ "\"{\"",
570
+ "\"}\"",
571
+ "\",\"",
572
+ "$start",
573
+ "target",
574
+ "expr",
575
+ "eof",
576
+ "conditional_or",
577
+ "conditional_and",
578
+ "relation",
579
+ "addition",
580
+ "multiplication",
581
+ "unary",
582
+ "member",
583
+ "negated_member",
584
+ "negative_member",
585
+ "primary",
586
+ "maybe_expr_list",
587
+ "maybe_field_inits",
588
+ "identifier",
589
+ "maybe_map_inits",
590
+ "literal",
591
+ "expr_list",
592
+ "map_inits",
593
+ "field_inits" ]
594
+
595
+ Racc_debug_parser = false
596
+
597
+ ##### State transition tables end #####
598
+
599
+ # reduce 0 omitted
600
+
601
+ # reduce 1 omitted
602
+
603
+ # reduce 2 omitted
604
+
605
+ # reduce 3 omitted
606
+
607
+ module_eval(<<'.,.,', 'parser.ry', 17)
608
+ def _reduce_4(val, _values, result)
609
+ result = Cel::Condition.new(val[0], val[2], val[4])
610
+ result
611
+ end
612
+ .,.,
613
+
614
+ # reduce 5 omitted
615
+
616
+ module_eval(<<'.,.,', 'parser.ry', 20)
617
+ def _reduce_6(val, _values, result)
618
+ result = Cel::Operation.new(val[1], [val[0], val[2]])
619
+ result
620
+ end
621
+ .,.,
622
+
623
+ # reduce 7 omitted
624
+
625
+ module_eval(<<'.,.,', 'parser.ry', 23)
626
+ def _reduce_8(val, _values, result)
627
+ result = Cel::Operation.new(val[1], [val[0], val[2]])
628
+ result
629
+ end
630
+ .,.,
631
+
632
+ # reduce 9 omitted
633
+
634
+ module_eval(<<'.,.,', 'parser.ry', 26)
635
+ def _reduce_10(val, _values, result)
636
+ result = Cel::Operation.new(val[1], [val[0], val[2]])
637
+ result
638
+ end
639
+ .,.,
640
+
641
+ # reduce 11 omitted
642
+
643
+ module_eval(<<'.,.,', 'parser.ry', 29)
644
+ def _reduce_12(val, _values, result)
645
+ result = Cel::Operation.new(val[1], [val[0], val[2]])
646
+ result
647
+ end
648
+ .,.,
649
+
650
+ module_eval(<<'.,.,', 'parser.ry', 30)
651
+ def _reduce_13(val, _values, result)
652
+ result = Cel::Operation.new(val[1], [val[0], val[2]])
653
+ result
654
+ end
655
+ .,.,
656
+
657
+ # reduce 14 omitted
658
+
659
+ module_eval(<<'.,.,', 'parser.ry', 33)
660
+ def _reduce_15(val, _values, result)
661
+ result = Cel::Operation.new(val[1], [val[0], val[2]])
662
+ result
663
+ end
664
+ .,.,
665
+
666
+ # reduce 16 omitted
667
+
668
+ # reduce 17 omitted
669
+
670
+ module_eval(<<'.,.,', 'parser.ry', 38)
671
+ def _reduce_18(val, _values, result)
672
+ result = Cel::Operation.new("!", [val[1]])
673
+ result
674
+ end
675
+ .,.,
676
+
677
+ module_eval(<<'.,.,', 'parser.ry', 39)
678
+ def _reduce_19(val, _values, result)
679
+ result = Cel::Operation.new("-", [val[1]])
680
+ result
681
+ end
682
+ .,.,
683
+
684
+ module_eval(<<'.,.,', 'parser.ry', 41)
685
+ def _reduce_20(val, _values, result)
686
+ result = Cel::Operation.new("!", [val[1]])
687
+ result
688
+ end
689
+ .,.,
690
+
691
+ # reduce 21 omitted
692
+
693
+ module_eval(<<'.,.,', 'parser.ry', 44)
694
+ def _reduce_22(val, _values, result)
695
+ result = Cel::Operation.new("-", [val[1]])
696
+ result
697
+ end
698
+ .,.,
699
+
700
+ # reduce 23 omitted
701
+
702
+ # reduce 24 omitted
703
+
704
+ module_eval(<<'.,.,', 'parser.ry', 48)
705
+ def _reduce_25(val, _values, result)
706
+ result = Cel::Invoke.new(var: val[0], func: val[2])
707
+ result
708
+ end
709
+ .,.,
710
+
711
+ module_eval(<<'.,.,', 'parser.ry', 49)
712
+ def _reduce_26(val, _values, result)
713
+ result = Cel::Invoke.new(var: val[0], func: val[2], args: [val[4]].flatten(1))
714
+ result
715
+ end
716
+ .,.,
717
+
718
+ module_eval(<<'.,.,', 'parser.ry', 50)
719
+ def _reduce_27(val, _values, result)
720
+ result = Cel::Invoke.new(var: val[0], func: "[]", args: val[2])
721
+ result
722
+ end
723
+ .,.,
724
+
725
+ module_eval(<<'.,.,', 'parser.ry', 51)
726
+ def _reduce_28(val, _values, result)
727
+ result = Cel::Message.new(val[0], val[2])
728
+ result
729
+ end
730
+ .,.,
731
+
732
+ module_eval(<<'.,.,', 'parser.ry', 54)
733
+ def _reduce_29(val, _values, result)
734
+ result = Cel::Identifier.new(val[0])
735
+ result
736
+ end
737
+ .,.,
738
+
739
+ module_eval(<<'.,.,', 'parser.ry', 55)
740
+ def _reduce_30(val, _values, result)
741
+ result = Cel::Invoke.new(func: val[0], args: [val[2]].flatten(1))
742
+ result
743
+ end
744
+ .,.,
745
+
746
+ module_eval(<<'.,.,', 'parser.ry', 56)
747
+ def _reduce_31(val, _values, result)
748
+ result = Cel::Group.new(val[1])
749
+ result
750
+ end
751
+ .,.,
752
+
753
+ module_eval(<<'.,.,', 'parser.ry', 57)
754
+ def _reduce_32(val, _values, result)
755
+ result = Cel::List.new(Array(val[1]))
756
+ result
757
+ end
758
+ .,.,
759
+
760
+ module_eval(<<'.,.,', 'parser.ry', 58)
761
+ def _reduce_33(val, _values, result)
762
+ result = Cel::Map.new(Hash[val[1]])
763
+ result
764
+ end
765
+ .,.,
766
+
767
+ # reduce 34 omitted
768
+
769
+ # reduce 35 omitted
770
+
771
+ # reduce 36 omitted
772
+
773
+ # reduce 37 omitted
774
+
775
+ module_eval(<<'.,.,', 'parser.ry', 65)
776
+ def _reduce_38(val, _values, result)
777
+ result = nil
778
+ result
779
+ end
780
+ .,.,
781
+
782
+ module_eval(<<'.,.,', 'parser.ry', 67)
783
+ def _reduce_39(val, _values, result)
784
+ result = nil
785
+ result
786
+ end
787
+ .,.,
788
+
789
+ # reduce 40 omitted
790
+
791
+ module_eval(<<'.,.,', 'parser.ry', 70)
792
+ def _reduce_41(val, _values, result)
793
+ result = Array(val[0]) << val[2]
794
+ result
795
+ end
796
+ .,.,
797
+
798
+ module_eval(<<'.,.,', 'parser.ry', 71)
799
+ def _reduce_42(val, _values, result)
800
+ [val[0]]
801
+ result
802
+ end
803
+ .,.,
804
+
805
+ module_eval(<<'.,.,', 'parser.ry', 73)
806
+ def _reduce_43(val, _values, result)
807
+ result = nil
808
+ result
809
+ end
810
+ .,.,
811
+
812
+ # reduce 44 omitted
813
+
814
+ # reduce 45 omitted
815
+
816
+ module_eval(<<'.,.,', 'parser.ry', 77)
817
+ def _reduce_46(val, _values, result)
818
+ result = { Cel::Identifier.new(val[0]) => val[2] }
819
+ result
820
+ end
821
+ .,.,
822
+
823
+ module_eval(<<'.,.,', 'parser.ry', 79)
824
+ def _reduce_47(val, _values, result)
825
+ val[0][val[2]] = val[4]; result = val[0]
826
+ result
827
+ end
828
+ .,.,
829
+
830
+ module_eval(<<'.,.,', 'parser.ry', 80)
831
+ def _reduce_48(val, _values, result)
832
+ result = { val[0] => val[2] }
833
+ result
834
+ end
835
+ .,.,
836
+
837
+ module_eval(<<'.,.,', 'parser.ry', 82)
838
+ def _reduce_49(val, _values, result)
839
+ result = Cel::Number.new(:int, val[0])
840
+ result
841
+ end
842
+ .,.,
843
+
844
+ module_eval(<<'.,.,', 'parser.ry', 83)
845
+ def _reduce_50(val, _values, result)
846
+ result = Cel::Number.new(:uint, val[0])
847
+ result
848
+ end
849
+ .,.,
850
+
851
+ module_eval(<<'.,.,', 'parser.ry', 84)
852
+ def _reduce_51(val, _values, result)
853
+ result = Cel::Number.new(:double, val[0])
854
+ result
855
+ end
856
+ .,.,
857
+
858
+ module_eval(<<'.,.,', 'parser.ry', 85)
859
+ def _reduce_52(val, _values, result)
860
+ result = Cel::Bool.new(val[0])
861
+ result
862
+ end
863
+ .,.,
864
+
865
+ module_eval(<<'.,.,', 'parser.ry', 86)
866
+ def _reduce_53(val, _values, result)
867
+ result = Cel::Null.new()
868
+ result
869
+ end
870
+ .,.,
871
+
872
+ module_eval(<<'.,.,', 'parser.ry', 87)
873
+ def _reduce_54(val, _values, result)
874
+ result = Cel::String.new(val[0])
875
+ result
876
+ end
877
+ .,.,
878
+
879
+ module_eval(<<'.,.,', 'parser.ry', 88)
880
+ def _reduce_55(val, _values, result)
881
+ result = Cel::Bytes.new(val[0])
882
+ result
883
+ end
884
+ .,.,
885
+
886
+ def _reduce_none(val, _values, result)
887
+ val[0]
888
+ end
889
+
890
+ end # class Parser
891
+ end # module Cel
892
+
893
+
894
+ # if $0 == __FILE__
895
+ # examples = <<EOS
896
+ # 123
897
+ # 12345
898
+ # 1.2
899
+ # 1e2
900
+ # -1.2e2
901
+ # 12u
902
+ # 0xa123
903
+ # ""
904
+ # '""'
905
+ # '''x''x'''
906
+ # "\""
907
+ # "\\"
908
+ # r"\\"
909
+ # b"abc"
910
+ # b"ÿ"
911
+ # b"\303\277"
912
+ # "\303\277"
913
+ # "\377"
914
+ # b"\377"
915
+ # "\xFF"
916
+ # b"\xFF"
917
+
918
+ # 1 + 2
919
+ # 3 - 2
920
+ # " Some String with \"escapes\""
921
+ # 'another string'
922
+ # a.b.c == 1
923
+ # d > 2
924
+ # a.b.c * 3 == 1 && d > 2
925
+ # a.b.c
926
+ # wiri
927
+ # // a.driving_license = "CA"
928
+ # // 1 = 2
929
+ # // 2 = "a"
930
+ # // a.b.c > "a"
931
+ # EOS
932
+ # puts 'Parsing...'
933
+ # parser = Cel::Parser.new
934
+ # examples.each_line do |line|
935
+ # puts "line: #{line.inspect}"
936
+ # puts parser.parse(line)
937
+ # end
938
+ # end
939
+
940
+ # The grammar of CEL is defined below, using | for alternatives, [] for optional, {} for repeated, and () for grouping.
941
+ # Expr = ConditionalOr ["?" ConditionalOr ":" Expr] ;
942
+ # ConditionalOr = [ConditionalOr "||"] ConditionalAnd ;
943
+ # ConditionalAnd = [ConditionalAnd "&&"] Relation ;
944
+ # Relation = [Relation Relop] Addition ;
945
+ # Relop = "<" | "<=" | ">=" | ">" | "==" | "!=" | "in" ;
946
+ # Addition = [Addition ("+" | "-")] Multiplication ;
947
+ # Multiplication = [Multiplication ("*" | "/" | "%")] Unary ;
948
+ # Unary = Member
949
+ # | "!" {"!"} Member
950
+ # | "-" {"-"} Member
951
+ # ;
952
+ # Member = Primary
953
+ # | Member "." IDENT ["(" [ExprList] ")"]
954
+ # | Member "[" Expr "]"
955
+ # | Member "{" [FieldInits] "}"
956
+ # ;
957
+ # Primary = ["."] IDENT ["(" [ExprList] ")"]
958
+ # | "(" Expr ")"
959
+ # | "[" [ExprList] "]"
960
+ # | "{" [MapInits] "}"
961
+ # | LITERAL
962
+ # ;
963
+ # ExprList = Expr {"," Expr} ;
964
+ # FieldInits = IDENT ":" Expr {"," IDENT ":" Expr} ;
965
+ # MapInits = Expr ":" Expr {"," Expr ":" Expr} ;
966
+
967
+ # IDENT ::= [_a-zA-Z][_a-zA-Z0-9]* - RESERVED
968
+ # LITERAL ::= INT_LIT | UINT_LIT | FLOAT_LIT | STRING_LIT | BYTES_LIT
969
+ # | BOOL_LIT | NULL_LIT
970
+ # INT_LIT ::= -? DIGIT+ | -? 0x HEXDIGIT+
971
+ # UINT_LIT ::= INT_LIT [uU]
972
+ # FLOAT_LIT ::= -? DIGIT* . DIGIT+ EXPONENT? | -? DIGIT+ EXPONENT
973
+ # DIGIT ::= [0-9]
974
+ # HEXDIGIT ::= [0-9abcdefABCDEF]
975
+ # EXPONENT ::= [eE] [+-]? DIGIT+
976
+ # STRING_LIT ::= [rR]? ( " ~( " | NEWLINE )* "
977
+ # | ' ~( ' | NEWLINE )* '
978
+ # | """ ~"""* """
979
+ # | ''' ~'''* '''
980
+ # )
981
+ # BYTES_LIT ::= [bB] STRING_LIT
982
+ # ESCAPE ::= \ [bfnrt"'\]
983
+ # | \ x HEXDIGIT HEXDIGIT
984
+ # | \ u HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT
985
+ # | \ U HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT
986
+ # | \ [0-3] [0-7] [0-7]
987
+ # NEWLINE ::= \r\n | \r | \n
988
+ # BOOL_LIT ::= "true" | "false"
989
+ # NULL_LIT ::= "null"
990
+ # RESERVED ::= BOOL_LIT | NULL_LIT | "in"
991
+ # | "as" | "break" | "const" | "continue" | "else"
992
+ # | "for" | "function" | "if" | "import" | "let"
993
+ # | "loop" | "package" | "namespace" | "return"
994
+ # | "var" | "void" | "while"
995
+ # WHITESPACE ::= [\t\n\f\r ]+
996
+ # COMMENT ::= '//' ~NEWLINE* NEWLINE