]> ocean-lang.org Git - ocean/blob - csrc/indent_test.mdc
parsergen: allow $$OUT to be satisfied are start-of-line.
[ocean] / csrc / indent_test.mdc
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.
5
6 # File: indent_test.mk
7         myCFLAGS := -Wall -g -fplan9-extensions
8         CFLAGS := $(filter-out $(myCFLAGS),$(CFLAGS)) $(myCFLAGS)
9         myLDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc
10         LDLIBS := $(filter-out $(myLDLIBS),$(LDLIBS)) $(myLDLIBS)
11
12         all :: itest
13         itest.c itest.h : indent_test.mdc parsergen libparser.o libscanner.o libmdcode.o
14                 ./parsergen -o itest --LALR --tag indent indent_test.mdc
15         indent_test.mk: indent_test.mdc md2c
16                 ./md2c indent_test.mdc
17         itest: itest.c
18
19         doitest: itest itest.code
20                 ./itest itest.code
21         checkitest: itest itest.code
22                 @grep -v '^#' itest.out > .tmp.out
23                 @./itest itest.code | diff -u - .tmp.out || echo itest FAILED
24                 @sed -e 'i\
25                 ' itest.code > itest2.code
26                 @./itest itest2.code| diff -u - .tmp.out || echo itest2 FAILED
27         demos :: doitest
28         tests :: checkitest
29
30 # indent: header
31
32  ./parsergen -o itest --LALR indent_test.cgm
33  cc -o itest itest.c lib*.o -licuuc -lgmp
34  ./itest itest.code
35
36         struct expression {
37                 struct text op;
38                 struct expression *left, *right;
39         };
40         struct statement {
41                 struct statement *next;
42                 struct expression *expr;
43                 struct statement *thenpart;
44                 struct statement *elsepart;
45         };
46
47 # indent: code
48         #include <unistd.h>
49         #include <stdlib.h>
50         #include <fcntl.h>
51         #include <sys/mman.h>
52         #include <stdio.h>
53         #include "mdcode.h"
54         #include "scanner.h"
55         #include "parser.h"
56         #include "itest.h"
57
58         static void free_expression(struct expression *e)
59         {
60                 if (!e)
61                         return;
62                 free_expression(e->left);
63                 free_expression(e->right);
64                 free(e);
65         }
66
67         static void free_statement(struct statement *s)
68         {
69                 if (!s)
70                         return;
71                 free_statement(s->next);
72                 free_expression(s->expr);
73                 free_statement(s->thenpart);
74                 free_statement(s->elsepart);
75                 free(s);
76         }
77
78         static void print_expression(struct expression *e)
79         {
80                 if (e->left && e->right) printf("(");
81                 if (e->left)
82                         print_expression(e->left);
83                 if (e->op.len)
84                         printf("%.*s", e->op.len, e->op.txt);
85                 if (e->right)
86                         print_expression(e->right);
87                 if (e->left && e->right) printf(")");
88         }
89
90         static void print_statement(struct statement *s, int depth)
91         {
92                 if (!s)
93                         return;
94                 if (!s->thenpart) {
95                         printf("%*.s", depth, "");
96                         print_expression(s->expr);
97                         printf(";\n");
98                 } else {
99                         printf("%*.sif ", depth, "");
100                         print_expression(s->expr);
101                         printf(":\n");
102                         print_statement(s->thenpart, depth+4);
103                         if (s->elsepart) {
104                                 printf("%*.selse:\n", depth, "");
105                                 print_statement(s->elsepart, depth+4);
106                         }
107                 }
108                 print_statement(s->next, depth);
109         }
110
111         int main(int argc, char *argv[])
112         {
113                 int fd = open(argv[1], O_RDONLY);
114                 int len = lseek(fd, 0, 2);
115                 char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
116                 struct section *s = code_extract(file, file+len, NULL);
117                 struct token_config config = {
118                         .ignored = (1 << TK_line_comment)
119                                  | (1 << TK_block_comment),
120                         .number_chars = ".,_+-",
121                         .word_start = "",
122                         .word_cont = "",
123                 };
124                 parse_itest(s->code, &config, argc > 2 ? stderr : NULL);
125                 while (s) {
126                         struct section *t = s->next;
127                         code_free(s->code);
128                         free(s);
129                         s = t;
130                 }
131                 exit(0);
132         }
133
134 # indent: grammar
135
136 ~~~~~~
137
138 Program -> Statementlist ${ print_statement($1, 0); }$
139
140 $*statement
141         Statementlist ->  Statements ${ $0 = $<1; }$
142                 | NEWLINE Statementlist ${ $0 = $<2; }$
143
144         Statements -> Statements Statement ${
145                                 {
146                                         struct statement **s;
147                                         $0 = $<1;
148                                         s = &$0;
149                                         while (*s)
150                                                 s = &(*s)->next;
151                                         *s = $<2;
152                                 }
153                                 }$
154                         | Statement ${ $0 = $<1; }$
155                         | ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$
156
157         Open -> {
158                 | NEWLINE Open
159         Close -> }
160                 | NEWLINE Close
161         Block -> Open Statementlist Close ${ $0 = $<2; }$
162                 | Open SimpleStatements } ${ $0 = $<2; }$
163                 | : SimpleStatements ${ $0 = $<2; }$
164                 | : StatementBlock ${ $0 = $<2; }$
165         StatementBlock -> Statementlist $$OUT ${ $0 = $<1; }$
166
167         SimpleStatements -> SimpleStatements ; SimpleStatement ${
168                         {
169                                 struct statement **s;
170                                 $0 = $<1;
171                                 s = &$0;
172                                 while (*s)
173                                         s = &(*s)->next;
174                                 *s = $<3;
175                         }
176                         }$
177                 | SimpleStatement ${ $0 = $<1; }$
178                 | SimpleStatements ; ${ $0 = $<1; }$
179
180         SimpleStatement -> Factor = Expression ${
181                         $0 = calloc(1, sizeof(struct statement));
182                         $0->expr = calloc(1, sizeof(struct expression));
183                         $0->expr->left = $<1;
184                         $0->expr->op = $2.txt;
185                         $0->expr->right = $<3;
186                         }$
187         SSline -> SimpleStatements NEWLINE ${ $0 = $<1; }$
188         Statement -> SSline ${ $0 = $<1; }$
189                 | IfStatement $$NEWLINE ${ $0 = $<1; }$
190                 | Statement NEWLINE ${ $0 = $<1; }$
191
192         $RIGHT else
193
194         IfHead -> if Expression Block ${
195                                 $0 = calloc(1, sizeof(struct statement));
196                                 $0->expr = $<2;
197                                 $0->thenpart = $<3;
198                                 }$
199                 | IfHead NEWLINE ${ $0 = $<1; }$
200
201         IfStatement -> IfHead $$else ${ $0 = $<1; }$
202                 | IfHead else Block ${
203                         $0 = $<1;
204                         $0->elsepart = $<3;
205                         }$
206                 | IfHead else IfStatement ${
207                         $0 = $<1;
208                         $0->elsepart = $<3;
209                         }$
210
211 $*expression
212         Expression -> Expression + Term ${
213                                 $0 = calloc(1, sizeof(struct expression));
214                                 $0->op = $2.txt;
215                                 $0->left = $<1;
216                                 $0->right = $<3;
217                         }$
218                     | Expression - Term ${
219                                 $0 = calloc(1, sizeof(struct expression));
220                                 $0->op = $2.txt;
221                                 $0->left = $<1;
222                                 $0->right = $<3;
223                         }$
224                     | Term ${ $0 = $<1; }$
225         Term -> Term * Factor ${
226                                 $0 = calloc(1, sizeof(struct expression));
227                                 $0->op = $2.txt;
228                                 $0->left = $<1;
229                                 $0->right = $<3;
230                         }$
231               | Term / Factor ${
232                                 $0 = calloc(1, sizeof(struct expression));
233                                 $0->op = $2.txt;
234                                 $0->left = $<1;
235                                 $0->right = $<3;
236                         }$
237               | Factor ${ $0 = $<1; }$
238         Factor -> IDENTIFIER ${
239                                 $0 = calloc(1, sizeof(struct expression));
240                                 $0->op = $1.txt;
241                         }$
242 ~~~~~~
243
244 # File: itest.code
245
246         # test code
247         ~~~~~~
248         hello = yes; mister = no;
249         there = x;
250         all = y;
251         if cond + cond2 :
252                 hello = x;
253                 hello2 = x;
254
255                 sum = val +
256                  val;
257
258                 if condX:
259                  foo = x *
260                     x +
261                     y
262                     / two;
263          else if cond2:
264                 there1 =x
265                 there1a=x
266         there2=x
267         there3=x;
268         all = y;
269         if true {yes=x;} else : no=x
270         if true: yes = no; no = yes;
271         if false: yes=ok; else: no=ok
272
273         if false {
274                 print = OK
275         } else {
276                 print = not_OK
277         }
278
279         if a:
280                 if b:
281                         c= d
282         x = y
283
284 # File: itest.out
285         (hello=yes);
286         (mister=no);
287         (there=x);
288         (all=y);
289         if (cond+cond2):
290             (hello=x);
291             (hello2=x);
292             (sum=(val+val));
293             if condX:
294                 (foo=((x*x)+(y/two)));
295         else:
296             if cond2:
297                 (there1=x);
298                 (there1a=x);
299         (there2=x);
300         (there3=x);
301         (all=y);
302         if true:
303             (yes=x);
304         else:
305             (no=x);
306         if true:
307             (yes=no);
308             (no=yes);
309         if false:
310             (yes=ok);
311         else:
312             (no=ok);
313         if false:
314             (print=OK);
315         else:
316             (print=not_OK);
317         if a:
318             if b:
319                 (c=d);
320         (x=y);