/usr/share/perl5/Catmandu/Fix/Parser.pm is in libcatmandu-perl 1.0700-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 | package Catmandu::Fix::Parser;
use Catmandu::Sane;
our $VERSION = '1.07';
use Catmandu::Util
qw(check_value is_array_ref is_instance is_able require_package);
use Moo;
use namespace::clean;
extends 'Parser::MGC';
sub FOREIGNBUILDARGS {
my ($class, $opts) = @_;
$opts->{toplevel} = 'parse_statements';
%$opts;
}
sub parse {
my ($self, $source) = @_;
check_value($source);
try {
$self->from_string($source);
}
catch {
my $err = $_;
if (is_instance($err, 'Catmandu::Error')) {
$err->set_source($source) if is_able($err, 'set_source');
$err->throw;
}
Catmandu::FixParseError->throw(message => $err, source => $source,);
};
}
sub pattern_comment {
qr/#[^\n]*/;
}
sub parse_statements {
my ($self) = @_;
$self->sequence_of('parse_statement');
}
sub parse_statement {
my ($self) = @_;
my $statement = $self->any_of(
'parse_filter', 'parse_if', 'parse_unless', 'parse_bind',
'parse_fix',
);
# support deprecated separator
$self->maybe_expect(';');
$statement;
}
sub parse_filter {
my ($self) = @_;
my $type = $self->token_kw('select', 'reject');
my $name = $self->parse_name;
my $args = $self->parse_arguments;
# support deprecated separator
$self->maybe_expect(';');
$self->_build_condition(
$name, $args,
$type eq 'reject',
require_package('Catmandu::Fix::reject')->new
);
}
sub parse_if {
my ($self) = @_;
my $type = $self->token_kw('if');
my $name = $self->parse_name;
my $args = $self->parse_arguments;
# support deprecated separator
$self->maybe_expect(';');
my $cond
= $self->_build_condition($name, $args, 1, $self->parse_statements);
my $elsif_conditions = $self->sequence_of(
sub {
$self->token_kw('elsif');
my $name = $self->parse_name;
my $args = $self->parse_arguments;
# support deprecated separator
$self->maybe_expect(';');
$self->_build_condition($name, $args, 1, $self->parse_statements);
}
);
my $else_fixes = $self->maybe(
sub {
$self->expect('else');
$self->parse_statements;
}
);
$self->expect('end');
# support deprecated separator
$self->maybe_expect(';');
my $last_cond = $cond;
if ($elsif_conditions) {
for my $c (@$elsif_conditions) {
$last_cond->fail_fixes([$c]);
$last_cond = $c;
}
}
if ($else_fixes) {
$last_cond->fail_fixes($else_fixes);
}
$cond;
}
sub parse_unless {
my ($self) = @_;
my $type = $self->token_kw('unless');
my $name = $self->parse_name;
my $args = $self->parse_arguments;
# support deprecated separator
$self->maybe_expect(';');
my $cond
= $self->_build_condition($name, $args, 0, $self->parse_statements);
$self->expect('end');
# support deprecated separator
$self->maybe_expect(';');
$cond;
}
sub parse_bind {
my ($self) = @_;
my $type = $self->token_kw('bind', 'do', 'doset');
my $name = $self->parse_name;
my $args = $self->parse_arguments;
# support deprecated separator
$self->maybe_expect(';');
my $bind = $self->_build_bind($name, $args, $type eq 'doset',
$self->parse_statements);
$self->expect('end');
# support deprecated separator
$self->maybe_expect(';');
$bind;
}
sub parse_fix {
my ($self) = @_;
my $lft_name = $self->parse_name;
my $lft_args = $self->parse_arguments;
my $bool = $self->maybe(
sub {
$self->any_of(
sub {$self->expect(qr/and|&&/); 1},
sub {$self->expect(qr/or|\|\|/); 0},
);
}
);
my $fix;
if (defined $bool) {
$self->commit;
my $rgt_name = $self->parse_name;
my $rgt_args = $self->parse_arguments;
$fix = $self->_build_condition($lft_name, $lft_args, $bool,
$self->_build_fix($rgt_name, $rgt_args));
}
else {
$fix = $self->_build_fix($lft_name, $lft_args);
}
# support deprecated separator
$self->maybe_expect(';');
$fix;
}
sub parse_name {
my ($self) = @_;
$self->generic_token(name => qr/[a-z][_0-9a-zA-Z]*/);
}
sub parse_arguments {
my ($self) = @_;
$self->expect('(');
my $args = $self->list_of(qr/[,:]|=>/, 'parse_value');
$self->expect(')');
$args;
}
sub parse_value {
my ($self) = @_;
$self->any_of('parse_double_quoted_string', 'parse_single_quoted_string',
'parse_bare_string',);
}
sub parse_bare_string {
my ($self) = @_;
$self->generic_token(bare_string => qr/[^\s\\,;:=>()"']+/);
}
sub parse_single_quoted_string {
my ($self) = @_;
my $str = $self->generic_token(string => qr/'(?:\\?+.)*?'/);
$str = substr($str, 1, length($str) - 2);
$str =~ s{\\'}{'}gxms;
$str;
}
sub parse_double_quoted_string {
my ($self) = @_;
my $str = $self->generic_token(string => qr/"(?:\\?+.)*?"/);
$str = substr($str, 1, length($str) - 2);
if (index($str, '\\') != -1) {
$str =~ s/\\u([0-9A-Fa-f]{4})/chr(hex($1))/egxms;
$str =~ s/\\n/\n/gxms;
$str =~ s/\\r/\r/gxms;
$str =~ s/\\b/\b/gxms;
$str =~ s/\\f/\f/gxms;
$str =~ s/\\t/\t/gxms;
$str =~ s/\\\\/\\/gxms;
$str =~ s{\\/}{/}gxms;
$str =~ s{\\"}{"}gxms;
}
$str;
}
sub _build_condition {
my ($self, $name, $args, $pass, $fixes) = @_;
$fixes = [$fixes] if !is_array_ref($fixes);
my $cond = $self->_build_fix_ns($name, 'Catmandu::Fix::Condition', $args);
if ($pass) {
$cond->pass_fixes($fixes);
}
else {
$cond->fail_fixes($fixes);
}
$cond;
}
sub _build_bind {
my ($self, $name, $args, $return, $fixes) = @_;
$fixes = [$fixes] if !is_array_ref($fixes);
my $bind = $self->_build_fix_ns($name, 'Catmandu::Fix::Bind', $args);
$bind->__return__($return);
$bind->__fixes__($fixes);
$bind;
}
sub _build_fix {
my ($self, $name, $args) = @_;
$self->_build_fix_ns($name, 'Catmandu::Fix', $args);
}
sub _build_fix_ns {
my ($self, $name, $ns, $args) = @_;
my $pkg;
try {
$pkg = require_package($name, $ns);
}
catch_case [
'Catmandu::NoSuchPackage' => sub {
Catmandu::NoSuchFixPackage->throw(
message => "No such fix package: $name",
package_name => $_->package_name,
fix_name => $name,
);
},
];
try {
$pkg->new(@$args);
}
catch {
$_->throw if is_instance($_, 'Catmandu::Error');
Catmandu::BadFixArg->throw(
message => $_,
package_name => $pkg,
fix_name => $name,
);
};
}
1;
__END__
=pod
=head1 NAME
Catmandu::Fix::Parser - the parser of the Catmandu::Fix language
=head1 SYNOPSIS
use Catmandu::Sane;
use Catmandu::Fix::Parser;
use Catmandu::Fix;
use Data::Dumper;
my $parser = Catmandu::Fix::Parser->new;
my $fixes;
try {
$fixes = $parser->parse(<<EOF);
add_field(test,123)
EOF
}
catch {
printf "[%s]\nscript:\n%s\nerror: %s\n"
, ref($_)
, $_->source
, $_->message;
};
my $fixer = Catmandu::Fix->new(fixes => $fixes);
print Dumper($fixer->fix({}));
=head1 DESCRIPTION
Programmers are discouraged to use the Catmandu::Parser directly in code but
use the Catmandu package that provides the same functionality:
use Catmandu;
my $fixer = Catmandu->fixer(<<EOF);
add_field(test,123)
EOF
print Dumper($fixer->fix({}));
=head1 METHODS
=head2 new()
Create a new Catmandu::Fix parser
=head2 parse($string)
Reads a string and returns a blessed object with parsed
Catmandu::Fixes. Throws an Catmandu::ParseError on failure.
=head1 SEE ALSO
L<Catmandu::Fix>
Or consult the webpages below for more information on the Catmandu::Fix language
http://librecat.org/Catmandu/#fixes
http://librecat.org/Catmandu/#fix-language
=cut
|