Debian Codegraph @

t/curly2.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 warnings;
19 use strict;
20
21 use English qw( -no_match_vars );
22
23 use Test::More ( import => [] );
24 use lib 'pperl';
25
26 BEGIN {
27 my $PPI_problem;
28 CHECK_PPI: {
29 if ( not eval { require PPI } ) {
30 $PPI_problem = "PPI not installed: $EVAL_ERROR";
31 last CHECK_PPI;
32 }
33 if ( not PPI->VERSION(1.206) ) {
34 $PPI_problem = 'PPI 1.206 not installed';
35 }
36 } ## end CHECK_PPI:
37 if ($PPI_problem) {
38 Test::More::plan skip_all => $PPI_problem;
39 }
40 else {
41 Test::More::plan tests => 1;
42 }
43 } ## end BEGIN
44
45 use Marpa::R2;
46 use Marpa::R2::Perl;
47 use lib 'inc';
48 use Marpa::R2::Test;
49
50 my $input_string = <<'END_OF_INPUT';
51 Note: line:column figures include preceding whitepace
52 The next line is a perl fragment
53 {42;{1,2,3;4}}
54 Code block from 3:5 to 3:13
55 Code block from 2:33 to 3:14
56 The next line is a perl fragment
57 {42;{1,2,3,4}}
58 Hash from 7:5 to 7:13
59 Code block from 7:5 to 7:13
60 Code block from 6:33 to 7:14
61 The next line is a perl fragment
62 {42;{;1,2,3;4}}
63 Code block from 12:5 to 12:14
64 Code block from 11:33 to 12:15
65 The next line is a perl fragment
66 {42;+{1,2,3,4}}
67 Hash from 16:6 to 16:14
68 Code block from 15:33 to 16:15
69 END_OF_INPUT
70
71 my $finder = Marpa::R2::Perl->new( { embedded => 1, closures => {} } );
72 my $main_parser = Marpa::R2::Perl->new( { closures => {} } );
73
74 sub linecol {
75 my ($token) = @_;
76 return q{?} if not defined $token;
77 return $token->logical_line_number() . q{:} . $token->column_number;
78 }
79
80 my $tokens = $finder->tokens( \$input_string );
81 $main_parser->clone_tokens($finder);
82 my $count_of_tokens = scalar @{$tokens};
83 my $perl_found = 0;
84 my $next_start = 0;
85 my $main_result = q{};
86
87 PERL_CODE: while (1) {
88 last PERL_CODE if $next_start >= $count_of_tokens;
89 my ( $start, $end ) = $finder->find_perl($next_start);
90 if ( not defined $start ) {
91 $next_start = $end + 1;
92 next PERL_CODE;
93 }
94 $perl_found += ( $end - $start ) + 1;
95 my $perl_code = join q{},
96 map { $_->content() } @{$tokens}[ $start .. $end ];
97 $perl_code =~ s/\A \s*//xms;
98 $perl_code =~ s/\s* \z//xms;
99 $main_result .= "Perl fragment: $perl_code\n";
100 $main_result .= find_curly( $main_parser, $start, $end );
101 $next_start = $end + 1;
102 } ## end PERL_CODE: while (1)
103
104 $main_result .= sprintf "perl tokens = %d; all tokens=%d; %.2f%%\n", $perl_found,
105 $count_of_tokens, ( $perl_found / $count_of_tokens ) * 100;
106
107 Marpa::R2::Test::is( $main_result, <<'END_OF_OUTPUT', 'Output' );
108 Perl fragment: {42;{1,2,3;4}}
109 Code block at 3:5 3:13 {1,2,3;4}
110 Code block at 2:33 3:14 {42;{1,2,3;4}}
111 Perl fragment: {42;{1,2,3,4}}
112 Ambiguous Hash at 7:5 7:13 {1,2,3,4}
113 Ambiguous Code block at 7:5 7:13 {1,2,3,4}
114 Code block at 6:33 7:14 {42;{1,2,3,4}}
115 Perl fragment: {42;{;1,2,3;4}}
116 Code block at 12:5 12:14 {;1,2,3;4}
117 Code block at 11:33 12:15 {42;{;1,2,3;4}}
118 Perl fragment: {42;+{1,2,3,4}}
119 Hash at 16:6 16:14 {1,2,3,4}
120 Code block at 15:33 16:15 {42;+{1,2,3,4}}
121 perl tokens = 62; all tokens=267; 23.22%
122 END_OF_OUTPUT
123
124 sub find_curly {
125 my ( $parser, $token_start_ix, $token_end_ix ) = @_;
126 my $result = q{};
127
128 $parser->read_tokens( $token_start_ix, $token_end_ix );
129
130 my $recce = $parser->{recce};
131 my $earleme_to_token = $parser->{earleme_to_PPI_token};
132 my $PPI_tokens = $parser->{PPI_tokens};
133 my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
134 my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
135 my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES];
136 for my $earley_set_id ( 0 .. $recce->latest_earley_set() ) {
137 my @hash_locations = ();
138 my @code_locations = ();
139 my $progress_report = $recce->progress($earley_set_id);
140 ITEM: for my $progress_item ( @{$progress_report} ) {
141 my ( $rule_id, $position, $origin_earley_set_id ) =
142 @{$progress_item};
143 last ITEM if not defined $rule_id;
144 next ITEM if $position >= 0;
145 $position = $grammar_c->rule_length($rule_id);
146
147 my $origin_earleme = $recce->earleme($origin_earley_set_id);
148
149 my $rule = $rules->[$rule_id];
150 my $rule_name = $rule->[Marpa::R2::Internal::Rule::NAME];
151 next ITEM if not defined $rule_name;
152 my $blocktype =
153 $rule_name eq 'anon_hash' ? 'hash'
154 : $rule_name eq 'block' ? 'code'
155 : $rule_name eq 'mblock' ? 'code'
156 : undef;
157 next ITEM if not defined $blocktype;
158 my $token = $PPI_tokens->[ $earleme_to_token->[$origin_earleme] ];
159 push @hash_locations, [ $origin_earleme, $earley_set_id - 1 ]
160 if $blocktype eq 'hash';
161 push @code_locations, [ $origin_earleme, $earley_set_id - 1 ]
162 if $blocktype eq 'code';
163
164 } ## end ITEM: for my $progress_item ( @{$progress_report} )
165 my @ambiguous = ();
166 push @ambiguous, 'Ambiguous'
167 if scalar @hash_locations and scalar @code_locations;
168 for my $hash_location (@hash_locations) {
169 my ( $start, $end ) = @{$hash_location};
170 my $start_ix = $earleme_to_token->[$start];
171 my $end_ix = $earleme_to_token->[$end];
172 while ( not defined $end_ix ) {
173 $end_ix = $earleme_to_token->[ --$end ];
174 }
175 my $string = join q{},
176 map { $_->content() } @{$tokens}[ $start_ix .. $end_ix ];
177 $string =~ s/\A \s* //xms;
178 $string =~ s/\s* \z//xms;
179 $result .= join q{ }, @ambiguous, 'Hash at',
180 linecol( $PPI_tokens->[$start_ix] ),
181 linecol( $PPI_tokens->[$end_ix] ), $string;
182 $result .= "\n";
183 } ## end for my $hash_location (@hash_locations)
184 for my $code_location (@code_locations) {
185 my ( $start, $end ) = @{$code_location};
186 my $start_ix = $earleme_to_token->[$start];
187 my $end_ix = $earleme_to_token->[$end];
188 while ( not defined $end_ix ) {
189 $end_ix = $earleme_to_token->[ --$end ];
190 }
191 my $string = join q{},
192 map { $_->content() } @{$tokens}[ $start_ix .. $end_ix ];
193 $string =~ s/\A \s*//xms;
194 $string =~ s/\s* \z//xms;
195 $result .= join q{ }, @ambiguous, 'Code block at',
196 linecol( $PPI_tokens->[$start_ix] ),
197 linecol( $PPI_tokens->[$end_ix] ), $string;
198 $result .= "\n";
199 } ## end for my $code_location (@code_locations)
200 } ## end for my $earley_set_id ( 0 .. $recce->latest_earley_set...)
201
202 return $result;
203
204 } ## end sub find_curly
205
206 # vim: expandtab shiftwidth=4: