summaryrefslogtreecommitdiffstats
path: root/perl_checker.src/lexer.mll
diff options
context:
space:
mode:
authorPascal Rigaux <pixel@mandriva.com>2003-10-01 10:22:24 +0000
committerPascal Rigaux <pixel@mandriva.com>2003-10-01 10:22:24 +0000
commitdf929f792ac9cfe60b38601cc8492dedceb8de95 (patch)
tree4fb22609a3f70b4e9b9a0a0e6de2ead28fadfa9e /perl_checker.src/lexer.mll
parent4c97e9229856a5ff3654aa0d1549d441b91c25b9 (diff)
downloadperl_checker-df929f792ac9cfe60b38601cc8492dedceb8de95.tar
perl_checker-df929f792ac9cfe60b38601cc8492dedceb8de95.tar.gz
perl_checker-df929f792ac9cfe60b38601cc8492dedceb8de95.tar.bz2
perl_checker-df929f792ac9cfe60b38601cc8492dedceb8de95.tar.xz
perl_checker-df929f792ac9cfe60b38601cc8492dedceb8de95.zip
suggest using 'xxx"xxx' instead of "xxx\"xxx" when there is no interpolation
in the string and no special \x
Diffstat (limited to 'perl_checker.src/lexer.mll')
-rw-r--r--perl_checker.src/lexer.mll37
1 files changed, 28 insertions, 9 deletions
diff --git a/perl_checker.src/lexer.mll b/perl_checker.src/lexer.mll
index 526843f..39a3c02 100644
--- a/perl_checker.src/lexer.mll
+++ b/perl_checker.src/lexer.mll
@@ -234,6 +234,7 @@ let raw_here_doc_next_line mark =
let delimit_char = ref '/'
type string_escape_kinds = Double_quote | Qq | Delimited | Here_doc
let string_escape_kind = ref Double_quote
+let string_escape_useful = ref (Left false)
let not_ok_for_match = ref (-1)
let string_nestness = ref 0
let string_is_i18n = ref false
@@ -273,9 +274,21 @@ let raw_ins_to_string t lexbuf =
not_ok_for_match := lexeme_end lexbuf;
RAW_STRING(s, pos)
let ins_to_string t lexbuf =
+ string_escape_useful := Left false ;
let s, pos = ins t lexbuf in
- string_is_i18n := false ;
+
+ (match !string_escape_useful, s with
+ | Right c, [ _, [] ] ->
+ let msg =
+ if c = "\"" then
+ "or you can replace \"xxx\\" ^ c ^ "xxx\" with 'xxx" ^ c ^ "xxx'"
+ else
+ "you can replace \"xxx\\" ^ c ^ "xxx\" with 'xxx" ^ c ^ "xxx'" in
+ warn_with_pos pos msg
+ | _ -> ());
+
not_ok_for_match := lexeme_end lexbuf;
+ string_is_i18n := false ;
STRING(s, pos)
let next_s s t lexbuf =
@@ -710,6 +723,7 @@ and string = parse
add_a_new_line(lexeme_end lexbuf);
next string lexbuf
}
+| "'" { string_escape_useful := Left true ; next string lexbuf }
| [^ '\n' '\\' '"' '$' '@']+ { next string lexbuf }
| eof { die_in_string lexbuf "Unterminated_string" }
@@ -810,22 +824,27 @@ and raw_here_doc = parse
and string_escape = parse
-| ['0'-'9'] { next_s (String.make 1 (Char.chr (int_of_string (lexeme lexbuf)))) (Stack.pop next_rule) lexbuf }
-| '\\'{ next_s "\\" (Stack.pop next_rule) lexbuf }
-| 'n' { next_s "\n" (Stack.pop next_rule) lexbuf }
-| 't' { next_s "\t" (Stack.pop next_rule) lexbuf }
-| "x{" [^ '}']* '}' { hex_in_string lexbuf next_rule (skip_n_char_ 2 1 (lexeme lexbuf)) }
-| 'x' [^ '{'] _ { hex_in_string lexbuf next_rule (skip_n_char 1 (lexeme lexbuf)) }
+| ['0'-'9'] { string_escape_useful := Left true; next_s (String.make 1 (Char.chr (int_of_string (lexeme lexbuf)))) (Stack.pop next_rule) lexbuf }
+| 'n' { string_escape_useful := Left true; next_s "\n" (Stack.pop next_rule) lexbuf }
+| 't' { string_escape_useful := Left true; next_s "\t" (Stack.pop next_rule) lexbuf }
+| "x{" [^ '}']* '}' { string_escape_useful := Left true; hex_in_string lexbuf next_rule (skip_n_char_ 2 1 (lexeme lexbuf)) }
+| 'x' [^ '{'] _ { string_escape_useful := Left true; hex_in_string lexbuf next_rule (skip_n_char 1 (lexeme lexbuf)) }
| '\n' { die lexbuf "do not use \"\\\" before end-of-line, it's useless and generally bad" }
-| ['b' 'f' '$' '@' '%' 'a' 'r' '{' '['] { next_s ("\\" ^ lexeme lexbuf) (Stack.pop next_rule) lexbuf }
+| '\\'{ next_s "\\" (Stack.pop next_rule) lexbuf }
+| ['b' 'f' '$' '@' '%' 'a' 'r' '{' '['] {
+ if !string_escape_useful = Left false then string_escape_useful := Right (lexeme lexbuf) ;
+ next_s ("\\" ^ lexeme lexbuf) (Stack.pop next_rule) lexbuf
+ }
| _ {
let c = lexeme lexbuf in
(match !string_escape_kind with
| Double_quote ->
if c <> "\"" then
warn_escape_unneeded lexbuf c
- else if not !string_is_i18n then
+ else if not !string_is_i18n then (
+ if !string_escape_useful = Left false then string_escape_useful := Right c ;
warn lexbuf "you can replace \"xxx\\\"xxx\" with qq(xxx\"xxx), that way you don't need to escape <\">"
+ )
| Qq -> if c <> "(" && c <> ")" then warn_escape_unneeded lexbuf c
| Here_doc -> warn_escape_unneeded lexbuf c
| Delimited -> if c = String.make 1 !delimit_char then