Debian Codegraph @

t/sl_lib115a.t

raw
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 # Dave Abrahams Libmarpa issue 115
18 # A smaller minimal working example
19 # "Bad ambiguity report"
20
21 use 5.010001;
22 use strict;
23 use warnings;
24 use Scalar::Util;
25 use Data::Dumper;
26 use Test::More tests => 1;
27
28 use lib 'inc';
29 use Marpa::R2::Test;
30
31 ## no critic (ErrorHandling::RequireCarping);
32
33 use Marpa::R2;
34
35 my $bnf = <<'END_OF_BNF';
36 :default ::= action => [values]
37 lexeme default = latm => 1
38 :start ::= prefixExpr
39 prefixExpr ::= null prefixExpr Arg2
40 prefixExpr ::= Arg1
41 null ::=
42
43 :discard ~ whitespace
44 unicorn ~ [^\d\D]
45 whitespace ~ [\s]+
46 Arg1 ~ unicorn
47 Arg2 ~ unicorn
48 END_OF_BNF
49
50 my $grammar = Marpa::R2::Scanless::G->new(
51 {
52 source => \$bnf,
53 }
54 );
55
56 my $string = <<'EOS';
57 type A {
58 var a: Int
59 fun foo(a: Int) { a.copy() }
60 fun foo(b: Int) -> Int {
61 let { a + b }
62 inout { b += a }
63 }
64 }
65 EOS
66
67 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
68
69 $recce->read( \$string, 0, 0 );
70
71 if ( not defined $recce->lexeme_read( 'Arg1', undef, 1, 'Arg1' ) ) {
72 die qq{Parser rejected token "Arg1"};
73 }
74 if ( not defined $recce->lexeme_read( 'Arg2', undef, 1, 'Arg2' ) ) {
75 die qq{Parser rejected token "Arg2"};
76 }
77 if ( not defined $recce->lexeme_read( 'Arg2', undef, 1, 'Arg2' ) ) {
78 die qq{Parser rejected token "Arg2"};
79 }
80
81 my $value_ref = $recce->value();
82 if ( not defined $value_ref ) {
83 die "No parse was found, after reading the entire input\n";
84 }
85
86 my $expected_value = \[ [], [ [], ['Arg1'], 'Arg2' ], 'Arg2' ];
87
88 Test::More::is_deeply(
89 Data::Dumper::Dumper($value_ref),
90 Data::Dumper::Dumper($expected_value),
91 'Value of parse'
92 );
93
94 # vim: expandtab shiftwidth=4: