From 3f177d363f1322d449d312c84168c1b0782f1089 Mon Sep 17 00:00:00 2001 From: Gaith Hallak Date: Sat, 11 Jun 2022 17:26:13 +0400 Subject: [PATCH] Add constraints to the syntax and parser --- src/aeso_parser.erl | 16 ++++++++++++++-- src/aeso_scan.erl | 2 +- src/aeso_syntax.erl | 4 ++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/aeso_parser.erl b/src/aeso_parser.erl index 51b81f6..b0f0bce 100644 --- a/src/aeso_parser.erl +++ b/src/aeso_parser.erl @@ -134,9 +134,20 @@ fun_block(Mods, Kind, [Decl]) -> fun_block(Mods, Kind, Decls) -> {block, get_ann(Kind), [ add_modifiers(Mods, Kind, Decl) || Decl <- Decls ]}. +typevar_constraint() -> + ?RULE(tvar(), keyword(is), choice([tok(eq), tok(ord)]), + {constraint, element(2, _1), element(3, _1), element(1, _3)}). + +typevars_constraints() -> + ?RULE(comma_sep1(typevar_constraint()), tok(';'), _1). + +fundecl() -> + choice([?RULE(id(), tok(':'), typevars_constraints(), type(), + {fun_decl, get_ann(_1), _1, {constrained_t, get_ann(_1), _3, _4}}), + ?RULE(id(), tok(':'), type(), {fun_decl, get_ann(_1), _1, _3})]). + fundef_or_decl() -> - choice([?RULE(id(), tok(':'), type(), {fun_decl, get_ann(_1), _1, _3}), - fundef()]). + choice([fundecl(), fundef()]). using() -> Alias = {keyword(as), con()}, @@ -527,6 +538,7 @@ parens(P) -> between(tok('('), P, tok(')')). braces(P) -> between(tok('{'), P, tok('}')). brackets(P) -> between(tok('['), P, tok(']')). comma_sep(P) -> sep(P, tok(',')). +comma_sep1(P) -> sep1(P, tok(',')). paren_list(P) -> parens(comma_sep(P)). brace_list(P) -> braces(comma_sep(P)). diff --git a/src/aeso_scan.erl b/src/aeso_scan.erl index 4587efa..11900b1 100644 --- a/src/aeso_scan.erl +++ b/src/aeso_scan.erl @@ -45,7 +45,7 @@ lexer() -> Keywords = ["contract", "include", "let", "switch", "type", "record", "datatype", "if", "elif", "else", "function", "stateful", "payable", "true", "false", "mod", "public", "entrypoint", "private", "indexed", "namespace", - "interface", "main", "using", "as", "for", "hiding" + "interface", "main", "using", "as", "for", "hiding", "is", "eq", "ord" ], KW = string:join(Keywords, "|"), diff --git a/src/aeso_syntax.erl b/src/aeso_syntax.erl index 1e01153..6461dac 100644 --- a/src/aeso_syntax.erl +++ b/src/aeso_syntax.erl @@ -79,11 +79,15 @@ -type constructor_t() :: {constr_t, ann(), con(), [type()]}. +-type tvar_constraint() :: eq | ord. +-type constraint() :: {constraint, ann(), name(), tvar_constraint()}. + -type type() :: {fun_t, ann(), [named_arg_t()], [type()], type()} | {app_t, ann(), type(), [type()]} | {tuple_t, ann(), [type()]} | {args_t, ann(), [type()]} %% old tuple syntax, old for error messages | {bytes_t, ann(), integer() | any} + | {constrained_t, ann(), [constraint()], type()} | id() | qid() | con() | qcon() %% contracts | tvar().