| 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 | # Dave Abrahams marpabook repo issue 3 |
| 18 | # Extended grammar |
| 19 | |
| 20 | use 5.010001scip-perl cpan Marpa-R2 12.000000 `5.010001`/; |
| 21 | use strictscip-perl cpan Marpa-R2 12.000000 strict/; |
| 22 | use warningsscip-perl cpan Marpa-R2 12.000000 warnings/; |
| 23 | use Scalar::Utilscip-perl cpan Marpa-R2 12.000000 Scalar/Util/; |
| 24 | use Data::Dumperscip-perl cpan Marpa-R2 12.000000 Data/Dumper/; |
| 25 | use Test::Morescip-perl cpan Marpa-R2 12.000000 Test/More/ tests => 5; |
| 26 | |
| 27 | use libscip-perl cpan Marpa-R2 12.000000 lib/ 'inc'; |
| 28 | use Marpa::R2::Testscip-perl cpan Marpa-R2 12.000000 Marpa/R2/Test/; |
| 29 | |
| 30 | ## no critic (ErrorHandling::RequireCarping); |
| 31 | |
| 32 | use Marpa::R2scip-perl cpan Marpa-R2 12.000000 Marpa/R2/; |
| 33 | |
| 34 | sayscip-perl cpan Marpa-R2 12.000000 main/say(). 'Libmarpa tag: ' . Marpa::R2::Thin::tagscip-perl cpan Marpa-R2 12.000000 Marpa/R2/Thin/tag().(); |
| 35 | |
| 36 | my $bnf = <<'END_OF_BNF'; |
| 37 | :default ::= action => [values] |
| 38 | lexeme default = latm => 1 |
| 39 | A ::= 'w' 'x' B | 'w' |
| 40 | B ::= C |
| 41 | C ::= 'y' 'z' A |
| 42 | C ::= 'y' 'z' 'w' 'x' 'y' 'z' 'w' |
| 43 | |
| 44 | :discard ~ whitespace |
| 45 | whitespace ~ [\s]+ |
| 46 | END_OF_BNF |
| 47 | |
| 48 | sub My_Actions::do_rulescip-perl cpan Marpa-R2 12.000000 main/`My_Actions::do_rule`(). { |
| 49 | my ( undef, $t1, undef, $t2 ) = @_scip-perl cpan Marpa-R2 12.000000 main/`@_`.; |
| 50 | return [$t1, @{$t2}]; |
| 51 | } |
| 52 | |
| 53 | my $grammar = Marpa::R2::Scanless::G->newscip-perl cpan Marpa-R2 12.000000 Marpa/R2/Scanless/G/new().( |
| 54 | { |
| 55 | source => \$bnf, |
| 56 | } |
| 57 | ); |
| 58 | |
| 59 | my $string = 'w x y z w x y z w'; |
| 60 | |
| 61 | my $recce = Marpa::R2::Scanless::R->newscip-perl cpan Marpa-R2 12.000000 Marpa/R2/Scanless/R/new().( { grammar => $grammar } ); |
| 62 | $recce->readscip-perl cpan Marpa-R2 12.000000 Marpa/R2/Scanless/R/read().( \$string ); |
| 63 | |
| 64 | my $metric = $recce->ambiguity_metricscip-perl cpan Marpa-R2 12.000000 Marpa/R2/Scanless/R/ambiguity_metric().(); |
| 65 | Test::More::isscip-perl cpan Marpa-R2 12.000000 Test/More/is().($metric, 2, "Ambiguity Metric"); |
| 66 | |
| 67 | my $expected_items = <<'END_OF_ITEMS'; |
| 68 | === Progress @0 === |
| 69 | P0 @0-0 L0c0 A -> . 'w' 'x' B |
| 70 | P1 @0-0 L0c0 A -> . 'w' |
| 71 | P5 @0-0 L0c0 :start -> . A |
| 72 | === Progress @1 === |
| 73 | R0:1 @0-1 L1c1 A -> 'w' . 'x' B |
| 74 | F1 @0-1 L1c1 A -> 'w' . |
| 75 | F5 @0-1 L1c1 :start -> A . |
| 76 | === Progress @2 === |
| 77 | R0:2 @0-2 L1c1-3 A -> 'w' 'x' . B |
| 78 | P2 @2-2 L1c3 B -> . C |
| 79 | P3 @2-2 L1c3 C -> . 'y' 'z' A |
| 80 | P4 @2-2 L1c3 C -> . 'y' 'z' 'w' 'x' 'y' 'z' 'w' |
| 81 | L2 [[A -> 'w' 'x' . B]; "B"; 0] |
| 82 | L2 [[A -> 'w' 'x' . B]; "C"; 2] |
| 83 | === Progress @3 === |
| 84 | R3:1 @2-3 L1c3-5 C -> 'y' . 'z' A |
| 85 | R4:1 @2-3 L1c3-5 C -> 'y' . 'z' 'w' 'x' 'y' 'z' 'w' |
| 86 | === Progress @4 === |
| 87 | P0 @4-4 L1c7 A -> . 'w' 'x' B |
| 88 | P1 @4-4 L1c7 A -> . 'w' |
| 89 | R3:2 @2-4 L1c3-7 C -> 'y' 'z' . A |
| 90 | R4:2 @2-4 L1c3-7 C -> 'y' 'z' . 'w' 'x' 'y' 'z' 'w' |
| 91 | L4 [[A -> 'w' 'x' . B]; "A"; 2] |
| 92 | === Progress @5 === |
| 93 | R0:1 @4-5 L1c7-9 A -> 'w' . 'x' B |
| 94 | F0 @0-5 L1c1-9 A -> 'w' 'x' B . |
| 95 | F1 @4-5 L1c7-9 A -> 'w' . |
| 96 | F2 @2-5 L1c3-9 B -> C . |
| 97 | F3 @2-5 L1c3-9 C -> 'y' 'z' A . |
| 98 | R4:3 @2-5 L1c3-9 C -> 'y' 'z' 'w' . 'x' 'y' 'z' 'w' |
| 99 | F5 @0-5 L1c1-9 :start -> A . |
| 100 | === Progress @6 === |
| 101 | R0:2 @4-6 L1c7-11 A -> 'w' 'x' . B |
| 102 | P2 @6-6 L1c11 B -> . C |
| 103 | P3 @6-6 L1c11 C -> . 'y' 'z' A |
| 104 | P4 @6-6 L1c11 C -> . 'y' 'z' 'w' 'x' 'y' 'z' 'w' |
| 105 | R4:4 @2-6 L1c3-11 C -> 'y' 'z' 'w' 'x' . 'y' 'z' 'w' |
| 106 | L6 [[A -> 'w' 'x' . B]; "B"; 4] |
| 107 | L6 [[A -> 'w' 'x' . B]; "C"; 6] |
| 108 | === Progress @7 === |
| 109 | R3:1 @6-7 L1c11-13 C -> 'y' . 'z' A |
| 110 | R4:1 @6-7 L1c11-13 C -> 'y' . 'z' 'w' 'x' 'y' 'z' 'w' |
| 111 | R4:5 @2-7 L1c3-13 C -> 'y' 'z' 'w' 'x' 'y' . 'z' 'w' |
| 112 | === Progress @8 === |
| 113 | P0 @8-8 L1c15 A -> . 'w' 'x' B |
| 114 | P1 @8-8 L1c15 A -> . 'w' |
| 115 | R3:2 @6-8 L1c11-15 C -> 'y' 'z' . A |
| 116 | R4:2 @6-8 L1c11-15 C -> 'y' 'z' . 'w' 'x' 'y' 'z' 'w' |
| 117 | R4:6 @2-8 L1c3-15 C -> 'y' 'z' 'w' 'x' 'y' 'z' . 'w' |
| 118 | L8 [[A -> 'w' 'x' . B]; "A"; 6] |
| 119 | === Progress @9 === |
| 120 | R0:1 @8-9 L1c15-17 A -> 'w' . 'x' B |
| 121 | F0 x2 @0,4-9 L1c1-17 A -> 'w' 'x' B . |
| 122 | F1 @8-9 L1c15-17 A -> 'w' . |
| 123 | F2 x2 @2,6-9 L1c3-17 B -> C . |
| 124 | F3 x2 @2,6-9 L1c3-17 C -> 'y' 'z' A . |
| 125 | R4:3 @6-9 L1c11-17 C -> 'y' 'z' 'w' . 'x' 'y' 'z' 'w' |
| 126 | F4 @2-9 L1c3-17 C -> 'y' 'z' 'w' 'x' 'y' 'z' 'w' . |
| 127 | F5 @0-9 L1c1-17 :start -> A . |
| 128 | END_OF_ITEMS |
| 129 | |
| 130 | my @actual_items = (); |
| 131 | my $latest_earley_set = $recce->latest_earley_setscip-perl cpan Marpa-R2 12.000000 Marpa/R2/Scanless/R/latest_earley_set().(); |
| 132 | for (my $i = 0; $i <= $latest_earley_set; $i++) { |
| 133 | pushscip-perl cpan Marpa-R2 12.000000 main/push(). @actual_items, (sprintfscip-perl cpan Marpa-R2 12.000000 main/sprintf(). "=== Progress @%d ===\n", $i); |
| 134 | pushscip-perl cpan Marpa-R2 12.000000 main/push(). @actual_items, $recce->show_parse_itemsscip-perl cpan Marpa-R2 12.000000 Marpa/R2/Scanless/R/show_parse_items().($i); |
| 135 | } |
| 136 | my $actual_items = joinscip-perl cpan Marpa-R2 12.000000 main/join(). '', @actual_items; |
| 137 | |
| 138 | Test::More::isscip-perl cpan Marpa-R2 12.000000 Test/More/is().($actual_items, $expected_items, "Parse items"); |
| 139 | |
| 140 | my %expected_value = ( |
| 141 | q{\['w','x',[['y','z','w','x','y','z','w']]]} => 1, |
| 142 | q{\['w','x',[['y','z',['w','x',[['y','z',['w']]]]]]]} => 1, |
| 143 | ); |
| 144 | |
| 145 | my $i = 0; |
| 146 | VALUE: while (1) { |
| 147 | my $value_ref = $recce->valuescip-perl cpan Marpa-R2 12.000000 Marpa/R2/Scanless/R/value().(); |
| 148 | last VALUE if not definedscip-perl cpan Marpa-R2 12.000000 main/defined(). $value_ref; |
| 149 | my $dumped_value = myDumpscip-perl cpan Marpa-R2 12.000000 main/myDump().( $value_ref ); |
| 150 | # say STDERR "Got: $dumped_value"; |
| 151 | if ( definedscip-perl cpan Marpa-R2 12.000000 main/defined(). $expected_value{$dumped_value} ) { |
| 152 | deletescip-perl cpan Marpa-R2 12.000000 main/delete(). $expected_value{$dumped_value}; |
| 153 | Test::More::passscip-perl cpan Marpa-R2 12.000000 Test/More/pass().("Expected Value $i: $dumped_value"); |
| 154 | } |
| 155 | else { |
| 156 | Test::More::failscip-perl cpan Marpa-R2 12.000000 Test/More/fail().("Unexpected Value $i: $dumped_value"); |
| 157 | } |
| 158 | $i++; |
| 159 | } |
| 160 | my @not_found = keysscip-perl cpan Marpa-R2 12.000000 main/keys(). %expected_value; |
| 161 | my $not_found_count = scalarscip-perl cpan Marpa-R2 12.000000 main/scalar(). @not_found; |
| 162 | if ($not_found_count) { |
| 163 | Test::More::failscip-perl cpan Marpa-R2 12.000000 Test/More/fail().("$not_found_count expected value(s) not found"); |
| 164 | for my $value (@not_found) { |
| 165 | Test::More::diagscip-perl cpan Marpa-R2 12.000000 Test/More/diag().("$value"); |
| 166 | } |
| 167 | } |
| 168 | else { |
| 169 | Test::More::passscip-perl cpan Marpa-R2 12.000000 Test/More/pass().("All expected values found"); |
| 170 | } |
| 171 | |
| 172 | sub main::dwimscip-perl cpan Marpa-R2 12.000000 main/`main::dwim`(). { |
| 173 | my @result = (); |
| 174 | shiftscip-perl cpan Marpa-R2 12.000000 main/shift().; |
| 175 | ARG: for my $v ( @_scip-perl cpan Marpa-R2 12.000000 main/`@_`. ) { |
| 176 | next ARG if not $v; |
| 177 | my $type = Scalar::Util::reftypescip-perl cpan Marpa-R2 12.000000 Scalar/Util/reftype(). $v; |
| 178 | if (not $type or $type ne 'ARRAY') { |
| 179 | pushscip-perl cpan Marpa-R2 12.000000 main/push(). @result, $v; |
| 180 | next ARG; |
| 181 | } |
| 182 | my $size = scalarscip-perl cpan Marpa-R2 12.000000 main/scalar(). @{$v}; |
| 183 | next ARG if $size == 0; |
| 184 | if ($size == 1) { |
| 185 | pushscip-perl cpan Marpa-R2 12.000000 main/push(). @result, ${$v}[0]; |
| 186 | next ARG; |
| 187 | } |
| 188 | pushscip-perl cpan Marpa-R2 12.000000 main/push(). @result, $v; |
| 189 | } |
| 190 | return [@result]; |
| 191 | } |
| 192 | |
| 193 | sub myDumpscip-perl cpan Marpa-R2 12.000000 main/myDump(). { |
| 194 | my $v = shiftscip-perl cpan Marpa-R2 12.000000 main/shift().; |
| 195 | return Data::Dumpscip-perl cpan Marpa-R2 12.000000 main/Dump().er->newscip-perl cpan Marpa-R2 12.000000 Data/Dumper/new().( [$v] )->Indentscip-perl cpan Marpa-R2 12.000000 main/Indent().(0)->Tersescip-perl cpan Marpa-R2 12.000000 main/Terse().(1)->Dump; |
| 196 | } |
| 197 | |
| 198 | # vim: expandtab shiftwidth=4: |