commit 478ca35964a855842d5dfebe3063e8e69d100204
parent 651281084fbeca1400a33388e3a123eaf4fe3b7f
Author: Ember Sawady <ecs@d2evs.net>
Date: Tue, 9 May 2023 23:38:33 +0000
hare::parse::ident: disallow empty identifier
Signed-off-by: Ember Sawady <ecs@d2evs.net>
Diffstat:
2 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/hare/parse/+test/ident_test.ha b/hare/parse/+test/ident_test.ha
@@ -10,6 +10,16 @@ use strings;
@test fn ident() void = {
{
+ const in = ";";
+ let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ let lexer = lex::init(&buf, "<test>");
+ defer lex::finish(&lexer);
+ ident(&lexer) as error: void;
+ let tok = lex::lex(&lexer) as lex::token;
+ assert(tok.0 == lex::ltok::SEMICOLON);
+ };
+
+ {
const in = "foo";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
let lexer = lex::init(&buf, "<test>");
diff --git a/hare/parse/ident.ha b/hare/parse/ident.ha
@@ -11,28 +11,31 @@ use strings;
fn ident_trailing(lexer: *lex::lexer) ((ast::ident, bool) | error) = {
let ident: []str = [];
+ let trailing = false;
let z = 0z;
+ append(ident, want(lexer, ltok::NAME)?.1 as str);
for (true) {
+ match (try(lexer, ltok::DOUBLE_COLON)?) {
+ case void => break;
+ case => void; // Grab the next ident
+ };
+ z += 1;
let name = match (try(lexer, ltok::NAME)?) {
case let t: lex::token =>
yield t.1 as str;
case void =>
- return (ident: ast::ident, true);
+ trailing = true;
+ break;
};
append(ident, name);
z += len(name);
- match (try(lexer, ltok::DOUBLE_COLON)?) {
- case void => break;
- case => void; // Grab the next ident
- };
- z += 1;
};
if (z > ast::IDENT_MAX) {
ast::ident_free(ident: ast::ident);
return syntaxerr(lex::mkloc(lexer),
"Identifier exceeds maximum length");
};
- return (ident: ast::ident, false);
+ return (ident: ast::ident, trailing);
};
// Parses a single identifier, i.e. 'foo::bar::baz'.