| 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 |
# Synopsis for Scannerless interface |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
use Test::More tests => 4; |
| 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 $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 |
# Marpa::R2::Display |
| 66 |
# name: SLG show_rules() synopsis |
| 67 |
|
| 68 |
my $show_rules_output = $grammar->show_rules(); |
| 69 |
|
| 70 |
# Marpa::R2::Display::End |
| 71 |
|
| 72 |
$show_rules_output .= $grammar->show_rules(1, 'L0'); |
| 73 |
|
| 74 |
Marpa::R2::Test::is( $show_rules_output, |
| 75 |
<<'END_OF_SHOW_RULES_OUTPUT', 'Scanless show_rules()' ); |
| 76 |
G1 R0 Script ::= Expression + |
| 77 |
G1 R1 Expression ::= Expression |
| 78 |
G1 R2 Expression ::= Expression |
| 79 |
G1 R3 Expression ::= Expression |
| 80 |
G1 R4 Expression ::= Expression |
| 81 |
G1 R5 Expression ::= Number |
| 82 |
G1 R6 Expression ::= '(' Expression ')' |
| 83 |
G1 R7 Expression ::= Expression '**' Expression |
| 84 |
G1 R8 Expression ::= Expression '*' Expression |
| 85 |
G1 R9 Expression ::= Expression '/' Expression |
| 86 |
G1 R10 Expression ::= Expression '+' Expression |
| 87 |
G1 R11 Expression ::= Expression '-' Expression |
| 88 |
G1 R12 :start ::= Script |
| 89 |
L0 R0 comma ::= [,] |
| 90 |
L0 R1 '(' ::= [\(] |
| 91 |
L0 R2 ')' ::= [\)] |
| 92 |
L0 R3 '**' ::= [\*] [\*] |
| 93 |
L0 R4 '*' ::= [\*] |
| 94 |
L0 R5 '/' ::= [\/] |
| 95 |
L0 R6 '+' ::= [\+] |
| 96 |
L0 R7 '-' ::= [\-] |
| 97 |
L0 R8 Number ::= [\d] + |
| 98 |
L0 R9 :discard ::= whitespace |
| 99 |
L0 R10 whitespace ::= [\s] + |
| 100 |
L0 R11 :discard ::= <hash comment> |
| 101 |
L0 R12 <hash comment> ::= <terminated hash comment> |
| 102 |
L0 R13 <hash comment> ::= <unterminated final hash comment> |
| 103 |
L0 R14 <terminated hash comment> ::= [\#] <hash comment body> <vertical space char> |
| 104 |
L0 R15 <unterminated final hash comment> ::= [\#] <hash comment body> |
| 105 |
L0 R16 <hash comment body> ::= <hash comment char> * |
| 106 |
L0 R17 <vertical space char> ::= [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 107 |
L0 R18 <hash comment char> ::= [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 108 |
L0 R19 :start_lex ::= Number |
| 109 |
L0 R20 :start_lex ::= :discard |
| 110 |
L0 R21 :start_lex ::= '(' |
| 111 |
L0 R22 :start_lex ::= ')' |
| 112 |
L0 R23 :start_lex ::= '**' |
| 113 |
L0 R24 :start_lex ::= '*' |
| 114 |
L0 R25 :start_lex ::= '/' |
| 115 |
L0 R26 :start_lex ::= '+' |
| 116 |
L0 R27 :start_lex ::= '-' |
| 117 |
L0 R28 :start_lex ::= comma |
| 118 |
END_OF_SHOW_RULES_OUTPUT |
| 119 |
|
| 120 |
sub my_parser { |
| 121 |
my ( $grammar, $p_input_string ) = @_; |
| 122 |
|
| 123 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 124 |
|
| 125 |
$recce->read($p_input_string); |
| 126 |
my $value_ref = $recce->value(); |
| 127 |
if ( not defined $value_ref ) { |
| 128 |
die "No parse was found, after reading the entire input\n"; |
| 129 |
} |
| 130 |
return ${$value_ref}->doit(); |
| 131 |
|
| 132 |
} ## end sub my_parser |
| 133 |
|
| 134 |
my @tests = ( |
| 135 |
[ '42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3)' => |
| 136 |
qr/\A 86[.]3\d+ \s+ 126 \s+ 125 \s+ 16\z/xms |
| 137 |
], |
| 138 |
[ '42*3+7, 42 * 3 + 7, 42 * 3+7' => qr/ \s* 133 \s+ 133 \s+ 133 \s* /xms |
| 139 |
], |
| 140 |
[ '15329 + 42 * 290 * 711, 42*3+7, 3*3+4* 4' => |
| 141 |
qr/ \s* 8675309 \s+ 133 \s+ 25 \s* /xms |
| 142 |
], |
| 143 |
); |
| 144 |
|
| 145 |
for my $test (@tests) { |
| 146 |
my ( $input, $output_re ) = @{$test}; |
| 147 |
my $result = my_parser( $grammar, \$input ); |
| 148 |
Test::More::like( $result, $output_re, 'Value of scannerless parse' ); |
| 149 |
} |
| 150 |
|
| 151 |
sub My_Nodes::script::doit { |
| 152 |
my ($self) = @_; |
| 153 |
return join q{ }, map { $_->doit() } @{$self}; |
| 154 |
} |
| 155 |
|
| 156 |
sub My_Nodes::add::doit { |
| 157 |
my ($self) = @_; |
| 158 |
my ( $a, $b ) = @{$self}; |
| 159 |
return $a->doit() + $b->doit(); |
| 160 |
} |
| 161 |
|
| 162 |
sub My_Nodes::subtract::doit { |
| 163 |
my ($self) = @_; |
| 164 |
my ( $a, $b ) = @{$self}; |
| 165 |
return $a->doit() - $b->doit(); |
| 166 |
} |
| 167 |
|
| 168 |
sub My_Nodes::multiply::doit { |
| 169 |
my ($self) = @_; |
| 170 |
my ( $a, $b ) = @{$self}; |
| 171 |
return $a->doit() * $b->doit(); |
| 172 |
} |
| 173 |
|
| 174 |
sub My_Nodes::divide::doit { |
| 175 |
my ($self) = @_; |
| 176 |
my ( $a, $b ) = @{$self}; |
| 177 |
return $a->doit() / $b->doit(); |
| 178 |
} |
| 179 |
|
| 180 |
sub My_Nodes::primary::doit { return $_[0]->[0]; } |
| 181 |
sub My_Nodes::parens::doit { return $_[0]->[0]->doit(); } |
| 182 |
|
| 183 |
sub My_Nodes::power::doit { |
| 184 |
my ($self) = @_; |
| 185 |
my ( $a, $b ) = @{$self}; |
| 186 |
return $a->doit()**$b->doit(); |
| 187 |
} |
| 188 |
|
| 189 |
# vim: expandtab shiftwidth=4: |