/usr/share/perl5/Catmandu/Fix/Parser.pm is in libcatmandu-perl 0.9206-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 | package Catmandu::Fix::Parser;
=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)
=head2 parse($file)
Reads a string or a file 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
use Catmandu::Sane;
use Marpa::R2;
use Data::Dumper;
use Catmandu;
use Catmandu::Util qw(check_value read_file);
use Moo;
with 'Catmandu::Logger';
my $GRAMMAR = <<'GRAMMAR';
:default ::= action => ::array
:start ::= fixes
:discard ~ discard
fixes ::= expression*
expression ::= old_if action => ::first
| old_unless action => ::first
| if action => ::first
| if_else action => ::first
| unless action => ::first
| select action => ::first
| reject action => ::first
| doset action => ::first
| do action => ::first
| fix action => ::first
old_if ::= old_if_condition fixes ('end()') bless => IfElse
old_unless ::= old_unless_condition fixes ('end()') bless => Unless
if ::= ('if') condition fixes ('end') bless => IfElse
if_else ::= ('if') condition fixes ('else') fixes ('end') bless => IfElse
unless ::= ('unless') condition fixes ('end') bless => Unless
select ::= ('select') condition bless => Select
reject ::= ('reject') condition bless => Reject
old_if_condition ::= old_if_name ('(') args (')') bless => OldCondition
old_unless_condition ::= old_unless_name ('(') args (')') bless => OldCondition
condition ::= name ('(') args (')') bless => Condition
doset ::= ('doset') bind fixes ('end') bless => DoSet
do ::= ('do') bind fixes ('end') bless => Do
bind ::= name ('(') args (')') bless => Bind
fix ::= name ('(') args (')') bless => Fix
args ::= arg* separator => sep
arg ::= int bless => Int
| qq_string bless => DoubleQuotedString
| q_string bless => SingleQuotedString
| bare_string bless => BareString
old_if_name ~ 'if_' [a-z] name_rest
old_unless_name ~ 'unless_' [a-z] name_rest
name ~ [a-z] name_rest
name_rest ~ [_\da-zA-Z]*
int ~ digits
| '-' digits
digits ~ [\d]+
qq_string ~ '"' qq_chars '"'
qq_chars ~ qq_char*
qq_char ~ [^"] | '\"'
q_string ~ ['] q_chars [']
q_chars ~ q_char*
q_char ~ [^'] | '\' [']
bare_string ~ [^\s\\\,;:=>()"']+
discard ~ comment | whitespace | old_terminator
whitespace ~ [\s]+
comment ~ '#' comment_chars
comment_chars ~ comment_char*
comment_char ~ [^\n]
old_terminator ~ ';'
sep ~ [,:]
| '=>'
GRAMMAR
sub parse {
state $grammar = Marpa::R2::Scanless::G->new({
bless_package => __PACKAGE__,
source => \$GRAMMAR,
});
my ($self, $source) = @_;
check_value($source);
if ($source =~ /[^\s]/ && $source !~ /\(/) {
$source = read_file($source);
}
my $val;
try {
my $recognizer = Marpa::R2::Scanless::R->new({grammar => $grammar});
$recognizer->read(\$source);
$val = ${$recognizer->value};
$self->log->debugf(Dumper($val)) if $self->log->is_debug();
[ map {$_->reify} @$val ];
}
catch {
my $message = "parse error";
if ($_ =~ /coercion for "_fixer"/) {
$message .= " - wrong number of arguments in your fix command";
}
Catmandu::ParseError->throw(message => $message, source => $source);
};
}
sub Catmandu::Fix::Parser::IfElse::reify {
my $cond = $_[0]->[0]->reify;
my $pass_fixes = $_[0]->[1];
my $fail_fixes = $_[0]->[2];
$cond->pass_fixes([map { $_->reify } @$pass_fixes]);
$cond->fail_fixes([map { $_->reify } @$fail_fixes]) if $fail_fixes;
$cond;
}
sub Catmandu::Fix::Parser::Unless::reify {
my $cond = $_[0]->[0]->reify;
my $fail_fixes = $_[0]->[1];
$cond->fail_fixes([map { $_->reify } @$fail_fixes]);
$cond;
}
sub Catmandu::Fix::Parser::Select::reify {
my $cond = $_[0]->[0]->reify;
$cond->fail_fixes([Catmandu::Util::require_package('Catmandu::Fix::reject')->new]);
$cond;
}
sub Catmandu::Fix::Parser::Reject::reify {
my $cond = $_[0]->[0]->reify;
$cond->pass_fixes([Catmandu::Util::require_package('Catmandu::Fix::reject')->new]);
$cond;
}
sub Catmandu::Fix::Parser::Fix::reify {
my $name = $_[0]->[0];
my $args = $_[0]->[1];
Catmandu::Util::require_package($name, 'Catmandu::Fix')
->new(map { $_->reify } @$args);
}
sub Catmandu::Fix::Parser::Condition::reify {
my $name = $_[0]->[0];
my $args = $_[0]->[1];
Catmandu::Util::require_package($name, 'Catmandu::Fix::Condition')
->new(map { $_->reify } @$args);
}
sub Catmandu::Fix::Parser::OldCondition::reify {
my $name = $_[0]->[0];
my $args = $_[0]->[1];
$name =~ s/^(?:if|unless)_//;
Catmandu::Util::require_package($name, 'Catmandu::Fix::Condition')
->new(map { $_->reify } @$args);
}
sub Catmandu::Fix::Parser::DoSet::reify {
my $bind = $_[0]->[0]->reify;
my $do_fixes = $_[0]->[1];
$bind->return(1);
$bind->fixes([map { $_->reify } @$do_fixes]);
$bind;
}
sub Catmandu::Fix::Parser::Do::reify {
my $bind = $_[0]->[0]->reify;
my $do_fixes = $_[0]->[1];
$bind->return(0);
$bind->fixes([map { $_->reify } @$do_fixes]);
$bind;
}
sub Catmandu::Fix::Parser::Bind::reify {
my $name = $_[0]->[0];
my $args = $_[0]->[1];
Catmandu::Util::require_package($name, 'Catmandu::Fix::Bind')
->new(map { $_->reify } @$args);
}
sub Catmandu::Fix::Parser::DoubleQuotedString::reify {
my $str = $_[0]->[0];
$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 Catmandu::Fix::Parser::SingleQuotedString::reify {
my $str = $_[0]->[0];
$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 Catmandu::Fix::Parser::BareString::reify {
$_[0]->[0];
}
sub Catmandu::Fix::Parser::Int::reify {
int($_[0]->[0]);
}
1;
|