1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * clippy (CLI preparator in python) C pseudo-lexer
4 * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
7 /* This is just enough of a lexer to make rough sense of a C source file.
8 * It handles C preprocessor directives, strings, and looks for FRR-specific
11 * There is some preliminary support for documentation comments for DEFUNs.
12 * They would look like this (note the ~): (replace \ by /)
14 * \*~ documentation for foobar_cmd
15 * * parameter does xyz
17 * DEFUN(foobar_cmd, ...)
19 * This is intended for user documentation / command reference. Don't put
20 * code documentation in it.
29 /* ignore harmless bugs in old versions of flex */
30 #pragma GCC diagnostic ignored "-Wsign-compare"
31 #pragma GCC diagnostic ignored "-Wunused-value"
38 #include "command_graph.h"
57 static void extendbuf(char **what, const char *arg)
62 size_t vall = strlen(*what), argl = strlen(arg);
63 *what = realloc(*what, vall + argl + 1);
64 memcpy(*what + vall, arg, argl);
65 (*what)[vall + argl] = '\0';
68 #define extend(x) extendbuf(&value, x)
70 #ifndef __clang_analyzer__
75 OPERATOR [!%&/\[\]{}=?:^|\*.;><~'\\+-]
83 %option outfile="lib/defun_lex.c"
84 %option prefix="def_yy"
97 <INITIAL,linestart,preproc>"/*" comment_link = YY_START; extend(yytext); BEGIN(comment);
98 <comment>[^*\n]* extend(yytext);
99 <comment>"*"+[^*/\n]* extend(yytext);
100 <comment>\n extend(yytext);
101 <comment>"*"+"/" extend(yytext); BEGIN(comment_link); return COMMENT;
103 <INITIAL,linestart,preproc>"//" comment_link = YY_START; extend(yytext); BEGIN(linecomment);
104 <linecomment>[^\n]* extend(yytext);
105 <linecomment>\n BEGIN((comment_link == INITIAL) ? linestart : comment_link); return COMMENT;
107 <linestart># BEGIN(preproc);
108 <preproc>\n BEGIN(INITIAL); return PREPROC;
109 <preproc>[^\n\\]+ extend(yytext);
110 <preproc>\\\n extend(yytext);
111 <preproc>\\+[^\n] extend(yytext);
113 [\"\'] string_end = yytext[0]; extend(yytext); BEGIN(rstring);
116 if (yytext[0] == string_end) {
121 <rstring>\\\n /* ignore */
122 <rstring>\\. extend(yytext);
123 <rstring>[^\\\"\']+ extend(yytext);
125 "DEFUN" value = strdup(yytext); return DEFUNNY;
126 "DEFUN_NOSH" value = strdup(yytext); return DEFUNNY;
127 "DEFUN_HIDDEN" value = strdup(yytext); return DEFUNNY;
128 "DEFPY" value = strdup(yytext); return DEFUNNY;
129 "DEFPY_NOSH" value = strdup(yytext); return DEFUNNY;
130 "DEFPY_ATTR" value = strdup(yytext); return DEFUNNY;
131 "DEFPY_HIDDEN" value = strdup(yytext); return DEFUNNY;
132 "DEFPY_YANG" value = strdup(yytext); return DEFUNNY;
133 "DEFPY_YANG_NOSH" value = strdup(yytext); return DEFUNNY;
134 "ALIAS" value = strdup(yytext); return DEFUNNY;
135 "ALIAS_HIDDEN" value = strdup(yytext); return DEFUNNY;
136 "install_element" value = strdup(yytext); return INSTALL;
137 "VTYSH_TARGETS" value = strdup(yytext); return AUXILIARY;
138 "VTYSH_NODESWITCH" value = strdup(yytext); return AUXILIARY;
140 [ \t\n]+ /* ignore */
142 {ID} BEGIN(INITIAL); value = strdup(yytext); return ID;
143 {OPERATOR} BEGIN(INITIAL); value = strdup(yytext); return OPERATOR;
144 {SPECIAL} BEGIN(INITIAL); value = strdup(yytext); return SPECIAL;
145 . /* printf("-- '%s' in init\n", yytext); */ BEGIN(INITIAL); return yytext[0];
149 #endif /* __clang_analyzer__ */
151 static int yylex_clr(char **retbuf)
153 int rv = def_yylex();
159 static PyObject *get_args(const char *filename, int lineno)
161 PyObject *pyObj = PyList_New(0);
162 PyObject *pyArg = NULL;
168 while ((token = yylex_clr(&tval)) != YY_NULL) {
169 if (token == SPECIAL && tval[0] == '(') {
173 if (token == COMMENT) {
177 fprintf(stderr, "invalid input!\n");
181 while ((token = yylex_clr(&tval)) != YY_NULL) {
182 if (token == COMMENT) {
186 if (token == PREPROC) {
189 return PyErr_Format(PyExc_ValueError,
190 "%s:%d: cannot process CPP directive within argument list",
193 if (token == SPECIAL) {
194 if (depth == 1 && (tval[0] == ',' || tval[0] == ')')) {
196 PyList_Append(pyObj, pyArg);
198 if (tval[0] == ')') {
211 pyArg = PyList_New(0);
212 PyList_Append(pyArg, PyUnicode_FromString(tval));
218 /* _clippy.parse() -- read a C file, returning a list of interesting bits.
219 * note this ditches most of the actual C code. */
220 PyObject *clippy_parse(PyObject *self, PyObject *args)
222 const char *filename;
223 if (!PyArg_ParseTuple(args, "s", &filename))
226 FILE *fd = fopen(filename, "r");
228 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
235 PyObject *pyCont = PyDict_New();
236 PyObject *pyObj = PyList_New(0);
237 PyDict_SetItemString(pyCont, "filename", PyUnicode_FromString(filename));
238 PyDict_SetItemString(pyCont, "data", pyObj);
240 while ((token = yylex_clr(&tval)) != YY_NULL) {
241 int lineno = yylineno;
242 PyObject *pyItem = NULL, *pyArgs;
247 pyArgs = get_args(filename, lineno);
253 pyItem = PyDict_New();
254 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString(tval));
255 PyDict_SetItemString(pyItem, "args", pyArgs);
258 if (strncmp(tval, "//~", 3) && strncmp(tval, "/*~", 3))
260 pyItem = PyDict_New();
261 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("COMMENT"));
262 PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
265 pyItem = PyDict_New();
266 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("PREPROC"));
267 PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
272 PyDict_SetItemString(pyItem, "lineno", PyLong_FromLong(lineno));
273 PyList_Append(pyObj, pyItem);