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 bugs in old versions of flex */
38 #pragma GCC diagnostic ignored "-Wsign-compare"
39 #pragma GCC diagnostic ignored "-Wunused-value"
46 #include "command_graph.h"
65 static void extendbuf(char **what, const char *arg)
70 size_t vall = strlen(*what), argl = strlen(arg);
71 *what = realloc(*what, vall + argl + 1);
72 memcpy(*what + vall, arg, argl);
73 (*what)[vall + argl] = '\0';
76 #define extend(x) extendbuf(&value, x)
81 OPERATOR [!%&/\[\]{}=?:^|\*.;><~'\\+-]
89 %option outfile="lib/defun_lex.c"
90 %option prefix="def_yy"
103 <INITIAL,linestart,preproc>"/*" comment_link = YY_START; extend(yytext); BEGIN(comment);
104 <comment>[^*\n]* extend(yytext);
105 <comment>"*"+[^*/\n]* extend(yytext);
106 <comment>\n extend(yytext);
107 <comment>"*"+"/" extend(yytext); BEGIN(comment_link); return COMMENT;
109 <INITIAL,linestart,preproc>"//" comment_link = YY_START; extend(yytext); BEGIN(linecomment);
110 <linecomment>[^\n]* extend(yytext);
111 <linecomment>\n BEGIN((comment_link == INITIAL) ? linestart : comment_link); return COMMENT;
113 <linestart># BEGIN(preproc);
114 <preproc>\n BEGIN(INITIAL); return PREPROC;
115 <preproc>[^\n\\]+ extend(yytext);
116 <preproc>\\\n extend(yytext);
117 <preproc>\\+[^\n] extend(yytext);
119 [\"\'] string_end = yytext[0]; extend(yytext); BEGIN(rstring);
122 if (yytext[0] == string_end) {
127 <rstring>\\\n /* ignore */
128 <rstring>\\. extend(yytext);
129 <rstring>[^\\\"\']+ extend(yytext);
131 "DEFUN" value = strdup(yytext); return DEFUNNY;
132 "DEFUN_NOSH" value = strdup(yytext); return DEFUNNY;
133 "DEFUN_HIDDEN" value = strdup(yytext); return DEFUNNY;
134 "DEFPY" value = strdup(yytext); return DEFUNNY;
135 "ALIAS" value = strdup(yytext); return DEFUNNY;
136 "ALIAS_HIDDEN" value = strdup(yytext); return DEFUNNY;
137 "install_element" value = strdup(yytext); return INSTALL;
138 "VTYSH_TARGETS" value = strdup(yytext); return AUXILIARY;
139 "VTYSH_NODESWITCH" value = strdup(yytext); return AUXILIARY;
141 [ \t\n]+ /* ignore */
143 {ID} BEGIN(INITIAL); value = strdup(yytext); return ID;
144 {OPERATOR} BEGIN(INITIAL); value = strdup(yytext); return OPERATOR;
145 {SPECIAL} BEGIN(INITIAL); value = strdup(yytext); return SPECIAL;
146 . /* printf("-- '%s' in init\n", yytext); */ BEGIN(INITIAL); return yytext[0];
150 static int yylex_clr(char **retbuf)
152 int rv = def_yylex();
158 static PyObject *get_args(void)
160 PyObject *pyObj = PyList_New(0);
161 PyObject *pyArg = NULL;
167 while ((token = yylex_clr(&tval)) != YY_NULL) {
168 if (token == SPECIAL && tval[0] == '(') {
172 if (token == COMMENT) {
176 fprintf(stderr, "invalid input!\n");
180 while ((token = yylex_clr(&tval)) != YY_NULL) {
181 if (token == COMMENT) {
185 if (token == SPECIAL) {
186 if (depth == 1 && (tval[0] == ',' || tval[0] == ')')) {
188 PyList_Append(pyObj, pyArg);
190 if (tval[0] == ')') {
203 pyArg = PyList_New(0);
204 PyList_Append(pyArg, PyUnicode_FromString(tval));
210 /* _clippy.parse() -- read a C file, returning a list of interesting bits.
211 * note this ditches most of the actual C code. */
212 PyObject *clippy_parse(PyObject *self, PyObject *args)
214 const char *filename;
215 if (!PyArg_ParseTuple(args, "s", &filename))
218 FILE *fd = fopen(filename, "r");
220 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
227 PyObject *pyCont = PyDict_New();
228 PyObject *pyObj = PyList_New(0);
229 PyDict_SetItemString(pyCont, "filename", PyUnicode_FromString(filename));
230 PyDict_SetItemString(pyCont, "data", pyObj);
232 while ((token = yylex_clr(&tval)) != YY_NULL) {
233 int lineno = yylineno;
234 PyObject *pyItem = NULL, *pyArgs;
240 pyItem = PyDict_New();
241 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString(tval));
242 PyDict_SetItemString(pyItem, "args", pyArgs);
245 if (strncmp(tval, "//~", 3) && strncmp(tval, "/*~", 3))
247 pyItem = PyDict_New();
248 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("COMMENT"));
249 PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
252 pyItem = PyDict_New();
253 PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("PREPROC"));
254 PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
258 PyDict_SetItemString(pyItem, "lineno", PyLong_FromLong(lineno));
259 PyList_Append(pyObj, pyItem);