Debian Codegraph @

t/sl_dwim.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 # This test is mainly a place to record his "dwim" function,
18 # which Jeffrey keeps losing track of.
19 # The DWIM (Do What I Mean) action is intended to be used as
20 # a default action. It looks at the values
21 # of each rule and gives you a compact first cut,
22 # which is reasonably likely to turn out to be
23 # what you want to do with the rule.
24
25 # The example is based on the smaller minimal working example
26 # for Libmarpa issue 115 (Dave Abrahams).
27
28 use 5.010001;
29 use strict;
30 use warnings;
31 use Scalar::Util;
32 use Data::Dumper;
33 use Test::More tests => 1;
34
35 use lib 'inc';
36 use Marpa::R2::Test;
37
38 ## no critic (ErrorHandling::RequireCarping);
39
40 use Marpa::R2;
41
42 my $bnf = <<'END_OF_BNF';
43 :default ::= action => main::dwim
44 lexeme default = latm => 1
45 :start ::= prefixExpr
46 prefixExpr ::= null prefixExpr Arg2
47 prefixExpr ::= Arg1
48 null ::=
49
50 :discard ~ whitespace
51 unicorn ~ [^\d\D]
52 whitespace ~ [\s]+
53 Arg1 ~ unicorn
54 Arg2 ~ unicorn
55 END_OF_BNF
56
57 my $grammar = Marpa::R2::Scanless::G->new(
58 {
59 source => \$bnf,
60 }
61 );
62
63 my $string = <<'EOS';
64 type A {
65 var a: Int
66 fun foo(a: Int) { a.copy() }
67 fun foo(b: Int) -> Int {
68 let { a + b }
69 inout { b += a }
70 }
71 }
72 EOS
73
74 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
75
76 $recce->read( \$string, 0, 0 );
77
78 if ( not defined $recce->lexeme_read( 'Arg1', undef, 1, 'Arg1' ) ) {
79 die qq{Parser rejected token "Arg1"};
80 }
81 if ( not defined $recce->lexeme_read( 'Arg2', undef, 1, 'Arg2' ) ) {
82 die qq{Parser rejected token "Arg2"};
83 }
84 if ( not defined $recce->lexeme_read( 'Arg2', undef, 1, 'Arg2' ) ) {
85 die qq{Parser rejected token "Arg2"};
86 }
87
88 my $value_ref = $recce->value();
89 if ( not defined $value_ref ) {
90 die "No parse was found, after reading the entire input\n";
91 }
92
93 my $expected_value = \[ [ 'Arg1', 'Arg2' ], 'Arg2' ];
94
95 Test::More::is_deeply(
96 Data::Dumper::Dumper($value_ref),
97 Data::Dumper::Dumper($expected_value),
98 'Value of parse'
99 );
100
101 sub main::dwim {
102 my @result = ();
103 shift;
104 ARG: for my $v ( @_ ) {
105 next ARG if not $v;
106 my $type = Scalar::Util::reftype $v;
107 if (not $type or $type ne 'ARRAY') {
108 push @result, $v;
109 next ARG;
110 }
111 my $size = scalar @{$v};
112 next ARG if $size == 0;
113 if ($size == 1) {
114 push @result, ${$v}[0];
115 next ARG;
116 }
117 push @result, $v;
118 }
119 return [@result];
120 }
121
122 # vim: expandtab shiftwidth=4: