| 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 |
# This example is from a Perl 6 advent blog post |
| 18 |
# (Day 18 2013) by Dwarring, adapted to Marpa by Jean-Damien |
| 19 |
# Durand. |
| 20 |
|
| 21 |
use 5.010001; |
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
use English qw( -no_match_vars ); |
| 25 |
use utf8; |
| 26 |
use open ':std', ':encoding(utf8)'; |
| 27 |
|
| 28 |
use Test::More tests => 54; |
| 29 |
use lib 'inc'; |
| 30 |
use Marpa::R2::Test; |
| 31 |
use Marpa::R2; |
| 32 |
|
| 33 |
my $base_dsl = <<'END_OF_BASE_DSL'; |
| 34 |
:start ::= deal |
| 35 |
deal ::= hands |
| 36 |
hands ::= hand | hands ';' hand |
| 37 |
hand ::= card card card card card |
| 38 |
card ~ face suit |
| 39 |
face ~ [2-9jqka] | '10' |
| 40 |
WS ~ [\s] |
| 41 |
:discard ~ WS |
| 42 |
|
| 43 |
:lexeme ~ <card> pause => after event => 'card' |
| 44 |
END_OF_BASE_DSL |
| 45 |
|
| 46 |
my @tests = (); |
| 47 |
push @tests, |
| 48 |
[ |
| 49 |
'2♥ 5♥ 7♦ 8♣ 9♠', |
| 50 |
'Parse OK', |
| 51 |
'Hand was 2♥ 5♥ 7♦ 8♣ 9♠', |
| 52 |
]; |
| 53 |
push @tests, |
| 54 |
[ |
| 55 |
'2♥ a♥ 7♦ 8♣ j♥', |
| 56 |
'Parse OK', |
| 57 |
'Hand was 2♥ a♥ 7♦ 8♣ j♥', |
| 58 |
]; |
| 59 |
push @tests, |
| 60 |
[ |
| 61 |
'a♥ a♥ 7♦ 8♣ j♥', |
| 62 |
'Parse stopped by application', |
| 63 |
'Duplicate card a♥' |
| 64 |
]; |
| 65 |
push @tests, |
| 66 |
[ |
| 67 |
'a♥ 7♥ 7♦ 8♣ j♥; 10♥ j♥ q♥ k♥ a♥', |
| 68 |
'Parse stopped by application', |
| 69 |
'Duplicate card j♥' |
| 70 |
]; |
| 71 |
push @tests, |
| 72 |
[ |
| 73 |
'2♥ 7♥ 2♦ 3♣ 3♦', |
| 74 |
'Parse OK', |
| 75 |
'Hand was 2♥ 7♥ 2♦ 3♣ 3♦', |
| 76 |
]; |
| 77 |
push @tests, |
| 78 |
[ |
| 79 |
'2♥ 7♥ 2♦ 3♣', |
| 80 |
'Parse reached end of input, but failed', |
| 81 |
'No hands were found' |
| 82 |
]; |
| 83 |
push @tests, [ |
| 84 |
'2♥ 7♥ 2♦ 3♣ 3♦ 1♦', |
| 85 |
'Parse failed before end', |
| 86 |
<<'END_OF_MESSAGE' |
| 87 |
Error in SLIF parse: No lexeme found at line 1, column 16 |
| 88 |
* String before error: 2\x{2665} 7\x{2665} 2\x{2666} 3\x{2663} 3\x{2666}\s |
| 89 |
* The error was at line 1, column 16, and at character 0x0031 '1', ... |
| 90 |
* here: 1\x{2666} |
| 91 |
END_OF_MESSAGE |
| 92 |
]; |
| 93 |
push @tests, |
| 94 |
[ |
| 95 |
'2♥ 7♥ 2♦ 3♣', |
| 96 |
'Parse reached end of input, but failed', |
| 97 |
'No hands were found' |
| 98 |
]; |
| 99 |
push @tests, |
| 100 |
[ |
| 101 |
'a♥ 7♥ 7♦ 8♣ j♥; 10♥ j♣ q♥ k♥', |
| 102 |
'Parse failed after finding hand(s)', |
| 103 |
'Last hand successfully parsed was a♥ 7♥ 7♦ 8♣ j♥' |
| 104 |
]; |
| 105 |
|
| 106 |
my @suit_line = ( |
| 107 |
[ 'suit ~ [\x{2665}\x{2666}\x{2663}\x{2660}]', 'hex' ], |
| 108 |
[ 'suit ~ [♥♦♣♠]', 'char class' ], |
| 109 |
[ q{suit ~ '♥' | '♦' | '♣'| '♠'}, 'strings' ], |
| 110 |
); |
| 111 |
|
| 112 |
for my $test_data (@tests) { |
| 113 |
my ( $input, $expected_result, $expected_value ) = @{$test_data}; |
| 114 |
my ( $actual_result, $actual_value ); |
| 115 |
|
| 116 |
for my $suit_line_data (@suit_line) { |
| 117 |
my ( $suit_line, $suit_line_type ) = @{$suit_line_data}; |
| 118 |
PROCESSING: { |
| 119 |
|
| 120 |
# Note: in production, you would compute the three grammar variants |
| 121 |
# ahead of time. |
| 122 |
my $full_dsl = $base_dsl . $suit_line; |
| 123 |
my $grammar = |
| 124 |
Marpa::R2::Scanless::G->new( { source => \$full_dsl } ); |
| 125 |
my $re = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 126 |
my $length = length $input; |
| 127 |
|
| 128 |
my %played = (); |
| 129 |
my $pos; |
| 130 |
my $eval_ok = eval { $pos = $re->read( \$input ); 1 }; |
| 131 |
while ( $eval_ok and $pos < $length ) { |
| 132 |
|
| 133 |
# In our example there is a single event: no need to ask Marpa what it is |
| 134 |
my ( $start, $length ) = |
| 135 |
$re->g1_location_to_span( $re->current_g1_location() ); |
| 136 |
my $card = $re->literal( $start, $length ); |
| 137 |
if ( ++$played{$card} > 1 ) { |
| 138 |
$actual_result = 'Parse stopped by application'; |
| 139 |
$actual_value = "Duplicate card " . $card; |
| 140 |
last PROCESSING; |
| 141 |
} |
| 142 |
$eval_ok = eval { $pos = $re->resume(); 1 }; |
| 143 |
} ## end while ( $eval_ok and $pos < $length ) |
| 144 |
if ( not $eval_ok ) { |
| 145 |
$actual_result = "Parse failed before end"; |
| 146 |
$actual_value = $EVAL_ERROR; |
| 147 |
$actual_value |
| 148 |
=~ s/ ^ Marpa::R2 \s+ exception \s+ at \s .* \z//xms; |
| 149 |
last PROCESSING; |
| 150 |
} ## end if ( not $eval_ok ) |
| 151 |
|
| 152 |
my $value_ref = $re->value(); |
| 153 |
my $last_hand; |
| 154 |
my ( $start, $end ) = $re->last_completed_range('hand'); |
| 155 |
if ( defined $start ) { |
| 156 |
$last_hand = $re->range_to_string( $start, $end ); |
| 157 |
} |
| 158 |
if ($value_ref) { |
| 159 |
$actual_result = 'Parse OK'; |
| 160 |
$actual_value = "Hand was $last_hand"; |
| 161 |
last PROCESSING; |
| 162 |
} |
| 163 |
if ( defined $last_hand ) { |
| 164 |
$actual_result = 'Parse failed after finding hand(s)'; |
| 165 |
$actual_value = |
| 166 |
"Last hand successfully parsed was $last_hand"; |
| 167 |
last PROCESSING; |
| 168 |
} ## end if ( defined $last_hand ) |
| 169 |
$actual_result = 'Parse reached end of input, but failed'; |
| 170 |
$actual_value = 'No hands were found'; |
| 171 |
} ## end PROCESSING: |
| 172 |
|
| 173 |
Marpa::R2::Test::is( $actual_result, $expected_result, |
| 174 |
"Result of $input using $suit_line_type" ); |
| 175 |
Marpa::R2::Test::is( $actual_value, $expected_value, |
| 176 |
"Value of $input using $suit_line_type" ); |
| 177 |
} ## end for my $suit_line_data (@suit_line) |
| 178 |
} ## end for my $test_data (@tests) |
| 179 |
|
| 180 |
# vim: expandtab shiftwidth=4: |