Code

frontend: Added very simple single-quoted string support.
[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 */
95                 if (! strcasecmp(yytext, "LIST"))
96                         return LIST;
97                 else if (! strcasecmp(yytext, "FETCH"))
98                         return FETCH;
100                 yylval->str = strdup(yytext);
101                 return IDENTIFIER;
102         }
103 {string} {
104                 yytext[yyleng - 1] = '\0';
105                 yylval->str = strdup(yytext + 1);
106                 return STRING;
107         }
109 .       { /* do nothing for now */ }
111 %%
113 sdb_fe_yyscan_t
114 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
116         yyscan_t scanner;
118         if (! str)
119                 return NULL;
121         if (sdb_fe_yylex_init(&scanner)) {
122                 char errbuf[1024];
123                 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
124                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
125                 return NULL;
126         }
128         sdb_fe_yyset_extra(yyext, scanner);
130         if (len < 0)
131                 len = strlen(str);
133         /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
134          * scanner and, thus, will be freed by yylex_destroy */
135         sdb_fe_yy_scan_bytes(str, len, scanner);
136         return scanner;
137 } /* sdb_fe_scanner_init */
139 void
140 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
142         sdb_fe_yylex_destroy(scanner);
143 } /* sdb_fe_scanner_destroy */
145 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */