summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fm/eversholt/common/escparse.y
blob: fe4b01a232c578e17441a523d67395a3fb7f3ca8 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
%{
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * escparse.y -- parser for esc
 *
 * this is the yacc-based parser for Eversholt.  the syntax is simple
 * and is defined by the LALR(1) grammar described by this file.  there
 * should be no shift/reduce or reduce/reduce messages when building this
 * file.
 *
 * as the input is parsed, a parse tree is built by calling the
 * tree_X() functions defined in tree.c.  any syntax errors cause
 * us to skip to the next semicolon, achieved via the "error" clause
 * in the stmt rule below.  the yacc state machine code will call
 * yyerror() in esclex.c and that will keep count of the errors and
 * display the filename, line number, and current input stream of tokens
 * to help the user figure out the problem.  the -Y flag to this program
 * turns on the yacc debugging output which is quite large.  you probably
 * only need to do that if you're debugging the grammar below.
 *
 */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "out.h"
#include "stable.h"
#include "literals.h"
#include "lut.h"
#include "esclex.h"
#include "tree.h"

%}
%union {
	struct tokstr tok;
	struct node *np;
}

%right '='

/*
 * make sure ':' comes immediately after '?' in precedence declarations
 */
%right '?'
%nonassoc ':'

%left OR
%left AND
%left '|'
%left '^'
%left '&'
%left EQ NE
%left LE GE '<' '>'
%left LSHIFT RSHIFT
%left '-' '+'
%left '*' '%' DIV '/'
%right '!' '~'
%left '.'

%token <tok> PROP MASK ARROW EVENT ENGINE ASRU FRU COUNT CONFIG
%token <tok> ID QUOTE NUMBER IF PATHFUNC
%type <tok> enameid
%type <np> root stmtlist stmt nvpairlist nvpair nvname nvexpr
%type <np> exprlist expr iterid ename pname epname eexprlist ipname iname
%type <np> numexpr cexpr func pfunc parglist parg
%type <np> eventlist event nork norkexpr globid propbody

%%

root	: stmtlist
		{ (void)tree_root($1); }
	;

stmtlist   : /*empty*/
		{ $$ = NULL; }
        | stmtlist stmt
		{ $$ = tree_expr(T_LIST, $1, $2); }
	;

stmt	: error ';'
     		{ $$ = tree_nothing(); }
	| IF '(' expr ')' stmt
		{ $$ = $5; }
	| IF '(' expr ')' '{' stmtlist '}'
		{ $$ = $6; }
	| EVENT event nvpairlist ';'
		{ $$ = tree_decl(T_EVENT, $2, $3, $1.file, $1.line); }
	| ENGINE event nvpairlist ';'
		{ $$ = tree_decl(T_ENGINE, $2, $3, $1.file, $1.line); }
	| PROP propbody ';'
		{
			$$ = tree_stmt(T_PROP, $2, $1.file, $1.line);
		}
	| MASK propbody ';'
		{
			$$ = tree_stmt(T_MASK, $2, $1.file, $1.line);
		}
	| ASRU pname nvpairlist ';'
		{
			$$ = tree_decl(T_ASRU, $2, $3, $1.file, $1.line);
		}
	| FRU pname nvpairlist ';'
		{
			$$ = tree_decl(T_FRU, $2, $3, $1.file, $1.line);
		}
	| CONFIG ipname nvpairlist ';'
		{
			$$ = tree_decl(T_CONFIG, $2, $3, $1.file, $1.line);
		}
	| /*superfluous semicolons are ignored*/ ';'
     		{ $$ = tree_nothing(); }
	;

propbody: eventlist nork ARROW nork eventlist
		{
			$$ = tree_arrow($1, $2, $4, $5);
		}
	| propbody nork ARROW nork eventlist
		{
			$$ = tree_arrow($1, $2, $4, $5);
		}
	;

nork	: /* empty */
		{ $$ = NULL; }
	| '(' norkexpr ')'
		{ $$ = $2; }
	;

norkexpr: NUMBER
		{ $$ = tree_num($1.s, $1.file, $1.line); }
	| ID
		/* really can only be 'A', enforced by check_arrow() later */
       		{ $$ = tree_name($1.s, IT_NONE, $1.file, $1.line); }
	| '(' norkexpr ')'
		{ $$ = $2; }
	| norkexpr '-' norkexpr
		{ $$ = tree_expr(T_SUB, $1, $3); }
	| norkexpr '+' norkexpr
		{ $$ = tree_expr(T_ADD, $1, $3); }
	| norkexpr '*' norkexpr
		{ $$ = tree_expr(T_MUL, $1, $3); }
	| norkexpr DIV norkexpr
		{ $$ = tree_expr(T_DIV, $1, $3); }
	| norkexpr '%' norkexpr
		{ $$ = tree_expr(T_MOD, $1, $3); }
	;

nvpairlist: /* empty */
		{ $$ = NULL; }
	| nvpair
	| nvpairlist ',' nvpair
		{ $$ = tree_expr(T_LIST, $1, $3); }
	;
	  
nvpair	: nvname '=' nvexpr
		{ $$ = tree_expr(T_NVPAIR, $1, $3); }
	| ENGINE '=' nvexpr
		/* "engine" is a reserved word, but a valid property name */
		{
			$$ = tree_expr(T_NVPAIR,
				tree_name($1.s, IT_NONE, $1.file, $1.line), $3);
		}
	| COUNT '=' nvexpr
		/* "count" is a reserved word, but a valid property name */
		{
			$$ = tree_expr(T_NVPAIR,
				tree_name($1.s, IT_NONE, $1.file, $1.line), $3);
		}
	;

nvname	: ID
		{ $$ = tree_name($1.s, IT_NONE, $1.file, $1.line); }
	| nvname '-' ID
		{
			/* hack to allow dashes in property names */
			$$ = tree_name_repairdash($1, $3.s);
		}
	;

/* the RHS of an nvpair can be a value, or an ename, or an ename@pname */
nvexpr	: numexpr
	| ename epname
		{ $$ = tree_event($1, $2, NULL); }
	| pname
	| globid
	| func
	| NUMBER ID
		/*
		 * ID must be timevals only ("ms", "us", etc.).
		 * enforced by tree_timeval().
		 */
		{ $$ = tree_timeval($1.s, $2.s, $1.file, $1.line); }
	| QUOTE
		{ $$ = tree_quote($1.s, $1.file, $1.line); }
	;

/* arithmetic operations, no variables or symbols */
numexpr	: numexpr '-' numexpr
		{ $$ = tree_expr(T_SUB, $1, $3); }
	| numexpr '+' numexpr
		{ $$ = tree_expr(T_ADD, $1, $3); }
	| numexpr '*' numexpr
		{ $$ = tree_expr(T_MUL, $1, $3); }
	| numexpr DIV numexpr
		{ $$ = tree_expr(T_DIV, $1, $3); }
	| numexpr '/' numexpr
		{ $$ = tree_expr(T_DIV, $1, $3); }
	| numexpr '%' numexpr
		{ $$ = tree_expr(T_MOD, $1, $3); }
 	| '(' numexpr ')'
		{ $$ = $2; }
	| NUMBER
		{ $$ = tree_num($1.s, $1.file, $1.line); }
	;

eventlist: event
	| eventlist ',' event
		{ $$ = tree_expr(T_LIST, $1, $3); }
	;

event	: ename epname eexprlist
		{ $$ = tree_event($1, $2, $3); }
	;

epname	: /* empty */
		{ $$ = NULL; }
	| '@' pname
		{ $$ = $2; }
	;

eexprlist: /* empty */
		{ $$ = NULL; }
	| '{' exprlist '}'
		{ $$ = $2; }
	;

exprlist: expr
	| exprlist ',' expr
		{ $$ = tree_expr(T_LIST, $1, $3); }
	;

/*
 * note that expr does not include pname, to avoid reduce/reduce
 * conflicts between cexpr and iterid involving the use of ID
 */
expr	: cexpr
	| NUMBER ID
		/*
		 * ID must be timevals only ("ms", "us", etc.).
		 * enforced by tree_timeval().
		 */
		{ $$ = tree_timeval($1.s, $2.s, $1.file, $1.line); }
	;

cexpr	: cexpr '=' cexpr
		{ $$ = tree_expr(T_ASSIGN, $1, $3); }
	| cexpr '?' cexpr
		{ $$ = tree_expr(T_CONDIF, $1, $3); }
	| cexpr ':' cexpr
		{ $$ = tree_expr(T_CONDELSE, $1, $3); }
	| cexpr OR cexpr
		{ $$ = tree_expr(T_OR, $1, $3); }
 	| cexpr AND cexpr
		{ $$ = tree_expr(T_AND, $1, $3); }
	| cexpr '|' cexpr
		{ $$ = tree_expr(T_BITOR, $1, $3); }
	| cexpr '^' cexpr
		{ $$ = tree_expr(T_BITXOR, $1, $3); }
	| cexpr '&' cexpr
		{ $$ = tree_expr(T_BITAND, $1, $3); }
	| cexpr EQ cexpr
		{ $$ = tree_expr(T_EQ, $1, $3); }
	| cexpr NE cexpr
		{ $$ = tree_expr(T_NE, $1, $3); }
	| cexpr '<' cexpr
		{ $$ = tree_expr(T_LT, $1, $3); }
	| cexpr LE cexpr
		{ $$ = tree_expr(T_LE, $1, $3); }
	| cexpr '>' cexpr
		{ $$ = tree_expr(T_GT, $1, $3); }
	| cexpr GE cexpr
		{ $$ = tree_expr(T_GE, $1, $3); }
	| cexpr LSHIFT cexpr
		{ $$ = tree_expr(T_LSHIFT, $1, $3); }
	| cexpr RSHIFT cexpr
		{ $$ = tree_expr(T_RSHIFT, $1, $3); }
	| cexpr '-' cexpr
		{ $$ = tree_expr(T_SUB, $1, $3); }
	| cexpr '+' cexpr
		{ $$ = tree_expr(T_ADD, $1, $3); }
	| cexpr '*' cexpr
		{ $$ = tree_expr(T_MUL, $1, $3); }
	| cexpr DIV cexpr
		{ $$ = tree_expr(T_DIV, $1, $3); }
	| cexpr '/' cexpr
		{ $$ = tree_expr(T_DIV, $1, $3); }
	| cexpr '%' cexpr
		{ $$ = tree_expr(T_MOD, $1, $3); }
	|  '!' cexpr
		{ $$ = tree_expr(T_NOT, $2, NULL); }
	|  '~' cexpr
		{ $$ = tree_expr(T_BITNOT, $2, NULL); }
	| '(' cexpr ')'
		{ $$ = $2; }
	| func
	| NUMBER
		{ $$ = tree_num($1.s, $1.file, $1.line); }
	| ID
       		{
			/* iteration variable */
			$$ = tree_name($1.s, IT_NONE, $1.file, $1.line);
		}
	| globid
	| QUOTE
		{ $$ = tree_quote($1.s, $1.file, $1.line); }
	;

func	: ID '(' ')'
		{ $$ = tree_func($1.s, NULL, $1.file, $1.line); }
	| ID '(' exprlist ')'
		{ $$ = tree_func($1.s, $3, $1.file, $1.line); }
	| PATHFUNC '(' parglist ')'
		{ $$ = tree_func($1.s, $3, $1.file, $1.line); }
	| pfunc
	;

parglist: parg
	| parglist ',' parg
		{ $$ = tree_expr(T_LIST, $1, $3); }
	;

parg	: pfunc
	| pname
		{ $$ = tree_pname($1); }
	| QUOTE
		{ $$ = tree_quote($1.s, $1.file, $1.line); }
	| ID '(' exprlist ')'
		{ $$ = tree_func($1.s, $3, $1.file, $1.line); }
	;

/*
 * these functions are in the grammar so we can force the arg to be
 * a path or an event.  they show up as functions in the parse tree.
 */
pfunc	: ASRU '(' pname ')'
		{ $$ = tree_func($1.s, tree_pname($3), $1.file, $1.line); }
	| FRU '(' pname ')'
		{ $$ = tree_func($1.s, tree_pname($3), $1.file, $1.line); }
	| COUNT '(' event ')'
		{ $$ = tree_func($1.s, $3, $1.file, $1.line); }
	;

globid	: '$' ID
       		{ $$ = tree_globid($2.s, $2.file, $2.line); }
	;

iterid	: ID
       		{ $$ = tree_name($1.s, IT_VERTICAL, $1.file, $1.line); }
	| ID '[' ']'
       		{ $$ = tree_name($1.s, IT_VERTICAL, $1.file, $1.line); }
	| ID '[' cexpr ']'
       		{
			$$ = tree_name_iterator(
			   tree_name($1.s, IT_VERTICAL, $1.file, $1.line), $3);
		}
	| ID '<' '>'
       		{ $$ = tree_name($1.s, IT_HORIZONTAL, $1.file, $1.line); }
	| ID '<' ID '>'
       		{
			$$ = tree_name_iterator(
			    tree_name($1.s, IT_HORIZONTAL, $1.file, $1.line),
			    tree_name($3.s, IT_NONE, $3.file, $3.line));
		}
	| ID '-' iterid
		{
			/* hack to allow dashes in path name components */
			$$ = tree_name_repairdash2($1.s, $3);
		}
	;

/* iname is an ID where we can peel numbers off the end */
iname	: ID
       		{ $$ = tree_iname($1.s, $1.file, $1.line); }
	;

/* base case of ID.ID instead of just ID requires ename to contain one dot */
ename	: ID '.' enameid
       		{
			$$ = tree_name_append(
			    tree_name($1.s, IT_ENAME, $1.file, $1.line),
			    tree_name($3.s, IT_NONE, $3.file, $3.line));
		}
	| ename '.' enameid
		{
			$$ = tree_name_append($1,
			    tree_name($3.s, IT_NONE, $3.file, $3.line));
		}
	| ename '-' enameid
		{
			/*
			 * hack to allow dashes in class names.  when we
			 * detect the dash here, we know we're in a class
			 * name because the left recursion of this rule
			 * means we've already matched at least:
			 * 	ID '.' ID
			 * so the ename here has an incomplete final
			 * component (because the lexer stopped at the
			 * dash).  so we repair that final component here.
			 */
			$$ = tree_name_repairdash($1, $3.s);
		}
	;

/* like an ID, but we let reserved words act unreserved in enames */
enameid	: ID
	| PROP
	| MASK
	| EVENT
	| ENGINE
	| ASRU
	| FRU
	| CONFIG
	| IF
	;

/* pname is a pathname, like x/y, x<i>/y[0], etc */
pname	: iterid
	| pname '/' iterid
		{ $$ = tree_name_append($1, $3); }
	;

/* ipname is an "instanced" pathname, like x0/y1 */
ipname	: iname
	| ipname '/' iname
		{ $$ = tree_name_append($1, $3); }
	;

%%