]> ocean-lang.org Git - ocean/blobdiff - csrc/indent_test.mdc
parsergen: allow $$OUT as well as $$NEWLINE
[ocean] / csrc / indent_test.mdc
index 540ecd22584868f832ac6b505b48b8088464ed0d..88e93914aa6f88a7b49638e230c3d99e372019eb 100644 (file)
@@ -16,8 +16,16 @@ with complete bracketing and indenting.
                ./md2c indent_test.mdc
        itest: itest.c
 
-       demos:: itest itest.code
+       doitest: itest itest.code
                ./itest itest.code
+       checkitest: itest itest.code
+               @grep -v '^#' itest.out > .tmp.out
+               @./itest itest.code | diff -u - .tmp.out || echo itest FAILED
+               @sed -e 'i\
+               ' itest.code > itest2.code
+               @./itest itest2.code| diff -u - .tmp.out || echo itest2 FAILED
+       demos :: doitest
+       tests :: checkitest
 
 # indent: header
 
@@ -129,11 +137,9 @@ with complete bracketing and indenting.
 
 Program -> Statementlist ${ print_statement($1, 0); }$
 
-OptNL -> NEWLINE
-       |
-
 $*statement
        Statementlist ->  Statements ${ $0 = $<1; }$
+               | NEWLINE Statementlist ${ $0 = $<2; }$
 
        Statements -> Statements Statement ${
                                {
@@ -149,12 +155,14 @@ $*statement
                        | ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$
 
        Open -> {
-               | NEWLINE {
+               | NEWLINE Open
        Close -> }
-               | NEWLINE }
+               | NEWLINE Close
        Block -> Open Statementlist Close ${ $0 = $<2; }$
                | Open SimpleStatements } ${ $0 = $<2; }$
-               | : Statementlist ${ $0 = $<2; }$
+               | : SimpleStatements $$NEWLINE ${ $0 = $<2; }$
+               | : StatementBlock ${ $0 = $<2; }$
+       StatementBlock -> Statementlist $$OUT ${ $0 = $<1; }$
 
        SimpleStatements -> SimpleStatements ; SimpleStatement ${
                        {
@@ -176,40 +184,33 @@ $*statement
                        $0->expr->op = $2.txt;
                        $0->expr->right = $<3;
                        }$
-       Statement -> SimpleStatements NEWLINE ${
-                       $0 = $<1;
-                       }$
+       SSline -> SimpleStatements NEWLINE ${ $0 = $<1; }$
+               | SSline NEWLINE ${ $0 = $<1; }$
+       Statement -> SSline ${ $0 = $<1; }$
                | IfStatement ${ $0 = $<1; }$
-               | Statement NEWLINE ${ $0 = $<1; }$
 
-       IfStatement -> if Expression Block OptNL ${
-                               $0 = calloc(1, sizeof(struct statement));
-                               $0->expr = $<2;
-                               $0->thenpart = $<3;
-                               }$
-               | if Expression : SimpleStatements ${
-                               $0 = calloc(1, sizeof(struct statement));
-                               $0->expr = $<2;
-                               $0->thenpart = $<4;
-                               }$
-               | if Expression Block OptNL else Block ${
-                               $0 = calloc(1, sizeof(struct statement));
-                               $0->expr = $<2;
-                               $0->thenpart = $<3;
-                               $0->elsepart = $<6;
-                               }$
-               | if Expression Block OptNL else : SimpleStatements ${
-                               $0 = calloc(1, sizeof(struct statement));
-                               $0->expr = $<2;
-                               $0->thenpart = $<3;
-                               $0->elsepart = $<7;
-                               }$
-               | if Expression Block OptNL else IfStatement ${
+       $RIGHT else
+
+       IfHead -> if Expression Block ${
                                $0 = calloc(1, sizeof(struct statement));
                                $0->expr = $<2;
                                $0->thenpart = $<3;
-                               $0->elsepart = $<6;
                                }$
+               | IfHead NEWLINE ${ $0 = $<1; }$
+       IfTail -> else Block ${ $0 = $<2; }$
+               | IfTail NEWLINE ${ $0 = $<1; }$
+
+       IfStatement -> IfHead $$else ${ $0 = $<1; }$
+               | IfHead IfTail ${
+                       $0 = $<1;
+                       $0->elsepart = $<2;
+                       }$
+               | IfHead else IfStatement ${
+                       $0 = $<1;
+                       $0->elsepart = $<3;
+                       }$
+               | IfStatement NEWLINE ${ $0 = $<1; }$
+
 
 $*expression
        Expression -> Expression + Term ${
@@ -283,3 +284,36 @@ $*expression
                        c= d
        x = y
 
+# File: itest.out
+       (hello=yes);
+       (mister=no);
+       (there=x);
+       (all=y);
+       if (cond+cond2):
+           (hello=x);
+           (hello2=x);
+           (sum=(val+val));
+           if condX:
+               (foo=((x*x)+(y/two)));
+       else:
+           if cond2:
+               (there1=x);
+               (there1a=x);
+       (there2=x);
+       (there3=x);
+       (all=y);
+       if true:
+           (yes=x);
+       else:
+           (no=x);
+       if true:
+           (yes=no);
+           (no=yes);
+       if false:
+           (print=OK);
+       else:
+           (print=not_OK);
+       if a:
+           if b:
+               (c=d);
+       (x=y);