Skip to content

Commit

Permalink
Don't overwrite %item with repeated subrule matches in a production.
Browse files Browse the repository at this point in the history
*** NON-BACKWARDS COMPATIBLE CHANGE! *** A repetition directive
such as 'id(s /,/)' correctly creates a temporary @item variable
to hold the 'id's that are matched.  That @item variable is them
used to set the real $item[] entry for that repetition.  The
same treatment is now given to %item.  Formerly, in a production like:

    id ',' id(s /,/)

matched against:

    xxx, yyy, zzz

The $item{id} entry which should be 'xxx' is overwritten by
'yyy' and then 'zzz' prior to the action being executed.  Now
'yyy' and 'zzz' set $item{id}, but in the private %item, which
goes out of scope once the repetition match completes.
  • Loading branch information
jtbraun committed Feb 6, 2012
1 parent d8875d0 commit 558f536
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
17 changes: 17 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,20 @@ NEXT_VERSION NEXT_RELEASE
- Warn on empty productions followed by other productions. The
empty production always matches, so following productions will
never be reached.

- *** NON-BACKWARDS COMPATIBLE CHANGE! *** A repetition directive
such as 'id(s /,/)' correctly creates a temporary @item variable
to hold the 'id's that are matched. That @item variable is them
used to set the real $item[] entry for that repetition. The
same treatment is now given to %item. Formerly, in a production like:

id ',' id(s /,/)

matched against:

xxx, yyy, zzz

The $item{id} entry which should be 'xxx' is overwritten by
'yyy' and then 'zzz' prior to the action being executed. Now
'yyy' and 'zzz' set $item{id}, but in the private %item, which
goes out of scope once the repetition match completes.
18 changes: 0 additions & 18 deletions ToDo
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,3 @@ END_OF_GRAMMAR

This is caused by a bug in Text::Balanced:
https://rt.cpan.org/Ticket/Display.html?id=74714

-----cut----------cut----------cut----------cut----------cut----------cut-----

This grammar reports back:
['xxx', 'zzzz']
rather than the expected:
['xxx', 'xxx']

-------------------------------------------------------
my $parser = new Parse::RecDescent (q {
startrule : id ',' id(s /,/) {
$return = [$item[1], $item{id}]
}

id : /\w+/ }) || die $@;
my $text = 'xxx,yyyy,zzzz';
print Dumper $parser->startrule(\$text);
-------------------------------------------------------
5 changes: 3 additions & 2 deletions lib/Parse/RecDescent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,8 @@ sub code($$$$)
OPLOOP: while (1)
{
$repcount = 0;
my @item;
my @item;
my %item;
';

$code .= '
Expand Down Expand Up @@ -1746,7 +1747,7 @@ sub code($$$$)

$code .= '
last;
}
} # end of OPLOOP
';

$code .= '
Expand Down

0 comments on commit 558f536

Please sign in to comment.