Code

frontend/grammar: Added support for 'ALL <obj> <cmp> <expr>'.
[sysdb.git] / src / frontend / scanner.l
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         { "LIST",       LIST },
63         { "LOOKUP",     LOOKUP },
64         { "MATCHING",   MATCHING },
65         { "NOT",        NOT },
66         { "NULL",       NULL_T },
67         { "OR",         OR },
68         { "START",      START },
69         { "TIMESERIES", TIMESERIES },
70 };
72 void
73 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
75 %}
77 %option never-interactive
78 %option reentrant
79 %option bison-bridge
80 %option bison-locations
81 %option 8bit
82 %option yylineno
83 %option nodefault
84 %option noinput
85 %option nounput
86 %option noyywrap
87 %option verbose
88 %option warn
89 %option prefix="sdb_fe_yy" outfile="lex.yy.c"
91 %x CSC
93 whitespace              ([ \t\n\r\f]+)
94 simple_comment  ("--"[^\n\r]*)
96 /*
97  * C style comments
98  */
99 csc_start       \/\*
100 csc_inside      ([^*/]+|[^*]\/|\*[^/])
101 csc_end         \*\/
103 /*
104  * Strings and identifiers.
105  */
106 identifier      ([A-Za-z_][A-Za-z_0-9$]*)
107 /* TODO: fully support SQL strings */
108 string          ('([^']|'')*')
110 /*
111  * Numeric constants.
112  */
113 dec                     ([\+\-]?[0-9]+)
114 exp                     ([\+\-]?[0-9]+[Ee]\+?[0-9]+)
115 integer         ({dec}|{exp})
116 float1          ([\+\-]?[0-9]+\.[0-9]*([Ee][\+\-]?[0-9]+)?)
117 float2          ([\+\-]?[0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?)
118 float3          ([\+\-]?[0-9]+[Ee]\-[0-9]+)
119 float4          ([\+\-]?[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)
120 float5          ([Nn][Aa][Nn])
121 float           ({float1}|{float2}|{float3}|{float4}|{float5})
123 /*
124  * Time constants.
125  */
126 date            ([0-9]{4}-[0-9]{2}-[0-9]{2})
127 time            ([0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2}(\.[0-9]{1,9})?)?)
129 %%
131 {whitespace} |
132 {simple_comment}        { /* ignore */ }
134 {csc_start}                     { BEGIN(CSC); }
135 <CSC>{csc_inside}       { /* ignore */ }
136 <CSC>{csc_end}          { BEGIN(INITIAL); }
137 <CSC><<EOF>> {
138                 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
139                 return SCANNER_ERROR;
140         }
142 {identifier} {
143                 size_t i;
144                 for (i = 0; i < SDB_STATIC_ARRAY_LEN(reserved_words); ++i)
145                         if (! strcasecmp(reserved_words[i].name, yytext))
146                                 return reserved_words[i].id;
148                 yylval->str = strdup(yytext);
149                 return IDENTIFIER;
150         }
151 {string} {
152                 char *quot;
153                 size_t len;
155                 /* remove the leading and trailing quote */
156                 yytext[yyleng - 1] = '\0';
157                 yylval->str = strdup(yytext + 1);
159                 quot = yylval->str;
160                 len = yyleng - 2;
161                 while ((quot = strstr(quot, "''")) != NULL) {
162                         memmove(quot, quot + 1, len - (quot - yylval->str) - 1);
163                         yylval->str[len - 1] = '\0';
164                         --len;
165                         ++quot;
166                 }
167                 return STRING;
168         }
169 {integer} {
170                 yylval->data.data.integer = (int64_t)strtoll(yytext, NULL, 10);
171                 yylval->data.type = SDB_TYPE_INTEGER;
172                 return INTEGER;
173         }
174 {float} {
175                 yylval->data.data.decimal = strtod(yytext, NULL);
176                 yylval->data.type = SDB_TYPE_DECIMAL;
177                 return FLOAT;
178         }
180 {date} {
181                 struct tm tm;
182                 memset(&tm, 0, sizeof(tm));
183                 if (! strptime(yytext, "%Y-%m-%d", &tm)) {
184                         char errmsg[1024];
185                         snprintf(errmsg, sizeof(errmsg),
186                                 "Failed to parse '%s' as date", yytext);
187                         sdb_fe_yyerror(yylloc, yyscanner, errmsg);
188                         return SCANNER_ERROR;
189                 }
190                 yylval->datetime = SECS_TO_SDB_TIME(mktime(&tm));
191                 return DATE;
192         }
193 {time} {
194                 struct tm tm;
195                 char time[9], ns[10];
196                 char *tmp;
198                 memset(&tm, 0, sizeof(tm));
199                 memset(time, '\0', sizeof(time));
200                 memset(ns, '0', sizeof(ns));
201                 ns[sizeof(ns) - 1] = '\0';
203                 tmp = strchr(yytext, '.');
204                 if (tmp) {
205                         size_t i;
206                         *tmp = '\0';
207                         ++tmp;
208                         strncpy(ns, tmp, sizeof(ns));
209                         for (i = strlen(ns); i < 9; ++i)
210                                 ns[i] = '0';
211                 }
212                 strncpy(time, yytext, sizeof(time));
213                 if (tmp) {
214                         /* reset for better error messages */
215                         --tmp;
216                         *tmp = '.';
217                 }
219                 tmp = strchr(time, ':');
220                 assert(tmp);
221                 tmp = strchr(tmp + 1, ':');
222                 if (! tmp)
223                         strncat(time, ":00", sizeof(time));
225                 if (! strptime(time, "%H:%M:%S", &tm)) {
226                         char errmsg[1024];
227                         snprintf(errmsg, sizeof(errmsg),
228                                 "Failed to parse '%s' as time", yytext);
229                         sdb_fe_yyerror(yylloc, yyscanner, errmsg);
230                         return SCANNER_ERROR;
231                 }
233                 yylval->datetime = SECS_TO_SDB_TIME(mktime(&tm));
234                 yylval->datetime += (sdb_time_t)strtoll(ns, NULL, 10);
235                 return TIME;
236         }
238 =       { return CMP_EQUAL; }
239 !=      { return CMP_NEQUAL; }
240 =~      { return CMP_REGEX; }
241 !~      { return CMP_NREGEX; }
242 \<      { return CMP_LT; }
243 \<=     { return CMP_LE; }
244 \>=     { return CMP_GE; }
245 \>      { return CMP_GT; }
246 \|\|    { return CONCAT; }
248 .       { /* XXX: */ return yytext[0]; }
250 %%
252 sdb_fe_yyscan_t
253 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
255         yyscan_t scanner;
257         if (! str)
258                 return NULL;
260         if (sdb_fe_yylex_init(&scanner)) {
261                 char errbuf[1024];
262                 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
263                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
264                 return NULL;
265         }
267         sdb_fe_yyset_extra(yyext, scanner);
269         if (len < 0)
270                 len = strlen(str);
272         /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
273          * scanner and, thus, will be freed by yylex_destroy */
274         sdb_fe_yy_scan_bytes(str, len, scanner);
275         return scanner;
276 } /* sdb_fe_scanner_init */
278 void
279 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
281         sdb_fe_yylex_destroy(scanner);
282 } /* sdb_fe_scanner_destroy */
284 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */