3 * clippy (CLI preparator in python) C pseudo-lexer
4 * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 /* This is just enough of a lexer to make rough sense of a C source file.
22 * It handles C preprocessor directives, strings, and looks for FRR-specific
25 * There is some preliminary support for documentation comments for DEFUNs.
26 * They would look like this (note the ~): (replace \ by /)
28 * \*~ documentation for foobar_cmd
29 * * parameter does xyz
31 * DEFUN(foobar_cmd, ...)
33 * This is intended for user documentation / command reference. Don't put
34 * code documentation in it.
37 /* ignore harmless bug in old versions of flex */
38 #pragma GCC diagnostic ignored "-Wsign-compare"
45 #include "command_graph.h"
64 static void extendbuf(char **what, const char *arg)
69 size_t vall = strlen(*what), argl = strlen(arg);
70 *what = realloc(*what, vall + argl + 1);
71 memcpy(*what + vall, arg, argl);
72 (*what)[vall + argl] = '\0';
75 #define extend(x) extendbuf(&value, x)
80 OPERATOR [!%&/\[\]{}=?:^|\*.;><~'\\+-]
88 %option outfile="lib/defun_lex.c"
89 %option prefix="def_yy"
102 <INITIAL,linestart,preproc>"/*" comment_link = YY_START; extend(yytext); BEGIN(comment);
103 <comment>[^*\n]* extend(yytext);
104 <comment>"*"+[^*/\n]* extend(yytext);
105 <comment>\n extend(yytext);
106 <comment>"*"+"/" extend(yytext); BEGIN(comment_link); return COMMENT;
108 <INITIAL,linestart,preproc>"//" comment_link = YY_START; extend(yytext); BEGIN(linecomment);
109 <linecomment>[^\n]* extend(yytext);
110 <linecomment>\n BEGIN((comment_link == INITIAL) ? linestart : comment_link); return COMMENT;
112 <linestart># BEGIN(preproc);
113 <preproc>\n BEGIN(INITIAL); return PREPROC;
114 <preproc>[^\n\\]+ extend(yytext);
115 <preproc>\\\n extend(yytext);
116 <preproc>\\+[^\n] extend(yytext);
118 [\"\'] string_end = yytext[0]; extend(yytext); BEGIN(rstring);
121 if (yytext[0] == string_end) {
126 <rstring>\\\n /* ignore */
127 <rstring>\\. extend(yytext);
128 <rstring>[^\\\"\']+ extend(yytext);
130 "DEFUN" value = strdup(yytext); return DEFUNNY;
131 "DEFUN_NOSH" value = strdup(yytext); return DEFUNNY;
132 "DEFUN_HIDDEN" value = strdup(yytext); return DEFUNNY;
133 "DEFPY" 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 static int yylex_clr(char **retbuf)
151 int rv = def_yylex();
157 static PyObject *get_args(void)
159 PyObject *pyObj = PyList_New(0);
160 PyObject *pyArg = NULL;
166 while ((token = yylex_clr(&tval)) != YY_NULL) {
167 if (token == SPECIAL && tval[0] == '(') {
171 if (token == COMMENT) {
175 fprintf(stderr, "invalid input!\n");
179 while ((token = yylex_clr(&tval)) != YY_NULL) {
180 if (token == COMMENT) {
184 if (token == SPECIAL) {
185 if (depth == 1 && (tval[0] == ',' || tval[0] == ')')) {
187 PyList_Append(pyObj, pyArg);
189 if (tval[0] == ')') {
202 pyArg = PyList_New(0);
203 PyList_Append(pyArg, PyUnicode_FromString(tval));
209 /* _clippy.parse() -- read a C file, returning a list of interesting bits.
210 * note this ditches most of the actual C code. */
211 PyObject *clippy_parse(PyObject *self, PyObject *args)
213 const char *filename;
214 if (!PyArg_ParseTuple(args, "s", &filename))
217 FILE *fd = fopen(filename, "r");
219 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
226 PyObject *pyCont = PyDict_New();
227 PyObject *pyObj = PyList_New(0);
228 PyDict_SetItemString(pyCont, "filename", PyUnicode_FromString(filename));
229 PyDict_SetItemString(pyCont, "data", pyObj);
231 while ((token = yylex_clr(&tval)) != YY_NULL) {
232 int lineno = yylineno;
233 PyObject *pyItem = NULL, *pyArgs;
239 pyItem = PyDict_New();
240 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString(tval));
241 PyDict_SetItemString(pyItem, "args", pyArgs);
244 if (strncmp(tval, "//~", 3) && strncmp(tval, "/*~", 3))
246 pyItem = PyDict_New();
247 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("COMMENT"));
248 PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
251 pyItem = PyDict_New();
252 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("PREPROC"));
253 PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
257 PyDict_SetItemString(pyItem, "lineno", PyLong_FromLong(lineno));
258 PyList_Append(pyObj, pyItem);