Code

f70f60f84dab33618a7ae84a67771ec33e0a022f
[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 static struct {
48         const char *name;
49         int id;
50 } reserved_words[] = {
51         { "AND",      AND },
52         { "FETCH",    FETCH },
53         { "IS",       IS },
54         { "LIST",     LIST },
55         { "LOOKUP",   LOOKUP },
56         { "MATCHING", MATCHING },
57         { "NOT",      NOT },
58         { "NULL",     NULL_T },
59         { "OR",       OR },
60 };
62 void
63 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
65 %}
67 %option never-interactive
68 %option reentrant
69 %option bison-bridge
70 %option bison-locations
71 %option 8bit
72 %option yylineno
73 %option nodefault
74 %option noinput
75 %option nounput
76 %option noyywrap
77 %option verbose
78 %option warn
79 %option prefix="sdb_fe_yy" outfile="lex.yy.c"
81 %x CSC
83 whitespace              ([ \t\n\r\f]+)
84 simple_comment  ("--"[^\n\r]*)
86 /*
87  * C style comments
88  */
89 csc_start       \/\*
90 csc_inside      ([^*/]+|[^*]\/|\*[^/])
91 csc_end         \*\/
93 /*
94  * Strings and identifiers.
95  */
96 identifier      ([A-Za-z_][A-Za-z_0-9$]*)
97 /* TODO: fully support SQL strings */
98 string          ('[^']*')
100 /*
101  * Numeric constants.
102  */
103 dec                     ([\+\-]?[0-9]+)
104 exp                     ([\+\-]?[0-9]+[Ee]\+?[0-9]+)
105 integer         ({dec}|{exp})
106 float1          ([\+\-]?[0-9]+\.[0-9]*([Ee][\+\-]?[0-9]+)?)
107 float2          ([\+\-]?[0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?)
108 float3          ([\+\-]?[0-9]+[Ee]\-[0-9]+)
109 float4          ([\+\-]?[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)
110 float5          ([Nn][Aa][Nn])
111 float           ({float1}|{float2}|{float3}|{float4}|{float5})
113 %%
115 {whitespace} |
116 {simple_comment}        { /* ignore */ }
118 {csc_start}                     { BEGIN(CSC); }
119 <CSC>{csc_inside}       { /* ignore */ }
120 <CSC>{csc_end}          { BEGIN(INITIAL); }
121 <CSC><<EOF>> {
122                 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
123                 return SCANNER_ERROR;
124         }
126 {identifier} {
127                 size_t i;
128                 for (i = 0; i < SDB_STATIC_ARRAY_LEN(reserved_words); ++i)
129                         if (! strcasecmp(reserved_words[i].name, yytext))
130                                 return reserved_words[i].id;
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)
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)
194         sdb_fe_yylex_destroy(scanner);
195 } /* sdb_fe_scanner_destroy */
197 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */