hare

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

commit a369d1faf2d9115ecfea9d8c87c0555c8c529e33
parent f60598635a47af86daabdf828570a2c46b32b5bb
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Fri, 28 May 2021 11:45:04 -0400

parse::mkloc: drop in favor of lex::mkloc

Signed-off-by: Eyal Sawady <ecs@d2evs.net>

Diffstat:
Mhare/lex/lex.ha | 2+-
Mhare/parse/decl.ha | 6+++---
Mhare/parse/expr.ha | 86++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mhare/parse/ident.ha | 4++--
Mhare/parse/import.ha | 6+++---
Mhare/parse/parse.ha | 10++--------
Mhare/parse/type.ha | 24++++++++++++------------
7 files changed, 66 insertions(+), 72 deletions(-)

diff --git a/hare/lex/lex.ha b/hare/lex/lex.ha @@ -804,7 +804,7 @@ fn unget(lex: *lexer, r: (rune | io::EOF)) void = { lex.rb[0] = r; }; -fn mkloc(lex: *lexer) location = location { +export fn mkloc(lex: *lexer) location = location { path = lex.path, line = lex.loc.0, col = lex.loc.1, diff --git a/hare/parse/decl.ha b/hare/parse/decl.ha @@ -123,9 +123,9 @@ fn decl_func(lexer: *lex::lexer) (ast::decl_func | error) = { }; want(lexer, ltok::FN)?; - let ident_loc = mkloc(lexer); + let ident_loc = lex::mkloc(lexer); let ident = ident(lexer)?; - let proto_loc = mkloc(lexer); + let proto_loc = lex::mkloc(lexer); let prototype = prototype(lexer)?; if (noreturn) { prototype.attrs |= ast::func_attrs::NORETURN; @@ -190,7 +190,7 @@ export fn decls(lexer: *lex::lexer) ([]ast::decl | error) = { }; append(decls, ast::decl { exported = exported, - loc = mkloc(lexer), + loc = lex::mkloc(lexer), decl = decl, docs = comment, }); diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha @@ -5,7 +5,7 @@ use strings; // Parses an expression. export fn expression(lexer: *lex::lexer) (ast::expr | error) = { - const loc = mkloc(lexer); + const loc = lex::mkloc(lexer); const indirect = match (try(lexer, ltok::TIMES)?) { void => false, lex::token => true, @@ -29,7 +29,7 @@ export fn expression(lexer: *lex::lexer) (ast::expr | error) = { expr; } else return binarithm(lexer, ast::expr { start = loc, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::unarithm_expr { op = ast::unarithm_op::DEREF, operand = alloc(expr), @@ -59,7 +59,7 @@ export fn expression(lexer: *lex::lexer) (ast::expr | error) = { * => return expr, }; - synassert(mkloc(lexer), + synassert(lex::mkloc(lexer), expr.expr is ast::access_expr || expr.expr is ast::slice_expr || indirect, "Expected an object-selector or slice for assignment target")?; const expr = ast::assign_expr { @@ -86,7 +86,7 @@ export fn expression(lexer: *lex::lexer) (ast::expr | error) = { return ast::expr { start = loc, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = expr, }; }; @@ -130,7 +130,7 @@ fn assert_expr(lexer: *lex::lexer, is_static: bool) (ast::expr | error) = { return ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = expr, }; }; @@ -149,7 +149,7 @@ fn alloc_expr(lexer: *lex::lexer) (ast::expr | error) = { return ast::expr { start = start.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::alloc_expr { init = alloc(init), capacity = cap, @@ -188,12 +188,12 @@ fn append_expr(lexer: *lex::lexer) (ast::expr | error) = { }; }; - synassert(mkloc(lexer), variadic != null || len(values) != 0, + synassert(lex::mkloc(lexer), variadic != null || len(values) != 0, "Expected values to append")?; return ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::append_expr { object = alloc(object), variadic = variadic, @@ -224,7 +224,7 @@ fn measurement(lexer: *lex::lexer) (ast::expr | error) = { return ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = expr, }; }; @@ -256,7 +256,7 @@ fn binarithm( const expr = ast::expr { start = lvalue.start, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::binarithm_expr { op = op, lvalue = alloc(lvalue), @@ -271,7 +271,7 @@ fn binarithm( }; fn binding(lexer: *lex::lexer, is_static: bool) (ast::expr | error) = { - const loc = mkloc(lexer); + const loc = lex::mkloc(lexer); const is_const = switch (want(lexer, ltok::LET, ltok::CONST)?.0) { ltok::LET => false, ltok::CONST => true, @@ -299,7 +299,7 @@ fn binding(lexer: *lex::lexer, is_static: bool) (ast::expr | error) = { return ast::expr { start = loc, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::binding_expr { is_static = is_static, is_const = is_const, @@ -342,7 +342,7 @@ fn builtin(lexer: *lex::lexer) (ast::expr | error) = { want(lexer, ltok::DEFER)?; ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = alloc(expression(lexer)?): ast::defer_expr, }; }, @@ -380,7 +380,7 @@ fn call(lexer: *lex::lexer, lvalue: ast::expr) (ast::expr | error) = { return ast::expr { start = lvalue.start, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::call_expr { lvalue = alloc(lvalue), variadic = variadic, @@ -407,7 +407,7 @@ fn cast(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = { }; return cast(lexer, ast::expr { start = lvalue.start, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::cast_expr { kind = kind, value = alloc(lvalue), @@ -428,11 +428,11 @@ fn constant(lexer: *lex::lexer) (ast::expr | error) = { ltok::TRUE => true, ltok::FALSE => false, ltok::NULL => ast::_null, - * => return syntaxerr(mkloc(lexer), "Expected constant expression"), + * => return syntaxerr(lex::mkloc(lexer), "Expected constant expression"), }; return ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = expr, }; }; @@ -455,7 +455,7 @@ fn control(lexer: *lex::lexer) (ast::expr | error) = { }; return ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = expr, }; }; @@ -468,7 +468,7 @@ fn delete_expr(lexer: *lex::lexer) (ast::expr | error) = { want(lexer, ltok::RPAREN)?; return ast::expr { start = start.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = expr: ast::delete_expr, }; }; @@ -489,7 +489,7 @@ fn expression_list(lexer: *lex::lexer) (ast::expr | error) = { want(lexer, ltok::RBRACE)?; return ast::expr { start = start.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = items, }; }; @@ -531,7 +531,7 @@ fn for_expr(lexer: *lex::lexer) (ast::expr | error) = { return ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::for_expr { label = label, bindings = bindings, @@ -549,7 +549,7 @@ fn free_expr(lexer: *lex::lexer) (ast::expr | error) = { want(lexer, ltok::RPAREN)?; return ast::expr { start = start.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = expr: ast::free_expr, }; }; @@ -566,7 +566,7 @@ fn if_expr(lexer: *lex::lexer) (ast::expr | error) = { }; return ast::expr { start = start.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::if_expr { cond = cond, tbranch = tbranch, @@ -595,7 +595,7 @@ fn indexing(lexer: *lex::lexer, lvalue: ast::expr) (ast::expr | error) = { want(lexer, ltok::RBRACKET)?; return ast::expr { start = lvalue.start, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = if (is_slice) ast::slice_expr { object = alloc(lvalue), start = start, @@ -612,7 +612,7 @@ fn indexing(lexer: *lex::lexer, lvalue: ast::expr) (ast::expr | error) = { fn objsel(lexer: *lex::lexer) (ast::expr | error) = { let expr = postfix(lexer, void)?; - synassert(mkloc(lexer), expr.expr is ast::access_expr, + synassert(lex::mkloc(lexer), expr.expr is ast::access_expr, "Expected object selector")?; return expr; }; @@ -628,7 +628,7 @@ fn plain_expression(lexer: *lex::lexer) (ast::expr | error) = { ltok::LBRACKET => plain_array(lexer)?, ltok::STRUCT => ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = plain_struct(lexer, [])?, }, ltok::LPAREN => { @@ -645,20 +645,20 @@ fn plain_expression(lexer: *lex::lexer) (ast::expr | error) = { return match (peek(lexer, ltok::LBRACE)?) { void => ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = id: ast::access_identifier, }, lex::token => { let s = plain_struct(lexer, id)?; ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = s, }; }, }; }, - * => syntaxerr(mkloc(lexer), + * => syntaxerr(lex::mkloc(lexer), "Unexpected {}, was expecting an expression", lex::tokstr(tok)), }; @@ -696,7 +696,7 @@ fn plain_array(lexer: *lex::lexer) (ast::expr | error) = { }; return ast::expr { start = start.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::array_constant { expand = expand, values = values, @@ -720,7 +720,7 @@ fn plain_struct( ltok::NAME, ltok::STRUCT)?; switch (tok.0) { ltok::ELLIPSIS => { - synassert(mkloc(lexer), len(alias) != 0, + synassert(lex::mkloc(lexer), len(alias) != 0, "Cannot use auto-fill with anonymous struct")?; autofill = true; try(lexer, ltok::COMMA)?; @@ -799,7 +799,7 @@ fn struct_field( }; fn plain_tuple(lexer: *lex::lexer, ex: ast::expr) (ast::expr | error) = { - const loc = mkloc(lexer); + const loc = lex::mkloc(lexer); let values: []*ast::expr = []; append(values, alloc(ex)); @@ -823,7 +823,7 @@ fn plain_tuple(lexer: *lex::lexer, ex: ast::expr) (ast::expr | error) = { // XXX: Why do we have to cast this twice? harec bug? return ast::expr { start = loc, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = values: ast::tuple_constant: ast::constant_expr, }; }; @@ -842,7 +842,7 @@ fn postfix(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = ltok::LBRACKET => indexing(lexer, lvalue)?, ltok::QUESTION => ast::expr { start = lvalue.start, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::propagate_expr { is_abort = false, expr = alloc(lvalue), @@ -850,7 +850,7 @@ fn postfix(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = }, ltok::LNOT => ast::expr { start = lvalue.start, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::propagate_expr { is_abort = true, expr = alloc(lvalue), @@ -868,7 +868,7 @@ fn postfix_dot( ) (ast::expr | error) = match (try(lexer, ltok::NAME)?) { tok: lex::token => ast::expr { start = lvalue.start, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::access_field { object = alloc(lvalue), field = tok.1 as str, @@ -877,14 +877,14 @@ fn postfix_dot( void => { let con = constant(lexer)?; let val = con.expr as ast::constant_expr; - synassert(mkloc(lexer), val is lex::value, + synassert(lex::mkloc(lexer), val is lex::value, "Expected integer constant")?; let val = val as lex::value; - synassert(mkloc(lexer), val is i64, + synassert(lex::mkloc(lexer), val is i64, "Expected integer constant")?; ast::expr { start = lvalue.start, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::access_tuple { object = alloc(lvalue), value = alloc(con), @@ -920,7 +920,7 @@ fn switch_expr(lexer: *lex::lexer) (ast::expr | error) = { break; }; }; - synassert(mkloc(lexer), len(opts) != 0, + synassert(lex::mkloc(lexer), len(opts) != 0, "Expected a list of options")?; }; @@ -939,7 +939,7 @@ fn switch_expr(lexer: *lex::lexer) (ast::expr | error) = { return ast::expr { start = start.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::switch_expr { value = alloc(value), cases = cases, @@ -1018,7 +1018,7 @@ fn match_expr(lexer: *lex::lexer) (ast::expr | error) = { return ast::expr { start = start.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::match_expr { value = alloc(value), cases = cases, @@ -1048,7 +1048,7 @@ fn unarithm(lexer: *lex::lexer) (ast::expr | error) = { else unarithm(lexer)?; return ast::expr { start = tok.2, - end = mkloc(lexer), + end = lex::mkloc(lexer), expr = ast::unarithm_expr { op = op, operand = alloc(operand), diff --git a/hare/parse/ident.ha b/hare/parse/ident.ha @@ -23,7 +23,7 @@ fn ident_trailing(lexer: *lex::lexer) ((ast::ident, bool) | error) = { }; if (z > ast::IDENT_MAX) { ast::ident_free(ident: ast::ident); - return syntaxerr(mkloc(lexer), + return syntaxerr(lex::mkloc(lexer), "Identifier exceeds maximum length"); }; return (ident: ast::ident, false); @@ -32,7 +32,7 @@ fn ident_trailing(lexer: *lex::lexer) ((ast::ident, bool) | error) = { // Parses a single identifier, i.e. 'foo::bar::baz'. export fn ident(lexer: *lex::lexer) (ast::ident | error) = { let ident = ident_trailing(lexer)?; - synassert(mkloc(lexer), !ident.1, "Unexpected trailing :: in ident")?; + synassert(lex::mkloc(lexer), !ident.1, "Unexpected trailing :: in ident")?; return ident.0; }; diff --git a/hare/parse/import.ha b/hare/parse/import.ha @@ -32,12 +32,12 @@ export fn imports(lexer: *lex::lexer) ([]ast::import | error) = { switch (want(lexer, ltok::SEMICOLON, ltok::LBRACE, ltok::EQUAL)?.0) { ltok::SEMICOLON => { - synassert(mkloc(lexer), !name.1, + synassert(lex::mkloc(lexer), !name.1, "Unexpected trailing :: in ident")?; append(imports, name.0: ast::import_module); }, ltok::LBRACE => { - synassert(mkloc(lexer), name.1, + synassert(lex::mkloc(lexer), name.1, "Expected trailing :: in ident")?; let objects = name_list(lexer)?; append(imports, ast::import_objects { @@ -47,7 +47,7 @@ export fn imports(lexer: *lex::lexer) ([]ast::import | error) = { want(lexer, ltok::SEMICOLON)?; }, ltok::EQUAL => { - synassert(mkloc(lexer), + synassert(lex::mkloc(lexer), len(name.0) == 1 && !name.1, "Expected name, not ident")?; let ident = ident(lexer)?; diff --git a/hare/parse/parse.ha b/hare/parse/parse.ha @@ -20,12 +20,6 @@ fn syntaxerr( return (loc, why): lex::syntax: lex::error; }; -fn mkloc(lex: *lex::lexer) lex::location = lex::location { - path = lex.path, - 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) = { @@ -42,13 +36,13 @@ fn want(lexer: *lex::lexer, want: lex::ltok...) (lex::token | error) = { let buf = strio::dynamic(); defer io::close(buf); for (let i = 0z; i < len(want); i += 1) { - const tstr = lex::tokstr((want[i], void, mkloc(lexer))); + const tstr = lex::tokstr((want[i], void, lex::mkloc(lexer))); fmt::fprintf(buf, "'{}'", tstr)!; if (i + 1 < len(want)) { fmt::fprint(buf, ", ")!; }; }; - return syntaxerr(mkloc(lexer), "Unexpected '{}', was expecting {}", + return syntaxerr(lex::mkloc(lexer), "Unexpected '{}', was expecting {}", lex::tokstr(tok), strio::string(buf)); }; diff --git a/hare/parse/type.ha b/hare/parse/type.ha @@ -8,7 +8,7 @@ fn prototype(lexer: *lex::lexer) (ast::func_type | error) = { let params: []ast::func_param = []; want(lexer, ltok::LPAREN)?; for (try(lexer, ltok::RPAREN)? is void) { - let loc = mkloc(lexer); + let loc = lex::mkloc(lexer); match (try(lexer, ltok::ELLIPSIS)?) { void => void, lex::token => { @@ -74,7 +74,7 @@ fn integer_type( ltok::U8 => builtin_type::U8, ltok::UINT => builtin_type::UINT, ltok::UINTPTR => builtin_type::UINTPTR, - * => syntaxerr(mkloc(lexer), "Expected integer type"), + * => syntaxerr(lex::mkloc(lexer), "Expected integer type"), }; fn primitive_type(lexer: *lex::lexer) (ast::_type | error) = { @@ -93,19 +93,19 @@ fn primitive_type(lexer: *lex::lexer) (ast::_type | error) = { ltok::F64 => builtin_type::F64, ltok::BOOL => builtin_type::BOOL, ltok::VOID => builtin_type::VOID, - * => return syntaxerr(mkloc(lexer), + * => return syntaxerr(lex::mkloc(lexer), "Unexected {}, was expecting primitive type", lex::tokstr(tok)), }; return ast::_type { - loc = mkloc(lexer), + loc = lex::mkloc(lexer), flags = 0, _type = builtin, }; }; fn alias_type(lexer: *lex::lexer) (ast::_type | error) = { - let loc = mkloc(lexer); + let loc = lex::mkloc(lexer); let unwrap = match (try(lexer, ltok::ELLIPSIS)?) { void => false, * => true, @@ -121,7 +121,7 @@ fn alias_type(lexer: *lex::lexer) (ast::_type | error) = { }; fn pointer_type(lexer: *lex::lexer) (ast::_type | error) = { - let loc = mkloc(lexer); + let loc = lex::mkloc(lexer); let flags = match (try(lexer, ltok::NULLABLE)?) { void => 0: ast::pointer_flags, * => ast::pointer_flags::NULLABLE, @@ -138,7 +138,7 @@ fn pointer_type(lexer: *lex::lexer) (ast::_type | error) = { }; fn tagged_type(lexer: *lex::lexer, first: ast::_type) (ast::_type | error) = { - let loc = mkloc(lexer); + let loc = lex::mkloc(lexer); let tagged: ast::tagged_type = []; append(tagged, alloc(first)); for (try(lexer, ltok::RPAREN)? is void) { @@ -159,7 +159,7 @@ fn tagged_type(lexer: *lex::lexer, first: ast::_type) (ast::_type | error) = { }; fn tuple_type(lexer: *lex::lexer, first: ast::_type) (ast::_type | error) = { - let loc = mkloc(lexer); + let loc = lex::mkloc(lexer); let tuple: ast::tuple_type = []; append(tuple, alloc(first)); for (try(lexer, ltok::RPAREN)? is void) { @@ -180,7 +180,7 @@ fn tuple_type(lexer: *lex::lexer, first: ast::_type) (ast::_type | error) = { }; fn fn_type(lexer: *lex::lexer) (ast::_type | error) = { - let loc = mkloc(lexer); + let loc = lex::mkloc(lexer); let attrs = match (try(lexer, ltok::ATTR_NORETURN)?) { void => 0: ast::func_attrs, * => ast::func_attrs::NORETURN, @@ -202,7 +202,7 @@ fn struct_union_type(lexer: *lex::lexer) (ast::_type | error) = { for (true) { if (try(lexer, ltok::RBRACE) is lex::token) { - synassert(mkloc(lexer), len(membs) != 0, + synassert(lex::mkloc(lexer), len(membs) != 0, "Expected field list")?; break; }; @@ -340,7 +340,7 @@ fn enum_type(lexer: *lex::lexer) (ast::_type | error) = { let membs: []ast::enum_field = []; for (true) { if (try(lexer, ltok::RBRACE) is lex::token) { - synassert(mkloc(lexer), len(membs) != 0, + synassert(lex::mkloc(lexer), len(membs) != 0, "Expected member list")?; break; }; @@ -408,7 +408,7 @@ export fn _type(lexer: *lex::lexer) (ast::_type | error) = { }, ltok::ATTR_NORETURN, ltok::FN => fn_type(lexer)?, ltok::ELLIPSIS, ltok::NAME => alias_type(lexer)?, - * => return syntaxerr(mkloc(lexer), + * => return syntaxerr(lex::mkloc(lexer), "Unexpected {}, was expecting type", lex::tokstr(tok)), };