Code

frontend: Added simple 'LOOKUP <type> WHERE <expression>' query.
[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 "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, "FETCH"))
96                         return FETCH;
97                 else if (! strcasecmp(yytext, "LIST"))
98                         return LIST;
99                 else if (! strcasecmp(yytext, "LOOKUP"))
100                         return LOOKUP;
101                 else if (! strcasecmp(yytext, "WHERE"))
102                         return WHERE;
104                 yylval->str = strdup(yytext);
105                 return IDENTIFIER;
106         }
107 {string} {
108                 yytext[yyleng - 1] = '\0';
109                 yylval->str = strdup(yytext + 1);
110                 return STRING;
111         }
113 .       { /* do nothing for now */ }
115 %%
117 sdb_fe_yyscan_t
118 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
120         yyscan_t scanner;
122         if (! str)
123                 return NULL;
125         if (sdb_fe_yylex_init(&scanner)) {
126                 char errbuf[1024];
127                 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
128                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
129                 return NULL;
130         }
132         sdb_fe_yyset_extra(yyext, scanner);
134         if (len < 0)
135                 len = strlen(str);
137         /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
138          * scanner and, thus, will be freed by yylex_destroy */
139         sdb_fe_yy_scan_bytes(str, len, scanner);
140         return scanner;
141 } /* sdb_fe_scanner_init */
143 void
144 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
146         sdb_fe_yylex_destroy(scanner);
147 } /* sdb_fe_scanner_destroy */
149 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */