| 1 | package TPath::Compiler; | ||
| 2 | $TPath::Compiler::VERSION = '1.007'; | ||
| 3 | # ABSTRACT: takes ASTs and returns compiled L<TPath::Expression> objects | ||
| 4 | |||
| 5 | |||
| 6 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | no if $] >= 5.018, warnings => "experimental"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Scalar::Util qw(refaddr blessed reftype); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use TPath::Grammar qw(%FUNCTIONS); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use parent 'Exporter'; | ||
| 15 | |||
| 16 | use aliased 'TPath::Attribute'; | ||
| 17 | use aliased 'TPath::AttributeTest'; | ||
| 18 | use aliased 'TPath::Concatenation'; | ||
| 19 | use aliased 'TPath::Expression'; | ||
| 20 | use aliased 'TPath::Function'; | ||
| 21 | use aliased 'TPath::Math'; | ||
| 22 | use aliased 'TPath::Predicate::Attribute' => 'PA'; | ||
| 23 | use aliased 'TPath::Predicate::AttributeTest' => 'PAT'; | ||
| 24 | use aliased 'TPath::Predicate::Boolean' => 'PB'; | ||
| 25 | use aliased 'TPath::Predicate::Expression' => 'PE'; | ||
| 26 | use aliased 'TPath::Predicate::Index'; | ||
| 27 | use aliased 'TPath::Selector'; | ||
| 28 | use aliased 'TPath::Selector::Expression' => 'SE'; | ||
| 29 | use aliased 'TPath::Selector::Id'; | ||
| 30 | use aliased 'TPath::Selector::Parent'; | ||
| 31 | use aliased 'TPath::Selector::Previous'; | ||
| 32 | use aliased 'TPath::Selector::Quantified'; | ||
| 33 | use aliased 'TPath::Selector::Self'; | ||
| 34 | use aliased 'TPath::Selector::Test::Anywhere'; | ||
| 35 | use aliased 'TPath::Selector::Test::AnywhereAttribute'; | ||
| 36 | use aliased 'TPath::Selector::Test::AnywhereMatch'; | ||
| 37 | use aliased 'TPath::Selector::Test::AnywhereTag'; | ||
| 38 | use aliased 'TPath::Selector::Test::AxisAttribute'; | ||
| 39 | use aliased 'TPath::Selector::Test::AxisMatch'; | ||
| 40 | use aliased 'TPath::Selector::Test::AxisTag'; | ||
| 41 | use aliased 'TPath::Selector::Test::AxisWildcard'; | ||
| 42 | use aliased 'TPath::Selector::Test::ChildAttribute'; | ||
| 43 | use aliased 'TPath::Selector::Test::ChildMatch'; | ||
| 44 | use aliased 'TPath::Selector::Test::ChildTag'; | ||
| 45 | use aliased 'TPath::Selector::Test::ClosestAttribute'; | ||
| 46 | use aliased 'TPath::Selector::Test::ClosestMatch'; | ||
| 47 | use aliased 'TPath::Selector::Test::ClosestTag'; | ||
| 48 | use aliased 'TPath::Selector::Test::Root'; | ||
| 49 | use aliased 'TPath::Test::And'; | ||
| 50 | use aliased 'TPath::Test::Not'; | ||
| 51 | use aliased 'TPath::Test::Or'; | ||
| 52 | use aliased 'TPath::Test::One'; | ||
| 53 | |||
| 54 | our @EXPORT_OK = qw(compile); | ||
| 55 | |||
| 56 | |||
| 57 | sub compile { | ||
| 58 | my $e = treepath(@_); | ||
| 59 | exp_attribute_link( $e, $e, {} ); | ||
| 60 | return $e; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub exp_attribute_link { | ||
| 64 | my ( $e, $ref, $cache ) = @_; | ||
| 65 | my $rt = reftype($ref) // ''; | ||
| 66 | return unless $rt eq 'HASH' or $rt eq 'ARRAY'; | ||
| 67 | return if $cache->{ refaddr $ref}++; | ||
| 68 | if ( blessed $ref && $ref->isa('TPath::Attribute') ) { | ||
| 69 | $ref->_expr($e); | ||
| 70 | } | ||
| 71 | for ($rt) { | ||
| 72 | when ('HASH') { | ||
| 73 | exp_attribute_link( $e, $_, $cache ) for values %$ref; | ||
| 74 | } | ||
| 75 | when ('ARRAY') { | ||
| 76 | exp_attribute_link( $e, $_, $cache ) for @$ref; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | sub treepath { | ||
| 82 | my ( $ref, $forester ) = @_; | ||
| 83 | my @paths; | ||
| 84 | for my $p ( @{ $ref->{treepath}{path} } ) { | ||
| 85 | push @paths, path( $p, $forester ); | ||
| 86 | } | ||
| 87 | return Expression->new( f => $forester, _selectors => \@paths ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub path { | ||
| 91 | my ( $p, $forester ) = @_; | ||
| 92 | my @selectors; | ||
| 93 | for my $step ( @{ $p->{segment} } ) { | ||
| 94 | push @selectors, step( $step, $forester ); | ||
| 95 | } | ||
| 96 | return \@selectors; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub step { | ||
| 100 | my ($step) = @_; | ||
| 101 | return cs(@_) if exists $step->{cs}; | ||
| 102 | return full(@_) if exists $step->{step} && exists $step->{step}{full}; | ||
| 103 | return abbreviated(@_); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub cs { | ||
| 107 | my ( $step, $forester ) = @_; | ||
| 108 | my $q = $step->{cs}{quantifier}; | ||
| 109 | if ( exists $step->{cs}{step} ) { | ||
| 110 | my $s = full( $step->{cs}, $forester ); | ||
| 111 | return Quantified->new( | ||
| 112 | s => $s, | ||
| 113 | quantifier => 'e', | ||
| 114 | top => $q->{enum}{end}, | ||
| 115 | bottom => $q->{enum}{start} | ||
| 116 | ) if ref $q; | ||
| 117 | return Quantified->new( | ||
| 118 | s => $s, | ||
| 119 | quantifier => $q | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | my @predicates = | ||
| 123 | predicates( $step->{cs}{grouped_step}{predicate}, $forester, 1 ); | ||
| 124 | return SE->new( | ||
| 125 | e => treepath( $step->{cs}{grouped_step}, $forester ), | ||
| 126 | predicates => \@predicates, | ||
| 127 | ) unless $q; | ||
| 128 | my $e = SE->new( e => treepath( $step->{cs}{grouped_step}, $forester ) ); | ||
| 129 | return Quantified->new( | ||
| 130 | s => $e, | ||
| 131 | quantifier => 'e', | ||
| 132 | top => $q->{enum}{end}, | ||
| 133 | bottom => $q->{enum}{start} | ||
| 134 | ) if ref $q; | ||
| 135 | return Quantified->new( | ||
| 136 | s => $e, | ||
| 137 | quantifier => $q | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub full { | ||
| 142 | my ( $step, $forester ) = @_; | ||
| 143 | my @predicates = predicates( $step->{step}{predicate}, $forester ); | ||
| 144 | return Previous->new( f => $forester, predicates => \@predicates ) | ||
| 145 | if $step->{step}{full}{previous}; | ||
| 146 | my $sep = $step->{separator}; | ||
| 147 | my $type = $step->{step}{full}{forward}; | ||
| 148 | my $complement = $step->{step}{full}{forward}{complement}; | ||
| 149 | my $axis = $step->{step}{full}{axis}; | ||
| 150 | my ( $key, $val ) = each %$type; | ||
| 151 | my $rv; # return value | ||
| 152 | |||
| 153 | for ($key) { | ||
| 154 | when ('wildcard') { | ||
| 155 | for ($sep) { | ||
| 156 | when ('/') { | ||
| 157 | if ($axis) { | ||
| 158 | $rv = AxisWildcard->new( | ||
| 159 | f => $forester, | ||
| 160 | axis => $axis, | ||
| 161 | predicates => \@predicates | ||
| 162 | ); | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | $rv = AxisWildcard->new( | ||
| 166 | f => $forester, | ||
| 167 | first_sensitive => 1, | ||
| 168 | predicates => \@predicates | ||
| 169 | ); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | when ('//') { | ||
| 173 | die 'axes disallowed with // separator' if defined $axis; | ||
| 174 | set_anywhere( \@predicates ); | ||
| 175 | $rv = Anywhere->new( | ||
| 176 | f => $forester, | ||
| 177 | predicates => \@predicates | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | when ('/>') { die '/>* disallowed' } | ||
| 181 | default { | ||
| 182 | $rv = | ||
| 183 | $axis | ||
| 184 | ? AxisWildcard->new( | ||
| 185 | f => $forester, | ||
| 186 | axis => $axis, | ||
| 187 | predicates => \@predicates | ||
| 188 | ) | ||
| 189 | : AxisWildcard->new( | ||
| 190 | f => $forester, | ||
| 191 | predicates => \@predicates | ||
| 192 | ); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | when ('specific') { | ||
| 197 | for ($sep) { | ||
| 198 | when ('/') { | ||
| 199 | if ($axis) { | ||
| 200 | $rv = AxisTag->new( | ||
| 201 | f => $forester, | ||
| 202 | axis => $axis, | ||
| 203 | tag => $val, | ||
| 204 | predicates => \@predicates | ||
| 205 | ); | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | $rv = ChildTag->new( | ||
| 209 | f => $forester, | ||
| 210 | first_sensitive => 1, | ||
| 211 | tag => $val, | ||
| 212 | predicates => \@predicates | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | when ('//') { | ||
| 217 | die 'axes disallowed with // separator' if defined $axis; | ||
| 218 | set_anywhere( \@predicates ); | ||
| 219 | $rv = AnywhereTag->new( | ||
| 220 | f => $forester, | ||
| 221 | tag => $val, | ||
| 222 | predicates => \@predicates | ||
| 223 | ); | ||
| 224 | } | ||
| 225 | when ('/>') { | ||
| 226 | die 'axes disallowed with /> separator' if defined $axis; | ||
| 227 | $rv = ClosestTag->new( | ||
| 228 | f => $forester, | ||
| 229 | tag => $val, | ||
| 230 | predicates => \@predicates | ||
| 231 | ); | ||
| 232 | } | ||
| 233 | default { | ||
| 234 | $rv = | ||
| 235 | $axis | ||
| 236 | ? AxisTag->new( | ||
| 237 | f => $forester, | ||
| 238 | axis => $axis, | ||
| 239 | tag => $val, | ||
| 240 | predicates => \@predicates | ||
| 241 | ) | ||
| 242 | : ChildTag->new( | ||
| 243 | f => $forester, | ||
| 244 | tag => $val, | ||
| 245 | predicates => \@predicates | ||
| 246 | ); | ||
| 247 | } | ||
| 248 | } | ||
| 249 | } | ||
| 250 | when ('pattern') { | ||
| 251 | my $rx = $forester->case_insensitive ? qr/$val/i : qr/$val/; | ||
| 252 | my %common_args = ( | ||
| 253 | f => $forester, | ||
| 254 | rx => $rx, | ||
| 255 | val => $val, | ||
| 256 | predicates => \@predicates | ||
| 257 | ); | ||
| 258 | for ($sep) { | ||
| 259 | when ('/') { | ||
| 260 | if ($axis) { | ||
| 261 | $rv = AxisMatch->new( %common_args, axis => $axis ); | ||
| 262 | } | ||
| 263 | else { | ||
| 264 | $rv = | ||
| 265 | ChildMatch->new( %common_args, first_sensitive => 1 ); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | when ('//') { | ||
| 269 | die 'axes disallowed with // separator' if defined $axis; | ||
| 270 | set_anywhere( $common_args{predicates} ); | ||
| 271 | $rv = | ||
| 272 | TPath::Selector::Test::AnywhereMatch->new(%common_args); | ||
| 273 | } | ||
| 274 | when ('/>') { | ||
| 275 | die 'axes disallowed with /> separator' if defined $axis; | ||
| 276 | $rv = ClosestMatch->new(%common_args); | ||
| 277 | } | ||
| 278 | default { | ||
| 279 | $rv = | ||
| 280 | $axis | ||
| 281 | ? AxisMatch->new( %common_args, axis => $axis, ) | ||
| 282 | : ChildMatch->new(%common_args); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | when ('attribute') { | ||
| 287 | my $a = | ||
| 288 | attribute( $step->{step}{full}{forward}{attribute}, $forester ); | ||
| 289 | for ($sep) { | ||
| 290 | when ('/') { | ||
| 291 | $rv = | ||
| 292 | $axis | ||
| 293 | ? AxisAttribute->new( | ||
| 294 | f => $forester, | ||
| 295 | axis => $axis, | ||
| 296 | a => $a, | ||
| 297 | predicates => \@predicates | ||
| 298 | ) | ||
| 299 | : ChildAttribute->new( | ||
| 300 | f => $forester, | ||
| 301 | first_sensitive => 1, | ||
| 302 | a => $a, | ||
| 303 | predicates => \@predicates | ||
| 304 | ); | ||
| 305 | } | ||
| 306 | when ('//') { | ||
| 307 | die 'axes disallowed with // separator' if defined $axis; | ||
| 308 | set_anywhere( \@predicates ); | ||
| 309 | $rv = AnywhereAttribute->new( | ||
| 310 | f => $forester, | ||
| 311 | a => $a, | ||
| 312 | predicates => \@predicates | ||
| 313 | ); | ||
| 314 | } | ||
| 315 | when ('/>') { | ||
| 316 | die 'axes disallowed with /> separator' if defined $axis; | ||
| 317 | $rv = ClosestAttribute->new( | ||
| 318 | f => $forester, | ||
| 319 | a => $a, | ||
| 320 | predicates => \@predicates | ||
| 321 | ); | ||
| 322 | } | ||
| 323 | default { | ||
| 324 | $rv = | ||
| 325 | $axis | ||
| 326 | ? AxisAttribute->new( | ||
| 327 | f => $forester, | ||
| 328 | axis => $axis, | ||
| 329 | a => $a, | ||
| 330 | predicates => \@predicates | ||
| 331 | ) | ||
| 332 | : ChildAttribute->new( | ||
| 333 | f => $forester, | ||
| 334 | a => $a, | ||
| 335 | predicates => \@predicates | ||
| 336 | ); | ||
| 337 | } | ||
| 338 | } | ||
| 339 | } | ||
| 340 | } | ||
| 341 | $rv->_invert if $complement; | ||
| 342 | return $rv; | ||
| 343 | } | ||
| 344 | |||
| 345 | # tell index predicates if they're attached to a // step | ||
| 346 | sub set_anywhere { | ||
| 347 | my $predicates = shift; | ||
| 348 | return unless $predicates; | ||
| 349 | for my $p (@$predicates) { | ||
| 350 | $p->_anywhere(1) if $p->isa('TPath::Predicate::Index'); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | sub predicates { | ||
| 355 | my ( $predicates, $forester, $outer ) = @_; | ||
| 356 | return () unless $predicates; | ||
| 357 | my @predicates = map { predicate( $_, $forester, $outer ) } @$predicates; | ||
| 358 | if ( 1 < grep { $_->isa('TPath::Predicate::Index') } @predicates ) { | ||
| 359 | die 'a step may only have one index predicate'; | ||
| 360 | } | ||
| 361 | return @predicates; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub predicate { | ||
| 365 | my ( $predicate, $forester, $outer ) = @_; | ||
| 366 | my $idx = $predicate->{idx}; | ||
| 367 | return Index->new( f => $forester, idx => $idx, outer => $outer ) | ||
| 368 | if defined $idx; | ||
| 369 | my $op = $predicate->{condition}{operator}; | ||
| 370 | return PB->new( | ||
| 371 | t => condition( $predicate, $forester, $op ), | ||
| 372 | outer => $outer | ||
| 373 | ) if defined $op; | ||
| 374 | return PE->new( e => treepath( $predicate, $forester ), outer => $outer ) | ||
| 375 | if exists $predicate->{treepath}; | ||
| 376 | my $at = $predicate->{attribute_test}; | ||
| 377 | return PAT->new( at => attribute_test( $at, $forester ), outer => $outer ) | ||
| 378 | if defined $at; | ||
| 379 | return PA->new( | ||
| 380 | a => attribute( $predicate->{attribute}, $forester ), | ||
| 381 | outer => $outer | ||
| 382 | ); | ||
| 383 | } | ||
| 384 | |||
| 385 | sub attribute { | ||
| 386 | my ( $attribute, $forester ) = @_; | ||
| 387 | my @args; | ||
| 388 | my $args = $attribute->{args}; | ||
| 389 | if ( defined $args ) { | ||
| 390 | push @args, arg( $_, $forester ) for @{ $args->{arg} }; | ||
| 391 | } | ||
| 392 | my $name = $attribute->{aname}{name}; | ||
| 393 | my $autoloaded = $attribute->{aname}{autoloaded}; | ||
| 394 | my $code; | ||
| 395 | if ($autoloaded) { | ||
| 396 | $code = $forester->autoload_attribute( $name, @args ); | ||
| 397 | die( ref $forester ) . ' cannot autoload attribute ' . $attribute->{''} | ||
| 398 | unless defined $code; | ||
| 399 | } | ||
| 400 | else { | ||
| 401 | $code = $forester->_attributes->{$name}; | ||
| 402 | die 'unkown attribute @' . $name unless defined $code; | ||
| 403 | } | ||
| 404 | return Attribute->new( | ||
| 405 | name => $name, | ||
| 406 | args => \@args, | ||
| 407 | code => $code, | ||
| 408 | autoloaded => $autoloaded | ||
| 409 | ); | ||
| 410 | } | ||
| 411 | |||
| 412 | sub arg { | ||
| 413 | my ( $arg, $forester ) = @_; | ||
| 414 | return $arg unless ref $arg; | ||
| 415 | my $v = $arg->{v}; | ||
| 416 | return $v if defined $v; | ||
| 417 | my $num = $arg->{num}; | ||
| 418 | return $num if defined $num; | ||
| 419 | return attribute( $arg, $forester ) if exists $arg->{aname}; | ||
| 420 | my $a = $arg->{attribute}; | ||
| 421 | return attribute( $a, $forester ) if defined $a; | ||
| 422 | return treepath( $arg, $forester ) if exists $arg->{treepath}; | ||
| 423 | my $at = $arg->{attribute_test}; | ||
| 424 | return attribute_test( $at, $forester ) if defined $at; | ||
| 425 | my $m = $arg->{math}; | ||
| 426 | return math( $m, $forester ) if defined $m; | ||
| 427 | my $f = $arg->{function}; | ||
| 428 | return function( $f, $forester ) if defined $f; | ||
| 429 | my $c = $arg->{concat}; | ||
| 430 | return concat( $forester, $c ) if defined $c; | ||
| 431 | my $op = $arg->{condition}{operator}; | ||
| 432 | return condition( $arg, $forester, $op ) if defined $op; | ||
| 433 | die | ||
| 434 | 'fatal compilation error; could not compile parsable argument with keys ' | ||
| 435 | . ( join ', ', sort keys %$arg ); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub concat { | ||
| 439 | my ( $f, $c ) = @_; | ||
| 440 | my @args = map { arg( $_, $f ) } @{ $c->{carg} }; | ||
| 441 | |||
| 442 | # concatenate all constants | ||
| 443 | my $i = 0; | ||
| 444 | my $acc; | ||
| 445 | while ( $i < @args ) { | ||
| 446 | my $v = $args[$i]; | ||
| 447 | if ( ref $v ) { | ||
| 448 | $i++; | ||
| 449 | $acc = 0; | ||
| 450 | } | ||
| 451 | elsif ($acc) { | ||
| 452 | $args[ $i - 1 ] .= $v; | ||
| 453 | splice @args, $i, 1; | ||
| 454 | $acc = 1; | ||
| 455 | } | ||
| 456 | else { | ||
| 457 | $acc = 1; | ||
| 458 | $i++; | ||
| 459 | } | ||
| 460 | } | ||
| 461 | return $args[0] unless @args > 1; | ||
| 462 | return Concatenation->new( args => \@args ); | ||
| 463 | } | ||
| 464 | |||
| 465 | sub function { | ||
| 466 | my ( $f, $forester ) = @_; | ||
| 467 | my $name = $f->{f}; | ||
| 468 | return Function->new( | ||
| 469 | f => $FUNCTIONS{$name}, | ||
| 470 | name => $name, | ||
| 471 | arg => arg( $f->{arg}, $forester ) | ||
| 472 | ); | ||
| 473 | } | ||
| 474 | |||
| 475 | sub math { | ||
| 476 | my ( $m, $forester ) = @_; | ||
| 477 | my @args = map { arg( $_, $forester ) } @{ $m->{item} }; | ||
| 478 | return Math->new( operator => $m->{operator}, args => \@args ); | ||
| 479 | } | ||
| 480 | |||
| 481 | sub attribute_test { | ||
| 482 | my ( $at, $forester ) = @_; | ||
| 483 | my $op = $at->{cmp}; | ||
| 484 | my @args = @{ $at->{value} }; | ||
| 485 | my $left = arg( $args[0], $forester ); | ||
| 486 | my $right = arg( $args[1], $forester ); | ||
| 487 | return AttributeTest->new( op => $op, left => $left, right => $right ); | ||
| 488 | } | ||
| 489 | |||
| 490 | sub condition { | ||
| 491 | my ( $predicate, $forester, $op ) = @_; | ||
| 492 | my $args = $predicate->{condition}{args}; | ||
| 493 | my @args; | ||
| 494 | push @args, arg( $_, $forester ) for @$args; | ||
| 495 | for ($op) { | ||
| 496 | when ('!') { return Not->new( t => $args[0] ) } | ||
| 497 | when ('&') { return And->new( tests => \@args ) } | ||
| 498 | when ('||') { return Or->new( tests => \@args ) } | ||
| 499 | when (';') { return One->new( tests => \@args ) } | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | sub abbreviated { | ||
| 504 | my ( $step, $forester ) = @_; | ||
| 505 | my $abb = $step->{step}{abbreviated}; | ||
| 506 | return id(@_) if ref $abb; | ||
| 507 | for ($abb) { | ||
| 508 | when ('.') { state $s = Self->new; return $s } | ||
| 509 | when ('..') { state $p = Parent->new; return $p } | ||
| 510 | when (':root') { return Root->new( f => $forester ) } | ||
| 511 | } | ||
| 512 | } | ||
| 513 | |||
| 514 | sub id { | ||
| 515 | my ($step) = @_; | ||
| 516 | return Id->new( id => $step->{step}{abbreviated}{id} ); | ||
| 517 | } | ||
| 518 | |||
| 519 | 1; | ||
| 520 | |||
| 521 | __END__ | ||