This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randal.t
248 lines (220 loc) · 6.43 KB
/
randal.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/perl
# Copyright 2012 Jeffrey Kegler
# This file is part of Marpa::XS. Marpa::XS is free software: you can
# redistribute it and/or modify it under the terms of the GNU Lesser
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Marpa::XS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser
# General Public License along with Marpa::XS. If not, see
# http://www.gnu.org/licenses/.
use 5.010;
use strict;
use warnings;
use English qw( -no_match_vars );
use Test::More tests => 5;
use lib 'tool/lib';
use Marpa::XS::Test;
BEGIN {
Test::More::use_ok('Marpa::XS');
}
package Test_Grammar;
# This grammar is from Data::Dumper,
# which disagrees with Perl::Critic about proper
# use of quotes and with perltidy about
# formatting
#<<< no perltidy
##no critic (ValuesAndExpressions::ProhibitNoisyQuotes)
$Test_Grammar::MARPA_OPTIONS = [
{
'rules' => [
{
'action' => 'comment',
'lhs' => 'comment:optional',
'rhs' => [
'comment'
]
},
{
'lhs' => 'comment:optional',
'rhs' => []
},
{
'action' => 'show_perl_line',
'lhs' => 'perl-line',
'rhs' => [
'perl-statements',
'comment:optional'
]
},
{
'action' => 'show_statement_sequence',
'lhs' => 'perl-statements',
'min' => 1,
'rhs' => [
'perl-statement'
],
'separator' => 'semicolon'
},
{
'action' => 'show_division',
'lhs' => 'perl-statement',
'rhs' => [
'division'
]
},
{
'action' => 'show_function_call',
'lhs' => 'perl-statement',
'rhs' => [
'function-call'
]
},
{
'action' => 'show_die',
'lhs' => 'perl-statement',
'rhs' => [
'die:k0',
'string-literal'
]
},
{
'lhs' => 'division',
'rhs' => [
'expr',
'division-sign',
'expr'
]
},
{
'lhs' => 'expr',
'rhs' => [
'function-call'
]
},
{
'lhs' => 'expr',
'rhs' => [
'number'
]
},
{
'action' => 'show_unary',
'lhs' => 'function-call',
'rhs' => [
'unary-function-name',
'argument'
]
},
{
'action' => 'show_nullary',
'lhs' => 'function-call',
'rhs' => [
'nullary-function-name'
]
},
{
'lhs' => 'argument',
'rhs' => [
'pattern-match'
]
}
],
'start' => 'perl-line',
'terminals' => [
'die:k0',
'unary-function-name',
'nullary-function-name',
'number',
'semicolon',
'division-sign',
'pattern-match',
'comment',
'string-literal'
],
}
];
my %regexes = (
'die:k0' => 'die',
'unary-function-name' => '(caller|eof|sin|localtime)',
'nullary-function-name' => '(caller|eof|sin|time|localtime)',
'number' => '\\d+',
'semicolon' => ';',
'division-sign' => '[/]',
'pattern-match' => '[/][^/]*/',
'comment' => '[#].*',
'string-literal' => '"[^"]*"',
);
## use critic
#>>>
#
package main;
my @test_data = (
[ 'sin',
q{sin / 25 ; # / ; die "this dies!"},
[ 'sin function call, die statement', 'division, comment' ]
],
[ 'time', q{time / 25 ; # / ; die "this dies!"}, ['division, comment'] ]
);
my $g = Marpa::XS::Grammar->new(
{ warnings => 1,
actions => 'main',
},
@{$Test_Grammar::MARPA_OPTIONS}
);
$g->precompute();
TEST: for my $test_data (@test_data) {
my ( $test_name, $test_input, $test_results ) = @{$test_data};
my $recce = Marpa::XS::Recognizer->new( { grammar => $g } );
my $input_length = length $test_input;
pos $test_input = 0;
# Marpa::XS::Display
# name: Recognizer terminals_expected Synopsis
my $terminals_expected = $recce->terminals_expected();
# Marpa::XS::Display::End
for ( my $pos = 0; $pos < $input_length; $pos++ ) {
TOKEN_TYPE: while ( my ( $token, $regex ) = each %regexes ) {
next TOKEN_TYPE if not $token ~~ $terminals_expected;
pos $test_input = $pos;
next TOKEN_TYPE
if not $test_input =~ m{ \G \s* (?<match>$regex) }xgms;
$recce->alternative(
$token,
$+{match}, ## no critic (Variables::ProhibitPunctuationVars)
( ( pos $test_input ) - $pos )
);
} ## end while ( my ( $token, $regex ) = each %regexes )
$recce->earleme_complete();
$terminals_expected = $recce->terminals_expected();
} ## end for ( my $pos = 0; $pos < $input_length; $pos++ )
$recce->end_input();
my @parses;
while ( defined( my $value_ref = $recce->value() ) ) {
my $value = $value_ref ? ${$value_ref} : 'No parse';
push @parses, $value;
}
my $expected_parse_count = scalar @{$test_results};
my $parse_count = scalar @parses;
Marpa::XS::Test::is( $parse_count, $expected_parse_count,
"$test_name: Parse count" );
my $expected = join "\n", sort @{$test_results};
my $actual = join "\n", sort @parses;
Marpa::XS::Test::is( $actual, $expected, "$test_name: Parse match" );
} ## end for my $test_data (@test_data)
sub show_perl_line {
shift;
return join ', ', grep {defined} @_;
}
sub comment { return 'comment' }
sub show_statement_sequence { shift; return join q{, }, @_ }
sub show_division { return 'division' }
sub show_function_call { return $_[1] }
sub show_die { return 'die statement' }
sub show_unary { return $_[1] . ' function call' }
sub show_nullary { return $_[1] . ' function call' }
1; # In case used as "do" file