Debian Codegraph @

t/sl_gsyn.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 # Synopsis for Scannerless version of Stuizand interface
18
19 use 5.010001;
20 use strict;
21 use warnings;
22 use Test::More tests => 5;
23 use English qw( -no_match_vars );
24
25 use lib 'inc';
26 use Marpa::R2::Test;
27
28 ## no critic (ErrorHandling::RequireCarping);
29
30 # Marpa::R2::Display
31 # name: Scanless grammar synopsis
32
33 use Marpa::R2;
34
35 my $grammar = Marpa::R2::Scanless::G->new(
36 {
37 source => \(<<'END_OF_SOURCE'),
38 :default ::= action => do_first_arg
39 :start ::= Script
40 Script ::= Expression+ separator => comma action => do_script
41 comma ~ [,]
42 Expression ::=
43 Number
44 | '(' Expression ')' action => do_parens assoc => group
45 || Expression '**' Expression action => do_pow assoc => right
46 || Expression '*' Expression action => do_multiply
47 | Expression '/' Expression action => do_divide
48 || Expression '+' Expression action => do_add
49 | Expression '-' Expression action => do_subtract
50 Number ~ [\d]+
51
52 :discard ~ whitespace
53 whitespace ~ [\s]+
54 # allow comments
55 :discard ~ <hash comment>
56 <hash comment> ~ <terminated hash comment> | <unterminated
57 final hash comment>
58 <terminated hash comment> ~ '#' <hash comment body> <vertical space char>
59 <unterminated final hash comment> ~ '#' <hash comment body>
60 <hash comment body> ~ <hash comment char>*
61 <vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
62 <hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
63 END_OF_SOURCE
64 }
65 );
66
67 my $input = '42 * 1 + 7';
68 my $value_ref = $grammar->parse( \$input, 'My_Actions' );
69
70 # Marpa::R2::Display::End
71
72 Marpa::R2::Test::is( ${$value_ref}, 49, 'Synopsis value test');
73
74 my $show_rules_output = $grammar->show_rules();
75 $show_rules_output .= $grammar->show_rules(1, 'L0');
76
77 Marpa::R2::Test::is( $show_rules_output,
78 <<'END_OF_SHOW_RULES_OUTPUT', 'Scanless show_rules()' );
79 G1 R0 Script ::= Expression +
80 G1 R1 Expression ::= Expression
81 G1 R2 Expression ::= Expression
82 G1 R3 Expression ::= Expression
83 G1 R4 Expression ::= Expression
84 G1 R5 Expression ::= Number
85 G1 R6 Expression ::= '(' Expression ')'
86 G1 R7 Expression ::= Expression '**' Expression
87 G1 R8 Expression ::= Expression '*' Expression
88 G1 R9 Expression ::= Expression '/' Expression
89 G1 R10 Expression ::= Expression '+' Expression
90 G1 R11 Expression ::= Expression '-' Expression
91 G1 R12 :start ::= Script
92 L0 R0 comma ::= [,]
93 L0 R1 '(' ::= [\(]
94 L0 R2 ')' ::= [\)]
95 L0 R3 '**' ::= [\*] [\*]
96 L0 R4 '*' ::= [\*]
97 L0 R5 '/' ::= [\/]
98 L0 R6 '+' ::= [\+]
99 L0 R7 '-' ::= [\-]
100 L0 R8 Number ::= [\d] +
101 L0 R9 :discard ::= whitespace
102 L0 R10 whitespace ::= [\s] +
103 L0 R11 :discard ::= <hash comment>
104 L0 R12 <hash comment> ::= <terminated hash comment>
105 L0 R13 <hash comment> ::= <unterminated final hash comment>
106 L0 R14 <terminated hash comment> ::= [\#] <hash comment body> <vertical space char>
107 L0 R15 <unterminated final hash comment> ::= [\#] <hash comment body>
108 L0 R16 <hash comment body> ::= <hash comment char> *
109 L0 R17 <vertical space char> ::= [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
110 L0 R18 <hash comment char> ::= [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
111 L0 R19 :start_lex ::= Number
112 L0 R20 :start_lex ::= :discard
113 L0 R21 :start_lex ::= '('
114 L0 R22 :start_lex ::= ')'
115 L0 R23 :start_lex ::= '**'
116 L0 R24 :start_lex ::= '*'
117 L0 R25 :start_lex ::= '/'
118 L0 R26 :start_lex ::= '+'
119 L0 R27 :start_lex ::= '-'
120 L0 R28 :start_lex ::= comma
121 END_OF_SHOW_RULES_OUTPUT
122
123 sub my_parser {
124 my ( $grammar, $p_input_string ) = @_;
125
126 # Marpa::R2::Display
127 # name: Scanless recognizer synopsis
128
129 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
130 my $self = bless { grammar => $grammar }, 'My_Actions';
131 $self->{recce} = $recce;
132
133 if ( not defined eval { $recce->read($p_input_string); 1 }
134 )
135 {
136 ## Add last expression found, and rethrow
137 my $eval_error = $EVAL_ERROR;
138 chomp $eval_error;
139 die $self->show_last_expression(), "\n", $eval_error, "\n";
140 } ## end if ( not defined eval { $event_count = $recce->read...})
141
142 my $value_ref = $recce->value( $self );
143 if ( not defined $value_ref ) {
144 die $self->show_last_expression(), "\n",
145 "No parse was found, after reading the entire input\n";
146 }
147
148 # Marpa::R2::Display::End
149
150 return ${$value_ref};
151
152 } ## end sub my_parser
153
154 my @tests = (
155 [ '42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3)' =>
156 qr/\A 86[.]3\d+ \s+ 126 \s+ 125 \s+ 16\z/xms
157 ],
158 [ '42*3+7, 42 * 3 + 7, 42 * 3+7' => qr/ \s* 133 \s+ 133 \s+ 133 \s* /xms
159 ],
160 [ '15329 + 42 * 290 * 711, 42*3+7, 3*3+4* 4' =>
161 qr/ \s* 8675309 \s+ 133 \s+ 25 \s* /xms
162 ],
163 );
164
165 for my $test (@tests) {
166 my ( $input, $output_re ) = @{$test};
167 my $value = my_parser( $grammar, \$input );
168 Test::More::like( $value, $output_re, 'Value of scannerless parse' );
169 }
170
171 # Marpa::R2::Display
172 # name: Scanless recognizer semantics
173
174 package My_Actions;
175
176 sub do_parens { shift; return $_[1] }
177 sub do_add { shift; return $_[0] + $_[2] }
178 sub do_subtract { shift; return $_[0] - $_[2] }
179 sub do_multiply { shift; return $_[0] * $_[2] }
180 sub do_divide { shift; return $_[0] / $_[2] }
181 sub do_pow { shift; return $_[0]**$_[2] }
182 sub do_first_arg { shift; return shift; }
183 sub do_script { shift; return join q{ }, @_ }
184
185 # Marpa::R2::Display::End
186
187 # Marpa::R2::Display
188 # name: Scanless recognizer diagnostics
189
190 sub show_last_expression {
191 my ($self) = @_;
192 my $recce = $self->{recce};
193 my ( $g1_start, $g1_len ) = $recce->last_completed('Expression');
194 return 'No expression was successfully parsed' if not defined $g1_start;
195 my $last_expression = $recce->substring( $g1_start, $g1_len );
196 return "Last expression successfully parsed was: $last_expression";
197 } ## end sub show_last_expression
198
199 # Marpa::R2::Display::End
200
201 # Local Variables:
202 # mode: cperl
203 # cperl-indent-level: 4
204 # fill-column: 100
205 # End:
206 # vim: expandtab shiftwidth=4: