Debian Codegraph @

t/null_value.t

raw
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 use 5.010001;
18 use strict;
19 use warnings;
20
21 use Test::More tests => 1;
22 use lib 'inc';
23 use Marpa::R2::Test;
24 use Marpa::R2;
25
26 package Test;
27
28 # The start rule
29
30 sub new { my $class = shift; return bless {}, $class }
31
32 ## no critic (Subroutines::RequireArgUnpacking)
33 sub rule0 {
34 return $_[1] . ', but ' . $_[2];
35 }
36 ## use critic
37
38 sub rule1 { return 'A is missing' }
39 sub rule2 { return q{I'm sometimes null and sometimes not} }
40 sub rule3 { return 'B is missing' }
41 sub rule4 { return 'C is missing' }
42 sub rule5 { return 'C matches Y' }
43 sub rule6 { return 'Zorro was here' }
44
45 package Test_Grammar;
46
47 $Test_Grammar::MARPA_OPTIONS = [
48 { 'rules' => [
49 { 'action' => 'rule0',
50 'lhs' => 's',
51 'rhs' => [ 'a', 'y' ]
52 },
53 { 'lhs' => 'a',
54 'rhs' => [],
55 action => 'rule1',
56 },
57 { 'action' => 'rule2',
58 'lhs' => 'a',
59 'rhs' => [ 'b', 'c' ]
60 },
61 { 'lhs' => 'b',
62 'rhs' => [],
63 action => 'rule3'
64 },
65 { 'lhs' => 'c',
66 'rhs' => [],
67 action => 'rule4'
68 },
69 { 'action' => 'rule5',
70 'lhs' => 'c',
71 'rhs' => ['y']
72 },
73 { 'action' => 'rule6',
74 'lhs' => 'y',
75 'rhs' => ['Z']
76 }
77 ],
78 'start' => 's',
79 'terminals' => ['Z'],
80 'action_object' => 'Test'
81 }
82 ];
83
84 package main;
85
86 my $g = Marpa::R2::Grammar->new( @{$Test_Grammar::MARPA_OPTIONS} );
87 $g->precompute();
88 my $recce = Marpa::R2::Recognizer->new( { grammar => $g } );
89 $recce->read( 'Z', 'Z' );
90 my $ref_value = $recce->value();
91 my $value = $ref_value ? ${$ref_value} : 'No parse';
92 Marpa::R2::Test::is(
93 $value,
94 'A is missing, but Zorro was here',
95 'null value example'
96 );
97
98 1; # In case used as "do" file
99
100 # Local Variables:
101 # mode: cperl
102 # cperl-indent-level: 4
103 # fill-column: 100
104 # End:
105 # vim: expandtab shiftwidth=4: