| 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 |
# Example of use of discard events |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
use Test::More tests => 1; |
| 23 |
use English qw( -no_match_vars ); |
| 24 |
use Scalar::Util; |
| 25 |
|
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
|
| 29 |
## no critic (ErrorHandling::RequireCarping); |
| 30 |
|
| 31 |
# Marpa::R2::Display |
| 32 |
# name: SLIF discard event synopsis |
| 33 |
|
| 34 |
use Marpa::R2; |
| 35 |
|
| 36 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 37 |
{ |
| 38 |
source => \(<<'END_OF_SOURCE'), |
| 39 |
:default ::= action => [g1start, g1len, values] |
| 40 |
lexeme default = latm => 1 |
| 41 |
|
| 42 |
Script ::= Expression+ separator => comma action => do_expression |
| 43 |
comma ~ [,] |
| 44 |
Expression ::= Subexpression action => [g1start,g1len,value] |
| 45 |
Subexpression ::= |
| 46 |
Number action => do_number |
| 47 |
| ('(') Subexpression (')') assoc => group action => do_paren |
| 48 |
|| Subexpression ('**') Subexpression assoc => right action => do_power |
| 49 |
|| Subexpression ('*') Subexpression action => do_multiply |
| 50 |
| Subexpression ('/') Subexpression action => do_divide |
| 51 |
|| Subexpression ('+') Subexpression action => do_add |
| 52 |
| Subexpression ('-') Subexpression action => do_subtract |
| 53 |
|
| 54 |
Number ~ [\d]+ |
| 55 |
:discard ~ whitespace event => ws |
| 56 |
whitespace ~ [\s]+ |
| 57 |
# allow comments |
| 58 |
:discard ~ <hash comment> event => comment |
| 59 |
<hash comment> ~ <terminated hash comment> | <unterminated |
| 60 |
final hash comment> |
| 61 |
<terminated hash comment> ~ '#' <hash comment body> <vertical space char> |
| 62 |
<unterminated final hash comment> ~ '#' <hash comment body> |
| 63 |
<hash comment body> ~ <hash comment char>* |
| 64 |
<vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 65 |
<hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 66 |
END_OF_SOURCE |
| 67 |
} |
| 68 |
); |
| 69 |
|
| 70 |
# Marpa::R2::Display::End |
| 71 |
|
| 72 |
my $input = <<'EOI'; |
| 73 |
42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3), |
| 74 |
# Hardy-Ramanujan number |
| 75 |
1729, 1**3+12**3, 9**3+10**3, |
| 76 |
# Next highest taxicab number |
| 77 |
# note: weird spacing is deliberate |
| 78 |
87539319, 167**3+ 436**3,228**3 + 423**3,255**3+414**3 |
| 79 |
EOI |
| 80 |
|
| 81 |
my $output_re = |
| 82 |
qr/\A 86[.]3\d+ \s+ 126 \s+ 125 \s+ 16 \s+ 1729 \s+ 1729 \s+ 1729 .*\z/xms; |
| 83 |
|
| 84 |
|
| 85 |
my $length = length $input; |
| 86 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar, |
| 87 |
semantics_package => 'My_Nodes', |
| 88 |
} ); |
| 89 |
|
| 90 |
my $pos = $recce->read(\$input); |
| 91 |
|
| 92 |
my @events = (); |
| 93 |
READ: while (1) { |
| 94 |
|
| 95 |
my @actual_events = (); |
| 96 |
|
| 97 |
EVENT: |
| 98 |
for my $event ( @{ $recce->events() } ) { |
| 99 |
my ( $name, @other_stuff ) = @{$event}; |
| 100 |
# say STDERR 'Event received!!! -- ', Data::Dumper::Dumper($event); |
| 101 |
push @events, $event; |
| 102 |
} |
| 103 |
|
| 104 |
last READ if $pos >= $length; |
| 105 |
$pos = $recce->resume($pos); |
| 106 |
} ## end READ: while (1) |
| 107 |
|
| 108 |
my $value_ref = $recce->value(); |
| 109 |
if ( not defined $value_ref ) { |
| 110 |
die "No parse was found, after reading the entire input\n"; |
| 111 |
} |
| 112 |
|
| 113 |
my $event_ix = 0; |
| 114 |
my $result = ''; |
| 115 |
for my $expression (@{${$value_ref}}) { |
| 116 |
my ($g1start, $g1len, $value) = @{$expression}; |
| 117 |
my $g1end = $g1start+$g1len; |
| 118 |
$result .= qq{expression: "} . $recce->substring( $g1start, $g1len ) . |
| 119 |
qq{" = } . round_value($value); |
| 120 |
$result .= "\n"; |
| 121 |
EVENT: while ($event_ix <= $#events) { |
| 122 |
my $event = $events[$event_ix]; |
| 123 |
my $g1loc = $event->[3]; |
| 124 |
last EVENT if $g1loc >= $g1end; |
| 125 |
my $type = $g1loc == $g1start ? 'preceding' : 'internal'; |
| 126 |
$result .= join q{ }, $type, display_event($recce, @{$event}); |
| 127 |
$result .= "\n"; |
| 128 |
$event_ix++; |
| 129 |
} |
| 130 |
$result .= "\n"; |
| 131 |
} |
| 132 |
|
| 133 |
EVENT: while ( $event_ix <= $#events ) { |
| 134 |
my $event = $events[$event_ix]; |
| 135 |
$result .= join q{ }, 'trailing', display_event($recce, @{$event}); |
| 136 |
$result .= "\n"; |
| 137 |
$event_ix++; |
| 138 |
} ## end EVENT: while ( $event_ix <= $#events ) |
| 139 |
|
| 140 |
|
| 141 |
# round value down, for testing on platforms |
| 142 |
# with various float precisions |
| 143 |
sub round_value { |
| 144 |
my ( $value ) = @_; |
| 145 |
return (int $value*100)/100; |
| 146 |
} |
| 147 |
|
| 148 |
sub display_event { |
| 149 |
my ( $recce, $event_name, $start, $end ) = @_; |
| 150 |
if ($event_name eq 'ws') { |
| 151 |
return "ws of length " . ($end-$start); |
| 152 |
} |
| 153 |
my $literal = $recce->literal($start, ($end-$start)); |
| 154 |
$literal =~ s/\n/\\n/xmsg; |
| 155 |
return qq{$event_name: "$literal"}; |
| 156 |
} |
| 157 |
|
| 158 |
my $expected_result = <<'END_OF_RESULT'; |
| 159 |
expression: "42*2+7/3" = 86.33 |
| 160 |
|
| 161 |
expression: "42*(2+7)/3" = 126 |
| 162 |
preceding ws of length 1 |
| 163 |
|
| 164 |
expression: "2**7-3" = 125 |
| 165 |
preceding ws of length 1 |
| 166 |
|
| 167 |
expression: "2**(7-3)" = 16 |
| 168 |
preceding ws of length 1 |
| 169 |
|
| 170 |
expression: "1729" = 1729 |
| 171 |
preceding ws of length 1 |
| 172 |
preceding comment: "# Hardy-Ramanujan number\n" |
| 173 |
|
| 174 |
expression: "1**3+12**3" = 1729 |
| 175 |
preceding ws of length 1 |
| 176 |
|
| 177 |
expression: "9**3+10**3" = 1729 |
| 178 |
preceding ws of length 1 |
| 179 |
|
| 180 |
expression: "87539319" = 87539319 |
| 181 |
preceding ws of length 1 |
| 182 |
preceding comment: "# Next highest taxicab number\n" |
| 183 |
preceding comment: "# note: weird spacing is deliberate\n" |
| 184 |
|
| 185 |
expression: "167**3+ 436**3" = 87539319 |
| 186 |
preceding ws of length 1 |
| 187 |
internal ws of length 1 |
| 188 |
|
| 189 |
expression: "228**3 + 423**3" = 87539319 |
| 190 |
internal ws of length 1 |
| 191 |
internal ws of length 1 |
| 192 |
|
| 193 |
expression: "255**3+414**3" = 87539319 |
| 194 |
|
| 195 |
trailing ws of length 1 |
| 196 |
END_OF_RESULT |
| 197 |
|
| 198 |
Marpa::R2::Test::is($result, $expected_result, "interweave of events and parse tree"); |
| 199 |
|
| 200 |
package My_Nodes; |
| 201 |
|
| 202 |
sub My_Nodes::do_expression { |
| 203 |
my ($parse, @values) = @_; |
| 204 |
return \@values; |
| 205 |
# say STDERR "pushing value: ", Data::Dumper::Dumper(\@_); |
| 206 |
} |
| 207 |
|
| 208 |
sub My_Nodes::do_number { |
| 209 |
my ($parse, $number) = @_; |
| 210 |
return $number+0; |
| 211 |
} |
| 212 |
|
| 213 |
sub My_Nodes::do_paren { |
| 214 |
my ($parse, $expr) = @_; |
| 215 |
return $expr; |
| 216 |
} |
| 217 |
|
| 218 |
sub My_Nodes::do_add { |
| 219 |
my ($parse, $right, $left) = @_; |
| 220 |
return $right + $left; |
| 221 |
} |
| 222 |
|
| 223 |
sub My_Nodes::do_subtract { |
| 224 |
my ($parse, $right, $left) = @_; |
| 225 |
return $right - $left; |
| 226 |
} |
| 227 |
|
| 228 |
sub My_Nodes::do_multiply { |
| 229 |
my ($parse, $right, $left) = @_; |
| 230 |
return $right * $left; |
| 231 |
} |
| 232 |
|
| 233 |
sub My_Nodes::do_divide { |
| 234 |
my ($parse, $right, $left) = @_; |
| 235 |
return $right / $left; |
| 236 |
} |
| 237 |
|
| 238 |
sub My_Nodes::do_power { |
| 239 |
my ($parse, $right, $left) = @_; |
| 240 |
return $right ** $left; |
| 241 |
} |
| 242 |
|
| 243 |
# vim: expandtab shiftwidth=4: |