Debian Codegraph @

t/sequence2.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 # Tests of the sequence in the Marpa::R2::Grammar doc
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Fatal qw(open close);
24 use Test::More tests => 3;
25
26 use lib 'inc';
27 use Marpa::R2::Test;
28 use Marpa::R2;
29
30 sub do_sequence { shift; return 'seq(' . ( join q{;}, @_ ) . ')' }
31 sub do_item { shift; return 'item(' . ( join q{;}, @_ ) . ')' }
32
33 my $grammar;
34 my $recce;
35 my $value_ref;
36 my $value;
37
38 my $min0 =
39 #<<< no perltidy
40 # Marpa::R2::Display
41 # name: Marpa::R2::Grammar min 0 sequence example
42
43 { lhs => 'sequence', rhs => ['item'], min => 0, action => 'do_sequence' }
44
45 # Marpa::R2::Display::End
46 ; # semicolon to terminate rule
47
48 $grammar = Marpa::R2::Grammar->new(
49 { start => 'sequence',
50 terminals => [qw(item)],
51 rules => [$min0],
52 actions => 'main'
53 }
54 );
55
56 $grammar->precompute();
57
58 $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
59
60 $recce->read( 'item', '0');
61 $recce->read( 'item', '1');
62
63 $value_ref = $recce->value();
64 $value = $value_ref ? ${$value_ref} : 'No Parse';
65 $value //= 'undef returned';
66
67 Marpa::R2::Test::is( $value, 'seq(0;1)', 'min 0 value' );
68
69 my $min1 =
70 #<<< no perltidy
71 # Marpa::R2::Display
72 # name: Marpa::R2::Grammar min 1 sequence example
73
74 { lhs => 'sequence', rhs => ['item'], min => 1, action => 'do_sequence' }
75
76 # Marpa::R2::Display::End
77 ; # semicolon to terminate rule
78
79 $grammar = Marpa::R2::Grammar->new({
80 start => 'sequence',
81 rules => [ $min1 ],
82 actions => 'main'
83 });
84
85 $grammar->precompute();
86
87 $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
88
89 $recce->read( 'item', '0');
90 $recce->read( 'item', '1');
91
92
93 $value_ref = $recce->value();
94 $value = $value_ref ? ${$value_ref} : 'No Parse';
95 $value //= 'undef returned';
96
97 Marpa::R2::Test::is( $value, 'seq(0;1)', 'min 1 value' );
98
99 my $multipart = [
100 #<<< no perltidy
101 # Marpa::R2::Display
102 # name: Marpa::R2::Grammar multipart rhs sequence example
103
104 { lhs => 'sequence', rhs => [qw(item)], min => 0, action => 'do_sequence' },
105 { lhs => 'item', rhs => [qw(part1 part2)], action => 'do_item' },
106
107 # Marpa::R2::Display::End
108 ]; # semicolon to terminate rule
109
110 $grammar = Marpa::R2::Grammar->new(
111 { start => 'sequence',
112 terminals => [qw(part1 part2)],
113 rules => $multipart,
114 actions => 'main'
115 }
116 );
117
118
119 $grammar->precompute();
120
121 $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
122
123 $recce->read( 'part1', '0' );
124 $recce->read( 'part2', '1' );
125
126 $value_ref = $recce->value();
127 $value = $value_ref ? ${$value_ref} : 'No Parse';
128 $value //= 'undef returned';
129
130 Marpa::R2::Test::is( $value, 'seq(item(0;1))', 'multipart rhs value' );
131
132 1; # In case used as "do" file
133
134 # Local Variables:
135 # mode: cperl
136 # cperl-indent-level: 4
137 # fill-column: 100
138 # End:
139 # vim: expandtab shiftwidth=4: