| 1 | package Pugs::Runtime::Match::HsBridge; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Pugs::Grammar::Base (); | ||
| 7 | use Pugs::Compiler::Regex (); | ||
| 8 | use base 'Pugs::Grammar::Base'; | ||
| 9 | |||
| 10 | BEGIN { local $@; eval { require 'utf8_heavy.pl' } } | ||
| 11 | |||
| 12 | use ops ($ENV{PUGS_SAFEMODE} ? (':default', 'binmode', 'entereval') : ()); | ||
| 13 | |||
| 14 | sub __RUN__ { | ||
| 15 | my $self = shift; | ||
| 16 | my $match_text = shift; | ||
| 17 | my $rule_text = shift; | ||
| 18 | my %subrules = @_; | ||
| 19 | |||
| 20 | while (my ($name, $body) = each %subrules) { | ||
| 21 | my %sub_opts = (grammar => __PACKAGE__); | ||
| 22 | ($1 and $sub_opts{$1} = 1) while $body =~ s/^:(\w*)\(1?\)\[(.*)\]\z/$2/s; | ||
| 23 | Pugs::Compiler::Regex->reinstall( $name => $body, \%sub_opts ); | ||
| 24 | } | ||
| 25 | |||
| 26 | my %opts = (grammar => __PACKAGE__); | ||
| 27 | ($1 and $opts{$1} = 1) while $rule_text =~ s/^:(\w*)\(1?\)\[(.*)\]\z/$2/s; | ||
| 28 | |||
| 29 | # L<S05/Modifiers/"The C<:ratchet> modifier also implies that the anchoring"> | ||
| 30 | if ( $opts{ratchet} ) { | ||
| 31 | $rule_text = "^$rule_text" unless $opts{p} or $opts{pos}; | ||
| 32 | $rule_text .= '$' unless $opts{c} or $opts{continue}; | ||
| 33 | } | ||
| 34 | |||
| 35 | local $SIG{__WARN__} = sub { 1 }; | ||
| 36 | |||
| 37 | my $rule = Pugs::Compiler::Regex->compile( $rule_text, \%opts ); | ||
| 38 | my $match = $rule->match( $match_text ); | ||
| 39 | |||
| 40 | return $match->dump_hs; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub __CMD__ { | ||
| 44 | local $| = 1; | ||
| 45 | |||
| 46 | # Command line shell interface - compatible with run_pge.pir | ||
| 47 | if ($] >= 5.007) { | ||
| 48 | binmode STDIN, ':utf8'; | ||
| 49 | binmode STDOUT, ':utf8'; | ||
| 50 | } | ||
| 51 | |||
| 52 | my @subrules; | ||
| 53 | while (<STDIN>) { | ||
| 54 | #/^(\w+) (\d+) (\d+)/ or die "Unrecognized command: $_"; | ||
| 55 | /^(\w+) \d+ \d+/ or die "Unrecognized command: $_"; | ||
| 56 | my $cmd = $1; # my ($cmd, $sz1, $sz2) = ($1, $2, $3); | ||
| 57 | |||
| 58 | my $line1 = <STDIN>; chomp($line1); $line1 =~ s{\\(?:(\\)|(n))}{$1 ? $1 : "\n"}eg; | ||
| 59 | my $line2 = <STDIN>; chomp($line2); $line2 =~ s{\\(?:(\\)|(n))}{$1 ? $1 : "\n"}eg; | ||
| 60 | |||
| 61 | if ($cmd eq 'add_rule') { | ||
| 62 | push @subrules, $line1, $line2; | ||
| 63 | } | ||
| 64 | elsif ($cmd eq 'match') { | ||
| 65 | if (my $rv = eval { __PACKAGE__->__RUN__($line1, $line2, @subrules) }) { | ||
| 66 | my $len = bytes::length($rv)+1; | ||
| 67 | print "OK $len\n"; | ||
| 68 | print "$rv\n\n"; | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | my $err = $@; | ||
| 72 | $err =~ s/([\\\n])/\\$1/g; | ||
| 73 | print "$err\n"; | ||
| 74 | } | ||
| 75 | @subrules = (); | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | die "Unrecognized command: $cmd"; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; |