-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.y
184 lines (164 loc) · 4.63 KB
/
parser.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
/*
* configk: an easy way to edit kernel configuration files and templates
* Copyright (C) 2023-2024 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See COPYING file or <http://www.gnu.org/licenses/> for more details.
*/
%locations
%define api.pure full
%define api.prefix {yy}
%define parse.error verbose
%initial-action { t = ch = NULL; chcount = 0; }
%union {
int num;
char *txt;
};
%token <txt> T_CONFIG T_CONFID
%token <txt> T_CHOICE T_ENDCHOICE
%token <num> T_TYPE T_DEFTYPE
%token <txt> T_DEFAULT
%token <txt> T_PROMPT
%token <txt> T_DEPENDS
%token <txt> T_SELECT T_IMPLY
%token <txt> T_RANGE
%token <txt> T_HELP T_HELPTEXT
%token <txt> T_EOL T_TAB
%token <txt> T_TEXT
%start clist
%type <txt> centry cname cattrs attr
%type <txt> choice endchoice
%{
#include <stdio.h>
#include "configk.h"
extern char *gstr;
extern char *types[];
extern int yylex(YYSTYPE *, YYLTYPE *);
void yyerror(YYLTYPE *, char const *);
cEntry *t, *ch;
uint16_t chcount;
%}
%%
clist:
%empty
| clist centry
;
centry:
cname { t = add_new_config($$, CENTRY); }
| choice {
char *chstr = calloc(strlen($$) + 5, sizeof(char));
sprintf(chstr, "CHOICE%03d", ++chcount);
t = ch = add_new_config(chstr, CHENTRY);
free($$);
}
| endchoice { tree_curr_root_up(); ch = NULL; free($$); }
| centry cattrs
| T_EOL
| error { yyerrok; }
;
cname:
T_CONFIG T_CONFID T_EOL { $$ = $2; }
;
choice:
T_CHOICE T_EOL { $$ = $1; }
;
endchoice:
T_ENDCHOICE T_EOL { $$ = $1; }
;
cattrs:
T_TAB attr T_EOL
| T_TYPE T_TEXT T_EOL { free($2); }
| T_DEFTYPE T_TEXT T_EOL { free($2); }
| T_DEFAULT T_TEXT T_EOL { free($2); }
| T_DEPENDS T_TEXT T_EOL { free($2); }
| T_PROMPT T_TEXT T_EOL { free($2); }
| T_SELECT T_TEXT T_EOL { free($2); }
| T_IMPLY T_TEXT T_EOL { free($2); }
| T_RANGE T_TEXT T_EOL { free($2); }
| T_HELP T_HELPTEXT T_EOL{ free($2); }
| T_HELP T_EOL { free($1); }
| T_TAB T_EOL
;
attr:
T_TYPE {
t->opt_type = t->opt_type ? t->opt_type : (cType)$1;
if (!t->opt_value
&& (CBOOL == t->opt_type || CTRISTATE == t->opt_type))
t->opt_value = strdup("n");
else if (!t->opt_value
&& (CINT == t->opt_type || CHEX == t->opt_type))
t->opt_value = strdup("0");
if (ch && !ch->opt_type)
ch->opt_type = t->opt_type;
}
| T_TYPE T_TEXT {
t->opt_type = t->opt_type ? t->opt_type : (cType)$1;
if (!t->opt_prompt)
t->opt_prompt = strdup($2);
free($2);
if (!t->opt_value
&& (CBOOL == t->opt_type || CTRISTATE == t->opt_type))
t->opt_value = strdup("n");
else if (!t->opt_value
&& (CINT == t->opt_type || CHEX == t->opt_type))
t->opt_value = strdup("0");
if (ch && !ch->opt_type)
ch->opt_type = t->opt_type;
}
| T_DEFTYPE T_TEXT {
t->opt_type = t->opt_type ? t->opt_type : (cType)$1;
free(t->opt_value);
t->opt_value = $2;
if (ch && !ch->opt_type)
ch->opt_type = t->opt_type;
}
| T_DEFAULT T_TEXT {
if (t->opt_value
&& (!strcmp(t->opt_value, "n") || !strcmp(t->opt_value, "0")))
{
free(t->opt_value);
t->opt_value = NULL;
}
t->opt_value = append(t->opt_value, $2); free($2);
}
| T_PROMPT T_TEXT {
if(!t->opt_prompt)
t->opt_prompt = strdup($2);
free($2);
}
| T_DEPENDS T_TEXT {
t->opt_depends = append(t->opt_depends, $2); free($2);
}
| T_SELECT T_TEXT {
t->opt_select = append(t->opt_select, $2); free($2);
}
| T_IMPLY T_TEXT {
t->opt_imply = append(t->opt_imply, $2); free($2);
}
| T_RANGE T_TEXT {
t->opt_range = append(t->opt_range, $2); free($2);
}
| T_HELP T_HELPTEXT {
if (!t->opt_help)
t->opt_help = strdup($2);
free($2);
}
;
%%
void
yyerror(YYLTYPE *loc, char const *serr)
{
if (opts & OUT_VERBOSE)
{
sEntry *s = (tree_root()->data);
warnx("%s: %d: %s", s->fname, loc->last_line, serr);
}
}