hare

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

commit f511ed6de3b5af381756e7cc4d7b2bdbdf3a3dd3
parent 198146b7990b34fc5d1b55d01a84500040427d6d
Author: Drew DeVault <sir@cmpwn.com>
Date:   Mon,  5 Apr 2021 20:03:06 -0400

hare::parse: introduce mustlex utility

Diffstat:
Mhare/parse/expr.ha | 25++++---------------------
Mhare/parse/util.ha | 9+++++++++
2 files changed, 13 insertions(+), 21 deletions(-)

diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha @@ -14,32 +14,19 @@ fn binarithm( expr: ast::expr => expr, }; - let tok = match (lex::lex(lexer)?) { - io::EOF => return syntaxerr(mkloc(lexer), - "Unexpected EOF, was expecting simple expression"), - t: (lex::token, lex::location) => t, - }; - + let tok = mustlex(lexer)?; let j = precedence(tok); for (j >= i; j = precedence(tok)) { const op = binop_for_tok(tok); let rvalue = cast(lexer, void)?; - tok = match (lex::lex(lexer)?) { - io::EOF => return syntaxerr(mkloc(lexer), - "Unexpected EOF, expecting simple expression"), - t: (lex::token, lex::location) => t, - }; + tok = mustlex(lexer)?; let k = precedence(tok); for (k > j; k = precedence(tok)) { lex::unlex(lexer, tok); rvalue = binarithm(lexer, rvalue, k)?; - tok = match (lex::lex(lexer)?) { - io::EOF => return syntaxerr(mkloc(lexer), - "Unexpected EOF, expecting simple expression"), - t: (lex::token, lex::location) => t, - }; + tok = mustlex(lexer)?; }; let expr = ast::binarithm_expr { @@ -177,11 +164,7 @@ fn expression_list(lexer: *lex::lexer) (ast::expr | error) = { let items: ast::list_expr = []; for (let more = true; more) { - let tok = match (lex::lex(lexer)?) { - io::EOF => return syntaxerr(mkloc(lexer), - "Unexpected EOF, expected scope or control expression"), - t: (lex::token, lex::location) => t, - }; + let tok = mustlex(lexer)?; lex::unlex(lexer, tok); let item = match (tok.0) { tok: btoken => switch (tok) { diff --git a/hare/parse/util.ha b/hare/parse/util.ha @@ -93,6 +93,15 @@ fn try_btoken( lex::unlex(lexer, tuple); }; +// Identical to [lex::lex], but considers io::EOF a syntax error. +fn mustlex(lexer: *lex::lexer) ((lex::token, lex::location) | error) = { + return match (lex::lex(lexer)) { + err: lex::error => err, + io::EOF => syntaxerr(mkloc(lexer), "Unexpected EOF"), + t: (lex::token, lex::location) => t, + }; +}; + // Returns a syntax error if cond is false and void otherwise fn synassert(loc: lex::location, cond: bool, msg: str) (void | error) = { if (!cond) {