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 <errno.h>
42 #include <string.h>
43 #include <stdlib.h>
45 #define YY_EXTRA_TYPE sdb_fe_yyextra_t *
47 static struct {
48 const char *name;
49 int id;
50 } reserved_words[] = {
51 { "AND", AND },
52 { "FETCH", FETCH },
53 { "FILTER", FILTER },
54 { "IS", IS },
55 { "LIST", LIST },
56 { "LOOKUP", LOOKUP },
57 { "MATCHING", MATCHING },
58 { "NOT", NOT },
59 { "NULL", NULL_T },
60 { "OR", OR },
61 };
63 void
64 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
66 %}
68 %option never-interactive
69 %option reentrant
70 %option bison-bridge
71 %option bison-locations
72 %option 8bit
73 %option yylineno
74 %option nodefault
75 %option noinput
76 %option nounput
77 %option noyywrap
78 %option verbose
79 %option warn
80 %option prefix="sdb_fe_yy" outfile="lex.yy.c"
82 %x CSC
84 whitespace ([ \t\n\r\f]+)
85 simple_comment ("--"[^\n\r]*)
87 /*
88 * C style comments
89 */
90 csc_start \/\*
91 csc_inside ([^*/]+|[^*]\/|\*[^/])
92 csc_end \*\/
94 /*
95 * Strings and identifiers.
96 */
97 identifier ([A-Za-z_][A-Za-z_0-9$]*)
98 /* TODO: fully support SQL strings */
99 string ('[^']*')
101 /*
102 * Numeric constants.
103 */
104 dec ([\+\-]?[0-9]+)
105 exp ([\+\-]?[0-9]+[Ee]\+?[0-9]+)
106 integer ({dec}|{exp})
107 float1 ([\+\-]?[0-9]+\.[0-9]*([Ee][\+\-]?[0-9]+)?)
108 float2 ([\+\-]?[0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?)
109 float3 ([\+\-]?[0-9]+[Ee]\-[0-9]+)
110 float4 ([\+\-]?[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)
111 float5 ([Nn][Aa][Nn])
112 float ({float1}|{float2}|{float3}|{float4}|{float5})
114 %%
116 {whitespace} |
117 {simple_comment} { /* ignore */ }
119 {csc_start} { BEGIN(CSC); }
120 <CSC>{csc_inside} { /* ignore */ }
121 <CSC>{csc_end} { BEGIN(INITIAL); }
122 <CSC><<EOF>> {
123 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
124 return SCANNER_ERROR;
125 }
127 {identifier} {
128 size_t i;
129 for (i = 0; i < SDB_STATIC_ARRAY_LEN(reserved_words); ++i)
130 if (! strcasecmp(reserved_words[i].name, yytext))
131 return reserved_words[i].id;
133 yylval->str = strdup(yytext);
134 return IDENTIFIER;
135 }
136 {string} {
137 yytext[yyleng - 1] = '\0';
138 yylval->str = strdup(yytext + 1);
139 return STRING;
140 }
141 {integer} {
142 yylval->data.data.integer = (int64_t)strtoll(yytext, NULL, 10);
143 yylval->data.type = SDB_TYPE_INTEGER;
144 return INTEGER;
145 }
146 {float} {
147 yylval->data.data.decimal = strtod(yytext, NULL);
148 yylval->data.type = SDB_TYPE_DECIMAL;
149 return FLOAT;
150 }
152 = { return CMP_EQUAL; }
153 != { return CMP_NEQUAL; }
154 =~ { return CMP_REGEX; }
155 !~ { return CMP_NREGEX; }
156 \< { return CMP_LT; }
157 \<= { return CMP_LE; }
158 \>= { return CMP_GE; }
159 \> { return CMP_GT; }
160 \|\| { return CONCAT; }
162 . { /* XXX: */ return yytext[0]; }
164 %%
166 sdb_fe_yyscan_t
167 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
168 {
169 yyscan_t scanner;
171 if (! str)
172 return NULL;
174 if (sdb_fe_yylex_init(&scanner)) {
175 char errbuf[1024];
176 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
177 sdb_strerror(errno, errbuf, sizeof(errbuf)));
178 return NULL;
179 }
181 sdb_fe_yyset_extra(yyext, scanner);
183 if (len < 0)
184 len = strlen(str);
186 /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
187 * scanner and, thus, will be freed by yylex_destroy */
188 sdb_fe_yy_scan_bytes(str, len, scanner);
189 return scanner;
190 } /* sdb_fe_scanner_init */
192 void
193 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
194 {
195 sdb_fe_yylex_destroy(scanner);
196 } /* sdb_fe_scanner_destroy */
198 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */