| 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 |
# Various that share a calculator semantics |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
use Test::More tests => 6; |
| 23 |
use English qw( -no_match_vars ); |
| 24 |
use Scalar::Util qw(blessed); |
| 25 |
|
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
|
| 29 |
## no critic (ErrorHandling::RequireCarping); |
| 30 |
|
| 31 |
use Marpa::R2; |
| 32 |
|
| 33 |
my $calculator_grammar = Marpa::R2::Scanless::G->new( |
| 34 |
{ bless_package => 'My_Nodes', |
| 35 |
source => \(<<'END_OF_SOURCE'), |
| 36 |
:default ::= action => ::array bless => ::lhs |
| 37 |
:start ::= Script |
| 38 |
Script ::= Expression+ separator => comma bless => script |
| 39 |
comma ~ [,] |
| 40 |
Expression ::= |
| 41 |
Number bless => primary |
| 42 |
| ('(') Expression (')') assoc => group bless => parens |
| 43 |
|| Expression ('**') Expression assoc => right bless => power |
| 44 |
|| Expression ('*') Expression bless => multiply |
| 45 |
| Expression ('/') Expression bless => divide |
| 46 |
|| Expression ('+') Expression bless => add |
| 47 |
| Expression ('-') Expression bless => subtract |
| 48 |
Number ~ [\d]+ |
| 49 |
|
| 50 |
:discard ~ whitespace |
| 51 |
whitespace ~ [\s]+ |
| 52 |
# allow comments |
| 53 |
:discard ~ <hash comment> |
| 54 |
<hash comment> ~ <terminated hash comment> | <unterminated |
| 55 |
final hash comment> |
| 56 |
<terminated hash comment> ~ '#' <hash comment body> <vertical space char> |
| 57 |
<unterminated final hash comment> ~ '#' <hash comment body> |
| 58 |
<hash comment body> ~ <hash comment char>* |
| 59 |
<vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 60 |
<hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 61 |
END_OF_SOURCE |
| 62 |
} |
| 63 |
); |
| 64 |
|
| 65 |
my $show_rules_output = $calculator_grammar->show_rules(); |
| 66 |
$show_rules_output .= $calculator_grammar->show_rules(1, 'L0'); |
| 67 |
|
| 68 |
Marpa::R2::Test::is( $show_rules_output, |
| 69 |
<<'END_OF_SHOW_RULES_OUTPUT', 'Scanless show_rules()' ); |
| 70 |
G1 R0 Script ::= Expression + |
| 71 |
G1 R1 Expression ::= Expression |
| 72 |
G1 R2 Expression ::= Expression |
| 73 |
G1 R3 Expression ::= Expression |
| 74 |
G1 R4 Expression ::= Expression |
| 75 |
G1 R5 Expression ::= Number |
| 76 |
G1 R6 Expression ::= '(' Expression ')' |
| 77 |
G1 R7 Expression ::= Expression '**' Expression |
| 78 |
G1 R8 Expression ::= Expression '*' Expression |
| 79 |
G1 R9 Expression ::= Expression '/' Expression |
| 80 |
G1 R10 Expression ::= Expression '+' Expression |
| 81 |
G1 R11 Expression ::= Expression '-' Expression |
| 82 |
G1 R12 :start ::= Script |
| 83 |
L0 R0 comma ::= [,] |
| 84 |
L0 R1 '(' ::= [\(] |
| 85 |
L0 R2 ')' ::= [\)] |
| 86 |
L0 R3 '**' ::= [\*] [\*] |
| 87 |
L0 R4 '*' ::= [\*] |
| 88 |
L0 R5 '/' ::= [\/] |
| 89 |
L0 R6 '+' ::= [\+] |
| 90 |
L0 R7 '-' ::= [\-] |
| 91 |
L0 R8 Number ::= [\d] + |
| 92 |
L0 R9 :discard ::= whitespace |
| 93 |
L0 R10 whitespace ::= [\s] + |
| 94 |
L0 R11 :discard ::= <hash comment> |
| 95 |
L0 R12 <hash comment> ::= <terminated hash comment> |
| 96 |
L0 R13 <hash comment> ::= <unterminated final hash comment> |
| 97 |
L0 R14 <terminated hash comment> ::= [\#] <hash comment body> <vertical space char> |
| 98 |
L0 R15 <unterminated final hash comment> ::= [\#] <hash comment body> |
| 99 |
L0 R16 <hash comment body> ::= <hash comment char> * |
| 100 |
L0 R17 <vertical space char> ::= [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 101 |
L0 R18 <hash comment char> ::= [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 102 |
L0 R19 :start_lex ::= Number |
| 103 |
L0 R20 :start_lex ::= :discard |
| 104 |
L0 R21 :start_lex ::= '(' |
| 105 |
L0 R22 :start_lex ::= ')' |
| 106 |
L0 R23 :start_lex ::= '**' |
| 107 |
L0 R24 :start_lex ::= '*' |
| 108 |
L0 R25 :start_lex ::= '/' |
| 109 |
L0 R26 :start_lex ::= '+' |
| 110 |
L0 R27 :start_lex ::= '-' |
| 111 |
L0 R28 :start_lex ::= comma |
| 112 |
END_OF_SHOW_RULES_OUTPUT |
| 113 |
|
| 114 |
do_test('Calculator 1', $calculator_grammar, |
| 115 |
'42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3)' => qr/\A 86[.]3\d+ \s+ 126 \s+ 125 \s+ 16\z/xms); |
| 116 |
do_test('Calculator 2', $calculator_grammar, |
| 117 |
'42*3+7, 42 * 3 + 7, 42 * 3+7' => qr/ \s* 133 \s+ 133 \s+ 133 \s* /xms); |
| 118 |
do_test('Calculator 3', $calculator_grammar, |
| 119 |
'15329 + 42 * 290 * 711, 42*3+7, 3*3+4* 4' => |
| 120 |
qr/ \s* 8675309 \s+ 133 \s+ 25 \s* /xms); |
| 121 |
|
| 122 |
my $priority_grammar = <<'END_OF_GRAMMAR'; |
| 123 |
:default ::= action => ::array |
| 124 |
:start ::= statement |
| 125 |
statement ::= (<say keyword>) expression bless => statement |
| 126 |
| expression bless => statement |
| 127 |
expression ::= |
| 128 |
number bless => primary |
| 129 |
| variable bless => variable |
| 130 |
|| sign expression bless => unary_sign |
| 131 |
|| expression ('+') expression bless => add |
| 132 |
number ~ [\d]+ |
| 133 |
variable ~ [[:alpha:]] <optional word characters> |
| 134 |
<optional word characters> ~ [[:alnum:]]* |
| 135 |
|
| 136 |
# Marpa::R2::Display |
| 137 |
# name: SLIF lexeme rule synopsis |
| 138 |
|
| 139 |
:lexeme ~ <say keyword> priority => 1 |
| 140 |
|
| 141 |
# Marpa::R2::Display::End |
| 142 |
|
| 143 |
<say keyword> ~ 'say' |
| 144 |
sign ~ [+-] |
| 145 |
:discard ~ whitespace |
| 146 |
whitespace ~ [\s]+ |
| 147 |
END_OF_GRAMMAR |
| 148 |
|
| 149 |
do_test( |
| 150 |
'Priority test 1', |
| 151 |
Marpa::R2::Scanless::G->new( |
| 152 |
{ bless_package => 'My_Nodes', |
| 153 |
source => \$priority_grammar, |
| 154 |
} |
| 155 |
), |
| 156 |
'say + 42' => qr/ 42 /xms |
| 157 |
); |
| 158 |
|
| 159 |
(my $priority_grammar2 = $priority_grammar) =~ s/priority \s+ => \s+ 1$/priority => -1/xms; |
| 160 |
do_test( |
| 161 |
'Priority test 2', |
| 162 |
Marpa::R2::Scanless::G->new( |
| 163 |
{ bless_package => 'My_Nodes', |
| 164 |
source => \$priority_grammar2, |
| 165 |
} |
| 166 |
), |
| 167 |
'say + 42' => qr/ 41 /xms |
| 168 |
); |
| 169 |
|
| 170 |
sub do_test { |
| 171 |
my ( $name, $grammar, $input, $output_re, $args ) = @_; |
| 172 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 173 |
|
| 174 |
$recce->read(\$input); |
| 175 |
my $value_ref = $recce->value(); |
| 176 |
if ( not defined $value_ref ) { |
| 177 |
die "No parse was found, after reading the entire input\n"; |
| 178 |
} |
| 179 |
my $parse = { variables => { say => -1 } }; |
| 180 |
my $value = ${$value_ref}->doit($parse); |
| 181 |
Test::More::like( $value, $output_re, $name ); |
| 182 |
} |
| 183 |
|
| 184 |
sub My_Nodes::script::doit { |
| 185 |
my ($self, $parse) = @_; |
| 186 |
return join q{ }, map { $_->doit($parse) } @{$self}; |
| 187 |
} |
| 188 |
sub My_Nodes::statement::doit { |
| 189 |
my ($self, $parse) = @_; |
| 190 |
return $self->[0]->doit($parse); |
| 191 |
} |
| 192 |
|
| 193 |
sub My_Nodes::add::doit { |
| 194 |
my ($self, $parse) = @_; |
| 195 |
my ( $a, $b ) = @{$self}; |
| 196 |
return $a->doit($parse) + $b->doit($parse); |
| 197 |
} |
| 198 |
|
| 199 |
sub My_Nodes::subtract::doit { |
| 200 |
my ($self, $parse) = @_; |
| 201 |
my ( $a, $b ) = @{$self}; |
| 202 |
return $a->doit($parse) - $b->doit($parse); |
| 203 |
} |
| 204 |
|
| 205 |
sub My_Nodes::multiply::doit { |
| 206 |
my ($self, $parse) = @_; |
| 207 |
my ( $a, $b ) = @{$self}; |
| 208 |
return $a->doit($parse) * $b->doit($parse); |
| 209 |
} |
| 210 |
|
| 211 |
sub My_Nodes::divide::doit { |
| 212 |
my ($self, $parse) = @_; |
| 213 |
my ( $a, $b ) = @{$self}; |
| 214 |
return $a->doit($parse) / $b->doit($parse); |
| 215 |
} |
| 216 |
|
| 217 |
sub My_Nodes::unary_sign::doit { |
| 218 |
my ($self, $parse) = @_; |
| 219 |
my ( $sign, $expression ) = @{$self}; |
| 220 |
my $unsigned_result = $expression->doit($parse); |
| 221 |
return $sign eq '+' ? $unsigned_result : -$unsigned_result; |
| 222 |
} ## end sub My_Nodes::unary_sign::doit |
| 223 |
|
| 224 |
sub My_Nodes::variable::doit { |
| 225 |
my ( $self, $parse ) = @_; |
| 226 |
my $name = $self->[0]; |
| 227 |
Marpa::R2::Context::bail(qq{variable "$name" does not exist}) |
| 228 |
if not exists $parse->{variables}->{$name}; |
| 229 |
return $parse->{variables}->{$name}; |
| 230 |
} ## end sub My_Nodes::variable::doit |
| 231 |
|
| 232 |
sub My_Nodes::primary::doit { |
| 233 |
my ($self, $parse) = @_; |
| 234 |
return $self->[0]; |
| 235 |
} |
| 236 |
sub My_Nodes::parens::doit { |
| 237 |
my ($self, $parse) = @_; |
| 238 |
return $self->[0]->doit($parse); |
| 239 |
} |
| 240 |
|
| 241 |
sub My_Nodes::power::doit { |
| 242 |
my ($self, $parse) = @_; |
| 243 |
my ( $a, $b ) = @{$self}; |
| 244 |
return $a->doit($parse)**$b->doit($parse); |
| 245 |
} |
| 246 |
|
| 247 |
# vim: expandtab shiftwidth=4: |