changeset 47:ff71c61a3cf9

gen/parse: Refine parser for top level structures
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 22 Aug 2019 10:50:53 +0200
parents e753d447a3e6
children 4e4e38310ab1
files gen/y.yy src/value.h
diffstat 2 files changed, 37 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/gen/y.yy	Thu Aug 22 10:50:15 2019 +0200
+++ b/gen/y.yy	Thu Aug 22 10:50:53 2019 +0200
@@ -35,6 +35,8 @@
 yvec_t exprs;
 // One yexpr.
 yexpr_t expr;
+// A reference.
+yref_t ref;
 // A number literal (todo: floats)
 long long number;
 // A string literal.
@@ -46,12 +48,14 @@
 %token <number> TOK_NUMBER_LITERAL
 %token <string> TOK_STRING_LITERAL
 %token <atom> TOK_ATOM
+%token <ref> TOK_ID
 %token TOK_LEFTP
 %token TOK_RIGHTP
 
 %type <exprs> many_sexpr
 %type <expr> sexpr
 %type <expr> inner_sexpr
+%type <expr> value
 
 %%
 
@@ -66,6 +70,7 @@
             // Push found sexpr to previous sexprs.
             YVEC_PUSH(&$1, &$2);
             yexpr_debug(&$2);
+            fputs("\n", stderr);
             // Push result on stack.
             $$ = $1;
     }
@@ -78,29 +83,10 @@
 sexpr: TOK_LEFTP inner_sexpr TOK_RIGHTP {
                 printf("found sexpr\n");
                 $$ = $2;
-};
+            };
 
-inner_sexpr: inner_sexpr TOK_ATOM {
-                printf("found atom %s\n", $2);
-                yexpr_t atom;
-                yexpr_set_atom_name(&atom, strdup($2));
-                yexpr_init_or_extend(&$1, atom);
-                $$ = $1;
-           }
-           | inner_sexpr TOK_NUMBER_LITERAL {
-                printf("found number %d\n", $2);
-                yexpr_t num;
-                yexpr_set_number(&num, $2);
-                yexpr_init_or_extend(&$1, num);
-                $$ = $1;
-           }
-           | inner_sexpr TOK_STRING_LITERAL {
-                printf("found string %s\n", $2);
-                yexpr_t strexpr;
-                ystr_t str;
-                ystr_init(&str, $2);
-                yexpr_set_str(&strexpr, str);
-                yexpr_init_or_extend(&$1, strexpr);
+inner_sexpr: inner_sexpr value {
+                yexpr_init_or_extend(&$1, $2);
                 $$ = $1;
            }
            | inner_sexpr sexpr {
@@ -114,6 +100,35 @@
                 yexpr_init(&e);
                 $$ = e;
            };
+
+value: TOK_ATOM {
+        printf("found atom %s\n", $1);
+        yexpr_t atom;
+        yexpr_set_atom_name(&atom, strdup($1));
+        $$ = atom;
+     }
+     | TOK_ID {
+        ystr_t debug = yref_debug(&$1);
+        printf("found ID %s\n", ystr_str(&debug));
+        ystr_destroy(&debug);
+
+        yexpr_t ref;
+        yexpr_set_ref(&ref, $1);
+        $$ = ref;
+     }
+     | TOK_NUMBER_LITERAL {
+        printf("found number %d\n", $1);
+        yexpr_t num;
+        yexpr_set_number(&num, $1);
+        $$ = num;
+     }
+     | TOK_STRING_LITERAL {
+        printf("found string %s\n", $1);
+        yexpr_t strexpr;
+        ystr_t str = ystr_new($1);
+        yexpr_set_str(&strexpr, str);
+        $$ = strexpr;
+     }
 %%
 
 int parse(FILE* in, yvec_t* out_exprs) {
--- a/src/value.h	Thu Aug 22 10:50:15 2019 +0200
+++ b/src/value.h	Thu Aug 22 10:50:53 2019 +0200
@@ -48,7 +48,6 @@
     enum YVALUE_TYPE typ;
 } yvalue_t;
 
-
 /**
  * @brief Free all memory used by a value.
  */