diff --git a/src/aeso_parser.erl b/src/aeso_parser.erl index b934e08..78b329f 100644 --- a/src/aeso_parser.erl +++ b/src/aeso_parser.erl @@ -68,9 +68,14 @@ decl() -> modifiers() -> many(choice([token(stateful), token(public), token(private), token(internal)])). -add_modifiers(Mods, Node) -> - lists:foldl(fun({Mod, _}, X) -> set_ann(Mod, true, X) end, - Node, Mods). +add_modifiers([], Node) -> Node; +add_modifiers(Mods = [Tok | _], Node) -> + %% Set the position to the position of the first modifier. This is + %% important for code transformation tools (like what we do in + %% create_calldata) to be able to get the indentation of the declaration. + set_pos(get_pos(Tok), + lists:foldl(fun({Mod, _}, X) -> set_ann(Mod, true, X) end, + Node, Mods)). %% -- Type declarations ------------------------------------------------------ diff --git a/test/aeso_abi_tests.erl b/test/aeso_abi_tests.erl index 706d7af..3993e68 100644 --- a/test/aeso_abi_tests.erl +++ b/test/aeso_abi_tests.erl @@ -105,9 +105,28 @@ calldata_init_test() -> Code = parameterized_contract("foo", ["int"]), encode_decode_calldata_(Code, "init", [], {tuple, [typerep, {tuple, []}]}). +calldata_indent_test() -> + Test = fun(Extra) -> + encode_decode_calldata_( + parameterized_contract(Extra, "foo", ["int"]), + "foo", ["42"], word) + end, + Test(" stateful function bla() = ()"), + Test(" type x = int"), + Test(" private function bla : int => int"), + Test(" public stateful function bla(x : int) =\n" + " x + 1"), + Test(" stateful private function bla(x : int) : int =\n" + " x + 1"), + ok. + parameterized_contract(FunName, Types) -> + parameterized_contract([], FunName, Types). + +parameterized_contract(ExtraCode, FunName, Types) -> lists:flatten( - ["contract Dummy =\n" + ["contract Dummy =\n", + ExtraCode, "\n", " type an_alias('a) = (string, 'a)\n" " record r = {x : an_alias(int), y : variant}\n" " datatype variant = Red | Blue(map(string, int))\n" @@ -127,7 +146,7 @@ permissive_literals_fail_test() -> "contract OracleTest =\n" " function haxx(o : oracle(list(string), option(int))) =\n" " Chain.spend(o, 1000000)\n", - {error, <<"Type errors\nCannot unify address\n and oracle", _/binary>>} = + {error, <<"Type errors\nCannot unify", _/binary>>} = aeso_compiler:check_call(Contract, "haxx", ["#123"], []), ok.