From b86e6ee2d96294a88eee910429168a43302dff1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20G=C3=B6m=C3=B6ri?= Date: Fri, 26 Nov 2021 13:08:47 +0100 Subject: [PATCH] Ensure parser always returns `erl_anno:location()` in errors --- src/erlfmt_parse.yrl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/erlfmt_parse.yrl b/src/erlfmt_parse.yrl index fe8d58c5..b38ee656 100644 --- a/src/erlfmt_parse.yrl +++ b/src/erlfmt_parse.yrl @@ -994,7 +994,7 @@ Erlang code. %% XXX. To be refined. -type error_description() :: term(). --type error_info() :: {erl_anno:line(), module(), error_description()}. +-type error_info() :: {erl_anno:location(), module(), error_description()}. -type token() :: erlfmt_scan:token(). %% mkop(Op, Arg) -> {op,Anno,Op,Arg}. @@ -1049,13 +1049,19 @@ parse_node(Tokens) -> case parse(Tokens) of {ok, _} = Res -> Res; - Error -> + {error, {AnnoOrLoc, Mod, Description}} -> case parse([{standalone_exprs, element(2, hd(Tokens))} | Tokens]) of {ok, _} = Res -> Res; - _ -> Error + _ -> {error, {error_location(AnnoOrLoc), Mod, Description}} end end. +-spec error_location(erlfmt_scan:anno() | erl_anno:location()) -> erl_anno:location(). +error_location(#{location := Loc}) -> + Loc; +error_location(Loc) -> + Loc. + %% unwrap single-expr definitions, wrapped in guards by the parser build_macro_def( {'-', Anno},