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 "frontend/connection.h"
35 #include "frontend/parser.h"
36 #include "frontend/grammar.h"
37 #include "utils/error.h"
39 #include <errno.h>
41 #include <string.h>
43 #define YY_EXTRA_TYPE sdb_fe_yyextra_t *
45 void
46 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
48 %}
50 %option never-interactive
51 %option reentrant
52 %option bison-bridge
53 %option bison-locations
54 %option 8bit
55 %option yylineno
56 %option nodefault
57 %option noinput
58 %option nounput
59 %option noyywrap
60 %option verbose
61 %option warn
62 %option prefix="sdb_fe_yy" outfile="lex.yy.c"
64 %x CSC
66 whitespace ([ \t\n\r\f]+)
67 simple_comment ("--"[^\n\r]*)
69 /*
70 * C style comments
71 */
72 csc_start \/\*
73 csc_inside ([^*/]+|[^*]\/|\*[^/])
74 csc_end \*\/
76 identifier ([A-Za-z_][A-Za-z_0-9$]*)
77 /* TODO: fully support SQL strings */
78 string ('[^']*')
80 %%
82 {whitespace} |
83 {simple_comment} { /* ignore */ }
85 {csc_start} { BEGIN(CSC); }
86 <CSC>{csc_inside} { /* ignore */ }
87 <CSC>{csc_end} { BEGIN(INITIAL); }
88 <CSC><<EOF>> {
89 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
90 return SCANNER_ERROR;
91 }
93 {identifier} {
94 /* XXX: simplify handling of reserved words */
95 if (! strcasecmp(yytext, "AND"))
96 return AND;
97 else if (! strcasecmp(yytext, "FETCH"))
98 return FETCH;
99 else if (! strcasecmp(yytext, "LIST"))
100 return LIST;
101 else if (! strcasecmp(yytext, "LOOKUP"))
102 return LOOKUP;
103 else if (! strcasecmp(yytext, "NOT"))
104 return NOT;
105 else if (! strcasecmp(yytext, "OR"))
106 return OR;
107 else if (! strcasecmp(yytext, "WHERE"))
108 return WHERE;
110 yylval->str = strdup(yytext);
111 return IDENTIFIER;
112 }
113 {string} {
114 yytext[yyleng - 1] = '\0';
115 yylval->str = strdup(yytext + 1);
116 return STRING;
117 }
119 = { return CMP_EQUAL; }
120 != { return CMP_NEQUAL; }
121 =~ { return CMP_REGEX; }
122 !~ { return CMP_NREGEX; }
124 . { /* XXX: */ return yytext[0]; }
126 %%
128 sdb_fe_yyscan_t
129 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
130 {
131 yyscan_t scanner;
133 if (! str)
134 return NULL;
136 if (sdb_fe_yylex_init(&scanner)) {
137 char errbuf[1024];
138 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
139 sdb_strerror(errno, errbuf, sizeof(errbuf)));
140 return NULL;
141 }
143 sdb_fe_yyset_extra(yyext, scanner);
145 if (len < 0)
146 len = strlen(str);
148 /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
149 * scanner and, thus, will be freed by yylex_destroy */
150 sdb_fe_yy_scan_bytes(str, len, scanner);
151 return scanner;
152 } /* sdb_fe_scanner_init */
154 void
155 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
156 {
157 sdb_fe_yylex_destroy(scanner);
158 } /* sdb_fe_scanner_destroy */
160 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */