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