File: parse.c

package info (click to toggle)
graphviz 2.26.3-5%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 63,032 kB
  • ctags: 25,930
  • sloc: ansic: 212,134; sh: 20,316; cpp: 7,239; makefile: 4,211; yacc: 3,335; xml: 2,450; tcl: 1,900; cs: 1,890; objc: 1,149; perl: 829; lex: 363; awk: 171; python: 41; ruby: 35; php: 26
file content (627 lines) | stat: -rw-r--r-- 12,659 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* $Id: parse.c,v 1.6 2009/11/23 22:05:54 erg Exp $ $Revision: 1.6 $ */
/* vim:set shiftwidth=4 ts=8: */

/**********************************************************
*      This software is part of the graphviz package      *
*                http://www.graphviz.org/                 *
*                                                         *
*            Copyright (c) 1994-2004 AT&T Corp.           *
*                and is licensed under the                *
*            Common Public License, Version 1.0           *
*                      by AT&T Corp.                      *
*                                                         *
*        Information and Software Systems Research        *
*              AT&T Research, Florham Park NJ             *
**********************************************************/


/*
 * Top-level parsing of gpr code into blocks
 *
 */

#include <ast.h>
#include <sfstr.h>
#include <error.h>
#include <parse.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

static int lineno = 1;		/* current line number */
static int col0 = 1;		/* true if char ptr is at column 0 */
static int startLine = 1;	/* set to start line of bracketd content */
static int kwLine = 1;		/* set to line of keyword */

static char *case_str[] = {
    "BEGIN",
    "END",
    "BEG_G",
    "END_G",
    "N",
    "E",
    "EOF",
    "ERROR",
};

/* caseStr:
 * Convert case_t to string.
 */
static char *caseStr(case_t cs)
{
    return case_str[(int) cs];
}

/* eol:
 * Eat characters until eol.
 */
static int eol (Sfio_t * str, Sfio_t * ostr)
{
    int c;
    while ((c = sfgetc(str)) != '\n') {
	if (c < 0)
	    return c;
    }
    lineno++;
    col0 = 1;
    if (ostr)
	sfputc(ostr, c);
    return c;
}

/* readc:
 * return character from input stream
 * while keeping track of line number.
 * Strip out comments, and return space or newline.
 * If a newline is seen in comment and ostr
 * is non-null, add newline to ostr.
 */
static int readc(Sfio_t * str, Sfio_t * ostr)
{
    int c;
    int cc;

    switch (c = sfgetc(str)) {
    case '\n':
	lineno++;
        col0 = 1;
	break;
    case '#':
	if (col0) { /* shell comment */
	    c = eol (str, ostr);
	}
	else col0 = 0;
	break;
    case '/':
	cc = sfgetc(str);
	switch (cc) {
	case '*':		/* in C comment   */
	    while (1) {
		switch (c = sfgetc(str)) {
		case '\n':
		    lineno++;
		    if (ostr)
			sfputc(ostr, c);
		    break;
		case '*':
		    switch (cc = sfgetc(str)) {
		    case -1:
			return cc;
			break;
		    case '\n':
			lineno++;
			if (ostr)
			    sfputc(ostr, cc);
			break;
		    case '*':
			sfungetc(str, cc);
			break;
		    case '/':
			col0 = 0;
			return ' ';
			break;
		    }
		}
	    }
	    break;
	case '/':		/* in C++ comment */
	    c = eol (str, ostr);
	    break;
	default:		/* not a comment  */
	    if (cc >= '\0')
		sfungetc(str, cc);
	    break;
	}
	break;
    default:
        col0 = 0;
	break;
    }
    return c;
}

/* unreadc;
 * push character back onto stream;
 * if newline, reduce lineno.
 */
void unreadc(Sfio_t * str, int c)
{
    sfungetc(str, c);
    if (c == '\n')
	lineno--;
}

/* skipWS:
 */
