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