| 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 |
|
| 17 |
# This test uses the thin interface It tests the LHS terminals feature |
| 18 |
# with a sequence rule LHS being used as a terminal. This is very much |
| 19 |
# a corner case -- LHS terminals are deprecated in libmarpa. This makes |
| 20 |
# it all the more important to watch for breakage in this "feature". |
| 21 |
|
| 22 |
use 5.010001; |
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
|
| 26 |
use Test::More tests => 2; |
| 27 |
|
| 28 |
use lib 'inc'; |
| 29 |
use Marpa::R2::Test; |
| 30 |
use English qw( -no_match_vars ); |
| 31 |
use Fatal qw( close open ); |
| 32 |
use Data::Dumper; |
| 33 |
use Marpa::R2; |
| 34 |
|
| 35 |
my $grammar = Marpa::R2::Thin::G->new( { if => 1 } ); |
| 36 |
$grammar->force_valued(); |
| 37 |
my $symbol_S = $grammar->symbol_new(); |
| 38 |
$grammar->start_symbol_set($symbol_S); |
| 39 |
my $symbol_seq = $grammar->symbol_new(); |
| 40 |
my $symbol_element = $grammar->symbol_new(); |
| 41 |
my $start_rule_id = $grammar->rule_new( $symbol_S, [$symbol_seq] ); |
| 42 |
my $seq_rule_id = |
| 43 |
$grammar->sequence_new( $symbol_seq, $symbol_element, { min => 0 } ); |
| 44 |
|
| 45 |
# Marpa::R2::Display |
| 46 |
# name: Thin symbol_is_terminal_set() example |
| 47 |
|
| 48 |
$grammar->symbol_is_terminal_set( $symbol_seq, 1 ); |
| 49 |
|
| 50 |
# Marpa::R2::Display::End |
| 51 |
|
| 52 |
$grammar->precompute(); |
| 53 |
|
| 54 |
my $recce = Marpa::R2::Thin::R->new($grammar); |
| 55 |
$recce->start_input(); |
| 56 |
|
| 57 |
# The numbers from 1 to 3 are themselves -- |
| 58 |
# that is, they index their own token value. |
| 59 |
# Important: zero cannot be itself! |
| 60 |
|
| 61 |
my @token_values = ( 0 .. 3 ); |
| 62 |
my $x_token_value = -1 + push @token_values, "x"; |
| 63 |
my $y_token_value = -1 + push @token_values, "y"; |
| 64 |
|
| 65 |
$recce->alternative( $symbol_element, $x_token_value, 1 ); |
| 66 |
$recce->earleme_complete(); |
| 67 |
$recce->alternative( $symbol_element, $x_token_value, 1 ); |
| 68 |
$recce->earleme_complete(); |
| 69 |
|
| 70 |
my %expected_value = ( q{\['SEQ:',['TOKEN:','x'],['TOKEN:','x']]} => 1, ); |
| 71 |
|
| 72 |
my $actual_values = evalIt($recce); |
| 73 |
my $i = 0; |
| 74 |
for my $actual_value (@$actual_values) { |
| 75 |
my $dumped_value = |
| 76 |
Data::Dumper->new( [ \$actual_value ] )->Indent(0)->Terse(1)->Dump; |
| 77 |
if ( defined $expected_value{$dumped_value} ) { |
| 78 |
delete $expected_value{$dumped_value}; |
| 79 |
Test::More::pass("Expected Value $i: $dumped_value"); |
| 80 |
} |
| 81 |
else { |
| 82 |
Test::More::fail("Unexpected Value $i: $dumped_value"); |
| 83 |
} |
| 84 |
$i++; |
| 85 |
} |
| 86 |
|
| 87 |
# Start a new recce |
| 88 |
$recce = Marpa::R2::Thin::R->new($grammar); |
| 89 |
$recce->start_input(); |
| 90 |
|
| 91 |
$recce->alternative( $symbol_element, $y_token_value, 1 ); |
| 92 |
$recce->earleme_complete(); |
| 93 |
|
| 94 |
%expected_value = ( q{\['SEQ:',['TOKEN:','y']]} => 1, ); |
| 95 |
|
| 96 |
$actual_values = evalIt($recce); |
| 97 |
$i = 0; |
| 98 |
for my $actual_value (@$actual_values) { |
| 99 |
my $dumped_value = |
| 100 |
Data::Dumper->new( [ \$actual_value ] )->Indent(0)->Terse(1)->Dump; |
| 101 |
if ( defined $expected_value{$dumped_value} ) { |
| 102 |
delete $expected_value{$dumped_value}; |
| 103 |
Test::More::pass("Expected Value $i: $dumped_value"); |
| 104 |
} |
| 105 |
else { |
| 106 |
Test::More::fail("Unexpected Value $i: $dumped_value"); |
| 107 |
} |
| 108 |
$i++; |
| 109 |
} |
| 110 |
|
| 111 |
sub evalIt { |
| 112 |
my ($recce) = @_; |
| 113 |
my $latest_earley_set_ID = $recce->latest_earley_set(); |
| 114 |
my $bocage = Marpa::R2::Thin::B->new( $recce, $latest_earley_set_ID ); |
| 115 |
my $order = Marpa::R2::Thin::O->new($bocage); |
| 116 |
my $tree = Marpa::R2::Thin::T->new($order); |
| 117 |
my @actual_values = (); |
| 118 |
while ( $tree->next() ) { |
| 119 |
my $valuator = Marpa::R2::Thin::V->new($tree); |
| 120 |
my @stack = (); |
| 121 |
STEP: while (1) { |
| 122 |
my ( $type, @step_data ) = $valuator->step(); |
| 123 |
last STEP if not defined $type; |
| 124 |
if ( $type eq 'MARPA_STEP_TOKEN' ) { |
| 125 |
|
| 126 |
# say STDERR "TOKEN: ", join " ", @step_data; |
| 127 |
my ( undef, $token_value_ix, $arg_n ) = @step_data; |
| 128 |
$stack[$arg_n] = [ "TOKEN:", $token_values[$token_value_ix] ]; |
| 129 |
next STEP; |
| 130 |
} |
| 131 |
if ( $type eq 'MARPA_STEP_RULE' ) { |
| 132 |
|
| 133 |
# say STDERR "RULE: ", join " ", @step_data; |
| 134 |
my ( $rule_id, $arg_0, $arg_n ) = @step_data; |
| 135 |
if ( $rule_id == $start_rule_id ) { |
| 136 |
|
| 137 |
# just leave value at stack 0 where it is |
| 138 |
next STEP; |
| 139 |
} |
| 140 |
if ( $rule_id == $seq_rule_id ) { |
| 141 |
my $elements = ["SEQ:"]; |
| 142 |
for my $i ( $arg_0 ... $arg_n ) { |
| 143 |
push @$elements, $stack[$i]; |
| 144 |
} |
| 145 |
$stack[$arg_0] = $elements; |
| 146 |
next STEP; |
| 147 |
} |
| 148 |
die "Unknown rule $rule_id"; |
| 149 |
} ## end if ( $type eq 'MARPA_STEP_RULE' ) |
| 150 |
die "Unexpected step type: $type"; |
| 151 |
} ## end STEP: while (1) |
| 152 |
push @actual_values, $stack[0]; |
| 153 |
} ## end while ( $tree->next() ) |
| 154 |
return \@actual_values; |
| 155 |
} |
| 156 |
|
| 157 |
# vim: expandtab shiftwidth=4: |