static int skipWS(Sfio_t * str)
{
    int c;

    while (1) {
	c = readc(str, 0);
	if (!isspace(c)) {
	    return c;
	}
    }
}

/* parseID:
 * Put initial alpha in buffer;
 * add additional alphas, up to buffer size.
 */
static void parseID(Sfio_t * str, int c, char *buf, size_t bsize)
{
    int more = 1;
    char *ptr = buf;
    char *eptr = buf + (bsize - 1);

    *ptr++ = c;
    while (more) {
	c = readc(str, 0);
	if (c < 0)
	    more = 0;
	if (isalpha(c) || (c == '_')) {
	    if (ptr == eptr)
		more = 0;
	    else
		*ptr++ = c;
	} else {
	    more = 0;
	    unreadc(str, c);
	}
    }
    *ptr = '\0';

}

#define BSIZE 8

/* parseKind:
 * Look for keywords: BEGIN, END, BEG_G, END_G, N, E
 * As side-effect, sets kwLine to line of keyword.
 */
static case_t parseKind(Sfio_t * str)
{
    int c;
    char buf[BSIZE];
    case_t cs = Error;

    c = skipWS(str);
    if (c < 0)
	return Eof;
    if (!isalpha(c)) {
	error(ERROR_ERROR,
	      "expected keyword BEGIN/END/N/E...; found '%c', line %d", c,
	      lineno);
	return Error;
    }

    kwLine = lineno;
    parseID(str, c, buf, BSIZE);
    switch (c) {
    case 'B':
	if (strcmp(buf, "BEGIN") == 0)
	    cs = Begin;
	if (strcmp(buf, "BEG_G") == 0)
	    cs = BeginG;
	break;
    case 'E':
	if (buf[1] == '\0')
	    cs = Edge;
	if (strcmp(buf, "END") == 0)
	    cs = End;
	if (strcmp(buf, "END_G") == 0)
	    cs = EndG;
	break;
    case 'N':
	if (buf[1] == '\0')
	    cs = Node;
	break;
    }
    if (cs == Error)
	error(ERROR_ERROR, "unexpected keyword \"%s\", line %d", buf,
	      kwLine);
    return cs;
}

/* endString:
 * eat characters from ins, putting them into outs,
 * up to and including a terminating character ec
 * that is not escaped with a back quote.
 */
static int endString(Sfio_t * ins, Sfio_t * outs, char ec)
{
    int sline = lineno;
    int c;

    while ((c = sfgetc(ins)) != ec) {
	if (c == '\\') {
	    sfputc(outs, c);
	    c = sfgetc(ins);
	}
	if (c < 0) {
	    error(ERROR_ERROR, "unclosed string, start line %d", sline);
	    return c;
	}
	if (c == '\n')
	    lineno++;
	sfputc(outs, (char) c);
    }
    sfputc(outs, c);
    return 0;
}

/* endBracket:
 * eat characters from ins, putting them into outs,
 * up to a terminating character ec.
 * Strings are treated as atomic units: any ec in them
 * is ignored. Since matching bc-ec pairs might nest,
 * the function is called recursively.
 */
static int endBracket(Sfio_t * ins, Sfio_t * outs, char bc, char ec)
{
    int c;

    while (1) {
	c = readc(ins, outs);
	if ((c < 0) || (c == ec))
	    return c;
	else if (c == bc) {
	    sfputc(outs, (char) c);
	    c = endBracket(ins, outs, bc, ec);
	    if (c < 0)
		return c;
	    else
		sfputc(outs, (char) c);
	} else if ((c == '\'') || (c == '"')) {
	    sfputc(outs, (char) c);
	    if (endString(ins, outs, c)) return -1;
	} else
	    sfputc(outs, (char) c);
    }
}

/* parseBracket:
 *  parse paired expression : bc <string> ec
 *  returning <string>
 * As a side-effect, set startLine to beginning of content.
 */
static char *parseBracket(Sfio_t * str, Sfio_t * buf, int bc, int ec)
{
    int c;

    c = skipWS(str);
    if (c < 0)
	return 0;
    if (c != bc) {
	unreadc(str, c);
	return 0;
    }
    startLine = lineno;
    c = endBracket(str, buf, bc, ec);
    if (c < 0) {
	if (!getErrorErrors())
	    error(ERROR_ERROR,
	      "unclosed bracket %c%c expression, start line %d", bc, ec,
	      startLine);
	return 0;
    }
    else
	return strdup(sfstruse(buf));
}

/* parseAction:
 */
static char *parseAction(Sfio_t * str, Sfio_t * buf)
{
    return parseBracket(str, buf, '{', '}');
}

/* parseGuard:
 */
static char *parseGuard(Sfio_t * str, Sfio_t * buf)
{
    return parseBracket(str, buf, '[', ']');
}

/* parseCase:
 * Recognize
 *   BEGIN <optional action>
 *   END <optional action>
 *   BEG_G <optional action>
 *   END_G <optional action>
 *   N <optional guard> <optional action>
 *   E <optional guard> <optional action>
 * where
 *   guard = '[' <expr> ']'
 *   action = '{' <expr> '}'
 */
static case_t
parseCase(Sfio_t * str, char **guard, int *gline, char **action,
	  int *aline)
{
    case_t kind;

    Sfio_t *buf = sfstropen();

    kind = parseKind(str);
    switch (kind) {
    case Begin:
    case BeginG:
    case End:
    case EndG:
	*action = parseAction(str, buf);
	*aline = startLine;
	if (getErrorErrors ())
	    kind = Error;
	break;
    case Edge:
    case Node:
	*guard = parseGuard(str, buf);
	*gline = startLine;
	if (!getErrorErrors ()) {
	    *action = parseAction(str, buf);
	    *aline = startLine;
	}
	if (getErrorErrors ())
	    kind = Error;
	break;
    case Eof:
    case Error:		/* to silence warnings */
	break;
    }

    sfstrclose(buf);
    return kind;
}

/* addBlock:
 * create new block and append to list;
 * return new item as tail
 */
static parse_block *addBlock (parse_block * last, char *stmt, int line,
	int n_nstmts, case_info *nodelist, int n_estmts, case_info *edgelist)
{
    parse_block* item = newof(0, parse_block, 1, 0);

    item->l_beging = line;
    item->begg_stmt = stmt;
    item->n_nstmts = n_nstmts;
    item->n_estmts = n_estmts;
    item->node_stmts = nodelist;
    item->edge_stmts = edgelist;
    if (last)
	last->next = item;

    return item;
}

/* addCase:
 * create new case_info and append to list;
 * return new item as tail
 */
static case_info *addCase(case_info * last, char *guard, int gline,
			  char *action, int line, int *cnt)
{
    case_info *item;

    if (!guard && !action) {
	error(ERROR_WARNING,
	      "Case with neither guard nor action, line %d - ignored",
	      kwLine);
	return last;
    }

    *cnt = (*cnt) + 1;
    item = newof(0, case_info, 1, 0);
    item->guard = guard;
    item->action = action;
    item->next = 0;
    if (guard)
	item->gstart = gline;
    if (action)
	item->astart = line;

    if (last)
	last->next = item;

    return item;
}

/* bindAction:
 *
 */
static void
bindAction(case_t cs, char *action, int aline, char **ap, int *lp)
{
    if (!action)
	error(ERROR_WARNING, "%s with no action, line %d - ignored",
	      caseStr(cs), kwLine);
    else if (*ap)
	error(ERROR_ERROR, "additional %s section, line %d", caseStr(cs),
	      kwLine);
    else {
	*ap = action;
	*lp = aline;
    }
}

/* parseProg:
 * Parses input into gpr sections.
 */
parse_prog *parseProg(char *input, int isFile)
{
    parse_prog *prog;
    Sfio_t *str;
    char *mode;
    char *guard = NULL;
    char *action = NULL;
    int more;
    parse_block *blocklist = 0;
    case_info *edgelist = 0;
    case_info *nodelist = 0;
    parse_block *blockl = 0;
    case_info *edgel = 0;
    case_info *nodel = 0;
    int n_blocks = 0;
    int n_nstmts = 0;
    int n_estmts = 0;
    int line = 0, gline = 0;
    int l_beging = 0;
    char *begg_stmt;

    
    lineno = col0 = startLine = kwLine = 1;
    prog = newof(0, parse_prog, 1, 0);
    if (!prog) {
	error(ERROR_ERROR, "parseProg: out of memory");
	return 0;
    }

    if (isFile) {
	mode = "r";
	prog->source = input;
	
    } else {
	mode = "rs";
	prog->source = 0;	/* command line */
    }

    str = sfopen(0, input, mode);
    if (!str) {
	if (isFile)
	    error(ERROR_ERROR, "could not open %s for reading", input);
	else
	    error(ERROR_ERROR, "parseProg : unable to create sfio stream");
	free (prog);
	return 0;
    }
    
    begg_stmt = 0;
    more = 1;
    while (more) {
	switch (parseCase(str, &guard, &gline, &action, &line)) {
	case Begin:
	    bindAction(Begin, action, line, &(prog->begin_stmt),
		       &(prog->l_begin));
	    break;
	case BeginG:
	    if (action && (begg_stmt || nodelist || edgelist)) { /* non-empty block */
		blockl = addBlock(blockl, begg_stmt, l_beging, 
		    n_nstmts, nodelist, n_estmts, edgelist);
	 	if (!blocklist)
		    blocklist = blockl;
		n_blocks++;

		/* reset values */
		n_nstmts = n_estmts = 0;
		edgel = nodel = edgelist = nodelist = 0;
		begg_stmt = 0;
	    }
	    bindAction(BeginG, action, line, &begg_stmt, &l_beging);
	    break;
	case End:
	    bindAction(End, action, line, &(prog->end_stmt),
		       &(prog->l_end));
	    break;
	case EndG:
	    bindAction(EndG, action, line, &(prog->endg_stmt),
		       &(prog->l_endg));
	    break;
	case Eof:
	    more = 0;
	    break;
	case Node:
	    nodel = addCase(nodel, guard, gline, action, line, &n_nstmts);
	    if (!nodelist)
		nodelist = nodel;
	    break;
	case Edge:
	    edgel = addCase(edgel, guard, gline, action, line, &n_estmts);
	    if (!edgelist)
		edgelist = edgel;
	    break;
	case Error:		/* to silence warnings */
	    more = 0;
	    break;
	}
    }

    if (begg_stmt || nodelist || edgelist) { /* non-empty block */
	blockl = addBlock(blockl, begg_stmt, l_beging, 
	    n_nstmts, nodelist, n_estmts, edgelist);
	if (!blocklist)
	    blocklist = blockl;
	n_blocks++;
    }

    prog->n_blocks = n_blocks;
    prog->blocks = blocklist;

    sfclose(str);

    if (getErrorErrors ()) {
	freeParseProg (prog);
	prog = 0;
    }

    return prog;
}

static void
freeCaseList (case_info* ip)
{
    case_info* nxt;
    while (ip) {
	nxt = ip->next;
	free (ip->guard);
	free (ip->action);
	free (ip);
	ip = nxt;
    }
}

static void
freeBlocks (parse_block* ip)
{
    parse_block* nxt;
    while (ip) {
	nxt = ip->next;
	free (ip->begg_stmt);
	freeCaseList (ip->node_stmts);
	freeCaseList (ip->edge_stmts);
	ip = nxt;
    }
}

void 
freeParseProg (parse_prog * prog)
{
    if (!prog) return;
    free (prog->begin_stmt);
    freeBlocks (prog->blocks);
    free (prog->endg_stmt);
    free (prog->end_stmt);
    free (prog);
}