summaryrefslogtreecommitdiff
path: root/lexer.l
blob: 18937e888355774c04e7d7883d8a017b19ad08a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
%{
/*
 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <ctype.h>

#include "parser.h"

int yylex(struct jp_state *s);

#define YY_DECL int yylex(struct jp_state *s)

static void
str_put(struct jp_state *s, char c)
{
	if ((s->str_ptr - s->str_buf + 1) < sizeof(s->str_buf))
		*s->str_ptr++ = c;
}

static void
str_decode(struct jp_state *s, const char *input, int base)
{
	int code;
	char *end;

	code = strtoul(input, &end, base);

	if (end == input || *end)
		return;

	if (code > 0 && code <= 0x7F)
	{
		str_put(s, code);
	}
	else if (code > 0 && code <= 0x7FF)
	{
		str_put(s, ((code >>  6) & 0x1F) | 0xC0);
		str_put(s, ( code        & 0x3F) | 0x80);
	}
	else if (code > 0 && code <= 0xFFFF)
	{
		str_put(s, ((code >> 12) & 0x0F) | 0xE0);
		str_put(s, ((code >>  6) & 0x3F) | 0x80);
		str_put(s, ( code        & 0x3F) | 0x80);
	}
	else if (code > 0 && code <= 0x10FFFF)
	{
		str_put(s, ((code >> 18) & 0x07) | 0xF0);
		str_put(s, ((code >> 12) & 0x3F) | 0x80);
		str_put(s, ((code >>  6) & 0x3F) | 0x80);
		str_put(s, ( code        & 0x3F) | 0x80);
	}
}

%}

%option outfile="lexer.c" header-file="lexer.h"
%option noyywrap nounput noinput

DOT			"."
LABEL		[a-zA-Z_][a-zA-Z0-9_]*

BROPEN		"["
BRCLOSE		"]"
POPEN		"("
PCLOSE		")"

ROOT		"$"
THIS		"@"

LT			"<"
LE			"<="
GT			">"
GE			">="
NE			"!="
EQ			"="
NOT			"!"
AND			"&&"
OR			"||"

NUMBER		-?[0-9]+
WILDCARD	"*"
BOOL		(true|false)

WS			[ \t\n]*

%x			STRING

%%

["'] {
	s->str_ptr = s->str_buf;
	s->str_quote = *yytext;
	memset(s->str_buf, 0, sizeof(s->str_buf));
	BEGIN(STRING);
}

<STRING>{
	["'] {
		if (*yytext == s->str_quote)
		{
			BEGIN(INITIAL);
			yylval.op = jp_alloc_op(T_STRING, 0, s->str_buf);
			return T_STRING;
		}

		str_put(s, *yytext);
	}

	\\([0-3][0-7]{1,2}|[0-7]{0,2})	{ str_decode(s, yytext + 1, 8); }
	\\x[A-Fa-f0-9]{2}				{ str_decode(s, yytext + 2, 16); }
	\\u[A-Fa-f0-9]{4}				{ str_decode(s, yytext + 2, 16); }
	\\a								{ str_put(s, '\a'); }
	\\b								{ str_put(s, '\b'); }
	\\e								{ str_put(s, '\e'); }
	\\f								{ str_put(s, '\f'); }
	\\n								{ str_put(s, '\n'); }
	\\r								{ str_put(s, '\r'); }
	\\t								{ str_put(s, '\t'); }
	\\v								{ str_put(s, '\v'); }
	\\.								{ str_put(s, *yytext); }
	[^\\"']+						{ while (*yytext) str_put(s, *yytext++); }
}

{BOOL} {
	yylval.op = jp_alloc_op(T_BOOL, (*yytext == 't'), NULL);
	return T_BOOL;
}

{NUMBER} {
	yylval.op = jp_alloc_op(T_NUMBER, atoi(yytext), NULL);
	return T_NUMBER;
}

{LABEL} {
	yylval.op = jp_alloc_op(T_LABEL, 0, yytext);
	return T_LABEL;
}

{WILDCARD} {
	yylval.op = jp_alloc_op(T_WILDCARD, 0, NULL);
	return T_WILDCARD;
}

{DOT}		{ return T_DOT; }
{BROPEN}	{ return T_BROPEN; }
{BRCLOSE}	{ return T_BRCLOSE; }
{POPEN}		{ return T_POPEN; }
{PCLOSE}	{ return T_PCLOSE; }

{ROOT}		{ return T_ROOT; }
{THIS}		{ return T_THIS; }

{LT}		{ return T_LT; }
{LE}		{ return T_LE; }
{GT}		{ return T_GT; }
{GE}		{ return T_GE; }
{EQ}		{ return T_EQ; }
{NE}		{ return T_NE; }
{NOT}		{ return T_NOT; }
{AND}		{ return T_AND; }
{OR}		{ return T_OR; }

{WS}		{ }

%%