32c13c54c355865d4024e94f683a06b6330b9492
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, "OR"))
104 return OR;
105 else if (! strcasecmp(yytext, "WHERE"))
106 return WHERE;
108 yylval->str = strdup(yytext);
109 return IDENTIFIER;
110 }
111 {string} {
112 yytext[yyleng - 1] = '\0';
113 yylval->str = strdup(yytext + 1);
114 return STRING;
115 }
117 = { return CMP_EQUAL; }
118 != { return CMP_NEQUAL; }
119 =~ { return CMP_REGEX; }
120 !~ { return CMP_NREGEX; }
122 . { /* XXX: */ return yytext[0]; }
124 %%
126 sdb_fe_yyscan_t
127 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
128 {
129 yyscan_t scanner;
131 if (! str)
132 return NULL;
134 if (sdb_fe_yylex_init(&scanner)) {
135 char errbuf[1024];
136 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
137 sdb_strerror(errno, errbuf, sizeof(errbuf)));
138 return NULL;
139 }
141 sdb_fe_yyset_extra(yyext, scanner);
143 if (len < 0)
144 len = strlen(str);
146 /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
147 * scanner and, thus, will be freed by yylex_destroy */
148 sdb_fe_yy_scan_bytes(str, len, scanner);
149 return scanner;
150 } /* sdb_fe_scanner_init */
152 void
153 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
154 {
155 sdb_fe_yylex_destroy(scanner);
156 } /* sdb_fe_scanner_destroy */
158 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */