Code

Query language: Changed 'LOOKUP .. WHERE' to 'LOOKUP .. MATCHING'.
[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 <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, "IS"))
112                         return IS;
113                 else if (! strcasecmp(yytext, "LIST"))
114                         return LIST;
115                 else if (! strcasecmp(yytext, "LOOKUP"))
116                         return LOOKUP;
117                 else if (! strcasecmp(yytext, "MATCHING"))
118                         return MATCHING;
119                 else if (! strcasecmp(yytext, "NOT"))
120                         return NOT;
121                 else if (! strcasecmp(yytext, "NULL"))
122                         return NULL_T;
123                 else if (! strcasecmp(yytext, "OR"))
124                         return OR;
126                 yylval->str = strdup(yytext);
127                 return IDENTIFIER;
128         }
129 {string} {
130                 yytext[yyleng - 1] = '\0';
131                 yylval->str = strdup(yytext + 1);
132                 return STRING;
133         }
134 {integer} {
135                 yylval->data.data.integer = (int64_t)strtoll(yytext, NULL, 10);
136                 yylval->data.type = SDB_TYPE_INTEGER;
137                 return INTEGER;
138         }
139 {float} {
140                 yylval->data.data.decimal = strtod(yytext, NULL);
141                 yylval->data.type = SDB_TYPE_DECIMAL;
142                 return FLOAT;
143         }
145 =       { return CMP_EQUAL; }
146 !=      { return CMP_NEQUAL; }
147 =~      { return CMP_REGEX; }
148 !~      { return CMP_NREGEX; }
149 \<      { return CMP_LT; }
150 \<=     { return CMP_LE; }
151 \>=     { return CMP_GE; }
152 \>      { return CMP_GT; }
154 .       { /* XXX: */ return yytext[0]; }
156 %%
158 sdb_fe_yyscan_t
159 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
161         yyscan_t scanner;
163         if (! str)
164                 return NULL;
166         if (sdb_fe_yylex_init(&scanner)) {
167                 char errbuf[1024];
168                 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
169                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
170                 return NULL;
171         }
173         sdb_fe_yyset_extra(yyext, scanner);
175         if (len < 0)
176                 len = strlen(str);
178         /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
179          * scanner and, thus, will be freed by yylex_destroy */
180         sdb_fe_yy_scan_bytes(str, len, scanner);
181         return scanner;
182 } /* sdb_fe_scanner_init */
184 void
185 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
187         sdb_fe_yylex_destroy(scanner);
188 } /* sdb_fe_scanner_destroy */
190 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */