1 /*
2 * SysDB - src/frontend/scanner.l
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 %{
30 #if HAVE_CONFIG_H
31 # include "config.h"
32 #endif /* HAVE_CONFIG_H */
34 #include "core/data.h"
35 #include "frontend/connection.h"
36 #include "frontend/parser.h"
37 #include "frontend/grammar.h"
38 #include "utils/error.h"
40 #include <assert.h>
41 #include <errno.h>
43 #include <string.h>
44 #include <stdlib.h>
46 #include <time.h>
48 #define YY_EXTRA_TYPE sdb_fe_yyextra_t *
50 static struct {
51 const char *name;
52 int id;
53 } reserved_words[] = {
54 { "AND", AND },
55 { "END", END },
56 { "FETCH", FETCH },
57 { "FILTER", FILTER },
58 { "IS", IS },
59 { "LIST", LIST },
60 { "LOOKUP", LOOKUP },
61 { "MATCHING", MATCHING },
62 { "NOT", NOT },
63 { "NULL", NULL_T },
64 { "OR", OR },
65 { "START", START },
66 { "TIMESERIES", TIMESERIES },
67 };
69 void
70 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
72 %}
74 %option never-interactive
75 %option reentrant
76 %option bison-bridge
77 %option bison-locations
78 %option 8bit
79 %option yylineno
80 %option nodefault
81 %option noinput
82 %option nounput
83 %option noyywrap
84 %option verbose
85 %option warn
86 %option prefix="sdb_fe_yy" outfile="lex.yy.c"
88 %x CSC
90 whitespace ([ \t\n\r\f]+)
91 simple_comment ("--"[^\n\r]*)
93 /*
94 * C style comments
95 */
96 csc_start \/\*
97 csc_inside ([^*/]+|[^*]\/|\*[^/])
98 csc_end \*\/
100 /*
101 * Strings and identifiers.
102 */
103 identifier ([A-Za-z_][A-Za-z_0-9$]*)
104 /* TODO: fully support SQL strings */
105 string ('([^']|'')*')
107 /*
108 * Numeric constants.
109 */
110 dec ([\+\-]?[0-9]+)
111 exp ([\+\-]?[0-9]+[Ee]\+?[0-9]+)
112 integer ({dec}|{exp})
113 float1 ([\+\-]?[0-9]+\.[0-9]*([Ee][\+\-]?[0-9]+)?)
114 float2 ([\+\-]?[0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?)
115 float3 ([\+\-]?[0-9]+[Ee]\-[0-9]+)
116 float4 ([\+\-]?[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)
117 float5 ([Nn][Aa][Nn])
118 float ({float1}|{float2}|{float3}|{float4}|{float5})
120 /*
121 * Time constants.
122 */
123 date ([0-9]{4}-[0-9]{2}-[0-9]{2})
124 time ([0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2}(\.[0-9]{1,9})?)?)
126 %%
128 {whitespace} |
129 {simple_comment} { /* ignore */ }
131 {csc_start} { BEGIN(CSC); }
132 <CSC>{csc_inside} { /* ignore */ }
133 <CSC>{csc_end} { BEGIN(INITIAL); }
134 <CSC><<EOF>> {
135 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
136 return SCANNER_ERROR;
137 }
139 {identifier} {
140 size_t i;
141 for (i = 0; i < SDB_STATIC_ARRAY_LEN(reserved_words); ++i)
142 if (! strcasecmp(reserved_words[i].name, yytext))
143 return reserved_words[i].id;
145 yylval->str = strdup(yytext);
146 return IDENTIFIER;
147 }
148 {string} {
149 char *quot;
150 size_t len;
152 /* remove the leading and trailing quote */
153 yytext[yyleng - 1] = '\0';
154 yylval->str = strdup(yytext + 1);
156 quot = yylval->str;
157 len = yyleng - 2;
158 while ((quot = strstr(quot, "''")) != NULL) {
159 memmove(quot, quot + 1, len - (quot - yylval->str) - 1);
160 yylval->str[len - 1] = '\0';
161 --len;
162 ++quot;
163 }
164 return STRING;
165 }
166 {integer} {
167 yylval->data.data.integer = (int64_t)strtoll(yytext, NULL, 10);
168 yylval->data.type = SDB_TYPE_INTEGER;
169 return INTEGER;
170 }
171 {float} {
172 yylval->data.data.decimal = strtod(yytext, NULL);
173 yylval->data.type = SDB_TYPE_DECIMAL;
174 return FLOAT;
175 }
177 {date} {
178 struct tm tm;
179 memset(&tm, 0, sizeof(tm));
180 if (! strptime(yytext, "%Y-%m-%d", &tm)) {
181 char errmsg[1024];
182 snprintf(errmsg, sizeof(errmsg),
183 "Failed to parse '%s' as date", yytext);
184 sdb_fe_yyerror(yylloc, yyscanner, errmsg);
185 return SCANNER_ERROR;
186 }
187 yylval->datetime = SECS_TO_SDB_TIME(mktime(&tm));
188 return DATE;
189 }
190 {time} {
191 struct tm tm;
192 char time[9], ns[10];
193 char *tmp;
195 memset(&tm, 0, sizeof(tm));
196 memset(time, '\0', sizeof(time));
197 memset(ns, '0', sizeof(ns));
198 ns[sizeof(ns) - 1] = '\0';
200 tmp = strchr(yytext, '.');
201 if (tmp) {
202 size_t i;
203 *tmp = '\0';
204 ++tmp;
205 strncpy(ns, tmp, sizeof(ns));
206 for (i = strlen(ns); i < 9; ++i)
207 ns[i] = '0';
208 }
209 strncpy(time, yytext, sizeof(time));
210 if (tmp) {
211 /* reset for better error messages */
212 --tmp;
213 *tmp = '.';
214 }
216 tmp = strchr(time, ':');
217 assert(tmp);
218 tmp = strchr(tmp + 1, ':');
219 if (! tmp)
220 strncat(time, ":00", sizeof(time));
222 if (! strptime(time, "%H:%M:%S", &tm)) {
223 char errmsg[1024];
224 snprintf(errmsg, sizeof(errmsg),
225 "Failed to parse '%s' as time", yytext);
226 sdb_fe_yyerror(yylloc, yyscanner, errmsg);
227 return SCANNER_ERROR;
228 }
230 yylval->datetime = SECS_TO_SDB_TIME(mktime(&tm));
231 yylval->datetime += (sdb_time_t)strtoll(ns, NULL, 10);
232 return TIME;
233 }
235 = { return CMP_EQUAL; }
236 != { return CMP_NEQUAL; }
237 =~ { return CMP_REGEX; }
238 !~ { return CMP_NREGEX; }
239 \< { return CMP_LT; }
240 \<= { return CMP_LE; }
241 \>= { return CMP_GE; }
242 \> { return CMP_GT; }
243 \|\| { return CONCAT; }
245 . { /* XXX: */ return yytext[0]; }
247 %%
249 sdb_fe_yyscan_t
250 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
251 {
252 yyscan_t scanner;
254 if (! str)
255 return NULL;
257 if (sdb_fe_yylex_init(&scanner)) {
258 char errbuf[1024];
259 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
260 sdb_strerror(errno, errbuf, sizeof(errbuf)));
261 return NULL;
262 }
264 sdb_fe_yyset_extra(yyext, scanner);
266 if (len < 0)
267 len = strlen(str);
269 /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
270 * scanner and, thus, will be freed by yylex_destroy */
271 sdb_fe_yy_scan_bytes(str, len, scanner);
272 return scanner;
273 } /* sdb_fe_scanner_init */
275 void
276 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
277 {
278 sdb_fe_yylex_destroy(scanner);
279 } /* sdb_fe_scanner_destroy */
281 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */