-
Notifications
You must be signed in to change notification settings - Fork 10
/
prscfl.y
209 lines (174 loc) · 4.8 KB
/
prscfl.y
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
%{
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <prscfl.h>
#include <prscfl_gram.h>
static int prscfl_yyerror(prscfl_yyscan_t yyscanner, const char *msg);
extern int prscfl_yylex (YYSTYPE * yylval_param, prscfl_yyscan_t yyscanner);
static ParamDef *output;
#define MakeScalarParam(r, t, n, v) do { \
(r) = malloc(sizeof(ParamDef)); \
(r)->paramType = t##Type; \
(r)->paramValue.t##val = (v); \
(r)->name = (n); \
(r)->comment = NULL; \
(r)->flags = 0; \
(r)->parent = NULL; \
(r)->next = NULL; \
} while(0)
#define MakeList(r, f, l) \
if (f) { \
(f)->next = (l); \
(r) = (f); \
} else { \
(r) = (l); \
}
#define SetParent(p, l) do { \
ParamDef *i = (l); \
while(i) { \
i->parent = (p); \
i = i->next; \
} \
} while(0)
#define PropagateStructFlags(s, f) do { \
if ((s)->paramType == structType && ((f) & PARAMDEF_RDONLY)) { \
ParamDef *child_def = (s)->paramValue.structval; \
\
while(child_def) { \
child_def->flags |= PARAMDEF_RDONLY; \
\
child_def = child_def->next; \
} \
} \
} while(0)
%}
%pure-parser
%expect 0
%name-prefix "prscfl_yy"
%error-verbose
%parse-param {prscfl_yyscan_t yyscanner}
%lex-param {prscfl_yyscan_t yyscanner}
%union {
int32_t int32val;
u_int32_t uint32val;
int64_t int64val;
u_int64_t uint64val;
double doubleval;
char *str;
ParamDef *node;
}
%type <str> identifier
%type <node> param param_list
%type <node> commented_param
%type <node> comment comment_opt
%type <node> cfg
%type <int32val> flags_opt flag flag_list
%token <str> KEY_P NULL_P STRING_P COMMENT_P RDONLY_P RDWR_P
BUILTIN_P REQUIRED_P FALSE_P TRUE_P
%token <int32val> INT32_P
%token <uint32val> UINT32_P
%token <int64val> INT64_P
%token <uint64val> UINT64_P
%token <doubleval> DOUBLE_P
%%
cfg:
BUILTIN_P param_list {
ParamDef *b;
MakeScalarParam(b, builtin, NULL, $1);
MakeList($$, b, $2);
output = $$;
}
| param_list { output = $$ = $1; }
;
identifier:
KEY_P { $$ = $1; }
| NULL_P { $$ = $1; }
| TRUE_P { $$ = $1; }
| FALSE_P { $$ = $1; }
| RDONLY_P { $$ = $1; }
| RDWR_P { $$ = $1; }
| REQUIRED_P { $$ = $1; }
;
param_list:
commented_param { $$ = $1; }
| commented_param param_list { MakeList($$, $1, $2); }
;
comment:
COMMENT_P { MakeScalarParam($$, comment, NULL, $1); }
| COMMENT_P comment {
ParamDef *comment;
MakeScalarParam(comment, comment, NULL, $1);
MakeList($$, comment, $2);
}
;
comment_opt:
comment { $$ = $1; }
| /* EMPTY */ { $$ = NULL; }
;
flag:
RDWR_P { $$ = 0; }
| RDONLY_P { $$ = PARAMDEF_RDONLY; }
| REQUIRED_P { $$ = PARAMDEF_REQUIRED; }
;
flag_list:
flag { $$ = $1; }
| flag_list ',' flag { $$ |= $3; }
;
flags_opt:
',' flag_list { $$ = $2; }
| /* EMPTY */ { $$ = 0; }
;
commented_param:
comment_opt param flags_opt {
$$ = $2; $$->comment = $1; $$->flags = $3;
PropagateStructFlags($2, $3);
}
;
param:
identifier '=' INT32_P { MakeScalarParam($$, int32, $1, $3); }
| identifier '=' UINT32_P { MakeScalarParam($$, uint32, $1, $3); }
| identifier '=' INT64_P { MakeScalarParam($$, int64, $1, $3); }
| identifier '=' UINT64_P { MakeScalarParam($$, uint64, $1, $3); }
| identifier '=' DOUBLE_P { MakeScalarParam($$, double, $1, $3); }
| identifier '=' STRING_P { MakeScalarParam($$, string, $1, $3); }
| identifier '=' NULL_P { MakeScalarParam($$, string, $1, NULL); }
| identifier '=' TRUE_P { MakeScalarParam($$, bool, $1, true); }
| identifier '=' FALSE_P { MakeScalarParam($$, bool, $1, false); }
| identifier '=' '{' param_list '}' { MakeScalarParam($$, struct, $1, $4); SetParent( $$, $4 ); }
| identifier '=' '[' param_list ']' {
ParamDef *s;
MakeScalarParam(s, struct, NULL, $4); SetParent( s, $4 );
MakeScalarParam($$, array, $1, s); SetParent( $$, s );
}
| identifier '=' '[' comment_opt '{' param_list '}' flags_opt ']' {
ParamDef *s;
MakeScalarParam(s, struct, NULL, $6); SetParent( s, $6 );
s->comment = $4;
s->flags = $8;
MakeScalarParam($$, array, $1, s); SetParent( $$, s );
PropagateStructFlags(s, $8);
}
;
%%
static int
prscfl_yyerror(prscfl_yyscan_t yyscanner, const char *msg) {
fprintf(stderr, "gram_yyerror: %s at line %d\n", msg, prscflGetLineNo(yyscanner));
return 0;
}
ParamDef*
parseCfgDef(FILE *fh) {
prscfl_yyscan_t yyscanner;
prscfl_yy_extra_type yyextra;
int yyresult;
yyscanner = prscflScannerInit(fh, &yyextra);
output = NULL;
yyresult = prscfl_yyparse(yyscanner);
prscflScannerFinish(yyscanner);
if (yyresult != 0)
return NULL;
return output;
}