File: Value.pm

package info (click to toggle)
libmarpa-r2-perl 12.000000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,660 kB
  • sloc: perl: 42,628; ansic: 23,387; sh: 4,363; makefile: 157
file content (2296 lines) | stat: -rw-r--r-- 93,304 bytes parent folder | download
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
# 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/.

package Marpa::R2::Value;

use 5.010001;
use warnings;
use strict;

use vars qw($VERSION $STRING_VERSION);
$VERSION        = '12.000000';
$STRING_VERSION = $VERSION;
## no critic (BuiltinFunctions::ProhibitStringyEval)
$VERSION = eval $VERSION;
## use critic

package Marpa::R2::Internal::Value;

use English qw( -no_match_vars );

use constant SKIP => -1;

sub Marpa::R2::show_rank_ref {
    my ($rank_ref) = @_;
    return 'undef' if not defined $rank_ref;
    return 'SKIP'  if $rank_ref == Marpa::R2::Internal::Value::SKIP;
    return ${$rank_ref};
} ## end sub Marpa::R2::show_rank_ref

package Marpa::R2::Internal::Value;

# Given the grammar and an action name, resolve it to a closure,
# or return undef
sub Marpa::R2::Internal::Recognizer::resolve_action {
    my ( $recce, $closure_name, $p_error ) = @_;
    my $grammar  = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $closures = $recce->[Marpa::R2::Internal::Recognizer::CLOSURES];
    my $trace_actions =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_ACTIONS];

    # A reserved closure name;
    return [ q{}, undef, '::!default' ] if not defined $closure_name;

    if ( $closure_name eq q{} ) {
        ${$p_error} = q{The action string cannot be the empty string}
            if defined $p_error;
        return;
    }

    return [ q{}, \undef, $closure_name ] if $closure_name eq '::undef';
    if (   substr( $closure_name, 0, 2 ) eq q{::}
        or substr( $closure_name, 0, 1 ) eq '[' )
    {
        return [ q{}, undef, $closure_name ];
    }

    if ( my $closure = $closures->{$closure_name} ) {
        if ($trace_actions) {
            print {$Marpa::R2::Internal::TRACE_FH}
                qq{Resolved "$closure_name" to explicit closure\n}
                or Marpa::R2::exception('Could not print to trace file');
        }

        return [ $closure_name, $closure, '::array' ];
    } ## end if ( my $closure = $closures->{$closure_name} )

    my $fully_qualified_name;
    if ( $closure_name =~ /([:][:])|[']/xms ) {
        $fully_qualified_name = $closure_name;
    }

    if ( not $fully_qualified_name ) {
        my $resolve_package =
            $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE];
        if ( not defined $resolve_package ) {
        say STDERR
            $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE];
        say STDERR
            $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE_SOURCE];
            ${$p_error} = Marpa::R2::Internal::X->new(
                {   message =>
                        qq{Could not fully qualify "$closure_name": no resolve package},
                    name => 'NO RESOLVE PACKAGE'
                }
            );
            return;
        } ## end if ( not defined $resolve_package )
        $fully_qualified_name = $resolve_package . q{::} . $closure_name;
    } ## end if ( not $fully_qualified_name )

    my $closure;
    my $type;
    TYPE: {
        no strict 'refs';
        $closure = *{$fully_qualified_name}{'CODE'};
        use strict;
        if ( defined $closure ) {
            $type = 'CODE';
            last TYPE;
        }
        no strict 'refs';
        $closure = *{$fully_qualified_name}{'SCALAR'};
        use strict;

        # Currently $closure is always defined, but this
        # behavior is said to be subject to change in perlref
        if ( defined $closure and defined ${$closure} ) {
            $type = 'SCALAR';
            last TYPE;
        }

        # Re other symbol tables entries:
        # We ignore ARRAY and HASH because they anything
        # we resolve to is a potential array entry, something
        # that not possible for arrays and hashes except
        # indirectly, via references.
        # FORMAT is deprecated.
        # IO and GLOB seem too abstruse at the moment.

        $closure = undef;
    } ## end TYPE:

    if ( defined $closure ) {
        if ($trace_actions) {
            print {$Marpa::R2::Internal::TRACE_FH}
                qq{Successful resolution of action "$closure_name" as $type },
                'to ', $fully_qualified_name, "\n"
                or Marpa::R2::exception('Could not print to trace file');
        } ## end if ($trace_actions)
        return [ $fully_qualified_name, $closure, '::array' ];
    } ## end if ( defined $closure )

    if ( $trace_actions or defined $p_error ) {
        for my $slot (qw(ARRAY HASH IO FORMAT)) {
            no strict 'refs';
            if ( defined *{$fully_qualified_name}{$slot} ) {
                my $error =
                    qq{Failed resolution of action "$closure_name" to $fully_qualified_name\n}
                    . qq{  $fully_qualified_name is present as a $slot, but a $slot is not an acceptable resolution\n};
                if ($trace_actions) {
                    print {$Marpa::R2::Internal::TRACE_FH} $error
                        or
                        Marpa::R2::exception('Could not print to trace file');
                }
                ${$p_error} = $error if defined $p_error;
                return;
            } ## end if ( defined *{$fully_qualified_name}{$slot} )
        } ## end for my $slot (qw(ARRAY HASH IO FORMAT))
    } ## end if ( $trace_actions or defined $p_error )

    {
        my $error =
            qq{Failed resolution of action "$closure_name" to $fully_qualified_name\n};
        ${$p_error} = $error if defined $p_error;
        if ($trace_actions) {
            print {$Marpa::R2::Internal::TRACE_FH} $error
                or Marpa::R2::exception('Could not print to trace file');
        }
    }
    return;

} ## end sub Marpa::R2::Internal::Recognizer::resolve_action

# Find the semantics for a lexeme.
sub Marpa::R2::Internal::Recognizer::lexeme_semantics_find {
    my ( $recce, $lexeme_id ) = @_;
    my $grammar   = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $symbols   = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
    my $symbol    = $symbols->[$lexeme_id];
    my $semantics = $symbol->[Marpa::R2::Internal::Symbol::LEXEME_SEMANTICS];
    return '::!default' if not defined $semantics;
    return $semantics;
} ## end sub Marpa::R2::Internal::Recognizer::lexeme_semantics_find

# Find the blessing for a rule.
sub Marpa::R2::Internal::Recognizer::rule_blessing_find {
    my ( $recce, $rule_id ) = @_;
    my $grammar  = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $rules    = $grammar->[Marpa::R2::Internal::Grammar::RULES];
    my $rule     = $rules->[$rule_id];
    my $blessing = $rule->[Marpa::R2::Internal::Rule::BLESSING];
    $blessing = '::undef' if not defined $blessing;
    return $blessing if $blessing eq '::undef';
    my $bless_package =
        $grammar->[Marpa::R2::Internal::Grammar::BLESS_PACKAGE];

    if ( not defined $bless_package ) {
        Marpa::R2::exception(
                  qq{A blessed rule is in a grammar with no bless_package\n}
                . qq{  The rule was blessed as "$blessing"\n} );
    }
    return join q{}, $bless_package, q{::}, $blessing;
} ## end sub Marpa::R2::Internal::Recognizer::rule_blessing_find

# Find the blessing for a lexeme.
sub Marpa::R2::Internal::Recognizer::lexeme_blessing_find {
    my ( $recce, $lexeme_id ) = @_;
    my $grammar  = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $symbols  = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
    my $symbol   = $symbols->[$lexeme_id];
    my $blessing = $symbol->[Marpa::R2::Internal::Symbol::BLESSING];

    return '::undef' if not defined $blessing;
    return '::undef' if $blessing eq '::undef';
    if ( $blessing =~ m/\A [:][:] /xms ) {
        my $tracer      = $grammar->[Marpa::R2::Internal::Grammar::TRACER];
        my $lexeme_name = $tracer->symbol_name($lexeme_id);
        $recce->[Marpa::R2::Internal::Recognizer::ERROR_MESSAGE] =
            qq{Symbol "$lexeme_name" has unknown blessing: "$blessing"};
        return;
    } ## end if ( $blessing =~ m/\A [:][:] /xms )
    if ( $blessing =~ m/ [:][:] /xms ) {
        return $blessing;
    }
    my $bless_package =
        $grammar->[Marpa::R2::Internal::Grammar::BLESS_PACKAGE];
    if ( not defined $bless_package ) {
        my $tracer      = $grammar->[Marpa::R2::Internal::Grammar::TRACER];
        my $lexeme_name = $tracer->symbol_name($lexeme_id);
        $recce->[Marpa::R2::Internal::Recognizer::ERROR_MESSAGE] =
            qq{Symbol "$lexeme_name" needs a blessing package, but grammar has none\n}
            . qq{  The blessing for "$lexeme_name" was "$blessing"\n};
        return;
    } ## end if ( not defined $bless_package )
    return $bless_package . q{::} . $blessing;
} ## end sub Marpa::R2::Internal::Recognizer::lexeme_blessing_find

# For diagnostics
sub Marpa::R2::Internal::Recognizer::brief_rule_list {
    my ( $recce, $rule_ids ) = @_;
    my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my @brief_rules = map { $grammar->brief_rule($_) } @{$rule_ids};
    return join q{}, map { q{    } . $_ . "\n" } @brief_rules;
} ## end sub Marpa::R2::Internal::Recognizer::brief_rule_list

our $CONTEXT_EXCEPTION_CLASS = __PACKAGE__ . '::Context_Exception';

sub Marpa::R2::Context::bail { ## no critic (Subroutines::RequireArgUnpacking)
    if ( scalar @_ == 1 and ref $_[0] ) {
        die bless { exception_object => $_[0] }, $CONTEXT_EXCEPTION_CLASS;
    }
    my $error_string = join q{}, @_;
    my ( $package, $filename, $line ) = caller;
    chomp $error_string;
    die bless { message => qq{User bailed at line $line in file "$filename"\n}
            . $error_string
            . "\n" }, $CONTEXT_EXCEPTION_CLASS;
} ## end sub Marpa::R2::Context::bail
## use critic

sub Marpa::R2::Context::location {
    my $valuator = $Marpa::R2::Internal::Context::VALUATOR;
    Marpa::R2::exception(
        'Marpa::R2::Context::location called outside of a valuation context')
        if not defined $valuator;
    return $valuator->location();
} ## end sub Marpa::R2::Context::location

sub code_problems {
    my $args = shift;

    my $grammar;
    my $fatal_error;
    my $warnings = [];
    my $where    = '?where?';
    my $long_where;
    my @msg = ();
    my $eval_value;
    my $eval_given = 0;

    push @msg, q{=} x 60, "\n";
    ARG: for my $arg ( keys %{$args} ) {
        my $value = $args->{$arg};
        if ( $arg eq 'fatal_error' ) { $fatal_error = $value; next ARG }
        if ( $arg eq 'grammar' )     { $grammar     = $value; next ARG }
        if ( $arg eq 'where' )       { $where       = $value; next ARG }
        if ( $arg eq 'long_where' )  { $long_where  = $value; next ARG }
        if ( $arg eq 'warnings' )    { $warnings    = $value; next ARG }
        if ( $arg eq 'eval_ok' ) {
            $eval_value = $value;
            $eval_given = 1;
            next ARG;
        }
        push @msg, "Unknown argument to code_problems: $arg";
    } ## end ARG: for my $arg ( keys %{$args} )

    GIVEN_FATAL_ERROR_REF_TYPE: {
        my $fatal_error_ref_type = ref $fatal_error;
        last GIVEN_FATAL_ERROR_REF_TYPE if not $fatal_error_ref_type;
        if ( $fatal_error_ref_type eq $CONTEXT_EXCEPTION_CLASS ) {
            my $exception_object = $fatal_error->{exception_object};
            die $exception_object if defined $exception_object;
            my $exception_message = $fatal_error->{message};
            die $exception_message if defined $exception_message;
            die "Internal error: bad $CONTEXT_EXCEPTION_CLASS object";
        } ## end if ( $fatal_error_ref_type eq $CONTEXT_EXCEPTION_CLASS)
        $fatal_error =
              "Exception thrown as object inside Marpa closure\n"
            . ( q{ } x 4 )
            . "This is not allowed\n"
            . ( q{ } x 4 )
            . qq{Exception as string is "$fatal_error"};
    } ## end GIVEN_FATAL_ERROR_REF_TYPE:

    my @problem_line     = ();
    my $max_problem_line = -1;
    for my $warning_data ( @{$warnings} ) {
        my ( $warning, $package, $filename, $problem_line ) =
            @{$warning_data};
        $problem_line[$problem_line] = 1;
        $max_problem_line = List::Util::max $problem_line, $max_problem_line;
    } ## end for my $warning_data ( @{$warnings} )

    $long_where //= $where;

    my $warnings_count = scalar @{$warnings};
    {
        my @problems;
        my $false_eval = $eval_given && !$eval_value && !$fatal_error;
        if ($false_eval) {
            push @problems, '* THE MARPA SEMANTICS RETURNED A PERL FALSE',
                'Marpa::R2 requires its semantics to return a true value';
        }
        if ($fatal_error) {
            push @problems, '* THE MARPA SEMANTICS PRODUCED A FATAL ERROR';
        }
        if ($warnings_count) {
            push @problems,
                "* THERE WERE $warnings_count WARNING(S) IN THE MARPA SEMANTICS:",
                'Marpa treats warnings as fatal errors';
        }
        if ( not scalar @problems ) {
            push @msg, '* THERE WAS A FATAL PROBLEM IN THE MARPA SEMANTICS';
        }
        push @msg, ( join "\n", @problems ) . "\n";
    }

    push @msg, "* THIS IS WHAT MARPA WAS DOING WHEN THE PROBLEM OCCURRED:\n"
        . $long_where . "\n";

    for my $warning_ix ( 0 .. ( $warnings_count - 1 ) ) {
        push @msg, "* WARNING MESSAGE NUMBER $warning_ix:\n";
        my $warning_message = $warnings->[$warning_ix]->[0];
        $warning_message =~ s/\n*\z/\n/xms;
        push @msg, $warning_message;
    } ## end for my $warning_ix ( 0 .. ( $warnings_count - 1 ) )

    if ($fatal_error) {
        push @msg, "* THIS WAS THE FATAL ERROR MESSAGE:\n";
        my $fatal_error_message = $fatal_error;
        $fatal_error_message =~ s/\n*\z/\n/xms;
        push @msg, $fatal_error_message;
    } ## end if ($fatal_error)

    Marpa::R2::exception(@msg);

    # this is to keep perlcritic happy
    return 1;

} ## end sub code_problems

# Dump semantics for diagnostics
sub show_semantics {
    my (@ops)    = @_;
    my @op_descs = ();
    my $op_ix    = 0;
    OP: while ( $op_ix < scalar @ops ) {
        my $op      = $ops[ $op_ix++ ];
        my $op_name = Marpa::R2::Thin::op_name($op);
        push @op_descs, $op_name;
        if ( $op_name eq 'bless' ) {
            push @op_descs, q{"} . $ops[$op_ix] . q{"};
            $op_ix++;
            next OP;
        }
        if ( $op_name eq 'push_constant' ) {
            push @op_descs, $ops[$op_ix];
            $op_ix++;
            next OP;
        }
        if ( $op_name eq 'push_one' ) {
            push @op_descs, $ops[$op_ix];
            $op_ix++;
            next OP;
        }
        if ( $op_name eq 'result_is_rhs_n' ) {
            push @op_descs, $ops[$op_ix];
            $op_ix++;
            next OP;
        }
        if ( $op_name eq 'result_is_n_of_sequence' ) {
            push @op_descs, $ops[$op_ix];
            $op_ix++;
            next OP;
        }
        if ( $op_name eq 'result_is_constant' ) {
            push @op_descs, $ops[$op_ix];
            $op_ix++;
            next OP;
        }
        if ( $op_name eq 'alternative' ) {
            push @op_descs, $ops[$op_ix];
            $op_ix++;
            push @op_descs, $ops[$op_ix];
            $op_ix++;
            next OP;
        } ## end if ( $op_name eq 'alternative' )
    } ## end OP: while ( $op_ix < scalar @ops )
    return join q{ }, @op_descs;
} ## end sub show_semantics

# Return false if no ordering was created,
# otherwise return the ordering.
sub Marpa::R2::Recognizer::ordering_get {
    my ($recce) = @_;
    return if $recce->[Marpa::R2::Internal::Recognizer::NO_PARSE];
    my $ordering = $recce->[Marpa::R2::Internal::Recognizer::O_C];
    return $ordering if $ordering;
    my $parse_set_arg =
        $recce->[Marpa::R2::Internal::Recognizer::END_OF_PARSE];
    my $grammar   = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
    my $recce_c   = $recce->[Marpa::R2::Internal::Recognizer::C];

    $grammar_c->throw_set(0);
    my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C] =
        Marpa::R2::Thin::B->new( $recce_c, ( $parse_set_arg // -1 ) );
    $grammar_c->throw_set(1);
    if ( not $bocage ) {
        $recce->[Marpa::R2::Internal::Recognizer::NO_PARSE] = 1;
        return;
    }
    $ordering = $recce->[Marpa::R2::Internal::Recognizer::O_C] =
        Marpa::R2::Thin::O->new($bocage);

    GIVEN_RANKING_METHOD: {
        my $ranking_method =
            $recce->[Marpa::R2::Internal::Recognizer::RANKING_METHOD];
        if ( $ranking_method eq 'high_rule_only' ) {
            do_high_rule_only($recce);
            last GIVEN_RANKING_METHOD;
        }
        if ( $ranking_method eq 'rule' ) {
            do_rank_by_rule($recce);
            last GIVEN_RANKING_METHOD;
        }
    } ## end GIVEN_RANKING_METHOD:

    return $ordering;
} ## end sub Marpa::R2::Recognizer::ordering_get

sub resolve_rule_by_id {
    my ( $recce, $rule_id ) = @_;
    my $grammar     = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $rules       = $grammar->[Marpa::R2::Internal::Grammar::RULES];
    my $rule        = $rules->[$rule_id];
    my $action_name = $rule->[Marpa::R2::Internal::Rule::ACTION_NAME];
    my $resolve_error;
    return if not defined $action_name;
    my $resolution = Marpa::R2::Internal::Recognizer::resolve_action( $recce,
        $action_name, \$resolve_error );

    if ( not $resolution ) {
        my $rule_desc = rule_describe( $grammar, $rule_id );
        Marpa::R2::exception(
            "Could not resolve rule action named '$action_name'\n",
            "  Rule was $rule_desc\n",
            q{  },
            ( $resolve_error // 'Failed to resolve action' )
        );
    } ## end if ( not $resolution )
    return $resolution;
} ## end sub resolve_rule_by_id

# For error messages -- checks if it is called in context with
# SLR defined
sub rule_describe {
    my ( $grammar, $rule_id ) = @_;
    return $Marpa::R2::Context::slr->rule_show($rule_id)
        if $Marpa::R2::Context::slr;
    return $grammar->rule_describe($rule_id);
} ## end sub rule_describe

sub resolve_recce {

    my ( $recce, $per_parse_arg ) = @_;
    my $grammar   = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
    my $rules     = $grammar->[Marpa::R2::Internal::Grammar::RULES];
    my $symbols   = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];

    my $trace_actions =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_ACTIONS] // 0;
    my $trace_file_handle =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_FILE_HANDLE];

    my $package_source =
        $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE_SOURCE];
    if (    not defined $package_source
        and defined $per_parse_arg
        and ( my $arg_blessing = Scalar::Util::blessed $per_parse_arg) )
    {
        $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE] =
            $arg_blessing;
        $package_source = 'arg';
    } ## end if ( not defined $package_source and defined $per_parse_arg...)
    $package_source //= 'semantics_package';
    $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE_SOURCE] =
        $package_source;

    if ( $package_source eq 'legacy' ) {

        # RESOLVE_PACKAGE is already set if not 'legacy'
        $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE] =
            $grammar->[Marpa::R2::Internal::Grammar::ACTIONS]
            // $grammar->[Marpa::R2::Internal::Grammar::ACTION_OBJECT];
    } ## end if ( $package_source eq 'legacy' )

    FIND_CONSTRUCTOR: {
        my $constructor_package =
            ( $package_source eq 'legacy' )
            ? $grammar->[Marpa::R2::Internal::Grammar::ACTION_OBJECT]
            : $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE];
        last FIND_CONSTRUCTOR if not defined $constructor_package;
        my $constructor_name = $constructor_package . q{::new};
        my $resolve_error;
        my $resolution =
            Marpa::R2::Internal::Recognizer::resolve_action( $recce,
            $constructor_name, \$resolve_error );
        if ($resolution) {
            $recce->[ Marpa::R2::Internal::Recognizer::PER_PARSE_CONSTRUCTOR ]
                = $resolution->[1];
            last FIND_CONSTRUCTOR;
        }
        last FIND_CONSTRUCTOR if $package_source ne 'legacy';
        Marpa::R2::exception(
            qq{Could not find constructor "$constructor_name"},
            q{  }, ( $resolve_error // 'Failed to resolve action' ) );
    } ## end FIND_CONSTRUCTOR:

    my $resolve_error;

    my $default_action =
        $grammar->[Marpa::R2::Internal::Grammar::DEFAULT_ACTION];
    my $default_action_resolution =
        Marpa::R2::Internal::Recognizer::resolve_action( $recce,
        $default_action, \$resolve_error );
    Marpa::R2::exception(
        "Could not resolve default action named '$default_action'\n",
        q{  }, ( $resolve_error // 'Failed to resolve action' ) )
        if not $default_action_resolution;

    my $default_empty_action =
        $grammar->[Marpa::R2::Internal::Grammar::DEFAULT_EMPTY_ACTION];
    my $default_empty_action_resolution;
    if ($default_empty_action) {
        $default_empty_action_resolution =
            Marpa::R2::Internal::Recognizer::resolve_action( $recce,
            $default_empty_action, \$resolve_error );
        Marpa::R2::exception(
            "Could not resolve default empty rule action named '$default_empty_action'",
            q{  },
            ( $resolve_error // 'Failed to resolve action' )
        ) if not $default_empty_action_resolution;
    } ## end if ($default_empty_action)

    my $rule_resolutions = [];

    RULE: for my $rule_id ( $grammar->rule_ids() ) {

        my $rule_resolution = resolve_rule_by_id( $recce, $rule_id );
        if (    not defined $rule_resolution
            and $default_empty_action
            and $grammar_c->rule_length($rule_id) == 0 )
        {
            $rule_resolution = $default_empty_action_resolution;
        } ## end if ( not defined $rule_resolution and $default_empty_action...)

        $rule_resolution //= $default_action_resolution;

        if ( not $rule_resolution ) {
            my $rule_desc = rule_describe( $grammar, $rule_id );
            my $message = "Could not resolve action\n  Rule was $rule_desc\n";

            my $rule   = $rules->[$rule_id];
            my $action = $rule->[Marpa::R2::Internal::Rule::ACTION_NAME];
            $message .= qq{  Action was specified as "$action"\n}
                if defined $action;
            my $recce_error =
                $recce->[Marpa::R2::Internal::Recognizer::ERROR_MESSAGE];
            $message .= q{  } . $recce_error if defined $recce_error;
            Marpa::R2::exception($message);
        } ## end if ( not $rule_resolution )

        DETERMINE_BLESSING: {

            my $blessing =
                Marpa::R2::Internal::Recognizer::rule_blessing_find( $recce,
                $rule_id );
            my ( $closure_name, $closure, $semantics ) = @{$rule_resolution};

            if ( $blessing ne '::undef' ) {
                $semantics = '::array' if $semantics eq '::!default';
                CHECK_SEMANTICS: {
                    last CHECK_SEMANTICS if $semantics eq '::array';
                    last CHECK_SEMANTICS
                        if ( substr $semantics, 0, 1 ) eq '[';
                    Marpa::R2::exception(
                        qq{Attempt to bless, but improper semantics: "$semantics"}
                    );
                } ## end CHECK_SEMANTICS:
            } ## end if ( $blessing ne '::undef' )

            $rule_resolution =
                [ $closure_name, $closure, $semantics, $blessing ];
        } ## end DETERMINE_BLESSING:

        $rule_resolutions->[$rule_id] = $rule_resolution;

    } ## end RULE: for my $rule_id ( $grammar->rule_ids() )

    if ( $trace_actions >= 2 ) {
        RULE: for my $rule_id ( 0 .. $#{$rules} ) {
            my ( $resolution_name, $closure ) =
                @{ $rule_resolutions->[$rule_id] };
            say {$trace_file_handle} 'Rule ',
                $grammar->brief_rule($rule_id),
                qq{ resolves to "$resolution_name"}
                or Marpa::R2::exception('print to trace handle failed');
        } ## end RULE: for my $rule_id ( 0 .. $#{$rules} )
    } ## end if ( $trace_actions >= 2 )

    my @lexeme_resolutions = ();
    SYMBOL: for my $lexeme_id ( 0 .. $#{$symbols} ) {
        my $semantics =
            Marpa::R2::Internal::Recognizer::lexeme_semantics_find( $recce,
            $lexeme_id );
        if ( not defined $semantics ) {
            my $message =
                  "Could not determine lexeme's semantics\n"
                . q{  Lexeme was }
                . $grammar->symbol_name($lexeme_id) . "\n";
            $message
                .= q{  }
                . $recce->[Marpa::R2::Internal::Recognizer::ERROR_MESSAGE];
            Marpa::R2::exception($message);
        } ## end if ( not defined $semantics )
        my $blessing =
            Marpa::R2::Internal::Recognizer::lexeme_blessing_find( $recce,
            $lexeme_id );
        if ( not defined $blessing ) {
            my $message =
                  "Could not determine lexeme's blessing\n"
                . q{  Lexeme was }
                . $grammar->symbol_name($lexeme_id) . "\n";
            $message
                .= q{  }
                . $recce->[Marpa::R2::Internal::Recognizer::ERROR_MESSAGE];
            Marpa::R2::exception($message);
        } ## end if ( not defined $blessing )
        $lexeme_resolutions[$lexeme_id] = [ $semantics, $blessing ];

    } ## end SYMBOL: for my $lexeme_id ( 0 .. $#{$symbols} )

    return ( $rule_resolutions, \@lexeme_resolutions );
} ## end sub resolve_recce

sub registration_init {
    my ( $recce, $per_parse_arg ) = @_;

    my $trace_file_handle =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_FILE_HANDLE];
    my $grammar   = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
    my $recce_c   = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $tracer    = $grammar->[Marpa::R2::Internal::Grammar::TRACER];
    my $trace_actions =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_ACTIONS] // 0;
    my $rules   = $grammar->[Marpa::R2::Internal::Grammar::RULES];
    my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];

    my @closure_by_rule_id   = ();
    my @semantics_by_rule_id = ();
    my @blessing_by_rule_id  = ();

    my ( $rule_resolutions, $lexeme_resolutions ) =
        resolve_recce( $recce, $per_parse_arg );

    # Set the arrays, and perform various checks on the resolutions
    # we received
    {
        # ::whatever is deprecated and has been removed from the docs
        # it is now equivalent to ::undef

        RULE:
        for my $rule_id ( $grammar->rule_ids() ) {
            my ( $new_resolution, $closure, $semantics, $blessing ) =
                @{ $rule_resolutions->[$rule_id] };
            my $lhs_id = $grammar_c->rule_lhs($rule_id);

            REFINE_SEMANTICS: {

                if ('[' eq substr $semantics,
                    0, 1 and ']' eq substr $semantics,
                    -1, 1
                    )
                {
                    # Normalize array semantics
                    $semantics =~ s/ //gxms;
                    last REFINE_SEMANTICS;
                } ## end if ( '[' eq substr $semantics, 0, 1 and ']' eq ...)

                state $allowed_semantics = {
                    map { ; ( $_, 1 ) }
                        qw(::array ::undef ::first ::whatever ::!default),
                    q{}
                };
                last REFINE_SEMANTICS if $allowed_semantics->{$semantics};
                last REFINE_SEMANTICS
                    if $semantics =~ m/ \A rhs \d+ \z /xms;

                Marpa::R2::exception(
                    q{Unknown semantics for rule },
                    $grammar->brief_rule($rule_id),
                    "\n",
                    qq{    Semantics were specified as "$semantics"\n}
                );

            } ## end REFINE_SEMANTICS:

            $semantics_by_rule_id[$rule_id] = $semantics;
            $blessing_by_rule_id[$rule_id]  = $blessing;
            $closure_by_rule_id[$rule_id]   = $closure;

            CHECK_BLESSING: {
                last CHECK_BLESSING if $blessing eq '::undef';
                if ($closure) {
                    my $ref_type = Scalar::Util::reftype $closure;
                    if ( $ref_type eq 'SCALAR' ) {

                        # The constant's dump might be long so I repeat the error message
                        Marpa::R2::exception(
                            qq{Fatal error: Attempt to bless a rule that resolves to a scalar constant\n},
                            qq{  Scalar constant is },
                            Data::Dumper::Dumper($closure),
                            qq{  Blessing is "$blessing"\n},
                            q{  Rule is: },
                            $grammar->brief_rule($rule_id),
                            "\n",
                            qq{  Cannot bless rule when it resolves to a scalar constant},
                            "\n",
                        );
                    } ## end if ( $ref_type eq 'SCALAR' )
                    last CHECK_BLESSING;
                } ## end if ($closure)
                last CHECK_BLESSING if $semantics eq '::array';
                last CHECK_BLESSING if ( substr $semantics, 0, 1 ) eq '[';
                Marpa::R2::exception(
                    qq{Cannot bless rule when the semantics are "$semantics"},
                    q{  Rule is: },
                    $grammar->brief_rule($rule_id),
                    "\n",
                    qq{  Blessing is "$blessing"\n},
                    qq{  Semantics are "$semantics"\n}
                );
            } ## end CHECK_BLESSING:

        } ## end RULE: for my $rule_id ( $grammar->rule_ids() )

    } ## end CHECK_FOR_WHATEVER_CONFLICT

    # A LHS can be nullable via more than one rule,
    # and that means more than one semantics might be specified for
    # the nullable symbol.  This logic deals with that.
    my @nullable_rule_ids_by_lhs = ();
    RULE: for my $rule_id ( $grammar->rule_ids() ) {
        my $lhs_id = $grammar_c->rule_lhs($rule_id);
        push @{ $nullable_rule_ids_by_lhs[$lhs_id] }, $rule_id
            if $grammar_c->rule_is_nullable($rule_id);
    }

    my @null_symbol_closures;
    LHS:
    for ( my $lhs_id = 0; $lhs_id <= $#nullable_rule_ids_by_lhs; $lhs_id++ ) {
        my $rule_ids = $nullable_rule_ids_by_lhs[$lhs_id];
        my $resolution_rule;

        # No nullable rules for this LHS?  No problem.
        next LHS if not defined $rule_ids;
        my $rule_count = scalar @{$rule_ids};

        # I am not sure if this test is necessary
        next LHS if $rule_count <= 0;

        # Just one nullable rule?  Then that's our semantics.
        if ( $rule_count == 1 ) {
            $resolution_rule = $rule_ids->[0];
            my ( $resolution_name, $closure ) =
                @{ $rule_resolutions->[$resolution_rule] };
            if ($trace_actions) {
                my $lhs_name = $grammar->symbol_name($lhs_id);
                say {$trace_file_handle}
                    qq{Nulled symbol "$lhs_name" },
                    qq{ resolved to "$resolution_name" from rule },
                    $grammar->brief_rule($resolution_rule)
                    or Marpa::R2::exception('print to trace handle failed');
            } ## end if ($trace_actions)
            $null_symbol_closures[$lhs_id] = $resolution_rule;
            next LHS;
        } ## end if ( $rule_count == 1 )

        # More than one rule?  Are any empty?
        # If so, use the semantics of the empty rule
        my @empty_rules =
            grep { $grammar_c->rule_length($_) <= 0 } @{$rule_ids};
        if ( scalar @empty_rules ) {
            $resolution_rule = $empty_rules[0];
            my ( $resolution_name, $closure ) =
                @{ $rule_resolutions->[$resolution_rule] };
            if ($trace_actions) {
                my $lhs_name = $grammar->symbol_name($lhs_id);
                say {$trace_file_handle}
                    qq{Nulled symbol "$lhs_name" },
                    qq{ resolved to "$resolution_name" from rule },
                    $grammar->brief_rule($resolution_rule)
                    or Marpa::R2::exception('print to trace handle failed');
            } ## end if ($trace_actions)
            $null_symbol_closures[$lhs_id] = $resolution_rule;
            next LHS;
        } ## end if ( scalar @empty_rules )

        # Multiple rules, none of them empty.
        my ( $first_resolution, @other_resolutions ) =
            map { $rule_resolutions->[$_] } @{$rule_ids};

        # Do they have more than one semantics?
        # If so, just call it an error and let the user sort it out.
        my ( $first_closure_name, undef, $first_semantics, $first_blessing )
            = @{$first_resolution};
        OTHER_RESOLUTION: for my $other_resolution (@other_resolutions) {
            my ( $other_closure_name, undef, $other_semantics,
                $other_blessing )
                = @{$other_resolution};

            if (   $first_closure_name ne $other_closure_name
                or $first_semantics ne $other_semantics
                or $first_blessing ne $other_blessing )
            {
                Marpa::R2::exception(
                    'When nulled, symbol ',
                    $grammar->symbol_name($lhs_id),
                    qq{  can have more than one semantics\n},
                    qq{  Marpa needs there to be only one semantics\n},
                    qq{  The rules involved are:\n},
                    Marpa::R2::Internal::Recognizer::brief_rule_list(
                        $recce, $rule_ids
                    )
                );
            } ## end if ( $first_closure_name ne $other_closure_name or ...)
        } ## end OTHER_RESOLUTION: for my $other_resolution (@other_resolutions)

        # Multiple rules, but they all have one semantics.
        # So (obviously) use that semantics
        $resolution_rule = $rule_ids->[0];
        my ( $resolution_name, $closure ) =
            @{ $rule_resolutions->[$resolution_rule] };
        if ($trace_actions) {
            my $lhs_name = $grammar->symbol_name($lhs_id);
            say {$trace_file_handle}
                qq{Nulled symbol "$lhs_name" },
                qq{ resolved to "$resolution_name" from rule },
                $grammar->brief_rule($resolution_rule)
                or Marpa::R2::exception('print to trace handle failed');
        } ## end if ($trace_actions)
        $null_symbol_closures[$lhs_id] = $resolution_rule;

    } ## end LHS: for ( my $lhs_id = 0; $lhs_id <= $#nullable_rule_ids_by_lhs...)

    # Do consistency checks

    # Set the object values
    $recce->[Marpa::R2::Internal::Recognizer::NULL_VALUES] =
        \@null_symbol_closures;

    my @semantics_by_lexeme_id = ();
    my @blessing_by_lexeme_id  = ();

    # Check the lexeme semantics
    {
        # ::whatever is deprecated and has been removed from the docs
        # it is now equivalent to ::undef
        LEXEME: for my $lexeme_id ( 0 .. $#{$symbols} ) {

            my ( $semantics, $blessing ) =
                @{ $lexeme_resolutions->[$lexeme_id] };
            CHECK_SEMANTICS: {
                if ( not $semantics ) {
                    $semantics = '::!default';
                    last CHECK_SEMANTICS;
                }
                if ( ( substr $semantics, 0, 1 ) eq '[' ) {
                    $semantics =~ s/ //gxms;
                    last CHECK_SEMANTICS;
                }
                state $allowed_semantics =
                    { map { ; ( $_, 1 ) } qw(::array ::undef ::!default ) };

                if ( not $allowed_semantics->{$semantics} ) {
                    Marpa::R2::exception(
                        q{Unknown semantics for lexeme },
                        $grammar->symbol_name($lexeme_id),
                        "\n",
                        qq{    Semantics were specified as "$semantics"\n}
                    );
                } ## end if ( not $allowed_semantics->{$semantics} )

            } ## end CHECK_SEMANTICS:
            CHECK_BLESSING: {
                if ( not $blessing ) {
                    $blessing = '::undef';
                    last CHECK_BLESSING;
                }
                last CHECK_BLESSING if $blessing eq '::undef';
                last CHECK_BLESSING
                    if $blessing =~ /\A [[:alpha:]] [:\w]* \z /xms;
                Marpa::R2::exception(
                    q{Unknown blessing for lexeme },
                    $grammar->symbol_name($lexeme_id),
                    "\n",
                    qq{    Blessing as specified as "$blessing"\n}
                );
            } ## end CHECK_BLESSING:
            $semantics_by_lexeme_id[$lexeme_id] = $semantics;
            $blessing_by_lexeme_id[$lexeme_id]  = $blessing;

        } ## end LEXEME: for my $lexeme_id ( 0 .. $#{$symbols} )

    }

    my $null_values = $recce->[Marpa::R2::Internal::Recognizer::NULL_VALUES];

    state $op_bless          = Marpa::R2::Thin::op('bless');
    state $op_callback       = Marpa::R2::Thin::op('callback');
    state $op_push_constant  = Marpa::R2::Thin::op('push_constant');
    state $op_push_g1_length = Marpa::R2::Thin::op('push_g1_length');
    state $op_push_g1_len    = Marpa::R2::Thin::op('push_g1_len');
    state $op_push_length    = Marpa::R2::Thin::op('push_length');
    state $op_push_undef     = Marpa::R2::Thin::op('push_undef');
    state $op_push_one       = Marpa::R2::Thin::op('push_one');
    state $op_push_sequence  = Marpa::R2::Thin::op('push_sequence');
    state $op_push_g1_start  = Marpa::R2::Thin::op('push_g1_start');
    state $op_push_start_location =
        Marpa::R2::Thin::op('push_start_location');
    state $op_push_values        = Marpa::R2::Thin::op('push_values');
    state $op_result_is_array    = Marpa::R2::Thin::op('result_is_array');
    state $op_result_is_constant = Marpa::R2::Thin::op('result_is_constant');
    state $op_result_is_n_of_sequence =
        Marpa::R2::Thin::op('result_is_n_of_sequence');
    state $op_result_is_rhs_n = Marpa::R2::Thin::op('result_is_rhs_n');
    state $op_result_is_token_value =
        Marpa::R2::Thin::op('result_is_token_value');
    state $op_result_is_undef = Marpa::R2::Thin::op('result_is_undef');

    my @nulling_symbol_by_semantic_rule;
    NULLING_SYMBOL: for my $nulling_symbol ( 0 .. $#{$null_values} ) {
        my $semantic_rule = $null_values->[$nulling_symbol];
        next NULLING_SYMBOL if not defined $semantic_rule;
        $nulling_symbol_by_semantic_rule[$semantic_rule] = $nulling_symbol;
    } ## end NULLING_SYMBOL: for my $nulling_symbol ( 0 .. $#{$null_values} )

    my @work_list = ();
    RULE: for my $rule_id ( $grammar->rule_ids() ) {

        my $semantics = $semantics_by_rule_id[$rule_id];
        my $blessing  = $blessing_by_rule_id[$rule_id];

        $semantics = '::undef'  if $semantics eq '::!default';
        $semantics = '[values]' if $semantics eq '::array';
        $semantics = '::undef'  if $semantics eq '::whatever';
        $semantics = '::rhs0'   if $semantics eq '::first';

        push @work_list, [ $rule_id, undef, $semantics, $blessing ];
    } ## end RULE: for my $rule_id ( $grammar->rule_ids() )

    RULE: for my $lexeme_id ( 0 .. $#{$symbols} ) {

        my $semantics = $semantics_by_lexeme_id[$lexeme_id];
        my $blessing  = $blessing_by_lexeme_id[$lexeme_id];

        $semantics = '::value' if $semantics eq '::!default';
        $semantics = '[value]' if $semantics eq '::array';

        push @work_list, [ undef, $lexeme_id, $semantics, $blessing ];
    } ## end RULE: for my $lexeme_id ( 0 .. $#{$symbols} )

    # Registering operations is postponed to this point, because
    # the valuator must exist for this to happen.  In the future,
    # it may be best to have a separate semantics object.
    my @nulling_closures = ();
    my @registrations    = ();
    my $top_nulling_ops;

    WORK_ITEM: for my $work_item (@work_list) {
        my ( $rule_id, $lexeme_id, $semantics, $blessing ) = @{$work_item};

        my ( $closure, $rule, $rule_length, $is_sequence_rule,
            $is_discard_sequence_rule, $nulling_symbol_id );
        if ( defined $rule_id ) {
            $nulling_symbol_id = $nulling_symbol_by_semantic_rule[$rule_id];
            $closure          = $closure_by_rule_id[$rule_id];
            $rule             = $rules->[$rule_id];
            $rule_length      = $grammar_c->rule_length($rule_id);
            $is_sequence_rule = defined $grammar_c->sequence_min($rule_id);
            $is_discard_sequence_rule = $is_sequence_rule
                && $rule->[Marpa::R2::Internal::Rule::DISCARD_SEPARATION];
        } ## end if ( defined $rule_id )

        # Determine the "fate" of the array of child values
        my $array_fate;
        ARRAY_FATE: {
            if ( defined $closure and ref $closure eq 'CODE' ) {
                $array_fate = $op_callback;
                last ARRAY_FATE;

            }

            if ( ( substr $semantics, 0, 1 ) eq '[' ) {
                $array_fate = $op_result_is_array;
                last ARRAY_FATE;
            }
        } ## end ARRAY_FATE:

        my @ops = ();

        SET_OPS: {

            if ( $semantics eq '::undef' ) {
                @ops = ($op_result_is_undef);
                last SET_OPS;
            }

            DO_CONSTANT: {
                last DO_CONSTANT if not defined $rule_id;
                my $thingy_ref = $closure_by_rule_id[$rule_id];
                last DO_CONSTANT if not defined $thingy_ref;
                my $ref_type = Scalar::Util::reftype $thingy_ref;
                if ( $ref_type eq q{} ) {
                    my $rule_desc = rule_describe( $grammar, $rule_id );
                    Marpa::R2::exception(
                        qq{An action resolved to a scalar.\n},
                        qq{  This is not allowed.\n},
                        qq{  A constant action must be a reference.\n},
                        qq{  Rule was $rule_desc\n}
                    );
                } ## end if ( $ref_type eq q{} )

                if ( $ref_type eq 'CODE' ) {

                    # Set the nulling closure if this is the nulling symbol of a rule
                    $nulling_closures[$nulling_symbol_id] = $thingy_ref
                        if defined $nulling_symbol_id
                        and defined $rule_id;
                    last DO_CONSTANT;
                } ## end if ( $ref_type eq 'CODE' )
                if ( $ref_type eq 'SCALAR' ) {
                    my $thingy = ${$thingy_ref};
                    if ( not defined $thingy ) {
                        @ops = ($op_result_is_undef);
                        last SET_OPS;
                    }
                    @ops = ( $op_result_is_constant, $thingy_ref );
                    last SET_OPS;
                } ## end if ( $ref_type eq 'SCALAR' )

                # No test for 'ARRAY' or 'HASH' --
                # The ref is currenly only to scalar and code slots in the symbol table,
                # and therefore cannot be to (among other things) an ARRAY or HASH

                if ( $ref_type eq 'REF' ) {
                    @ops = ( $op_result_is_constant, $thingy_ref );
                    last SET_OPS;
                }

                my $rule_desc = rule_describe( $grammar, $rule_id );
                Marpa::R2::exception(
                    qq{Constant action is not of an allowed type.\n},
                    qq{  It was of type reference to $ref_type.\n},
                    qq{  Rule was $rule_desc\n}
                );
            } ## end DO_CONSTANT:

            # After this point, any closure will be a ref to 'CODE'

            if ( defined $lexeme_id and $semantics eq '::value' ) {
                @ops = ($op_result_is_token_value);
                last SET_OPS;
            }

            PROCESS_SINGLETON_RESULT: {
                last PROCESS_SINGLETON_RESULT if not defined $rule_id;

                my $singleton;
                if ( $semantics =~ m/\A [:][:] rhs (\d+)  \z/xms ) {
                    $singleton = $1 + 0;
                }

                last PROCESS_SINGLETON_RESULT if not defined $singleton;

                my $singleton_element = $singleton;
                if ($is_discard_sequence_rule) {
                    @ops =
                        ( $op_result_is_n_of_sequence, $singleton_element );
                    last SET_OPS;
                }
                if ($is_sequence_rule) {
                    @ops = ( $op_result_is_rhs_n, $singleton_element );
                    last SET_OPS;
                }
                my $mask = $rule->[Marpa::R2::Internal::Rule::MASK];
                my @elements =
                    grep { $mask->[$_] } 0 .. ( $rule_length - 1 );
                if ( not scalar @elements ) {
                    my $original_semantics = $semantics_by_rule_id[$rule_id];
                    Marpa::R2::exception(
                        q{Impossible semantics for empty rule: },
                        $grammar->brief_rule($rule_id),
                        "\n",
                        qq{    Semantics were specified as "$original_semantics"\n}
                    );
                } ## end if ( not scalar @elements )
                $singleton_element = $elements[$singleton];

                if ( not defined $singleton_element ) {
                    my $original_semantics = $semantics_by_rule_id[$rule_id];
                    Marpa::R2::exception(
                        q{Impossible semantics for rule: },
                        $grammar->brief_rule($rule_id),
                        "\n",
                        qq{    Semantics were specified as "$original_semantics"\n}
                    );
                } ## end if ( not defined $singleton_element )
                @ops = ( $op_result_is_rhs_n, $singleton_element );
                last SET_OPS;
            } ## end PROCESS_SINGLETON_RESULT:

            if ( not defined $array_fate ) {
                @ops = ($op_result_is_undef);
                last SET_OPS;
            }

            # if here, $array_fate is defined

            my @bless_ops = ();
            if ( $blessing ne '::undef' ) {
                push @bless_ops, $op_bless, \$blessing;
            }

            Marpa::R2::exception(qq{Unknown semantics: "$semantics"})
                if ( substr $semantics, 0, 1 ) ne '[';

            my @push_ops = ();
            my $array_descriptor = substr $semantics, 1, -1;
            $array_descriptor =~ s/^\s*|\s*$//g;
            RESULT_DESCRIPTOR:
            for my $result_descriptor ( split /[,]\s*/xms, $array_descriptor )
            {
                $result_descriptor =~ s/^\s*|\s*$//g;
                if ( $result_descriptor eq 'g1start' ) {
                    push @push_ops, $op_push_g1_start;
                    next RESULT_DESCRIPTOR;
                }
                if ( $result_descriptor eq 'g1len' ) {
                    push @push_ops, $op_push_g1_len;
                    next RESULT_DESCRIPTOR;
                }
                if ( $result_descriptor eq 'g1length' ) {
                    push @push_ops, $op_push_g1_length;
                    next RESULT_DESCRIPTOR;
                }
                if ( $result_descriptor eq 'start' ) {
                    push @push_ops, $op_push_start_location;
                    next RESULT_DESCRIPTOR;
                }
                if ( $result_descriptor eq 'length' ) {
                    push @push_ops, $op_push_length;
                    next RESULT_DESCRIPTOR;
                }

                if ( $result_descriptor eq 'lhs' ) {
                    if ( defined $rule_id ) {
                        my $lhs_id = $grammar_c->rule_lhs($rule_id);
                        push @push_ops, $op_push_constant, \$lhs_id;
                        next RESULT_DESCRIPTOR;
                    }
                    if ( defined $lexeme_id ) {
                        push @push_ops, $op_push_constant, \$lexeme_id;
                        next RESULT_DESCRIPTOR;
                    }
                    push @push_ops, $op_push_undef;
                    next RESULT_DESCRIPTOR;
                } ## end if ( $result_descriptor eq 'lhs' )

                if ( $result_descriptor eq 'name' ) {
                    if ( defined $rule_id ) {
                        my $name = $grammar->rule_name($rule_id);
                        push @push_ops, $op_push_constant, \$name;
                        next RESULT_DESCRIPTOR;
                    }
                    if ( defined $lexeme_id ) {
                        my $name = $tracer->symbol_name($lexeme_id);
                        push @push_ops, $op_push_constant, \$name;
                        next RESULT_DESCRIPTOR;
                    }
                    if ( defined $nulling_symbol_id ) {
                        my $name = $tracer->symbol_name($nulling_symbol_id);
                        push @push_ops, $op_push_constant, \$name;
                        next RESULT_DESCRIPTOR;
                    }
                    push @push_ops, $op_push_undef;
                    next RESULT_DESCRIPTOR;
                } ## end if ( $result_descriptor eq 'name' )

                if ( $result_descriptor eq 'symbol' ) {
                    if ( defined $rule_id ) {
                        my $lhs_id = $grammar_c->rule_lhs($rule_id);
                        my $name   = $tracer->symbol_name($lhs_id);
                        push @push_ops, $op_push_constant, \$name;
                        next RESULT_DESCRIPTOR;
                    } ## end if ( defined $rule_id )
                    if ( defined $lexeme_id ) {
                        my $name = $tracer->symbol_name($lexeme_id);
                        push @push_ops, $op_push_constant, \$name;
                        next RESULT_DESCRIPTOR;
                    }
                    if ( defined $nulling_symbol_id ) {
                        my $name = $tracer->symbol_name($nulling_symbol_id);
                        push @push_ops, $op_push_constant, \$name;
                        next RESULT_DESCRIPTOR;
                    }
                    push @push_ops, $op_push_undef;
                    next RESULT_DESCRIPTOR;
                } ## end if ( $result_descriptor eq 'symbol' )

                if ( $result_descriptor eq 'rule' ) {
                    if ( defined $rule_id ) {
                        push @push_ops, $op_push_constant, \$rule_id;
                        next RESULT_DESCRIPTOR;
                    }
                    push @push_ops, $op_push_undef;
                    next RESULT_DESCRIPTOR;
                } ## end if ( $result_descriptor eq 'rule' )
                if (   $result_descriptor eq 'values'
                    or $result_descriptor eq 'value' )
                {
                    if ( defined $lexeme_id ) {
                        push @push_ops, $op_push_values;
                        next RESULT_DESCRIPTOR;
                    }
                    if ($is_sequence_rule) {
                        my $push_op =
                              $is_discard_sequence_rule
                            ? $op_push_sequence
                            : $op_push_values;
                        push @push_ops, $push_op;
                        next RESULT_DESCRIPTOR;
                    } ## end if ($is_sequence_rule)
                    my $mask = $rule->[Marpa::R2::Internal::Rule::MASK];
                    if ( $rule_length > 0 ) {
                        push @push_ops,
                            map { $mask->[$_] ? ( $op_push_one, $_ ) : () }
                            0 .. $rule_length - 1;
                    }
                    next RESULT_DESCRIPTOR;
                } ## end if ( $result_descriptor eq 'values' or ...)
                Marpa::R2::exception(
                    qq{Unknown result descriptor: "$result_descriptor"\n},
                    qq{  The full semantics were "$semantics"}
                );
            } ## end RESULT_DESCRIPTOR: for my $result_descriptor ( split /[,]\s*/xms, ...)
            @ops = ( @push_ops, @bless_ops, $array_fate );

        } ## end SET_OPS:

        if ( defined $rule_id ) {
            push @registrations, [ 'rule', $rule_id, @ops ];
        }

        if ( defined $nulling_symbol_id ) {

            push @registrations, [ 'nulling', $nulling_symbol_id, @ops ];
        } ## end if ( defined $nulling_symbol_id )

        if ( defined $lexeme_id ) {
            push @registrations, [ 'token', $lexeme_id, @ops ];
        }

    } ## end WORK_ITEM: for my $work_item (@work_list)

    SLR_NULLING_GRAMMAR_HACK: {
        last SLR_NULLING_GRAMMAR_HACK if not $Marpa::R2::Context::slr;

        # A hack for nulling SLR grammars --
        # the nulling semantics of the start symbol should
        # be those of the symbol on the
        # RHS of the start rule --
        # so copy them.

        my $start_symbol_id = $tracer->symbol_by_name('[:start]');
        last SLR_NULLING_GRAMMAR_HACK
            if not $grammar_c->symbol_is_nullable($start_symbol_id);

        my $start_rhs_symbol_id;
        RULE: for my $rule_id ( $grammar->rule_ids() ) {
            my ( $lhs, $rhs0 ) = $tracer->rule_expand($rule_id);
            if ( $start_symbol_id == $lhs ) {
                $start_rhs_symbol_id = $rhs0;
                last RULE;
            }
        } ## end RULE: for my $rule_id ( $grammar->rule_ids() )

        REGISTRATION: for my $registration (@registrations) {
            my ( $type, $nulling_symbol_id ) = @{$registration};
            if ( $nulling_symbol_id == $start_rhs_symbol_id ) {
                my ( undef, undef, @ops ) = @{$registration};
                push @registrations, [ 'nulling', $start_symbol_id, @ops ];
                $nulling_closures[$start_symbol_id] =
                    $nulling_closures[$start_rhs_symbol_id];
                last REGISTRATION;
            } ## end if ( $nulling_symbol_id == $start_rhs_symbol_id )
        } ## end REGISTRATION: for my $registration (@registrations)
    } ## end SLR_NULLING_GRAMMAR_HACK:

    $recce->[Marpa::R2::Internal::Recognizer::REGISTRATIONS] =
        \@registrations;
    $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_SYMBOL_ID] =
        \@nulling_closures;
    $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_RULE_ID] =
        \@closure_by_rule_id;

} ## end sub registration_init

