summaryrefslogtreecommitdiff
path: root/parser.y
blob: 75c33407586d39864d597a75d368a0bd8993cebc (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
%{
/*
 * 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 <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include <libubox/utils.h>

#include "parser.h"

static struct jp_opcode *append_op(struct jp_opcode *a, struct jp_opcode *b);

int yylex(struct jp_state *s);
void *yy_scan_string (const char *str);
int yylex_destroy(void);

int yyparse(struct jp_state *s);
void yyerror(struct jp_state *s, const char *msg);

%}

%output  "parser.c"
%defines "parser.h"

%parse-param { struct jp_state *s }
%lex-param { struct jp_state *s }

%code requires {

#ifndef __PARSER_H_
#define __PARSER_H_

struct jp_opcode {
	int type;
	struct jp_opcode *next;
	struct jp_opcode *down;
	struct jp_opcode *sibling;
	char *str;
	int num;
};

struct jp_state {
	struct jp_opcode *pool;
	struct jp_opcode *path;
	const char *error;
	char str_quote;
	char str_buf[128];
	char *str_ptr;
};

struct jp_opcode *_jp_alloc_op(struct jp_state *s, int type, int num, char *str, ...);
#define jp_alloc_op(type, num, str, ...) _jp_alloc_op(s, type, num, str, ##__VA_ARGS__, NULL)

struct jp_state *jp_parse(const char *expr);
void jp_free(struct jp_state *s);

#endif

}

%union {
	struct jp_opcode *op;
}


%token T_ROOT T_THIS T_DOT T_BROPEN T_BRCLOSE
%token T_OR T_AND T_LT T_LE T_GT T_GE T_EQ T_NE T_POPEN T_PCLOSE T_NOT

%token <op> T_BOOL T_NUMBER T_STRING T_LABEL T_WILDCARD

%type <op> expr path segments segment or_exps or_exp and_exps and_exp cmp_exp unary_exp

%error-verbose

%%

input
	: expr 							{ s->path = $1; }
	;

expr
	: T_LABEL T_EQ path				{ $1->down = $3; $$ = $1; }
	| path							{ $$ = $1; }
	;

path
	: T_ROOT segments				{ $$ = jp_alloc_op(T_ROOT, 0, NULL, $2); }
	| T_THIS segments				{ $$ = jp_alloc_op(T_THIS, 0, NULL, $2); }
	;

segments
	: segments segment				{ $$ = append_op($1, $2); }
	| segment						{ $$ = $1; }
	;

segment
	: T_DOT T_LABEL					{ $$ = $2; }
	| T_DOT T_WILDCARD				{ $$ = $2; }
	| T_BROPEN or_exps T_BRCLOSE	{ $$ = $2; }
	;

or_exps
	: or_exp						{ $$ = $1->sibling ? jp_alloc_op(T_OR, 0, NULL, $1) : $1; }
	;

or_exp
	: or_exp T_OR and_exps			{ $$ = append_op($1, $3); }
	| and_exps						{ $$ = $1; }
	;

and_exps
	: and_exp						{ $$ = $1->sibling ? jp_alloc_op(T_AND, 0, NULL, $1) : $1; }
	;

and_exp
	: and_exp T_AND cmp_exp			{ $$ = append_op($1, $3); }
	| cmp_exp						{ $$ = $1; }
	;

cmp_exp
	: unary_exp T_LT unary_exp		{ $$ = jp_alloc_op(T_LT, 0, NULL, $1, $3); }
	| unary_exp T_LE unary_exp		{ $$ = jp_alloc_op(T_LE, 0, NULL, $1, $3); }
	| unary_exp T_GT unary_exp		{ $$ = jp_alloc_op(T_GT, 0, NULL, $1, $3); }
	| unary_exp T_GE unary_exp		{ $$ = jp_alloc_op(T_GE, 0, NULL, $1, $3); }
	| unary_exp T_EQ unary_exp		{ $$ = jp_alloc_op(T_EQ, 0, NULL, $1, $3); }
	| unary_exp T_NE unary_exp		{ $$ = jp_alloc_op(T_NE, 0, NULL, $1, $3); }
	| unary_exp						{ $$ = $1; }
	;

unary_exp
	: T_BOOL						{ $$ = $1; }
	| T_NUMBER						{ $$ = $1; }
	| T_STRING						{ $$ = $1; }
	| T_WILDCARD					{ $$ = $1; }
	| T_POPEN or_exps T_PCLOSE		{ $$ = $2; }
	| T_NOT unary_exp				{ $$ = jp_alloc_op(T_NOT, 0, NULL, $2); }
	| path							{ $$ = $1; }
	;

%%

void
yyerror(struct jp_state *s, const char *msg)
{
	s->error = msg;
}

static struct jp_opcode *
append_op(struct jp_opcode *a, struct jp_opcode *b)
{
	struct jp_opcode *tail = a;

	while (tail->sibling)
		tail = tail->sibling;

	tail->sibling = b;

	return a;
}

struct jp_opcode *
_jp_alloc_op(struct jp_state *s, int type, int num, char *str, ...)
{
	va_list ap;
	char *ptr;
	struct jp_opcode *newop, *child;

	newop = calloc_a(sizeof(*newop),
	                 str ? &ptr : NULL, str ? strlen(str) + 1 : 0);

	if (!newop)
	{
		fprintf(stderr, "Out of memory\n");
		exit(127);
	}

	newop->type = type;
	newop->num = num;

	if (str)
		newop->str = strcpy(ptr, str);

	va_start(ap, str);

	while ((child = va_arg(ap, void *)) != NULL)
		if (!newop->down)
			newop->down = child;
		else
			append_op(newop->down, child);

	va_end(ap);

	newop->next = s->pool;
	s->pool = newop;

	return newop;
}

struct jp_state *
jp_parse(const char *expr)
{
	struct jp_state *s;

	s = calloc(1, sizeof(*s));

	if (!s)
		return NULL;

	yy_scan_string(expr);

	if (yyparse(s))
		s->path = NULL;

	yylex_destroy();

	return s;
}

void
jp_free(struct jp_state *s)
{
	struct jp_opcode *op, *tmp;

	for (op = s->pool; op;)
	{
		tmp = op->next;
		free(op);
		op = tmp;
	}

	free(s);
}