Debian Codegraph @

t/sl_discard2.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 SLIF's discard events
18
19 use 5.010001;
20 use strict;
21 use warnings;
22 use Test::More tests => 23;
23 use English qw( -no_match_vars );
24 use Scalar::Util;
25
26 use lib 'inc';
27 use Marpa::R2::Test;
28
29 ## no critic (ErrorHandling::RequireCarping);
30
31 use Marpa::R2;
32
33 my $null_grammar = Marpa::R2::Scanless::G->new(
34 { bless_package => 'My_Nodes',
35 source => \(<<'END_OF_SOURCE'),
36 :default ::= action => [g1start,g1len,name,values]
37 discard default = event => :symbol=off
38 lexeme default = action => [ g1start, g1len, start, length, value ]
39 latm => 1
40
41 Script ::=
42 :discard ~ whitespace event => ws
43 whitespace ~ [\s]
44 END_OF_SOURCE
45 }
46 );
47
48 for my $input ( q{}, ' ', ' ', ' ' ) {
49 my $recce = Marpa::R2::Scanless::R->new(
50 { grammar => $null_grammar },
51 );
52
53 my $length = length $input;
54 my $pos = $recce->read( \$input );
55
56 my $p_events = gather_events( $recce, $pos, $length );
57 my $actual_events = join q{ }, map { $_->[0], $_->[-1] } @{$p_events};
58 my $expected_events = join q{ }, ( ('ws 0') x $length );
59 Test::More::is( $actual_events, $expected_events,
60 "Test of $length discarded spaces" );
61
62 my $value_ref = $recce->value();
63 die "No parse was found\n" if not defined $value_ref;
64
65 } ## end for my $input ( q{}, ' ', ' ', ' ' )
66
67 # Test of 2 types of events
68 my $grammar2 = Marpa::R2::Scanless::G->new(
69 { bless_package => 'My_Nodes',
70 source => \(<<'END_OF_SOURCE'),
71 :default ::= action => [g1start,g1len,name,values]
72 discard default = event => :symbol=off
73 lexeme default = action => [ g1start, g1len, start, length, value ]
74 latm => 1
75
76 Script ::=
77 :discard ~ whitespace event => ws
78 whitespace ~ [\s]
79 :discard ~ bracketed event => bracketed
80 bracketed ~ '(' <no close bracket> ')'
81 <no close bracket> ~ [^)]*
82 END_OF_SOURCE
83 }
84 );
85
86
87 for my $input ( q{ (x) }, q{(x) }, q{ (x)})
88 {
89 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar2 }, );
90
91 my $length = length $input;
92 my $pos = $recce->read( \$input );
93
94 my $p_events = gather_events( $recce, $pos, $length );
95 my $actual_events = join q{ }, map { $_->[0], $_->[-1] } @{$p_events};
96 my $expected_events = $input;
97 $expected_events =~ s/[(] [x]+ [)]/bracketed 0/xms;
98 $expected_events =~ s/\A \s /ws 0 /xms;
99 $expected_events =~ s/\s \z/ ws 0/xms;
100 Test::More::is( $actual_events, $expected_events,
101 qq{Test of two discard types, input="$input"} );
102
103 my $value_ref = $recce->value();
104 die "No parse was found\n" if not defined $value_ref;
105
106 }
107
108 # Discards with a non-trivial grammar
109
110 my $non_trivial_grammar = Marpa::R2::Scanless::G->new(
111 { bless_package => 'My_Nodes',
112 source => \(<<'END_OF_SOURCE'),
113 :default ::= action => [g1start,g1len,name,values]
114 discard default = event => :symbol=off
115 lexeme default = action => [ g1start, g1len, start, length, value ]
116 latm => 1
117
118 text ::= a b c
119 a ~ 'a'
120 b ~ 'b'
121 c ~ 'c'
122 :discard ~ whitespace event => ws
123 whitespace ~ [\s]
124 END_OF_SOURCE
125 }
126 );
127
128 for my $pattern (0 .. 15)
129 {
130 # use binary numbers to generate all possible
131 # space patterns
132 my @spaces = split //xms, sprintf "%04b", $pattern;
133 my @chars = qw{a b c};
134 my @input = ();
135 for my $i (0 .. 2) {
136 push @input, ' ' if $spaces[$i];
137 push @input, $chars[$i];
138 }
139 push @input, ' ' if $spaces[3];
140
141 my @expected = ();
142 for my $i (0 .. 3) {
143 push @expected, "ws $i" if $spaces[$i];
144 }
145
146 # say join q{}, '^', @input, '$';
147 my $input = join q{}, @input;
148
149 my $recce = Marpa::R2::Scanless::R->new( { grammar => $non_trivial_grammar }, );
150
151 my $length = length $input;
152 my $pos = $recce->read( \$input );
153
154 my $p_events = gather_events( $recce, $pos, $length );
155 my $actual_events = join q{ }, map { $_->[0], $_->[-1] } @{$p_events};
156 my $expected_events = join q{ }, @expected;
157 Test::More::is( $actual_events, $expected_events,
158 qq{Test of non-trivial parse, input="$input"} );
159
160 my $value_ref = $recce->value();
161 die "No parse was found\n" if not defined $value_ref;
162
163 }
164
165 sub gather_events {
166 my ($recce, $pos, $length) = @_;
167 my @actual_events;
168 READ: while (1) {
169
170 EVENT:
171 for my $event ( @{ $recce->events() } ) {
172 my ( $name, @other_stuff ) = @{$event};
173 # say STDERR 'Event received!!! -- ', Data::Dumper::Dumper($event);
174 push @actual_events, $event;
175 }
176
177 last READ if $pos >= $length;
178 $pos = $recce->resume($pos);
179 } ## end READ: while (1)
180 return \@actual_events;
181 } ## end sub gather_event
182 # vim: expandtab shiftwidth=4: