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 { "ALL", ALL },
55 { "AND", AND },
56 { "ANY", ANY },
57 { "END", END },
58 { "FETCH", FETCH },
59 { "FILTER", FILTER },
60 { "IN", IN },
61 { "IS", IS },
62 { "LAST", LAST },
63 { "LIST", LIST },
64 { "LOOKUP", LOOKUP },
65 { "MATCHING", MATCHING },
66 { "NOT", NOT },
67 { "NULL", NULL_T },
68 { "OR", OR },
69 { "START", START },
70 { "STORE", STORE },
71 { "TIMESERIES", TIMESERIES },
72 { "UPDATE", UPDATE },
74 /* object types */
75 { "host", HOST_T },
76 { "hosts", HOSTS_T },
77 { "service", SERVICE_T },
78 { "services", SERVICES_T },
79 { "metric", METRIC_T },
80 { "metrics", METRICS_T },
81 { "attribute", ATTRIBUTE_T },
82 { "attributes", ATTRIBUTES_T },
83 /* queryable fields */
84 { "name", NAME_T },
85 { "last_update", LAST_UPDATE_T },
86 { "age", AGE_T },
87 { "interval", INTERVAL_T },
88 { "backend", BACKEND_T },
89 };
91 void
92 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
94 %}
96 %option never-interactive
97 %option reentrant
98 %option bison-bridge
99 %option bison-locations
100 %option 8bit
101 %option yylineno
102 %option nodefault
103 %option noinput
104 %option nounput
105 %option noyywrap
106 %option verbose
107 %option warn
108 %option prefix="sdb_fe_yy" outfile="lex.yy.c"
110 %x CSC
112 whitespace ([ \t\n\r\f]+)
113 simple_comment ("--"[^\n\r]*)
115 /*
116 * C style comments
117 */
118 csc_start \/\*
119 csc_inside ([^*/]+|[^*]\/|\*[^/])
120 csc_end \*\/
122 /*
123 * Strings and identifiers.
124 */
125 identifier ([A-Za-z_][A-Za-z_0-9$]*)
126 /* TODO: fully support SQL strings */
127 string ('([^']|'')*')
129 /*
130 * Numeric constants.
131 */
132 dec ([\+\-]?[0-9]+)
133 exp ([\+\-]?[0-9]+[Ee]\+?[0-9]+)
134 integer ({dec}|{exp})
135 float1 ([\+\-]?[0-9]+\.[0-9]*([Ee][\+\-]?[0-9]+)?)
136 float2 ([\+\-]?[0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?)
137 float3 ([\+\-]?[0-9]+[Ee]\-[0-9]+)
138 float4 ([\+\-]?[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)
139 float5 ([Nn][Aa][Nn])
140 float ({float1}|{float2}|{float3}|{float4}|{float5})
142 /*
143 * Time constants.
144 */
145 date ([0-9]{4}-[0-9]{2}-[0-9]{2})
146 time ([0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2}(\.[0-9]{1,9})?)?)
148 %%
150 {whitespace} |
151 {simple_comment} { /* ignore */ }
153 {csc_start} { BEGIN(CSC); }
154 <CSC>{csc_inside} { /* ignore */ }
155 <CSC>{csc_end} { BEGIN(INITIAL); }
156 <CSC><<EOF>> {
157 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
158 return SCANNER_ERROR;
159 }
161 {identifier} {
162 size_t i;
163 for (i = 0; i < SDB_STATIC_ARRAY_LEN(reserved_words); ++i)
164 if (! strcasecmp(reserved_words[i].name, yytext))
165 return reserved_words[i].id;
167 yylval->str = strdup(yytext);
168 return IDENTIFIER;
169 }
170 {string} {
171 char *quot;
172 size_t len;
174 /* remove the leading and trailing quote */
175 yytext[yyleng - 1] = '\0';
176 yylval->str = strdup(yytext + 1);
178 quot = yylval->str;
179 len = yyleng - 2;
180 while ((quot = strstr(quot, "''")) != NULL) {
181 memmove(quot, quot + 1, len - (quot - yylval->str) - 1);
182 yylval->str[len - 1] = '\0';
183 --len;
184 ++quot;
185 }
186 return STRING;
187 }
188 {integer} {
189 yylval->data.data.integer = (int64_t)strtoll(yytext, NULL, 10);
190 yylval->data.type = SDB_TYPE_INTEGER;
191 return INTEGER;
192 }
193 {float} {
194 yylval->data.data.decimal = strtod(yytext, NULL);
195 yylval->data.type = SDB_TYPE_DECIMAL;
196 return FLOAT;
197 }
199 {date} {
200 struct tm tm;
201 memset(&tm, 0, sizeof(tm));
202 if (! strptime(yytext, "%Y-%m-%d", &tm)) {
203 char errmsg[1024];
204 snprintf(errmsg, sizeof(errmsg),
205 "Failed to parse '%s' as date", yytext);
206 sdb_fe_yyerror(yylloc, yyscanner, errmsg);
207 return SCANNER_ERROR;
208 }
209 yylval->datetime = SECS_TO_SDB_TIME(mktime(&tm));
210 return DATE;
211 }
212 {time} {
213 struct tm tm;
214 char time[9], ns[10];
215 char *tmp;
216 int t;
218 memset(&tm, 0, sizeof(tm));
219 memset(time, '\0', sizeof(time));
220 memset(ns, '0', sizeof(ns));
221 ns[sizeof(ns) - 1] = '\0';
223 tmp = strchr(yytext, '.');
224 if (tmp) {
225 size_t i;
226 *tmp = '\0';
227 ++tmp;
228 strncpy(ns, tmp, sizeof(ns));
229 for (i = strlen(ns); i < 9; ++i)
230 ns[i] = '0';
231 }
232 strncpy(time, yytext, sizeof(time));
233 if (tmp) {
234 /* reset for better error messages */
235 --tmp;
236 *tmp = '.';
237 }
239 tmp = strchr(time, ':');
240 assert(tmp);
241 tmp = strchr(tmp + 1, ':');
242 if (! tmp)
243 strncat(time, ":00", sizeof(time) - strlen(time) - 1);
245 if (! strptime(time, "%H:%M:%S", &tm)) {
246 char errmsg[1024];
247 snprintf(errmsg, sizeof(errmsg),
248 "Failed to parse '%s' as time", yytext);
249 sdb_fe_yyerror(yylloc, yyscanner, errmsg);
250 return SCANNER_ERROR;
251 }
253 t = tm.tm_sec + 60 * tm.tm_min + 3600 * tm.tm_hour;
254 yylval->datetime = SECS_TO_SDB_TIME(t);
255 yylval->datetime += (sdb_time_t)strtoll(ns, NULL, 10);
256 return TIME;
257 }
259 = { return CMP_EQUAL; }
260 != { return CMP_NEQUAL; }
261 =~ { return CMP_REGEX; }
262 !~ { return CMP_NREGEX; }
263 \< { return CMP_LT; }
264 \<= { return CMP_LE; }
265 \>= { return CMP_GE; }
266 \> { return CMP_GT; }
267 \|\| { return CONCAT; }
269 . { /* XXX: */ return yytext[0]; }
271 %%
273 sdb_fe_yyscan_t
274 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
275 {
276 yyscan_t scanner;
278 if (! str)
279 return NULL;
281 if (sdb_fe_yylex_init(&scanner)) {
282 char errbuf[1024];
283 sdb_strbuf_sprintf(yyext->errbuf, "yylex_init_failed: %s",
284 sdb_strerror(errno, errbuf, sizeof(errbuf)));
285 return NULL;
286 }
288 sdb_fe_yyset_extra(yyext, scanner);
290 if (len < 0)
291 len = strlen(str);
293 /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
294 * scanner and, thus, will be freed by yylex_destroy */
295 sdb_fe_yy_scan_bytes(str, len, scanner);
296 return scanner;
297 } /* sdb_fe_scanner_init */
299 void
300 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
301 {
302 sdb_fe_yylex_destroy(scanner);
303 } /* sdb_fe_scanner_destroy */
305 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */