rgviz 0.38 → 0.39

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rgviz/lexer.rb +99 -600
  2. metadata +4 -4
data/lib/rgviz/lexer.rb CHANGED
@@ -1,615 +1,114 @@
1
+ require 'strscan'
2
+
1
3
  module Rgviz
2
- class Lexer
4
+ class Lexer < StringScanner
3
5
  def initialize(str, options = {})
4
- @str = (str || '').scan(/./)
5
- @len = @str.length
6
+ super
6
7
  @token = Token.new
7
8
  @extensions = options[:extensions]
8
- @p = 0
9
9
  end
10
10
 
11
11
  def next_token
12
- while true
13
- @token.start = @p
14
- @token.string = nil
12
+ @token.start = pos
13
+ @token.string = nil
14
+ @token.value = nil
15
15
 
16
- case c
17
- when nil
18
- @token.value = Token::EOF
19
- return @token
20
- when ' ', "\n", "\r", "\t"
21
- @p += 1
22
- next
23
- when '`', '"', "'"
24
- ending = c
25
- while c! != ending && c != nil
26
- end
27
- raise ParseException.new("Unmatched #{char}") if c != ending
28
- @p += 1
29
- @token.value = ending == '`' ? Token::ID : Token::STRING
30
- @token.string = @str[@token.start + 1 ... @p - 1].join
31
- return @token
32
- when '0' .. '9'
33
- found_dot = false
34
- while true
35
- case c!
36
- when '0' .. '9'
37
- next
38
- when '.'
39
- break if found_dot
40
- found_dot = true
41
- next
42
- else
43
- break
44
- end
45
- end
46
- if found_dot
47
- @token.value = Token::DECIMAL
48
- @token.number = @str[@token.start ... @p].join.to_f
49
- else
50
- @token.value = Token::INTEGER
51
- @token.number = @str[@token.start ... @p].join.to_i
52
- end
53
- return @token
54
- when '.'
55
- while true
56
- case c!
57
- when '0' .. '9'
58
- next
59
- else
60
- break
61
- end
62
- end
63
- @token.value = Token::DECIMAL
64
- @token.number = @str[@token.start ... @p].join.to_f
65
- return @token
66
- when '+'; return keyword!(Token::PLUS)
67
- when '-'; return keyword!(Token::MINUS)
68
- when '*'; return keyword!(Token::STAR)
69
- when '/'; return keyword!(Token::SLASH)
70
- when ','; return keyword!(Token::COMMA)
71
- when '('; return keyword!(Token::LPAREN)
72
- when ')'; return keyword!(Token::RPAREN)
73
- when '='; return keyword!(Token::EQ)
74
- when '!';
75
- raise ParseException.new("Unexpected character '!'") if c! != '='
76
- return keyword!(Token::NEQ)
77
- when '<';
78
- case c!
79
- when '='
80
- @p += 1
81
- @token.value = Token::LTE
82
- return @token
83
- when '>'
84
- @p += 1
85
- @token.value = Token::NEQ
86
- return @token
87
- else
88
- @token.value = Token::LT
89
- return @token
90
- end
91
- when '>';
92
- if c! == '='
93
- @p += 1
94
- @token.value = Token::GTE
95
- return @token
96
- end
97
- @token.value = Token::GT
98
- return @token
99
- when 'a', 'A'
100
- case c!
101
- when 'n', 'N'
102
- case c! when 'd', 'D'
103
- return keyword(Token::And) if end_of_identifier!
104
- end
105
- when 's', 'S'
106
- case c! when 'c', 'C'
107
- return keyword(Token::Asc) if end_of_identifier!
108
- end
109
- when 'v', 'V'
110
- case c! when 'g', 'G'
111
- return keyword(Token::Avg) if end_of_identifier!
112
- end
113
- end
114
- when 'b', 'B'
115
- case c! when 'y', 'Y'
116
- return keyword(Token::By) if end_of_identifier!
117
- end
118
- when 'c', 'C'
119
- case c! when 'o', 'O'
120
- case c!
121
- when 'n', 'N'
122
- case c!
123
- when 'c', 'C'
124
- if @extensions
125
- case c! when 'a', 'A'
126
- case c! when 't', 'T'
127
- return keyword(Token::Concat) if end_of_identifier!
128
- end
129
- end
130
- end
131
- when 't', 'T'
132
- case c! when 'a', 'A'
133
- case c! when 'i', 'I'
134
- case c! when 'n', 'N'
135
- case c! when 's', 'S'
136
- return keyword(Token::Contains) if end_of_identifier!
137
- end
138
- end
139
- end
140
- end
141
- end
142
- when 'u', 'U'
143
- case c! when 'n', 'N'
144
- case c! when 't', 'T'
145
- return keyword(Token::Count) if end_of_identifier!
146
- end
147
- end
148
- end
149
- end
150
- when 'd', 'D'
151
- case c!
152
- when 'a', 'A'
153
- case c!
154
- when 't', 'T'
155
- case c! when 'e', 'E'
156
- return keyword(Token::Date) if end_of_identifier!
157
- case c
158
- when 'd', 'D'
159
- case c! when 'i', 'I'
160
- case c! when 'f', 'F'
161
- case c! when 'f', 'F'
162
- return keyword(Token::DateDiff) if end_of_identifier!
163
- end
164
- end
165
- end
166
- when 't', 'T'
167
- case c! when 'i', 'I'
168
- case c! when 'm', 'M'
169
- case c! when 'e', 'E'
170
- return keyword(Token::DateTime) if end_of_identifier!
171
- end
172
- end
173
- end
174
- end
175
- end
176
- when 'y', 'Y'
177
- return keyword(Token::Day) if end_of_identifier!
178
- case c when 'o', 'O'
179
- case c! when 'f', 'F'
180
- case c! when 'w', 'W'
181
- case c! when 'e', 'E'
182
- case c! when 'e', 'E'
183
- case c! when 'k', 'K'
184
- return keyword(Token::DayOfWeek) if end_of_identifier!
185
- end
186
- end
187
- end
188
- end
189
- end
190
- end
191
- end
192
- when 'e', 'E'
193
- case c! when 's', 'S'
194
- case c! when 'c', 'C'
195
- return keyword(Token::Desc) if end_of_identifier!
196
- end
197
- end
198
- end
199
- when 'e', 'E'
200
- case c! when 'n', 'N'
201
- case c! when 'd', 'D'
202
- case c! when 's', 'S'
203
- return keyword(Token::Ends) if end_of_identifier!
204
- end
205
- end
206
- end
207
- when 'f', 'F'
208
- case c!
209
- when 'a', 'A'
210
- case c! when 'l', 'L'
211
- case c! when 's', 'S'
212
- case c! when 'e', 'E'
213
- return keyword(Token::False) if end_of_identifier!
214
- end
215
- end
216
- end
217
- when 'o', 'O'
218
- case c! when 'r', 'R'
219
- case c! when 'm', 'M'
220
- case c! when 'a', 'A'
221
- case c! when 't', 'T'
222
- return keyword(Token::Format) if end_of_identifier!
223
- end
224
- end
225
- end
226
- end
227
- end
228
- when 'g', 'G'
229
- case c! when 'r', 'R'
230
- case c! when 'o', 'O'
231
- case c! when 'u', 'U'
232
- case c! when 'p', 'P'
233
- return keyword(Token::Group) if end_of_identifier!
234
- end
235
- end
236
- end
237
- end
238
- when 'h', 'H'
239
- case c! when 'o', 'O'
240
- case c! when 'u', 'U'
241
- case c! when 'r', 'R'
242
- return keyword(Token::Hour) if end_of_identifier!
243
- end
244
- end
245
- end
246
- when 'i', 'I'
247
- case c! when 's', 'S'
248
- return keyword(Token::Is) if end_of_identifier!
249
- end
250
- when 'l', 'L'
251
- case c!
252
- when 'a', 'A'
253
- case c! when 'b', 'B'
254
- case c! when 'e', 'E'
255
- case c! when 'l', 'L'
256
- return keyword(Token::Label) if end_of_identifier!
257
- end
258
- end
259
- end
260
- when 'i', 'I'
261
- case c!
262
- when 'k', 'K'
263
- case c! when 'e', 'E'
264
- return keyword(Token::Like) if end_of_identifier!
265
- end
266
- when 'm', 'M'
267
- case c! when 'i', 'I'
268
- case c! when 't', 'T'
269
- return keyword(Token::Limit) if end_of_identifier!
270
- end
271
- end
272
- end
273
- when 'o', 'O'
274
- case c! when 'w', 'W'
275
- case c! when 'e', 'E'
276
- case c! when 'r', 'R'
277
- return keyword(Token::Lower) if end_of_identifier!
278
- end
279
- end
280
- end
281
- end
282
- when 'm', 'M'
283
- case c!
284
- when 'a', 'A'
285
- case c!
286
- when 't', 'T'
287
- case c! when 'c', 'C'
288
- case c! when 'h', 'H'
289
- case c! when 'e', 'E'
290
- case c! when 's', 'S'
291
- return keyword(Token::Matches) if end_of_identifier!
292
- end
293
- end
294
- end
295
- end
296
- when 'x', 'X'
297
- return keyword(Token::Max) if end_of_identifier!
298
- end
299
- when 'i', 'I'
300
- case c! when 'l', 'L'
301
- case c! when 'l', 'L'
302
- case c! when 'i', 'I'
303
- case c! when 's', 'S'
304
- case c! when 'e', 'E'
305
- case c! when 'c', 'C'
306
- case c! when 'o', 'O'
307
- case c! when 'n', 'N'
308
- case c! when 'd', 'D'
309
- return keyword(Token::Millisecond) if end_of_identifier!
310
- end
311
- end
312
- end
313
- end
314
- end
315
- end
316
- end
317
- end
318
- when 'n', 'N'
319
- return keyword(Token::Min) if end_of_identifier!
320
- case c when 'u', 'U'
321
- case c! when 't', 'T'
322
- case c! when 'e', 'E'
323
- return keyword(Token::Minute) if end_of_identifier!
324
- end
325
- end
326
- end
327
- end
328
- when 'o', 'O'
329
- case c! when 'n', 'N'
330
- case c! when 't', 'T'
331
- case c! when 'h', 'H'
332
- return keyword(Token::Month) if end_of_identifier!
333
- end
334
- end
335
- end
336
- end
337
- when 'n', 'N'
338
- case c!
339
- when 'o', 'O'
340
- case c!
341
- when 't', 'T'
342
- return keyword(Token::Not) if end_of_identifier!
343
- when 'w', 'W'
344
- return keyword(Token::Now) if end_of_identifier!
345
- when '_'
346
- case c!
347
- when 'f', 'F'
348
- case c! when 'o', 'O'
349
- case c! when 'r', 'R'
350
- case c! when 'm', 'M'
351
- case c! when 'a', 'A'
352
- case c! when 't', 'T'
353
- return keyword(Token::NoFormat) if end_of_identifier!
354
- end
355
- end
356
- end
357
- end
358
- end
359
- when 'v', 'V'
360
- case c! when 'a', 'A'
361
- case c! when 'l', 'L'
362
- case c! when 'u', 'U'
363
- case c! when 'e', 'E'
364
- case c! when 's', 'S'
365
- return keyword(Token::NoValues) if end_of_identifier!
366
- end
367
- end
368
- end
369
- end
370
- end
371
- end
372
- end
373
- when 'u', 'U'
374
- case c! when 'l', 'L'
375
- case c! when 'l', 'L'
376
- return keyword(Token::Null) if end_of_identifier!
377
- end
378
- end
379
- end
380
- when 'o', 'O'
381
- case c!
382
- when 'f', 'F'
383
- case c! when 'f', 'F'
384
- case c! when 's', 'S'
385
- case c! when 'e', 'E'
386
- case c! when 't', 'T'
387
- return keyword(Token::Offset) if end_of_identifier!
388
- end
389
- end
390
- end
391
- end
392
- when 'p', 'P'
393
- case c! when 't', 'T'
394
- case c! when 'i', 'I'
395
- case c! when 'o', 'O'
396
- case c! when 'n', 'N'
397
- case c! when 's', 'S'
398
- return keyword(Token::Options) if end_of_identifier!
399
- end
400
- end
401
- end
402
- end
403
- end
404
- when 'r', 'R'
405
- return keyword(Token::Or) if end_of_identifier!
406
- case c when 'd', 'D'
407
- case c! when 'e', 'E'
408
- case c! when 'r', 'R'
409
- return keyword(Token::Order) if end_of_identifier!
410
- end
411
- end
412
- end
413
- end
414
- when 'p', 'P'
415
- case c! when 'i', 'I'
416
- case c! when 'v', 'V'
417
- case c! when 'o', 'O'
418
- case c! when 't', 'T'
419
- return keyword(Token::Pivot) if end_of_identifier!
420
- end
421
- end
422
- end
423
- end
424
- when 'q', 'Q'
425
- case c! when 'u', 'U'
426
- case c! when 'a', 'A'
427
- case c! when 'r', 'R'
428
- case c! when 't', 'T'
429
- case c! when 'e', 'E'
430
- case c! when 'r', 'R'
431
- return keyword(Token::Quarter) if end_of_identifier!
432
- end
433
- end
434
- end
435
- end
436
- end
437
- end
438
- when 's', 'S'
439
- case c!
440
- when 'e', 'E'
441
- case c!
442
- when 'c', 'C'
443
- case c! when 'o', 'O'
444
- case c! when 'n', 'N'
445
- case c! when 'd', 'D'
446
- return keyword(Token::Second) if end_of_identifier!
447
- end
448
- end
449
- end
450
- when 'l', 'L'
451
- case c! when 'e', 'E'
452
- case c! when 'c', 'C'
453
- case c! when 't', 'T'
454
- return keyword(Token::Select) if end_of_identifier!
455
- end
456
- end
457
- end
458
- end
459
- when 't', 'T'
460
- case c! when 'a', 'A'
461
- case c! when 'r', 'R'
462
- case c! when 't', 'T'
463
- case c! when 's', 'S'
464
- return keyword(Token::Starts) if end_of_identifier!
465
- end
466
- end
467
- end
468
- end
469
- when 'u', 'U'
470
- case c! when 'm', 'M'
471
- return keyword(Token::Sum) if end_of_identifier!
472
- end
473
- end
474
- when 't', 'T'
475
- case c!
476
- when 'i', 'I'
477
- case c! when 'm', 'M'
478
- case c! when 'e', 'E'
479
- case c!
480
- when 'o', 'O'
481
- case c! when 'f', 'F'
482
- case c! when 'd', 'D'
483
- case c! when 'a', 'A'
484
- case c! when 'y', 'Y'
485
- return keyword(Token::TimeOfDay) if end_of_identifier!
486
- end
487
- end
488
- end
489
- end
490
- when 's', 'S'
491
- case c! when 't', 'T'
492
- case c! when 'a', 'A'
493
- case c! when 'm', 'M'
494
- case c! when 'p', 'P'
495
- return keyword(Token::Timestamp) if end_of_identifier!
496
- end
497
- end
498
- end
499
- end
500
- end
501
- end
502
- end
503
- when 'o', 'O'
504
- case c! when 'd', 'D'
505
- case c! when 'a', 'A'
506
- case c! when 't', 'T'
507
- case c! when 'e', 'E'
508
- return keyword(Token::ToDate) if end_of_identifier!
509
- end
510
- end
511
- end
512
- end
513
- when 'r', 'R'
514
- case c! when 'u', 'U'
515
- case c! when 'e', 'E'
516
- return keyword(Token::True) if end_of_identifier!
517
- end
518
- end
519
- end
520
- when 'u', 'U'
521
- case c! when 'p', 'P'
522
- case c! when 'p', 'P'
523
- case c! when 'e', 'E'
524
- case c! when 'r', 'R'
525
- return keyword(Token::Upper) if end_of_identifier!
526
- end
527
- end
528
- end
529
- end
530
- when 'w', 'W'
531
- case c!
532
- when 'h', 'H'
533
- case c! when 'e', 'E'
534
- case c! when 'r', 'R'
535
- case c! when 'e', 'E'
536
- return keyword(Token::Where) if end_of_identifier!
537
- end
538
- end
539
- end
540
- when 'i', 'I'
541
- case c! when 't', 'T'
542
- case c! when 'h', 'H'
543
- return keyword(Token::With) if end_of_identifier!
544
- end
545
- end
546
- end
547
- when 'y', 'Y'
548
- case c! when 'e', 'E'
549
- case c! when 'a', 'A'
550
- case c! when 'r', 'R'
551
- return keyword(Token::Year) if end_of_identifier!
552
- end
553
- end
554
- end
555
- end
556
-
557
- if @token.start == @p
558
- case c
559
- when 'a' .. 'z', 'A' .. 'Z', '_'
560
- @p += 1
561
- else
562
- raise ParseException.new("Unexpected character #{c} (code: #{c[0]})")
563
- end
16
+ skip /\s*/
17
+
18
+ if eos?
19
+ @token.value = Token::EOF
20
+ elsif scan /"([^"]*)"/
21
+ @token.value = Token::STRING
22
+ @token.string = self[1]
23
+ elsif scan /'([^']*)'/
24
+ @token.value = Token::STRING
25
+ @token.string = self[1]
26
+ elsif scan /`([^`]*)`/
27
+ @token.value = Token::ID
28
+ @token.string = self[1]
29
+ elsif scan /\d+\.\d*/ or scan /\.\d+/
30
+ @token.value = Token::DECIMAL
31
+ @token.number = matched.to_f
32
+ elsif scan /\d+/
33
+ @token.value = Token::INTEGER
34
+ @token.number = matched.to_f
35
+ else
36
+ @token.value = if scan /\+/ then Token::PLUS
37
+ elsif scan /\-/ then Token::MINUS
38
+ elsif scan /\*/ then Token::STAR
39
+ elsif scan /\// then Token::SLASH
40
+ elsif scan /\,/ then Token::COMMA
41
+ elsif scan /\(/ then Token::LPAREN
42
+ elsif scan /\)/ then Token::RPAREN
43
+ elsif scan /\=/ then Token::EQ
44
+ elsif scan /\!\=/ or scan /\<\>/ then Token::NEQ
45
+ elsif scan /\>\=/ then Token::GTE
46
+ elsif scan /\>/ then Token::GT
47
+ elsif scan /\<\=/ then Token::LTE
48
+ elsif scan /\</ then Token::LT
49
+ elsif scan /and\b/i then Token::And
50
+ elsif scan /asc\b/i then Token::Asc
51
+ elsif scan /avg\b/i then Token::Avg
52
+ elsif scan /by\b/i then Token::By
53
+ elsif @extensions and scan /concat\b/i then Token::Concat
54
+ elsif scan /contains\b/i then Token::Contains
55
+ elsif scan /count\b/i then Token::Count
56
+ elsif scan /date\b/i then Token::Date
57
+ elsif scan /datediff\b/i then Token::DateDiff
58
+ elsif scan /datetime\b/i then Token::DateTime
59
+ elsif scan /day\b/i then Token::Day
60
+ elsif scan /dayofweek\b/i then Token::DayOfWeek
61
+ elsif scan /desc\b/i then Token::Desc
62
+ elsif scan /ends\b/i then Token::Ends
63
+ elsif scan /false\b/i then Token::False
64
+ elsif scan /format\b/i then Token::Format
65
+ elsif scan /group\b/i then Token::Group
66
+ elsif scan /hour\b/i then Token::Hour
67
+ elsif scan /is\b/i then Token::Is
68
+ elsif scan /label\b/i then Token::Label
69
+ elsif scan /like\b/i then Token::Like
70
+ elsif scan /limit\b/i then Token::Limit
71
+ elsif scan /lower\b/i then Token::Lower
72
+ elsif scan /matches\b/i then Token::Matches
73
+ elsif scan /max\b/i then Token::Max
74
+ elsif scan /millisecond\b/i then Token::Millisecond
75
+ elsif scan /min\b/i then Token::Min
76
+ elsif scan /minute\b/i then Token::Minute
77
+ elsif scan /month\b/i then Token::Month
78
+ elsif scan /not\b/i then Token::Not
79
+ elsif scan /now\b/i then Token::Now
80
+ elsif scan /no_format\b/i then Token::NoFormat
81
+ elsif scan /no_values\b/i then Token::NoValues
82
+ elsif scan /null\b/i then Token::Null
83
+ elsif scan /offset\b/i then Token::Offset
84
+ elsif scan /options\b/i then Token::Options
85
+ elsif scan /or\b/i then Token::Or
86
+ elsif scan /order\b/i then Token::Order
87
+ elsif scan /pivot\b/i then Token::Pivot
88
+ elsif scan /quarter\b/i then Token::Quarter
89
+ elsif scan /second\b/i then Token::Second
90
+ elsif scan /select\b/i then Token::Select
91
+ elsif scan /starts\b/i then Token::Starts
92
+ elsif scan /sum\b/i then Token::Sum
93
+ elsif scan /timeofday\b/i then Token::TimeOfDay
94
+ elsif scan /timestamp\b/i then Token::Timestamp
95
+ elsif scan /todate\b/i then Token::ToDate
96
+ elsif scan /true\b/i then Token::True
97
+ elsif scan /upper\b/i then Token::Upper
98
+ elsif scan /where\b/i then Token::Where
99
+ elsif scan /with\b/i then Token::With
100
+ elsif scan /year\b/i then Token::Year
101
+ elsif scan /[a-zA-Z_]\w*\b/ then Token::ID
564
102
  end
565
103
 
566
- while true
567
- case c
568
- when 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '_'
569
- @p += 1
570
- else
571
- break
572
- end
104
+ if @token.value
105
+ @token.string = matched
106
+ else
107
+ raise ParseException.new("Unexpected character #{string[pos].chr}")
573
108
  end
574
-
575
- @token.value = Token::ID
576
- @token.string = @str[@token.start ... @p].join
577
- return @token
578
- end
579
- end
580
-
581
- def c
582
- @str[@p]
583
- end
584
-
585
- def c!
586
- @p += 1
587
- c
588
- end
589
-
590
- def end_of_identifier?
591
- case c
592
- when ' ', "\n", "\r", "\t", '(', ')', '+', '-', '*', '/', '<', '=', '>', '`', '"', "'", ',', nil
593
- true
594
- else
595
- false
596
109
  end
597
- end
598
-
599
- def end_of_identifier!
600
- @p += 1
601
- end_of_identifier?
602
- end
603
-
604
- def keyword(value)
605
- @token.value = value
606
- @token.string = @str[@token.start ... @p].join
607
- @token
608
- end
609
-
610
- def keyword!(value)
611
- @p += 1
612
- keyword value
110
+
111
+ return @token
613
112
  end
614
113
  end
615
114
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgviz
3
3
  version: !ruby/object:Gem::Version
4
- hash: 71
4
+ hash: 69
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 38
9
- version: "0.38"
8
+ - 39
9
+ version: "0.39"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ary Borenszweig
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-08 00:00:00 +07:00
17
+ date: 2010-06-08 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies: []
20
20