Debian Codegraph @

t/sl_astsyn.t

raw
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 DSL pod of SLIF
18
19 use 5.010001;
20 use strict;
21 use warnings;
22 use Test::More tests => 3;
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 DSL synopsis
33
34 use Marpa::R2;
35
36 my $grammar = Marpa::R2::Scanless::G->new(
37 { bless_package => 'My_Nodes',
38 source => \(<<'END_OF_SOURCE'),
39 :default ::= action => [values] bless => ::lhs
40 lexeme default = action => [ start, length, value ]
41 bless => ::name latm => 1
42
43 :start ::= Script
44 Script ::= Expression+ separator => comma
45 comma ~ [,]
46 Expression ::=
47 Number bless => primary
48 | '(' Expression ')' bless => paren assoc => group
49 || Expression '**' Expression bless => exponentiate assoc => right
50 || Expression '*' Expression bless => multiply
51 | Expression '/' Expression bless => divide
52 || Expression '+' Expression bless => add
53 | Expression '-' Expression bless => subtract
54
55 Number ~ [\d]+
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_SOURCE
68 }
69 );
70
71 # Marpa::R2::Display::End
72
73 sub my_parser {
74 my ( $grammar, $p_input_string ) = @_;
75
76 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
77
78 $recce->read($p_input_string);
79
80 my $value_ref = $recce->value();
81 if ( not defined $value_ref ) {
82 die "No parse was found, after reading the entire input\n";
83 }
84 return ${$value_ref}->doit();
85
86 } ## end sub my_parser
87
88 my @tests = (
89 [ '42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3)' =>
90 qr/\A 86[.]3\d+ \s+ 126 \s+ 125 \s+ 16\z/xms
91 ],
92 [ '42*3+7, 42 * 3 + 7, 42 * 3+7' => qr/ \s* 133 \s+ 133 \s+ 133 \s* /xms
93 ],
94 [ '15329 + 42 * 290 * 711, 42*3+7, 3*3+4* 4' =>
95 qr/ \s* 8675309 \s+ 133 \s+ 25 \s* /xms
96 ],
97 );
98
99 for my $test (@tests) {
100 my ( $input, $output_re ) = @{$test};
101 my $value = my_parser( $grammar, \$input );
102 Test::More::like( $value, $output_re, 'Value of scannerless parse' );
103 }
104
105 package My_Nodes;
106
107 sub My_Nodes::primary::doit { return $_[0]->[0]->doit() }
108 sub My_Nodes::Number::doit { return $_[0]->[2] }
109 sub My_Nodes::paren::doit { my ($self) = @_; $self->[1]->doit() }
110
111 sub My_Nodes::add::doit {
112 my ($self) = @_;
113 $self->[0]->doit() + $self->[2]->doit();
114 }
115
116 sub My_Nodes::subtract::doit {
117 my ($self) = @_;
118 $self->[0]->doit() - $self->[2]->doit();
119 }
120
121 sub My_Nodes::multiply::doit {
122 my ($self) = @_;
123 $self->[0]->doit() * $self->[2]->doit();
124 }
125
126 sub My_Nodes::divide::doit {
127 my ($self) = @_;
128 $self->[0]->doit() / $self->[2]->doit();
129 }
130
131 sub My_Nodes::exponentiate::doit {
132 my ($self) = @_;
133 $self->[0]->doit()**$self->[2]->doit();
134 }
135
136 sub My_Nodes::Script::doit {
137 my ($self) = @_;
138 return join q{ }, map { $_->doit() } @{$self};
139 }
140
141 # vim: expandtab shiftwidth=4: