diff options
author | Pascal Rigaux <pixel@mandriva.com> | 2003-05-27 09:01:04 +0000 |
---|---|---|
committer | Pascal Rigaux <pixel@mandriva.com> | 2003-05-27 09:01:04 +0000 |
commit | 644c0e14e2a0715ae638fda88d5e3c34b158cece (patch) | |
tree | 352e8f5a3a94f4de934e997e5907fe9b0dd4e84f | |
parent | 5f1163eda4898cc0f38975cc4d8b506e3fc36052 (diff) | |
download | perl_checker-644c0e14e2a0715ae638fda88d5e3c34b158cece.tar perl_checker-644c0e14e2a0715ae638fda88d5e3c34b158cece.tar.gz perl_checker-644c0e14e2a0715ae638fda88d5e3c34b158cece.tar.bz2 perl_checker-644c0e14e2a0715ae638fda88d5e3c34b158cece.tar.xz perl_checker-644c0e14e2a0715ae638fda88d5e3c34b158cece.zip |
- disallow return(...), prefering return ...
- P_call_no_paren has a lower priority than P_comma
-rw-r--r-- | perl_checker.src/parser.mly | 6 | ||||
-rw-r--r-- | perl_checker.src/parser_helper.ml | 38 | ||||
-rw-r--r-- | perl_checker.src/parser_helper.mli | 19 | ||||
-rw-r--r-- | perl_checker.src/types.mli | 2 |
4 files changed, 47 insertions, 18 deletions
diff --git a/perl_checker.src/parser.mly b/perl_checker.src/parser.mly index 14a0a07..1490611 100644 --- a/perl_checker.src/parser.mly +++ b/perl_checker.src/parser.mly @@ -319,10 +319,10 @@ term: | ONE_SCALAR_PARA word argexpr {check_parenthesized_first_argexpr_with_Ident $2.any $3; call_one_scalar_para $1 [call(Deref(I_func, $2.any), $3.any.expr)] $1 $3} /* ref foo $a, $b */ | ONE_SCALAR_PARA hash PKG_SCOPE {sp_0($3); call_one_scalar_para $1 [ Call(Too_complex, [$2.any]) ] $1 $3} /* keys %main:: */ -| func parenthesized {sp_0($2); new_pesp M_unknown P_tok (call_func true ($1.any, $2.any.expr)) $1 $2} /* &foo(@args) */ -| word argexpr {check_parenthesized_first_argexpr_with_Ident $1.any $2; new_pesp M_unknown P_call_no_paren (call(Deref(I_func, $1.any), $2.any.expr)) $1 $2} /* foo $a, $b */ +| func parenthesized {sp_0($2); new_pesp M_unknown P_tok (call_func $1 $2) $1 $2} /* &foo(@args) */ +| word argexpr {check_parenthesized_first_argexpr_with_Ident $1.any $2; call_no_paren $1 $2} /* foo $a, $b */ | word BRACKET lines BRACKET_END COMMA argexpr %prec LSTOP {sp_n($2); new_pesp M_unknown P_call_no_paren (call(Deref(I_func, $1.any), Ref(I_hash, List $3.any) :: $6.any.expr)) $1 $6} /* bless { foo }, $bar */ -| word_paren parenthesized {sp_0($2); new_pesp M_unknown P_tok (call(Deref(I_func, $1.any), $2.any.expr)) $1 $2} /* foo(@args) */ +| word_paren parenthesized {sp_0($2); call_with_paren $1 $2} /* foo(@args) */ | word BRACKET lines BRACKET_END listexpr %prec LSTOP {sp_n($2); check_block_sub $3 $4; call_and_context(Deref(I_func, $1.any), anonymous_sub None $3 :: $5.any.expr) (if $5.any.expr = [] then P_tok else P_call_no_paren) $1 $5} /* map { foo } @bar */ | word BRACKET BRACKET expr BRACKET_END BRACKET_END listexpr %prec LSTOP {sp_n($2); sp_p($3); sp_p($4); sp_p($5); sp_p($6); new_pesp M_unknown (if $7.any.expr = [] then P_tok else P_call_no_paren) (call(Deref(I_func, $1.any), anonymous_sub None (new_esp (M_ref M_hash) [ Ref(I_hash, $4.any.expr) ] $4 $4) :: $7.any.expr)) $1 $7} /* map { { foo } } @bar */ | word BRACKET BRACKET expr BRACKET_END semi_colon BRACKET_END listexpr %prec LSTOP {sp_n($2); sp_p($3); sp_p($4); sp_p($5); sp_p($7); new_pesp M_unknown (if $8.any.expr = [] then P_tok else P_call_no_paren) (call(Deref(I_func, $1.any), anonymous_sub None (new_esp (M_ref M_hash) [ Ref(I_hash, $4.any.expr); Semi_colon ] $4 $4) :: $8.any.expr)) $1 $8} /* map { { foo }; } @bar */ diff --git a/perl_checker.src/parser_helper.ml b/perl_checker.src/parser_helper.ml index b2726e4..bdf3d49 100644 --- a/perl_checker.src/parser_helper.ml +++ b/perl_checker.src/parser_helper.ml @@ -193,10 +193,10 @@ let rec prio_less = function | _, P_and -> true | P_and, _ -> false - | _, P_comma -> true - | P_comma, _ -> false | _, P_call_no_paren -> true | P_call_no_paren, _ -> false + | _, P_comma -> true + | P_comma, _ -> false | _, P_assign -> true | P_assign, _ -> false | _, P_ternary -> true @@ -228,21 +228,22 @@ let rec prio_less = function | P_paren _, _ -> true | P_tok, _ -> true -let prio_lo pri_out in_ = - if prio_less(in_.any.priority, pri_out) then - (match in_.any.priority with +let prio_lo_check pri_out pri_in pos expr = + if prio_less(pri_in, pri_out) then + (match pri_in with | P_paren (P_paren_wanted _) -> () | P_paren pri_in' -> if pri_in' <> pri_out && - prio_less(pri_in', pri_out) && not_complex (un_parenthesize in_.any.expr) then - warn in_.pos "unneeded parentheses" + prio_less(pri_in', pri_out) && not_complex (un_parenthesize expr) then + warn pos "unneeded parentheses" | _ -> ()) else - (match in_.any.expr with + (match expr with | Call_op ("print", [Deref (I_star, Ident (None, "STDOUT", _)); Deref(I_scalar, ident)], _) -> - warn in_.pos (sprintf "use parentheses: replace \"print $%s ...\" with \"print($%s ...)\"" (string_of_Ident ident) (string_of_Ident ident)) - | _ -> warn in_.pos "missing parentheses (needed for clarity)") ; - in_.any.expr + warn pos (sprintf "use parentheses: replace \"print $%s ...\" with \"print($%s ...)\"" (string_of_Ident ident) (string_of_Ident ident)) + | _ -> warn pos "missing parentheses (needed for clarity)") + +let prio_lo pri_out in_ = prio_lo_check pri_out in_.any.priority in_.pos in_.any.expr ; in_.any.expr let prio_lo_after pri_out in_ = if in_.any.priority = P_call_no_paren then in_.any.expr else prio_lo pri_out in_ @@ -737,7 +738,7 @@ msgstr \"\" ) pot_strings ; close_out fd -let call_func is_a_func (e, para) = +let call_raw is_a_func (e, para) = match e with | Deref(I_func, Ident(None, f, _)) -> let para' = match f with @@ -802,7 +803,18 @@ let call_func is_a_func (e, para) = in Call(e, some_or para' para) | _ -> Call(e, para) -let call(e, para) = call_func false (e, para) +let call(e, para) = call_raw false (e, para) + +let call_func esp_func esp_para = call_raw true (esp_func.any, esp_para.any.expr) + +let check_return esp_func esp_para = + match esp_func.any with + | Ident(None, "return", _) -> + prio_lo_check P_call_no_paren esp_para.any.priority esp_para.pos (List esp_para.any.expr) + | _ -> () + +let call_no_paren esp_func esp_para = check_return esp_func esp_para; new_pesp M_unknown P_call_no_paren (call(Deref(I_func, esp_func.any), esp_para.any.expr)) esp_func esp_para +let call_with_paren esp_func esp_para = check_return esp_func esp_para; new_pesp M_unknown P_tok (call(Deref(I_func, esp_func.any), esp_para.any.expr)) esp_func esp_para let call_and_context(e, para) priority esp_start esp_end = let context = diff --git a/perl_checker.src/parser_helper.mli b/perl_checker.src/parser_helper.mli index 9a5e2fa..fc27172 100644 --- a/perl_checker.src/parser_helper.mli +++ b/perl_checker.src/parser_helper.mli @@ -65,6 +65,8 @@ val warn_no_space : int -> unit val warn_cr : int -> unit val warn_space : int -> unit val prio_less : Types.priority * Types.priority -> bool +val prio_lo_check : + Types.priority -> Types.priority -> int * int -> Types.fromparser -> unit val prio_lo : Types.priority -> Types.fromparser Types.prio_anyexpr Types.any_spaces_pos -> @@ -182,9 +184,24 @@ val po_comments : string list ref val po_comment : string Types.any_spaces_pos -> unit val check_format_a_la_printf : string -> int -> unit val generate_pot : string -> unit -val call_func : +val call_raw : bool -> Types.fromparser * Types.fromparser list -> Types.fromparser val call : Types.fromparser * Types.fromparser list -> Types.fromparser +val call_func : + Types.fromparser Types.any_spaces_pos -> + Types.fromparser list Types.prio_anyexpr Types.any_spaces_pos -> + Types.fromparser +val check_return : + Types.fromparser Types.any_spaces_pos -> + Types.fromparser list Types.prio_anyexpr Types.any_spaces_pos -> unit +val call_no_paren : + Types.fromparser Types.any_spaces_pos -> + Types.fromparser list Types.prio_anyexpr Types.any_spaces_pos -> + Types.fromparser Types.prio_anyexpr Types.any_spaces_pos +val call_with_paren : + Types.fromparser Types.any_spaces_pos -> + Types.fromparser list Types.prio_anyexpr Types.any_spaces_pos -> + Types.fromparser Types.prio_anyexpr Types.any_spaces_pos val call_and_context : Types.fromparser * Types.fromparser list -> Types.priority -> diff --git a/perl_checker.src/types.mli b/perl_checker.src/types.mli index 98b49e5..19f8d9d 100644 --- a/perl_checker.src/types.mli +++ b/perl_checker.src/types.mli @@ -73,8 +73,8 @@ type priority = | P_tight_or | P_ternary | P_assign -| P_call_no_paren | P_comma +| P_call_no_paren | P_and | P_or | P_loose |