diff --git a/src/aeso_parser.erl b/src/aeso_parser.erl index 80002ca..c33e47c 100644 --- a/src/aeso_parser.erl +++ b/src/aeso_parser.erl @@ -224,6 +224,9 @@ arg() -> choice( ?RULE(id(), {arg, get_ann(_1), _1, type_wildcard(get_ann(_1))}), ?RULE(id(), tok(':'), type(), {arg, get_ann(_1), _1, _3})). +letpat() -> + ?RULE(keyword('('), id(), tok('='), pattern(), tok(')'), {letpat, get_ann(_1), _2, _4}). + %% -- Types ------------------------------------------------------------------ type_vars() -> paren_list(tvar()). @@ -328,6 +331,7 @@ exprAtom() -> , ?RULE(keyword('['), Expr, token('|'), comma_sep(comprehension_exp()), tok(']'), list_comp_e(_1, _2, _4)) , ?RULE(tok('['), Expr, binop('..'), Expr, tok(']'), _3(_2, _4)) , ?RULE(keyword('('), comma_sep(Expr), tok(')'), tuple_e(_1, _2)) + , letpat() ]) end). @@ -588,6 +592,8 @@ tuple_e(Ann, Exprs) -> {tuple, Ann, Exprs}. list_comp_e(Ann, Expr, Binds) -> {list_comp, Ann, Expr, Binds}. -spec parse_pattern(aeso_syntax:expr()) -> aeso_parse_lib:parser(aeso_syntax:pat()). +parse_pattern({letpat, Ann, Id, Pat}) -> + {letpat, Ann, Id, parse_pattern(Pat)}; parse_pattern({app, Ann, Con = {'::', _}, Es}) -> {app, Ann, Con, lists:map(fun parse_pattern/1, Es)}; parse_pattern({app, Ann, {'-', _}, [{int, _, N}]}) -> diff --git a/src/aeso_pretty.erl b/src/aeso_pretty.erl index 6b43b4c..9be659a 100644 --- a/src/aeso_pretty.erl +++ b/src/aeso_pretty.erl @@ -299,6 +299,8 @@ tuple_type(Factors) -> ]). -spec expr_p(integer(), aeso_syntax:arg_expr()) -> doc(). +expr_p(P, {letpat, _, Id, Pat}) -> + paren(P > 100, follow(hsep(expr(Id), text("=")), expr(Pat))); expr_p(P, {named_arg, _, Name, E}) -> paren(P > 100, follow(hsep(expr(Name), text("=")), expr(E))); expr_p(P, {lam, _, Args, E}) -> diff --git a/src/aeso_syntax.erl b/src/aeso_syntax.erl index 8148a1a..1873d8e 100644 --- a/src/aeso_syntax.erl +++ b/src/aeso_syntax.erl @@ -58,6 +58,7 @@ -type letval() :: {letval, ann(), pat(), expr()}. -type letfun() :: {letfun, ann(), id(), [pat()], type(), expr()}. +-type letpat() :: {letpat, ann(), id(), pat()}. -type fundecl() :: {fun_decl, ann(), id(), type()}. -type letbind() @@ -122,7 +123,8 @@ | {block, ann(), [stmt()]} | {op(), ann()} | id() | qid() | con() | qcon() - | constant(). + | constant() + | letpat(). -type record_or_map() :: record | map | record_or_map_error. @@ -156,6 +158,7 @@ | {list, ann(), [pat()]} | {typed, ann(), pat(), type()} | {record, ann(), [field(pat())]} + | letpat() | constant() | con() | id().