| 1 |
#!/usr/bin/env 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 |
use Marpa::R2 2.097_002; |
| 21 |
use Data::Dumper; |
| 22 |
use Test::More tests => 3; |
| 23 |
use Getopt::Long (); |
| 24 |
|
| 25 |
my $verbose; |
| 26 |
die if not Getopt::Long::GetOptions( verbose => \$verbose ); |
| 27 |
|
| 28 |
# This example tests the Ruby Slippers using the SLIF |
| 29 |
|
| 30 |
my $grammar = << '=== GRAMMAR ==='; |
| 31 |
:default ::= action => [ name, value ] |
| 32 |
lexeme default = action => [ name, value ] latm => 1 # to add token names to ast |
| 33 |
|
| 34 |
script ::= E+ separator => semi action => [values] |
| 35 |
E ::= |
| 36 |
number action => main::number |
| 37 |
|| E ('*') E action => main::multiply |
| 38 |
|| E ('+') E action => main::add |
| 39 |
|
| 40 |
:discard ~ whitespace |
| 41 |
whitespace ~ [\s]+ |
| 42 |
number ~ [\d]+ |
| 43 |
semi ~ ';' |
| 44 |
|
| 45 |
=== GRAMMAR === |
| 46 |
|
| 47 |
my $g = Marpa::R2::Scanless::G->new( { source => \($grammar) } ); |
| 48 |
|
| 49 |
my @tests = ( |
| 50 |
[ '1+2+3*4', '15' ], |
| 51 |
[ '1+2 3+4', '3,7' ], |
| 52 |
[ '0+42 21*2 3*7+21 3*7*2', '42,42,42,42' ], |
| 53 |
); |
| 54 |
|
| 55 |
for my $test (@tests) { |
| 56 |
my ( $string, $expected_result ) = @{$test}; |
| 57 |
my $actual_result = test( $g, $string ); |
| 58 |
say "Input: $string"; |
| 59 |
Test::More::is( $actual_result, $expected_result, |
| 60 |
qq{Result of "$string"} ); |
| 61 |
} ## end for my $test (@tests) |
| 62 |
|
| 63 |
sub test { |
| 64 |
my ( $g, $string ) = @_; |
| 65 |
my @found = (); |
| 66 |
|
| 67 |
diag("Input: $string") if $verbose; |
| 68 |
my $original_length = length $string; |
| 69 |
my $suffixed_string = $string . ';'; |
| 70 |
my $target_start = 0; |
| 71 |
|
| 72 |
# state $recce_debug_args = { trace_terminals => 1, trace_values => 1 }; |
| 73 |
state $recce_debug_args = {}; |
| 74 |
|
| 75 |
# Marpa::R2::Display |
| 76 |
# name: SLIF rejection recognizer setting synopsis |
| 77 |
|
| 78 |
my $recce = Marpa::R2::Scanless::R->new( |
| 79 |
{ grammar => $g, |
| 80 |
rejection => 'event', |
| 81 |
}, |
| 82 |
$recce_debug_args |
| 83 |
); |
| 84 |
my $pos = $recce->read( \$suffixed_string, 0, $original_length ); |
| 85 |
|
| 86 |
READ_LOOP: while (1) { |
| 87 |
my $rejection = 0; |
| 88 |
my $pos = $recce->pos(); |
| 89 |
EVENT: |
| 90 |
for my $event ( @{ $recce->events() } ) { |
| 91 |
my ($name) = @{$event}; |
| 92 |
if ( $name eq q('rejected) ) { |
| 93 |
$rejection = 1; |
| 94 |
diag("You fool! you forget the semi-colon at location $pos!") |
| 95 |
if $verbose; |
| 96 |
next EVENT; |
| 97 |
|
| 98 |
} ## end if ( $name eq q('rejected) ) |
| 99 |
die join q{ }, "Spurious event at position $pos: '$name'"; |
| 100 |
} ## end EVENT: for my $event ( @{ $recce->events() } ) |
| 101 |
|
| 102 |
last READ_LOOP if not $rejection; |
| 103 |
$recce->resume( $original_length, 1 ); |
| 104 |
diag("I fixed it for you. Now you owe me.") if $verbose; |
| 105 |
$recce->resume( $pos, $original_length - $pos ); |
| 106 |
} ## end READ_LOOP: while (1) |
| 107 |
|
| 108 |
# Marpa::R2::Display::End |
| 109 |
|
| 110 |
my $ref_value = $recce->value(); |
| 111 |
return 'No parse' if not $ref_value; |
| 112 |
if ( ref $ref_value ne 'REF' ) { |
| 113 |
my $ref_type = ref $ref_value; |
| 114 |
my $ref_description = $ref_type ? "ref to $ref_type" : 'not a ref'; |
| 115 |
return "Got $ref_description -- want REF to REF to ARRAY"; |
| 116 |
} |
| 117 |
return join q{,}, @{ ${$ref_value} }; |
| 118 |
|
| 119 |
} ## end sub test |
| 120 |
|
| 121 |
sub number { |
| 122 |
my ( undef, $v1 ) = @_; |
| 123 |
return $v1->[1]; |
| 124 |
} |
| 125 |
|
| 126 |
sub add { |
| 127 |
my ( $undef, $v1, $v2 ) = @_; |
| 128 |
return $v1 + $v2; |
| 129 |
} |
| 130 |
|
| 131 |
sub multiply { |
| 132 |
my ( $undef, $v1, $v2 ) = @_; |
| 133 |
return $v1 * $v2; |
| 134 |
} |
| 135 |
|
| 136 |
# vim: expandtab shiftwidth=4: |