harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 6cc3ee5c3dc09c4a4b0897e1f2abee120eb9d93d
parent cb636689bcb1147378d48c1a9bc3a318a0012adc
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Sun,  5 Feb 2023 15:51:37 +0100

lex: error out on leading zeros in base 10 literals

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Msrc/lex.c | 6+++++-
Mtests/00-constants.ha | 4++++
2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/lex.c b/src/lex.c @@ -322,7 +322,7 @@ lex_literal(struct lexer *lexer, struct token *out) uint32_t c = next(lexer, &out->loc, true); assert(c != C_EOF && c <= 0x7F && isdigit(c)); - bool started = false; + bool started = false, leadingzero = false; int base = 10; const char *basechrs = "0123456789"; if (c == '0') { @@ -344,6 +344,7 @@ lex_literal(struct lexer *lexer, struct token *out) break; default: started = true; + leadingzero = true; push(lexer, c, true); break; } @@ -426,6 +427,9 @@ finalize: if (!started) { error(&out->loc, "Invalid literal"); } + if (leadingzero && lexer->buflen >= 2 && strchr(basechrs, lexer->buf[1])) { + error(&out->loc, "Leading zero in base 10 literal"); + } lexer->require_int = false; out->token = T_LITERAL; if (isfloat) { diff --git a/tests/00-constants.ha b/tests/00-constants.ha @@ -168,6 +168,10 @@ fn basics() void = { "let x = 0b14;", "let x = 0o82;", "let x = 0o28;", + + // leading zeros + "let x = 05;" + "let x = 0000000010;" ]; for (let i = 0z; i < len(failures); i += 1) { assert(compile(failures[i]) as exited != EXIT_SUCCESS);