Debian Codegraph @

html/lib/Marpa/R2/HTML/Config/Compile.pm

raw
1 # Copyright 2022 Jeffrey Kegler
2 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
3 # redistribute it and/or modify it under the terms of the GNU Lesser
4 # General Public License as published by the Free Software Foundation,
5 # either version 3 of the License, or (at your option) any later version.
6 #
7 # Marpa::R2 is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser
13 # General Public License along with Marpa::R2. If not, see
14 # http://www.gnu.org/licenses/.
15
16 package Marpa::R2::HTML::Config::Compile;
17
18 use 5.010001;
19 use strict;
20 use warnings;
21
22 use vars qw($VERSION $STRING_VERSION);
23 $VERSION = '14.000000';
24 $STRING_VERSION = $VERSION;
25 ## no critic(BuiltinFunctions::ProhibitStringyEval)
26 $VERSION = eval $VERSION;
27 ## use critic
28
29 use Data::Dumper;
30 use English qw( -no_match_vars );
31
32 use Marpa::R2::HTML::Config::Core;
33 use Marpa::R2::Thin::Trace;
34
35 # Indexes into the symbol table
36 use constant CONTEXT_CLOSED => 0;
37 use constant CONTENTS_CLOSED => 1;
38 use constant CONTEXT => 2;
39 use constant CONTENTS => 3;
40
41 sub do_is_included_statement {
42 my ( $self, $external_element, undef, undef, undef, $external_group ) = @_;
43 my $tag = $external_element;
44 $tag =~ s/\A [<] \s* //xms;
45 $tag =~ s/\s* [>] \z //xms;
46 my $element = 'ELE_' . $tag;
47 ( my $group_name = $external_group ) =~ s/\A [%] //xms;
48 my $group = 'GRP_' . $group_name;
49
50 my $symbol_table = $self->{symbol_table};
51 my $element_entry = $symbol_table->{$element} //= [];
52 my $group_entry = $symbol_table->{$group};
53
54 # For now, new groups cannot be defined
55 Carp::croak(
56 qq{Group "$group" does not exist\n},
57 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
58 ) if not defined $group_entry;
59
60 my $closed_reason = $element_entry->[CONTEXT_CLOSED];
61 if ($closed_reason) {
62 Carp::croak(
63 qq{Context of "$element" cannot be changed:\n},
64 qq{ Reason: $closed_reason\n},
65 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
66 );
67 } ## end if ($closed_reason)
68 $closed_reason = $group_entry->[CONTENTS_CLOSED];
69 if ($closed_reason) {
70 Carp::croak(
71 qq{Contents of "$group" cannot be changed:\n},
72 qq{ Reason: $closed_reason\n},
73 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
74 );
75 } ## end if ($closed_reason)
76
77 # If this is the first, it sets the primary group
78 my $primary_group_by_tag = $self->{primary_group_by_tag};
79 $primary_group_by_tag->{$tag} //= $group;
80 push @{ $element_entry->[CONTEXT] }, $group;
81
82 return;
83
84 } ## end sub do_is_included
85
86 sub do_is_a_included_statement {
87 my ( $self, $external_element, undef, undef, $external_flow, undef, undef, $external_group ) = @_;
88 my $tag = $external_element;
89 $tag =~ s/\A [<] \s* //xms;
90 $tag =~ s/\s* [>] \z //xms;
91 ( my $flow_name = $external_flow ) =~ s/\A [*] //xms;
92 ( my $group_name = $external_group ) =~ s/\A [%] //xms;
93 my $flow = 'FLO_' . $flow_name;
94 my $group = 'GRP_' . $group_name;
95 my $element = 'ELE_' . $tag;
96 my $symbol_table = $self->{symbol_table};
97 my $element_entry = $symbol_table->{$element} //= [];
98 my $group_entry = $symbol_table->{$group};
99 my $flow_entry = $symbol_table->{$flow};
100
101 # For now, new flows and groups cannot be defined
102 Carp::croak(
103 qq{Group "$group" does not exist\n},
104 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
105 ) if not defined $group_entry;
106 Carp::croak(
107 qq{Flow "$flow" does not exist\n},
108 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
109 ) if not defined $flow_entry;
110
111 my $closed_reason = $element_entry->[CONTEXT_CLOSED];
112 if ($closed_reason) {
113 Carp::croak(
114 qq{Context of "$element" cannot be changed:\n},
115 qq{ Reason: $closed_reason\n},
116 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
117 );
118 } ## end if ($closed_reason)
119 $closed_reason = $element_entry->[CONTENTS_CLOSED];
120 if ($closed_reason) {
121 Carp::croak(
122 qq{Contents of "$element" cannot be changed:\n},
123 qq{ Reason: $closed_reason\n},
124 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
125 );
126 } ## end if ($closed_reason)
127 $closed_reason = $flow_entry->[CONTEXT_CLOSED];
128 if ($closed_reason) {
129 Carp::croak(
130 qq{Context of "$flow" cannot be changed:\n},
131 qq{ Reason: $closed_reason\n},
132 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
133 );
134 } ## end if ($closed_reason)
135 $closed_reason = $group_entry->[CONTENTS_CLOSED];
136 if ($closed_reason) {
137 Carp::croak(
138 qq{Contents of "$group" cannot be changed:\n},
139 qq{ Reason: $closed_reason\n},
140 qq{ Problem was in this line: }, $Marpa::R2::HTML::Config::Compile::LINE
141 );
142 } ## end if ($closed_reason)
143
144 Carp::croak(
145 qq{Contents of "$element" are already being defined:\n},
146 qq{ Problem was in this line: },
147 $Marpa::R2::HTML::Config::Compile::LINE
148 ) if defined $element_entry->[CONTENTS];
149 Carp::croak(
150 qq{Context of "$element" is already being defined:\n},
151 qq{ Problem was in this line: },
152 $Marpa::R2::HTML::Config::Compile::LINE
153 ) if defined $element_entry->[CONTEXT];
154
155 # Always sets the primary group
156 my $primary_group_by_tag = $self->{primary_group_by_tag};
157 $primary_group_by_tag->{$tag} = $group;
158 $element_entry->[CONTENTS] = $flow;
159 $element_entry->[CONTEXT] = $group;
160 $element_entry->[CONTEXT_CLOSED] = $element_entry->[CONTENTS_CLOSED] =
161 'Element is already fully defined';
162
163 return;
164 } ## end sub do_is_a_included
165
166 sub do_is_statement {
167
168 my ( $self, $external_element, undef, $external_flow ) = @_;
169 my $tag = $external_element;
170 $tag =~ s/\A [<] \s* //xms;
171 $tag =~ s/\s* [>] \z //xms;
172 ( my $flow_name = $external_flow ) =~ s/\A [*] //xms;
173 my $flow = 'FLO_' . $flow_name;
174 my $element = 'ELE_' . $tag;
175 my $symbol_table = $self->{symbol_table};
176 my $element_entry = $symbol_table->{$element} //= [];
177 my $flow_entry = $symbol_table->{$flow};
178
179 # For now, new flows cannot be defined
180 Carp::croak(
181 qq{Flow "$flow" does not exist\n},
182 qq{ Problem was in this line: },
183 $Marpa::R2::HTML::Config::Compile::LINE
184 ) if not defined $flow_entry;
185
186 my $closed_reason = $element_entry->[CONTENTS_CLOSED];
187 if ($closed_reason) {
188 Carp::croak(
189 qq{Contents of "$element" cannot be changed:\n},
190 qq{ Reason: $closed_reason\n},
191 qq{ Problem was in this line: },
192 $Marpa::R2::HTML::Config::Compile::LINE
193 );
194 } ## end if ($closed_reason)
195 $closed_reason = $flow_entry->[CONTEXT_CLOSED];
196 if ($closed_reason) {
197 Carp::croak(
198 qq{Context of "$flow" cannot be changed:\n},
199 qq{ Reason: $closed_reason\n},
200 qq{ Problem was in this line: },
201 $Marpa::R2::HTML::Config::Compile::LINE
202 );
203 } ## end if ($closed_reason)
204
205 Carp::croak(
206 qq{Contents of "$element" are already being defined:\n},
207 qq{ Problem was in this line: },
208 $Marpa::R2::HTML::Config::Compile::LINE
209 ) if defined $element_entry->[CONTENTS];
210
211 $element_entry->[CONTENTS] = $flow;
212 $element_entry->[CONTENTS_CLOSED] =
213 'Contents of Element are already defined';
214
215 return;
216 } ## end sub do_is
217
218 sub problem_in_rule {
219 my ($string) = @_;
220 Marpa::R2::Context::bail( [ 'rule', $string, Marpa::R2::Context::location() ] );
221 }
222
223 sub do_contains_statement {
224 my ( $self, $external_element, undef, $external_contents ) = @_;
225
226 # Production is Element with custom flow
227 my $tag = $external_element;
228 $tag =~ s/\A [<] \s* //xms;
229 $tag =~ s/\s* [>] \z //xms;
230 my $element_symbol = 'ELE_' . $tag;
231 my $symbol_table = $self->{symbol_table};
232 my $element_entry = $symbol_table->{$element_symbol} //= [];
233 my $closed_reason = $element_entry->[CONTENTS_CLOSED];
234 if ($closed_reason) {
235 Carp::croak(
236 qq{Contents of "$element_symbol" cannot be changed:\n},
237 qq{ Reason: $closed_reason\n},
238 qq{ Problem was in this line: },
239 $Marpa::R2::HTML::Config::Compile::LINE
240 );
241 } ## end if ($closed_reason)
242
243 my @contents = ();
244
245 CONTAINED_SYMBOL:
246 for my $external_content_symbol (@{$external_contents}) {
247 my $content_symbol;
248 if ( $external_content_symbol =~ /\A [<] (\w+) [>] \z/xms ) {
249 $content_symbol = 'ELE_' . $1;
250 }
251 if ( $external_content_symbol =~ /\A [%] (\w+) \z/xms ) {
252 $content_symbol = 'GRP_' . $1;
253 }
254 $content_symbol //= $external_content_symbol;
255 my $content_entry = $symbol_table->{$content_symbol};
256 if ( not defined $content_entry ) {
257 if ( not $content_symbol =~ /\A ELE_ /xms ) {
258 problem_in_rule(
259 qq{Symbol "$external_content_symbol" is undefined\n});
260 }
261 $content_entry = [];
262 } ## end if ( not defined $content_entry )
263 $closed_reason = $content_entry->[CONTEXT_CLOSED];
264 if ($closed_reason) {
265 Carp::croak(
266 qq{Context of "$external_content_symbol" cannot be changed:\n},
267 qq{ Reason: $closed_reason\n},
268 qq{ Problem was in this line: },
269 $Marpa::R2::HTML::Config::Compile::LINE
270 );
271 } ## end if ($closed_reason)
272 push @contents, $content_symbol;
273 } ## end CONTAINED_SYMBOL: for my $external_content_symbol (@external_contents)
274
275 push @{ $element_entry->[CONTENTS] }, @contents;
276
277 return;
278
279 } ## end sub do_contains
280
281 sub do_array_assignment {
282 my ( $self, $external_list, undef, $external_members ) = @_;
283 ( my $new_list = $external_list ) =~ s/\A [@] //xms;
284 my $lists = $self->{lists};
285 Carp::croak(
286 "Problem in line: ", $Marpa::R2::HTML::Config::Compile::LINE,
287 "\n", 'list @' . $new_list . ' is already defined'
288 ) if defined $lists->{$new_list};
289 my @members = ();
290 RAW_MEMBER: for my $raw_member (@{$external_members}) {
291 if ( $raw_member =~ / \A [@] (.*) \z/xms ) {
292 my $member_list = $1;
293 Carp::croak(
294 "Problem in line: ",
295 $Marpa::R2::HTML::Config::Compile::LINE,
296 "\n",
297 'member list @' . $member_list . ' is not yet defined'
298 ) if not defined $lists->{$member_list};
299 push @members, @{ $lists->{$member_list} };
300 next RAW_MEMBER;
301 } ## end if ( $raw_member =~ / \A [@] (.*) \z/xms )
302 push @members, $raw_member;
303 } ## end RAW_MEMBER: for my $raw_member (@{$external_members})
304 $lists->{$new_list} = \@members;
305 return;
306 } ## end sub do_array_assignment
307
308 sub do_ruby_statement {
309 my ( $self, $external_reject_symbol, undef, $external_candidates ) = @_;
310 my $lists = $self->{lists};
311 my @symbols = ($external_reject_symbol);
312 RAW_CANDIDATE: for my $raw_candidate ( @{$external_candidates} ) {
313 if ( $raw_candidate =~ / \A [@] (.*) \z/xms ) {
314 my $list = $1;
315 Carp::croak(
316 "Problem in line: ",
317 $Marpa::R2::HTML::Config::Compile::LINE,
318 "\n", 'candidate list @' . $list . ' is not yet defined'
319 ) if not defined $lists->{$list};
320 push @symbols, @{ $lists->{$list} };
321 next RAW_CANDIDATE;
322 } ## end if ( $raw_candidate =~ / \A [@] (.*) \z/xms )
323 push @symbols, $raw_candidate;
324 } ## end RAW_CANDIDATE: for my $raw_candidate ( @{$external_candidates} )
325 my @internal_symbols = ();
326 SYMBOL: for my $symbol (@symbols) {
327 if ( $symbol eq 'CDATA' or $symbol eq 'PCDATA' ) {
328 push @internal_symbols, $symbol;
329 next SYMBOL;
330 }
331 if ( $symbol =~ /\A ( [<] [%] (inline|head|block) [>] ) \z/xms ) {
332 my $special_symbol = $1;
333 push @internal_symbols, $special_symbol;
334 next SYMBOL;
335 }
336 if ( $symbol =~ m{\A ( [<] [/] [%] (inline|head|block) [>] ) \z}xms )
337 {
338 my $special_symbol = $1;
339 push @internal_symbols, $special_symbol;
340 next SYMBOL;
341 } ## end if ( $symbol =~ ...)
342 if ( $symbol =~ m{\A ( [<] [*] [>] ) \z}xms ) {
343 my $special_symbol = $1;
344 push @internal_symbols, $special_symbol;
345 next SYMBOL;
346 }
347 if ( $symbol =~ m{\A ( [<] [/] [*] [>] ) \z}xms ) {
348 my $special_symbol = $1;
349 push @internal_symbols, $special_symbol;
350 next SYMBOL;
351 }
352 if ( $symbol =~ /\A [<] (\w+) [>] \z/xms ) {
353 my $start_tag = 'S_' . $1;
354 push @internal_symbols, $start_tag;
355 next SYMBOL;
356 }
357 if ( $symbol =~ m{\A [<] [/](\w+) [>] \z}xms ) {
358 my $end_tag = 'E_' . $1;
359 push @internal_symbols, $end_tag;
360 next SYMBOL;
361 }
362 Carp::croak(
363 "Problem in line: ",
364 $Marpa::R2::HTML::Config::Compile::LINE,
365 "\n", qq{Misformed symbol "$symbol"}
366 );
367 } ## end SYMBOL: for my $symbol (@symbols)
368 my $rejected_symbol = shift @internal_symbols;
369 $self->{ruby_config}->{$rejected_symbol} = \@internal_symbols;
370 return;
371 } ## end sub do_ruby_statement
372
373 sub die_on_read_problem {
374 my ( $rec, $t, $token_value, $string, $position ) = @_;
375 say $rec->show_progress() or die "say failed: $ERRNO";
376 my $problem_position = $position - length $1;
377 my $before_start = $problem_position - 40;
378 $before_start = 0 if $before_start < 0;
379 my $before_length = $problem_position - $before_start;
380 die "Problem near position $problem_position\n",
381 q{Problem is here: "},
382 ( substr $string, $before_start, $before_length + 40 ),
383 qq{"\n},
384 ( q{ } x ( $before_length + 18 ) ), qq{^\n},
385 q{Token rejected, "}, $t->[0], qq{", "$token_value"},
386 ;
387 } ## end sub die_on_read_problem
388
389 sub do_array { shift; return [@_]; }
390
391 sub do_what_I_mean {
392
393 # The first argument is the per-parse variable.
394 # At this stage, just throw it away
395 shift;
396
397 # Throw away any undef's
398 my @children = grep { defined } @_;
399
400 # Return what's left
401 return scalar @children > 1 ? \@children : shift @children;
402 }
403
404 # Order matters !!
405 my @terminals = (
406 [ kw_CDATA => qr/CDATA\b/xms ],
407 [ kw_PCDATA => qr/PCDATA\b/xms ],
408 [ kw_is => qr/is\b/ixms ],
409 [ kw_a => qr/a\b/ixms ],
410 [ kw_contains => qr/contains\b/ixms ],
411 [ kw_included => qr/included\b/ixms ],
412 [ kw_in => qr/in\b/ixms ],
413 [ flow => qr/[*]\w+\b/xms ],
414 [ group => qr/[%]\w+\b/xms ],
415 [ list => qr/[@]\w+\b/xms ],
416 [ start_tag => qr/[<]\w+[>]/xms ],
417 [ end_tag => qr{[<][/]\w+[>]}xms ],
418 [ wildcard_start_tag => qr/[<][*][>]/xms ],
419 [ wildcard_end_tag => qr{[<][/][*][>]}xms ],
420 [ group_start_tag => qr/[<][%]\w+[>]/xms ],
421 [ group_end_tag => qr/[<][%]\w+[>]/xms ],
422 [ op_assign => qr/[=]/xms ],
423 [ op_ruby => qr/[-][>]/xms ],
424 [ semi_colon => qr/[;]/xms ],
425 );
426
427 sub create_grammar {
428
429 my $source = <<'END_OF_GRAMMAR';
430 translation_unit ::= statement*
431 statement ::= is_included_statement
432 | is_a_included_statement
433 | is_statement
434 | contains_statement
435 | list_assignment
436 | ruby_statement
437 is_included_statement ::= element kw_is kw_included kw_in <group>
438 action => do_is_included_statement
439 element ::= start_tag
440 is_a_included_statement ::= element kw_is kw_a flow kw_included kw_in <group>
441 action => do_is_a_included_statement
442 is_statement ::= element kw_is flow
443 action => do_is_statement
444 contains_statement ::= element kw_contains contents
445 action => do_contains_statement
446 contents ::= content_item*
447 action => do_array
448 list_assignment ::= list op_assign list_members
449 action => do_array_assignment
450 list_members ::= list_member*
451 action => do_array
452 list_member ::= ruby_symbol
453 list_member ::= list
454 content_item ::= element | <group> | kw_PCDATA | kw_CDATA
455 ruby_statement ::= ruby_symbol op_ruby ruby_symbol_list
456 action => do_ruby_statement
457 ruby_symbol_list ::= ruby_symbol*
458 action => do_array
459 ruby_symbol ::= kw_PCDATA | kw_CDATA
460 | start_tag | group_start_tag | wildcard_start_tag
461 | end_tag | group_end_tag | wildcard_end_tag
462 | list
463 END_OF_GRAMMAR
464
465 my $grammar = Marpa::R2::Grammar->new(
466 { start => 'translation_unit',
467 action_object => __PACKAGE__,
468 rules =>$source,
469 default_action => 'do_what_I_mean'
470 }
471 );
472 $grammar->precompute();
473 return $grammar;
474 }
475
476 sub source_by_location_range {
477 my ( $self, $start, $end ) = @_;
478 my $positions = $self->{positions};
479 my $start_pos = $start > 0 ? $positions->[$start] : 0;
480 my $end_pos = $positions->[$end];
481 return substr ${ $self->{source_ref} }, $start_pos, $end_pos - $start_pos;
482 } ## end sub source_by_location_range
483
484 sub compile {
485 my ($source_ref) = @_;
486
487 # A quasi-object, not used outside this routine
488 my $self = bless {}, __PACKAGE__;
489
490 my %species_handler = (
491 cruft => 'SPE_CRUFT',
492 comment => 'SPE_COMMENT',
493 pi => 'SPE_PI',
494 decl => 'SPE_DECL',
495 document => 'SPE_TOP',
496 whitespace => 'SPE_WHITESPACE',
497 pcdata => 'SPE_PCDATA',
498 cdata => 'SPE_CDATA',
499 prolog => 'SPE_PROLOG',
500 trailer => 'SPE_TRAILER',
501 );
502
503 my @core_rules = ();
504 my %runtime_tag = ();
505 my %primary_group_by_tag = ();
506 $self->{primary_group_by_tag} = \%primary_group_by_tag;
507
508 {
509 LINE:
510 for my $line ( split /\n/xms,
511 $Marpa::R2::HTML::Internal::Core::CORE_BNF )
512 {
513 my $definition = $line;
514 chomp $definition;
515 $definition =~ s/ [#] .* //xms; # Remove comments
516 next LINE
517 if not $definition =~ / \S /xms; # ignore all-whitespace line
518 my $sequence = ( $definition =~ s/ [*] \s* $//xms );
519 if ( $definition =~ s/ \s* [:][:][=] \s* / /xms ) {
520
521 # Production is Ordinary BNF rule
522 my @symbols = ( split q{ }, $definition );
523 my $lhs = shift @symbols;
524 my %rule_descriptor = (
525 lhs => $lhs,
526 rhs => \@symbols,
527 );
528 if ($sequence) {
529 $rule_descriptor{min} = 0;
530 }
531 if ( my $handler = $species_handler{$lhs} ) {
532 $rule_descriptor{action} = $handler;
533 }
534 elsif ( $lhs =~ /^ELE_/xms ) {
535 $rule_descriptor{action} = "$lhs";
536 }
537 push @core_rules, \%rule_descriptor;
538 next LINE;
539 } ## end if ( $definition =~ s/ \s* [:][:][=] \s* / /xms )
540 die "Badly formed line in grammar description: $line";
541 } ## end LINE: for my $line ( split /\n/xms, ...)
542 }
543
544 my @core_symbols = map { ( $_->{lhs}, @{ $_->{rhs} } ) } @core_rules;
545
546 # Start out by closing the context and contents of everything
547 my %symbol_table = map {
548 $_ =>
549 [ 'Reserved by the core grammar', 'Reserved by the core grammar' ]
550 } @core_symbols;
551 $self->{symbol_table} = \%symbol_table;
552
553 # A few token symbols are allowed as contents -- most non-element
554 # tokens are included via the SGML group
555 for my $token_symbol (qw(cdata pcdata)) {
556 $symbol_table{$token_symbol}->[CONTEXT_CLOSED] = 0;
557 }
558
559 # Many groups are defined to to be used
560 for my $group_symbol (
561 qw( GRP_anywhere GRP_pcdata GRP_cdata GRP_mixed GRP_block GRP_head GRP_inline)
562 )
563 {
564 $symbol_table{$group_symbol}->[CONTEXT_CLOSED] = 0;
565 } ## end for my $group_symbol ( ...)
566
567 # Flow symbols are almost all allowed as contents
568 FLOW_SYMBOL:
569 for my $flow_symbol ( grep { $_ =~ m/\A FLO_ /xms } @core_symbols ) {
570
571 # The SGML flow is included automatically as needed
572 # and should not be explicity specified
573 next FLOW_SYMBOL if $flow_symbol eq 'FLO_SGML';
574 $symbol_table{$flow_symbol}->[CONTEXT_CLOSED] = 0;
575 } ## end for my $flow_symbol ( grep { $_ =~ m/\A FLO_ /xms } ...)
576
577 # A few groups are also extensible
578 for my $group_symbol (qw( GRP_anywhere GRP_block GRP_head GRP_inline )) {
579 $symbol_table{$group_symbol}->[CONTENTS_CLOSED] = 0;
580 }
581
582 # As very special cases the contents of the <head> and <body>
583 # elements can be changed
584 for my $element_symbol (qw( ELE_head ELE_body )) {
585 $symbol_table{$element_symbol}->[CONTENTS_CLOSED] = 0;
586 }
587
588 {
589 # Make sure everything for which we have a handler was defined in
590 # the core grammar
591 my @species_not_defined = grep { not defined $symbol_table{$_} }
592 keys %species_handler;
593 if ( scalar @species_not_defined ) {
594 die
595 'Definitions for the following required text components are missing: ',
596 join q{ }, @species_not_defined;
597 }
598 }
599
600 my %ruby_config = ();
601 my %lists = ();
602 $self->{ruby_config} = \%ruby_config;
603 $self->{lists} = \%lists;
604 $self->{source_ref} = $source_ref;
605 my @positions = (0);
606 $self->{positions} = \@positions;
607
608 state $grammar = create_grammar();
609 my $recce = Marpa::R2::Recognizer->new({ grammar => $grammar});
610 my $string = ${$source_ref};
611 my $length = length $string;
612 pos $string = 0;
613 TOKEN: while ( pos $string < $length ) {
614
615 # skip comment
616 next TOKEN if $string =~ m/\G \s* [#] [^\n]* \n/gcxms;
617
618 # skip whitespace
619 next TOKEN if $string =~ m/\G\s+/gcxms;
620
621 # read other tokens
622 TOKEN_TYPE: for my $t (@terminals) {
623 next TOKEN_TYPE if not $string =~ m/\G($t->[1])/gcxms;
624 # say join " ", $t->[0], '->', $1;
625 if ( not defined $recce->read( $t->[0], $1 ) ) {
626 die_on_read_problem( $recce, $t, $1, $string, pos $string );
627 }
628 my $latest_earley_set = $recce->latest_earley_set();
629 $positions[$latest_earley_set] = pos $string;
630 next TOKEN;
631 } ## end TOKEN_TYPE: for my $t (@terminals)
632
633 die q{No token at "}, ( substr $string, pos $string, 40 ),
634 q{", position }, pos $string;
635 } ## end TOKEN: while ( pos $string < $length )
636
637 # Value not used
638 my $parse_value_ref;
639 my $eval_ok = eval {
640
641 # Have the new() just return the current $self
642 local *new = sub { return $self };
643 $parse_value_ref = $recce->value();
644 1;
645 };
646 if ( not defined $eval_ok ) {
647 my $eval_ref_type = ref $EVAL_ERROR;
648 die $EVAL_ERROR if not $eval_ref_type;
649 if ( $eval_ref_type eq 'ARRAY' and $EVAL_ERROR->[0] eq 'rule' ) {
650 my ( undef, $message, $start, $end ) = @{$EVAL_ERROR};
651 chomp $message;
652 die $message, "\n",
653 "Rule with problem was: ",
654 $self->source_by_location_range( $start, $end ), "\n";
655 } ## end if ( $eval_ref_type eq 'ARRAY' and $EVAL_ERROR->[0] ...)
656 die "Unknown exception: ", Data::Dumper::Dumper($EVAL_ERROR);
657 } ## end if ( not defined $eval_ok )
658 if ( not defined $parse_value_ref ) {
659 die "Compile of HTML configuration failed: source did not parse";
660 }
661
662 my %sgml_flow_included = ();
663 SYMBOL: for my $element_symbol ( keys %symbol_table ) {
664 next SYMBOL if not 'ELE_' eq substr $element_symbol, 0, 4;
665 my $tag = substr $element_symbol, 4;
666 my $entry = $symbol_table{$element_symbol};
667 my $context = $entry->[CONTEXT];
668 my $contents = $entry->[CONTENTS];
669 next SYMBOL if not defined $context and not defined $contents;
670 if ( defined $context and not defined $contents ) {
671 Carp::croak(
672 qq{Element <$tag> was defined but was never given any contents}
673 );
674 }
675
676 # Contents without context are OK at this point
677 # We will check later for elements defined but not used
678 $context //= [];
679
680 # The special case where both are defined and both
681 # are scalars is for elements to be created at runtime
682 if ( not ref $context and not ref $contents ) {
683 $runtime_tag{$tag} = $contents;
684 next SYMBOL;
685 }
686
687 if ( ref $contents ) {
688 my $contents_symbol = 'Contents_ELE_' . $tag;
689 my $item_symbol = 'GRP_ELE_' . $tag;
690 push @core_rules,
691 {
692 lhs => $element_symbol,
693 rhs => [ "S_$tag", $contents_symbol, "E_$tag" ],
694 action => $element_symbol,
695 },
696 {
697 lhs => $contents_symbol,
698 rhs => [$item_symbol],
699 min => 0
700 };
701 for my $content_item ( @{$contents} ) {
702 push @core_rules,
703 {
704 lhs => $item_symbol,
705 rhs => [$content_item],
706 };
707 } ## end for my $content_item ( @{$contents} )
708 if ( !$sgml_flow_included{$item_symbol} ) {
709 $sgml_flow_included{$item_symbol} = 1;
710 push @core_rules,
711 {
712 lhs => $item_symbol,
713 rhs => ['GRP_SGML'],
714 };
715 } ## end if ( !$sgml_flow_included{$item_symbol} )
716 } ## end if ( ref $contents )
717 else {
718 push @core_rules,
719 {
720 lhs => $element_symbol,
721 rhs => [ "S_$tag", $contents, "E_$tag" ],
722 action => $element_symbol,
723 };
724 } ## end else [ if ( ref $contents ) ]
725
726 $context = [$context] if not ref $context;
727 for my $context_item ( @{$context} ) {
728 push @core_rules,
729 {
730 lhs => $context_item,
731 rhs => [$element_symbol],
732 };
733 } ## end for my $context_item ( @{$context} )
734 } ## end SYMBOL: for my $element_symbol ( keys %symbol_table )
735
736 # Finish out the Ruby Slippers configuration
737 # Make sure the last resort defaults are always defined
738 for my $required_rubies_desc (qw( <*> </*> <!element> )) {
739 $ruby_config{$required_rubies_desc} //= [];
740 }
741
742 DESC: for my $rubies_desc ( keys %ruby_config ) {
743 my $candidates = $ruby_config{$rubies_desc};
744 next DESC if grep { '</*>' eq $_ } @{$candidates};
745 $ruby_config{$rubies_desc} = [ @{$candidates}, '</*>' ];
746 }
747
748 my %is_empty_element = ();
749 {
750 for my $tag ( keys %runtime_tag ) {
751 my $contents = $runtime_tag{$tag};
752 $is_empty_element{$tag} = 1 if $contents eq 'FLO_empty';
753 }
754 RULE: for my $rule (@core_rules) {
755 my $lhs = $rule->{lhs};
756 next RULE if not 'ELE_' eq substr $lhs, 0, 4;
757 my $contents = $rule->{rhs}->[1];
758 $is_empty_element{ substr $lhs, 4 } = 1
759 if $contents eq 'FLO_empty';
760 } ## end RULE: for my $rule (@core_rules)
761 }
762
763 {
764 # Make sure no ruby candidates or rejected symbols are
765 # end tags of empty elements
766 SYMBOL: for my $rejected_symbol ( keys %ruby_config ) {
767 next SYMBOL if 'E_' ne substr $rejected_symbol, 0, 2;
768 my $tag = substr $rejected_symbol, 2;
769 next SYMBOL if not $is_empty_element{$tag};
770 Carp::croak(
771 qq{Ruby Slippers alternatives specified for </$tag>\n},
772 qq{ "$tag" is an empty element and this is not allowed"}
773 );
774 } ## end SYMBOL: for my $rejected_symbol ( keys %ruby_config )
775 SYMBOL:
776 for my $candidate_symbol ( map { @{$_} } values %ruby_config ) {
777 next SYMBOL if 'E_' ne substr $candidate_symbol, 0, 2;
778 my $tag = substr $candidate_symbol, 2;
779 next SYMBOL if not $is_empty_element{$tag};
780 Carp::croak(
781 qq{Tag </$tag> specified as a Ruby Slippers alternative\n},
782 qq{ "$tag" is an empty element and this is not allowed"}
783 );
784 } ## end for my $candidate_symbol ( map { @{$_} } values ...)
785 }
786
787 # Special case the EOF Ruby Slippers treatment
788 {
789 @{ $ruby_config{EOF} } =
790 qw( S_html S_head S_body </*> E_body E_html );
791 }
792
793 {
794 # Find the tag descriptors which refer to required
795 # elements and add them
796
797 # Required elements are those which we may have to
798 # supply even though they are not in the physical input
799
800 # Anything which has a start tag among the ruby candidates
801 # is required, since we may be required to create a
802 # non-physical one
803 my @ruby_start_tags =
804 grep { ( substr $_, 0, 2 ) eq 'S_' }
805 map { @{$_} } values %ruby_config;
806
807 my %defined_in_core_rules =
808 map { ( substr $_, 4 ) => 'core' }
809 grep {m/\A ELE_ /xms} map { $_->{lhs} } @core_rules;
810
811 my %required_tags = map { ( substr $_, 2 ) => 1 } @ruby_start_tags;
812 TAG: for my $tag ( keys %required_tags ) {
813 next TAG if $defined_in_core_rules{$tag};
814 my $flow = $runtime_tag{$tag};
815 die qq{Required element "ELE_$tag" was never defined}
816 if not defined $flow;
817 my $group = $primary_group_by_tag{$tag};
818 my $element = 'ELE_' . $tag;
819 push @core_rules,
820 {
821 lhs => $element,
822 rhs => [ "S_$tag", $flow, "E_$tag" ],
823 action => $element
824 },
825 {
826 lhs => $group,
827 rhs => [$element],
828 };
829 delete $runtime_tag{$tag};
830 } ## end TAG: for my $tag ( keys %required_tags )
831 }
832
833 {
834 my @mentioned_in_core =
835 map { substr $_, 4 }
836 grep {m/\A ELE_ /xms} map { @{ $_->{rhs} } } @core_rules;
837 my %defined_in_core =
838 map { ( substr $_, 4 ) => 'core' }
839 grep {m/\A ELE_ /xms} map { $_->{lhs} } @core_rules;
840 my @symbols_with_no_ruby_status =
841 grep { !$defined_in_core{$_} and !$runtime_tag{$_} }
842 @mentioned_in_core;
843 die 'symbols with no ruby status: ', join q{ },
844 @symbols_with_no_ruby_status
845 if scalar @symbols_with_no_ruby_status;
846 }
847
848 # Calculate the numeric Ruby ranks
849 my %ruby_rank = ();
850 for my $rejected_symbol ( keys %ruby_config ) {
851 my $rank = 1;
852 for my $candidate ( reverse @{ $ruby_config{$rejected_symbol} } ) {
853 $ruby_rank{$rejected_symbol}{$candidate} = $rank++;
854 }
855 } ## end for my $rejected_symbol ( keys %ruby_config )
856
857 {
858 my %element_used =
859 map { ( $_ => 1 ) }
860 grep {m/\A ELE_ /xms} map { @{ $_->{rhs} } } @core_rules;
861 my @elements_defined_but_not_used =
862 grep { !$element_used{$_} }
863 grep {m/\A ELE_ /xms} map { $_->{lhs} } @core_rules;
864 die 'elements defined but never used: ', join q{ },
865 @elements_defined_but_not_used
866 if scalar @elements_defined_but_not_used;
867 }
868
869 {
870 my %seen = ();
871 for my $rule (@core_rules) {
872 my $lhs = $rule->{lhs};
873 my $rhs = $rule->{rhs};
874 my $desc = join q{ }, $lhs, '::=', @{$rhs};
875 if ( $seen{$desc} ) {
876 Carp::croak("Duplicate rule: $desc");
877 }
878 $seen{$desc}++;
879 } ## end for my $rule (@core_rules)
880 }
881
882 return {
883 rules => \@core_rules,
884 runtime_tag => \%runtime_tag,
885 ruby_slippers_rank_by_name => \%ruby_rank,
886 is_empty_element => \%is_empty_element,
887 primary_group_by_tag => \%primary_group_by_tag
888 };
889
890 } ## end sub compile
891
892 1;
893
894 # vim: expandtab shiftwidth=4: