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 identifier ([A-Za-z_][A-Za-z_0-9$]*)
79 /* TODO: fully support SQL strings */
80 string ('[^']*')
82 dec ([\+\-]?[0-9]+)
83 exp ([\+\-]?[0-9]+[Ee]\+?[0-9]+)
84 integer ({dec}|{exp})
85 float1 ([\+\-]?[0-9]+\.[0-9]*([Ee][\+\-]?[0-9]+)?)
86 float2 ([\+\-]?[0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?)
87 float3 ([\+\-]?[0-9]+[Ee]\-[0-9]+)
88 float4 ([\+\-]?[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)
89 float5 ([Nn][Aa][Nn])
90 float ({float1}|{float2}|{float3}|{float4}|{float5})
92 %%
94 {whitespace} |
95 {simple_comment} { /* ignore */ }
97 {csc_start} { BEGIN(CSC); }
98 <CSC>{csc_inside} { /* ignore */ }
99 <CSC>{csc_end} { BEGIN(INITIAL); }
100 <CSC><<EOF>> {
101 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
102 return SCANNER_ERROR;
103 }
105 {identifier} {
106 /* XXX: simplify handling of reserved words */
107 if (! strcasecmp(yytext, "AND"))
108 return AND;
109 else if (! strcasecmp(yytext, "FETCH"))
110 return FETCH;
111 else if (! strcasecmp(yytext, "LIST"))
112 return LIST;
113 else if (! strcasecmp(yytext, "LOOKUP"))
114 return LOOKUP;
115 else if (! strcasecmp(yytext, "NOT"))
116 return NOT;
117 else if (! strcasecmp(yytext, "OR"))
118 return OR;
119 else if (! strcasecmp(yytext, "WHERE"))
120 return WHERE;
122 yylval->str = strdup(yytext);
123 return IDENTIFIER;
124 }
125 {string} {
126 yytext[yyleng - 1] = '\0';
127 yylval->str = strdup(yytext + 1);
128 return STRING;
129 }
130 {integer} {
131 yylval->data.data.integer = (int64_t)strtoll(yytext, NULL, 10);
132 yylval->data.type = SDB_TYPE_INTEGER;
133 return INTEGER;
134 }
135 {float} {
136 yylval->data.data.decimal = strtod(yytext, NULL);
137 yylval->data.type = SDB_TYPE_DECIMAL;
138 return FLOAT;
139 }
141 = { return CMP_EQUAL; }
142 != { return CMP_NEQUAL; }
143 =~ { return CMP_REGEX; }
144 !~ { return CMP_NREGEX; }
145 \< { return CMP_LT; }
146 \<= { return CMP_LE; }
147 \>= { return CMP_GE; }
148 \> { return CMP_GT; }
150 . { /* XXX: */ return yytext[0]; }
152 %%
154 sdb_fe_yyscan_t
155 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
156 {
157 yyscan_t scanner;
159 if (! str)
160 return NULL;
162 if (sdb_fe_yylex_init(&scanner)) {
163 char errbuf[1024];
164 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
165 sdb_strerror(errno, errbuf, sizeof(errbuf)));
166 return NULL;
167 }
169 sdb_fe_yyset_extra(yyext, scanner);
171 if (len < 0)
172 len = strlen(str);
174 /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
175 * scanner and, thus, will be freed by yylex_destroy */
176 sdb_fe_yy_scan_bytes(str, len, scanner);
177 return scanner;
178 } /* sdb_fe_scanner_init */
180 void
181 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
182 {
183 sdb_fe_yylex_destroy(scanner);
184 } /* sdb_fe_scanner_destroy */
186 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */