]> git.mdlowis.com Git - proto/obnc.git/commitdiff
Added module and import list rules
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 16 Apr 2021 20:10:25 +0000 (16:10 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 16 Apr 2021 20:10:25 +0000 (16:10 -0400)
cerise/parser.c

index 39343d1079aa2c59edfbef854a7cc1807e919046..9eb79a6a5c4d5a428182f19a8657ff694b02a5bf 100644 (file)
@@ -7,7 +7,8 @@ static int Indent = 0;
 #define parse_enter() \
     (printf("%*c-> %s\n", ++Indent * 2, ' ', __func__))
 #define parse_exit() \
-    (printf("%*c<- %s\n", --Indent * 2, ' ', __func__))
+    Indent--
+//    (printf("%*c<- %s\n", --Indent * 2, ' ', __func__))
 #else
 #define parse_enter()
 #define parse_exit()
@@ -122,7 +123,7 @@ static char* expect_text(Parser* p, TokType type)
 /* Grammar Definition
  *****************************************************************************/
 
-void import_list(Parser* p)
+static void import_list(Parser* p)
 {
     parse_enter();
     expect(p, IMPORT);
@@ -143,6 +144,16 @@ void import_list(Parser* p)
     parse_exit();
 }
 
+static void declaration_seq(Parser* p)
+{
+    (void)p;
+}
+
+static void statement_seq(Parser* p)
+{
+    (void)p;
+}
+
 void module(Parser* p)
 {
     parse_enter();
@@ -154,10 +165,10 @@ void module(Parser* p)
     {
         import_list(p);
     }
-//    declaration_seq(p);
+    declaration_seq(p);
     if (accept(p, BEGIN))
     {
-//        statement_seq(p);
+        statement_seq(p);
     }
     expect(p, END);
     char* ename = expect_text(p, IDENT);
@@ -176,7 +187,7 @@ void module(Parser* p)
 
 Parser Ctx = {0};
 
-void parse_module(char* fname, char* string)
+void parse_init(char* fname, char* string)
 {
     memset(&Ctx, 0, sizeof(Ctx));
     LexFile* file = calloc(sizeof(LexFile), 1u);
@@ -184,6 +195,17 @@ void parse_module(char* fname, char* string)
     file->fbeg = file->fpos = strdup(string);
     file->next = Ctx.file;
     Ctx.file = file;
+}
+
+static void parse_rule(void (*rule)(Parser*), char* string)
+{
+    parse_init("test_input", string);
+    rule(&Ctx);
+}
+
+static void parse_module(char* fname, char* string)
+{
+    parse_init(fname, string);
     module(&Ctx);
 }
 
@@ -195,10 +217,18 @@ TEST_SUITE(Grammar)
             "module Empty; end Empty;");
         parse_module("ModA",
             "module ModA; import ModB; end ModA;");
-        parse_module("ModA",
-            "module ModA; import ModB, ModC; end ModA;");
-        parse_module("ModA",
-            "module ModA; import B = ModB, C = ModC; end ModA;");
+    }
+
+    TEST(Should parse imports)
+    {
+        parse_rule(import_list,
+            "import A;");
+        parse_rule(import_list,
+            "import A = ModA;");
+        parse_rule(import_list,
+            "import A, B;");
+        parse_rule(import_list,
+            "import A, B = ModB, C;");
     }
 }
 #endif