hare

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

commit c039dd017288e4c9f96faf311c889fd4f9414f16
parent 5e4a38b89f8c59d513ea8005d2ac9cf0413bdfe7
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu, 15 Apr 2021 10:06:51 -0400

hare::parse: merge util.ha => parse.ha

Diffstat:
Mhare/parse/parse.ha | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dhare/parse/util.ha | 72------------------------------------------------------------------------
Mscripts/gen-stdlib | 1-
Mstdlib.mk | 4+---
4 files changed, 71 insertions(+), 76 deletions(-)

diff --git a/hare/parse/parse.ha b/hare/parse/parse.ha @@ -1,5 +1,8 @@ use fmt; +use hare::ast; use hare::lex; +use io; +use strio; // All possible error types export type error = lex::error!; @@ -21,3 +24,70 @@ fn mkloc(lex: *lex::lexer) lex::location = lex::location { line = lex.loc.0, col = lex.loc.1, }; + +// Requires the next token to have a matching ltok. Returns that token, or an +// error. +fn want(lexer: *lex::lexer, want: lex::ltok...) (lex::token | error) = { + let tok = lex::lex(lexer)?; + if (len(want) == 0) { + return tok; + }; + for (let i = 0z; i < len(want); i += 1) { + if (tok.0 == want[i]) { + return tok; + }; + }; + + let buf = strio::dynamic(); + defer io::close(buf); + for (let i = 0z; i < len(want); i += 1) { + fmt::fprintf(buf, "{}", lex::tokstr((want[i], void, mkloc(lexer)))); + if (i + 1 < len(want)) { + fmt::fprint(buf, ", "); + }; + }; + return syntaxerr(mkloc(lexer), "Unexpected {}, was expecting {}", + lex::tokstr(tok), strio::string(buf)); +}; + +// Looks for a matching ltok from the lexer, and if not present, unlexes the +// token and returns void. If found, the token is consumed from the lexer and is +// returned. +fn try( + lexer: *lex::lexer, + want: lex::ltok... +) (lex::token | error | void) = { + let tok = lex::lex(lexer)?; + assert(len(want) > 0); + for (let i = 0z; i < len(want); i += 1) { + if (tok.0 == want[i]) { + return tok; + }; + }; + lex::unlex(lexer, tok); +}; + +// Looks for a matching ltok from the lexer, unlexes the token, and returns +// it; or void if it was not a ltok. +fn peek( + lexer: *lex::lexer, + want: lex::ltok... +) (lex::token | error | void) = { + let tok = lex::lex(lexer)?; + lex::unlex(lexer, tok); + if (len(want) == 0) { + return tok; + }; + for (let i = 0z; i < len(want); i += 1) { + if (tok.0 == want[i]) { + return tok; + }; + }; +}; + +// Returns a syntax error if cond is false and void otherwise +fn synassert(loc: lex::location, cond: bool, msg: str) (void | error) = { + if (!cond) { + return syntaxerr(loc, msg); + }; +}; diff --git a/hare/parse/util.ha b/hare/parse/util.ha @@ -1,72 +0,0 @@ -use fmt; -use hare::ast; -use hare::lex; -use io; -use strio; - -// Requires the next token to have a matching ltok. Returns that token, or an -// error. -fn want(lexer: *lex::lexer, want: lex::ltok...) (lex::token | error) = { - let tok = lex::lex(lexer)?; - if (len(want) == 0) { - return tok; - }; - for (let i = 0z; i < len(want); i += 1) { - if (tok.0 == want[i]) { - return tok; - }; - }; - - let buf = strio::dynamic(); - defer io::close(buf); - for (let i = 0z; i < len(want); i += 1) { - fmt::fprintf(buf, "{}", lex::tokstr((want[i], void, mkloc(lexer)))); - if (i + 1 < len(want)) { - fmt::fprint(buf, ", "); - }; - }; - return syntaxerr(mkloc(lexer), "Unexpected {}, was expecting {}", - lex::tokstr(tok), strio::string(buf)); -}; - -// Looks for a matching ltok from the lexer, and if not present, unlexes the -// token and returns void. If found, the token is consumed from the lexer and is -// returned. -fn try( - lexer: *lex::lexer, - want: lex::ltok... -) (lex::token | error | void) = { - let tok = lex::lex(lexer)?; - assert(len(want) > 0); - for (let i = 0z; i < len(want); i += 1) { - if (tok.0 == want[i]) { - return tok; - }; - }; - lex::unlex(lexer, tok); -}; - -// Looks for a matching ltok from the lexer, unlexes the token, and returns -// it; or void if it was not a ltok. -fn peek( - lexer: *lex::lexer, - want: lex::ltok... -) (lex::token | error | void) = { - let tok = lex::lex(lexer)?; - lex::unlex(lexer, tok); - if (len(want) == 0) { - return tok; - }; - for (let i = 0z; i < len(want); i += 1) { - if (tok.0 == want[i]) { - return tok; - }; - }; -}; - -// Returns a syntax error if cond is false and void otherwise -fn synassert(loc: lex::location, cond: bool, msg: str) (void | error) = { - if (!cond) { - return syntaxerr(loc, msg); - }; -}; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -379,7 +379,6 @@ gensrcs_hare_parse() { parse.ha \ type.ha \ unit.ha \ - util.ha \ $* } diff --git a/stdlib.mk b/stdlib.mk @@ -520,8 +520,7 @@ stdlib_hare_parse_srcs= \ $(STDLIB)/hare/parse/import.ha \ $(STDLIB)/hare/parse/parse.ha \ $(STDLIB)/hare/parse/type.ha \ - $(STDLIB)/hare/parse/unit.ha \ - $(STDLIB)/hare/parse/util.ha + $(STDLIB)/hare/parse/unit.ha $(HARECACHE)/hare/parse/hare_parse.ssa: $(stdlib_hare_parse_srcs) $(stdlib_rt) $(stdlib_hare_ast) $(stdlib_hare_lex) $(stdlib_hare_unparse) $(stdlib_fmt) @printf 'HAREC \t$@\n' @@ -1384,7 +1383,6 @@ testlib_hare_parse_srcs= \ $(STDLIB)/hare/parse/parse.ha \ $(STDLIB)/hare/parse/type.ha \ $(STDLIB)/hare/parse/unit.ha \ - $(STDLIB)/hare/parse/util.ha \ $(STDLIB)/hare/parse/+test/expr.ha \ $(STDLIB)/hare/parse/+test/ident.ha \ $(STDLIB)/hare/parse/+test/roundtrip.ha \