Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:19
lfe
0003-Improve-error-reporting-for-patterns-map-k...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0003-Improve-error-reporting-for-patterns-map-keys-and-li.patch of Package lfe
From 6656ffae5efbfbc18df45aad61d626bea1913843 Mon Sep 17 00:00:00 2001 From: Robert Virding <rvirding@gmail.com> Date: Thu, 24 Nov 2016 00:48:57 +0100 Subject: [PATCH] Improve error reporting for patterns, map keys and literals --- src/lfe_eval.erl | 30 ++++++++++++++++-------------- src/lfe_lint.erl | 41 +++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/lfe_eval.erl b/src/lfe_eval.erl index 96a9731..123eb2e 100644 --- a/src/lfe_eval.erl +++ b/src/lfe_eval.erl @@ -60,9 +60,11 @@ format_error({multi_var,V}) -> lfe_io:format1("multiple occurrence of variable: ~w", [V]); format_error(illegal_bitsize) -> "illegal bitsize"; format_error(illegal_bitseg) -> "illegal bitsegment"; -format_error(illegal_pattern) -> "illegal pattern"; +format_error({illegal_pattern,Pat}) -> + lfe_io:format1("illegal pattern: ~w", [Pat]); format_error(illegal_literal) -> "illegal literal value"; -format_error(illegal_mapkey) -> "illegal map key"; +format_error({illegal_mapkey,Key}) -> + lfe_io:format1("illegal map key: ~w", [Key]); format_error(bad_arity) -> "arity mismatch"; format_error({argument_limit,Arity}) -> lfe_io:format1("too many arguments: ~w", [Arity]); @@ -355,10 +357,10 @@ map_key([quote,E], _) -> E; map_key([_|_]=L, _) -> case is_posint_list(L) of true -> L; %Literal strings only - false -> illegal_mapkey_error() + false -> illegal_mapkey_error(L) end; map_key(E, _) when not is_atom(E) -> E; %Everything else -map_key(_, _) -> illegal_mapkey_error(). +map_key(E, _) -> illegal_mapkey_error(E). -endif. %% new_vars(N) -> Vars. @@ -846,10 +848,10 @@ gmap_key([quote,E], _) -> E; gmap_key([_|_]=L, _) -> case is_posint_list(L) of true -> L; %Literal strings only - false -> illegal_mapkey_error() + false -> illegal_mapkey_error(L) end; gmap_key(E, _) when not is_atom(E) -> E; %Everything else -gmap_key(_, _) -> illegal_mapkey_error(). +gmap_key(E, _) -> illegal_mapkey_error(E). -endif. %% eval_gif(IfBody, Env) -> Val. @@ -906,7 +908,7 @@ match([_|_]=List, Val, Pbs, _) -> %No constructor if List =:= Val -> {yes,Pbs}; true -> no end; - false -> eval_error(illegal_pattern) + false -> eval_error({illegal_pattern,List}) end; match([], [], Pbs, _) -> {yes,Pbs}; match(Symb, Val, Pbs, Env) when is_atom(Symb) -> @@ -1093,16 +1095,16 @@ match_map([K,V|Ps], Map, Pbs0, Env) -> false -> no end; match_map([], _, Pbs, _) -> {yes,Pbs}; -match_map(_, _, _, _) -> eval_error(illegal_pattern). +match_map(Ps, _, _, _) -> eval_error({illegal_pattern,Ps}). pat_map_key([quote,E]) -> E; pat_map_key([_|_]=L) -> case is_posint_list(L) of true -> L; %Literal strings only - false -> illegal_mapkey_error() + false -> illegal_mapkey_error(L) end; pat_map_key(E) when not is_atom(E) -> E; %Everything else -pat_map_key(_) -> illegal_mapkey_error(). +pat_map_key(K) -> illegal_mapkey_error(K). %% eval_lit(Literal, Env) -> Value. %% Evaluate a literal expression. Error if invalid. @@ -1119,8 +1121,8 @@ eval_lit([binary|Bs], Env) -> eval_lit([map|As], Env) -> KVs = eval_lit_map(As, Env), maps:from_list(KVs); -eval_lit([_|_], _) -> %All other lists illegal - eval_error(illegal_literal); +eval_lit([_|_]=Lit, _) -> %All other lists illegal + eval_error({illegal_literal,Lit}); eval_lit(Symb, Env) when is_atom(Symb) -> case get_vbinding(Symb, Env) of {yes,Val} -> Val; @@ -1151,8 +1153,8 @@ unbound_func_error(Func) -> bad_form_error(Form) -> eval_error({bad_form,Form}). -illegal_mapkey_error() -> - eval_error(illegal_mapkey). +illegal_mapkey_error(Key) -> + eval_error({illegal_mapkey,Key}). eval_error(Error) -> erlang:raise(error, Error, stacktrace()). diff --git a/src/lfe_lint.erl b/src/lfe_lint.erl index cf202d4..527c2f5 100644 --- a/src/lfe_lint.erl +++ b/src/lfe_lint.erl @@ -80,11 +80,14 @@ format_error({redef_fun,F}) -> lfe_io:format1("redefining function: ~w", [F]); format_error({bad_fdef,F}) -> lfe_io:format1("bad function definition: ~w", [F]); -format_error(illegal_literal) -> "illegal literal"; -format_error(illegal_pattern) -> "illegal pattern"; +format_error({illegal_literal,Lit}) -> + lfe_io:format1("illegal literal value: ~w", [Lit]); +format_error({illegal_pattern,Pat}) -> + lfe_io:format1("illegal pattern: ~w", [Pat]); format_error(illegal_guard) -> "illegal guard"; format_error(illegal_bitseg) -> "illegal bit segment"; -format_error(illegal_mapkey) -> "illegal map key"; +format_error({illegal_mapkey,Key}) -> + lfe_io:format1("illegal map key: ~w", [Key]); format_error({undefined_bittype,S}) -> lfe_io:format1("bit type ~w undefined", [S]); format_error(bittype_unit) -> @@ -590,7 +593,7 @@ map_key(Key, Env, L, St) -> map_key(Key, _, L, St) -> case is_map_key(Key) of true -> St; - false -> add_error(L, illegal_mapkey, St) + false -> illegal_mapkey_error(L, Key, St) end. is_map_key([quote,Lit]) -> is_literal(Lit); @@ -997,7 +1000,7 @@ gmap_key(Key, Env, L, St) -> gmap_key(Key, _, L, St) -> case is_gmap_key(Key) of true -> St; - false -> add_error(L, illegal_mapkey, St) + false -> illegal_mapkey_error(L, Key, St) end. is_gmap_key([quote,Lit]) -> is_literal(Lit); @@ -1032,7 +1035,7 @@ pattern(Pat, Env, L, St) -> %% try %% pattern(Pat, [], Env, L, St) %% catch -%% _:_ -> {[],add_error(L, illegal_pattern, St)} +%% _:_ -> {[],illegal_pattern_error(L, Pat, St)} %% end. pattern([quote,Lit], Pvs, Env, L, St) -> @@ -1056,14 +1059,14 @@ pattern([tuple|Ps], Pvs, Env, L, St) -> %Tuple elements pat_list(Ps, Pvs, Env, L, St); pattern([binary|Segs], Pvs, Env, L, St) -> pat_binary(Segs, Pvs, Env, L, St); -pattern([map|As], Pvs, Env, L, St) -> - pat_map(As, Pvs, Env, L, St); +pattern([map|Ps], Pvs, Env, L, St) -> + pat_map(Ps, Pvs, Env, L, St); %% Check old no contructor list forms. pattern([_|_]=List, Pvs0, _, L, St0) -> case is_posint_list(List) of true -> {Pvs0,St0}; %A string false -> %Illegal pattern - {Pvs0,add_error(L, illegal_pattern, St0)} + {Pvs0,illegal_pattern_error(L, List, St0)} end; pattern([], Pvs, _, _, St) -> {Pvs,St}; pattern(Symb, Pvs, _, L, St) when is_atom(Symb) -> @@ -1075,8 +1078,8 @@ pat_list([P|Ps], Pvs0, Env, L, St0) -> {Pvs1,St1} = pattern(P, Pvs0, Env, L, St0), pat_list(Ps, Pvs1, Env, L, St1); pat_list([], Pvs, _, _, St) -> {Pvs,St}; -pat_list(_, Pvs, _, L, St) -> - {Pvs,add_error(L, illegal_pattern, St)}. +pat_list(Pat, Pvs, _, L, St) -> + {Pvs,illegal_pattern_error(L, Pat, St)}. pat_symb('_', Pvs, _, St) -> {Pvs,St}; %Don't care variable pat_symb(Symb, Pvs, L, St) -> @@ -1201,7 +1204,7 @@ pat_bit_expr(S, Bvs, Pvs, _, L, St) when is_atom(S) -> pat_bit_expr(_, Bvs, _, _, L, St) -> {Bvs,add_error(L, illegal_bitseg, St)}. -%% pat_map(Args, PatVars, Env, Line, State) -> {PatVars,State}. +%% pat_map(Pairs, PatVars, Env, Line, State) -> {PatVars,State}. -ifdef(HAS_MAPS). pat_map([K,V|As], Pvs0, Env, L, St0) -> @@ -1221,7 +1224,7 @@ pat_map_assoc(K, V, Pvs, Env, L, St0) -> pat_map_key(Key, _, L, St) -> case is_pat_map_key(Key) of true -> St; - false -> add_error(L, illegal_mapkey, St) + false -> illegal_mapkey_error(L, Key, St) end. is_pat_map_key([quote,Lit]) -> is_literal(Lit); @@ -1229,8 +1232,8 @@ is_pat_map_key([_|_]=L) -> is_posint_list(L); %Literal strings only is_pat_map_key(E) when is_atom(E) -> false; is_pat_map_key(Lit) -> is_literal(Lit). -else. -pat_map(_, Pvs, _, L, St) -> - {Pvs,add_error(L, illegal_pattern, St)}. +pat_map(Ps, Pvs, _, L, St) -> + {Pvs,illegal_pattern_error(L, Ps, St)}. -endif. %% is_literal(Literal) -> true | false. @@ -1243,7 +1246,7 @@ pat_map(_, Pvs, _, L, St) -> literal(Lit, _, L, St) -> case is_literal(Lit) of true -> St; - false -> add_error(L, illegal_literal, St) + false -> add_error(L, {illegal_literal,Lit}, St) end. is_literal(A) when is_atom(A) -> true; @@ -1375,9 +1378,15 @@ bad_form_error(L, F, St) -> bad_gform_error(L, F, St) -> add_error(L, {bad_gform,F}, St). +illegal_mapkey_error(L, K, St) -> + add_error(L, {illegal_mapkey,K}, St). + bad_pat_error(L, F, St) -> add_error(L, {bad_pat,F}, St). +illegal_pattern_error(L, P, St) -> + add_error(L, {illegal_pattern,P}, St). + bad_mdef_error(L, D, St) -> add_error(L, {bad_mdef,D}, St). -- 2.11.0
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor