/
Grammar.pm
1517 lines (1291 loc) · 55.6 KB
/
Grammar.pm
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
# Copyright 2012 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::Grammar;
use 5.010;
use warnings;
# There's a problem with this perlcritic check
# as of 9 Aug 2010
## no critic (TestingAndDebugging::ProhibitNoWarnings)
no warnings qw(recursion qw);
## use critic
use strict;
# It's all integers, except for the version number
use integer;
use utf8;
use vars qw($VERSION $STRING_VERSION);
$VERSION = '2.021_005';
$STRING_VERSION = $VERSION;
## no critic(BuiltinFunctions::ProhibitStringyEval)
$VERSION = eval $VERSION;
## use critic
=begin Implementation:
Structures and Objects: The design is to present an object-oriented
interface, but internally to avoid overheads. So internally, where
objects might be used, I use array with constant indices to imitate
what in C would be structures.
=end Implementation:
=cut
BEGIN {
my $structure = <<'END_OF_STRUCTURE';
:package=Marpa::R2::Internal::Symbol
ID { Unique ID }
NAME
END_OF_STRUCTURE
Marpa::R2::offset($structure);
} ## end BEGIN
BEGIN {
my $structure = <<'END_OF_STRUCTURE';
:package=Marpa::R2::Internal::Rule
ID
NAME
ACTION { action for this rule as specified by user }
DISCARD_SEPARATION
END_OF_STRUCTURE
Marpa::R2::offset($structure);
} ## end BEGIN
BEGIN {
my $structure = <<'END_OF_STRUCTURE';
:package=Marpa::R2::Internal::Grammar
C { A C structure }
RULES { array of rule refs }
SYMBOLS { array of symbol refs }
ACTIONS { Default package in which to find actions }
DEFAULT_ACTION { Action for rules without one }
TRACE_FILE_HANDLE
WARNINGS { print warnings about grammar? }
RULE_NAME_REQUIRED
RULE_BY_NAME
=LAST_BASIC_DATA_FIELD
{ === Evaluator Fields === }
SYMBOL_HASH { hash to symbol ID by name of symbol }
DEFAULT_EMPTY_ACTION { default value for empty rules }
ACTION_OBJECT
INFINITE_ACTION
=LAST_EVALUATOR_FIELD
PROBLEMS { fatal problems }
=LAST_RECOGNIZER_FIELD
START_NAME { name of original symbol }
INACCESSIBLE_OK
UNPRODUCTIVE_OK
TRACE_RULES
=LAST_FIELD
END_OF_STRUCTURE
Marpa::R2::offset($structure);
} ## end BEGIN
package Marpa::R2::Internal::Grammar;
use English qw( -no_match_vars );
our %DEFAULT_SYMBOLS_RESERVED;
%DEFAULT_SYMBOLS_RESERVED = map { $_, 1 } split //xms, '}]>)';
sub Marpa::R2::Internal::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";
while ( my ( $arg, $value ) = each %{$args} ) {
given ($arg) {
when ('fatal_error') { $fatal_error = $value }
when ('grammar') { $grammar = $value }
when ('where') { $where = $value }
when ('long_where') { $long_where = $value }
when ('warnings') { $warnings = $value }
when ('eval_ok') {
$eval_value = $value;
$eval_given = 1;
}
default { push @msg, "Unknown argument to code_problems: $arg" };
} ## end given
} ## end while ( my ( $arg, $value ) = each %{$args} )
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)
push @msg, q{* ONE PLACE TO LOOK FOR THE PROBLEM IS IN THE CODE};
Marpa::R2::exception(@msg);
# this is to keep perlcritic happy
return 1;
} ## end sub Marpa::R2::Internal::code_problems
sub Marpa::R2::uncaught_error {
my ($error) = @_;
# This would be Carp::confess, but in the testing
# the stack trace includes the hoped for error
# message, which causes spurious success reports.
Carp::croak(
"libmarpa reported an error which Marpa::R2 did not catch\n",
$error
);
} ## end sub Marpa::R2::uncaught_error
package Marpa::R2::Internal::Grammar;
sub Marpa::R2::Grammar::new {
my ( $class, @arg_hashes ) = @_;
my $grammar = [];
bless $grammar, $class;
# set the defaults and the default defaults
$grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE] = *STDERR;
$grammar->[Marpa::R2::Internal::Grammar::TRACE_RULES] = 0;
$grammar->[Marpa::R2::Internal::Grammar::WARNINGS] = 1;
$grammar->[Marpa::R2::Internal::Grammar::INACCESSIBLE_OK] = {};
$grammar->[Marpa::R2::Internal::Grammar::UNPRODUCTIVE_OK] = {};
$grammar->[Marpa::R2::Internal::Grammar::INFINITE_ACTION] = 'fatal';
$grammar->[Marpa::R2::Internal::Grammar::SYMBOLS] = [];
$grammar->[Marpa::R2::Internal::Grammar::SYMBOL_HASH] = {};
$grammar->[Marpa::R2::Internal::Grammar::RULES] = [];
$grammar->[Marpa::R2::Internal::Grammar::RULE_BY_NAME] = {};
$grammar->[Marpa::R2::Internal::Grammar::RULE_NAME_REQUIRED] = 0;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C] =
Marpa::R2::Thin::G->new( { if => 1 } );
$grammar->set(@arg_hashes);
return $grammar;
} ## end sub Marpa::R2::Grammar::new
sub Marpa::R2::Grammar::thin {
$_[0]->[Marpa::R2::Internal::Grammar::C];
}
sub Marpa::R2::Grammar::thin_symbol {
my ($grammar, $symbol_name) = @_;
return $grammar->[Marpa::R2::Internal::Grammar::SYMBOL_HASH]->{$symbol_name};
}
use constant GRAMMAR_OPTIONS => [
qw{
action_object
actions
infinite_action
default_action
default_empty_action
default_rank
inaccessible_ok
rule_name_required
rules
start
symbols
terminals
trace_file_handle
unproductive_ok
warnings
}
];
sub Marpa::R2::Grammar::set {
my ( $grammar, @arg_hashes ) = @_;
# set trace_fh even if no tracing, because we may turn it on in this method
my $trace_fh =
$grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE];
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
for my $args (@arg_hashes) {
my $ref_type = ref $args;
if ( not $ref_type or $ref_type ne 'HASH' ) {
Carp::croak(
'Marpa::R2::Grammar expects args as ref to HASH, got ',
( "ref to $ref_type" || 'non-reference' ),
' instead'
);
} ## end if ( not $ref_type or $ref_type ne 'HASH' )
if (my @bad_options =
grep { not $_ ~~ Marpa::R2::Internal::Grammar::GRAMMAR_OPTIONS }
keys %{$args}
)
{
Carp::croak( 'Unknown option(s) for Marpa::R2::Grammar: ',
join q{ }, @bad_options );
} ## end if ( my @bad_options = grep { not $_ ~~ ...})
# First pass options: These affect processing of other
# options and are expected to take force for the other
# options, even if specified afterwards
if ( defined( my $value = $args->{'trace_file_handle'} ) ) {
$trace_fh = $grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE] =
$value;
}
if ( defined( my $value = $args->{'default_rank'} ) ) {
Marpa::R2::exception(
'default_rank option not allowed after grammar is precomputed'
) if $grammar_c->is_precomputed();
$grammar_c->default_rank_set($value);
} ## end if ( defined( my $value = $args->{'default_rank'} ) )
# Second pass options
if ( defined( my $value = $args->{'symbols'} ) ) {
Marpa::R2::exception(
'symbols option not allowed after grammar is precomputed')
if $grammar_c->is_precomputed();
Marpa::R2::exception('symbols value must be REF to HASH')
if ref $value ne 'HASH';
while ( my ( $symbol, $properties ) = each %{$value} ) {
assign_user_symbol( $grammar, $symbol, $properties );
}
} ## end if ( defined( my $value = $args->{'symbols'} ) )
if ( defined( my $value = $args->{'rule_name_required'} ) ) {
$grammar->[Marpa::R2::Internal::Grammar::RULE_NAME_REQUIRED] =
!!$value;
}
if ( defined( my $value = $args->{'terminals'} ) ) {
Marpa::R2::exception(
'terminals option not allowed after grammar is precomputed')
if $grammar_c->is_precomputed();
Marpa::R2::exception('terminals value must be REF to ARRAY')
if ref $value ne 'ARRAY';
for my $symbol ( @{$value} ) {
assign_user_symbol( $grammar, $symbol, { terminal => 1 } );
}
} ## end if ( defined( my $value = $args->{'terminals'} ) )
if ( defined( my $value = $args->{'start'} ) ) {
Marpa::R2::exception(
'start option not allowed after grammar is precomputed')
if $grammar_c->is_precomputed();
$grammar->[Marpa::R2::Internal::Grammar::START_NAME] = $value;
} ## end if ( defined( my $value = $args->{'start'} ) )
if ( defined( my $value = $args->{'rules'} ) ) {
Marpa::R2::exception(
'rules option not allowed after grammar is precomputed')
if $grammar_c->is_precomputed();
Marpa::R2::exception('rules value must be reference to array')
if ref $value ne 'ARRAY';
add_user_rules( $grammar, $value );
} ## end if ( defined( my $value = $args->{'rules'} ) )
if ( exists $args->{'default_empty_action'} ) {
my $value = $args->{'default_empty_action'};
$grammar->[Marpa::R2::Internal::Grammar::DEFAULT_EMPTY_ACTION] =
$value;
}
if ( defined( my $value = $args->{'actions'} ) ) {
$grammar->[Marpa::R2::Internal::Grammar::ACTIONS] = $value;
}
if ( defined( my $value = $args->{'action_object'} ) ) {
$grammar->[Marpa::R2::Internal::Grammar::ACTION_OBJECT] = $value;
}
if ( defined( my $value = $args->{'default_action'} ) ) {
$grammar->[Marpa::R2::Internal::Grammar::DEFAULT_ACTION] = $value;
}
if ( defined( my $value = $args->{'infinite_action'} ) ) {
if ( $value && $grammar_c->is_precomputed() ) {
say {$trace_fh}
'"infinite_action" option is useless after grammar is precomputed'
or Marpa::R2::exception("Could not print: $ERRNO");
}
Marpa::R2::exception(
q{infinite_action must be 'warn', 'quiet' or 'fatal'})
if not $value ~~ [qw(warn quiet fatal)];
$grammar->[Marpa::R2::Internal::Grammar::INFINITE_ACTION] =
$value;
} ## end if ( defined( my $value = $args->{'infinite_action'}...))
if ( defined( my $value = $args->{'warnings'} ) ) {
if ( $value && $grammar_c->is_precomputed() ) {
say {$trace_fh}
q{"warnings" option is useless after grammar is precomputed}
or Marpa::R2::exception("Could not print: $ERRNO");
}
$grammar->[Marpa::R2::Internal::Grammar::WARNINGS] = $value;
} ## end if ( defined( my $value = $args->{'warnings'} ) )
if ( defined( my $value = $args->{'inaccessible_ok'} ) ) {
if ( $value && $grammar_c->is_precomputed() ) {
say {$trace_fh}
q{"inaccessible_ok" option is useless after grammar is precomputed}
or Marpa::R2::exception("Could not print: $ERRNO");
} ## end if ( $value && $grammar_c->is_precomputed() )
given ( ref $value ) {
when (q{}) {
$value //= {
}
}
when ('ARRAY') {
$value = {
map { ( $_, 1 ) } @{$value}
}
}
default {
Marpa::R2::exception(
'value of inaccessible_ok option must be boolean or an array ref'
)
}
} ## end given
$grammar->[Marpa::R2::Internal::Grammar::INACCESSIBLE_OK] =
$value;
} ## end if ( defined( my $value = $args->{'inaccessible_ok'}...))
if ( defined( my $value = $args->{'unproductive_ok'} ) ) {
if ( $value && $grammar_c->is_precomputed() ) {
say {$trace_fh}
q{"unproductive_ok" option is useless after grammar is precomputed}
or Marpa::R2::exception("Could not print: $ERRNO");
}
given ( ref $value ) {
when (q{}) {
$value //= {
};
}
when ('ARRAY') {
$value = {
map { ( $_, 1 ) } @{$value}
}
}
default {
Marpa::R2::exception(
'value of unproductive_ok option must be boolean or an array ref'
)
}
} ## end given
$grammar->[Marpa::R2::Internal::Grammar::UNPRODUCTIVE_OK] =
$value;
} ## end if ( defined( my $value = $args->{'unproductive_ok'}...))
} ## end for my $args (@arg_hashes)
return 1;
} ## end sub Marpa::R2::Grammar::set
sub Marpa::R2::Grammar::symbol_reserved_set {
my ( $grammar, $final_character, $boolean ) = @_;
if ( length $final_character != 1 ) {
Marpa::R2::exception( 'symbol_reserved_set(): "',
$final_character, '" is not a symbol' );
}
if ( $final_character eq ']' ) {
return if $boolean;
Marpa::R2::exception(
q{symbol_reserved_set(): Attempt to unreserve ']'; this is not allowed}
);
} ## end if ( $final_character eq ']' ) ([)
if ( not exists $DEFAULT_SYMBOLS_RESERVED{$final_character} ) {
Marpa::R2::exception(
qq{symbol_reserved_set(): "$final_character" is not a reservable symbol}
);
}
$DEFAULT_SYMBOLS_RESERVED{$final_character} = $boolean ? 1 : 0;
} ## end sub Marpa::R2::Grammar::symbol_reserved_set
sub Marpa::R2::Grammar::precompute {
my $grammar = shift;
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES];
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $trace_fh =
$grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE];
my $problems = $grammar->[Marpa::R2::Internal::Grammar::PROBLEMS];
if ($problems) {
Marpa::R2::exception(
Marpa::R2::Grammar::show_problems($grammar),
"Second attempt to precompute grammar with fatal problems\n",
'Marpa::R2 cannot proceed'
);
} ## end if ($problems)
return $grammar if $grammar_c->is_precomputed();
set_start_symbol($grammar);
# Catch errors in precomputation
my $precompute_error_code = $Marpa::R2::Error::NONE;
$grammar_c->throw_set(0);
my $precompute_result = $grammar_c->precompute();
$grammar_c->throw_set(1);
if ( $precompute_result < 0 ) {
($precompute_error_code) = $grammar_c->error();
if ( not defined $precompute_error_code ) {
Marpa::R2::exception(
'libmarpa error, but no error code returned');
}
# If already precomputed, just return success
return $grammar
if $precompute_error_code == $Marpa::R2::Error::PRECOMPUTED;
# Cycles are not necessarily errors,
# and get special handling
$precompute_error_code = $Marpa::R2::Error::NONE
if $precompute_error_code == $Marpa::R2::Error::GRAMMAR_HAS_CYCLE;
} ## end if ( not defined $precompute_result )
if ( $precompute_error_code != $Marpa::R2::Error::NONE ) {
# Report the errors, then return failure
if ( $precompute_error_code == $Marpa::R2::Error::NO_RULES ) {
Marpa::R2::exception(
'Attempted to precompute grammar with no rules');
}
if ( $precompute_error_code == $Marpa::R2::Error::NULLING_TERMINAL ) {
my @nulling_terminals = ();
my $event_count = $grammar_c->event_count();
EVENT:
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ )
{
my ( $event_type, $value ) = $grammar_c->event( $event_ix );
if ( $event_type eq 'MARPA_EVENT_NULLING_TERMINAL' ) {
push @nulling_terminals, $grammar->symbol_name($value);
}
} ## end for ( my $event_ix = 0; $event_ix < $event_count; ...)
my @nulling_terminal_messages =
map {qq{Nulling symbol "$_" is also a terminal\n}}
@nulling_terminals;
Marpa::R2::exception( @nulling_terminal_messages,
'A terminal symbol cannot also be a nulling symbol' );
} ## end if ( $precompute_error_code == ...)
if ( $precompute_error_code == $Marpa::R2::Error::COUNTED_NULLABLE ) {
my @counted_nullables = ();
my $event_count = $grammar_c->event_count();
EVENT:
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ ) {
my ( $event_type, $value ) = $grammar_c->event($event_ix);
if ( $event_type eq 'MARPA_EVENT_COUNTED_NULLABLE' ) {
push @counted_nullables, $grammar->symbol_name($value);
}
} ## end for ( my $event_ix = 0; $event_ix < $event_count; ...)
my @counted_nullable_messages = map {
q{Nullable symbol "}
. $_
. qq{" is on rhs of counted rule\n}
} @counted_nullables;
Marpa::R2::exception( @counted_nullable_messages,
'Counted nullables confuse Marpa -- please rewrite the grammar'
);
} ## end if ( $precompute_error_code == ...)
if ( $precompute_error_code == $Marpa::R2::Error::NO_START_SYMBOL ) {
Marpa::R2::exception('No start symbol');
}
if ( $precompute_error_code == $Marpa::R2::Error::START_NOT_LHS ) {
my $name = $grammar->[Marpa::R2::Internal::Grammar::START_NAME];
Marpa::R2::exception(
qq{Start symbol "$name" not on LHS of any rule});
}
if ( $precompute_error_code == $Marpa::R2::Error::UNPRODUCTIVE_START )
{
my $name = $grammar->[Marpa::R2::Internal::Grammar::START_NAME];
Marpa::R2::exception(qq{Unproductive start symbol: "$name"});
}
Marpa::R2::uncaught_error( scalar $grammar_c->error() );
} ## end if ( $precompute_error_code != $Marpa::R2::Error::NONE)
# Shadow all the new rules
{
my $highest_rule_id = $grammar_c->highest_rule_id();
RULE:
for ( my $rule_id = 0; $rule_id <= $highest_rule_id; $rule_id++ )
{
next RULE if defined $rules->[$rule_id];
# The Marpa::R2 logic assumes no "gaps" in the rule numbering,
# which is currently the case for Libmarpa,
# but not guaranteed.
shadow_rule( $grammar, $rule_id );
} ## end RULE: for ( my $rule_id = 0; $rule_id <= $highest_rule_id; ...)
}
my $infinite_action =
$grammar->[Marpa::R2::Internal::Grammar::INFINITE_ACTION];
# Above I went through the error events
# Here I go through the events for situations where there was no
# hard error returned from libmarpa
my $loop_rule_count = 0;
{
my $event_count = $grammar_c->event_count();
EVENT:
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ ) {
my ( $event_type, $value ) = $grammar_c->event($event_ix);
if ( $event_type ne 'MARPA_EVENT_LOOP_RULES' ) {
Marpa::R2::exception(
qq{Unknown grammar precomputation event; type="$event_type"}
);
}
$loop_rule_count = $value;
} ## end for ( my $event_ix = 0; $event_ix < $event_count; $event_ix...)
}
if ( $loop_rule_count and $infinite_action ne 'quiet' ) {
my @loop_rules =
grep { $grammar_c->rule_is_loop($_) } ( 0 .. $#{$rules} );
for my $rule_id (@loop_rules) {
print {$trace_fh}
'Cycle found involving rule: ',
$grammar->brief_rule($rule_id), "\n"
or Marpa::R2::exception("Could not print: $ERRNO");
} ## end for my $rule_id (@loop_rules)
Marpa::R2::exception('Cycles in grammar, fatal error')
if $infinite_action eq 'fatal';
} ## end if ( $loop_rule_count and $infinite_action ne 'quiet')
# A bit hackish here: INACCESSIBLE_OK is not a HASH ref iff
# it is a Boolean TRUE indicating that all inaccessibles are OK.
# A Boolean FALSE will have been replaced with an empty hash.
if ($grammar->[Marpa::R2::Internal::Grammar::WARNINGS]
and ref(
my $ok = $grammar->[Marpa::R2::Internal::Grammar::INACCESSIBLE_OK]
) eq 'HASH'
)
{
SYMBOL:
for my $symbol (
@{ Marpa::R2::Grammar::inaccessible_symbols($grammar) } )
{
# Inaccessible internal symbols may be created
# from inaccessible use symbols -- ignore these.
# This assumes that Marpa's logic
# is correct and that
# it is not creating inaccessible symbols from
# accessible ones.
next SYMBOL if $symbol =~ /\]/xms;
next SYMBOL if $ok->{$symbol};
say {$trace_fh} "Inaccessible symbol: $symbol"
or Marpa::R2::exception("Could not print: $ERRNO");
} ## end for my $symbol ( @{ Marpa::R2::Grammar::inaccessible_symbols...})
} ## end if ( $grammar->[Marpa::R2::Internal::Grammar::WARNINGS...])
# A bit hackish here: UNPRODUCTIVE_OK is not a HASH ref iff
# it is a Boolean TRUE indicating that all inaccessibles are OK.
# A Boolean FALSE will have been replaced with an empty hash.
if ($grammar->[Marpa::R2::Internal::Grammar::WARNINGS]
and ref(
my $ok = $grammar->[Marpa::R2::Internal::Grammar::UNPRODUCTIVE_OK]
) eq 'HASH'
)
{
SYMBOL:
for my $symbol (
@{ Marpa::R2::Grammar::unproductive_symbols($grammar) } )
{
# Unproductive internal symbols may be created
# from unproductive use symbols -- ignore these.
# This assumes that Marpa's logic
# is correct and that
# it is not creating unproductive symbols from
# productive ones.
next SYMBOL if $symbol =~ /\]/xms;
next SYMBOL if $ok->{$symbol};
say {$trace_fh} "Unproductive symbol: $symbol"
or Marpa::R2::exception("Could not print: $ERRNO");
} ## end for my $symbol ( @{ Marpa::R2::Grammar::unproductive_symbols...})
} ## end if ( $grammar->[Marpa::R2::Internal::Grammar::WARNINGS...])
return $grammar;
} ## end sub Marpa::R2::Grammar::precompute
sub Marpa::R2::Grammar::rule_by_name {
my ( $grammar, $name ) = @_;
return $grammar->[Marpa::R2::Internal::Grammar::RULE_BY_NAME]->{$name};
}
sub Marpa::R2::Grammar::show_problems {
my ($grammar) = @_;
my $problems = $grammar->[Marpa::R2::Internal::Grammar::PROBLEMS];
if ($problems) {
my $problem_count = scalar @{$problems};
return
"Grammar has $problem_count problems:\n"
. ( join "\n", @{$problems} ) . "\n";
} ## end if ($problems)
return "Grammar has no problems\n";
} ## end sub Marpa::R2::Grammar::show_problems
sub Marpa::R2::Grammar::show_symbol {
my ( $grammar, $symbol ) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $text = q{};
my $symbol_id = $symbol->[Marpa::R2::Internal::Symbol::ID];
my $name = $grammar->symbol_name($symbol_id);
$text .= "$symbol_id: $name";
my @tag_list = ();
$grammar_c->symbol_is_productive($symbol_id) or push @tag_list, 'unproductive';
$grammar_c->symbol_is_accessible($symbol_id) or push @tag_list, 'inaccessible';
$grammar_c->symbol_is_nulling($symbol_id) and push @tag_list, 'nulling';
$grammar_c->symbol_is_terminal($symbol_id) and push @tag_list, 'terminal';
$text .= join q{ }, q{,}, @tag_list if scalar @tag_list;
$text .= "\n";
return $text;
} ## end sub Marpa::R2::Grammar::show_symbol
sub Marpa::R2::Grammar::show_symbols {
my ($grammar) = @_;
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
my $text = q{};
for my $symbol_ref ( @{$symbols} ) {
$text .= $grammar->show_symbol($symbol_ref);
}
return $text;
} ## end sub Marpa::R2::Grammar::show_symbols
sub Marpa::R2::Grammar::show_nulling_symbols {
my ($grammar) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
return join q{ }, sort map { $grammar->symbol_name($_) }
grep { $grammar_c->symbol_is_nulling($_) } ( 0 .. $#{$symbols} );
} ## end sub Marpa::R2::Grammar::show_nulling_symbols
sub Marpa::R2::Grammar::show_productive_symbols {
my ($grammar) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
return join q{ }, sort map { $grammar->symbol_name($_) }
grep { $grammar_c->symbol_is_productive($_) } ( 0 .. $#{$symbols} );
} ## end sub Marpa::R2::Grammar::show_productive_symbols
sub Marpa::R2::Grammar::show_accessible_symbols {
my ($grammar) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
return join q{ }, sort map { $grammar->symbol_name($_) }
grep { $grammar_c->symbol_is_accessible($_) } ( 0 .. $#{$symbols} );
} ## end sub Marpa::R2::Grammar::show_accessible_symbols
sub Marpa::R2::Grammar::inaccessible_symbols {
my ($grammar) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
return [
sort map { $grammar->symbol_name($_) }
grep { !$grammar_c->symbol_is_accessible($_) }
( 0 .. $#{$symbols} )
];
} ## end sub Marpa::R2::Grammar::inaccessible_symbols
sub Marpa::R2::Grammar::unproductive_symbols {
my ($grammar) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
return [
sort map { $grammar->symbol_name($_) }
grep { !$grammar_c->symbol_is_productive($_) }
( 0 .. $#{$symbols} )
];
} ## end sub Marpa::R2::Grammar::unproductive_symbols
sub Marpa::R2::Grammar::brief_rule {
my ( $grammar, $rule_id ) = @_;
my ($lhs, @rhs) = $grammar->rule($rule_id);
return join q{ }, "$rule_id:", $lhs, '->', @rhs;
} ## end sub Marpa::R2::Grammar::brief_rule
sub Marpa::R2::Grammar::show_rule {
my ( $grammar, $rule ) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $rule_id = $rule->[Marpa::R2::Internal::Rule::ID];
my @comment = ();
$grammar_c->rule_length($rule_id) == 0 and push @comment, 'empty';
$grammar->rule_is_used($rule_id) or push @comment, '!used';
$grammar_c->rule_is_productive($rule_id) or push @comment, 'unproductive';
$grammar_c->rule_is_accessible($rule_id) or push @comment, 'inaccessible';
$rule->[Marpa::R2::Internal::Rule::DISCARD_SEPARATION]
and push @comment, 'discard_sep';
my $text = $grammar->brief_rule($rule_id);
if (@comment) {
$text .= q{ } . ( join q{ }, q{/*}, @comment, q{*/} );
}
return $text .= "\n";
} # sub show_rule
sub Marpa::R2::Grammar::show_rules {
my ($grammar) = @_;
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES];
my $text;
for my $rule ( @{$rules} ) {
$text .= $grammar->show_rule($rule);
}
return $text;
} ## end sub Marpa::R2::Grammar::show_rules
# This logic tests for gaps in the rule numbering.
# Currently there are none, but Libmarpa does not
# guarantee this.
sub Marpa::R2::Grammar::rule_ids {
my ($grammar) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
return
grep { $grammar_c->rule_length($_); }
0 .. $grammar_c->highest_rule_id();
} ## end sub Marpa::R2::Grammar::rule_ids
sub Marpa::R2::Grammar::rule {
my ( $grammar, $rule_id ) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $rule_length = $grammar_c->rule_length($rule_id);
return if not defined $rule_length;
my @symbol_ids = ( $grammar_c->rule_lhs( $rule_id) );
push @symbol_ids, map { $grammar_c->rule_rhs( $rule_id, $_ ) } (0 .. $rule_length - 1);
return map { $grammar->symbol_name($_) } @symbol_ids;
} ## end sub Marpa::R2::Grammar::rule
sub Marpa::R2::Grammar::action {
my ( $grammar, $rule_id ) = @_;
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES];
my $rule = $rules->[$rule_id];
Marpa::R2::exception("No such rule ID: $rule_id") if not defined $rule;
return $rule->[Marpa::R2::Internal::Rule::ACTION];
} ## end sub Marpa::R2::Grammar::action
sub Marpa::R2::Grammar::show_dotted_rule {
my ( $grammar, $rule_id, $dot_position ) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my ($lhs, @rhs) = $grammar->rule($rule_id);
$dot_position = 0 if $dot_position < 0;
splice(@rhs, $dot_position, 0, q{.});
return join q{ }, $lhs, q{->}, @rhs;
} ## end sub Marpa::R2::Grammar::show_dotted_rule
# Used by lexers to check that symbol is a terminal
sub Marpa::R2::Grammar::check_terminal {
my ( $grammar, $name ) = @_;
return 0 if not defined $name;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $symbol_hash = $grammar->[Marpa::R2::Internal::Grammar::SYMBOL_HASH];
my $symbol_id = $symbol_hash->{$name};
return 0 if not defined $symbol_id;
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
my $symbol = $symbols->[$symbol_id];
return $grammar_c->symbol_is_terminal($symbol_id) ? 1 : 0;
} ## end sub Marpa::R2::Grammar::check_terminal
sub Marpa::R2::Grammar::symbol_name {
my ( $grammar, $id ) = @_;
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
# The next is a little roundabout to prevent auto-instantiation
if ( defined $symbols->[$id] ) {
return $symbols->[$id]->[Marpa::R2::Internal::Symbol::NAME];
}
return '[SYMBOL' . $id . ']';
} ## end sub Marpa::R2::Grammar::symbol_name
sub shadow_symbol {
my ( $grammar, $symbol_id, $name ) = @_;
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
my $symbol_hash = $grammar->[Marpa::R2::Internal::Grammar::SYMBOL_HASH];
my $symbol = $symbols->[$symbol_id] = [];
$symbol->[Marpa::R2::Internal::Symbol::ID] = $symbol_id;
$symbol->[Marpa::R2::Internal::Symbol::NAME] = $name;
$symbol_hash->{$name} = $symbol_id;
return $symbol;
} ## end sub shadow_symbol
# Create the structure which "shadows" the libmarpa rule
sub shadow_rule {
my ( $grammar, $rule_id ) = @_;
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES];
my $new_rule = $rules->[$rule_id] = [];
$new_rule->[Marpa::R2::Internal::Rule::ID] = $rule_id;
return $new_rule;
} ## end sub shadow_rule
sub assign_symbol {
my ( $grammar, $name ) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $symbol_hash = $grammar->[Marpa::R2::Internal::Grammar::SYMBOL_HASH];
my $symbol_id = $symbol_hash->{$name};
if ( defined $symbol_id ) {
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS];
return $symbols->[$symbol_id];
}
$symbol_id = $grammar_c->symbol_new();
return shadow_symbol( $grammar, $symbol_id, $name );
} ## end sub assign_symbol
sub assign_user_symbol {
my $grammar = shift;
my $name = shift;
my $options = shift;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
if ( my $type = ref $name ) {
Marpa::R2::exception(
"Symbol name was ref to $type; it must be a scalar string");
}
my $final_symbol = substr $name, -1;
if ( $DEFAULT_SYMBOLS_RESERVED{$final_symbol} ) {
Marpa::R2::exception(
qq{Symbol name $name ends in "$final_symbol": that's not allowed});
}
my $symbol = assign_symbol( $grammar, $name );
my $symbol_id = $symbol->[Marpa::R2::Internal::Symbol::ID];
PROPERTY: while ( my ( $property, $value ) = each %{$options} ) {
if ( not $property ~~ [qw(terminal rank )] ) {
Marpa::R2::exception(qq{Unknown symbol property "$property"});
}
if ( $property eq 'terminal' ) {
$grammar_c->symbol_is_terminal_set( $symbol_id, $value );
}
if ( $property eq 'rank' ) {
Marpa::R2::exception(qq{Symbol "$name": rank must be an integer})
if not Scalar::Util::looks_like_number($value)
or int($value) != $value;
$grammar_c->symbol_rank_set($symbol_id) = $value;
} ## end if ( $property eq 'rank' )
} ## end while ( my ( $property, $value ) = each %{$options} )
return $symbol;
} ## end sub assign_user_symbol
sub action_set {
my ( $rule_id, $grammar, $action ) = @_;
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C];
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES];
my $rule = $rules->[$rule_id];
if ( not $rule ) {
$rules->[$rule_id] = $rule = [];
}
my $rh_length = $grammar_c->rule_length($rule_id);
if ( defined $action and $rh_length < 0 ) {
my $lhs_id = $grammar_c->rule_lhs($rule_id);
my $lhs_name = $grammar->symbol_name($lhs_id);
Marpa::R2::exception(
"Empty Rule cannot have an action\n",
" Rule #$rule_id: $lhs_name -> /* empty */",
"\n"
);
} ## end if ( defined $action and $rh_length < 0 )
$rule->[Marpa::R2::Internal::Rule::ACTION] = $action;
return 1;
} ## end sub action_set
# add one or more rules
sub add_user_rules {
my ( $grammar, $rules ) = @_;
RULE: for my $rule ( @{$rules} ) {
given ( ref $rule ) {