Debian Codegraph @

t/sl_diag.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 # Test of scannerless parsing -- diagnostics
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 12;
24 use English qw( -no_match_vars );
25 use lib 'inc';
26 use Marpa::R2::Test;
27 use Marpa::R2;
28
29 my $dsl = <<'END_OF_RULES';
30 :start ::= Script
31 Script ::= Calculation* action => do_list
32 Calculation ::= Expression | ('say') Expression
33 Expression ::=
34 Number
35 | ('+') Expression Expression action => do_add
36 Number ~ [\d] +
37 :discard ~ whitespace
38 whitespace ~ [\s]+
39 # allow comments
40 :discard ~ <hash comment>
41 <hash comment> ~ <terminated hash comment> | <unterminated
42 final hash comment>
43 <terminated hash comment> ~ '#' <hash comment body> <vertical space char>
44 <unterminated final hash comment> ~ '#' <hash comment body>
45 <hash comment body> ~ <hash comment char>*
46 <vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
47 <hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
48 END_OF_RULES
49
50 my $grammar = Marpa::R2::Scanless::G->new(
51 { action_object => 'My_Actions',
52 default_action => 'do_arg0',
53 source => \$dsl,
54 }
55 );
56
57 my $g0_rules_description;
58
59 # Marpa::R2::Display
60 # name: Scanless g0_rule() synopsis
61
62 my @g0_rule_ids = $grammar->g0_rule_ids();
63 for my $g0_rule_id (@g0_rule_ids) {
64 $g0_rules_description .= "$g0_rule_id "
65 . ( join q{ }, map {"<$_>"} $grammar->g0_rule($g0_rule_id) ) . "\n";
66 }
67
68 # Marpa::R2::Display::End
69
70 Marpa::R2::Test::is(
71 $g0_rules_description,
72 <<'END_OF_DESCRIPTION',
73 0 <[Lex-0]> <[[s]]> <[[a]]> <[[y]]>
74 1 <[Lex-1]> <[[\+]]>
75 2 <Number> <[[\d]]>
76 3 <[:discard]> <whitespace>
77 4 <whitespace> <[[\s]]>
78 5 <[:discard]> <hash comment>
79 6 <hash comment> <terminated hash comment>
80 7 <hash comment> <unterminated final hash comment>
81 8 <terminated hash comment> <[[\#]]> <hash comment body> <vertical space char>
82 9 <unterminated final hash comment> <[[\#]]> <hash comment body>
83 10 <hash comment body> <hash comment char>
84 11 <vertical space char> <[[\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]]>
85 12 <hash comment char> <[[^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]]>
86 13 <[:start_lex]> <Number>
87 14 <[:start_lex]> <[:discard]>
88 15 <[:start_lex]> <[Lex-0]>
89 16 <[:start_lex]> <[Lex-1]>
90 END_OF_DESCRIPTION
91 'g0_rule_ids() and g0_rule()'
92 );
93
94 my $g1_rules_description;
95
96 # Marpa::R2::Display
97 # name: Scanless rule() synopsis
98
99 my @g1_rule_ids = $grammar->g1_rule_ids();
100 for my $g1_rule_id (@g1_rule_ids) {
101 $g1_rules_description .= "$g1_rule_id "
102 . ( join q{ }, map {"<$_>"} $grammar->rule($g1_rule_id) ) . "\n";
103 }
104
105 # Marpa::R2::Display::End
106
107 Marpa::R2::Test::is(
108 $g1_rules_description,
109 <<'END_OF_DESCRIPTION',
110 0 <Script> <Calculation>
111 1 <Calculation> <Expression>
112 2 <Calculation> <[Lex-0]> <Expression>
113 3 <Expression> <Number>
114 4 <Expression> <[Lex-1]> <Expression> <Expression>
115 5 <[:start]> <Script>
116 END_OF_DESCRIPTION
117 'g1_rule_ids() and rule()'
118 );
119
120 package My_Actions;
121 # The SELF object is a very awkward way of specifying the per-parse
122 # argument directly, one which was necessary before the $recce->value()
123 # method took an argument.
124 # This way of doing things is discourage and preserved here for testing purposes.
125 our $SELF;
126 sub new { return $SELF }
127
128 sub do_list {
129 my ( $self, @results ) = @_;
130 return +( scalar @results ) . ' results: ' . join q{ }, @results;
131 }
132
133 sub do_add { shift; return $_[0] + $_[1] }
134 sub do_arg0 { shift; return shift; }
135
136 sub show_last_expression {
137 my ($self) = @_;
138 my $recce = $self->{recce};
139 my ( $start, $end ) = $recce->last_completed_range('Expression');
140 return if not defined $start;
141 my $last_expression = $recce->range_to_string( $start, $end );
142 return $last_expression;
143 } ## end sub show_last_expression
144
145 package main;
146
147 sub my_parser {
148 my ( $grammar, $string ) = @_;
149
150 my $self = bless { grammar => $grammar }, 'My_Actions';
151 local $My_Actions::SELF = $self;
152
153 my $trace_output = q{};
154 open my $trace_fh, q{>}, \$trace_output;
155 my $recce = Marpa::R2::Scanless::R->new(
156 { grammar => $grammar,
157 trace_terminals => 2,
158 trace_file_handle => $trace_fh,
159 too_many_earley_items => 100, # test this
160 }
161 );
162 $self->{recce} = $recce;
163 my ( $parse_value, $parse_status, $last_expression );
164
165 my $eval_ok = eval { $recce->read( \$string ); 1 };
166 close $trace_fh;
167
168 if ( not defined $eval_ok ) {
169 my $abbreviated_error = $EVAL_ERROR;
170 chomp $abbreviated_error;
171 $abbreviated_error =~ s/\n.*//xms;
172 die $self->show_last_expression(), $EVAL_ERROR;
173 } ## end if ( not defined $eval_ok )
174 my $value_ref = $recce->value;
175 if ( not defined $value_ref ) {
176 die join q{ },
177 'Input read to end but no parse',
178 $self->show_last_expression();
179 }
180 return $recce, ${$value_ref}, $trace_output;
181 } ## end sub my_parser
182
183 my @tests_data = (
184 [ '+++ 1 2 3 + + 1 2 4', '1 results: 13', 'Parse OK', 'entire input' ],
185 );
186
187 TEST:
188 for my $test_data (@tests_data) {
189 my ($test_string, $expected_value,
190 $expected_result, $expected_last_expression
191 ) = @{$test_data};
192 my ( $recce, $actual_value, $trace_output ) =
193 my_parser( $grammar, $test_string );
194
195 # Marpa::R2::Display
196 # name: Scanless terminals_expected() synopsis
197
198 my @terminals_expected = @{$recce->terminals_expected()};
199
200 # Marpa::R2::Display::End
201
202 Marpa::R2::Test::is(
203 ( join q{ }, sort @terminals_expected ),
204 'Number [Lex-0] [Lex-1]',
205 qq{SLIF terminals_expected()}
206 );
207
208 # Marpa::R2::Display
209 # name: Scanless show_progress() synopsis
210
211 my $show_progress_output = $recce->show_progress();
212
213 # Marpa::R2::Display::End
214
215 Marpa::R2::Test::is( $show_progress_output,
216 <<'END_OF_EXPECTED_OUTPUT', qq{Scanless show_progess()} );
217 F0 @0-11 L1c1-19 Script -> Calculation * .
218 P1 @11-11 L1c19 Calculation -> . Expression
219 F1 @0-11 L1c1-19 Calculation -> Expression .
220 P2 @11-11 L1c19 Calculation -> . 'say' Expression
221 P3 @11-11 L1c19 Expression -> . Number
222 F3 @10-11 L1c17-19 Expression -> Number .
223 P4 @11-11 L1c19 Expression -> . '+' Expression Expression
224 F4 x2 @0,6-11 L1c1-19 Expression -> '+' Expression Expression .
225 F5 @0-11 L1c1-19 :start -> Script .
226 END_OF_EXPECTED_OUTPUT
227
228 Marpa::R2::Test::is( $actual_value, $expected_value,
229 qq{Value of "$test_string"} );
230 Marpa::R2::Test::is( $trace_output,
231 <<'END_OF_OUTPUT', qq{Trace output for "$test_string"} );
232 Setting trace_terminals option
233 Expecting "Number" at earleme 0
234 Expecting "[Lex-0]" at earleme 0
235 Expecting "[Lex-1]" at earleme 0
236 Registering character U+002b '+' as symbol 5: [\+]
237 Registering character U+002b '+' as symbol 9: [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
238 Accepted lexeme L1c1 e1: '+'; value="+"
239 Accepted lexeme L1c2 e2: '+'; value="+"
240 Accepted lexeme L1c3 e3: '+'; value="+"
241 Registering character U+0020 as symbol 7: [\s]
242 Registering character U+0020 as symbol 9: [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
243 Registering character U+0031 '1' as symbol 6: [\d]
244 Registering character U+0031 '1' as symbol 9: [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
245 Discarded lexeme L1c4: whitespace
246 Accepted lexeme L1c5 e4: Number; value="1"
247 Registering character U+0032 '2' as symbol 6: [\d]
248 Registering character U+0032 '2' as symbol 9: [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
249 Discarded lexeme L1c6: whitespace
250 Accepted lexeme L1c7 e5: Number; value="2"
251 Registering character U+0033 '3' as symbol 6: [\d]
252 Registering character U+0033 '3' as symbol 9: [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
253 Discarded lexeme L1c8: whitespace
254 Accepted lexeme L1c9 e6: Number; value="3"
255 Discarded lexeme L1c10: whitespace
256 Accepted lexeme L1c11 e7: '+'; value="+"
257 Discarded lexeme L1c12: whitespace
258 Accepted lexeme L1c13 e8: '+'; value="+"
259 Discarded lexeme L1c14: whitespace
260 Accepted lexeme L1c15 e9: Number; value="1"
261 Discarded lexeme L1c16: whitespace
262 Accepted lexeme L1c17 e10: Number; value="2"
263 Registering character U+0034 '4' as symbol 6: [\d]
264 Registering character U+0034 '4' as symbol 9: [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
265 Discarded lexeme L1c18: whitespace
266 Accepted lexeme L1c19 e11: Number; value="4"
267 END_OF_OUTPUT
268
269 my $expected_progress_output = [
270 [ 0, -1, 0 ],
271 [ 1, -1, 0 ],
272 [ 3, -1, 10 ],
273 [ 4, -1, 0 ],
274 [ 4, -1, 6 ],
275 [ 5, -1, 0 ],
276 [ 1, 0, 11 ],
277 [ 2, 0, 11 ],
278 [ 3, 0, 11 ],
279 [ 4, 0, 11 ],
280 ];
281
282 # Marpa::R2::Display
283 # name: Scanless progress() synopsis
284
285 my $progress_output = $recce->progress();
286
287 # Marpa::R2::Display::End
288
289 Marpa::R2::Test::is(
290 Data::Dumper::Dumper($progress_output),
291 Data::Dumper::Dumper($expected_progress_output),
292 qq{Scanless progress()}
293 );
294
295 my $latest_g1_location = $recce->latest_g1_location();
296 Test::More::is( $latest_g1_location, 11, qq{Scanless latest_g1_location()} );
297
298 # Marpa::R2::Display
299 # name: Scanless current_g1_location() synopsis
300
301 my $current_g1_location = $recce->current_g1_location();
302
303 # Marpa::R2::Display::End
304
305 Test::More::is( $current_g1_location, 11, qq{Scanless current_g1_location()} );
306
307 # Marpa::R2::Display
308 # name: SLIF pos() example
309
310 my $pos = $recce->pos();
311
312 # Marpa::R2::Display::End
313
314 Test::More::is( $pos, 19, qq{Scanless pos()} );
315
316 # Marpa::R2::Display
317 # name: SLIF input_length() example
318
319 my $input_length = $recce->input_length();
320
321 # Marpa::R2::Display::End
322
323 Test::More::is( $input_length, 19, qq{Scanless input_length()} );
324
325 # Test translation from G1 location to input stream spans
326 my %location_seen = ();
327 my @spans = ();
328 for my $g1_location (
329 sort { $a <=> $b }
330 grep { !$location_seen{$_}++; } map { $_->[-1] } @{$progress_output}
331 )
332 {
333
334 # Marpa::R2::Display
335 # name: Scanless g1_location_to_span() synopsis
336
337 my ( $span_start, $span_length ) =
338 $recce->g1_location_to_span($g1_location);
339
340 # Marpa::R2::Display::End
341
342 push @spans, [ $g1_location, $span_start, $span_length ];
343 } ## end for my $g1_location ( sort { $a <=> $b } grep { !$location_seen...})
344
345 # One result for each unique G1 location in progress report
346 # Format of each result is [g1_location, span_start, span_length]
347 my $expected_spans =
348 [ [ 0, 0, 0 ], [ 6, 8, 1 ], [ 10, 16, 1 ], [ 11, 18, 1 ] ];
349 Test::More::is_deeply( \@spans, $expected_spans,
350 qq{Scanless g1_location_to_span()} );
351
352 } ## end TEST: for my $test_data (@tests_data)
353
354 # vim: expandtab shiftwidth=4: