Code

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