| 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 |
# Test of scannerless parsing -- a DSL |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
|
| 23 |
use Test::More tests => 7; |
| 24 |
my $builder = Test::More->builder; |
| 25 |
binmode $builder->output, ":utf8"; |
| 26 |
binmode $builder->failure_output, ":utf8"; |
| 27 |
binmode $builder->todo_output, ":utf8"; |
| 28 |
use English qw( -no_match_vars ); |
| 29 |
use lib 'inc'; |
| 30 |
use Marpa::R2::Test; |
| 31 |
use Marpa::R2; |
| 32 |
|
| 33 |
my $rules = <<'END_OF_GRAMMAR'; |
| 34 |
:default ::= action => do_arg0 |
| 35 |
:start ::= script |
| 36 |
script ::= expression |
| 37 |
script ::= (script ';') expression |
| 38 |
reduce_op ::= '+' | '-' | '/' | '*' |
| 39 |
expression ::= |
| 40 |
NUM |
| 41 |
| VAR action => do_is_var |
| 42 |
| ('(') expression (')') assoc => group |
| 43 |
| ([\x{300C}]) expression ([\x{300D}]) assoc => group |
| 44 |
|| '-' expression action => do_negate |
| 45 |
|| expression '^' expression action => do_caret assoc => right |
| 46 |
|| expression '*' expression action => do_star |
| 47 |
| expression '/' expression action => do_slash |
| 48 |
|| expression '+' expression action => do_plus |
| 49 |
| expression '-' expression action => do_minus |
| 50 |
|| expression ',' expression action => do_array |
| 51 |
|| reduce_op 'reduce' expression action => do_reduce |
| 52 |
|| VAR '=' expression action => do_set_var |
| 53 |
|
| 54 |
NUM ~ [\d]+ |
| 55 |
VAR ~ [\w]+ |
| 56 |
:discard ~ whitespace |
| 57 |
whitespace ~ [\s]+ |
| 58 |
# allow comments |
| 59 |
:discard ~ <hash comment> |
| 60 |
<hash comment> ~ <terminated hash comment> | <unterminated |
| 61 |
final hash comment> |
| 62 |
<terminated hash comment> ~ '#' <hash comment body> <vertical space char> |
| 63 |
<unterminated final hash comment> ~ '#' <hash comment body> |
| 64 |
<hash comment body> ~ <hash comment char>* |
| 65 |
<vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 66 |
<hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 67 |
END_OF_GRAMMAR |
| 68 |
|
| 69 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 70 |
{ |
| 71 |
source => \$rules, |
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
my %binop_closure = ( |
| 76 |
'*' => sub { $_[0] * $_[1] }, |
| 77 |
'/' => sub { |
| 78 |
Marpa::R2::Context::bail('Division by zero') if not $_[1]; |
| 79 |
$_[0] / $_[1]; |
| 80 |
}, |
| 81 |
'+' => sub { $_[0] + $_[1] }, |
| 82 |
'-' => sub { $_[0] - $_[1] }, |
| 83 |
'^' => sub { $_[0]**$_[1] }, |
| 84 |
); |
| 85 |
|
| 86 |
my %symbol_table = (); |
| 87 |
|
| 88 |
package main; |
| 89 |
|
| 90 |
# For debugging |
| 91 |
sub add_brackets { |
| 92 |
my ( undef, @children ) = @_; |
| 93 |
return $children[0] if 1 == scalar @children; |
| 94 |
my $original = join q{}, grep {defined} @children; |
| 95 |
return '[' . $original . ']'; |
| 96 |
} ## end sub add_brackets |
| 97 |
|
| 98 |
sub calculate { |
| 99 |
my ($p_string) = @_; |
| 100 |
|
| 101 |
%symbol_table = (); |
| 102 |
|
| 103 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 104 |
|
| 105 |
my $self = bless { grammar => $grammar }, 'My_Actions'; |
| 106 |
$self->{slr} = $recce; |
| 107 |
|
| 108 |
if ( not defined eval { $recce->read($p_string); 1 } ) { |
| 109 |
|
| 110 |
# Add last expression found, and rethrow |
| 111 |
my $eval_error = $EVAL_ERROR; |
| 112 |
chomp $eval_error; |
| 113 |
die $self->show_last_expression(), "\n", $eval_error, "\n"; |
| 114 |
} ## end if ( not defined eval { $event_count = $recce->read...}) |
| 115 |
my $value_ref = $recce->value( $self ); |
| 116 |
if ( not defined $value_ref ) { |
| 117 |
die $self->show_last_expression(), "\n", |
| 118 |
"No parse was found, after reading the entire input\n"; |
| 119 |
} |
| 120 |
return ${$value_ref}; |
| 121 |
|
| 122 |
} ## end sub calculate |
| 123 |
|
| 124 |
sub report_calculation { |
| 125 |
my ($string) = @_; |
| 126 |
my $result = calculate(\$string); |
| 127 |
$result = join q{,}, @{$result} if ref $result eq 'ARRAY'; |
| 128 |
my $output = "Parse: $result\n"; |
| 129 |
for my $symbol ( sort keys %symbol_table ) { |
| 130 |
$output .= qq{"$symbol" = "} . $symbol_table{$symbol} . qq{"\n}; |
| 131 |
} |
| 132 |
chomp $output; |
| 133 |
return $output; |
| 134 |
} ## end sub report_calculation |
| 135 |
|
| 136 |
my @tests_data = ( |
| 137 |
[ "4 * 3 + 42 / 1" => 'Parse: 54' ], |
| 138 |
[ "4 * 3 / (a = b = 5) + 42 - 1" => |
| 139 |
qq{Parse: 43.4\n"a" = "5"\n"b" = "5"} |
| 140 |
], |
| 141 |
[ "4 * 3 / 5 - - - 3 + 42 - 1" => 'Parse: 40.4' ], |
| 142 |
[ "a=1;b = 5; - a - b" => qq{Parse: -6\n"a" = "1"\n"b" = "5"} ], |
| 143 |
[ "1 * 2 + 3 * 4 ^ 2 ^ 2 ^ 2 * 42 + 1" => 'Parse: 541165879299' ], |
| 144 |
[ "+ reduce 1 + 2, 3,4*2 , 5" => 'Parse: 19' ] |
| 145 |
); |
| 146 |
|
| 147 |
my $unicoded_string = "4 * 3 / (a = b = 5) + 42 - 1"; |
| 148 |
$unicoded_string =~ tr/() /\x{300C}\x{300D}\x{2028}/; |
| 149 |
push @tests_data, [ $unicoded_string, qq{Parse: 43.4\n"a" = "5"\n"b" = "5"} ]; |
| 150 |
|
| 151 |
TEST: |
| 152 |
for my $test_data (@tests_data) { |
| 153 |
my ($test_string, $expected_value) = @{$test_data}; |
| 154 |
my $actual_value = report_calculation( $test_string ); |
| 155 |
$actual_value //= 'NO PARSE!'; |
| 156 |
Test::More::is( $actual_value, $expected_value, qq{Value of "$test_string"} ); |
| 157 |
} ## end TEST: for my $test_string (@test_strings) |
| 158 |
|
| 159 |
package My_Actions; |
| 160 |
|
| 161 |
sub do_is_var { |
| 162 |
my ( undef, $var ) = @_; |
| 163 |
my $value = $symbol_table{$var}; |
| 164 |
Marpa::R2::Context::bail(qq{Undefined variable "$var"}) |
| 165 |
if not defined $value; |
| 166 |
return $value; |
| 167 |
} ## end sub do_is_var |
| 168 |
|
| 169 |
sub do_set_var { |
| 170 |
my ( undef, $var, undef, $value ) = @_; |
| 171 |
return $symbol_table{$var} = $value; |
| 172 |
} |
| 173 |
|
| 174 |
sub do_negate { |
| 175 |
return -$_[2]; |
| 176 |
} |
| 177 |
|
| 178 |
sub do_arg0 { return $_[1]; } |
| 179 |
sub do_arg1 { return $_[2]; } |
| 180 |
sub do_arg2 { return $_[3]; } |
| 181 |
|
| 182 |
sub do_array { |
| 183 |
my ( undef, $left, undef, $right ) = @_; |
| 184 |
my @value = (); |
| 185 |
my $ref; |
| 186 |
if ( $ref = ref $left ) { |
| 187 |
Marpa::R2::Context::bail("Bad ref type for array operand: $ref") |
| 188 |
if $ref ne 'ARRAY'; |
| 189 |
push @value, @{$left}; |
| 190 |
} |
| 191 |
else { |
| 192 |
push @value, $left; |
| 193 |
} |
| 194 |
if ( $ref = ref $right ) { |
| 195 |
Marpa::R2::Context::bail("Bad ref type for array operand: $ref") |
| 196 |
if $ref ne 'ARRAY'; |
| 197 |
push @value, @{$right}; |
| 198 |
} |
| 199 |
else { |
| 200 |
push @value, $right; |
| 201 |
} |
| 202 |
return \@value; |
| 203 |
} ## end sub do_array |
| 204 |
|
| 205 |
sub do_binop { |
| 206 |
my ( $op, $left, $right ) = @_; |
| 207 |
my $closure = $binop_closure{$op}; |
| 208 |
Marpa::R2::Context::bail( |
| 209 |
qq{Do not know how to perform binary operation "$op"}) |
| 210 |
if not defined $closure; |
| 211 |
return $closure->( $left, $right ); |
| 212 |
} ## end sub do_binop |
| 213 |
|
| 214 |
sub do_caret { |
| 215 |
my ( undef, $left, undef, $right ) = @_; |
| 216 |
return do_binop( '^', $left, $right ); |
| 217 |
} |
| 218 |
|
| 219 |
sub do_star { |
| 220 |
my ( undef, $left, undef, $right ) = @_; |
| 221 |
return do_binop( '*', $left, $right ); |
| 222 |
} |
| 223 |
|
| 224 |
sub do_slash { |
| 225 |
my ( undef, $left, undef, $right ) = @_; |
| 226 |
return do_binop( '/', $left, $right ); |
| 227 |
} |
| 228 |
|
| 229 |
sub do_plus { |
| 230 |
my ( undef, $left, undef, $right ) = @_; |
| 231 |
return do_binop( '+', $left, $right ); |
| 232 |
} |
| 233 |
|
| 234 |
sub do_minus { |
| 235 |
my ( undef, $left, undef, $right ) = @_; |
| 236 |
return do_binop( '-', $left, $right ); |
| 237 |
} |
| 238 |
|
| 239 |
sub do_reduce { |
| 240 |
my ( undef, $op, undef, $args ) = @_; |
| 241 |
my $closure = $binop_closure{$op}; |
| 242 |
Marpa::R2::Context::bail( |
| 243 |
qq{Do not know how to perform binary operation "$op"}) |
| 244 |
if not defined $closure; |
| 245 |
$args = [$args] if ref $args eq ''; |
| 246 |
my @stack = @{$args}; |
| 247 |
OP: while (1) { |
| 248 |
return $stack[0] if scalar @stack <= 1; |
| 249 |
my $result = $closure->( $stack[-2], $stack[-1] ); |
| 250 |
splice @stack, -2, 2, $result; |
| 251 |
} |
| 252 |
Marpa::R2::Context::bail('Should not get here'); |
| 253 |
} ## end sub do_reduce |
| 254 |
|
| 255 |
sub show_last_expression { |
| 256 |
my ($self) = @_; |
| 257 |
my $slr = $self->{slr}; |
| 258 |
my ( $start, $end ) = $slr->last_completed_range('expression'); |
| 259 |
return 'No expression was successfully parsed' if not defined $start; |
| 260 |
my $last_expression = $slr->range_to_string( $start, $end ); |
| 261 |
return "Last expression successfully parsed was: $last_expression"; |
| 262 |
} ## end sub show_last_expression |
| 263 |
|
| 264 |
# vim: expandtab shiftwidth=4: |