Code

Enable flex verbose output only in verbose build mode.
[sysdb.git] / src / parser / scanner.l
1 /*
2  * SysDB - src/parser/scanner.l
3  * Copyright (C) 2013-2015 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"
36 #include "parser/parser.h"
37 #include "parser/grammar.h"
39 #include "utils/error.h"
41 #include <assert.h>
42 #include <errno.h>
44 #include <string.h>
45 #include <stdlib.h>
47 #include <time.h>
49 #define YY_EXTRA_TYPE sdb_parser_yyextra_t *
51 static struct {
52         const char *name;
53         int id;
54 } reserved_words[] = {
55         { "ALL",         ALL },
56         { "AND",         AND },
57         { "ANY",         ANY },
58         { "END",         END },
59         { "FALSE",       FALSE },
60         { "FETCH",       FETCH },
61         { "FILTER",      FILTER },
62         { "IN",          IN },
63         { "IS",          IS },
64         { "LAST",        LAST },
65         { "LIST",        LIST },
66         { "LOOKUP",      LOOKUP },
67         { "MATCHING",    MATCHING },
68         { "NOT",         NOT },
69         { "NULL",        NULL_T },
70         { "OR",          OR },
71         { "START",       START },
72         { "STORE",       STORE },
73         { "TIMESERIES",  TIMESERIES },
74         { "TRUE",        TRUE },
75         { "UPDATE",      UPDATE },
77         /* object types */
78         { "host",        HOST_T },
79         { "hosts",       HOSTS_T },
80         { "service",     SERVICE_T },
81         { "services",    SERVICES_T },
82         { "metric",      METRIC_T },
83         { "metrics",     METRICS_T },
84         { "attribute",   ATTRIBUTE_T },
85         { "attributes",  ATTRIBUTES_T },
86         /* queryable fields */
87         { "name",        NAME_T },
88         { "last_update", LAST_UPDATE_T },
89         { "age",         AGE_T },
90         { "interval",    INTERVAL_T },
91         { "backend",     BACKEND_T },
92         { "value",       VALUE_T },
93 };
95 void
96 sdb_parser_yyerror(YYLTYPE *lval, sdb_parser_yyscan_t scanner, const char *msg);
98 %}
100 %option never-interactive
101 %option reentrant
102 %option bison-bridge
103 %option bison-locations
104 %option 8bit
105 %option yylineno
106 %option nodefault
107 %option noinput
108 %option nounput
109 %option noyywrap
110 %option warn
111 %option prefix="sdb_parser_yy" outfile="lex.yy.c"
113 %x CSC
115 whitespace              ([ \t\n\r\f]+)
116 simple_comment  ("--"[^\n\r]*)
118 /*
119  * C style comments
120  */
121 csc_start       \/\*
122 csc_inside      ([^*/]+|[^*]\/|\*[^/])
123 csc_end         \*\/
125 /*
126  * Strings and identifiers.
127  */
128 identifier      ([A-Za-z_][A-Za-z_0-9$]*)
129 /* TODO: fully support SQL strings */
130 string          ('([^']|'')*')
132 /*
133  * Numeric constants.
134  */
135 dec                     ([\+\-]?[0-9]+)
136 exp                     ([\+\-]?[0-9]+[Ee]\+?[0-9]+)
137 integer         ({dec}|{exp})
138 float1          ([\+\-]?[0-9]+\.[0-9]*([Ee][\+\-]?[0-9]+)?)
139 float2          ([\+\-]?[0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?)
140 float3          ([\+\-]?[0-9]+[Ee]\-[0-9]+)
141 float4          ([\+\-]?[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)
142 float5          ([Nn][Aa][Nn])
143 float           ({float1}|{float2}|{float3}|{float4}|{float5})
145 /*
146  * Time constants.
147  */
148 date            ([0-9]{4}-[0-9]{2}-[0-9]{2})
149 time            ([0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2}(\.[0-9]{1,9})?)?)
151 %%
153 {whitespace} |
154 {simple_comment}        { /* ignore */ }
156 {csc_start}                     { BEGIN(CSC); }
157 <CSC>{csc_inside}       { /* ignore */ }
158 <CSC>{csc_end}          { BEGIN(INITIAL); }
159 <CSC><<EOF>> {
160                 sdb_parser_yyerror(yylloc, yyscanner, "unterminated C-style comment");
161                 return SCANNER_ERROR;
162         }
164 {identifier} {
165                 size_t i;
166                 for (i = 0; i < SDB_STATIC_ARRAY_LEN(reserved_words); ++i)
167                         if (! strcasecmp(reserved_words[i].name, yytext))
168                                 return reserved_words[i].id;
170                 yylval->str = strdup(yytext);
171                 return IDENTIFIER;
172         }
173 {string} {
174                 char *quot;
175                 size_t len;
177                 /* remove the leading and trailing quote */
178                 yytext[yyleng - 1] = '\0';
179                 yylval->str = strdup(yytext + 1);
181                 quot = yylval->str;
182                 len = yyleng - 2;
183                 while ((quot = strstr(quot, "''")) != NULL) {
184                         memmove(quot, quot + 1, len - (quot - yylval->str) - 1);
185                         yylval->str[len - 1] = '\0';
186                         --len;
187                         ++quot;
188                 }
189                 return STRING;
190         }
191 {integer} {
192                 yylval->data.data.integer = (int64_t)strtoll(yytext, NULL, 10);
193                 yylval->data.type = SDB_TYPE_INTEGER;
194                 return INTEGER;
195         }
196 {float} {
197                 yylval->data.data.decimal = strtod(yytext, NULL);
198                 yylval->data.type = SDB_TYPE_DECIMAL;
199                 return FLOAT;
200         }
202 {date} {
203                 struct tm tm;
204                 memset(&tm, 0, sizeof(tm));
205                 if (! strptime(yytext, "%Y-%m-%d", &tm)) {
206                         char errmsg[1024];
207                         snprintf(errmsg, sizeof(errmsg),
208                                 "Failed to parse '%s' as date", yytext);
209                         sdb_parser_yyerror(yylloc, yyscanner, errmsg);
210                         return SCANNER_ERROR;
211                 }
212                 yylval->datetime = SECS_TO_SDB_TIME(mktime(&tm));
213                 return DATE;
214         }
215 {time} {
216                 struct tm tm;
217                 char time[9], ns[10];
218                 char *tmp;
219                 int t;
221                 memset(&tm, 0, sizeof(tm));
222                 memset(time, '\0', sizeof(time));
223                 memset(ns, '0', sizeof(ns));
224                 ns[sizeof(ns) - 1] = '\0';
226                 tmp = strchr(yytext, '.');
227                 if (tmp) {
228                         size_t i;
229                         *tmp = '\0';
230                         ++tmp;
231                         strncpy(ns, tmp, sizeof(ns));
232                         for (i = strlen(ns); i < 9; ++i)
233                                 ns[i] = '0';
234                 }
235                 strncpy(time, yytext, sizeof(time));
236                 if (tmp) {
237                         /* reset for better error messages */
238                         --tmp;
239                         *tmp = '.';
240                 }
242                 tmp = strchr(time, ':');
243                 assert(tmp);
244                 tmp = strchr(tmp + 1, ':');
245                 if (! tmp)
246                         strncat(time, ":00", sizeof(time) - strlen(time) - 1);
248                 if (! strptime(time, "%H:%M:%S", &tm)) {
249                         char errmsg[1024];
250                         snprintf(errmsg, sizeof(errmsg),
251                                 "Failed to parse '%s' as time", yytext);
252                         sdb_parser_yyerror(yylloc, yyscanner, errmsg);
253                         return SCANNER_ERROR;
254                 }
256                 t = tm.tm_sec + 60 * tm.tm_min + 3600 * tm.tm_hour;
257                 yylval->datetime = SECS_TO_SDB_TIME(t);
258                 yylval->datetime += (sdb_time_t)strtoll(ns, NULL, 10);
259                 return TIME;
260         }
262 =       { return CMP_EQUAL; }
263 !=      { return CMP_NEQUAL; }
264 =~      { return CMP_REGEX; }
265 !~      { return CMP_NREGEX; }
266 \<      { return CMP_LT; }
267 \<=     { return CMP_LE; }
268 \>=     { return CMP_GE; }
269 \>      { return CMP_GT; }
270 \|\|    { return CONCAT; }
272 .       { /* XXX: */ return yytext[0]; }
274 %%
276 sdb_parser_yyscan_t
277 sdb_parser_scanner_init(const char *str, int len, sdb_parser_yyextra_t *yyext)
279         yyscan_t scanner;
281         if (! str)
282                 return NULL;
284         if (sdb_parser_yylex_init(&scanner)) {
285                 char errbuf[1024];
286                 sdb_strbuf_sprintf(yyext->errbuf, "yylex_init_failed: %s",
287                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
288                 return NULL;
289         }
291         sdb_parser_yyset_extra(yyext, scanner);
293         if (len < 0)
294                 len = strlen(str);
296         /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
297          * scanner and, thus, will be freed by yylex_destroy */
298         sdb_parser_yy_scan_bytes(str, len, scanner);
299         return scanner;
300 } /* sdb_parser_scanner_init */
302 void
303 sdb_parser_scanner_destroy(sdb_parser_yyscan_t scanner)
305         sdb_parser_yylex_destroy(scanner);
306 } /* sdb_parser_scanner_destroy */
308 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */