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
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* clippy (CLI preparator in python) C pseudo-lexer
* Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
*/
/* This is just enough of a lexer to make rough sense of a C source file.
* It handles C preprocessor directives, strings, and looks for FRR-specific
* idioms (aka DEFUN).
*
* There is some preliminary support for documentation comments for DEFUNs.
* They would look like this (note the ~): (replace \ by /)
*
* \*~ documentation for foobar_cmd
* * parameter does xyz
* *\
* DEFUN(foobar_cmd, ...)
*
* This is intended for user documentation / command reference. Don't put
* code documentation in it.
*/
%top{
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
}
%{
/* ignore harmless bugs in old versions of flex */
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wunused-value"
#include "config.h"
#include <Python.h>
#include <string.h>
#include <stdlib.h>
#include "command_graph.h"
#include "clippy.h"
#define ID 258
#define PREPROC 259
#define OPERATOR 260
#define STRING 261
#define COMMENT 262
#define SPECIAL 263
#define DEFUNNY 270
#define INSTALL 271
#define AUXILIARY 272
int comment_link;
char string_end;
char *value;
static const char *yyfilename;
static void extendbuf(char **what, const char *arg)
{
if (!*what)
*what = strdup(arg);
else {
size_t vall = strlen(*what), argl = strlen(arg);
*what = realloc(*what, vall + argl + 1);
memcpy(*what + vall, arg, argl);
(*what)[vall + argl] = '\0';
}
}
#define extend(x) extendbuf(&value, x)
#ifndef __clang_analyzer__
%}
ID [A-Za-z0-9_]+
OPERATOR [!%&/\[\]{}=?:^|\*.;><~'\\+-]
SPECIAL [(),]
%pointer
%option yylineno
%option noyywrap
%option noinput
%option nounput
%option outfile="lib/defun_lex.c"
%option prefix="def_yy"
%option 8bit
%s linestart
%x comment
%x linecomment
%x preproc
%x rstring
%%
BEGIN(linestart);
\n BEGIN(linestart);
<INITIAL,linestart,preproc>"/*" comment_link = YY_START; extend(yytext); BEGIN(comment);
<comment>[^*\n]* extend(yytext);
<comment>"*"+[^*/\n]* extend(yytext);
<comment>\n extend(yytext);
<comment>"*"+"/" extend(yytext); BEGIN(comment_link); return COMMENT;
<INITIAL,linestart,preproc>"//" comment_link = YY_START; extend(yytext); BEGIN(linecomment);
<linecomment>[^\n]* extend(yytext);
<linecomment>\n BEGIN((comment_link == INITIAL) ? linestart : comment_link); return COMMENT;
<linestart># BEGIN(preproc);
<preproc>\n BEGIN(INITIAL); return PREPROC;
<preproc>[^\n\\]+ extend(yytext);
<preproc>\\\n extend(yytext);
<preproc>\\+[^\n] extend(yytext);
[\"\'] string_end = yytext[0]; extend(yytext); BEGIN(rstring);
<rstring>[\"\'] {
extend(yytext);
if (yytext[0] == string_end) {
BEGIN(INITIAL);
return STRING;
}
}
<rstring>\\\n /* ignore */
<rstring>\n {
fprintf(stderr,
"%s:%d: string continues past the end of the line\n",
yyfilename, yylineno);
free(value);
value = NULL;
BEGIN(INITIAL);
return STRING;
}
<rstring>\\. extend(yytext);
<rstring>[^\\\"\'\n]+ extend(yytext);
"DEFUN" value = strdup(yytext); return DEFUNNY;
"DEFUN_NOSH" value = strdup(yytext); return DEFUNNY;
"DEFUN_HIDDEN" value = strdup(yytext); return DEFUNNY;
"DEFPY" value = strdup(yytext); return DEFUNNY;
"DEFPY_NOSH" value = strdup(yytext); return DEFUNNY;
"DEFPY_ATTR" value = strdup(yytext); return DEFUNNY;
"DEFPY_HIDDEN" value = strdup(yytext); return DEFUNNY;
"DEFPY_YANG" value = strdup(yytext); return DEFUNNY;
"DEFPY_YANG_HIDDEN" value = strdup(yytext); return DEFUNNY;
"DEFPY_YANG_NOSH" value = strdup(yytext); return DEFUNNY;
"ALIAS" value = strdup(yytext); return DEFUNNY;
"ALIAS_HIDDEN" value = strdup(yytext); return DEFUNNY;
"install_element" value = strdup(yytext); return INSTALL;
"VTYSH_TARGETS" value = strdup(yytext); return AUXILIARY;
"VTYSH_NODESWITCH" value = strdup(yytext); return AUXILIARY;
[ \t\n]+ /* ignore */
\\ /* ignore */
{ID} BEGIN(INITIAL); value = strdup(yytext); return ID;
{OPERATOR} BEGIN(INITIAL); value = strdup(yytext); return OPERATOR;
{SPECIAL} BEGIN(INITIAL); value = strdup(yytext); return SPECIAL;
. /* printf("-- '%s' in init\n", yytext); */ BEGIN(INITIAL); return yytext[0];
%%
#endif /* __clang_analyzer__ */
static int yylex_clr(char **retbuf)
{
int rv = def_yylex();
*retbuf = value;
value = NULL;
return rv;
}
static PyObject *get_args(const char *filename, int lineno)
{
PyObject *pyObj = PyList_New(0);
PyObject *pyArg = NULL;
char *tval;
int depth = 1;
int token;
while ((token = yylex_clr(&tval)) != YY_NULL) {
if (token == SPECIAL && tval[0] == '(') {
free(tval);
break;
}
if (token == COMMENT) {
free(tval);
continue;
}
fprintf(stderr, "invalid input!\n");
exit(1);
}
while ((token = yylex_clr(&tval)) != YY_NULL) {
if (token == COMMENT) {
free(tval);
continue;
}
if (token == PREPROC) {
free(tval);
Py_DECREF(pyObj);
return PyErr_Format(PyExc_ValueError,
"%s:%d: cannot process CPP directive within argument list",
filename, lineno);
}
if (token == SPECIAL) {
if (depth == 1 && (tval[0] == ',' || tval[0] == ')')) {
if (pyArg)
PyList_Append(pyObj, pyArg);
pyArg = NULL;
if (tval[0] == ')') {
free(tval);
break;
}
free(tval);
continue;
}
if (tval[0] == '(')
depth++;
if (tval[0] == ')')
depth--;
}
if (!tval)
return PyErr_Format(PyExc_ValueError,
"%s:%d: invalid token in DEFPY parameters",
filename, lineno);
if (!pyArg)
pyArg = PyList_New(0);
PyList_Append(pyArg, PyUnicode_FromString(tval));
free(tval);
}
return pyObj;
}
/* _clippy.parse() -- read a C file, returning a list of interesting bits.
* note this ditches most of the actual C code. */
PyObject *clippy_parse(PyObject *self, PyObject *args)
{
const char *filename;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
FILE *fd = fopen(filename, "r");
if (!fd)
return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
char *tval;
int token;
yyin = fd;
value = NULL;
yyfilename = filename;
PyObject *pyCont = PyDict_New();
PyObject *pyObj = PyList_New(0);
PyDict_SetItemString(pyCont, "filename", PyUnicode_FromString(filename));
PyDict_SetItemString(pyCont, "data", pyObj);
while ((token = yylex_clr(&tval)) != YY_NULL) {
int lineno = yylineno;
PyObject *pyItem = NULL, *pyArgs;
switch (token) {
case DEFUNNY:
case INSTALL:
case AUXILIARY:
pyArgs = get_args(filename, lineno);
if (!pyArgs) {
free(tval);
Py_DECREF(pyCont);
yyfilename = NULL;
return NULL;
}
pyItem = PyDict_New();
PyDict_SetItemString(pyItem, "type", PyUnicode_FromString(tval));
PyDict_SetItemString(pyItem, "args", pyArgs);
break;
case COMMENT:
if (strncmp(tval, "//~", 3) && strncmp(tval, "/*~", 3))
break;
pyItem = PyDict_New();
PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("COMMENT"));
PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
break;
case PREPROC:
pyItem = PyDict_New();
PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("PREPROC"));
PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
lineno--;
break;
}
if (pyItem) {
PyDict_SetItemString(pyItem, "lineno", PyLong_FromLong(lineno));
PyList_Append(pyObj, pyItem);
}
free(tval);
}
def_yylex_destroy();
fclose(fd);
yyfilename = NULL;
return pyCont;
}
|