aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-07-09 23:04:47 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-07-09 23:04:54 +0200
commit2b0228cec24e49369634cb8239a1939f0fad7e2e (patch)
tree18d6f926ae47da1485e651896bfc0a3c06d7a4c0 /src
parent2b06086473b466fe989ce8629f941b9539ba1095 (diff)
Disallow keywords as identifier names
Diffstat (limited to 'src')
-rw-r--r--src/keywords.gperf40
-rw-r--r--src/parser.c6
2 files changed, 46 insertions, 0 deletions
diff --git a/src/keywords.gperf b/src/keywords.gperf
new file mode 100644
index 0000000..7d81705
--- /dev/null
+++ b/src/keywords.gperf
@@ -0,0 +1,40 @@
+%compare-lengths
+%compare-strncmp
+%includes
+%readonly-tables
+
+%{
+#include <stdbool.h>
+
+#include "strview.h"
+
+static const char *in_word_set(const char *, size_t);
+%}
+
+%%
+pub
+static
+bool
+i8
+i16
+i32
+i64
+i128
+int
+u8
+u16
+u32
+u64
+u128
+uint
+f16
+f32
+f64
+f128
+rune
+%%
+bool
+iskeyword(strview_t sv)
+{
+ return in_word_set(sv.p, sv.len) != NULL;
+}
diff --git a/src/parser.c b/src/parser.c
index defe47d..9e748b5 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -47,6 +47,9 @@ static idx_t astalloc(ast_t *ast)
static void astresz(ast_t *ast)
__attribute__((nonnull));
+/* Generated by Gperf */
+bool iskeyword(strview_t);
+
/* TODO: Make thread-local? */
static size_t toksidx;
@@ -177,6 +180,9 @@ parsedecl(ast_t *ast, aux_t *aux, lexemes_t toks, bool toplvl)
ast->lexemes[i] = toksidx;
}
+ if (iskeyword(toks.strs[toksidx]))
+ err("parser: Cannot use reserved word as identifier");
+
if (toks.kinds[toksidx++] != LEXIDENT)
err("parser: Expected identifier");
if (toks.kinds[toksidx++] != LEXCOLON)