1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
# parsedate2.ry: Written by Tadayoshi Funaba 1999, 2000
# $Id: parsedate2.ry,v 1.5 2000/03/20 16:23:46 tadf Exp $
## Generated by rbison version 0.0.5.
class ParseDatePar
class ParseError < StandardError ; end
class LexError < StandardError ; end
NULL = nil
attr :yydebug, true
DAY = 257
DIGITS = 258
MON = 259
LETTERS = 260
MERID = 261
## Parser declarations begin
## Parser declarations end
YYFINAL = 84
YYFLAG = -32768
YYNTBASE = 14
YYTRANSLATE = [ 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 13, 8, 9, 10, 11, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 12, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 1, 3, 4, 5, 6,
7
]
YYPRHS = [ 0,
0, 1, 3, 6, 8, 12, 14, 20, 26, 32,
34, 38, 40, 44, 45, 48, 50, 51, 53, 55,
57, 60, 63, 65, 67, 69, 71, 73, 80, 88,
94, 98, 104, 108, 115, 116, 119, 122, 123, 125,
128, 129, 131, 132, 134, 136, 139, 142, 147, 149
]
YYRHS = [ -1,
17, 0, 16, 15, 0, 19, 0, 19, 18, 22,
0, 28, 0, 28, 18, 19, 18, 22, 0, 19,
18, 28, 18, 22, 0, 19, 18, 22, 18, 28,
0, 23, 0, 23, 18, 28, 0, 30, 0, 30,
18, 32, 0, 0, 17, 18, 0, 3, 0, 0,
8, 0, 20, 0, 21, 0, 4, 5, 0, 5,
4, 0, 4, 0, 24, 0, 25, 0, 26, 0,
27, 0, 4, 9, 4, 9, 4, 31, 0, 6,
4, 10, 4, 10, 4, 31, 0, 4, 9, 5,
9, 4, 0, 4, 11, 4, 0, 4, 11, 4,
11, 4, 0, 4, 7, 34, 0, 4, 12, 4,
29, 33, 34, 0, 0, 12, 4, 0, 4, 31,
0, 0, 6, 0, 4, 34, 0, 0, 7, 0,
0, 35, 0, 6, 0, 6, 6, 0, 36, 4,
0, 36, 4, 12, 4, 0, 9, 0, 13, 0
]
YYRLINE = [ 0,
14, 14, 14, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 28, 28, 30, 32, 32, 34, 34,
36, 38, 40, 42, 42, 42, 42, 44, 58, 72,
86, 91, 105, 109, 116, 116, 118, 141, 141, 143,
156, 156, 158, 158, 160, 161, 166, 167, 170, 170
]
YYTNAME = [ "$","error","$undefined.","DAY","DIGITS",
"MON","LETTERS","MERID","','","'-'","'.'","'/'","':'","'+'","repr","dat","odaycom",
"day","ocom","woy","eu","us","year","wy","iso","jis","vms","sla","time","osec",
"ddd","otee","ttt","omerid","ozone","zone","sign", NULL
]
YYR1 = [ 0,
14, 14, 14, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 16, 16, 17, 18, 18, 19, 19,
20, 21, 22, 23, 23, 23, 23, 24, 25, 26,
27, 27, 28, 28, 29, 29, 30, 31, 31, 32,
33, 33, 34, 34, 35, 35, 35, 35, 36, 36
]
YYR2 = [ 0,
0, 1, 2, 1, 3, 1, 5, 5, 5, 1,
3, 1, 3, 0, 2, 1, 0, 1, 1, 1,
2, 2, 1, 1, 1, 1, 1, 6, 7, 5,
3, 5, 3, 6, 0, 2, 2, 0, 1, 2,
0, 1, 0, 1, 1, 2, 2, 4, 1, 1
]
YYDEFACT = [ 14,
16, 0, 17, 38, 0, 0, 3, 4, 19, 20,
10, 24, 25, 26, 27, 17, 12, 18, 15, 21,
39, 43, 0, 0, 0, 37, 22, 0, 0, 0,
0, 0, 45, 49, 50, 33, 44, 0, 0, 0,
31, 35, 0, 23, 5, 17, 0, 11, 0, 17,
43, 13, 46, 47, 0, 0, 0, 0, 41, 0,
0, 0, 0, 40, 0, 38, 30, 32, 36, 42,
43, 0, 9, 23, 8, 7, 48, 28, 34, 38,
29, 0, 0, 0
]
YYDEFGOTO = [ 82,
7, 2, 3, 19, 8, 9, 10, 45, 11, 12,
13, 14, 15, 16, 59, 17, 26, 52, 71, 36,
37, 38
]
YYPACT = [ 10,
-32768, 30, 6, 11, 37, 39,-32768, 23,-32768,-32768,
23,-32768,-32768,-32768,-32768, 7, 23,-32768,-32768,-32768,
-32768, 19, 25, 40, 41,-32768,-32768, 36, 43, 44,
35, 45, 46,-32768,-32768,-32768,-32768, 47, 48, 49,
42, 38, 50, 14, 23, 51, 14,-32768, 55, 51,
19,-32768,-32768, 52, 57, 58, 59, 61, 60, 56,
44, 64, 64,-32768, 65, 66,-32768,-32768,-32768,-32768,
19, 67,-32768,-32768,-32768,-32768,-32768,-32768,-32768, 66,
-32768, 70, 73,-32768
]
YYPGOTO = [ -32768,
-32768,-32768,-32768, -8, 24,-32768,-32768, -51,-32768,-32768,
-32768,-32768,-32768, -28,-32768,-32768, -61,-32768,-32768, -47,
-32768,-32768
]
YYLAST = 73
YYTABLE = [ 29,
46, 48, 30, 64, 78, -2, -6, 31, 32, -1,
75, 76, 1, 18, 18, 20, 21, 22, 81, 23,
22, 24, 25, 79, 33, 25, -17, 34, 39, 40,
18, 35, 73, 4, 5, 6, 61, 62, 49, 5,
27, 63, 28, 41, 42, 43, 44, 47, 51, 58,
54, 53, 57, 60, 50, 0, 55, 56, 18, 20,
66, 67, 68, 65, 69, 72, 70, 74, 77, 83,
80, 21, 84
]
YYCHECK = [ 8,
29, 30, 11, 51, 66, 0, 0, 16, 17, 0,
62, 63, 3, 8, 8, 5, 6, 7, 80, 9,
7, 11, 12, 71, 6, 12, 4, 9, 4, 5,
8, 13, 61, 4, 5, 6, 45, 46, 4, 5,
4, 50, 4, 4, 4, 10, 4, 4, 4, 12,
4, 6, 11, 4, 31, -1, 9, 9, 8, 5,
4, 4, 4, 12, 4, 10, 7, 4, 4, 0,
4, 6, 0
]
YYEMPTY = -2
YYEOF = 0
YYTERROR = 1
def initialize
@yydebug = false
end
def yyparse(lexer)
yyerrstatus = 0
yychar = YYEMPTY
yystate = 0
yyss = []
yyvs = ["nil"]
yyval = 0
jump = :YYNEWSTATE
while true
case jump
when :YYNEWSTATE
if @yydebug
printf($stderr, "Entering state %d\n", yystate)
end
yyss.push(yystate)
jump = :YYBACKUP
next
when :YYBACKUP
yyn = YYPACT[yystate]
if yyn == YYFLAG
jump = :YYDEFAULT
next
end
## get a lookahead token if we don't already have one
if yychar == YYEMPTY
if @yydebug
printf($stderr, "Reading a token: ")
end
begin
yychar, yylval = lexer.yylex
rescue LexError
raise ParseError, "lex error"
end
end
## if lookahead <= 0, end of input
if yychar <= 0
yychar1 = 0
yychar = YYEOF
if @yydebug
printf($stderr, "Now at end of input.\n")
end
else
yychar1 = yychar < 0 || yychar > 261 ? 37 : YYTRANSLATE[yychar]
if @yydebug
printf($stderr, "Next token is %d (%s,%s)\n", yychar,
YYTNAME[yychar1], yylval)
end
end
## see if we know what to do with this token in this state
yyn += yychar1
if yyn < 0 || yyn > YYLAST || YYCHECK[yyn] != yychar1
jump = :YYDEFAULT
next
end
## yyn is what to do for this token type in this state
## negative -> reduce, - yyn is the rule number
## positive -> shift, yyn is the new state
## New state is final state, don't bother to shift, just
## return success
## 0, or most negative number -> error
yyn = YYTABLE[yyn]
if yyn < 0
if yyn == YYFLAG
jump = :YYERRLAB
next
end
yyn = - yyn
jump = :YYREDUCE
next
elsif yyn == 0
jump = :YYERRLAB
next
end
if yyn == YYFINAL
return ## accept
end
## shift the lookahead token
if @yydebug
printf($stderr, "Shifting token %d (%s), ", yychar,
YYTNAME[yychar1])
end
## discard the token being shifted unless it is eof
if yychar != YYEOF
yychar = YYEMPTY
end
yyvs.push(yylval)
## count tokens shifted since error; after, three turn off
## error status
yyerrstatus -= 1 if yyerrstatus > 0
yystate = yyn
jump = :YYNEWSTATE
next
when :YYDEFAULT
yyn = YYDEFACT[yystate]
if yyn == 0
jump = :YYERRLAB
next
else
jump = :YYREDUCE
next
end
## do a reduction. yyn is the number of the rule to reduce with
when :YYREDUCE
yylen = YYR2[yyn]
if yylen > 0
yyval = yyvs[yyvs.size - yylen]
end
if @yydebug
printf($stderr, "Reducing via rule %d (line %d), ", yyn,
YYRLINE[yyn])
i = YYPRHS[yyn]
while YYRHS[i] > 0
printf($stderr, "%s ", YYTNAME[YYRHS[i]])
i += 1
end
printf($stderr, " -> %s\n", YYTNAME[YYR1[yyn]])
end
case yyn
when 16
store(:wday, yyvs[-1].to_i)
when 21
store(:mday, yyvs[-2].to_i); store(:mon, yyvs[-1])
when 22
store(:mon, yyvs[-2]); store(:mday, yyvs[-1].to_i)
when 23
store(:year, yyvs[-1].to_i)
when 28
if yyvs[-1 + -1].size >= 4
store(:mday, yyvs[-1 + -5].to_i)
store(:mon, yyvs[-1 + -3].to_i)
store(:year, yyvs[-1 + -1].to_i)
else
store(:year, yyvs[-1 + -5].to_i)
store(:mon, yyvs[-1 + -3].to_i)
store(:mday, yyvs[-1 + -1].to_i)
end
when 29
e = { 'M'=>1867,
'T'=>1911,
'S'=>1925,
'H'=>1988
}[yyvs[-1 + -6]]
raise ParseError, 'YYERROR' unless e
store(:year, yyvs[-1 + -5].to_i + e)
store(:mon, yyvs[-1 + -3].to_i)
store(:mday, yyvs[-1 + -1].to_i)
when 30
if yyvs[-1 + -4].size >= 4
store(:year, yyvs[-1 + -4].to_i)
store(:mon, yyvs[-1 + -2])
store(:mday, yyvs[-1 + 0].to_i)
else
store(:mday, yyvs[-1 + -4].to_i)
store(:mon, yyvs[-1 + -2])
store(:year, yyvs[-1 + 0].to_i)
end
when 31
store(:mon, yyvs[-1 + -2].to_i)
store(:mday, yyvs[-1 + 0].to_i)
when 32
if yyvs[-1 + -4].size >= 4
store(:year, yyvs[-1 + -4].to_i)
store(:mon, yyvs[-1 + -2].to_i)
store(:mday, yyvs[-1 + 0].to_i)
else
store(:mon, yyvs[-1 + -4].to_i)
store(:mday, yyvs[-1 + -2].to_i)
store(:year, yyvs[-1 + 0].to_i)
end
when 33
store(:hour, yyvs[-1 + -2].to_i + yyvs[-1 + -1])
when 34
store(:hour, yyvs[-1 + -5].to_i + yyvs[-1 + -1])
store(:min, yyvs[-1 + -3].to_i)
when 36
store(:sec, yyvs[-1].to_i)
when 37
case yyvs[-1 + -1].size
when 4
store(:mon, yyvs[-1 + -1][ 0, 2].to_i)
store(:mday, yyvs[-1 + -1][ 2, 2].to_i)
when 6
store(:year, yyvs[-1 + -1][ 0, 2].to_i)
store(:mon, yyvs[-1 + -1][ 2, 2].to_i)
store(:mday, yyvs[-1 + -1][ 4, 2].to_i)
when 8, 10, 12, 14
store(:year, yyvs[-1 + -1][ 0, 4].to_i)
store(:mon, yyvs[-1 + -1][ 4, 2].to_i)
store(:mday, yyvs[-1 + -1][ 6, 2].to_i)
store(:hour, yyvs[-1 + -1][ 8, 2].to_i) if yyvs[-1 + -1].size >= 10
store(:min, yyvs[-1 + -1][10, 2].to_i) if yyvs[-1 + -1].size >= 12
store(:sec, yyvs[-1 + -1][12, 2].to_i) if yyvs[-1 + -1].size >= 14
else
raise ParseError, 'YYERROR'
end
when 39
raise ParseError, 'YYERROR' unless yyvs[-1] == 'T'
when 40
case yyvs[-1 + -1].size
when 2, 4, 6
store(:hour, yyvs[-1 + -1][ 0, 2].to_i)
store(:min, yyvs[-1 + -1][ 2, 2].to_i) if yyvs[-1 + -1].size >= 4
store(:sec, yyvs[-1 + -1][ 4, 2].to_i) if yyvs[-1 + -1].size >= 6
else
raise ParseError, 'YYERROR'
end
when 41
yyval = 0
when 45
store(:zone, yyvs[-1])
when 46
raise ParseError, 'YYERROR' unless yyvs[-1 + 0] == 'DST'
store(:zone, yyvs[-1 + -1] + ' ' + yyvs[-1 + 0])
when 47
store(:zone, yyvs[-2] + yyvs[-1])
when 48
store(:zone, yyvs[-4] + yyvs[-3] + yyvs[-2] + yyvs[-1])
when -65536 ## never used, placeholder for ruby
end
if yylen > 0
yyss[(yyss.size - yylen) .. (yyss.size - 1)] = []
yyvs[(yyvs.size - yylen) .. (yyvs.size - 1)] = []
end
yyvs.push(yyval)
if @yydebug
printf($stderr, "State stack now: %s\n", yyss.join(' '))
printf($stderr, "Value stack now: %s\n", yyvs.join(' '))
end
## "Shift" the result of the reduction.
yyn = YYR1[yyn]
yystate = YYPGOTO[yyn - YYNTBASE] + yyss[-1]
if yystate >=0 && yystate <= YYLAST &&
YYCHECK[yystate] == yyss[-1]
yystate = YYTABLE[yystate]
else
yystate = YYDEFGOTO[yyn - YYNTBASE]
end
jump = :YYNEWSTATE
next
when :YYERRLAB
if yyerrstatus == 0 && @yydebug
printf($stderr, "Parse error!\n")
end
jump = :YYERRLAB1
next
when :YYERRLAB1
if yyerrstatus == 3
if yychar == YYEOF
raise ParseError, "parse error"
end
if @yydebug
printf($stderr, "Discarding token %d (%s).\n", yychar,
YYTNAME[yychar1])
end
yychar = YYEMPTY
end
yyerrstatus = 3
jump = :YYERRHANDLE
next
when :YYERRPOP
if yyvs.empty?
raise ParseError, "parse error"
end
## don't pop if the state on top of the stack can handle
## the error token
yystate = yyss[-1]
if YYCHECK[YYPACT[yystate] + YYTERROR] != YYTERROR
yyvs.pop
yyss.pop
if @yydebug
printf($stderr, "Error: state stack now: %s\n",
yyss.join(' '))
printf($stderr, "Error: Value stack now: %s\n",
yyvs.join(' '))
end
end
jump = :YYERRHANDLE
next
when :YYERRHANDLE
yyn = YYPACT[yystate]
if yyn == YYFLAG
jump = :YYERRPOP
next
end
yyn += YYTERROR
if yyn < 0 || yyn > YYLAST || YYCHECK[yyn] != YYTERROR
jump = :YYERRPOP
next
end
yyn = YYTABLE[yyn]
if yyn < 0
if yyn == YYFLAG
jump = :YYERRPOP
next
end
yyn = -yyn
jump = :YYREDUCE
next
elsif yyn == 0
jump = :YYERRPOP
next
end
if yyn == YYFINAL
return ## accept
end
if @yydebug
printf($stderr, "Shifting error token, ")
end
yyvs.push(yylval)
yystate = yyn
jump = :YYNEWSTATE
next
end ## case
end ## while true
end ## yyparse
end ## class
## Additional user code
class ParseDateLex
MONTHS = {
'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12
}
MONTHPAT = MONTHS.keys.sort.reverse.join('|')
DAYS = {
'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3,
'thu' => 4, 'fri' => 5, 'sat' => 6
}
DAYPAT = DAYS.keys.sort.reverse.join('|')
def initialize(str) @str = str end
def reset(str) @str = str end
def yylex
@str = @str.sub(/\A\s+/, '')
return [-1, nil] if @str.size == 0
if /\A(#{DAYPAT})[a-z]*\.?/i =~ @str
@str = $'
return [ParseDatePar::DAY, DAYS[$1[0, 3].downcase]]
end
if /\A(\d+)(?:(?:th|st|nd|rd)\b)?/ =~ @str
@str = $'
return [ParseDatePar::DIGITS, $1]
end
if /\A(#{MONTHPAT})[a-z]*\.?/i =~ @str
@str = $'
return [ParseDatePar::MON, MONTHS[$1[0, 3].downcase]]
end
if /\A([ap]\.?m\.?\b)/i =~ @str
@str = $'
return [ParseDatePar::MERID,
if $1[0, 1].downcase == 'a' then 0 else 12 end]
end
if /\A([a-z]+)/i =~ @str
@str = $'
return [ParseDatePar::LETTERS, $1]
end
if /\A(.)/ =~ @str
@str = $'
return [$1[0], $1]
end
end
end
class ParseDatePar
def clear() @values = {} end
def store(key, val) @values[key] = val end
def values(cyear)
year = @values[:year]
if cyear and year
if year < 100
if year >= 69
year += 1900
else
year += 2000
end
end
end
@values[:year] = year
@values.indexes(:year, :mon, :mday,
:hour, :min, :sec, :zone, :wday)
end
end
module ParseDate
def parsedate(date, cyear=false)
lex = ParseDateLex.new(date)
par = ParseDatePar.new
par.clear
begin par.yyparse(lex); rescue; end
par.values(cyear)
end
module_function :parsedate
end
|