Debian Codegraph @

t/sl_dyck.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 # A test using the Dyck-Hollerith language
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 1;
24 use English qw( -no_match_vars );
25 use lib 'inc';
26 use Marpa::R2::Test;
27 use Marpa::R2;
28 use Data::Dumper;
29
30 my $dsl = <<'END_OF_DSL';
31 # The BNF
32 :default ::= action => ::first
33 :start ::= sentence
34 sentence ::= element
35 array ::= 'A' <array count> '(' elements ')'
36 action => check_array
37 string ::= ( 'S' <string length> '(' ) text ( ')' )
38 elements ::= element+
39 action => ::array
40 element ::= string | array
41
42 # Declare the places where we pause before
43 # and after lexemes
44 :lexeme ~ <string length> pause => after event => 'string length'
45 event 'expecting text' = predicted <text>
46
47 # Declare the lexemes themselves
48 <array count> ~ [\d]+
49 <string length> ~ [\d]+
50 # define <text> as one character of anything, as a stub
51 # the external scanner determines its actual size and value
52 text ~ [\d\D]
53 END_OF_DSL
54
55 my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } );
56 my $recce = Marpa::R2::Scanless::R->new(
57 { grammar => $grammar, semantics_package => 'My_Actions' } );
58
59 my $input = 'A2(A2(S3(Hey)S13(Hello, World!))S5(Ciao!))';
60
61 my $last_string_length;
62 my $input_length = length $input;
63 INPUT:
64 for (
65 my $pos = $recce->read( \$input );
66 $pos < $input_length;
67 $pos = $recce->resume($pos)
68 )
69 {
70 EVENT: for my $event ( @{ $recce->events() } ) {
71 my ($name) = @{$event};
72 if ( $name eq 'expecting text' ) {
73 my $text_length = $last_string_length;
74 $recce->lexeme_read( 'text', $pos, $text_length );
75 $pos += $text_length;
76 next EVENT;
77 } ## end if ( $name eq 'expecting text' )
78 if ( $name eq 'string length' ) {
79 my ( $start_pos, $length ) = $recce->pause_span();
80 $last_string_length = $recce->literal( $start_pos, $length ) + 0;
81 $pos = $start_pos + $length;
82 next EVENT;
83 } ## end if ( $name eq 'string length' )
84 die "Unexpected event: ", join q{ }, @{$event};
85 } ## end EVENT: for my $event ( @{ $recce->events() } )
86 } ## end INPUT: for ( my $pos = $recce->read( \$input ); $pos < $input_length...)
87
88 my $result = $recce->value();
89 die 'No parse' if not defined $result;
90 my $received = Data::Dumper::Dumper( ${$result} );
91
92 my $expected = <<'EXPECTED_OUTPUT';
93 $VAR1 = [
94 [
95 'Hey',
96 'Hello, World!'
97 ],
98 'Ciao!'
99 ];
100 EXPECTED_OUTPUT
101 Test::More::is( $received, $expected , 'Dyck-Hollerith value');
102
103 sub My_Actions::check_array {
104 my ( undef, undef, $declared_size, undef, $array ) = @_;
105 my $actual_size = @{$array};
106 warn
107 "Array size ($actual_size) does not match that specified ($declared_size)"
108 if $declared_size != $actual_size;
109 return $array;
110 } ## end sub check_array
111