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