3 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 int yylex(struct jp_state *s);
24 #define YY_DECL int yylex(struct jp_state *s)
27 str_put(struct jp_state *s, char c)
29 if ((s->str_ptr - s->str_buf + 1) < sizeof(s->str_buf))
34 str_decode(struct jp_state *s, const char *input, int base)
39 code = strtoul(input, &end, base);
41 if (end == input || *end)
44 if (code > 0 && code <= 0x7F)
48 else if (code > 0 && code <= 0x7FF)
50 str_put(s, ((code >> 6) & 0x1F) | 0xC0);
51 str_put(s, ( code & 0x3F) | 0x80);
53 else if (code > 0 && code <= 0xFFFF)
55 str_put(s, ((code >> 12) & 0x0F) | 0xE0);
56 str_put(s, ((code >> 6) & 0x3F) | 0x80);
57 str_put(s, ( code & 0x3F) | 0x80);
59 else if (code > 0 && code <= 0x10FFFF)
61 str_put(s, ((code >> 18) & 0x07) | 0xF0);
62 str_put(s, ((code >> 12) & 0x3F) | 0x80);
63 str_put(s, ((code >> 6) & 0x3F) | 0x80);
64 str_put(s, ( code & 0x3F) | 0x80);
70 %option outfile="lexer.c" header-file="lexer.h"
71 %option noyywrap nounput noinput
74 LABEL [a-zA-Z_][a-zA-Z0-9_]*
105 s->str_ptr = s->str_buf;
106 s->str_quote = *yytext;
107 memset(s->str_buf, 0, sizeof(s->str_buf));
113 if (*yytext == s->str_quote)
116 yylval.op = jp_alloc_op(T_STRING, 0, s->str_buf);
123 \\([0-3][0-7]{1,2}|[0-7]{0,2}) { str_decode(s, yytext + 1, 8); }
124 \\x[A-Fa-f0-9]{2} { str_decode(s, yytext + 2, 16); }
125 \\u[A-Fa-f0-9]{4} { str_decode(s, yytext + 2, 16); }
126 \\a { str_put(s, '\a'); }
127 \\b { str_put(s, '\b'); }
128 \\e { str_put(s, '\e'); }
129 \\f { str_put(s, '\f'); }
130 \\n { str_put(s, '\n'); }
131 \\r { str_put(s, '\r'); }
132 \\t { str_put(s, '\t'); }
133 \\v { str_put(s, '\v'); }
134 \\. { str_put(s, *yytext); }
135 [^\\"']+ { while (*yytext) str_put(s, *yytext++); }
139 yylval.op = jp_alloc_op(T_BOOL, (*yytext == 't'), NULL);
144 yylval.op = jp_alloc_op(T_NUMBER, atoi(yytext), NULL);
149 yylval.op = jp_alloc_op(T_LABEL, 0, yytext);
154 yylval.op = jp_alloc_op(T_WILDCARD, 0, NULL);
158 {DOT} { return T_DOT; }
159 {BROPEN} { return T_BROPEN; }
160 {BRCLOSE} { return T_BRCLOSE; }
161 {POPEN} { return T_POPEN; }
162 {PCLOSE} { return T_PCLOSE; }
164 {ROOT} { return T_ROOT; }
165 {THIS} { return T_THIS; }
167 {LT} { return T_LT; }
168 {LE} { return T_LE; }
169 {GT} { return T_GT; }
170 {GE} { return T_GE; }
171 {EQ} { return T_EQ; }
172 {NE} { return T_NE; }
173 {NOT} { return T_NOT; }
174 {AND} { return T_AND; }
175 {OR} { return T_OR; }