Debian Codegraph @

t/context.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 # NAIF semantics examples
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 7;
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: Action context synopsis
35
36 sub do_S {
37 my ($action_object) = @_;
38 my $rule_id = $Marpa::R2::Context::rule;
39 my $grammar = $Marpa::R2::Context::grammar;
40 my ( $lhs, @rhs ) = $grammar->rule($rule_id);
41 $action_object->{text} =
42 "rule $rule_id: $lhs ::= "
43 . ( join q{ }, @rhs ) . "\n"
44 . "locations: "
45 . ( join q{-}, Marpa::R2::Context::location() ) . "\n";
46 return $action_object;
47 } ## end sub do_S
48
49 # Marpa::R2::Display::End
50
51 # Marpa::R2::Display
52 # name: Semantics bail synopsis
53
54 my $bail_message = "This is a bail out message!";
55
56 sub do_bail_with_message_if_A {
57 my ($action_object, $terminal) = @_;
58 Marpa::R2::Context::bail($bail_message) if $terminal eq 'A';
59 }
60
61 sub do_bail_with_object_if_A {
62 my ($action_object, $terminal) = @_;
63 Marpa::R2::Context::bail([$bail_message]) if $terminal eq 'A';
64 }
65
66 # Marpa::R2::Display::End
67
68 my @terminals = qw/A B C D/;
69 my $grammar = Marpa::R2::Grammar->new(
70 { start => 'S',
71 rules =>
72 [ { lhs => 'S', rhs => \@terminals, action => 'main::do_S' }, ],
73 symbols => { map { ( $_ => { terminal => 1 } ) } @terminals }
74 }
75 );
76
77 $grammar->precompute();
78
79 # Marpa::R2::Display
80 # name: rule_ids() Synopsis
81
82 my @rule_ids = $grammar->rule_ids();
83
84 # Marpa::R2::Display::End
85
86 Test::More::is( ( join q{ }, @rule_ids ), '0', '$g->rule_ids() ok?' );
87
88 sub do_parse {
89 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
90 for my $terminal (@terminals) {
91 $recce->read( $terminal, $terminal );
92 }
93 return $recce->value();
94 } ## end sub do_parse
95
96 my $value_ref;
97 $value_ref = do_parse();
98 VALUE_TEST: {
99 if ( ref $value_ref ne 'REF' ) {
100 my $ref_type = ref $value_ref;
101 Test::More::fail(
102 qq{Parse result ref type is "$ref_type"; it needs to be "REF"});
103 last VALUE_TEST;
104 } ## end if ( ref $value_ref ne 'REF' )
105 my $value = ${$value_ref};
106 if ( ref $value ne 'HASH' ) {
107 my $ref_type = ref $value;
108 Test::More::fail(
109 qq{Parse value ref type is "$ref_type"; it needs to be "HASH"});
110 last VALUE_TEST;
111 } ## end if ( ref $value ne 'HASH' )
112 my $expected_text = qq{rule 0: S ::= A B C D\nlocations: 0-4\n};
113 Test::More::is( $value->{text}, $expected_text, 'Parse ok?' );
114 } ## end VALUE_TEST:
115
116 my $eval_ok;
117 {
118 local *do_S = *do_bail_with_message_if_A;
119 $eval_ok = eval { $value_ref = do_parse(); 1 };
120 }
121 my $actual_eval_error = $EVAL_ERROR
122 // 'no eval error'; # grab it now to be safe
123 Test::More::ok( ( not defined $eval_ok ),
124 "bail with string argument happened" );
125 $actual_eval_error
126 =~ s/\A User \s+ bailed \s+ at \s+ line \s+ \d+ [^\n]* \n/<LOCATION LINE>/xms;
127 Test::More::is(
128 $actual_eval_error,
129 '<LOCATION LINE>' . $bail_message . "\n",
130 "bail with string argument"
131 );
132
133 {
134 local *do_S = *do_bail_with_object_if_A;
135 $eval_ok = eval { $value_ref = do_parse(); 1 };
136 }
137 $actual_eval_error = $EVAL_ERROR;
138 my $eval_error_ref_type = ref $actual_eval_error;
139 my $exception_value_desc =
140 $eval_error_ref_type eq 'ARRAY'
141 ? $actual_eval_error->[0]
142 : "ref type of exception is $eval_error_ref_type";
143 Test::More::ok( ( not defined $eval_ok ),
144 "bail with object argument happened" );
145 Test::More::is( $eval_error_ref_type, 'ARRAY',
146 "bail with object argument ref type" );
147 Test::More::is( $exception_value_desc, $bail_message,
148 "bail with object argument value" );
149
150 # Local Variables:
151 # mode: cperl
152 # cperl-indent-level: 4
153 # fill-column: 100
154 # End:
155 # vim: expandtab shiftwidth=4: