perltest.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #! /usr/bin/env perl
  2. # Program for testing regular expressions with perl to check that PCRE handles
  3. # them the same. This version needs to have "use utf8" at the start for running
  4. # the UTF-8 tests, but *not* for the other tests. The only way I've found for
  5. # doing this is to cat this line in explicitly in the RunPerlTest script. I've
  6. # also used this method to supply "require Encode" for the UTF-8 tests, so that
  7. # the main test will still run where Encode is not installed.
  8. #use utf8;
  9. #require Encode;
  10. # Function for turning a string into a string of printing chars.
  11. sub pchars {
  12. my($t) = "";
  13. if ($utf8)
  14. {
  15. @p = unpack('U*', $_[0]);
  16. foreach $c (@p)
  17. {
  18. if ($c >= 32 && $c < 127) { $t .= chr $c; }
  19. else { $t .= sprintf("\\x{%02x}", $c);
  20. }
  21. }
  22. }
  23. else
  24. {
  25. foreach $c (split(//, $_[0]))
  26. {
  27. if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
  28. else { $t .= sprintf("\\x%02x", ord $c); }
  29. }
  30. }
  31. $t;
  32. }
  33. # Read lines from named file or stdin and write to named file or stdout; lines
  34. # consist of a regular expression, in delimiters and optionally followed by
  35. # options, followed by a set of test data, terminated by an empty line.
  36. # Sort out the input and output files
  37. if (@ARGV > 0)
  38. {
  39. open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
  40. $infile = "INFILE";
  41. }
  42. else { $infile = "STDIN"; }
  43. if (@ARGV > 1)
  44. {
  45. open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n";
  46. $outfile = "OUTFILE";
  47. }
  48. else { $outfile = "STDOUT"; }
  49. printf($outfile "Perl $] Regular Expressions\n\n");
  50. # Main loop
  51. NEXT_RE:
  52. for (;;)
  53. {
  54. printf " re> " if $infile eq "STDIN";
  55. last if ! ($_ = <$infile>);
  56. printf $outfile "$_" if $infile ne "STDIN";
  57. next if ($_ =~ /^\s*$/ || $_ =~ /^< forbid/);
  58. $pattern = $_;
  59. while ($pattern !~ /^\s*(.).*\1/s)
  60. {
  61. printf " > " if $infile eq "STDIN";
  62. last if ! ($_ = <$infile>);
  63. printf $outfile "$_" if $infile ne "STDIN";
  64. $pattern .= $_;
  65. }
  66. chomp($pattern);
  67. $pattern =~ s/\s+$//;
  68. # The private /+ modifier means "print $' afterwards".
  69. $showrest = ($pattern =~ s/\+(?=[a-zA-Z]*$)//);
  70. # A doubled version is used by pcretest to print remainders after captures
  71. $pattern =~ s/\+(?=[a-zA-Z]*$)//;
  72. # Remove /8 from a UTF-8 pattern.
  73. $utf8 = $pattern =~ s/8(?=[a-zA-Z]*$)//;
  74. # Remove /J from a pattern with duplicate names.
  75. $pattern =~ s/J(?=[a-zA-Z]*$)//;
  76. # Remove /K from a pattern (asks pcretest to check MARK data) */
  77. $pattern =~ s/K(?=[a-zA-Z]*$)//;
  78. # /W asks pcretest to set PCRE_UCP; change this to /u for Perl
  79. $pattern =~ s/W(?=[a-zA-Z]*$)/u/;
  80. # Remove /S or /SS from a pattern (asks pcretest to study or not to study)
  81. $pattern =~ s/S(?=[a-zA-Z]*$)//g;
  82. # Remove /Y and /O from a pattern (disable PCRE optimizations)
  83. $pattern =~ s/[YO](?=[a-zA-Z]*$)//;
  84. # Check that the pattern is valid
  85. eval "\$_ =~ ${pattern}";
  86. if ($@)
  87. {
  88. printf $outfile "Error: $@";
  89. if ($infile != "STDIN")
  90. {
  91. for (;;)
  92. {
  93. last if ! ($_ = <$infile>);
  94. last if $_ =~ /^\s*$/;
  95. }
  96. }
  97. next NEXT_RE;
  98. }
  99. # If the /g modifier is present, we want to put a loop round the matching;
  100. # otherwise just a single "if".
  101. $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
  102. # If the pattern is actually the null string, Perl uses the most recently
  103. # executed (and successfully compiled) regex is used instead. This is a
  104. # nasty trap for the unwary! The PCRE test suite does contain null strings
  105. # in places - if they are allowed through here all sorts of weird and
  106. # unexpected effects happen. To avoid this, we replace such patterns with
  107. # a non-null pattern that has the same effect.
  108. $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
  109. # Read data lines and test them
  110. for (;;)
  111. {
  112. printf "data> " if $infile eq "STDIN";
  113. last NEXT_RE if ! ($_ = <$infile>);
  114. chomp;
  115. printf $outfile "$_\n" if $infile ne "STDIN";
  116. s/\s+$//; # Remove trailing space
  117. s/^\s+//; # Remove leading space
  118. s/\\Y//g; # Remove \Y (pcretest flag to set PCRE_NO_START_OPTIMIZE)
  119. last if ($_ eq "");
  120. $x = eval "\"$_\""; # To get escapes processed
  121. # Empty array for holding results, ensure $REGERROR and $REGMARK are
  122. # unset, then do the matching.
  123. @subs = ();
  124. $pushes = "push \@subs,\$&;" .
  125. "push \@subs,\$1;" .
  126. "push \@subs,\$2;" .
  127. "push \@subs,\$3;" .
  128. "push \@subs,\$4;" .
  129. "push \@subs,\$5;" .
  130. "push \@subs,\$6;" .
  131. "push \@subs,\$7;" .
  132. "push \@subs,\$8;" .
  133. "push \@subs,\$9;" .
  134. "push \@subs,\$10;" .
  135. "push \@subs,\$11;" .
  136. "push \@subs,\$12;" .
  137. "push \@subs,\$13;" .
  138. "push \@subs,\$14;" .
  139. "push \@subs,\$15;" .
  140. "push \@subs,\$16;" .
  141. "push \@subs,\$'; }";
  142. undef $REGERROR;
  143. undef $REGMARK;
  144. eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
  145. if ($@)
  146. {
  147. printf $outfile "Error: $@\n";
  148. next NEXT_RE;
  149. }
  150. elsif (scalar(@subs) == 0)
  151. {
  152. printf $outfile "No match";
  153. if (defined $REGERROR && $REGERROR != 1)
  154. { printf $outfile (", mark = %s", &pchars($REGERROR)); }
  155. printf $outfile "\n";
  156. }
  157. else
  158. {
  159. while (scalar(@subs) != 0)
  160. {
  161. printf $outfile (" 0: %s\n", &pchars($subs[0]));
  162. printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest;
  163. $last_printed = 0;
  164. for ($i = 1; $i <= 16; $i++)
  165. {
  166. if (defined $subs[$i])
  167. {
  168. while ($last_printed++ < $i-1)
  169. { printf $outfile ("%2d: <unset>\n", $last_printed); }
  170. printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
  171. $last_printed = $i;
  172. }
  173. }
  174. splice(@subs, 0, 18);
  175. }
  176. # It seems that $REGMARK is not marked as UTF-8 even when use utf8 is
  177. # set and the input pattern was a UTF-8 string. We can, however, force
  178. # it to be so marked.
  179. if (defined $REGMARK && $REGMARK != 1)
  180. {
  181. $xx = $REGMARK;
  182. $xx = Encode::decode_utf8($xx) if $utf8;
  183. printf $outfile ("MK: %s\n", &pchars($xx));
  184. }
  185. }
  186. }
  187. }
  188. # printf $outfile "\n";
  189. # End