Debian Codegraph @

cmd/tools/gmlscan.l

raw
1 /* By default, Flex emits a lexer using symbols prefixed with "yy". Graphviz
2 * contains multiple Flex-generated lexers, so we alter this prefix to avoid
3 * symbol clashes.
4 */
5 %option prefix="gml"
6
7 /* Avoid generating an unused input function. See
8 https://westes.github.io/flex/manual/Scanner-Options.html
9 */
10 %option noinput
11
12 %{
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <gml2gv.h>
17 #include <gmlparse.h>
18 #include <util/alloc.h>
19 #include <util/agxbuf.h>
20 #include "config.h"
21
22 #define GRAPH_EOF_TOKEN '@' /* lex class must be defined below */
23
24 static int line_num = 1;
25 static int errors;
26 static FILE* Ifile;
27
28 void initgmlscan(FILE *ifile)
29 {
30 if (ifile) {
31 Ifile = ifile;
32 line_num = 1;
33 }
34 errors = 0;
35 }
36
37 #ifndef YY_INPUT
38 #define YY_INPUT(buf,result,max_size) \
39 if ((result = fread(buf, 1, max_size, Ifile)) < 0) \
40 YY_FATAL_ERROR( "input in flex scanner failed" )
41 #endif
42
43 /// buffer for arbitrary length strings
44 static agxbuf Sbuf;
45
46 static void beginstr(void) {
47 assert(agxblen(&Sbuf) == 0 && "leaking memory");
48 }
49
50 static void addstr(const char *src) {
51 agxbput(&Sbuf, src);
52 }
53
54 static void endstr(void) {
55 // take ownership of the Sbuf backing memory
56 gmllval.str = agxbdisown(&Sbuf);
57 }
58
59 %}
60 GRAPH_EOF_TOKEN [@]
61 ASCII [ !#$%\047-\177]
62 DIGIT [0-9]
63 L_INT [-+]?{DIGIT}+
64 MANTISSA E[-+]?{DIGIT}
65 L_REAL [-+]?{DIGIT}*\.{DIGIT}*{MANTISSA}?
66 L_ID [a-zA-Z_][_a-zA-Z0-9]*
67 %x qstring
68 %%
69 {GRAPH_EOF_TOKEN} return(EOF);
70 <INITIAL,qstring>\n line_num++;
71 ^"#".* /* ignore # line */
72 [ \t\r] /* ignore whitespace */
73
74 "graph" return (GRAPH);
75 "node" return (NODE);
76 "edge" return (EDGE);
77 "directed" return (DIRECTED);
78 "id" return (ID);
79 "source" return (SOURCE);
80 "target" return (TARGET);
81 "x" return (XVAL);
82 "y" return (YVAL);
83 "w" return (WVAL);
84 "h" return (HVAL);
85 "label" return (LABEL);
86 "graphics" return (GRAPHICS);;
87 "LabelGraphics" return (LABELGRAPHICS);
88 "type" return (TYPE);
89 "fill" return (FILL);
90 "outline" return (OUTLINE);
91 "outlineStyle" return (OUTLINESTYLE);
92 "outlineWidth" return (OUTLINEWIDTH);
93 "width" return (WIDTH);
94 "style" return (STYLE);
95 "Line" return (LINE);
96 "point" return (POINT);
97 "text" return (TEXT);
98 "fontSize" return (FONTSIZE);
99 "fontName" return (FONTNAME);
100 "color" return (COLOR);
101 {L_INT} { gmllval.str = gv_strdup(yytext); return (INTEGER); }
102 {L_REAL} { gmllval.str = gv_strdup(yytext); return (REAL); }
103 {L_ID} { gmllval.str = gv_strdup(yytext); return (NAME); }
104 ["] BEGIN(qstring); beginstr();
105
106 <qstring>["] BEGIN(INITIAL); endstr(); return (STRING);
107 <qstring>([^"]+) addstr(yytext);
108
109 . return (yytext[0]);
110
111 %%
112
113 void gmlerror(const char *str)
114 {
115 if (errors)
116 return;
117 errors = 1;
118 agwarningf(" %s in line %d near '%s'\n", str,line_num,yytext);
119 }
120
121 int gmlerrors(void)
122 {
123 return errors;
124 }
125
126 void gmllexeof(void) { unput(GRAPH_EOF_TOKEN); }
127
128 #ifndef YY_CALL_ONLY_ARG
129 # define YY_CALL_ONLY_ARG void
130 #endif
131
132 int yywrap(YY_CALL_ONLY_ARG)
133 {
134 return 1;
135 }
136