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
| | /* Date parser bison grammar file
* Copyright (c) 2012 Google Inc.
* Written by Michal Nazarewicz <mina86@mina86.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ . */
%code requires {
#ifndef YYSTYPE
# define YYSTYPE long
#endif
#ifndef YYLTYPE
# define YYLTYPE struct yylocation
#endif
#ifdef YYLLOC_DEFAULT
# undef YYLLOC_DEFAULT
#endif
#define YYLLOC_DEFAULT(Cur, Rhs, N) do { \
if (N) { \
(Cur).start = YYRHSLOC(Rhs, 1).start; \
(Cur).end = YYRHSLOC(Rhs, N).end; \
} else { \
(Cur) = YYRHSLOC(Rhs, 0); \
} \
} while (0)
}
%code{
#include "date-parser-grammar.tab.h"
#include "date-parser.h"
#define ASSERT(cond, loc, message) do { \
if (!(cond)) { \
parse_date_print_error(&loc, message); \
YYERROR; \
} \
} while (0)
}
%locations
%defines
%error-verbose
%define api.pure
%parse-param {struct date *ret}
%parse-param {const char **inputp}
%lex-param {const char **inputp}
%token T_NUMBER "<num>" /* Always positive. */
%token T_NUMBER_4 "####" /* Four digit number. */
%token T_AGO "ago"
/* Also used for minutes, hours, days and weeks. */
%token T_SECONDS "seconds"
/* Also used for quarters and years */
%token T_MONTHS "months"
%token T_YESTERDAY "yesterday"
%token T_AMPM "am/pm"
%token T_HOUR "<hour>"
%token T_MONTH "<month>"
%expect 3 /* Two shift/reduce conflicts caused by year_maybe, and onde
* caused by day_maybe. */
%%
/* For backwards compatibility, just a number and nothing else
* is treated as timestamp */
input : number { date_set_from_stamp(ret, $1) }
| date
;
date : part
| date part
;
part : integer "seconds" ago_maybe {
ASSERT(date_add_seconds(ret, $1 * $3, $2), @$,
"offset ends up in date out of range")
}
| integer "months" ago_maybe {
ASSERT(date_add_months(ret, $1 * $3, $2), @$,
"offset ends up in date out of range")
}
| "yesterday" {
ASSERT(date_set_yesterday(ret), @$,
"offset ends up in date out of range")
}
| '@' number { date_set_from_stamp(ret, $2) }
| "<hour>" { date_set_time(ret, $1, -1, -1, -1) }
/* HH:MM, HH:MM:SS, HH:MM am/pm, HH:MM:SS am/pm */
| number ':' number seconds_maybe ampm_maybe {
ASSERT(date_set_time(ret, $1, $3, $4, $5), @$,
"invalid time")
}
| number "am/pm" { /* HH am/pm */
ASSERT(date_set_time(ret, $1, -1, -1, $2), @$, "invalid hour")
}
| "####" '/' "<num>" '/' "<num>" { /* YYYY/MM/DD */
ASSERT(date_set_date(ret, $1, $3, $5), @$, "invalid date")
}
| "####" '-' "<num>" '-' "<num>" { /* YYYY-MM-DD */
ASSERT(date_set_date(ret, $1, $3, $5), @$, "invalid date")
}
| "<num>" '-' "<num>" '-' "####" { /* DD-MM-YYYY */
ASSERT(date_set_date(ret, $5, $3, $1), @$, "invalid date")
}
/* No MM/DD/YYYY or DD/MM/YYYY because it's confusing. */
| "<num>" "<month>" year_maybe { /* 1 January 2012 */
ASSERT(date_set_date(ret, $3, $2, $1), @$, "invalid date")
}
| "<month>" day_maybe year_maybe { /* January 1 2012 */
ASSERT(date_set_date(ret, $3, $1, $2), @$, "invalid date")
}
| "####" 'q' "<num>" { /* Quarter, 2012q1 */
ASSERT(date_set_quarter(ret, $1, $3), @$, "invalid quarter");
}
;
number : "<num>" { $$ = $1 }
| "####" { $$ = $1 }
;
integer : number { $$ = $1 }
| '-' number { $$ = -$2 }
;
ago_maybe
: /* empty */ { $$ = 1 }
| "ago" { $$ = -1 }
;
seconds_maybe
: /* empty */ { $$ = -1 }
| ':' "<num>" { $$ = $2 }
;
ampm_maybe
: /* empty */ { $$ = -1 }
| "am/pm" { $$ = $$ }
/* For people who like writing "a.m." or "p.m." and since dot
* is ignored by the lexer (ie. it's treated just like white
* space), dot is lost. */
| 'a' 'm' { $$ = 0 }
| 'p' 'm' { $$ = 0 }
;
day_maybe
: /* empty */ { $$ = -1 }
| "<num>" { $$ = $1 }
;
year_maybe
: /* empty */ { $$ = -1 }
| "####" { $$ = $1 }
;
%%
|