Debian Codegraph @

t/sl_fullsyn.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 # A "full" Synopsis for the intro doc to the SLIF
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 full 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
72 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
73
74 my $input = '42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3)';
75 $recce->read(\$input);
76 my $value_ref = $recce->value();
77 die "No parse was found\n" if not defined $value_ref;
78
79 # Result will be something like "86.33... 126 125 16"
80 # depending on the floating point precision
81 my $result = ${$value_ref}->doit();
82
83 package My_Nodes;
84
85 sub My_Nodes::primary::doit { return $_[0]->[0]->doit() }
86 sub My_Nodes::Number::doit { return $_[0]->[2] }
87 sub My_Nodes::paren::doit { my ($self) = @_; $self->[1]->doit() }
88
89 sub My_Nodes::add::doit {
90 my ($self) = @_;
91 $self->[0]->doit() + $self->[2]->doit();
92 }
93
94 sub My_Nodes::subtract::doit {
95 my ($self) = @_;
96 $self->[0]->doit() - $self->[2]->doit();
97 }
98
99 sub My_Nodes::multiply::doit {
100 my ($self) = @_;
101 $self->[0]->doit() * $self->[2]->doit();
102 }
103
104 sub My_Nodes::divide::doit {
105 my ($self) = @_;
106 $self->[0]->doit() / $self->[2]->doit();
107 }
108
109 sub My_Nodes::exponentiate::doit {
110 my ($self) = @_;
111 $self->[0]->doit()**$self->[2]->doit();
112 }
113
114 sub My_Nodes::Script::doit {
115 my ($self) = @_;
116 return join q{ }, map { $_->doit() } @{$self};
117 }
118
119 # Marpa::R2::Display::End
120
121 package main;
122
123 Test::More::like(
124 $result,
125 qr/\A 86[.]3\d+ \s+ 126 \s+ 125 \s+ 16\z/xms,
126 'Value of scannerless parse'
127 );
128
129 # vim: expandtab shiftwidth=4: