Debian Codegraph @

t/sl_action.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 # SLIF semantics examples
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 6;
24
25 use English qw( -no_match_vars );
26 use Fatal qw( open close );
27 use lib 'inc';
28 use Marpa::R2::Test;
29 use Marpa::R2;
30
31 my $trace_rules = q{};
32
33 # Marpa::R2::Display
34 # name: SLIF action context synopsis
35
36 sub do_S {
37 my ($action_object) = @_;
38 my $rule_id = $Marpa::R2::Context::rule;
39 my $slg = $Marpa::R2::Context::slg;
40 my ( $lhs, @rhs ) =
41 map { $slg->symbol_display_form($_) } $slg->rule_expand($rule_id);
42 $action_object->{text} =
43 "rule $rule_id: $lhs ::= "
44 . ( join q{ }, @rhs ) . "\n"
45 . "locations: "
46 . ( join q{-}, Marpa::R2::Context::location() ) . "\n";
47 return $action_object;
48 } ## end sub do_S
49
50 # Marpa::R2::Display::End
51
52 # Marpa::R2::Display
53 # name: SLIF bail synopsis
54
55 my $bail_message = "This is a bail out message!";
56
57 sub do_bail_with_message_if_A {
58 my ($action_object, $terminal) = @_;
59 Marpa::R2::Context::bail($bail_message) if $terminal eq 'A';
60 }
61
62 sub do_bail_with_object_if_A {
63 my ($action_object, $terminal) = @_;
64 Marpa::R2::Context::bail([$bail_message]) if $terminal eq 'A';
65 }
66
67 # Marpa::R2::Display::End
68
69 my @terminals = qw/A B C D/;
70 my $grammar = Marpa::R2::Scanless::G->new(
71 { source => \<<'END_OF_SOURCE',
72 :start ::= S
73 S ::= A B C D action => main::do_S
74 A ~ 'A'
75 B ~ 'B'
76 C ~ 'C'
77 D ~ 'D'
78 END_OF_SOURCE
79 });
80
81 sub do_parse {
82 my $slr = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
83 $slr->read( \'ABCD' );
84 return $slr->value();
85 } ## end sub do_parse
86
87 my $value_ref;
88 $value_ref = do_parse();
89 VALUE_TEST: {
90 if ( ref $value_ref ne 'REF' ) {
91 my $ref_type = ref $value_ref;
92 Test::More::fail(
93 qq{Parse result ref type is "$ref_type"; it needs to be "REF"});
94 last VALUE_TEST;
95 } ## end if ( ref $value_ref ne 'REF' )
96 my $value = ${$value_ref};
97 if ( ref $value ne 'HASH' ) {
98 my $ref_type = ref $value;
99 Test::More::fail(
100 qq{Parse value ref type is "$ref_type"; it needs to be "HASH"});
101 last VALUE_TEST;
102 } ## end if ( ref $value ne 'HASH' )
103 my $expected_text = qq{rule 0: S ::= A B C D\nlocations: 0-4\n};
104 Test::More::is( $value->{text}, $expected_text, 'Parse ok?' );
105 } ## end VALUE_TEST:
106
107 my $eval_ok;
108 {
109 local *do_S = *do_bail_with_message_if_A;
110 $eval_ok = eval { $value_ref = do_parse(); 1 };
111 }
112 my $actual_eval_error = $EVAL_ERROR
113 // 'no eval error'; # grab it now to be safe
114 Test::More::ok( ( not defined $eval_ok ),
115 "bail with string argument happened" );
116 $actual_eval_error
117 =~ s/\A User \s+ bailed \s+ at \s+ line \s+ \d+ [^\n]* \n/<LOCATION LINE>/xms;
118 Test::More::is(
119 $actual_eval_error,
120 '<LOCATION LINE>' . $bail_message . "\n",
121 "bail with string argument"
122 );
123
124 {
125 local *do_S = *do_bail_with_object_if_A;
126 $eval_ok = eval { $value_ref = do_parse(); 1 };
127 }
128 $actual_eval_error = $EVAL_ERROR;
129 my $eval_error_ref_type = ref $actual_eval_error;
130 my $exception_value_desc =
131 $eval_error_ref_type eq 'ARRAY'
132 ? $actual_eval_error->[0]
133 : "ref type of exception is $eval_error_ref_type";
134 Test::More::ok( ( not defined $eval_ok ),
135 "bail with object argument happened" );
136 Test::More::is( $eval_error_ref_type, 'ARRAY',
137 "bail with object argument ref type" );
138 Test::More::is( $exception_value_desc, $bail_message,
139 "bail with object argument value" );
140
141 # vim: expandtab shiftwidth=4: