Code

948b6badf155e848c5182ad1ca38f8004a49eb3f
[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 #include "frontend/connection.h"
31 #include "frontend/parser.h"
32 #include "frontend/grammar.h"
33 #include "utils/error.h"
35 #include <errno.h>
37 #include <string.h>
39 #define YY_EXTRA_TYPE sdb_fe_yyextra_t *
41 void
42 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
44 %}
46 %option never-interactive
47 %option reentrant
48 %option bison-bridge
49 %option bison-locations
50 %option 8bit
51 %option yylineno
52 %option nodefault
53 %option noinput
54 %option nounput
55 %option noyywrap
56 %option verbose
57 %option warn
58 %option prefix="sdb_fe_yy" outfile="lex.yy.c"
60 %x CSC
62 whitespace              ([ \t\n\r\f]+)
63 simple_comment  ("--"[^\n\r]*)
65 /*
66  * C style comments
67  */
68 csc_start       \/\*
69 csc_inside      ([^*/]+|[^*]\/|\*[^/])
70 csc_end         \*\/
72 identifier      ([A-Za-z_][A-Za-z_0-9$]*)
74 %%
76 {whitespace} |
77 {simple_comment}        { /* ignore */ }
79 {csc_start}                     { BEGIN(CSC); }
80 <CSC>{csc_inside}       { /* ignore */ }
81 <CSC>{csc_end}          { BEGIN(INITIAL); }
82 <CSC><<EOF>> {
83                 sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
84                 return SCANNER_ERROR;
85         }
87 {identifier} {
88                 /* XXX */
89                 if (! strcasecmp(yytext, "LIST"))
90                         return LIST;
92                 return IDENTIFIER;
93         }
95 .       { /* do nothing for now */ }
97 %%
99 sdb_fe_yyscan_t
100 sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
102         yyscan_t scanner;
104         if (! str)
105                 return NULL;
107         if (sdb_fe_yylex_init(&scanner)) {
108                 char errbuf[1024];
109                 sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
110                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
111                 return NULL;
112         }
114         sdb_fe_yyset_extra(yyext, scanner);
116         if (len < 0)
117                 len = strlen(str);
119         /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
120          * scanner and, thus, will be freed by yylex_destroy */
121         sdb_fe_yy_scan_bytes(str, len, scanner);
122         return scanner;
123 } /* sdb_fe_scanner_init */
125 void
126 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
128         sdb_fe_yylex_destroy(scanner);
129 } /* sdb_fe_scanner_destroy */
131 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */