-
Notifications
You must be signed in to change notification settings - Fork 12
/
unitparse.l
372 lines (312 loc) · 10.6 KB
/
unitparse.l
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
/*
Copyright (C) 2016-2018 Christoph Berg
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 2 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.
*/
%{
#include "unit.h"
#include "unitparse.tab.h"
#include <executor/spi.h>
#include <utils/builtins.h> /* CStringGetTextDatum */
#include <catalog/pg_type.h> /* TEXTOID */
#include <math.h> /* INFINITY, NAN */
#define when(x) if (!strcmp(yytext, x))
%}
%option prefix="yyunit"
%option noyywrap
%option nounput
%option noinput
/* UTF-8 support, see http://stackoverflow.com/questions/9611682/flexlexer-support-for-unicode */
U [\x80-\xbf]
/* 2-byte UTF-8: \xc2-\xdf excluding SUPER_2, SUPER_3 and SUPER_1 */
U2_1 \xc2[\x80-\xb1]
U2_2 \xc2[\xb4-\xb8]
U2_3 \xc2[\xba-\xbf]
U2_4 [\xc3-\xdf]{U}
/* 3-byte UTF-8: \xe0-\xef excluding SUPER_0 .. SUPER_MINUS */
U3_1 [\xe0-\xe1]{U}{U}
U3_2 \xe2\x80{U}
U3_3 \xe2\x81[\x80-\xaf]
U3_4 \xe2\x81[\xbc-\xbf]
U3_5 \xe2[\x82-\xbf]{U}
U3_6 [\xe3-\xef]{U}{U}
/* 4-byte UTF-8: \xf0-\xf4 */
U4 [\xf0-\xf4]{U}{U}{U}
UTF8 {U2_1}|{U2_2}|{U2_3}|{U2_4}|{U3_1}|{U3_2}|{U3_3}|{U3_4}|{U3_5}|{U3_6}|{U4}
ALPHA [a-zA-Z$%'"_]|{UTF8}
ALNUM [a-zA-Z$%'"_0-9]|{UTF8}
DOUBLE_R [0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?
TIME_R [0-9]+:[0-9]+:[0-9]+(\.[0-9]+)?
UNIT_R {ALPHA}{ALNUM}*
EXP_R \^[\-+]?[0-9]+
SUPER_PLUS \xe2\x81\xba
SUPER_MINUS \xe2\x81\xbb
SUPER_0 \xe2\x81\xb0
SUPER_1 \xc2\xb9
SUPER_2 \xc2\xb2
SUPER_3 \xc2\xb3
SUPER_4 \xe2\x81\xb4
SUPER_5 \xe2\x81\xb5
SUPER_6 \xe2\x81\xb6
SUPER_7 \xe2\x81\xb7
SUPER_8 \xe2\x81\xb8
SUPER_9 \xe2\x81\xb9
%%
{DOUBLE_R} {
yyunitlval.DOUBLE = atof(yytext);
return DOUBLE;
}
[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])? {
yyunitlval.DOUBLE = INFINITY;
return DOUBLE;
}
[Nn][Aa][Nn] {
yyunitlval.DOUBLE = NAN;
return DOUBLE;
}
sqrt {
yyunitlval.FUNCTION = FUNCTION_SQRT;
return FUNCTION;
}
exp {
yyunitlval.FUNCTION = FUNCTION_EXP;
return FUNCTION;
}
ln {
yyunitlval.FUNCTION = FUNCTION_LN;
return FUNCTION;
}
log2 {
yyunitlval.FUNCTION = FUNCTION_LOG2;
return FUNCTION;
}
asin {
yyunitlval.FUNCTION = FUNCTION_ASIN;
return FUNCTION;
}
tan {
yyunitlval.FUNCTION = FUNCTION_TAN;
return FUNCTION;
}
{TIME_R} { /* hh:mm:ss[.sss] */
char *colon;
yyunitlval.DOUBLE = TIME_HOUR * atoi(yytext); /* hh */
colon = strchr(yytext, ':');
yyunitlval.DOUBLE += TIME_MINUTE * atoi(colon + 1); /* mm */
colon = strchr(colon + 1, ':');
yyunitlval.DOUBLE += atof(colon + 1); /* ss[.sss] */
return DOUBLE;
}
{UNIT_R} {
unit_names_t *name;
int ret;
Oid argtypes[1];
Datum values[1];
size_t yytext_len;
/* Check if it's a predefined or previously seen unit */
name = hash_search(unit_names, yytext, HASH_FIND, NULL);
if (name)
{
elog(DEBUG1, "unit %s found in unit_names hash table", name->name);
yyunitlval.UNIT_SHIFT = name->unit_shift;
return UNIT_SHIFT;
}
SPI_connect();
argtypes[0] = TEXTOID;
values[0] = CStringGetTextDatum(yytext);
/* look up unit definition without prefix first */
ret = SPI_execute_with_args("SELECT unit, shift "
"FROM unit_units WHERE "
"name = $1",
1, /* nargs */
argtypes,
values,
NULL, /* nulls */
true, /* read only */
0); /* limit */
if (ret != SPI_OK_SELECT)
elog(ERROR, "internal error determining definition of unit \"%s\"", yytext);
if (SPI_processed == 1) /* found definition */
{
Unit *unitp;
Datum shift;
bool is_null;
unitp = (Unit *) DatumGetPointer(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &is_null));
if (is_null)
elog(ERROR, "unit \"%s\" definition is NULL", yytext);
yyunitlval.UNIT_SHIFT.unit = *unitp;
shift = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &is_null);
yyunitlval.UNIT_SHIFT.shift = is_null ? 0.0 : DatumGetFloat8(shift);
elog(DEBUG1, "unit %s (value %g)",
SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1),
unitp->value);
goto found;
}
/* look up definition with prefix */
ret = SPI_execute_with_args("SELECT prefix, factor, name, unit, shift "
"FROM unit_prefixes CROSS JOIN unit_units WHERE "
"prefix||name = $1 AND "
"$1 LIKE prefix||'%' AND "
"$1 LIKE '%'||name "
"ORDER BY prefix",
1, /* nargs */
argtypes,
values,
NULL, /* nulls */
true, /* read only */
0); /* limit */
if (ret != SPI_OK_SELECT)
elog(ERROR, "internal error determining definition of unit \"%s\"", yytext);
if (SPI_processed > 2)
{
long processed = SPI_processed; /* SPI_processed was uint32 before 9.6 */
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unit \"%s\" is ambiguous, %lu prefix/name combinations found",
yytext, processed)));
}
if (SPI_processed == 2)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unit \"%s\" is ambiguous, \"%s-%s\" vs. \"%s-%s\"", yytext,
SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1),
SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3),
SPI_getvalue(SPI_tuptable->vals[1], SPI_tuptable->tupdesc, 1),
SPI_getvalue(SPI_tuptable->vals[1], SPI_tuptable->tupdesc, 3))));
if (SPI_processed == 1) /* found definition */
{
double factor;
Unit *unitp;
Datum shift;
bool is_null;
factor = DatumGetFloat8(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &is_null)); /* we discard is_null here */
unitp = (Unit *) DatumGetPointer(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 4, &is_null));
if (is_null)
elog(ERROR, "unit \"%s\" definition is NULL", yytext);
yyunitlval.UNIT_SHIFT.unit = *unitp;
yyunitlval.UNIT_SHIFT.unit.value *= factor;
shift = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 5, &is_null);
yyunitlval.UNIT_SHIFT.shift = is_null ? 0.0 : DatumGetFloat8(shift);
goto found;
}
/* if unit is long enough, look up unit definition with plural 's' removed */
yytext_len = strlen(yytext);
if (yytext_len >= MIN_PLURAL_LENGTH && yytext[yytext_len - 1] == 's') {
ret = SPI_execute_with_args("SELECT unit, shift "
"FROM unit_units WHERE "
"name = substring($1 for length($1) - 1)",
1, /* nargs */
argtypes,
values,
NULL, /* nulls */
true, /* read only */
0); /* limit */
if (ret != SPI_OK_SELECT)
elog(ERROR, "internal error determining definition of unit \"%s\"", yytext);
if (SPI_processed == 1) /* found definition */
{
Unit *unitp;
Datum shift;
bool is_null;
unitp = (Unit *) DatumGetPointer(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &is_null));
if (is_null)
elog(ERROR, "unit \"%s\" definition is NULL", yytext);
yyunitlval.UNIT_SHIFT.unit = *unitp;
shift = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &is_null);
yyunitlval.UNIT_SHIFT.shift = is_null ? 0.0 : DatumGetFloat8(shift);
elog(DEBUG1, "unit %s (value %g)",
SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1),
unitp->value);
goto found;
}
/* look up definition with prefix */
ret = SPI_execute_with_args("SELECT prefix, factor, name, unit, shift "
"FROM unit_prefixes CROSS JOIN unit_units WHERE "
"prefix||name = substring($1 for length($1) - 1) AND "
"substring($1 for length($1) - 1) LIKE prefix||'%' AND "
"substring($1 for length($1) - 1) LIKE '%'||name "
"ORDER BY prefix",
1, /* nargs */
argtypes,
values,
NULL, /* nulls */
true, /* read only */
0); /* limit */
if (ret != SPI_OK_SELECT)
elog(ERROR, "internal error determining definition of unit \"%s\"", yytext);
if (SPI_processed > 2)
{
long processed = SPI_processed; /* SPI_processed was uint32 before 9.6 */
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unit \"%s\" is ambiguous, %lu prefix/name combinations found",
yytext, processed)));
}
if (SPI_processed == 2)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unit \"%s\" is ambiguous, \"%s-%s\" vs. \"%s-%s\"", yytext,
SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1),
SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3),
SPI_getvalue(SPI_tuptable->vals[1], SPI_tuptable->tupdesc, 1),
SPI_getvalue(SPI_tuptable->vals[1], SPI_tuptable->tupdesc, 3))));
if (SPI_processed == 1) /* found definition */
{
double factor;
Unit *unitp;
Datum shift;
bool is_null;
factor = DatumGetFloat8(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &is_null)); /* we discard is_null here */
unitp = (Unit *) DatumGetPointer(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 4, &is_null));
if (is_null)
elog(ERROR, "unit \"%s\" definition is NULL", yytext);
yyunitlval.UNIT_SHIFT.unit = *unitp;
yyunitlval.UNIT_SHIFT.unit.value *= factor;
shift = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 5, &is_null);
yyunitlval.UNIT_SHIFT.shift = is_null ? 0.0 : DatumGetFloat8(shift);
goto found;
}
}
/* nothing found, error out */
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unit \"%s\" is not known", yytext)));
found:
SPI_finish();
/* store computed unit in hash table */
name = hash_search(unit_names, yytext, HASH_ENTER, NULL);
strlcpy(name->name, yytext, UNIT_NAME_LENGTH);
name->unit_shift = yyunitlval.UNIT_SHIFT;
return UNIT_SHIFT;
}
{EXP_R} {
yyunitlval.EXPONENT = atoi(yytext+1);
return EXPONENT;
}
{SUPER_PLUS} { yyunitlval.SUPER_SIGN = 1; return SUPER_SIGN; }
{SUPER_MINUS} { yyunitlval.SUPER_SIGN = -1; return SUPER_SIGN; }
{SUPER_0} { yyunitlval.SUPER = 0; return SUPER; }
{SUPER_1} { yyunitlval.SUPER = 1; return SUPER; }
{SUPER_2} { yyunitlval.SUPER = 2; return SUPER; }
{SUPER_3} { yyunitlval.SUPER = 3; return SUPER; }
{SUPER_4} { yyunitlval.SUPER = 4; return SUPER; }
{SUPER_5} { yyunitlval.SUPER = 5; return SUPER; }
{SUPER_6} { yyunitlval.SUPER = 6; return SUPER; }
{SUPER_7} { yyunitlval.SUPER = 7; return SUPER; }
{SUPER_8} { yyunitlval.SUPER = 8; return SUPER; }
{SUPER_9} { yyunitlval.SUPER = 9; return SUPER; }
\+ return '+';
- return '-';
\/ return '/';
\* return '*';
\| return '|';
\( return '(';
\) return ')';
[ \t\n]* /* eat whitespace */
. return ERR;