Skip to content

Commit

Permalink
handle skip - fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jan 8, 2020
1 parent d2cee4d commit ed1f243
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/XML/Invisible.pm
Expand Up @@ -83,33 +83,42 @@ sub _atoms2canonical {
sub _extract_canonical {
my ($atoms, $elt, $grammar_tree, $elt_sought, $grammar_frag) = @_;
$grammar_frag ||= $grammar_tree->{$elt_sought};
return undef if !ref $elt; # just text node - not valid
return undef if defined($elt_sought) and $elt_sought ne $elt->{nodename}; # non-match
if (defined($elt)) {
return undef if !ref $elt; # just text node - not valid
return undef if defined($elt_sought) and $elt_sought ne $elt->{nodename};
}
if ($grammar_frag->{'.rgx'}) {
# RE, so parent of text nodes
return $elt->{children} ? join('', @{$elt->{children}}) : $elt->{nodename};
}
if (my $all = $grammar_frag->{'.all'}) {
# sequence of productions
return undef if @$all != @{$elt->{children}};
my @results;
my ($childcount, @results) = (0);
for my $i (0..$#$all) {
my $child = $elt->{children}[$childcount];
my $all_frag = $all->[$i];
if ($all_frag->{'-skip'}) {
$child = undef;
} else {
$childcount++;
}
my @partial = _extract_canonical(
$atoms, $elt->{children}[$i], $grammar_tree, undef, $all->[$i],
$atoms, $child, $grammar_tree, undef, $all_frag,
);
return undef if grep !defined, @partial; # any non-match
push @results, @partial;
}
return @results;
} elsif (my $ref = $grammar_frag->{'.ref'}) {
return $atoms->{$ref} if exists $atoms->{$ref};
return undef if $elt->{nodename} ne $ref;
return undef if defined($elt) and $elt->{nodename} ne $ref;
my $new_frag = $grammar_tree->{$ref};
if ($new_frag->{'.ref'}) {
# this one is just a single-child empty if it's a match
return undef if @{$elt->{children}} != 1;
return undef if defined($elt) and @{$elt->{children}} != 1;
my $child = defined($elt) ? $elt->{children}[0] : undef;
return _extract_canonical(
$atoms, $elt->{children}[0], $grammar_tree, $new_frag->{'.ref'}, $new_frag,
$atoms, $child, $grammar_tree, $new_frag->{'.ref'}, $new_frag,
);
}
# treat ourselves as if we're the ref-ed to thing
Expand Down
13 changes: 13 additions & 0 deletions t/reverse.t
Expand Up @@ -25,4 +25,17 @@ EOF
'basic',
);

run_test(
<<'EOF',
expr: target .assign source
target: name
assign: (- EQUAL -)
source: name
name: /( ALPHA (: ALPHA | DIGIT )* )/
EOF
'a = b',
'a=b',
'skip',
);

done_testing;

0 comments on commit ed1f243

Please sign in to comment.