From 2b0228cec24e49369634cb8239a1939f0fad7e2e Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 9 Jul 2024 23:04:47 +0200 Subject: Disallow keywords as identifier names --- src/keywords.gperf | 40 ++++++++++++++++++++++++++++++++++++++++ src/parser.c | 6 ++++++ 2 files changed, 46 insertions(+) create mode 100644 src/keywords.gperf (limited to 'src') 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 + +#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) -- cgit v1.2.3