/usr/share/perl5/Pegex/Parser.pm is in libpegex-perl 0.64-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | package Pegex::Parser;
use Pegex::Base;
no warnings qw( recursion );
use Pegex::Input;
use Pegex::Optimizer;
use Scalar::Util;
has grammar => (required => 1);
has receiver => ();
has input => ();
has recursion_count => 0;
has iteration_count => 0;
has debug => ();
has debug_indent => ();
has debug_color => ();
has debug_got_color => ();
has debug_not_color => ();
has recursion_limit => ();
has recursion_warn_limit => ();
has iteration_limit => ();
sub BUILD {
my ($self) = @_;
$self->{throw_on_error} ||= 1;
$self->{debug} //=
$ENV{PERL_PEGEX_DEBUG} //
$Pegex::Parser::Debug // 0;
$self->{debug_indent} //=
$ENV{PERL_PEGEX_DEBUG_INDENT} //
$Pegex::Parser::DebugIndent // 1;
$self->{debug_indent} = 1 if (
not length $self->{debug_indent}
or $self->{debug_indent} =~ tr/0-9//c
or $self->{debug_indent} < 0
);
if ($self->{debug}) {
$self->{debug_color} //=
$ENV{PERL_PEGEX_DEBUG_COLOR} //
$Pegex::Parser::DebugColor // 1;
my ($got, $not);
($self->{debug_color}, $got, $not) =
split / *, */, $self->{debug_color};
$got ||= 'bright_green';
$not ||= 'bright_red';
$_ = [split ' ', $_] for ($got, $not);
$self->{debug_got_color} = $got;
$self->{debug_not_color} = $not;
my $c = $self->{debug_color} // 1;
$self->{debug_color} =
$c eq 'always' ? 1 :
$c eq 'auto' ? (-t STDERR ? 1 : 0) :
$c eq 'never' ? 0 :
$c =~ /^\d+$/ ? $c : 0;
if ($self->{debug_color}) {
require Term::ANSIColor;
if ($Term::ANSIColor::VERSION < 3.00) {
s/^bright_// for
@{$self->{debug_got_color}},
@{$self->{debug_not_color}};
}
}
}
$self->{recursion_limit} //=
$ENV{PERL_PEGEX_RECURSION_LIMIT} //
$Pegex::Parser::RecursionLimit // 0;
$self->{recursion_warn_limit} //=
$ENV{PERL_PEGEX_RECURSION_WARN_LIMIT} //
$Pegex::Parser::RecursionWarnLimit // 0;
$self->{iteration_limit} //=
$ENV{PERL_PEGEX_ITERATION_LIMIT} //
$Pegex::Parser::IterationLimit // 0;
}
# XXX Add an optional $position argument. Default to 0. This is the position
# to start parsing. Set position and farthest below to this value. Allows for
# sub-parsing. Need to somehow return the finishing position of a subparse.
# Maybe this all goes in a subparse() method.
sub parse {
my ($self, $input, $start) = @_;
$start =~ s/-/_/g if $start;
$self->{position} = 0;
$self->{farthest} = 0;
$self->{input} = (not ref $input)
? Pegex::Input->new(string => $input)
: $input;
$self->{input}->open
unless $self->{input}{_is_open};
$self->{buffer} = $self->{input}->read;
$self->{last_line_pos} = 0;
$self->{last_line} = 1;
$self->{grammar}{tree} ||= $self->{grammar}->make_tree;
my $start_rule_ref = $start ||
$self->{grammar}{tree}{'+toprule'} ||
$self->{grammar}{tree}{'TOP'} & 'TOP' or
die "No starting rule for Pegex::Parser::parse";
die "No 'receiver'. Can't parse"
unless $self->{receiver};
my $optimizer = Pegex::Optimizer->new(
parser => $self,
grammar => $self->{grammar},
receiver => $self->{receiver},
);
$optimizer->optimize_grammar($start_rule_ref);
# Add circular ref and weaken it.
$self->{receiver}{parser} = $self;
Scalar::Util::weaken($self->{receiver}{parser});
if ($self->{receiver}->can("initial")) {
$self->{rule} = $start_rule_ref;
$self->{parent} = {};
$self->{receiver}->initial();
}
local *match_next;
{
no warnings 'redefine';
*match_next = (
$self->{recursion_warn_limit} or
$self->{recursion_limit} or
$self->{iteration_limit}
) ? \&match_next_with_limit :
\&match_next_normal;
}
my $match = $self->debug ? do {
my $method = $optimizer->make_trace_wrapper(\&match_ref);
$self->$method($start_rule_ref, {'+asr' => 0});
} : $self->match_ref($start_rule_ref, {});
$self->{input}->close;
if (not $match or $self->{position} < length ${$self->{buffer}}) {
$self->throw_error("Parse document failed for some reason");
return; # In case $self->throw_on_error is off
}
if ($self->{receiver}->can("final")) {
$self->{rule} = $start_rule_ref;
$self->{parent} = {};
$match = [ $self->{receiver}->final(@$match) ];
}
$match->[0];
}
sub match_next_normal {
my ($self, $next) = @_;
my ($rule, $method, $kind, $min, $max, $assertion) =
@{$next}{'rule', 'method', 'kind', '+min', '+max', '+asr'};
my ($position, $match, $count) =
($self->{position}, [], 0);
while (my $return = $method->($self, $rule, $next)) {
$position = $self->{position} unless $assertion;
$count++;
push @$match, @$return;
last if $max == 1;
}
if (not $count and $min == 0 and $kind eq 'all') {
$match = [[]];
}
if ($max != 1) {
if ($next->{-flat}) {
$match = [ map { (ref($_) eq 'ARRAY') ? (@$_) : ($_) } @$match ];
}
else {
$match = [$match]
}
}
my $result = ($count >= $min and (not $max or $count <= $max))
^ ($assertion == -1);
if (not($result) or $assertion) {
$self->{farthest} = $position
if ($self->{position} = $position) > $self->{farthest};
}
($result ? $next->{'-skip'} ? [] : $match : 0);
}
sub match_next_with_limit {
my ($self, $next) = @_;
sub limit_msg {
"Deep recursion ($_[0] levels) on Pegex::Parser::match_next\n";
}
$self->{iteration_count}++;
$self->{recursion_count}++;
if (
$self->{recursion_limit} and
$self->{recursion_count} >= $self->{recursion_limit}
) { die limit_msg $self->{recursion_count} }
elsif (
$self->{recursion_warn_limit} and
not ($self->{recursion_count} % $self->{recursion_warn_limit})
) { warn limit_msg $self->{recursion_count} }
elsif (
$self->{iteration_limit} and
$self->{iteration_count} > $self->{iteration_limit}
) { die "Pegex iteration limit of $self->{iteration_limit} reached." }
my $result = $self->match_next_normal($next);
$self->{recursion_count}--;
return $result;
}
sub match_rule {
my ($self, $position, $match) = (@_, []);
$self->{position} = $position;
$self->{farthest} = $position
if $position > $self->{farthest};
$match = [ $match ] if @$match > 1;
my ($ref, $parent) = @{$self}{'rule', 'parent'};
my $rule = $self->{grammar}{tree}{$ref}
or die "No rule defined for '$ref'";
[ $rule->{action}->($self->{receiver}, @$match) ];
}
sub match_ref {
my ($self, $ref, $parent) = @_;
my $rule = $self->{grammar}{tree}{$ref}
or die "No rule defined for '$ref'";
my $match = $self->match_next($rule) or return;
return $Pegex::Constant::Dummy unless $rule->{action};
@{$self}{'rule', 'parent'} = ($ref, $parent);
# XXX Possible API mismatch.
# Not sure if we should "splat" the $match.
[ $rule->{action}->($self->{receiver}, @$match) ];
}
sub match_rgx {
my ($self, $regexp) = @_;
my $buffer = $self->{buffer};
pos($$buffer) = $self->{position};
$$buffer =~ /$regexp/g or return;
$self->{position} = pos($$buffer);
$self->{farthest} = $self->{position}
if $self->{position} > $self->{farthest};
no strict 'refs';
my $captures = [ map $$_, 1..$#+ ];
$captures = [ $captures ] if $#+ > 1;
return $captures;
}
sub match_all {
my ($self, $list) = @_;
my $position = $self->{position};
my $set = [];
my $len = 0;
for my $elem (@$list) {
if (my $match = $self->match_next($elem)) {
if (not ($elem->{'+asr'} or $elem->{'-skip'})) {
push @$set, @$match;
$len++;
}
}
else {
$self->{farthest} = $position
if ($self->{position} = $position) > $self->{farthest};
return;
}
}
$set = [ $set ] if $len > 1;
return $set;
}
sub match_any {
my ($self, $list) = @_;
for my $elem (@$list) {
if (my $match = $self->match_next($elem)) {
return $match;
}
}
return;
}
sub match_err {
my ($self, $error) = @_;
$self->throw_error($error);
}
sub trace {
my ($self, $action) = @_;
my $indent = ($action =~ /^try_/) ? 1 : 0;
$self->{indent} ||= 0;
$self->{indent}-- unless $indent;
$action = (
$action =~ m/got_/ ?
Term::ANSIColor::colored($self->{debug_got_color}, $action) :
$action =~ m/not_/ ?
Term::ANSIColor::colored($self->{debug_not_color}, $action) :
$action
) if $self->{debug_color};
print STDERR ' ' x ($self->{indent} * $self->{debug_indent});
$self->{indent}++ if $indent;
my $snippet = substr(${$self->{buffer}}, $self->{position});
$snippet = substr($snippet, 0, 30) . "..."
if length $snippet > 30;
$snippet =~ s/\n/\\n/g;
print STDERR sprintf("%-30s", $action) .
($indent ? " >$snippet<\n" : "\n");
}
sub throw_error {
my ($self, $msg) = @_;
$@ = $self->{error} = $self->format_error($msg);
return undef unless $self->{throw_on_error};
require Carp;
Carp::croak($self->{error});
}
sub format_error {
my ($self, $msg) = @_;
my $buffer = $self->{buffer};
my $position = $self->{farthest};
my $real_pos = $self->{position};
my $line = $self->line($position);
my $column = $position - rindex($$buffer, "\n", $position);
my $pretext = substr(
$$buffer,
$position < 50 ? 0 : $position - 50,
$position < 50 ? $position : 50
);
my $context = substr($$buffer, $position, 50);
$pretext =~ s/.*\n//gs;
$context =~ s/\n/\\n/g;
return <<"...";
Error parsing Pegex document:
msg: $msg
line: $line
column: $column
context: $pretext$context
${\ (' ' x (length($pretext) + 10) . '^')}
position: $position ($real_pos pre-lookahead)
...
}
# TODO Move this to a Parser helper role/subclass
sub line_column {
my ($self, $position) = @_;
$position ||= $self->{position};
my $buffer = $self->{buffer};
my $line = $self->line($position);
my $column = $position - rindex($$buffer, "\n", $position);
return [$line, $column];
}
sub line {
my ($self, $position) = @_;
$position ||= $self->{position};
my $buffer = $self->{buffer};
my $last_line = $self->{last_line};
my $last_line_pos = $self->{last_line_pos};
my $len = $position - $last_line_pos;
if ($len == 0) {
return $last_line;
}
my $line;
if ($len < 0) {
$line = $last_line - scalar substr($$buffer, $position, -$len) =~ tr/\n//;
} else {
$line = $last_line + scalar substr($$buffer, $last_line_pos, $len) =~ tr/\n//;
}
$self->{last_line} = $line;
$self->{last_line_pos} = $position;
return $line;
}
# XXX Need to figure out what uses this. (sample.t)
{
package Pegex::Constant;
our $Null = [];
our $Dummy = [];
}
1;
|