Debian Codegraph @

t/sl_external1.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 # Test of SLIF external interface
18
19 use 5.010001;
20 use strict;
21 use warnings;
22 use Test::More tests => 1;
23
24 use lib 'inc';
25 use Marpa::R2::Test;
26
27 ## no critic (ErrorHandling::RequireCarping);
28
29 use Marpa::R2;
30
31 my $grammar = Marpa::R2::Scanless::G->new(
32 { bless_package => 'My_Nodes',
33 source => \(<<'END_OF_SOURCE'),
34 :default ::= action => ::array
35 :start ::= Script
36 Script ::= Expression+ separator => <op comma> bless => script
37 Expression ::=
38 Number bless => primary
39 | (<op lparen>) Expression (<op rparen>) bless => parens assoc => group
40 || Expression (<op pow>) Expression bless => power assoc => right
41 || Expression (<op times>) Expression bless => multiply
42 | Expression (<op divide>) Expression bless => divide
43 || Expression (<op add>) Expression bless => add
44 | Expression (<op subtract>) Expression bless => subtract
45 Number ~ unicorn
46 <op comma> ~ unicorn
47 <op lparen> ~ unicorn
48 <op rparen> ~ unicorn
49 <op pow> ~ unicorn
50 <op times> ~ unicorn
51 <op divide> ~ unicorn
52 <op add> ~ unicorn
53 <op subtract> ~ unicorn
54 unicorn ~ [^\s\S]
55 END_OF_SOURCE
56 }
57 );
58
59 my @terminals = (
60 [ Number => qr/\d+/xms, "Number" ],
61 [ 'op pow' => qr/[\^]/xms, 'Exponentiation operator' ],
62 [ 'op pow' => qr/[*][*]/xms, 'Exponentiation' ], # order matters!
63 [ 'op times' => qr/[*]/xms, 'Multiplication operator' ], # order matters!
64 [ 'op divide' => qr/[\/]/xms, 'Division operator' ],
65 [ 'op add' => qr/[+]/xms, 'Addition operator' ],
66 [ 'op subtract' => qr/[-]/xms, 'Subtraction operator' ],
67 [ 'op lparen' => qr/[(]/xms, 'Left parenthesis' ],
68 [ 'op rparen' => qr/[)]/xms, 'Right parenthesis' ],
69 [ 'op comma' => qr/[,]/xms, 'Comma operator' ],
70 );
71
72 sub my_parser {
73 my ( $grammar, $string ) = @_;
74 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
75
76 # Marpa::R2::Display
77 # name: SLIF external read example
78
79 $recce->read( \$string, 0, 0 );
80
81 # Marpa::R2::Display::End
82
83 my $length = length $string;
84 pos $string = 0;
85 TOKEN: while (1) {
86 my $start_of_lexeme = pos $string;
87 last TOKEN if $start_of_lexeme >= $length;
88 next TOKEN if $string =~ m/\G\s+/gcxms; # skip whitespace
89 TOKEN_TYPE: for my $t (@terminals) {
90 my ( $token_name, $regex, $long_name ) = @{$t};
91 next TOKEN_TYPE if not $string =~ m/\G($regex)/gcxms;
92 my $lexeme = $1;
93
94 # Marpa::R2::Display
95 # name: SLIF lexeme_alternative() example
96
97 if ( not defined $recce->lexeme_alternative($token_name) ) {
98 die
99 qq{Parser rejected token "$long_name" at position $start_of_lexeme, before "},
100 substr( $string, $start_of_lexeme, 40 ), q{"};
101 }
102 next TOKEN
103 if $recce->lexeme_complete( $start_of_lexeme,
104 ( length $lexeme ) );
105
106 # Marpa::R2::Display::End
107
108 } ## end TOKEN_TYPE: for my $t (@terminals)
109 die qq{No token found at position $start_of_lexeme, before "},
110 substr( $string, pos $string, 40 ), q{"};
111 } ## end TOKEN: while (1)
112 my $value_ref = $recce->value();
113 if ( not defined $value_ref ) {
114 die "No parse was found, after reading the entire input\n";
115 }
116 return ${$value_ref}->doit();
117 } ## end sub my_parser
118
119 my $value = my_parser( $grammar, '42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3)' );
120
121 Test::More::like( $value, qr/\A 86[.]3\d+ \s+ 126 \s+ 125 \s+ 16\z/xms, 'Value of parse' );
122
123 sub My_Nodes::script::doit {
124 my ($self) = @_;
125 return join q{ }, map { $_->doit() } @{$self};
126 }
127
128 sub My_Nodes::add::doit {
129 my ($self) = @_;
130 my ( $a, $b ) = @{$self};
131 return $a->doit() + $b->doit();
132 }
133
134 sub My_Nodes::subtract::doit {
135 my ($self) = @_;
136 my ( $a, $b ) = @{$self};
137 return $a->doit() - $b->doit();
138 }
139
140 sub My_Nodes::multiply::doit {
141 my ($self) = @_;
142 my ( $a, $b ) = @{$self};
143 return $a->doit() * $b->doit();
144 }
145
146 sub My_Nodes::divide::doit {
147 my ($self) = @_;
148 my ( $a, $b ) = @{$self};
149 return $a->doit() / $b->doit();
150 }
151
152 sub My_Nodes::primary::doit { return $_[0]->[0]; }
153 sub My_Nodes::parens::doit { return $_[0]->[0]->doit(); }
154
155 sub My_Nodes::power::doit {
156 my ($self) = @_;
157 my ( $a, $b ) = @{$self};
158 return $a->doit()**$b->doit();
159 }
160
161 # vim: expandtab shiftwidth=4: