| 1 |
#!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 |
# The example from p. 166 of Leo's paper, |
| 17 |
# augmented to test Leo prediction items. |
| 18 |
# |
| 19 |
|
| 20 |
use 5.010001; |
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use Test::More tests => 9; |
| 25 |
|
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
use Marpa::R2; |
| 29 |
|
| 30 |
## no critic (Subroutines::RequireArgUnpacking) |
| 31 |
|
| 32 |
sub main::default_action { |
| 33 |
shift; |
| 34 |
return ( join q{}, grep {defined} @_ ); |
| 35 |
} |
| 36 |
|
| 37 |
## use critic |
| 38 |
|
| 39 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 40 |
{ |
| 41 |
default_action => 'main::default_action', |
| 42 |
source => \(<<'END_OF_DSL'), |
| 43 |
:start ::= <expression> |
| 44 |
<expression> ::= 'x' | <assignment> |
| 45 |
<assignment> ::= <divide assignment> |
| 46 |
<assignment> ::= <multiply assignment> |
| 47 |
<assignment> ::= <add assignment> |
| 48 |
<assignment> ::= <subtract assignment> |
| 49 |
<assignment> ::= <plain assignment> |
| 50 |
<divide assignment> ::= 'x' '/=' <expression> |
| 51 |
<multiply assignment> ::= 'x' '*=' <expression> |
| 52 |
<add assignment> ::= 'x' '+=' <expression> |
| 53 |
<subtract assignment> ::= 'x' '-=' <expression> |
| 54 |
<plain assignment> ::= 'x' '=' <expression> |
| 55 |
event divide = completed <divide assignment> |
| 56 |
event multiply = completed <multiply assignment> |
| 57 |
event subtract = completed <subtract assignment> |
| 58 |
event add = completed <add assignment> |
| 59 |
event plain = completed <plain assignment> |
| 60 |
:discard ~ whitespace |
| 61 |
whitespace ~ [\s]* |
| 62 |
END_OF_DSL |
| 63 |
} |
| 64 |
); |
| 65 |
|
| 66 |
# Reaches closure |
| 67 |
do_test($grammar, 'x = x += x -= x *= x /= x', |
| 68 |
<<'END_OF_HISTORY' |
| 69 |
plain |
| 70 |
add plain |
| 71 |
add plain subtract |
| 72 |
add multiply plain subtract |
| 73 |
add divide multiply plain subtract |
| 74 |
END_OF_HISTORY |
| 75 |
); |
| 76 |
|
| 77 |
# Reaches closure and continues |
| 78 |
do_test($grammar, 'x = x += x -= x *= x /= x = x += x -= x *= x /= x', |
| 79 |
<<'END_OF_HISTORY' |
| 80 |
plain |
| 81 |
add plain |
| 82 |
add plain subtract |
| 83 |
add multiply plain subtract |
| 84 |
add divide multiply plain subtract |
| 85 |
add divide multiply plain subtract |
| 86 |
add divide multiply plain subtract |
| 87 |
add divide multiply plain subtract |
| 88 |
add divide multiply plain subtract |
| 89 |
add divide multiply plain subtract |
| 90 |
END_OF_HISTORY |
| 91 |
); |
| 92 |
|
| 93 |
# Reaches closure and continues |
| 94 |
my $test_slr = do_test($grammar, |
| 95 |
'x = x += x -= x *= x /= x = x += x -= x *= x /= x |
| 96 |
= x = x = x = x = x = x = x = x = x = x', |
| 97 |
<<'END_OF_HISTORY' |
| 98 |
plain |
| 99 |
add plain |
| 100 |
add plain subtract |
| 101 |
add multiply plain subtract |
| 102 |
add divide multiply plain subtract |
| 103 |
add divide multiply plain subtract |
| 104 |
add divide multiply plain subtract |
| 105 |
add divide multiply plain subtract |
| 106 |
add divide multiply plain subtract |
| 107 |
add divide multiply plain subtract |
| 108 |
add divide multiply plain subtract |
| 109 |
add divide multiply plain subtract |
| 110 |
add divide multiply plain subtract |
| 111 |
add divide multiply plain subtract |
| 112 |
add divide multiply plain subtract |
| 113 |
add divide multiply plain subtract |
| 114 |
add divide multiply plain subtract |
| 115 |
add divide multiply plain subtract |
| 116 |
add divide multiply plain subtract |
| 117 |
add divide multiply plain subtract |
| 118 |
END_OF_HISTORY |
| 119 |
); |
| 120 |
|
| 121 |
my $show_progress_output = $test_slr->show_progress(); |
| 122 |
|
| 123 |
# Marpa::R2::Display |
| 124 |
# name: SLIF Leo show_progress() example |
| 125 |
# start-after-line: END_OF_OUTPUT |
| 126 |
# end-before-line: '^END_OF_OUTPUT$' |
| 127 |
|
| 128 |
my $expected_show_progress_output = <<'END_OF_OUTPUT'; |
| 129 |
F0 @40-41 L2c38-40 expression -> 'x' . |
| 130 |
F1 x20 @0...38-41 L1c1-L2c40 expression -> assignment . |
| 131 |
F2 x2 @8,18-41 L1c17-L2c40 assignment -> <divide assignment> . |
| 132 |
F3 x2 @6,16-41 L1c12-L2c40 assignment -> <multiply assignment> . |
| 133 |
F4 x2 @2,12-41 L1c3-L2c40 assignment -> <add assignment> . |
| 134 |
F5 x2 @4,14-41 L1c7-L2c40 assignment -> <subtract assignment> . |
| 135 |
F6 x12 @0...38-41 L1c1-L2c40 assignment -> <plain assignment> . |
| 136 |
R7:1 @40-41 L2c38-40 <divide assignment> -> 'x' . '/=' expression |
| 137 |
F7 x2 @8,18-41 L1c17-L2c40 <divide assignment> -> 'x' '/=' expression . |
| 138 |
R8:1 @40-41 L2c38-40 <multiply assignment> -> 'x' . '*=' expression |
| 139 |
F8 x2 @6,16-41 L1c12-L2c40 <multiply assignment> -> 'x' '*=' expression . |
| 140 |
R9:1 @40-41 L2c38-40 <add assignment> -> 'x' . '+=' expression |
| 141 |
F9 x2 @2,12-41 L1c3-L2c40 <add assignment> -> 'x' '+=' expression . |
| 142 |
R10:1 @40-41 L2c38-40 <subtract assignment> -> 'x' . '-=' expression |
| 143 |
F10 x2 @4,14-41 L1c7-L2c40 <subtract assignment> -> 'x' '-=' expression . |
| 144 |
R11:1 @40-41 L2c38-40 <plain assignment> -> 'x' . '=' expression |
| 145 |
F11 x12 @0...38-41 L1c1-L2c40 <plain assignment> -> 'x' '=' expression . |
| 146 |
F12 @0-41 L1c1-L2c40 :start -> expression . |
| 147 |
END_OF_OUTPUT |
| 148 |
|
| 149 |
# Marpa::R2::Display::End |
| 150 |
|
| 151 |
Marpa::R2::Test::is( |
| 152 |
$show_progress_output, |
| 153 |
$expected_show_progress_output, |
| 154 |
"SLIF Leo show_progress() example" |
| 155 |
); |
| 156 |
|
| 157 |
# Never reaches closure |
| 158 |
do_test($grammar, 'x = x += x -= x = x += x -= x', |
| 159 |
<<'END_OF_HISTORY' |
| 160 |
plain |
| 161 |
add plain |
| 162 |
add plain subtract |
| 163 |
add plain subtract |
| 164 |
add plain subtract |
| 165 |
add plain subtract |
| 166 |
END_OF_HISTORY |
| 167 |
); |
| 168 |
|
| 169 |
sub do_test { |
| 170 |
my ( $grammar, $input, $expected_history ) = @_; |
| 171 |
my $slr = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 172 |
my $event_history; |
| 173 |
my $pos = $slr->read( \$input ); |
| 174 |
READ: while (1) { |
| 175 |
my @event_names; |
| 176 |
for ( my $ix = 0; my $event = $slr->event($ix); $ix++ ) { |
| 177 |
push @event_names, @{$event}; |
| 178 |
} |
| 179 |
$event_history .= join q{ }, sort @event_names; |
| 180 |
$event_history .= "\n"; |
| 181 |
last READ if $pos >= length $input; |
| 182 |
$pos = $slr->resume(); |
| 183 |
} ## end READ: while (1) |
| 184 |
my $value_ref = $slr->value(); |
| 185 |
my $value = $value_ref ? ${$value_ref} : 'No parse'; |
| 186 |
( my $expected = $input ) =~ s/\s//gxms; |
| 187 |
Marpa::R2::Test::is( $value, $expected, "Leo SLIF parse of $expected" ); |
| 188 |
Marpa::R2::Test::is( $event_history, $expected_history, "Event history of $expected" ); |
| 189 |
return $slr; |
| 190 |
} ## end sub do_test |
| 191 |
|
| 192 |
# vim: expandtab shiftwidth=4: |