Debian Codegraph @

html/lib/Marpa/R2/HTML/Callback.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 use 5.010001;
17 use warnings;
18 use strict;
19
20 package Marpa::R2::HTML::Callback;
21
22 use vars qw( $VERSION $STRING_VERSION );
23 $VERSION = '14.000000';
24 $STRING_VERSION = $VERSION;
25 ## use critic (BuiltinFunctions::ProhibitStringyEval)
26 $VERSION = eval $VERSION;
27 ## no critic
28
29 package Marpa::R2::HTML::Internal::Callback;
30
31 use English qw( -no_match_vars );
32
33 sub Marpa::R2::HTML::start_tag {
34
35 my $parse_instance = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
36 Marpa::R2::exception(q{Attempt to fetch start tag outside of a parse})
37 if not defined $parse_instance;
38 return undef if not defined $Marpa::R2::HTML::Internal::START_TAG_IX;
39
40 return ${
41 Marpa::R2::HTML::Internal::token_range_to_original(
42 $parse_instance,
43 $Marpa::R2::HTML::Internal::START_TAG_IX,
44 $Marpa::R2::HTML::Internal::START_TAG_IX
45 )
46 };
47
48 } ## end sub Marpa::R2::HTML::start_tag
49
50 # We do not always need the end tag, so it is found lazily,
51 # unlike the start tag which is determined eagerly.
52 # Return the end token ix, or undef if none.
53 # As a side effect set $Marpa::R2::HTML::Internal::END_TAG_IX_REF
54 # to be a reference to that result
55 sub end_tag_set {
56
57 my $parse_instance = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
58
59 # be idempotent -- but this is probably unnecessary
60 return ${$Marpa::R2::HTML::Internal::END_TAG_IX_REF}
61 if defined $Marpa::R2::HTML::Internal::END_TAG_IX_REF;
62
63 # default to no end tag
64 $Marpa::R2::HTML::Internal::END_TAG_IX_REF = \undef;
65
66 # return undef if the current rule is not an element
67 return undef if not $Marpa::R2::HTML::Internal::ELEMENT;
68
69 my $arg_n = $Marpa::R2::HTML::Internal::ARG_N;
70 my $end_tag_tdesc_item = $Marpa::R2::HTML::Internal::STACK->[$arg_n];
71 my $end_tag_type = $end_tag_tdesc_item->[0];
72
73 return undef if not defined $end_tag_type;
74 return undef if $end_tag_type ne 'PHYSICAL_TOKEN';
75
76 my $end_tag_token_ix =
77 $end_tag_tdesc_item->[Marpa::R2::HTML::Internal::TDesc::END_TOKEN];
78 my $tokens = $parse_instance->{tokens};
79 my $html_token = $tokens->[$end_tag_token_ix];
80 my $html_token_type =
81 $html_token->[Marpa::R2::HTML::Internal::Token::TYPE];
82 return undef if $html_token_type ne 'E';
83 $Marpa::R2::HTML::Internal::END_TAG_IX_REF = \$end_tag_token_ix;
84 return $end_tag_token_ix;
85
86 } ## end sub end_tag_set
87
88 sub Marpa::R2::HTML::end_tag {
89
90 my $parse_instance = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
91 Marpa::R2::exception(q{Attempt to fetch an end tag outside of a parse})
92 if not defined $parse_instance;
93 defined $Marpa::R2::HTML::Internal::END_TAG_IX_REF or end_tag_set();
94 my $end_tag_token_ix = ${$Marpa::R2::HTML::Internal::END_TAG_IX_REF};
95 return undef if not defined $end_tag_token_ix;
96
97 return ${
98 Marpa::R2::HTML::Internal::token_range_to_original( $parse_instance,
99 $end_tag_token_ix, $end_tag_token_ix, )
100 };
101 } ## end sub Marpa::R2::HTML::end_tag
102
103 sub Marpa::R2::HTML::contents {
104
105 my $self = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
106 Marpa::R2::exception(
107 q{Attempt to fetch an element contents outside of a parse})
108 if not defined $self;
109
110 return undef if not $Marpa::R2::HTML::Internal::ELEMENT;
111
112 my $contents_start_ix =
113 defined $Marpa::R2::HTML::Internal::START_TAG_IX
114 ? $Marpa::R2::HTML::Internal::START_TAG_IX + 1
115 : $Marpa::R2::HTML::Internal::START_HTML_TOKEN_IX;
116 defined $Marpa::R2::HTML::Internal::END_TAG_IX_REF or end_tag_set();
117 my $end_tag_token_ix = ${$Marpa::R2::HTML::Internal::END_TAG_IX_REF};
118 my $contents_end_ix =
119 defined $end_tag_token_ix
120 ? $end_tag_token_ix + 1
121 : $Marpa::R2::HTML::Internal::END_HTML_TOKEN_IX;
122
123 # An element does not necessarily have any tokens
124 return q{} if not defined $contents_start_ix;
125
126 my $content_values = [
127 @{$Marpa::R2::HTML::Internal::STACK}[
128 ( $Marpa::R2::HTML::Internal::ARG_0 + 1 )
129 .. ( $Marpa::R2::HTML::Internal::ARG_N - 1 )
130 ]
131 ];
132 return ${
133 Marpa::R2::HTML::Internal::range_and_values_to_literal( $self,
134 $contents_start_ix, $contents_end_ix, $content_values )
135 };
136
137 } ## end sub Marpa::R2::HTML::contents
138
139 sub Marpa::R2::HTML::values {
140
141 my $parse_instance = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
142 Marpa::R2::exception(q{Attempt to examine rule while not in a parse})
143 if not defined $parse_instance;
144
145 my @flat_tdesc_list = ();
146 STACK_IX:
147 for my $stack_ix (
148 $Marpa::R2::HTML::Internal::ARG_0 .. $Marpa::R2::HTML::Internal::ARG_N
149 )
150 {
151 my $tdesc_item = $Marpa::R2::HTML::Internal::STACK->[$stack_ix];
152 my $type = $tdesc_item->[0];
153 next STACK_IX if not defined $type;
154 if ( $type eq 'VALUES' ) {
155 push @flat_tdesc_list,
156 @{ $tdesc_item->[Marpa::R2::HTML::Internal::TDesc::VALUE] };
157 next STACK_IX;
158 }
159 if ( $type eq 'VALUED_SPAN' ) {
160 push @flat_tdesc_list, $tdesc_item;
161 next STACK_IX;
162 }
163 } ## end STACK_IX: for my $stack_ix ( $Marpa::R2::HTML::Internal::ARG_0 ...)
164
165 return [ grep {defined}
166 map { $_->[Marpa::R2::HTML::Internal::TDesc::VALUE] }
167 @flat_tdesc_list ];
168 } ## end sub Marpa::R2::HTML::values
169
170 sub Marpa::R2::HTML::descendants {
171
172 my ($argspecs) = @_;
173
174 my $parse_instance = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
175 Marpa::R2::exception(q{Attempt to fetch an end tag outside of a parse})
176 if not defined $parse_instance;
177 my $tokens = $parse_instance->{tokens};
178
179 my @argspecs = ();
180 for my $argspec ( split /,/xms, $argspecs ) {
181 $argspec =~ s/\A \s* //xms;
182 $argspec =~ s/ \s* \z//xms;
183 push @argspecs, $argspec;
184 }
185
186 my @flat_tdesc_list = ();
187 STACK_IX:
188 for my $stack_ix (
189 $Marpa::R2::HTML::Internal::ARG_0 .. $Marpa::R2::HTML::Internal::ARG_N
190 )
191 {
192 my $tdesc_item = $Marpa::R2::HTML::Internal::STACK->[$stack_ix];
193
194 my $type = $tdesc_item->[0];
195 next STACK_IX if not defined $type;
196 next STACK_IX if $type eq 'ZERO_SPAN';
197 next STACK_IX if $type eq 'RUBY_SLIPPERS_TOKEN';
198 if ( $type eq 'VALUES' ) {
199 push @flat_tdesc_list,
200 @{ $tdesc_item->[Marpa::R2::HTML::Internal::TDesc::VALUE] };
201 next STACK_IX;
202 }
203 push @flat_tdesc_list, $tdesc_item;
204 } ## end STACK_IX: for my $stack_ix ( $Marpa::R2::HTML::Internal::ARG_0 ...)
205
206 my $next_token_ix = $Marpa::R2::HTML::Internal::START_HTML_TOKEN_IX;
207 my $final_token_ix = $Marpa::R2::HTML::Internal::END_HTML_TOKEN_IX;
208
209 my @descendants = ();
210 TDESC_ITEM: for my $tdesc_item (@flat_tdesc_list) {
211 my ( $tdesc_item_type, $next_explicit_token_ix,
212 $furthest_explicit_token_ix )
213 = @{$tdesc_item};
214
215 if (not defined $next_explicit_token_ix) {
216 ## An element can contain no HTML tokens -- it may contain
217 ## only Ruby Slippers tokens.
218 ## Treat this as a special case.
219 if ( $tdesc_item_type eq 'VALUED_SPAN'
220 and defined $tdesc_item->[Marpa::R2::HTML::Internal::TDesc::VALUE]
221 )
222 {
223 push @descendants, [ 1, $tdesc_item ];
224 }
225 next TDESC_ITEM;
226 }
227
228 push @descendants,
229 map { [ 0, $_ ] }
230 ( $next_token_ix .. $next_explicit_token_ix - 1 );
231 if ( $tdesc_item_type eq 'VALUED_SPAN'
232 and defined $tdesc_item->[Marpa::R2::HTML::Internal::TDesc::VALUE]
233 )
234 {
235 push @descendants, [ 1, $tdesc_item ];
236 $next_token_ix = $furthest_explicit_token_ix + 1;
237 next TDESC_ITEM;
238 } ## end if ( $tdesc_item_type eq 'VALUED_SPAN' and defined ...)
239 push @descendants,
240 map { [ 0, $_ ] }
241 ( $next_explicit_token_ix .. $furthest_explicit_token_ix );
242 $next_token_ix = $furthest_explicit_token_ix + 1;
243 } ## end TDESC_ITEM: for my $tdesc_item (@flat_tdesc_list)
244
245 my @results;
246 DESCENDANT: for my $descendant (@descendants) {
247 my @per_descendant_results = ();
248 my ( $is_valued, $data ) = @{$descendant};
249 ARGSPEC: for my $argspec_ix ( 0 .. $#argspecs ) {
250 ## Work with a copy, so we can change it
251 my $argspec = $argspecs[$argspec_ix];
252 my $deref = 1;
253 if ( $argspec =~ s/_ref\z//xms ) {
254 $deref = 0;
255 }
256 if ( $argspec eq 'literal' ) {
257 if ($is_valued) {
258 push @per_descendant_results,
259 q{}
260 . $data->[Marpa::R2::HTML::Internal::TDesc::VALUE];
261 next ARGSPEC;
262 } ## end if ($is_valued)
263 $argspec = 'original';
264 ## FALL THROUGH
265 } ## end if ( $argspec eq 'literal' )
266 if ( $argspec eq 'value' ) {
267 my $value =
268 $is_valued
269 ? $data->[Marpa::R2::HTML::Internal::TDesc::VALUE]
270 : undef;
271
272 push @per_descendant_results, $value;
273 next ARGSPEC;
274 } ## end if ( $argspec eq 'value' )
275 if ( $argspec eq 'original' ) {
276 my ( $start_ix, $end_ix ) =
277 $is_valued
278 ? (
279 @{$data}[
280 Marpa::R2::HTML::Internal::TDesc::START_TOKEN,
281 Marpa::R2::HTML::Internal::TDesc::END_TOKEN
282 ]
283 )
284 : ( $data, $data );
285 my $result =
286 Marpa::R2::HTML::Internal::token_range_to_original(
287 $parse_instance, $start_ix, $end_ix );
288 $result = ${$result} if $deref;
289 push @per_descendant_results, $result;
290 next ARGSPEC;
291 } ## end if ( $argspec eq 'original' )
292 if ( $argspec eq 'token_type' ) {
293 if ($is_valued) {
294 push @per_descendant_results, undef;
295 next ARGSPEC;
296 }
297 my $token_ix = $data;
298 my $html_token = $tokens->[$token_ix];
299 push @per_descendant_results,
300 $html_token->[Marpa::R2::HTML::Internal::Token::TYPE];
301 next ARGSPEC;
302 } ## end if ( $argspec eq 'token_type' )
303 if ( $argspec eq 'element' ) {
304 if ( not $is_valued ) {
305 push @per_descendant_results, undef;
306 next ARGSPEC;
307 }
308 my $rule_id =
309 $data->[Marpa::R2::HTML::Internal::TDesc::RULE_ID];
310 my $action =
311 $parse_instance->{action_by_rule_id}->[$rule_id];
312 if ( not defined $action ) {
313 push @per_descendant_results, undef;
314 next ARGSPEC;
315 }
316 if ( ( index $action, 'ELE_' ) != 0 ) {
317 push @per_descendant_results, undef;
318 next ARGSPEC;
319 }
320 push @per_descendant_results, ( substr $action, 4 );
321 next ARGSPEC;
322 } ## end if ( $argspec eq 'element' )
323 if ( $argspec eq 'pseudoclass' ) {
324 ## This argspec needs to be better defined/implemented
325 ## As of VERSION 2.021_000 it has been removed
326 ## from the documentation.
327 if ( not $is_valued ) {
328 push @per_descendant_results, undef;
329 next ARGSPEC;
330 }
331 my $rule_id =
332 $data->[Marpa::R2::HTML::Internal::TDesc::RULE_ID];
333 my $action =
334 $parse_instance->{action_by_rule_id}->[$rule_id];
335 if ( not defined $action ) {
336 push @per_descendant_results, undef;
337 next ARGSPEC;
338 }
339 if ( ( index $action, 'SPE_' ) != 0 ) {
340 push @per_descendant_results, undef;
341 next ARGSPEC;
342 }
343 push @per_descendant_results, ( substr $action, 4 );
344 push @per_descendant_results, $action;
345 next ARGSPEC;
346 } ## end if ( $argspec eq 'pseudoclass' )
347 die "Unimplemented argspec: $argspec";
348
349 } ## end ARGSPEC: for my $argspec_ix ( 0 .. $#argspecs )
350 push @results, \@per_descendant_results;
351 } ## end CHILD: for my $child (@children)
352
353 return \@results;
354 } ## end sub Marpa::R2::HTML::descendants
355
356 sub Marpa::R2::HTML::attributes {
357 return $Marpa::R2::HTML::Internal::ATTRIBUTES // {};
358 } ## end sub Marpa::R2::HTML::attributes
359
360 # This assumes that a start token, if there is one
361 # with attributes, is the first token
362 sub create_fetch_attribute_closure {
363 my ($attribute) = @_;
364 return sub {
365 my $attributes = $Marpa::R2::HTML::Internal::ATTRIBUTES;
366 return undef if not defined $attributes;
367 return $attributes->{$attribute};
368 };
369 } ## end sub create_fetch_attribute_closure
370
371 no strict 'refs';
372 *{'Marpa::R2::HTML::id'} = create_fetch_attribute_closure('id');
373 *{'Marpa::R2::HTML::class'} = create_fetch_attribute_closure('class');
374 *{'Marpa::R2::HTML::title'} = create_fetch_attribute_closure('title');
375 use strict;
376
377 package Marpa::R2::HTML::Internal::Callback;
378
379 sub Marpa::R2::HTML::tagname {
380 return $Marpa::R2::HTML::Internal::ELEMENT;
381 }
382
383 sub Marpa::R2::HTML::is_empty_element {
384 return undef if not defined $Marpa::R2::HTML::Internal::ELEMENT;
385 my $self = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
386 return $self->{is_empty_element}->{$Marpa::R2::HTML::Internal::ELEMENT};
387 }
388
389 sub Marpa::R2::HTML::species {
390 return $Marpa::R2::HTML::Internal::SPECIES;
391 }
392
393 sub Marpa::R2::HTML::literal_ref {
394
395 my $self = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
396 Marpa::R2::exception(
397 q{Attempt to fetch an element contents outside of a parse})
398 if not defined $self;
399
400 my $contents_start_ix = $Marpa::R2::HTML::Internal::START_HTML_TOKEN_IX;
401
402 # A rule does not necessarily have any tokens
403 return \q{} if not defined $contents_start_ix;
404
405 my $contents_end_ix = $Marpa::R2::HTML::Internal::END_HTML_TOKEN_IX;
406
407 my $content_values = [
408 @{$Marpa::R2::HTML::Internal::STACK}[
409 ($Marpa::R2::HTML::Internal::ARG_0)
410 .. ($Marpa::R2::HTML::Internal::ARG_N)
411 ]
412 ];
413 return Marpa::R2::HTML::Internal::range_and_values_to_literal( $self,
414 $contents_start_ix, $contents_end_ix, $content_values );
415
416 } ## end sub Marpa::R2::HTML::literal_ref
417
418 sub Marpa::R2::HTML::literal {
419 return ${Marpa::R2::HTML::literal_ref()};
420 } ## end sub Marpa::R2::HTML::literal
421
422 sub Marpa::R2::HTML::offset {
423 my $self = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
424 my $start_token_ix = $Marpa::R2::HTML::Internal::START_HTML_TOKEN_IX;
425 return undef if not defined $start_token_ix;
426 my $tokens = $self->{tokens};
427 return $tokens->[$start_token_ix]
428 ->[Marpa::R2::HTML::Internal::Token::START_OFFSET];
429 } ## end sub Marpa::R2::HTML::offset
430
431 sub Marpa::R2::HTML::token_type {
432 my $self = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
433 my $start_token_ix = $Marpa::R2::HTML::Internal::START_HTML_TOKEN_IX;
434 return undef if not defined $start_token_ix;
435 my $tokens = $self->{tokens};
436 return $tokens->[$start_token_ix]
437 ->[Marpa::R2::HTML::Internal::Token::TYPE];
438 } ## end sub Marpa::R2::HTML::token_type
439
440 sub Marpa::R2::HTML::original_ref {
441
442 my $self = $Marpa::R2::HTML::Internal::PARSE_INSTANCE;
443 Marpa::R2::exception(q{Attempt to look at a rule while not in a parse})
444 if not defined $self;
445
446 my $start_token_ix = $Marpa::R2::HTML::Internal::START_HTML_TOKEN_IX;
447
448 # An rule does not necessarily have any HTML tokens
449 return q{} if not defined $start_token_ix;
450
451 return Marpa::R2::HTML::Internal::token_range_to_original( $self,
452 $start_token_ix, $Marpa::R2::HTML::Internal::END_HTML_TOKEN_IX );
453
454 } ## end sub Marpa::R2::HTML::original_ref
455
456 sub Marpa::R2::HTML::original {
457 return ${Marpa::R2::HTML::original_ref()};
458 }
459
460 1;
461
462 # vim: set expandtab shiftwidth=4: