Code

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