Code

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