Debian Codegraph @

t/curly.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 Getopt::Long ();
24 use Test::More ( import => [] );
25 use lib 'pperl';
26
27 BEGIN {
28 my $PPI_problem;
29 CHECK_PPI: {
30 if ( not eval { require PPI } ) {
31 $PPI_problem = "PPI not installed: $EVAL_ERROR";
32 last CHECK_PPI;
33 }
34 if ( not PPI->VERSION(1.206) ) {
35 $PPI_problem = 'PPI 1.206 not installed';
36 }
37 } ## end CHECK_PPI:
38 if ($PPI_problem) {
39 Test::More::plan skip_all => $PPI_problem;
40 }
41 else {
42 Test::More::plan tests => 8;
43 }
44 } ## end BEGIN
45
46 use Marpa::R2;
47 use Marpa::R2::Perl;
48 use lib 'inc';
49 use Marpa::R2::Test;
50
51 # Run in utility mode?
52 my $utility = 0;
53 die if not Getopt::Long::GetOptions( utility => \$utility );
54
55 my %hash;
56 my %codeblock;
57
58 my @tests;
59 if ($utility) {
60 my $string = do { local $RS = undef; <STDIN> };
61 @tests = ( [ $string, q{} ] );
62 }
63 else {
64 @tests = (
65 [ '{42;{1,2,3;4}}', << 'END_OF_RESULT', 1
66 Code block at line 1, column 1
67 Code block at line 1, column 5
68 END_OF_RESULT
69 ],
70 [ '{42;{1,2,3,4}}', << 'END_OF_RESULT', 2
71 Code block at line 1, column 1
72 Code block at line 1, column 5
73 Hash at line 1, column 5
74 END_OF_RESULT
75 ],
76 [ '{42;{;1,2,3;4}}', << 'END_OF_RESULT', 1
77 Code block at line 1, column 1
78 Code block at line 1, column 5
79 END_OF_RESULT
80 ],
81 [ '{42;+{1,2,3,4}}', << 'END_OF_RESULT', 1
82 Code block at line 1, column 1
83 Hash at line 1, column 6
84 END_OF_RESULT
85 ],
86 );
87 } ## end else [ if ($utility) ]
88
89 my $parser = Marpa::R2::Perl->new( { closures => {} } );
90
91 TEST: for my $test (@tests) {
92
93 my ( $string, $expected, $expected_parse_count ) = @{$test};
94 $parser = $parser->read( \$string );
95 my @values = $parser->eval();
96 my $recce = $parser->{recce};
97 my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
98 my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
99 my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES];
100 for my $earley_set_id ( 0 .. $recce->latest_earley_set() ) {
101 my $progress_report = $recce->progress($earley_set_id);
102 ITEM: for my $progress_item ( @{$progress_report} ) {
103 my ( $rule_id, $position, $origin_earley_set_id ) = @{$progress_item};
104 last ITEM if not defined $rule_id;
105 next ITEM if $position >= 0;
106 $position = $grammar_c->rule_length($rule_id);
107
108 # Marpa::R2::Display
109 # name: earleme() Synopsis
110
111 my $origin_earleme = $recce->earleme($origin_earley_set_id);
112
113 # Marpa::R2::Display::End
114
115 my $rule = $rules->[$rule_id];
116 my $rule_name = $rule->[Marpa::R2::Internal::Rule::NAME];
117 next ITEM if not defined $rule_name;
118 my $blocktype =
119 $rule_name eq 'anon_hash' ? 'hash'
120 : $rule_name eq 'block' ? 'code'
121 : $rule_name eq 'mblock' ? 'code'
122 : undef;
123 next ITEM if not defined $blocktype;
124 my $PPI_tokens = $parser->{PPI_tokens};
125 my $earleme_to_token = $parser->{earleme_to_PPI_token};
126 my $token = $PPI_tokens->[ $earleme_to_token->[$origin_earleme] ];
127 my $location = 'line '
128 . $token->logical_line_number()
129 . q{, column }
130 . $token->column_number;
131 $hash{$location}++ if $blocktype eq 'hash';
132 $codeblock{$location}++ if $blocktype eq 'code';
133 } ## end for my $progress_item ( @{$progress_report} )
134 } ## end for my $earley_set_id ( 0 .. $recce->latest_earley_set...)
135 Marpa::R2::Test::is(
136 ( scalar @values ),
137 $expected_parse_count,
138 'Count of values'
139 );
140 my @result;
141 for my $location ( sort keys %hash ) {
142 push @result, "Hash at $location\n";
143 }
144 for my $location ( sort keys %codeblock ) {
145 push @result, "Code block at $location\n";
146 }
147 my $result = join q{}, sort @result;
148 if ($utility) {
149 say $result or die 'say builtin failed';
150 }
151 else {
152 Marpa::R2::Test::is( $result, $expected, qq{Test of "$string"} );
153 }
154 %hash = ();
155 %codeblock = ();
156 } ## end for my $test (@tests)
157
158 # vim: expandtab shiftwidth=4: