Graphviz 12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
scan.l
Go to the documentation of this file.
1
5/*************************************************************************
6 * Copyright (c) 2011 AT&T Intellectual Property
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * https://www.eclipse.org/legal/epl-v10.html
11 *
12 * Contributors: Details at https://graphviz.org
13 *************************************************************************/
14
15
16/* requires flex (i.e. not lex) */
17
18 /* By default, Flex emits a lexer using symbols prefixed with "yy". Graphviz
19 * contains multiple Flex-generated lexers, so we alter this prefix to avoid
20 * symbol clashes.
21 */
22%option prefix="aag"
23
24 /* Avoid generating an unused input function. See
25 https://westes.github.io/flex/manual/Scanner-Options.html
26 */
27%option noinput
28
29%{
30#include <assert.h>
31#include <grammar.h>
32#include <cgraph/cghdr.h>
33#include <cgraph/agxbuf.h>
34#include <cgraph/gv_ctype.h>
35#include <cgraph/startswith.h>
36#include <stdbool.h>
37#include <stddef.h>
38#include <string.h>
39// #define YY_BUF_SIZE 128000
40#define GRAPH_EOF_TOKEN '@' /* lex class must be defined below */
41 /* this is a workaround for linux flex */
42static int line_num = 1;
43static int html_nest = 0; /* nesting level for html strings */
44static const char* InputFile;
45static Agdisc_t *Disc;
46static void *Ifile;
47static int graphType;
48
49 /* Reset line number */
50void agreadline(int n) { line_num = n; }
51
52 /* (Re)set file:
53 */
54void agsetfile(const char* f) { InputFile = f; line_num = 1; }
55
56/* There is a hole here, because switching channels
57 * requires pushing back whatever was previously read.
58 * There probably is a right way of doing this.
59 */
60void aglexinit(Agdisc_t *disc, void *ifile) { Disc = disc; Ifile = ifile; graphType = 0;}
61
62/* By default, Flex calls isatty() to determine whether the input it is
63 * scanning is coming from the user typing or from a file. However, our input
64 * is being provided by Graphviz' I/O channel mechanism, which does not have a
65 * valid file descriptor that supports isatty().
66 */
67#define isatty(x) gv_isatty_suppression
69
70#ifndef YY_INPUT
71#define YY_INPUT(buf,result,max_size) \
72 if ((result = Disc->io->afread(Ifile, buf, max_size)) < 0) \
73 YY_FATAL_ERROR( "input in flex scanner failed" )
74#endif
75
76/* buffer for arbitrary length strings (longer than BUFSIZ) */
77static agxbuf Sbuf;
78
79static void beginstr(void) {
80 // nothing required, but we should not have pending string data
81 assert(agxblen(&Sbuf) == 0 &&
82 "pending string data that was not consumed (missing "
83 "endstr()/endhtmlstr()?)");
84}
85
86static void addstr(char *src) {
87 agxbput(&Sbuf, src);
88}
89
90static void endstr(void) {
92}
93
94static void endstr_html(void) {
96}
97
98static void storeFileName(char* fname, size_t len) {
99 static size_t cnt;
100 static char* buf;
101
102 if (len > cnt) {
103 buf = gv_realloc(buf, cnt + 1, len + 1);
104 cnt = len;
105 }
106 strcpy (buf, fname);
107 InputFile = buf;
108}
109
110/* ppDirective:
111 * Process a possible preprocessor line directive.
112 * aagtext = #.*
113 */
114static void ppDirective (void)
115{
116 int r, cnt, lineno;
117 char buf[2];
118 char* s = aagtext + 1; /* skip initial # */
119
120 if (startswith(s, "line")) s += strlen("line");
121 r = sscanf(s, "%d %1[\"]%n", &lineno, buf, &cnt);
122 if (r > 0) { /* got line number */
123 // ignore if line number was out of range
124 if (lineno <= 0) {
125 return;
126 }
127 line_num = lineno - 1;
128 if (r > 1) { /* saw quote */
129 char* p = s + cnt;
130 char* e = p;
131 while (*e && *e != '"') e++;
132 if (e != p && *e == '"') {
133 *e = '\0';
134 storeFileName(p, (size_t)(e - p));
135 }
136 }
137 }
138}
139
140/* twoDots:
141 * Return true if token has more than one '.';
142 * we know the last character is a '.'.
143 */
144static bool twoDots(void) {
145 const char *dot = strchr(aagtext, '.');
146 // was there a dot and was it not the last character?
147 return dot != NULL && dot != &aagtext[aagleng - 1];
148}
149
150/* chkNum:
151 * The regexp for NUMBER allows a terminating letter or '.'.
152 * This way we can catch a number immediately followed by a name
153 * or something like 123.456.78, and report this to the user.
154 */
155static int chkNum(void) {
156 char c = aagtext[aagleng - 1]; // last character
157 if ((!gv_isdigit(c) && c != '.') || (c == '.' && twoDots())) { // c is letter
158 const char* fname;
159
160 if (InputFile)
162 else
163 fname = "input";
164
165 agwarningf("syntax ambiguity - badly delimited number '%s' in line %d of "
166 "%s splits into two tokens\n", aagtext, line_num, fname);
167
168 return 1;
169 }
170 else return 0;
171}
172
173/* The LETTER class below consists of ascii letters, underscore, all non-ascii
174 * characters. This allows identifiers to have characters from any
175 * character set independent of locale. The downside is that, for certain
176 * character sets, non-letter and, in fact, undefined characters will be
177 * accepted. This is not likely and, from dot's stand, shouldn't do any
178 * harm. (Presumably undefined characters will be ignored in display.) And,
179 * it allows a greater wealth of names. */
static size_t agxbput(agxbuf *xb, const char *s)
append string s into xb
Definition agxbuf.h:249
static size_t agxblen(const agxbuf *xb)
return number of characters currently stored
Definition agxbuf.h:88
static char * agxbuse(agxbuf *xb)
Definition agxbuf.h:286
static void * gv_realloc(void *ptr, size_t old_size, size_t new_size)
Definition alloc.h:49
cgraph.h additions
Agraph_t * Ag_G_global
Definition graph.c:24
static char * fname
#define dot(v, w)
Definition geom.c:228
static double len(glCompPoint p)
Definition glutils.c:150
AAGSTYPE aaglval
node NULL
Definition grammar.y:149
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:199
void agwarningf(const char *fmt,...)
Definition agerror.c:173
void agsetfile(const char *f)
sets the current file name for subsequent error reporting
Definition scan.l:54
void agreadline(int n)
sets input line number for subsequent error reporting
Definition scan.l:50
char * agstrdup(Agraph_t *, const char *)
returns a pointer to a reference-counted copy of the argument string, creating one if necessary
Definition refstr.c:130
char * agstrdup_html(Agraph_t *, const char *)
Definition refstr.c:134
replacements for ctype.h functions
static bool gv_isdigit(int c)
Definition gv_ctype.h:41
static int lineno
Definition parse.c:29
static int chkNum(void)
Definition scan.l:155
static const char * InputFile
Definition scan.l:44
static Agdisc_t * Disc
Definition scan.l:45
static void endstr(void)
Definition scan.l:90
void aglexinit(Agdisc_t *disc, void *ifile)
Definition scan.l:60
static void storeFileName(char *fname, size_t len)
Definition scan.l:98
static void * Ifile
Definition scan.l:46
static bool twoDots(void)
Definition scan.l:144
static int line_num
Definition scan.l:42
static void ppDirective(void)
Definition scan.l:114
static void endstr_html(void)
Definition scan.l:94
static void addstr(char *src)
Definition scan.l:86
static int html_nest
Definition scan.l:43
int gv_isatty_suppression
Definition scan.l:68
static agxbuf Sbuf
Definition scan.l:77
static void beginstr(void)
Definition scan.l:79
static int graphType
Definition scan.l:47
static bool startswith(const char *s, const char *prefix)
does the string s begin with the string prefix?
Definition startswith.h:11
user's discipline
Definition cgraph.h:337
char * str
Definition grammar.c:236
Definition grammar.c:93
180%}
181GRAPH_EOF_TOKEN [@]
182LETTER [A-Za-z_\200-\377]
183DIGIT [0-9]
184NAME {LETTER}({LETTER}|{DIGIT})*
185NUMBER [-]?(({DIGIT}+(\.{DIGIT}*)?)|(\.{DIGIT}+))(\.|{LETTER})?
186ID ({NAME}|{NUMBER})
187%x comment
188%x qstring
189%x hstring
191{GRAPH_EOF_TOKEN} return(EOF);
192<INITIAL,comment>\n line_num++;
193"/*" BEGIN(comment);
#define BEGIN
Definition gmlscan.c:377
#define comment
Definition scan.c:970
194<comment>[^*\n]* /* eat anything not a '*' */
195<comment>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
196<comment>"*"+"/" BEGIN(INITIAL);
#define INITIAL
Definition gmlscan.c:897
197"//".* /* ignore C++-style comments */
198^"#".* ppDirective ();
199"#".* /* ignore shell-like comments */
200[ \t\r] /* ignore whitespace */
201"\xEF\xBB\xBF" /* ignore BOM */
202"node" return(T_node); /* see tokens in agcanonstr */
#define T_node
Definition grammar.c:218
203"edge" return(T_edge);
#define T_edge
Definition grammar.c:219
204"graph" if (!graphType) graphType = T_graph; return(T_graph);
#define T_graph
Definition grammar.c:217
205"digraph" if (!graphType) graphType = T_digraph; return(T_digraph);
#define T_digraph
Definition grammar.c:220
206"strict" return(T_strict);
#define T_strict
Definition grammar.c:222
207"subgraph" return(T_subgraph);
#define T_subgraph
Definition grammar.c:221
208"->" if (graphType == T_digraph) return(T_edgeop); else return('-');
#define T_edgeop
Definition grammar.c:223
209"--" if (graphType == T_graph) return(T_edgeop); else return('-');
210{NAME} { aaglval.str = agstrdup(Ag_G_global,aagtext); return(T_atom); }
#define T_atom
Definition grammar.c:226
211{NUMBER} { if (chkNum()) yyless(aagleng-1); aaglval.str = agstrdup(Ag_G_global,aagtext); return(T_atom); }
#define yyless(n)
Definition gmlscan.c:429
212["] BEGIN(qstring); beginstr();
#define qstring
Definition gmlscan.c:898
213<qstring>["] BEGIN(INITIAL); endstr(); return (T_qatom);
#define T_qatom
Definition grammar.c:227
214<qstring>[\\]["] addstr ("\"");
215<qstring>[\\][\\] addstr ("\\\\");
216<qstring>[\\][\n] line_num++; /* ignore escaped newlines */
217<qstring>[\n] addstr ("\n"); line_num++;
218<qstring>([^"\\\n]*|[\\]) addstr(aagtext);
219[<] BEGIN(hstring); html_nest = 1; beginstr();
#define hstring
Definition scan.c:972
220<hstring>[>] html_nest--; if (html_nest) addstr(aagtext); else {BEGIN(INITIAL); endstr_html(); return (T_qatom);}
221<hstring>[<] html_nest++; addstr(aagtext);
222<hstring>[\n] addstr(aagtext); line_num++; /* add newlines */
223<hstring>([^><\n]*) addstr(aagtext);
224. return aagtext[0];
225%%
226
227void aagerror(const char *str);
228void aagerror(const char *str)
229{
230 agxbuf xb = {0};
231 if (InputFile) {
232 agxbprint (&xb, "%s: ", InputFile);
233 }
234 agxbprint (&xb, "%s in line %d", str, line_num);
235 if (*aagtext) {
236 agxbprint(&xb, " near '%s'", aagtext);
237 }
238 else switch (YYSTATE) {
239 case qstring: {
240 agxbprint(&xb, " scanning a quoted string (missing endquote? longer than %d?)", YY_BUF_SIZE);
241 if (agxblen(&Sbuf) > 0) {
242 agxbprint(&xb, "\nString starting:\"%.80s", agxbuse(&Sbuf));
243 }
244 break;
245 }
246 case hstring: {
247 agxbprint(&xb, " scanning a HTML string (missing '>'? bad nesting? longer than %d?)", YY_BUF_SIZE);
248 if (agxblen(&Sbuf) > 0) {
249 agxbprint(&xb, "\nString starting:<%.80s", agxbuse(&Sbuf));
250 }
251 break;
252 }
253 case comment :
254 agxbprint(&xb, " scanning a /*...*/ comment (missing '*/? longer than %d?)", YY_BUF_SIZE);
255 break;
256 default: // nothing extra to note
257 break;
258 }
259 agxbputc (&xb, '\n');
260 agerrorf("%s", agxbuse(&xb));
261 agxbfree(&xb);
262 BEGIN(INITIAL);
263}
264/* must be here to see flex's macro defns */
266
268
269#ifndef YY_CALL_ONLY_ARG
270# define YY_CALL_ONLY_ARG void
271#endif
272
274{
275 return 1;
276}
277
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:77
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:213
static int agxbputc(agxbuf *xb, char c)
add character to buffer
Definition agxbuf.h:256
#define YYSTATE
Definition gmlscan.c:383
#define unput(c)
Definition gmlscan.c:441
#define YY_FLUSH_BUFFER
Definition gmlscan.c:553
#define YY_BUF_SIZE
Definition gmlscan.c:399
void agerrorf(const char *fmt,...)
Definition agerror.c:165
agxbuf * str
Definition htmlparse.c:97
void aglexbad(void)
Definition scan.l:267
#define GRAPH_EOF_TOKEN
Definition scan.l:40
int aagwrap(YY_CALL_ONLY_ARG)
Definition scan.l:273
#define YY_CALL_ONLY_ARG
Definition scan.l:270
void aglexeof(void)
Definition scan.l:265
void aagerror(const char *str)
Definition scan.l:228