# Returns false if no parse
sub Marpa::R2::Recognizer::value {
    my ( $recce, $slr, $per_parse_arg ) = @_;
    my $grammar   = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
    my $recce_c   = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $tracer    = $grammar->[Marpa::R2::Internal::Grammar::TRACER];

    my $trace_actions =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_ACTIONS] // 0;
    my $trace_values =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_VALUES] // 0;
    my $trace_file_handle =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_FILE_HANDLE];
    local $Marpa::R2::Internal::TRACE_FH = $trace_file_handle;

    my $rules   = $grammar->[Marpa::R2::Internal::Grammar::RULES];
    my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];

    if ( scalar @_ != 1 ) {
        Marpa::R2::exception(
            'Too many arguments to Marpa::R2::Recognizer::value')
            if ref $slr ne 'Marpa::R2::Scanless::R';
    }

    $recce->[Marpa::R2::Internal::Recognizer::TREE_MODE] //= 'tree';
    if ( $recce->[Marpa::R2::Internal::Recognizer::TREE_MODE] ne 'tree' ) {
        Marpa::R2::exception(
            "value() called when recognizer is not in tree mode\n",
            '  The current mode is "',
            $recce->[Marpa::R2::Internal::Recognizer::TREE_MODE],
            qq{"\n}
        );
    } ## end if ( $recce->[Marpa::R2::Internal::Recognizer::TREE_MODE...])

    my $furthest_earleme       = $recce_c->furthest_earleme();
    my $last_completed_earleme = $recce_c->current_earleme();
    Marpa::R2::exception(
        "Attempt to evaluate incompletely recognized parse:\n",
        "  Last token ends at location $furthest_earleme\n",
        "  Recognition done only as far as location $last_completed_earleme\n"
    ) if $furthest_earleme > $last_completed_earleme;

    my $tree = $recce->[Marpa::R2::Internal::Recognizer::T_C];

    if ($tree) {

        # On second and later calls to value() in a parse series, we need
        # to check the per-parse arg
        CHECK_ARG: {
            my $package_source = $recce
                ->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE_SOURCE];
            last CHECK_ARG
                if $package_source eq 'semantics_package';    # Anything is OK
            if ( $package_source eq 'legacy' ) {
                if ( defined $per_parse_arg ) {
                    Marpa::R2::exception(
                        "value() called with an argument while incompatible options are in use.\n",
                        "  Often this means that the discouraged 'action_object' named argument was used,\n",
                        "  and that 'semantics_package' should be used instead.\n"
                    );
                } ## end if ( defined $per_parse_arg )
                last CHECK_ARG;
            } ## end if ( $package_source eq 'legacy' )

            # If here the resolve package source is 'arg'
            if ( not defined $per_parse_arg ) {
                Marpa::R2::exception(
                    "No value() arg, when one is required to resolve semantics.\n",
                    "  Once value() has been called with a argument whose blessing is used to\n",
                    "  find the parse's semantics closures, it must always be called with an arg\n",
                    "  that is blessed in the same package\n",
                    q{  In this case, the package was "},
                    $recce
                        ->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE],
                    qq{"\n"}
                );
            } ## end if ( not defined $per_parse_arg )

            my $arg_blessing = Scalar::Util::blessed $per_parse_arg;
            if ( not defined $arg_blessing ) {
                Marpa::R2::exception(
                    "value() arg is not blessed when required for the semantics.\n",
                    "  Once value() has been called with a argument whose blessing is used to\n",
                    "  find the parse's semantics closures, it must always be called with an arg\n",
                    "  that is blessed in the same package\n",
                    q{  In this case, the original package was "},
                    $recce
                        ->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE],
                    qq{"\n"},
                    qq{  and the blessing in this call was "$arg_blessing"\n}
                );
            } ## end if ( not defined $arg_blessing )

            my $required_blessing =
                $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE];
            if ( $arg_blessing ne $required_blessing ) {
                Marpa::R2::exception(
                    "value() arg is blessed into the wrong package.\n",
                    "  Once value() has been called with a argument whose blessing is used to\n",
                    "  find the parse's semantics closures, it must always be called with an arg\n",
                    "  that is blessed in the same package\n",
                    qq{  In this case, the original package was "$required_blessing" and \n},
                    qq{  and the blessing in this call was "$arg_blessing"\n}
                );
            } ## end if ( $arg_blessing ne $required_blessing )

        } ## end CHECK_ARG:

        # If we have a bocage, we are initialized
        if ( not $tree ) {

            # No tree means we are in ASF mode
            Marpa::R2::exception('value() called for recognizer in ASF mode');
        }
        my $max_parses =
            $recce->[Marpa::R2::Internal::Recognizer::MAX_PARSES];
        my $parse_count = $tree->parse_count();
        if ( $max_parses and $parse_count > $max_parses ) {
            Marpa::R2::exception(
                "Maximum parse count ($max_parses) exceeded");
        }

    } ## end if ($tree)
    else {
        # No tree, therefore not initialized

        my $order = $recce->ordering_get();
        return if not $order;
        $tree = $recce->[Marpa::R2::Internal::Recognizer::T_C] =
            Marpa::R2::Thin::T->new($order);

    } ## end else [ if ($tree) ]

    if ( $recce->[Marpa::R2::Internal::Recognizer::TRACE_AND_NODES] ) {
        print {$trace_file_handle} 'AND_NODES: ', $recce->show_and_nodes()
            or Marpa::R2::exception('print to trace handle failed');
    }

    if ( $recce->[Marpa::R2::Internal::Recognizer::TRACE_OR_NODES] ) {
        print {$trace_file_handle} 'OR_NODES: ', $recce->show_or_nodes()
            or Marpa::R2::exception('print to trace handle failed');
    }

    if ( $recce->[Marpa::R2::Internal::Recognizer::TRACE_BOCAGE] ) {
        print {$trace_file_handle} 'BOCAGE: ', $recce->show_bocage()
            or Marpa::R2::exception('print to trace handle failed');
    }

    return if not defined $tree->next();

    local $Marpa::R2::Context::grammar = $grammar;
    local $Marpa::R2::Context::rule    = undef;
    local $Marpa::R2::Context::slr     = $slr;
    local $Marpa::R2::Context::slg =
        $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]
        if defined $slr;

    if ( not $recce->[Marpa::R2::Internal::Recognizer::REGISTRATIONS] ) {
        registration_init( $recce, $per_parse_arg );
    }

    my $semantics_arg0;
    RUN_CONSTRUCTOR: {
        # Do not run the constructor if there is a per-parse arg
        last RUN_CONSTRUCTOR if defined $per_parse_arg;

        my $per_parse_constructor =
            $recce->[Marpa::R2::Internal::Recognizer::PER_PARSE_CONSTRUCTOR];

        # Do not run the constructor if there isn't one
        last RUN_CONSTRUCTOR if not defined $per_parse_constructor;

        my $constructor_arg0;
        if ( $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE_SOURCE]
            eq 'legacy' )
        {
            $constructor_arg0 =
                $grammar->[Marpa::R2::Internal::Grammar::ACTION_OBJECT];
        } ## end if ( $recce->[...])
        else {
            $constructor_arg0 =
                $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE];
        }
        my @warnings;
        my $eval_ok;
        my $fatal_error;
        DO_EVAL: {
            local $EVAL_ERROR = undef;
            local $SIG{__WARN__} = sub {
                push @warnings, [ $_[0], ( caller 0 ) ];
            };

            $eval_ok = eval {
                $semantics_arg0 = $per_parse_constructor->($constructor_arg0);
                1;
            };
            $fatal_error = $EVAL_ERROR;
        } ## end DO_EVAL:

        if ( not $eval_ok or @warnings ) {
            code_problems(
                {   fatal_error => $fatal_error,
                    grammar     => $grammar,
                    eval_ok     => $eval_ok,
                    warnings    => \@warnings,
                    where       => 'constructing action object',
                }
            );
        } ## end if ( not $eval_ok or @warnings )
    } ## end RUN_CONSTRUCTOR:

    $semantics_arg0 //= $per_parse_arg // {};

    my $value = Marpa::R2::Thin::V->new($tree);
    if ($slr) {
        $value->slr_set( $slr->thin() );
    }
    else {
        my $token_values =
            $recce->[Marpa::R2::Internal::Recognizer::TOKEN_VALUES];
        $value->valued_force();
        TOKEN_IX:
        for ( my $token_ix = 2; $token_ix <= $#{$token_values}; $token_ix++ )
        {
            my $token_value = $token_values->[$token_ix];
            $value->token_value_set( $token_ix, $token_value )
                if defined $token_value;
        } ## end TOKEN_IX: for ( my $token_ix = 2; $token_ix <= $#{...})
    } ## end else [ if ($slr) ]
    local $Marpa::R2::Internal::Context::VALUATOR = $value;
    value_trace( $value, $trace_values ? 1 : 0 );
    $value->trace_values($trace_values);
    $value->stack_mode_set();

    my $null_values = $recce->[Marpa::R2::Internal::Recognizer::NULL_VALUES];
    my $nulling_closures =
        $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_SYMBOL_ID];
    my $rule_closures =
        $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_RULE_ID];
    REGISTRATION:
    for my $registration (
        @{ $recce->[Marpa::R2::Internal::Recognizer::REGISTRATIONS] } )
    {
        my ( $type, $id, @raw_ops ) = @{$registration};
        my @ops = ();
        PRINT_TRACES: {
            last PRINT_TRACES if $trace_values <= 2;
            if ( $type eq 'nulling' ) {
                say {$trace_file_handle}
                    "Registering semantics for nulling symbol: ",
                    $grammar->symbol_name($id),
                    "\n", '  Semantics are ', show_semantics(@raw_ops)
                    or
                    Marpa::R2::exception('Cannot say to trace file handle');
                last PRINT_TRACES;
            } ## end if ( $type eq 'nulling' )
            say {$trace_file_handle}
                "Registering semantics for $type: ",
                $grammar->symbol_name($id),
                "\n", '  Semantics are ', show_semantics(@raw_ops)
                or Marpa::R2::exception('Cannot say to trace file handle');
        } ## end PRINT_TRACES:

        OP: for my $raw_op (@raw_ops) {
            if ( ref $raw_op ) {
                push @ops, $value->constant_register( ${$raw_op} );
                next OP;
            }
            push @ops, $raw_op;
        } ## end OP: for my $raw_op (@raw_ops)
        if ( $type eq 'token' ) {
            $value->token_register( $id, @ops );
            next REGISTRATION;
        }
        if ( $type eq 'nulling' ) {
            $value->nulling_symbol_register( $id, @ops );
            next REGISTRATION;
        }
        if ( $type eq 'rule' ) {
            $value->rule_register( $id, @ops );
            next REGISTRATION;
        }
        Marpa::R2::exception(
            'Registration: with unknown type: ',
            Data::Dumper::Dumper($registration)
        );
    } ## end REGISTRATION: for my $registration ( @{ $recce->[...]})

    STEP: while (1) {
        my ( $value_type, @value_data ) = $value->stack_step();

        if ($trace_values) {
            EVENT: while (1) {
                my $event = $value->event();
                last EVENT if not defined $event;
                my ( $event_type, @event_data ) = @{$event};
                if ( $event_type eq 'MARPA_STEP_TOKEN' ) {
                    my ( $token_id, $token_value_ix, $token_value ) = @event_data;
                    trace_token_evaluation( $recce, $value, $token_id,
                        $token_value );
                    next EVENT;
                } ## end if ( $event_type eq 'MARPA_STEP_TOKEN' )
                say {$trace_file_handle} join q{ },
                    'value event:',
                    map { $_ // 'undef' } $event_type, @event_data
                    or Marpa::R2::exception('say to trace handle failed');
            } ## end EVENT: while (1)

            if ( $trace_values >= 9 ) {
                for my $i ( reverse 0 .. $value->highest_index ) {
                    printf {$trace_file_handle} "Stack position %3d:\n", $i,
                        or
                        Marpa::R2::exception('print to trace handle failed');
                    print {$trace_file_handle} q{ },
                        Data::Dumper->new( [ \$value->absolute($i) ] )
                        ->Terse(1)->Dump
                        or
                        Marpa::R2::exception('print to trace handle failed');
                } ## end for my $i ( reverse 0 .. $value->highest_index )
            } ## end if ( $trace_values >= 9 )

        } ## end if ($trace_values)

        last STEP if not defined $value_type;
        next STEP if $value_type eq 'trace';

        if ( $value_type eq 'MARPA_STEP_NULLING_SYMBOL' ) {
            my ($token_id) = @value_data;
            my $value_ref = $nulling_closures->[$token_id];
            my $result;

            my @warnings;
            my $eval_ok;

            DO_EVAL: {
                local $SIG{__WARN__} = sub {
                    push @warnings, [ $_[0], ( caller 0 ) ];
                };

                $eval_ok = eval {
                    local $Marpa::R2::Context::rule =
                        $null_values->[$token_id];
                    $result = $value_ref->($semantics_arg0);
                    1;
                };

            } ## end DO_EVAL:

            if ( not $eval_ok or @warnings ) {
                my $fatal_error = $EVAL_ERROR;
                code_problems(
                    {   fatal_error => $fatal_error,
                        grammar     => $grammar,
                        eval_ok     => $eval_ok,
                        warnings    => \@warnings,
                        where       => 'computing value',
                        long_where  => 'Computing value for null symbol: '
                            . $grammar->symbol_name($token_id),
                    }
                );
            } ## end if ( not $eval_ok or @warnings )

            $value->result_set($result);
            trace_token_evaluation( $recce, $value, $token_id, \$result )
                if $trace_values;
            next STEP;
        } ## end if ( $value_type eq 'MARPA_STEP_NULLING_SYMBOL' )

        if ( $value_type eq 'MARPA_STEP_RULE' ) {
            my ( $rule_id, $values ) = @value_data;
            my $closure = $rule_closures->[$rule_id];

            next STEP if not defined $closure;
            my $result;

            if ( ref $closure eq 'CODE' ) {
                my @warnings;
                my $eval_ok;
                DO_EVAL: {
                    local $SIG{__WARN__} = sub {
                        push @warnings, [ $_[0], ( caller 0 ) ];
                    };
                    local $Marpa::R2::Context::rule = $rule_id;

                    if ( Scalar::Util::blessed($values) ) {
                        $eval_ok = eval {
                            $result = $closure->( $semantics_arg0, $values );
                            1;
                        };
                        last DO_EVAL;
                    } ## end if ( Scalar::Util::blessed($values) )
                    $eval_ok = eval {
                        $result = $closure->( $semantics_arg0, @{$values} );
                        1;
                    };

                } ## end DO_EVAL:

                if ( not $eval_ok or @warnings ) {
                    my $fatal_error = $EVAL_ERROR;
                    code_problems(
                        {   fatal_error => $fatal_error,
                            grammar     => $grammar,
                            eval_ok     => $eval_ok,
                            warnings    => \@warnings,
                            where       => 'computing value',
                            long_where  => 'Computing value for rule: '
                                . $grammar->brief_rule($rule_id),
                        }
                    );
                } ## end if ( not $eval_ok or @warnings )
            } ## end if ( ref $closure eq 'CODE' )
            else {
                $result = ${$closure};
            }
            $value->result_set($result);

            if ($trace_values) {
                say {$trace_file_handle}
                    trace_stack_1( $grammar, $recce, $value, $values,
                    $rule_id )
                    or Marpa::R2::exception('Could not print to trace file');
                print {$trace_file_handle}
                    'Calculated and pushed value: ',
                    Data::Dumper->new( [$result] )->Terse(1)->Dump
                    or Marpa::R2::exception('print to trace handle failed');
            } ## end if ($trace_values)

            next STEP;

        } ## end if ( $value_type eq 'MARPA_STEP_RULE' )

        if ( $value_type eq 'MARPA_STEP_TRACE' ) {

            if ( my $trace_output = trace_op( $grammar, $recce, $value ) ) {
                print {$trace_file_handle} $trace_output
                    or Marpa::R2::exception('Could not print to trace file');
            }

            next STEP;

        } ## end if ( $value_type eq 'MARPA_STEP_TRACE' )

        die "Internal error: Unknown value type $value_type";

    } ## end STEP: while (1)

    return \( $value->absolute(0) );

} ## end sub Marpa::R2::Recognizer::value

sub do_high_rule_only {
    my ($recce) = @_;
    my $order = $recce->[Marpa::R2::Internal::Recognizer::O_C];
    $order->high_rank_only_set(1);
    $order->rank();
    return 1;
} ## end sub do_high_rule_only

sub do_rank_by_rule {
    my ($recce) = @_;
    my $order = $recce->[Marpa::R2::Internal::Recognizer::O_C];

    # Rank by rule is the default, but just in case
    $order->high_rank_only_set(0);
    $order->rank();
    return 1;
} ## end sub do_rank_by_rule

# INTERNAL OK AFTER HERE _marpa_

sub Marpa::R2::Recognizer::show_bocage {
    my ($recce, $verbose)   = @_;
    $verbose //= 0;
    my @data      = ();
    my $id        = 0;
    my $recce_c   = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $bocage    = $recce->[Marpa::R2::Internal::Recognizer::B_C];
    my $grammar   = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
    OR_NODE: for ( my $or_node_id = 0;; $or_node_id++ ) {
        my $irl_id = $bocage->_marpa_b_or_node_irl($or_node_id);
        last OR_NODE if not defined $irl_id;
        my $position        = $bocage->_marpa_b_or_node_position($or_node_id);
        my $or_origin       = $bocage->_marpa_b_or_node_origin($or_node_id);
        my $origin_earleme  = $recce_c->earleme($or_origin);
        my $or_set          = $bocage->_marpa_b_or_node_set($or_node_id);
        my $current_earleme = $recce_c->earleme($or_set);
        my @and_node_ids =
            ( $bocage->_marpa_b_or_node_first_and($or_node_id)
                .. $bocage->_marpa_b_or_node_last_and($or_node_id) );
        AND_NODE:

        for my $and_node_id (@and_node_ids) {
            my $symbol = $bocage->_marpa_b_and_node_symbol($and_node_id);
            my $cause_tag;

            if ( defined $symbol ) {
                $cause_tag = "S$symbol";
            }
            my $cause_id = $bocage->_marpa_b_and_node_cause($and_node_id);
            if ( defined $cause_id ) {
                $cause_tag = "OR#$cause_id=" .
                    Marpa::R2::Recognizer::or_node_tag( $recce, $cause_id );
            }
            my $parent_tag =
                Marpa::R2::Recognizer::or_node_tag( $recce, $or_node_id );
            my $predecessor_id =
                $bocage->_marpa_b_and_node_predecessor($and_node_id);
            my $predecessor_tag = q{-};
            if ( defined $predecessor_id ) {
                $predecessor_tag =
                    $predecessor_tag = "OR#$predecessor_id=" .
                    Marpa::R2::Recognizer::or_node_tag( $recce, $predecessor_id );
            }
                 
            my $tag = join q{ }, "AND#$and_node_id:", "parent=OR#$or_node_id=$parent_tag",
                "pred=$predecessor_tag", "cause=$cause_tag";

            push @data, [ $and_node_id, $tag ];
        } ## end AND_NODE: for my $and_node_id (@and_node_ids)
    } ## end OR_NODE: for ( my $or_node_id = 0;; $or_node_id++ )
    my @sorted_data = map { $_->[-1] } sort { $a->[0] <=> $b->[0] } @data;
    return ( join "\n", @sorted_data ) . "\n";
} ## end sub Marpa::R2::Recognizer::show_bocage

