From 1f02aa8f075c052e845d55a38a8c7595c3eff7a7 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Fri, 5 Mar 2021 19:20:22 +1100 Subject: [PATCH] parsergen: change how reserved_words are stored 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 --- csrc/parsergen.mdc | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/csrc/parsergen.mdc b/csrc/parsergen.mdc index fa4e5c7..da9335e 100644 --- a/csrc/parsergen.mdc +++ b/csrc/parsergen.mdc @@ -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; } } -- 2.43.0