Debian Codegraph @

t/balanced.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 use English qw( -no_match_vars );
21
22 use List::Util qw(min);
23 use Test::More tests => 7;
24 use lib 'inc';
25 use Marpa::R2::Test;
26 use Marpa::R2;
27
28 sub find_match {
29 my ($s) = @_;
30
31 my $grammar_args = {
32 start => 'S',
33 rules => [
34 [ S => [qw(prefix first_balanced endmark )] ],
35 { lhs => 'S',
36 rhs => [qw(prefix first_balanced )]
37 },
38 { lhs => 'prefix', rhs => [qw(prefix_char)], min => 0 },
39 { lhs => 'prefix_char', rhs => [qw(xlparen)] },
40 { lhs => 'prefix_char', rhs => [qw(rparen)] },
41 { lhs => 'lparen', rhs => [qw(xlparen)] },
42 { lhs => 'lparen', rhs => [qw(ilparen)] },
43 { lhs => 'first_balanced',
44 rhs => [qw(xlparen balanced_sequence rparen)],
45 },
46 { lhs => 'balanced',
47 rhs => [qw(lparen balanced_sequence rparen)],
48 },
49 { lhs => 'balanced_sequence',
50 rhs => [qw(balanced)],
51 min => 0,
52 },
53 ],
54 };
55
56 my $grammar = Marpa::R2::Grammar->new($grammar_args);
57
58 $grammar->precompute();
59
60 my ($first_balanced_rule) =
61 grep { ( $grammar->rule($_) )[0] eq 'first_balanced' }
62 $grammar->rule_ids();
63
64 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
65
66 # Marpa::R2::Display
67 # name: Recognizer expected_symbol_event_set() Synopsis
68
69 $recce->expected_symbol_event_set( 'endmark', 1 );
70
71 # Marpa::R2::Display::End
72
73 my $location = 0;
74 my $string_length = length $s;
75 my $end_of_match;
76
77 # find the match which ends first -- the one which starts
78 # first must start at or before it does
79 CHAR: while ( $location < $string_length ) {
80 my $value = substr $s, $location, 1;
81 my $event_count;
82 if ( $value eq '(' ) {
83
84 # say "Adding xlparen at $location";
85 $event_count = $recce->read('xlparen');
86 }
87 else {
88 # say "Adding rparen at $location";
89 $event_count = $recce->read('rparen');
90 }
91 if ( $event_count
92 and grep { $_->[0] eq 'SYMBOL_EXPECTED' } @{ $recce->events() } )
93 {
94 $end_of_match = $location + 1;
95 last CHAR;
96 } ## end if ( $event_count and grep { $_->[0] eq 'SYMBOL_EXPECTED'...})
97 $location++;
98 } ## end CHAR: while ( $location < $string_length )
99
100 if ( not defined $end_of_match ) {
101 say "No balanced parens";
102 return 0;
103 }
104
105 CHAR: while ( ++$location < $string_length ) {
106 my $value = substr $s, $location, 1;
107 my $token = $value eq '(' ? 'ilparen' : 'rparen';
108
109 # say "Adding $token at $location";
110 my $event_count = $recce->read($token);
111 last CHAR if not defined $event_count;
112 if ( $event_count
113 and grep { $_->[0] eq 'SYMBOL_EXPECTED' } @{ $recce->events() } )
114 {
115 $end_of_match = $location + 1;
116 }
117 } ## end CHAR: while ( ++$location < $string_length )
118
119 my $report = $recce->progress($end_of_match);
120
121 # say Dumper($report);
122 my $start_of_match = List::Util::min map { $_->[2] }
123 grep { $_->[1] < 0 && $_->[0] == $first_balanced_rule } @{$report};
124 return "$start_of_match-$end_of_match";
125
126 } ## end sub find_match
127
128 my $base_string = '(' x 40;
129 my $target = '(()())';
130 for my $pos (
131 0, 1, 2,
132 -( 2 + length $target ),
133 -( 1 + length $target ),
134 -( length $target )
135 )
136 {
137 my $test_string = $base_string;
138 substr $test_string, $pos, ( length $target ), $target;
139 my ( $expected_start, $expected_end );
140 if ( $pos >= 0 ) {
141 $expected_start = $pos;
142 $expected_end = $pos + length $target;
143 }
144 else {
145 $expected_start = $pos + 40;
146 $expected_end = $pos + 40 + length $target;
147 }
148 my $expected = $expected_start . q{-} . $expected_end;
149 Marpa::R2::Test::is( find_match($test_string),
150 $expected, "target at pos $pos" );
151 } ## end for my $pos ( 0, 1, 2, -( 2 + length $target ), -( 1 ...))
152 my $test_string = '(' x 20 . ')' x 20;
153 Marpa::R2::Test::is( find_match($test_string), '0-40', 'Middle target' );