Code

parser: Add support for <expr> IS [NOT] TRUE / FALSE queries.
[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 verbose
111 %option warn
112 %option prefix="sdb_parser_yy" outfile="lex.yy.c"
114 %x CSC
116 whitespace              ([ \t\n\r\f]+)
117 simple_comment  ("--"[^\n\r]*)
119 /*
120  * C style comments
121  */
122 csc_start       \/\*
123 csc_inside      ([^*/]+|[^*]\/|\*[^/])
124 csc_end         \*\/
126 /*
127  * Strings and identifiers.
128  */
129 identifier      ([A-Za-z_][A-Za-z_0-9$]*)
130 /* TODO: fully support SQL strings */
131 string          ('([^']|'')*')
133 /*
134  * Numeric constants.
135  */
136 dec                     ([\+\-]?[0-9]+)
137 exp                     ([\+\-]?[0-9]+[Ee]\+?[0-9]+)
138 integer         ({dec}|{exp})
139 float1          ([\+\-]?[0-9]+\.[0-9]*([Ee][\+\-]?[0-9]+)?)
140 float2          ([\+\-]?[0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?)
141 float3          ([\+\-]?[0-9]+[Ee]\-[0-9]+)
142 float4          ([\+\-]?[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)
143 float5          ([Nn][Aa][Nn])
144 float           ({float1}|{float2}|{float3}|{float4}|{float5})
146 /*
147  * Time constants.
148  */
149 date            ([0-9]{4}-[0-9]{2}-[0-9]{2})
150 time            ([0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2}(\.[0-9]{1,9})?)?)
152 %%
154 {whitespace} |
155 {simple_comment}        { /* ignore */ }
157 {csc_start}                     { BEGIN(CSC); }
158 <CSC>{csc_inside}       { /* ignore */ }
159 <CSC>{csc_end}          { BEGIN(INITIAL); }
160 <CSC><<EOF>> {
161                 sdb_parser_yyerror(yylloc, yyscanner, "unterminated C-style comment");
162                 return SCANNER_ERROR;
163         }
165 {identifier} {
166                 size_t i;
167                 for (i = 0; i < SDB_STATIC_ARRAY_LEN(reserved_words); ++i)
168                         if (! strcasecmp(reserved_words[i].name, yytext))
169                                 return reserved_words[i].id;
171                 yylval->str = strdup(yytext);
172                 return IDENTIFIER;
173         }
174 {string} {
175                 char *quot;
176                 size_t len;
178                 /* remove the leading and trailing quote */
179                 yytext[yyleng - 1] = '\0';
180                 yylval->str = strdup(yytext + 1);
182                 quot = yylval->str;
183                 len = yyleng - 2;
184                 while ((quot = strstr(quot, "''")) != NULL) {
185                         memmove(quot, quot + 1, len - (quot - yylval->str) - 1);
186                         yylval->str[len - 1] = '\0';
187                         --len;
188                         ++quot;
189                 }
190                 return STRING;
191         }
192 {integer} {
193                 yylval->data.data.integer = (int64_t)strtoll(yytext, NULL, 10);
194                 yylval->data.type = SDB_TYPE_INTEGER;
195                 return INTEGER;
196         }
197 {float} {
198                 yylval->data.data.decimal = strtod(yytext, NULL);
199                 yylval->data.type = SDB_TYPE_DECIMAL;
200                 return FLOAT;
201         }
203 {date} {
204                 struct tm tm;
205                 memset(&tm, 0, sizeof(tm));
206                 if (! strptime(yytext, "%Y-%m-%d", &tm)) {
207                         char errmsg[1024];
208                         snprintf(errmsg, sizeof(errmsg),
209                                 "Failed to parse '%s' as date", yytext);
210                         sdb_parser_yyerror(yylloc, yyscanner, errmsg);
211                         return SCANNER_ERROR;
212                 }
213                 yylval->datetime = SECS_TO_SDB_TIME(mktime(&tm));
214                 return DATE;
215         }
216 {time} {
217                 struct tm tm;
218                 char time[9], ns[10];
219                 char *tmp;
220                 int t;
222                 memset(&tm, 0, sizeof(tm));
223                 memset(time, '\0', sizeof(time));
224                 memset(ns, '0', sizeof(ns));
225                 ns[sizeof(ns) - 1] = '\0';
227                 tmp = strchr(yytext, '.');
228                 if (tmp) {
229                         size_t i;
230                         *tmp = '\0';
231                         ++tmp;
232                         strncpy(ns, tmp, sizeof(ns));
233                         for (i = strlen(ns); i < 9; ++i)
234                                 ns[i] = '0';
235                 }
236                 strncpy(time, yytext, sizeof(time));
237                 if (tmp) {
238                         /* reset for better error messages */
239                         --tmp;
240                         *tmp = '.';
241                 }
243                 tmp = strchr(time, ':');
244                 assert(tmp);
245                 tmp = strchr(tmp + 1, ':');
246                 if (! tmp)
247                         strncat(time, ":00", sizeof(time) - strlen(time) - 1);
249                 if (! strptime(time, "%H:%M:%S", &tm)) {
250                         char errmsg[1024];
251                         snprintf(errmsg, sizeof(errmsg),
252                                 "Failed to parse '%s' as time", yytext);
253                         sdb_parser_yyerror(yylloc, yyscanner, errmsg);
254                         return SCANNER_ERROR;
255                 }
257                 t = tm.tm_sec + 60 * tm.tm_min + 3600 * tm.tm_hour;
258                 yylval->datetime = SECS_TO_SDB_TIME(t);
259                 yylval->datetime += (sdb_time_t)strtoll(ns, NULL, 10);
260                 return TIME;
261         }
263 =       { return CMP_EQUAL; }
264 !=      { return CMP_NEQUAL; }
265 =~      { return CMP_REGEX; }
266 !~      { return CMP_NREGEX; }
267 \<      { return CMP_LT; }
268 \<=     { return CMP_LE; }
269 \>=     { return CMP_GE; }
270 \>      { return CMP_GT; }
271 \|\|    { return CONCAT; }
273 .       { /* XXX: */ return yytext[0]; }
275 %%
277 sdb_parser_yyscan_t
278 sdb_parser_scanner_init(const char *str, int len, sdb_parser_yyextra_t *yyext)
280         yyscan_t scanner;
282         if (! str)
283                 return NULL;
285         if (sdb_parser_yylex_init(&scanner)) {
286                 char errbuf[1024];
287                 sdb_strbuf_sprintf(yyext->errbuf, "yylex_init_failed: %s",
288                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
289                 return NULL;
290         }
292         sdb_parser_yyset_extra(yyext, scanner);
294         if (len < 0)
295                 len = strlen(str);
297         /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
298          * scanner and, thus, will be freed by yylex_destroy */
299         sdb_parser_yy_scan_bytes(str, len, scanner);
300         return scanner;
301 } /* sdb_parser_scanner_init */
303 void
304 sdb_parser_scanner_destroy(sdb_parser_yyscan_t scanner)
306         sdb_parser_yylex_destroy(scanner);
307 } /* sdb_parser_scanner_destroy */
309 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */