From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

#!/usr/bin/perl
# Copyright 2015 Jeffrey Kegler
# This file is part of Marpa::R2. Marpa::R2 is free software: you can
# redistribute it and/or modify it under the terms of the GNU Lesser
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Marpa::R2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser
# General Public License along with Marpa::R2. If not, see
use 5.010;
use strict;
use Test::More tests => 12;
use English qw( -no_match_vars );
use Fatal qw( open close );
use lib 'inc';
my $progress_report = q{};
# Marpa::R2::Display
# name: Debug Example Part 1
my $grammar = Marpa::R2::Grammar->new(
{ start => 'Expression',
actions => 'My_Actions',
default_action => 'first_arg',
rules => [
## This is a deliberate error in the grammar
## The next line should be:
## { lhs => 'Expression', rhs => [qw/Term/] },
## I have changed the Term to 'Factor' which
## will cause problems.
{ lhs => 'Expression', rhs => [qw/Factor/] },
{ lhs => 'Term', rhs => [qw/Factor/] },
{ lhs => 'Factor', rhs => [qw/Number/] },
{ lhs => 'Term',
rhs => [qw/Term Add Term/],
action => 'do_add'
},
{ lhs => 'Factor',
rhs => [qw/Factor Multiply Factor/],
action => 'do_multiply'
},
],
}
);
# Marpa::R2::Display::End
## no critic (InputOutput::RequireBriefOpen)
open my $trace_fh, q{>}, \( my $trace_output = q{} );
## use critic
# Marpa::R2::Display
# name: Grammar set Synopsis
$grammar->set( { trace_file_handle => $trace_fh } );
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: Debug Example Part 2
$grammar->precompute();
my @tokens = (
[ 'Number', 42 ],
[ 'Multiply', q{*} ],
[ 'Number', 1 ],
[ 'Add', q{+} ],
[ 'Number', 7 ],
);
sub My_Actions::do_add {
my ( undef, $t1, undef, $t2 ) = @_;
return $t1 + $t2;
}
sub My_Actions::do_multiply {
my ( undef, $t1, undef, $t2 ) = @_;
return $t1 * $t2;
}
sub My_Actions::first_arg { shift; return shift; }
my $recce = Marpa::R2::Recognizer->new(
{ grammar => $grammar, trace_terminals => 2 } );
my $token_ix = 0;
TOKEN: for my $token_and_value (@tokens) {
last TOKEN if not defined $recce->read( @{$token_and_value} );
}
$progress_report = $recce->show_progress( 0, -1 );
# Marpa::R2::Display::End
my $value_ref = $recce->value;
my $value = $value_ref ? ${$value_ref} : 'No Parse';
Test::More::is( $value, 42, 'value' );
# Marpa::R2::Display
# name: Debug Example Progress Report
# start-after-line: END_PROGRESS_REPORT
# end-before-line: '^END_PROGRESS_REPORT$'
Marpa::R2::Test::is( $progress_report,
<<'END_PROGRESS_REPORT', 'progress report' );
P0 @0-0 Expression -> . Factor
P2 @0-0 Factor -> . Number
P4 @0-0 Factor -> . Factor Multiply Factor
F0 @0-1 Expression -> Factor .
F2 @0-1 Factor -> Number .
R4:1 @0-1 Factor -> Factor . Multiply Factor
P2 @2-2 Factor -> . Number
P4 @2-2 Factor -> . Factor Multiply Factor
R4:2 @0-2 Factor -> Factor Multiply . Factor
F0 @0-3 Expression -> Factor .
F2 @2-3 Factor -> Number .
R4:1 x2 @0,2-3 Factor -> Factor . Multiply Factor
F4 @0-3 Factor -> Factor Multiply Factor .
END_PROGRESS_REPORT
# Marpa::R2::Display::End
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;
# Marpa::R2::Display
# name: progress(0) example
my $report0 = $recce->progress(0);
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: progress() output at location 0
# start-after-line: END_PROGRESS_REPORT
# end-before-line: '^END_PROGRESS_REPORT$'
chomp( my $expected_report0 = <<'END_PROGRESS_REPORT');
[[0,0,0],[2,0,0],[4,0,0]]
END_PROGRESS_REPORT
Marpa::R2::Test::is( Data::Dumper::Dumper($report0),
$expected_report0, 'progress report at location 0' );
# Marpa::R2::Display::End
# Try again with negative index
$report0 = $recce->progress(-4);
Marpa::R2::Test::is( Data::Dumper::Dumper($report0),
$expected_report0, 'progress report at location -4' );
my $report1 = $recce->progress(1);
# Marpa::R2::Display
# name: progress() output at location 1
# start-after-line: END_PROGRESS_REPORT
# end-before-line: '^END_PROGRESS_REPORT$'
chomp( my $expected_report1 = <<'END_PROGRESS_REPORT');
[[0,-1,0],[2,-1,0],[4,1,0]]
END_PROGRESS_REPORT
Marpa::R2::Test::is( Data::Dumper::Dumper($report1),
$expected_report1, 'progress report at location 1' );
# Marpa::R2::Display::End
# Try again with negative index
$report1 = $recce->progress(-3);
Marpa::R2::Test::is( Data::Dumper::Dumper($report1),
$expected_report1, 'progress report at location -3' );
my $report2 = $recce->progress(2);
# Marpa::R2::Display
# name: progress() output at location 2
# start-after-line: END_PROGRESS_REPORT
# end-before-line: '^END_PROGRESS_REPORT$'
chomp( my $expected_report2 = <<'END_PROGRESS_REPORT');
[[2,0,2],[4,0,2],[4,2,0]]
END_PROGRESS_REPORT
Marpa::R2::Test::is( Data::Dumper::Dumper($report2),
$expected_report2, 'progress report at location 2' );
# Marpa::R2::Display::End
# Try again with negative index
$report2 = $recce->progress(-2);
Marpa::R2::Test::is( Data::Dumper::Dumper($report2),
$expected_report2, 'progress report at location -2' );
# Marpa::R2::Display
# name: progress() example
my $latest_report = $recce->progress();
# Marpa::R2::Display::End
# Marpa::R2::Display
# name: progress() output at location 3
# start-after-line: END_PROGRESS_REPORT
# end-before-line: '^END_PROGRESS_REPORT$'
chomp( my $expected_report3 = <<'END_PROGRESS_REPORT');
[[0,-1,0],[2,-1,2],[4,-1,0],[4,1,0],[4,1,2]]
END_PROGRESS_REPORT
Marpa::R2::Test::is( Data::Dumper::Dumper($latest_report),
$expected_report3, 'progress report at location 3' );
# Marpa::R2::Display::End
# Try latest report again with explicit index
my $report3 = $recce->progress(3);
Marpa::R2::Test::is( Data::Dumper::Dumper($report3),
$expected_report3, 'progress report at location 3' );
# Try latest report again with negative index
$latest_report = $recce->progress(-1);
Marpa::R2::Test::is( Data::Dumper::Dumper($latest_report),
$expected_report3, 'progress report at location -1' );
# Marpa::R2::Display
# name: Debug Example Trace Output
# start-after-line: END_TRACE_OUTPUT
# end-before-line: '^END_TRACE_OUTPUT$'
Marpa::R2::Test::is( $trace_output, <<'END_TRACE_OUTPUT', 'trace output' );
Inaccessible symbol: Add
Inaccessible symbol: Term
Setting trace_terminals option
Expecting "Number" at earleme 0
Accepted "Number" at 0-1
Expecting "Multiply" at 1
Accepted "Multiply" at 1-2
Expecting "Number" at 2
Accepted "Number" at 2-3
Expecting "Multiply" at 3
Rejected "Add" at 3-4
END_TRACE_OUTPUT
# Marpa::R2::Display::End
1; # In case used as "do" file
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: