commit 684792252543c89d2a50ccb156e09a3a390f9a6d
parent 0c3c90e7b5dbfa37090e6d68d9f54533b8a1d7b0
Author: Drew DeVault <sir@cmpwn.com>
Date: Tue, 23 Feb 2021 15:06:53 -0500
hare::parse: use error propagation
Diffstat:
2 files changed, 9 insertions(+), 25 deletions(-)
diff --git a/hare/parse/parse.ha b/hare/parse/parse.ha
@@ -6,15 +6,9 @@ use slice;
export fn ident(lexer: *lex::lexer) (ast::ident | error) = {
let ident: []str = [];
for (true) {
- let name = match (want_name(lexer)) {
- err: error => return err,
- n: lex::name => n: str,
- };
-
- append(ident, name);
-
- match (try_btoken(lexer, lex::btoken::DOUBLE_COLON)) {
- err: error => return err,
+ let name = want_name(lexer)?;
+ append(ident, name: str);
+ match (try_btoken(lexer, lex::btoken::DOUBLE_COLON)?) {
void => break,
* => void, // Grab the next ident
};
@@ -27,22 +21,15 @@ export fn ident(lexer: *lex::lexer) (ast::ident | error) = {
export fn imports(lexer: *lex::lexer) ([]ast::import | error) = {
let imports: []ast::import = [];
for (true) {
- match (try_btoken(lexer, lex::btoken::USE)) {
- err: error => return err,
+ match (try_btoken(lexer, lex::btoken::USE)?) {
void => break,
* => void,
};
- let name = match (ident(lexer)) {
- err: error => return err,
- ident: ast::ident => ident,
- };
+ let name = ident(lexer)?;
// TODO: Parse alternate import syntaxes
- match (want_btoken(lexer, lex::btoken::SEMICOLON)) {
- err: error => return err,
- lex::btoken => void,
- };
+ want_btoken(lexer, lex::btoken::SEMICOLON)?;
append(imports, name: ast::import_module);
};
diff --git a/hare/parse/util.ha b/hare/parse/util.ha
@@ -3,10 +3,9 @@ use hare::lex;
// Requires the next token to be a name. Returns that name, or an error.
fn want_name(lexer: *lex::lexer) (lex::name | error) = {
- match (lex::lex(lexer)) {
+ match (lex::lex(lexer)?) {
io::EOF => return syntaxerr(mkloc(lexer),
"Expected name, found EOF"),
- err: lex::error => return err,
t: (lex::token, lex::location) => match (t.0) {
n: lex::name => return n,
// TODO: Use fmt+lex::tokstr here:
@@ -21,10 +20,9 @@ fn want_btoken(
lexer: *lex::lexer,
want: lex::btoken...
) (lex::btoken | error) = {
- match (lex::lex(lexer)) {
+ match (lex::lex(lexer)?) {
io::EOF => return syntaxerr(mkloc(lexer),
"Expected name, found EOF"),
- err: lex::error => return err,
t: (lex::token, lex::location) => match (t.0) {
b: lex::btoken => {
for (let i = 0z; i < len(want); i += 1) {
@@ -51,9 +49,8 @@ fn try_btoken(
want: lex::btoken...
) (lex::btoken | error | void) = {
let tok = lex::lex(lexer);
- let tuple = match (tok) {
+ let tuple = match (tok?) {
io::EOF => return,
- err: lex::error => return err,
t: (lex::token, lex::location) => {
match (t.0) {
b: lex::btoken =>