sub Marpa::R2::Recognizer::and_node_tag {
    my ( $recce, $and_node_id ) = @_;
    my $bocage            = $recce->[Marpa::R2::Internal::Recognizer::B_C];
    my $recce_c           = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $parent_or_node_id = $bocage->_marpa_b_and_node_parent($and_node_id);
    my $origin         = $bocage->_marpa_b_or_node_origin($parent_or_node_id);
    my $origin_earleme = $recce_c->earleme($origin);
    my $current_earley_set =
        $bocage->_marpa_b_or_node_set($parent_or_node_id);
    my $current_earleme = $recce_c->earleme($current_earley_set);
    my $cause_id        = $bocage->_marpa_b_and_node_cause($and_node_id);
    my $predecessor_id = $bocage->_marpa_b_and_node_predecessor($and_node_id);

    my $middle_earley_set = $bocage->_marpa_b_and_node_middle($and_node_id);
    my $middle_earleme    = $recce_c->earleme($middle_earley_set);

    my $position = $bocage->_marpa_b_or_node_position($parent_or_node_id);
    my $irl_id   = $bocage->_marpa_b_or_node_irl($parent_or_node_id);

#<<<  perltidy introduces trailing space on this
    my $tag =
          'R'
        . $irl_id . q{:}
        . $position . q{@}
        . $origin_earleme . q{-}
        . $current_earleme;
#>>>
    if ( defined $cause_id ) {
        my $cause_irl_id = $bocage->_marpa_b_or_node_irl($cause_id);
        $tag .= 'C' . $cause_irl_id;
    }
    else {
        my $symbol = $bocage->_marpa_b_and_node_symbol($and_node_id);
        $tag .= 'S' . $symbol;
    }
    $tag .= q{@} . $middle_earleme;
    return $tag;
} ## end sub Marpa::R2::Recognizer::and_node_tag

