hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit b49a98a00cddfa9e1db75d475ccd4b4f464d44df
parent 9cf88dcf24c49899763068c040f31ee17ff2f967
Author: Sebastian <sebastian@sebsite.pw>
Date:   Fri, 17 Jun 2022 17:02:08 -0400

hare::lex: fix number lexing

The following invalid number literals are now forbidden:

0x
0xz
0xu
0xu64
0be+0
0bf64
3.5i64

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mhare/lex/lex.ha | 20+++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/hare/lex/lex.ha b/hare/lex/lex.ha @@ -399,6 +399,7 @@ fn lex_literal(lex: *lexer) (token | error) = { yield r; }; + let started = false; let base = 10u; if (r.0 == '0') { append(chars, utf8::encoderune(r.0)...); @@ -416,6 +417,7 @@ fn lex_literal(lex: *lexer) (token | error) = { case 'x' => base = 16; case => + started = true; unget(lex, r); }; } else unget(lex, r); @@ -443,6 +445,10 @@ fn lex_literal(lex: *lexer) (token | error) = { }; if (!strings::contains(basechrs, r.0)) switch (r.0) { case '.' => + if (!started) { + return syntaxerr(loc, + "Expected integer literal"); + }; if (float || exp is size || suff is size || base != 10 || lex.require_int) { unget(lex, r); @@ -468,6 +474,10 @@ fn lex_literal(lex: *lexer) (token | error) = { append(chars, utf8::encoderune('.')...); }; case 'e', 'E' => + if (!started) { + return syntaxerr(loc, + "Expected integer literal"); + }; if (exp is size || suff is size || base != 10) { unget(lex, r); break; @@ -490,7 +500,11 @@ fn lex_literal(lex: *lexer) (token | error) = { basechrs = "0123456789"; }; case 'i', 'u', 'f', 'z' => - if (suff is size) { + if (!started) { + return syntaxerr(loc, + "Expected integer literal"); + }; + if (suff is size || r.0 != 'f' && float) { unget(lex, r); break; } else { @@ -503,6 +517,10 @@ fn lex_literal(lex: *lexer) (token | error) = { unget(lex, r); break; } else append(chars, utf8::encoderune(r.0)...); + started = true; + }; + if (!started) { + return syntaxerr(loc, "expected integer literal"); }; if (end == 0) end = len(chars); lex.require_int = false;