]> ocean-lang.org Git - ocean/commitdiff
parsergen: change how reserved_words are stored
authorNeilBrown <neil@brown.name>
Fri, 5 Mar 2021 08:20:22 +0000 (19:20 +1100)
committerNeilBrown <neil@brown.name>
Fri, 5 Mar 2021 08:20:22 +0000 (19:20 +1100)
Rather than a simple array with holes, have a dense array mapping number
to name.  This will enable a future change which adds names that don't
have numbers assigned.

Signed-off-by: NeilBrown <neil@brown.name>
csrc/parsergen.mdc

index fa4e5c77d2614c60939929daeecfc7f0340eb76a..da9335ed0df1794fe49c35030272e333956259a3 100644 (file)
@@ -169,17 +169,18 @@ table of known symbols and the resulting parser will report them as
 different token types that `scanner` can report.
 
 ###### declarations
-       static char *reserved_words[] = {
-               [TK_error]        = "ERROR",
-               [TK_number]       = "NUMBER",
-               [TK_ident]        = "IDENTIFIER",
-               [TK_mark]         = "MARK",
-               [TK_string]       = "STRING",
-               [TK_multi_string] = "MULTI_STRING",
-               [TK_in]           = "IN",
-               [TK_out]          = "OUT",
-               [TK_newline]      = "NEWLINE",
-               [TK_eof]          = "$eof",
+
+       static struct { int num; char *name; } reserved_words[] = {
+               { TK_error,        "ERROR" },
+               { TK_number,       "NUMBER" },
+               { TK_ident,        "IDENTIFIER" },
+               { TK_mark,         "MARK" },
+               { TK_string,       "STRING" },
+               { TK_multi_string, "MULTI_STRING" },
+               { TK_in,           "IN" },
+               { TK_out,          "OUT" },
+               { TK_newline,      "NEWLINE" },
+               { TK_eof,          "$eof" },
        };
 ###### symbol fields
        short num;
@@ -235,13 +236,11 @@ symbol, but its type might be `Unknown`.
                for (i = 0; i < entries; i++) {
                        struct text t;
                        struct symbol *s;
-                       t.txt = reserved_words[i];
-                       if (!t.txt)
-                               continue;
+                       t.txt = reserved_words[i].name;
                        t.len = strlen(t.txt);
                        s = sym_find(g, t);
                        s->type = Terminal;
-                       s->num = i;
+                       s->num = reserved_words[i].num;
                }
        }