Skip to content

Commit

Permalink
Fix Events AoA example: t++
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Kegler committed Jul 2, 2017
1 parent ab1d2be commit a673813
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
29 changes: 16 additions & 13 deletions cpan/pod/Event.pod
Expand Up @@ -1705,16 +1705,18 @@ There are two ways to do this.

=over 4

=item * Return C<'pause'> from any of the event
=item * B<Pause> method:
Return C<'pause'> from any of the event
handlers at I<pos>.
The recognizer will return control to the app
once all processing at I<pos> is complete,
and the app can do as it wishes.

=item * Store the data for the event in
an array indexed by I<pos>,
=item * B<AoA> method:
Store the data for the event in
an AoA (array of arrays) indexed by I<pos>,
and process the events later,
from the array.
from the AoA.

=back

Expand Down Expand Up @@ -1760,22 +1762,23 @@ normalize-whitespace: 1

=for Marpa::R3::Display::End

=head2 Per-location event processing, using an array
=head2 Per-location event processing, using an AoA

This example
illustrates the "array" method of handling events
which need to be processed together in groups,
based on their trigger location.
The events are gathered into an array,
illustrates the AoA (Array Of Arrays)
method of handling events
that need to be processed is sets, grouped
by trigger location.
The events are gathered into an array of arrays,
and processed when the recognizer is finished.

The previous example illustrated the more powerful
and general "pause" method.
The "array" method can be more elegant,
The AoA method can be more elegant,
and is powerful enough for many cases.

=for Marpa::R3::Display
name: event examples - per-location processing, using array
name: event examples - per-location processing, using AoA
normalize-whitespace: 1

@results = ();
Expand All @@ -1784,9 +1787,9 @@ normalize-whitespace: 1
grammar => $grammar4,
event_handlers => {
"'default" => sub () {
my ( $slr, $event_name ) = @_;
my ( $slr, @event_data ) = @_;
my $pos = $slr->pos();
$results[$pos]{$event_name} = 1;
push @{$results[$pos]}, \@event_data;
'ok';
},
}
Expand Down
16 changes: 9 additions & 7 deletions cpan/t/gie_d.t
Expand Up @@ -319,19 +319,19 @@ END_OF_TEXT
'per-location, using pause' );
}

## Per-location processing, using array
## Per-location processing, using AoA
# Marpa::R3::Display
# name: event examples - per-location processing, using array
# name: event examples - per-location processing, using AoA

@results = ();
$recce = Marpa::R3::Scanless::R->new(
{
grammar => $grammar4,
event_handlers => {
"'default" => sub () {
my ( $slr, $event_name ) = @_;
my ( $slr, @event_data ) = @_;
my $pos = $slr->pos();
$results[$pos]{$event_name} = 1;
push @{$results[$pos]}, \@event_data;
'ok';
},
}
Expand All @@ -345,9 +345,11 @@ $recce = Marpa::R3::Scanless::R->new(
$recce->read( \$input );
my @events_by_pos = ();
for (my $ix = 0; $ix <= $#results; $ix++) {
my @these_events = keys %{$results[$ix]};
push @events_by_pos, "$ix " . join q{ }, sort @these_events
if @these_events;
my $these_events = $results[$ix];
if ($these_events) {
my @event_names = sort map { $_->[0] } @{$these_events};
push @events_by_pos, "$ix " . join q{ }, @event_names;
}
}
my $actual_history = join "\n", @events_by_pos, q{};

Expand Down

0 comments on commit a673813

Please sign in to comment.