sub Marpa::R2::Recognizer::show_and_nodes {
    my ($recce, $verbose) = @_;
    $verbose //= 0;
    my $recce_c = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $bocage  = $recce->[Marpa::R2::Internal::Recognizer::B_C];
    my $text;
    my @data = ();
    AND_NODE: for ( my $id = 0;; $id++ ) {
        my $parent      = $bocage->_marpa_b_and_node_parent($id);
        my $predecessor = $bocage->_marpa_b_and_node_predecessor($id);
        my $cause       = $bocage->_marpa_b_and_node_cause($id);
        my $symbol      = $bocage->_marpa_b_and_node_symbol($id);
        last AND_NODE if not defined $parent;
        my $origin            = $bocage->_marpa_b_or_node_origin($parent);
        my $set               = $bocage->_marpa_b_or_node_set($parent);
        my $irl_id            = $bocage->_marpa_b_or_node_irl($parent);
        my $position          = $bocage->_marpa_b_or_node_position($parent);
        my $origin_earleme    = $recce_c->earleme($origin);
        my $current_earleme   = $recce_c->earleme($set);
        my $middle_earley_set = $bocage->_marpa_b_and_node_middle($id);
        my $middle_earleme    = $recce_c->earleme($middle_earley_set);

#<<<  perltidy introduces trailing space on this
        my $desc =
              "And-node #$id: R"
            . $irl_id . q{:}
            . $position . q{@}
            . $origin_earleme . q{-}
            . $current_earleme;
#>>>
        my $cause_rule = -1;
        if ( defined $cause ) {
            my $cause_irl_id = $bocage->_marpa_b_or_node_irl($cause);
            $desc .= 'C' . $cause_irl_id;
        }
        else {
            $desc .= 'S' . $symbol;
        }
        $desc .= q{@} . $middle_earleme;
        if ($verbose) {
            $desc .= " parent=" . (defined $parent ?  "OR#$parent" : '-');
            $desc .= " pred=" . (defined $predecessor ?  "OR#$predecessor" : '-');
            $desc .= " cause=" . (defined $cause ?  "OR#$cause" : '-');
        }
        push @data, $desc;
    } ## end AND_NODE: for ( my $id = 0;; $id++ )
    return ( join "\n", @data ) . "\n";
} ## end sub Marpa::R2::Recognizer::show_and_nodes

sub Marpa::R2::Recognizer::or_node_tag {
    my ( $recce, $or_node_id ) = @_;
    my $bocage   = $recce->[Marpa::R2::Internal::Recognizer::B_C];
    my $set      = $bocage->_marpa_b_or_node_set($or_node_id);
    my $irl_id   = $bocage->_marpa_b_or_node_irl($or_node_id);
    my $origin   = $bocage->_marpa_b_or_node_origin($or_node_id);
    my $position = $bocage->_marpa_b_or_node_position($or_node_id);
    return 'R' . $irl_id . q{:} . $position . q{@} . $origin . q{-} . $set;
} ## end sub Marpa::R2::Recognizer::or_node_tag

sub Marpa::R2::Recognizer::show_or_nodes {
    my ( $recce, $verbose ) = @_;
    my $recce_c = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $bocage  = $recce->[Marpa::R2::Internal::Recognizer::B_C];
    my $text;
    my @data = ();
    my $id   = 0;
    OR_NODE: for ( ;; ) {
        my $origin   = $bocage->_marpa_b_or_node_origin($id);
        my $set      = $bocage->_marpa_b_or_node_set($id);
        my $irl_id   = $bocage->_marpa_b_or_node_irl($id);
        my $position = $bocage->_marpa_b_or_node_position($id);
        $id++;
        last OR_NODE if not defined $origin;
        my $origin_earleme  = $recce_c->earleme($origin);
        my $current_earleme = $recce_c->earleme($set);

#<<<  perltidy introduces trailing space on this
        my $desc =
              'R'
            . $irl_id . q{:}
            . $position . q{@}
            . $origin_earleme . q{-}
            . $current_earleme;
#>>>
        push @data,
            [ $origin_earleme, $current_earleme, $irl_id, $position, $desc ];
    } ## end OR_NODE: for ( ;; )
    my @sorted_data = map { $_->[-1] } sort {
               $a->[0] <=> $b->[0]
            or $a->[1] <=> $b->[1]
            or $a->[2] <=> $b->[2]
            or $a->[3] <=> $b->[3]
    } @data;
    return ( join "\n", @sorted_data ) . "\n";
} ## end sub Marpa::R2::Recognizer::show_or_nodes

# Not sorted and therefore not suitable for test suite
sub Marpa::R2::Recognizer::verbose_or_nodes {
    my ($recce) = @_;
    my $text = q{};
    OR_NODE:
    for (
        my $or_node_id = 0;
        defined( my $or_node_desc = $recce->verbose_or_node($or_node_id) );
        $or_node_id++
        )
    {
        $text .= $or_node_desc;
    } ## end OR_NODE: for ( my $or_node_id = 0; defined( my $or_node_desc =...))
    return $text;
} ## end sub Marpa::R2::Recognizer::verbose_or_nodes

