1 This parser is primarily for testing indent and linebreak handling.
2 It reads a very simple code fragment with if/else statements and
3 simple assignments with expressions, and then prints out the same
4 with complete bracketing and indenting.
7 itestCFLAGS := -Wall -g -fplan9-extensions
8 itestLDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc
11 itest.c itest.h : indent_test.mdc parsergen
12 ./parsergen -o itest --LALR --tag indent indent_test.mdc
13 indent_test.mk itest.code: indent_test.mdc md2c
14 ./md2c indent_test.mdc
15 itest: itest.c | $(filter %.o,$(itestLDLIBS))
16 $(CC) $(itestCFLAGS) $^ $(itestLDLIBS) -o $@
18 doitest: itest itest.code
20 checkitest: itest itest.code
21 @grep -v '^#' itest.out > .tmp.out
22 @./itest itest.code | diff -u .tmp.out - || echo itest FAILED
24 ' itest.code > itest2.code
25 @./itest itest2.code| diff -u .tmp.out - || echo itest2 FAILED
33 struct expression *left, *right;
36 struct statement *next;
37 struct expression *expr;
38 struct statement *thenpart;
39 struct statement *elsepart;
53 static void free_expression(struct expression *e)
57 free_expression(e->left);
58 free_expression(e->right);
62 static void free_statement(struct statement *s)
66 free_statement(s->next);
67 free_expression(s->expr);
68 free_statement(s->thenpart);
69 free_statement(s->elsepart);
73 static void print_expression(struct expression *e)
75 if (e->left && e->right) printf("(");
77 print_expression(e->left);
79 printf("%.*s", e->op.len, e->op.txt);
81 print_expression(e->right);
82 if (e->left && e->right) printf(")");
85 static void print_statement(struct statement *s, int depth)
90 printf("%*.s", depth, "");
91 print_expression(s->expr);
94 printf("%*.sif ", depth, "");
95 print_expression(s->expr);
97 print_statement(s->thenpart, depth+4);
99 printf("%*.selse:\n", depth, "");
100 print_statement(s->elsepart, depth+4);
103 print_statement(s->next, depth);
106 int main(int argc, char *argv[])
108 int fd = open(argv[1], O_RDONLY);
109 int len = lseek(fd, 0, 2);
110 char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
111 struct section *s = code_extract(file, file+len, NULL);
112 struct token_config config = {
113 .number_chars = ".,_+-",
117 parse_itest(s->code, &config, argc > 2 ? stderr : NULL);
119 struct section *t = s->next;
131 $TERM if { } : * + - / ; =
133 Program -> Statementlist ${ print_statement($1, 0); }$
138 Statementlist -> Statements ${ $0 = $<S; }$
139 | Newlines Statements ${ $0 = $<S1; }$
141 Statements -> Statements Statement ${
143 struct statement **s;
151 | Statement ${ $0 = $<1; }$
152 | ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$
158 Block -> Open Statementlist Close ${ $0 = $<S; }$
159 | Open SimpleStatements } ${ $0 = $<S; }$
160 | : SimpleStatements ${ $0 = $<SS; }$
161 | : StatementBlock ${ $0 = $<SB; }$
162 StatementBlock -> Statementlist $$OUT ${ $0 = $<Sl; }$
164 SimpleStatements -> SimpleStatements ; SimpleStatement ${
166 struct statement **s;
174 | SimpleStatement ${ $0 = $<1; }$
175 | SimpleStatements ; ${ $0 = $<1; }$
177 SimpleStatement -> Factor = Expression ${
178 $0 = calloc(1, sizeof(struct statement));
179 $0->expr = calloc(1, sizeof(struct expression));
180 $0->expr->left = $<1;
181 $0->expr->op = $2.txt;
182 $0->expr->right = $<3;
184 SSline -> SimpleStatements NEWLINE ${ $0 = $<1; }$
185 Statement -> SSline ${ $0 = $<1; }$
186 | IfStatement $$NEWLINE ${ $0 = $<1; }$
187 | Statement NEWLINE ${ $0 = $<1; }$
191 IfHead -> if Expression Block ${
192 $0 = calloc(1, sizeof(struct statement));
196 | IfHead NEWLINE ${ $0 = $<1; }$
198 IfStatement -> IfHead $$else ${ $0 = $<1; }$
199 | IfHead else Block ${
203 | IfHead else IfStatement ${
209 Expression -> Expression + Term ${
210 $0 = calloc(1, sizeof(struct expression));
215 | Expression - Term ${
216 $0 = calloc(1, sizeof(struct expression));
221 | Term ${ $0 = $<1; }$
222 Term -> Term * Factor ${
223 $0 = calloc(1, sizeof(struct expression));
229 $0 = calloc(1, sizeof(struct expression));
234 | Factor ${ $0 = $<1; }$
235 Factor -> IDENTIFIER ${
236 $0 = calloc(1, sizeof(struct expression));
245 hello = yes; mister = no;
281 if true {yes=x;} else : no=x
282 if true: yes = no; no = yes;
283 if false: yes=ok; else: no=ok
308 (foo=((x*x)+(y/two)));
318 (foo=((x*x)+(y/two)));