Code

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