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$]*)
78 %%
80 {whitespace} |
81 {simple_comment} { /* ignore */ }
83 {csc_start} { BEGIN(CSC); }
84 <CSC>{csc_inside} { /* ignore */ }
85 <CSC>{csc_end} { BEGIN(INITIAL); }
86 <CSC><<EOF>> {
87 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
88 return SCANNER_ERROR;
89 }
91 {identifier} {
92 /* XXX */
93 if (! strcasecmp(yytext, "LIST"))
94 return LIST;
96 yylval->str = strdup(yytext);
97 return IDENTIFIER;
98 }
100 . { /* do nothing for now */ }
102 %%
104 sdb_fe_yyscan_t
105 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
106 {
107 yyscan_t scanner;
109 if (! str)
110 return NULL;
112 if (sdb_fe_yylex_init(&scanner)) {
113 char errbuf[1024];
114 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
115 sdb_strerror(errno, errbuf, sizeof(errbuf)));
116 return NULL;
117 }
119 sdb_fe_yyset_extra(yyext, scanner);
121 if (len < 0)
122 len = strlen(str);
124 /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
125 * scanner and, thus, will be freed by yylex_destroy */
126 sdb_fe_yy_scan_bytes(str, len, scanner);
127 return scanner;
128 } /* sdb_fe_scanner_init */
130 void
131 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
132 {
133 sdb_fe_yylex_destroy(scanner);
134 } /* sdb_fe_scanner_destroy */
136 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */