Permalink
Browse files

OTWO-1300 Improves *.pl disambiguation to ignore smileys :-)

Smiley faces in Perl strings and comments look similar to Prolog
rule syntax. This patch makes two improvements:

 - Better detection of perl shebangs (#!%PERL% now recognized)
 - A prolog ':-' token must be followed by a space or a newline
  • Loading branch information...
1 parent 140f487 commit 8aa2a6a058d65e414c24cbe53b7847d98a2945cf @robinluckey robinluckey committed Feb 8, 2012
View
@@ -769,16 +769,23 @@ const char *disambiguate_pp(SourceFile *sourcefile) {
}
const char *disambiguate_pl(SourceFile *sourcefile) {
- // Attempt to detect based on file contents.
char *contents = ohcount_sourcefile_get_contents(sourcefile);
- if (contents && strstr(contents, "#!/usr/bin/perl"))
- return LANG_PERL;
- else if (contents && strstr(contents, "#!/usr/local/bin/perl"))
+ if (!contents)
+ return NULL;
+
+ // Check for a perl shebang on first line of file
+ const char *error;
+ int erroffset;
+ pcre *re = pcre_compile("#![^\\n]*perl", PCRE_CASELESS, &error, &erroffset, NULL);
+ if (pcre_exec(re, NULL, contents, mystrnlen(contents, 100), 0, PCRE_ANCHORED, NULL, 0) > -1)
return LANG_PERL;
- else if (contents && strstr(contents, ":-"))
+
+ // Check for prolog :- rules
+ if (strstr(contents, ":- ") || strstr(contents, ":-\n"))
return LANG_PROLOG;
- else
- return LANG_PERL;
+
+ // Perl by default.
+ return LANG_PERL;
}
#define QMAKE_SOURCES_SPACE "SOURCES +="
@@ -0,0 +1,3 @@
+#!%PERL%
+% This is prolog code, but the shebang says perl, so we respect the shebang and choose perl.
+Head :- Body.
@@ -0,0 +1,2 @@
+# This is not prolog. Do not be confused by the smiley, which looks like a Prolog rule.
+print "Hello, world :-)\n"
@@ -86,6 +86,8 @@ void test_detector_disambiguate_pl() {
ASSERT_DETECT(LANG_PERL, "foo_perl1.pl");
ASSERT_DETECT(LANG_PERL, "foo_perl2.pl");
ASSERT_DETECT(LANG_PROLOG, "foo_prolog1.pl");
+ ASSERT_DETECT(LANG_PERL, "perl_with_smiley.pl");
+ ASSERT_DETECT(LANG_PERL, "perl_shebang_prolog_body.pl");
}
void test_detector_disambiguate_pro() {

0 comments on commit 8aa2a6a

Please sign in to comment.