Improve some parse errors
This commit is contained in:
parent
61faa3e2dd
commit
412b0b8b6d
@ -15,6 +15,8 @@
|
|||||||
many/1, many1/1, sep/2, sep1/2,
|
many/1, many1/1, sep/2, sep1/2,
|
||||||
infixl/2, infixr/2]).
|
infixl/2, infixr/2]).
|
||||||
|
|
||||||
|
-export([current_file/0, set_current_file/1]).
|
||||||
|
|
||||||
%% -- Types ------------------------------------------------------------------
|
%% -- Types ------------------------------------------------------------------
|
||||||
|
|
||||||
-export_type([parser/1, parser_expr/1, pos/0, token/0, tokens/0]).
|
-export_type([parser/1, parser_expr/1, pos/0, token/0, tokens/0]).
|
||||||
@ -159,7 +161,7 @@ layout() -> ?layout.
|
|||||||
-spec parse(parser(A), tokens()) -> {ok, A} | {error, term()}.
|
-spec parse(parser(A), tokens()) -> {ok, A} | {error, term()}.
|
||||||
parse(P, S) ->
|
parse(P, S) ->
|
||||||
case parse1(apply_p(P, fun(X) -> {return_plus, X, {fail, no_error}} end), S) of
|
case parse1(apply_p(P, fun(X) -> {return_plus, X, {fail, no_error}} end), S) of
|
||||||
{[], {Pos, Err}} -> {error, {Pos, parse_error, flatten_error(Err)}};
|
{[], {Pos, Err}} -> {error, {add_current_file(Pos), parse_error, flatten_error(Err)}};
|
||||||
{[A], _} -> {ok, A};
|
{[A], _} -> {ok, A};
|
||||||
{As, _} -> {error, {{1, 1}, ambiguous_parse, As}}
|
{As, _} -> {error, {{1, 1}, ambiguous_parse, As}}
|
||||||
end.
|
end.
|
||||||
@ -289,7 +291,7 @@ parse1({tok_bind, Map}, Ts, Acc, Err) ->
|
|||||||
%% y + y)(4)
|
%% y + y)(4)
|
||||||
case maps:get(vclose, Map, '$not_found') of
|
case maps:get(vclose, Map, '$not_found') of
|
||||||
'$not_found' ->
|
'$not_found' ->
|
||||||
{Acc, unexpected_token_error(Ts, T)};
|
{Acc, unexpected_token_error(Ts, maps:keys(Map), T)};
|
||||||
F ->
|
F ->
|
||||||
VClose = {vclose, pos(T)},
|
VClose = {vclose, pos(T)},
|
||||||
Ts2 = pop_layout(VClose, Ts#ts{ last = VClose }),
|
Ts2 = pop_layout(VClose, Ts#ts{ last = VClose }),
|
||||||
@ -333,7 +335,45 @@ mk_error(Ts, Err) ->
|
|||||||
|
|
||||||
-spec unexpected_token_error(#ts{}, token()) -> error().
|
-spec unexpected_token_error(#ts{}, token()) -> error().
|
||||||
unexpected_token_error(Ts, T) ->
|
unexpected_token_error(Ts, T) ->
|
||||||
mk_error(Ts, io_lib:format("Unexpected token ~p", [tag(T)])).
|
unexpected_token_error(Ts, [], T).
|
||||||
|
|
||||||
|
unexpected_token_error(Ts, Expect, {Tag, _}) when Tag == vclose; Tag == vsemi ->
|
||||||
|
Braces = [')', ']', '}'],
|
||||||
|
Fix = case lists:filter(fun(T) -> lists:member(T, Braces) end, Expect) of
|
||||||
|
[] -> " Probable causes:\n"
|
||||||
|
" - something is missing in the previous statement, or\n"
|
||||||
|
" - this line should be indented more.";
|
||||||
|
[T | _] -> io_lib:format(" Did you forget a ~p?", [T])
|
||||||
|
end,
|
||||||
|
Msg = io_lib:format("Unexpected indentation.~s", [Fix]),
|
||||||
|
mk_error(Ts, Msg);
|
||||||
|
unexpected_token_error(Ts, Expect, T) ->
|
||||||
|
ExpectCon = lists:member(con, Expect),
|
||||||
|
ExpectId = lists:member(id, Expect),
|
||||||
|
Fix = case T of
|
||||||
|
{id, _, X} when ExpectCon, hd(X) /= $_ -> io_lib:format(" Did you mean ~s?", [mk_upper(X)]);
|
||||||
|
{con, _, X} when ExpectId -> io_lib:format(" Did you mean ~s?", [mk_lower(X)]);
|
||||||
|
{qcon, _, Xs} when ExpectCon -> io_lib:format(" Did you mean ~s?", [lists:last(Xs)]);
|
||||||
|
{qid, _, Xs} when ExpectId -> io_lib:format(" Did you mean ~s?", [lists:last(Xs)]);
|
||||||
|
_ -> ""
|
||||||
|
end,
|
||||||
|
mk_error(Ts, io_lib:format("Unexpected ~s.~s", [describe(T), Fix])).
|
||||||
|
|
||||||
|
mk_upper([C | Rest]) -> string:to_upper([C]) ++ Rest.
|
||||||
|
mk_lower([C | Rest]) -> string:to_lower([C]) ++ Rest.
|
||||||
|
|
||||||
|
|
||||||
|
describe({id, _, X}) -> io_lib:format("identifier ~s", [X]);
|
||||||
|
describe({con, _, X}) -> io_lib:format("identifier ~s", [X]);
|
||||||
|
describe({qid, _, Xs}) -> io_lib:format("qualified identifier ~s", [string:join(Xs, ".")]);
|
||||||
|
describe({qcon, _, Xs}) -> io_lib:format("qualified identifier ~s", [string:join(Xs, ".")]);
|
||||||
|
describe({tvar, _, X}) -> io_lib:format("type variable ~s", [X]);
|
||||||
|
describe({char, _, _}) -> "character literal";
|
||||||
|
describe({string, _, _}) -> "string literal";
|
||||||
|
describe({hex, _, _}) -> "integer literal";
|
||||||
|
describe({int, _, _}) -> "integer literal";
|
||||||
|
describe({bytes, _, _}) -> "bytes literal";
|
||||||
|
describe(T) -> io_lib:format("token '~s'", [tag(T)]).
|
||||||
|
|
||||||
%% Get the next token from a token stream. Inserts layout tokens if necessary.
|
%% Get the next token from a token stream. Inserts layout tokens if necessary.
|
||||||
-spec next_token(#ts{}) -> false | {token(), #ts{}}.
|
-spec next_token(#ts{}) -> false | {token(), #ts{}}.
|
||||||
@ -417,3 +457,13 @@ merge_with(Fun, Map1, Map2) ->
|
|||||||
end, Map2, maps:to_list(Map1))
|
end, Map2, maps:to_list(Map1))
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% Current source file
|
||||||
|
current_file() ->
|
||||||
|
get('$current_file').
|
||||||
|
|
||||||
|
set_current_file(File) ->
|
||||||
|
put('$current_file', File).
|
||||||
|
|
||||||
|
add_current_file({L, C}) -> {current_file(), L, C};
|
||||||
|
add_current_file(Pos) -> Pos.
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
type/1]).
|
type/1]).
|
||||||
|
|
||||||
-include("aeso_parse_lib.hrl").
|
-include("aeso_parse_lib.hrl").
|
||||||
|
-import(aeso_parse_lib, [current_file/0, set_current_file/1]).
|
||||||
|
|
||||||
-type parse_result() :: aeso_syntax:ast() | none().
|
-type parse_result() :: aeso_syntax:ast() | none().
|
||||||
|
|
||||||
@ -451,12 +452,6 @@ bracket_list(P) -> brackets(comma_sep(P)).
|
|||||||
-spec pos_ann(ann_line(), ann_col()) -> ann().
|
-spec pos_ann(ann_line(), ann_col()) -> ann().
|
||||||
pos_ann(Line, Col) -> [{file, current_file()}, {line, Line}, {col, Col}].
|
pos_ann(Line, Col) -> [{file, current_file()}, {line, Line}, {col, Col}].
|
||||||
|
|
||||||
current_file() ->
|
|
||||||
get('$current_file').
|
|
||||||
|
|
||||||
set_current_file(File) ->
|
|
||||||
put('$current_file', File).
|
|
||||||
|
|
||||||
ann_pos(Ann) ->
|
ann_pos(Ann) ->
|
||||||
{proplists:get_value(file, Ann),
|
{proplists:get_value(file, Ann),
|
||||||
proplists:get_value(line, Ann),
|
proplists:get_value(line, Ann),
|
||||||
|
@ -162,7 +162,16 @@ not_yet_compilable(aevm) -> [].
|
|||||||
end)()).
|
end)()).
|
||||||
|
|
||||||
failing_contracts() ->
|
failing_contracts() ->
|
||||||
[ ?TEST(name_clash,
|
%% Parse errors
|
||||||
|
[ ?TEST(field_parse_error,
|
||||||
|
[<<?Pos(5, 26)
|
||||||
|
"Cannot use nested fields or keys in record construction: p.x">>])
|
||||||
|
, ?TEST(vsemi, [<<?Pos(3, 3) "Unexpected indentation. Did you forget a '}'?">>])
|
||||||
|
, ?TEST(vclose, [<<?Pos(4, 3) "Unexpected indentation. Did you forget a ']'?">>])
|
||||||
|
, ?TEST(indent_fail, [<<?Pos(3, 2) "Unexpected token 'entrypoint'.">>])
|
||||||
|
|
||||||
|
%% Type errors
|
||||||
|
, ?TEST(name_clash,
|
||||||
[<<?Pos(14, 3)
|
[<<?Pos(14, 3)
|
||||||
"Duplicate definitions of abort at\n"
|
"Duplicate definitions of abort at\n"
|
||||||
" - (builtin location)\n"
|
" - (builtin location)\n"
|
||||||
@ -444,9 +453,6 @@ failing_contracts() ->
|
|||||||
"The init function should return the initial state as its result and cannot read the state,\n"
|
"The init function should return the initial state as its result and cannot read the state,\n"
|
||||||
"but it calls\n"
|
"but it calls\n"
|
||||||
" - state (at line 13, column 13)">>])
|
" - state (at line 13, column 13)">>])
|
||||||
, ?TEST(field_parse_error,
|
|
||||||
[<<?Pos("field_parse_error", 5, 26)
|
|
||||||
"Cannot use nested fields or keys in record construction: p.x">>])
|
|
||||||
, ?TEST(modifier_checks,
|
, ?TEST(modifier_checks,
|
||||||
[<<?Pos(11, 3)
|
[<<?Pos(11, 3)
|
||||||
"The function all_the_things (at line 11, column 3) cannot be both public and private.">>,
|
"The function all_the_things (at line 11, column 3) cannot be both public and private.">>,
|
||||||
|
3
test/contracts/indent_fail.aes
Normal file
3
test/contracts/indent_fail.aes
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
contract IndentFail =
|
||||||
|
entrypoint twoSpace() = ()
|
||||||
|
entrypoint oneSpace() = ()
|
4
test/contracts/vclose.aes
Normal file
4
test/contracts/vclose.aes
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
contract VClose =
|
||||||
|
entrypoint missing_bracket() =
|
||||||
|
let x = [1, 2, 3
|
||||||
|
entrypoint bar() = ()
|
3
test/contracts/vsemi.aes
Normal file
3
test/contracts/vsemi.aes
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
contract VSemi =
|
||||||
|
record missing_brace = { x : int
|
||||||
|
entrypoint foo() = ()
|
Loading…
x
Reference in New Issue
Block a user