2 * Copyright (c) 2015-2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 %option never-interactive
30 #include "sexpr_impl.h"
31 #include "sexpr.tab.h"
33 extern void sexpr_error2(char *, char *);
35 #define YY_INPUT(buf, result, max_size) \
36 result = sexpr_reader_input_proc(buf, max_size, yyscanner)
37 int sexpr_reader_input_proc(char *buf, int size, yyscan_t scanner);
39 static void inc_lineno(yyscan_t scanner);
45 char string_buf[65536];
46 char *string_ptr = NULL;
49 string_ptr = string_buf;
54 yylval->s = STR_DUP(string_buf);
62 *string_ptr++ = *tmp++;
64 /* TODO: handle octal escapes */
65 /* TODO: handle hex escapes */
66 <string>\\n { *string_ptr++ = '\n'; }
67 <string>\\t { *string_ptr++ = '\t'; }
68 <string>\\r { *string_ptr++ = '\r'; }
69 <string>\\b { *string_ptr++ = '\b'; }
70 <string>\\f { *string_ptr++ = '\f'; }
71 <string>\\\\ { *string_ptr++ = '\\'; }
72 <string>\\\" { *string_ptr++ = '"'; }
74 [().'] { return *yytext; }
75 #[tf] { yylval->b = (yytext[1] == 't'); return BOOL; }
76 0[0-7]* { ASSERT0(__str2u64(yytext, &yylval->i, 8)); return NUMBER; }
77 0x[0-9a-fA-F]* { ASSERT0(__str2u64(yytext, &yylval->i, 16)); return NUMBER; }
78 #x[0-9a-fA-F]+ { ASSERT0(__str2u64(yytext + 2, &yylval->i, 16)); return NUMBER; }
79 [1-9][0-9]* { ASSERT0(str2u64(yytext, &yylval->i)); return NUMBER; }
80 [a-zA-Z+=|&_-]?[a-zA-Z0-9+=|&_-]* { yylval->s = STR_DUP(yytext); return SYMBOL; }
81 [ \r\t] { /* ignore */ }
82 \n { inc_lineno(yyscanner); /* ignore */}
83 ;[^\n]* { /* comment: ignore it */ }
84 . { sexpr_error2("sexp text contains invalid characters", yytext); yyterminate(); }
87 static void inc_lineno(yyscan_t scanner)
89 struct sexpr_parser_state *out;
91 out = (struct sexpr_parser_state *) sexpr_reader_get_extra(scanner);
96 int sexpr_reader_input_proc(char *buf, int size, yyscan_t scanner)
98 struct sexpr_parser_state *out;
101 out = (struct sexpr_parser_state *) sexpr_reader_get_extra(scanner);
102 num = out->len - out->pos;
110 memcpy(buf, out->input + out->pos, num);
116 int sexpr_reader_wrap(yyscan_t scanner)