| 1 |
#!/usr/bin/perl |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 4 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 5 |
# General Public License as published by the Free Software Foundation, |
| 6 |
# either version 3 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser |
| 14 |
# General Public License along with Marpa::R2. If not, see |
| 15 |
# http://www.gnu.org/licenses/. |
| 16 |
|
| 17 |
use 5.010001; |
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
|
| 21 |
use Test::More tests => 12; |
| 22 |
|
| 23 |
use English qw( -no_match_vars ); |
| 24 |
use Fatal qw( open close ); |
| 25 |
use lib 'inc'; |
| 26 |
use Marpa::R2::Test; |
| 27 |
use Marpa::R2; |
| 28 |
use Data::Dumper; |
| 29 |
|
| 30 |
my $progress_report = q{}; |
| 31 |
|
| 32 |
# Marpa::R2::Display |
| 33 |
# name: Debug Example Part 1 |
| 34 |
|
| 35 |
my $grammar = Marpa::R2::Grammar->new( |
| 36 |
{ start => 'Expression', |
| 37 |
actions => 'My_Actions', |
| 38 |
default_action => 'first_arg', |
| 39 |
rules => [ |
| 40 |
## This is a deliberate error in the grammar |
| 41 |
## The next line should be: |
| 42 |
## { lhs => 'Expression', rhs => [qw/Term/] }, |
| 43 |
## I have changed the Term to 'Factor' which |
| 44 |
## will cause problems. |
| 45 |
{ lhs => 'Expression', rhs => [qw/Factor/] }, |
| 46 |
{ lhs => 'Term', rhs => [qw/Factor/] }, |
| 47 |
{ lhs => 'Factor', rhs => [qw/Number/] }, |
| 48 |
{ lhs => 'Term', |
| 49 |
rhs => [qw/Term Add Term/], |
| 50 |
action => 'do_add' |
| 51 |
}, |
| 52 |
{ lhs => 'Factor', |
| 53 |
rhs => [qw/Factor Multiply Factor/], |
| 54 |
action => 'do_multiply' |
| 55 |
}, |
| 56 |
], |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
# Marpa::R2::Display::End |
| 61 |
|
| 62 |
## no critic (InputOutput::RequireBriefOpen) |
| 63 |
open my $trace_fh, q{>}, \( my $trace_output = q{} ); |
| 64 |
## use critic |
| 65 |
|
| 66 |
# Marpa::R2::Display |
| 67 |
# name: Grammar set Synopsis |
| 68 |
|
| 69 |
$grammar->set( { trace_file_handle => $trace_fh } ); |
| 70 |
|
| 71 |
# Marpa::R2::Display::End |
| 72 |
|
| 73 |
# Marpa::R2::Display |
| 74 |
# name: Debug Example Part 2 |
| 75 |
|
| 76 |
$grammar->precompute(); |
| 77 |
|
| 78 |
my @tokens = ( |
| 79 |
[ 'Number', 42 ], |
| 80 |
[ 'Multiply', q{*} ], |
| 81 |
[ 'Number', 1 ], |
| 82 |
[ 'Add', q{+} ], |
| 83 |
[ 'Number', 7 ], |
| 84 |
); |
| 85 |
|
| 86 |
sub My_Actions::do_add { |
| 87 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 88 |
return $t1 + $t2; |
| 89 |
} |
| 90 |
|
| 91 |
sub My_Actions::do_multiply { |
| 92 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 93 |
return $t1 * $t2; |
| 94 |
} |
| 95 |
|
| 96 |
sub My_Actions::first_arg { shift; return shift; } |
| 97 |
|
| 98 |
my $recce = Marpa::R2::Recognizer->new( |
| 99 |
{ grammar => $grammar, trace_terminals => 2 } ); |
| 100 |
|
| 101 |
my $token_ix = 0; |
| 102 |
|
| 103 |
TOKEN: for my $token_and_value (@tokens) { |
| 104 |
last TOKEN if not defined $recce->read( @{$token_and_value} ); |
| 105 |
} |
| 106 |
|
| 107 |
$progress_report = $recce->show_progress( 0, -1 ); |
| 108 |
|
| 109 |
# Marpa::R2::Display::End |
| 110 |
|
| 111 |
my $value_ref = $recce->value; |
| 112 |
my $value = $value_ref ? ${$value_ref} : 'No Parse'; |
| 113 |
|
| 114 |
Test::More::is( $value, 42, 'value' ); |
| 115 |
|
| 116 |
# Marpa::R2::Display |
| 117 |
# name: Debug Example Progress Report |
| 118 |
# start-after-line: END_PROGRESS_REPORT |
| 119 |
# end-before-line: '^END_PROGRESS_REPORT$' |
| 120 |
|
| 121 |
Marpa::R2::Test::is( $progress_report, |
| 122 |
<<'END_PROGRESS_REPORT', 'progress report' ); |
| 123 |
P0 @0-0 Expression -> . Factor |
| 124 |
P2 @0-0 Factor -> . Number |
| 125 |
P4 @0-0 Factor -> . Factor Multiply Factor |
| 126 |
F0 @0-1 Expression -> Factor . |
| 127 |
F2 @0-1 Factor -> Number . |
| 128 |
R4:1 @0-1 Factor -> Factor . Multiply Factor |
| 129 |
P2 @2-2 Factor -> . Number |
| 130 |
P4 @2-2 Factor -> . Factor Multiply Factor |
| 131 |
R4:2 @0-2 Factor -> Factor Multiply . Factor |
| 132 |
F0 @0-3 Expression -> Factor . |
| 133 |
F2 @2-3 Factor -> Number . |
| 134 |
R4:1 x2 @0,2-3 Factor -> Factor . Multiply Factor |
| 135 |
F4 @0-3 Factor -> Factor Multiply Factor . |
| 136 |
END_PROGRESS_REPORT |
| 137 |
|
| 138 |
# Marpa::R2::Display::End |
| 139 |
|
| 140 |
$Data::Dumper::Indent = 0; |
| 141 |
$Data::Dumper::Terse = 1; |
| 142 |
|
| 143 |
# Marpa::R2::Display |
| 144 |
# name: progress(0) example |
| 145 |
|
| 146 |
my $report0 = $recce->progress(0); |
| 147 |
|
| 148 |
# Marpa::R2::Display::End |
| 149 |
|
| 150 |
# Marpa::R2::Display |
| 151 |
# name: progress() output at location 0 |
| 152 |
# start-after-line: END_PROGRESS_REPORT |
| 153 |
# end-before-line: '^END_PROGRESS_REPORT$' |
| 154 |
|
| 155 |
chomp( my $expected_report0 = <<'END_PROGRESS_REPORT'); |
| 156 |
[[0,0,0],[2,0,0],[4,0,0]] |
| 157 |
END_PROGRESS_REPORT |
| 158 |
Marpa::R2::Test::is( Data::Dumper::Dumper($report0), |
| 159 |
$expected_report0, 'progress report at location 0' ); |
| 160 |
|
| 161 |
# Marpa::R2::Display::End |
| 162 |
|
| 163 |
# Try again with negative index |
| 164 |
$report0 = $recce->progress(-4); |
| 165 |
Marpa::R2::Test::is( Data::Dumper::Dumper($report0), |
| 166 |
$expected_report0, 'progress report at location -4' ); |
| 167 |
|
| 168 |
my $report1 = $recce->progress(1); |
| 169 |
|
| 170 |
# Marpa::R2::Display |
| 171 |
# name: progress() output at location 1 |
| 172 |
# start-after-line: END_PROGRESS_REPORT |
| 173 |
# end-before-line: '^END_PROGRESS_REPORT$' |
| 174 |
|
| 175 |
chomp( my $expected_report1 = <<'END_PROGRESS_REPORT'); |
| 176 |
[[0,-1,0],[2,-1,0],[4,1,0]] |
| 177 |
END_PROGRESS_REPORT |
| 178 |
Marpa::R2::Test::is( Data::Dumper::Dumper($report1), |
| 179 |
$expected_report1, 'progress report at location 1' ); |
| 180 |
|
| 181 |
# Marpa::R2::Display::End |
| 182 |
|
| 183 |
# Try again with negative index |
| 184 |
$report1 = $recce->progress(-3); |
| 185 |
Marpa::R2::Test::is( Data::Dumper::Dumper($report1), |
| 186 |
$expected_report1, 'progress report at location -3' ); |
| 187 |
|
| 188 |
my $report2 = $recce->progress(2); |
| 189 |
|
| 190 |
# Marpa::R2::Display |
| 191 |
# name: progress() output at location 2 |
| 192 |
# start-after-line: END_PROGRESS_REPORT |
| 193 |
# end-before-line: '^END_PROGRESS_REPORT$' |
| 194 |
|
| 195 |
chomp( my $expected_report2 = <<'END_PROGRESS_REPORT'); |
| 196 |
[[2,0,2],[4,0,2],[4,2,0]] |
| 197 |
END_PROGRESS_REPORT |
| 198 |
Marpa::R2::Test::is( Data::Dumper::Dumper($report2), |
| 199 |
$expected_report2, 'progress report at location 2' ); |
| 200 |
|
| 201 |
# Marpa::R2::Display::End |
| 202 |
|
| 203 |
# Try again with negative index |
| 204 |
$report2 = $recce->progress(-2); |
| 205 |
Marpa::R2::Test::is( Data::Dumper::Dumper($report2), |
| 206 |
$expected_report2, 'progress report at location -2' ); |
| 207 |
|
| 208 |
# Marpa::R2::Display |
| 209 |
# name: progress() example |
| 210 |
|
| 211 |
my $latest_report = $recce->progress(); |
| 212 |
|
| 213 |
# Marpa::R2::Display::End |
| 214 |
|
| 215 |
# Marpa::R2::Display |
| 216 |
# name: progress() output at location 3 |
| 217 |
# start-after-line: END_PROGRESS_REPORT |
| 218 |
# end-before-line: '^END_PROGRESS_REPORT$' |
| 219 |
|
| 220 |
chomp( my $expected_report3 = <<'END_PROGRESS_REPORT'); |
| 221 |
[[0,-1,0],[2,-1,2],[4,-1,0],[4,1,0],[4,1,2]] |
| 222 |
END_PROGRESS_REPORT |
| 223 |
Marpa::R2::Test::is( Data::Dumper::Dumper($latest_report), |
| 224 |
$expected_report3, 'progress report at location 3' ); |
| 225 |
|
| 226 |
# Marpa::R2::Display::End |
| 227 |
|
| 228 |
# Try latest report again with explicit index |
| 229 |
my $report3 = $recce->progress(3); |
| 230 |
Marpa::R2::Test::is( Data::Dumper::Dumper($report3), |
| 231 |
$expected_report3, 'progress report at location 3' ); |
| 232 |
|
| 233 |
# Try latest report again with negative index |
| 234 |
$latest_report = $recce->progress(-1); |
| 235 |
Marpa::R2::Test::is( Data::Dumper::Dumper($latest_report), |
| 236 |
$expected_report3, 'progress report at location -1' ); |
| 237 |
|
| 238 |
# Marpa::R2::Display |
| 239 |
# name: Debug Example Trace Output |
| 240 |
# start-after-line: END_TRACE_OUTPUT |
| 241 |
# end-before-line: '^END_TRACE_OUTPUT$' |
| 242 |
|
| 243 |
Marpa::R2::Test::is( $trace_output, <<'END_TRACE_OUTPUT', 'trace output' ); |
| 244 |
Inaccessible symbol: Add |
| 245 |
Inaccessible symbol: Term |
| 246 |
Setting trace_terminals option |
| 247 |
Expecting "Number" at earleme 0 |
| 248 |
Accepted "Number" at 0-1 |
| 249 |
Expecting "Multiply" at 1 |
| 250 |
Accepted "Multiply" at 1-2 |
| 251 |
Expecting "Number" at 2 |
| 252 |
Accepted "Number" at 2-3 |
| 253 |
Expecting "Multiply" at 3 |
| 254 |
Rejected "Add" at 3-4 |
| 255 |
END_TRACE_OUTPUT |
| 256 |
|
| 257 |
# Marpa::R2::Display::End |
| 258 |
|
| 259 |
1; # In case used as "do" file |
| 260 |
|
| 261 |
# Local Variables: |
| 262 |
# mode: cperl |
| 263 |
# cperl-indent-level: 4 |
| 264 |
# fill-column: 100 |
| 265 |
# End: |
| 266 |
# vim: expandtab shiftwidth=4: |