-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcligen_parse.l
251 lines (217 loc) · 8.54 KB
/
cligen_parse.l
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
/*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2001-2022 Olof Hagsand
This file is part of CLIgen.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2, indicate
your decision by deleting the provisions above and replace them with the
notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
*/
%{
#include "cligen_config.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <netinet/in.h>
#include <ctype.h>
#ifndef isblank
#define isblank(c) (c==' ')
#endif /* isblank */
#include "cligen_parse.tab.h" /* generated */
#include "cligen_buf.h"
#include "cligen_cv.h"
#include "cligen_cvec.h"
#include "cligen_parsetree.h"
#include "cligen_callback.h"
#include "cligen_object.h"
#include "cligen_parse.h"
/* Redefine main lex function so that you can send arguments to it */
#define YY_DECL int cligen_parselex(void *_cy)
/* typecast macro */
#define _CY ((cligen_yacc *)_cy)
#undef cligen_parsewrap
int
cligen_parsewrap(void)
{
return 1;
}
/*! like strdup but strip \:s */
static char *
stripdup(char *s0)
{
char *s1;
char *s;
if ((s1 = strdup(s0)) == NULL){
fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno));
return NULL;
}
while ((s = index(s1, '\\')) != NULL)
memmove(s, s+1, strlen(s));
return s1;
}
/*
See block_chart for lex states.
*/
%}
%s OPTION
%s HELP
%s COMMENT
%s NAMEORTYPE
%s VARIABLE
%s CHOICE
%s ESCAPE
%s STRING
%%
<INITIAL>[ \t]*
<INITIAL>, { BEGIN(OPTION); return *yytext; }
<INITIAL># { _CY->cy_lex_state =INITIAL; BEGIN(COMMENT); }
<INITIAL>\n { _CY->cy_linenum++; }
<INITIAL>\r
<INITIAL>= { BEGIN(OPTION); return *yytext; }
<INITIAL>; { return *yytext; }
<INITIAL><<EOF>> { return MY_EOF; }
<INITIAL>\< { BEGIN(NAMEORTYPE); return *yytext; }
<INITIAL>\( { return *yytext; }
<INITIAL>\) { return *yytext; }
<INITIAL>\| { return *yytext; }
<INITIAL>\(\" { BEGIN(HELP); return PDQ; /* parenth double-q*/ }
<INITIAL>\[ { return *yytext; }
<INITIAL>\] { return *yytext; }
<INITIAL>"@{" { return SETS; }
<INITIAL>\{ { return *yytext; }
<INITIAL>\} { return *yytext; }
<INITIAL>\@ { return *yytext; }
<INITIAL>([^@ \t,#\n\r=;\\<\(\)\[\]\|\{\}]|\\.)+ {
cligen_parselval.string = stripdup(yytext);
return NAME; }
<INITIAL>. { return -1; }
<HELP>\n { _CY->cy_linenum++;
return *yytext; }
<HELP>\r
<HELP><<EOF>> { return MY_EOF; }
<HELP>\"\) { BEGIN(INITIAL); return DQP; /* double-quote parenthes */}
<HELP>\\ { _CY->cy_lex_state = HELP; BEGIN(ESCAPE); }
<HELP>[^\\"\n\r]+ {
int i;
for (i=0;i<strlen(yytext);i++){
if (!isblank(yytext[i]))
break;
}
cligen_parselval.string = yytext+i;
return HELPSTR;
}
<HELP>. { cligen_parselval.string = yytext;
return HELPSTR;}
<NAMEORTYPE>\> { BEGIN(INITIAL); return *yytext; }
<NAMEORTYPE>\n { _CY->cy_linenum++; }
<NAMEORTYPE>\: { return *yytext; }
<NAMEORTYPE>[ \t]+ { BEGIN(VARIABLE); }
<NAMEORTYPE>([^ \t>:]|\\.)+ {
cligen_parselval.string = strdup(yytext);
return NAME; }
<VARIABLE>[ \t]+
<VARIABLE>\n { _CY->cy_linenum++; }
<VARIABLE>\> { BEGIN(INITIAL); return *yytext; }
<VARIABLE>\: { return *yytext; }
<VARIABLE>\, { return *yytext; }
<VARIABLE>\| { return *yytext; }
<VARIABLE>\( { return *yytext; }
<VARIABLE>\) { return *yytext; }
<VARIABLE>\[ { return *yytext; }
<VARIABLE>\] { return *yytext; }
<VARIABLE>! { return *yytext; }
<VARIABLE>\" { _CY->cy_lex_string_state =VARIABLE;BEGIN(STRING); return DQ; }
<VARIABLE>show { return V_SHOW; }
<VARIABLE>range { return V_RANGE; }
<VARIABLE>length { return V_LENGTH; }
<VARIABLE>fraction-digits { return V_FRACTION_DIGITS; }
<VARIABLE>choice: { BEGIN(CHOICE);return V_CHOICE; }
<VARIABLE>keyword { return V_KEYWORD; }
<VARIABLE>regexp { return V_REGEXP; }
<VARIABLE>translate { return V_TRANSLATE; }
<VARIABLE>preference { return V_PREFERENCE; }
<VARIABLE>[-+]?[0-9]+\.[0-9]+ { cligen_parselval.string = strdup(yytext); return DECIMAL;}
<VARIABLE>[-+]?[0-9]+ { cligen_parselval.string = strdup(yytext); return NUMBER;}
<VARIABLE>([^ \t\n>:\|\"\(\)\[\]]|\\.)+ {
cligen_parselval.string = strdup(yytext);
return NAME; }
<CHOICE>[ \t]+
<CHOICE>\n { _CY->cy_linenum++; }
<CHOICE>\| { return *yytext; }
<CHOICE>\> { BEGIN(INITIAL); return *yytext; }
<CHOICE>[-+]?[0-9]+\.[0-9]+ { cligen_parselval.string = strdup(yytext); return DECIMAL;}
<CHOICE>[-+]?[0-9]+ { cligen_parselval.string = strdup(yytext); return NUMBER;}
<CHOICE>([^ \t\n>\|\"\[\]]|\\.)+ {
cligen_parselval.string = strdup(yytext);
return NAME; }
<OPTION>[ \t]+
<OPTION>, { return *yytext; }
<OPTION># { _CY->cy_lex_state =INITIAL; BEGIN(COMMENT); }
<OPTION>\n { _CY->cy_linenum++; }
<OPTION>= { return *yytext; }
<OPTION>; { BEGIN(INITIAL); return *yytext;}
<OPTION><<EOF>> { return MY_EOF; }
<OPTION>\( { return *yytext; }
<OPTION>\) { return *yytext; }
<OPTION>\{ { BEGIN(INITIAL); return *yytext;}
<OPTION>\" { _CY->cy_lex_string_state =INITIAL;BEGIN(STRING); return DQ; }
<OPTION>([^ \t,#\n=;\(\)\{\}\"]|\\.)+ {
cligen_parselval.string = strdup(yytext);
return NAME; }
<OPTION>. { return -1;}
<STRING>\n { _CY->cy_linenum++; }
<STRING>\" { BEGIN(_CY->cy_lex_string_state); return DQ; }
<STRING>\\\" { cligen_parselval.string = strdup(yytext);
return CHARS;}
<STRING>\\ { cligen_parselval.string = strdup(yytext);
return CHARS;}
<STRING>[^\\"\n]+ { cligen_parselval.string = strdup(yytext);
return CHARS;}
<ESCAPE>. { BEGIN(_CY->cy_lex_state);
cligen_parselval.string = strdup(yytext);
return CHARS; }
<COMMENT>\n { _CY->cy_linenum++; BEGIN(_CY->cy_lex_state);}
<COMMENT><<EOF>> { return MY_EOF; }
<COMMENT>[^\n]+
%%
/*! Initialize scanner.
*/
int
cgl_init(cligen_yacc *cy)
{
BEGIN(INITIAL);
cy->cy_lexbuf = yy_scan_string (cy->cy_parse_string);
#if 1 /* XXX: just to use unput to avoid warning */
if (0)
yyunput(0, "");
#endif
return 0;
}
/*! Exit cligen lex parser. Free buffers
*
* Even within Flex version 2.5 (this is assumed), freeing buffers is different.
*/
int
cgl_exit(cligen_yacc *cy)
{
yy_delete_buffer(cy->cy_lexbuf);
cligen_parselex_destroy(); /* modern */
return 0;
}