sub Marpa::R2::Recognizer::verbose_or_node {
    my ( $recce, $or_node_id ) = @_;
    my $recce_c = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $bocage  = $recce->[Marpa::R2::Internal::Recognizer::B_C];
    my $origin  = $bocage->_marpa_b_or_node_origin($or_node_id);
    return if not defined $origin;
    my $grammar         = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];
    my $tracer          = $grammar->[Marpa::R2::Internal::Grammar::TRACER];
    my $set             = $bocage->_marpa_b_or_node_set($or_node_id);
    my $irl_id          = $bocage->_marpa_b_or_node_irl($or_node_id);
    my $position        = $bocage->_marpa_b_or_node_position($or_node_id);
    my $origin_earleme  = $recce_c->earleme($origin);
    my $current_earleme = $recce_c->earleme($set);
    my $text =
        "OR-node #$or_node_id: R$irl_id" . q{:}
      . $position . q{@}
      . $origin_earleme . q{-}
      . $current_earleme . "\n";
    $text .=
      ( q{ } x 4 ) . $tracer->show_dotted_irl( $irl_id, $position ) . "\n";

    for my $and_node_id ( $bocage->_marpa_b_or_node_first_and($or_node_id)
        .. $bocage->_marpa_b_or_node_last_and($or_node_id) )
    {
        my $parent      = $bocage->_marpa_b_and_node_parent($and_node_id);
        my $predecessor = $bocage->_marpa_b_and_node_predecessor($and_node_id);
        my $cause       = $bocage->_marpa_b_and_node_cause($and_node_id);
        my $symbol      = $bocage->_marpa_b_and_node_symbol($and_node_id);
        last AND_NODE if not defined $parent;
        my $origin            = $bocage->_marpa_b_or_node_origin($parent);
        my $set               = $bocage->_marpa_b_or_node_set($parent);
        my $irl_id            = $bocage->_marpa_b_or_node_irl($parent);
        my $position          = $bocage->_marpa_b_or_node_position($parent);
        my $origin_earleme    = $recce_c->earleme($origin);
        my $current_earleme   = $recce_c->earleme($set);
        my $middle_earley_set = $bocage->_marpa_b_and_node_middle($and_node_id);
        my $middle_earleme    = $recce_c->earleme($middle_earley_set);

#<<<  perltidy introduces trailing space on this
        my $desc =
              "And-node #$and_node_id: R"
            . $irl_id . q{:}
            . $position . q{@}
            . $origin_earleme . q{-}
            . $current_earleme;
#>>>
        my $cause_rule = -1;
        if ( defined $cause ) {
            my $cause_irl_id = $bocage->_marpa_b_or_node_irl($cause);
            $desc .= 'C' . $cause_irl_id;
        }
        else {
            $desc .= 'S' . $symbol;
        }
        $desc .= q{@} . $middle_earleme;
        $text .= "    $desc\n";
    }
    return $text;
} ## end sub Marpa::R2::Recognizer::verbose_or_node

sub Marpa::R2::Recognizer::show_nook {
    my ( $recce, $nook_id, $verbose ) = @_;
    my $recce_c = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $order   = $recce->[Marpa::R2::Internal::Recognizer::O_C];
    my $tree    = $recce->[Marpa::R2::Internal::Recognizer::T_C];

    my $or_node_id = $tree->_marpa_t_nook_or_node($nook_id);
    return if not defined $or_node_id;

    my $text = "o$or_node_id";
    my $parent = $tree->_marpa_t_nook_parent($nook_id) // q{-};
    CHILD_TYPE: {
        if ( $tree->_marpa_t_nook_is_cause($nook_id) ) {
            $text .= "[c$parent]";
            last CHILD_TYPE;
        }
        if ( $tree->_marpa_t_nook_is_predecessor($nook_id) ) {
            $text .= "[p$parent]";
            last CHILD_TYPE;
        }
        $text .= '[-]';
    } ## end CHILD_TYPE:
    my $or_node_tag =
        Marpa::R2::Recognizer::or_node_tag( $recce, $or_node_id );
    $text .= " $or_node_tag";

    $text .= ' p';
    $text .=
        $tree->_marpa_t_nook_predecessor_is_ready($nook_id)
        ? q{=ok}
        : q{-};
    $text .= ' c';
    $text .= $tree->_marpa_t_nook_cause_is_ready($nook_id) ? q{=ok} : q{-};
    $text .= "\n";

    DESCRIBE_CHOICES: {
        my $this_choice = $tree->_marpa_t_nook_choice($nook_id);
        CHOICE: for ( my $choice_ix = 0;; $choice_ix++ ) {
            my $and_node_id =
                $order->_marpa_o_and_node_order_get( $or_node_id,
                $choice_ix );
            last CHOICE if not defined $and_node_id;
            $text .= " o$or_node_id" . '[' . $choice_ix . ']';
            if ( defined $this_choice and $this_choice == $choice_ix ) {
                $text .= q{*};
            }
            my $and_node_tag =
                Marpa::R2::Recognizer::and_node_tag( $recce, $and_node_id );
            $text .= " ::= a$and_node_id $and_node_tag";
            $text .= "\n";
        } ## end CHOICE: for ( my $choice_ix = 0;; $choice_ix++ )
    } ## end DESCRIBE_CHOICES:
    return $text;
} ## end sub Marpa::R2::Recognizer::show_nook

sub Marpa::R2::Recognizer::show_tree {
    my ( $recce, $verbose ) = @_;
    my $text = q{};
    NOOK: for ( my $nook_id = 0; 1; $nook_id++ ) {
        my $nook_text = $recce->show_nook( $nook_id, $verbose );
        last NOOK if not defined $nook_text;
        $text .= "$nook_id: $nook_text";
    }
    return $text;
} ## end sub Marpa::R2::Recognizer::show_tree

sub trace_token_evaluation {
    my ( $recce, $value, $token_id, $token_value ) = @_;
    my $order   = $recce->[Marpa::R2::Internal::Recognizer::O_C];
    my $tree    = $recce->[Marpa::R2::Internal::Recognizer::T_C];
    my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR];

    my $nook_ix = $value->_marpa_v_nook();
    if ( not defined $nook_ix ) {
        print {$Marpa::R2::Internal::TRACE_FH} "Nulling valuator\n"
            or Marpa::R2::exception('Could not print to trace file');
        return;
    }
    my $or_node_id = $tree->_marpa_t_nook_or_node($nook_ix);
    my $choice     = $tree->_marpa_t_nook_choice($nook_ix);
    my $and_node_id =
        $order->_marpa_o_and_node_order_get( $or_node_id, $choice );
    my $token_name;
    if ( defined $token_id ) {
        $token_name = $grammar->symbol_name($token_id);
    }

    print {$Marpa::R2::Internal::TRACE_FH}
        'Pushed value from ',
        Marpa::R2::Recognizer::and_node_tag( $recce, $and_node_id ),
        ': ',
        ( $token_name ? qq{$token_name = } : q{} ),
        Data::Dumper->new( [ \$token_value ] )->Terse(1)->Dump
        or Marpa::R2::exception('print to trace handle failed');

    return;

} ## end sub trace_token_evaluation

sub trace_stack_1 {
    my ( $grammar, $recce, $value, $args, $rule_id ) = @_;
    my $recce_c = $recce->[Marpa::R2::Internal::Recognizer::C];
    my $bocage  = $recce->[Marpa::R2::Internal::Recognizer::B_C];
    my $order   = $recce->[Marpa::R2::Internal::Recognizer::O_C];
    my $tree    = $recce->[Marpa::R2::Internal::Recognizer::T_C];

    my $argc       = scalar @{$args};
    my $nook_ix    = $value->_marpa_v_nook();
    my $or_node_id = $tree->_marpa_t_nook_or_node($nook_ix);
    my $choice     = $tree->_marpa_t_nook_choice($nook_ix);
    my $and_node_id =
        $order->_marpa_o_and_node_order_get( $or_node_id, $choice );

    return 'Popping ', $argc,
        ' values to evaluate ',
        Marpa::R2::Recognizer::and_node_tag( $recce, $and_node_id ),
        ', rule: ', $grammar->brief_rule($rule_id);

} ## end sub trace_stack_1

sub trace_op {

    my ( $grammar, $recce, $value ) = @_;

    my $trace_output = q{};
    my $trace_values =
        $recce->[Marpa::R2::Internal::Recognizer::TRACE_VALUES] // 0;

    return $trace_output if not $trace_values >= 2;

    my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
    my $bocage    = $recce->[Marpa::R2::Internal::Recognizer::B_C];
    my $order     = $recce->[Marpa::R2::Internal::Recognizer::O_C];
    my $tree      = $recce->[Marpa::R2::Internal::Recognizer::T_C];

    my $nook_ix    = $value->_marpa_v_nook();
    my $or_node_id = $tree->_marpa_t_nook_or_node($nook_ix);
    my $choice     = $tree->_marpa_t_nook_choice($nook_ix);
    my $and_node_id =
        $order->_marpa_o_and_node_order_get( $or_node_id, $choice );
    my $trace_irl_id = $bocage->_marpa_b_or_node_irl($or_node_id);
    my $virtual_rhs  = $grammar_c->_marpa_g_irl_is_virtual_rhs($trace_irl_id);
    my $virtual_lhs  = $grammar_c->_marpa_g_irl_is_virtual_lhs($trace_irl_id);

    return $trace_output
        if $bocage->_marpa_b_or_node_position($or_node_id)
        != $grammar_c->_marpa_g_irl_length($trace_irl_id);

    return $trace_output if not $virtual_rhs and not $virtual_lhs;

    if ( $virtual_rhs and not $virtual_lhs ) {

        $trace_output .= join q{},
            'Head of Virtual Rule: ',
            Marpa::R2::Recognizer::and_node_tag( $recce, $and_node_id ),
            ', rule: ', $grammar->brief_irl($trace_irl_id),
            "\n",
            'Incrementing virtual rule by ',
            $grammar_c->_marpa_g_real_symbol_count($trace_irl_id), ' symbols',
            "\n"
            or Marpa::R2::exception('Could not print to trace file');

        return $trace_output;

    } ## end if ( $virtual_rhs and not $virtual_lhs )

    if ( $virtual_lhs and $virtual_rhs ) {

        $trace_output .= join q{},
            'Virtual Rule: ',
            Marpa::R2::Recognizer::and_node_tag( $recce, $and_node_id ),
            ', rule: ', $grammar->brief_irl($trace_irl_id),
            "\nAdding ",
            $grammar_c->_marpa_g_real_symbol_count($trace_irl_id),
            "\n";

        return $trace_output;

    } ## end if ( $virtual_lhs and $virtual_rhs )

    if ( not $virtual_rhs and $virtual_lhs ) {

        $trace_output .= join q{},
            'New Virtual Rule: ',
            Marpa::R2::Recognizer::and_node_tag( $recce, $and_node_id ),
            ', rule: ', $grammar->brief_irl($trace_irl_id),
            "\nReal symbol count is ",
            $grammar_c->_marpa_g_real_symbol_count($trace_irl_id),
            "\n";

        return $trace_output;

    } ## end if ( not $virtual_rhs and $virtual_lhs )

    return $trace_output;
} ## end sub trace_op

sub value_trace {
    my ( $value, $trace_flag ) = @_;
    return $value->_marpa_v_trace($trace_flag);
}

1;

# vim: expandtab shiftwidth=4: