/[suikacvs]/markup/html/whatpm/Whatpm/XML/Parser.pm.src
Suika

Contents of /markup/html/whatpm/Whatpm/XML/Parser.pm.src

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download) (as text)
Tue Oct 14 05:34:05 2008 UTC (16 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +5 -2 lines
File MIME type: application/x-wais-source
++ whatpm/Whatpm/HTML/ChangeLog	14 Oct 2008 05:33:48 -0000
	* Tokenizer.pm.src: Introduced "in_xml" flag for CDATA section
	support in XML.

2008-10-14  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/XML/ChangeLog	14 Oct 2008 05:34:00 -0000
	* Parser.pm.src: Set |in_xml| flag for tokenizer.

2008-10-14  Wakaba  <wakaba@suika.fam.cx>

	* Parser.pm.src: A bug on end tag handling fixed.

2008-10-14  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 package Whatpm::XML::Parser;
2     use strict;
3    
4     push our @ISA, 'Whatpm::HTML';
5     use Whatpm::HTML::Tokenizer qw/:token/;
6    
7     sub parse_char_string ($$$;$$) {
8     #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
9     my $self = shift;
10     my $s = ref $_[0] ? $_[0] : \($_[0]);
11     require Whatpm::Charset::DecodeHandle;
12     my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
13     return $self->parse_char_stream ($input, @_[1..$#_]);
14     } # parse_char_string
15    
16     sub parse_char_stream ($$$;$$) {
17     my $self = ref $_[0] ? shift : shift->new;
18     my $input = $_[0];
19     $self->{document} = $_[1];
20     @{$self->{document}->child_nodes} = ();
21    
22     ## NOTE: |set_inner_html| copies most of this method's code
23    
24     $self->{confident} = 1 unless exists $self->{confident};
25     $self->{document}->input_encoding ($self->{input_encoding})
26     if defined $self->{input_encoding};
27     ## TODO: |{input_encoding}| is needless?
28    
29     $self->{line_prev} = $self->{line} = 1;
30     $self->{column_prev} = -1;
31     $self->{column} = 0;
32     $self->{set_nc} = sub {
33     my $self = shift;
34    
35     my $char = '';
36     if (defined $self->{next_nc}) {
37     $char = $self->{next_nc};
38     delete $self->{next_nc};
39     $self->{nc} = ord $char;
40     } else {
41     $self->{char_buffer} = '';
42     $self->{char_buffer_pos} = 0;
43    
44     my $count = $input->manakai_read_until
45     ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
46     if ($count) {
47     $self->{line_prev} = $self->{line};
48     $self->{column_prev} = $self->{column};
49     $self->{column}++;
50     $self->{nc}
51     = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
52     return;
53     }
54    
55     if ($input->read ($char, 1)) {
56     $self->{nc} = ord $char;
57     } else {
58     $self->{nc} = -1;
59     return;
60     }
61     }
62    
63     ($self->{line_prev}, $self->{column_prev})
64     = ($self->{line}, $self->{column});
65     $self->{column}++;
66    
67     if ($self->{nc} == 0x000A) { # LF
68     !!!cp ('j1');
69     $self->{line}++;
70     $self->{column} = 0;
71     } elsif ($self->{nc} == 0x000D) { # CR
72     !!!cp ('j2');
73     ## TODO: support for abort/streaming
74     my $next = '';
75     if ($input->read ($next, 1) and $next ne "\x0A") {
76     $self->{next_nc} = $next;
77     }
78     $self->{nc} = 0x000A; # LF # MUST
79     $self->{line}++;
80     $self->{column} = 0;
81     } elsif ($self->{nc} == 0x0000) { # NULL
82     !!!cp ('j4');
83     !!!parse-error (type => 'NULL');
84     $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
85     }
86     };
87    
88     $self->{read_until} = sub {
89     #my ($scalar, $specials_range, $offset) = @_;
90     return 0 if defined $self->{next_nc};
91    
92     my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
93     my $offset = $_[2] || 0;
94    
95     if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
96     pos ($self->{char_buffer}) = $self->{char_buffer_pos};
97     if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
98     substr ($_[0], $offset)
99     = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
100     my $count = $+[0] - $-[0];
101     if ($count) {
102     $self->{column} += $count;
103     $self->{char_buffer_pos} += $count;
104     $self->{line_prev} = $self->{line};
105     $self->{column_prev} = $self->{column} - 1;
106     $self->{nc} = -1;
107     }
108     return $count;
109     } else {
110     return 0;
111     }
112     } else {
113     my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
114     if ($count) {
115     $self->{column} += $count;
116     $self->{line_prev} = $self->{line};
117     $self->{column_prev} = $self->{column} - 1;
118     $self->{nc} = -1;
119     }
120     return $count;
121     }
122     }; # $self->{read_until}
123    
124     my $onerror = $_[2] || sub {
125     my (%opt) = @_;
126     my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
127     my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
128     warn "Parse error ($opt{type}) at line $line column $column\n";
129     };
130     $self->{parse_error} = sub {
131     $onerror->(line => $self->{line}, column => $self->{column}, @_);
132     };
133    
134     my $char_onerror = sub {
135     my (undef, $type, %opt) = @_;
136     !!!parse-error (layer => 'encode',
137     line => $self->{line}, column => $self->{column} + 1,
138     %opt, type => $type);
139     }; # $char_onerror
140    
141     if ($_[3]) {
142     $input = $_[3]->($input);
143     $input->onerror ($char_onerror);
144     } else {
145     $input->onerror ($char_onerror) unless defined $input->onerror;
146     }
147    
148     $self->_initialize_tokenizer;
149     $self->_initialize_tree_constructor;
150     $self->_construct_tree;
151     $self->_terminate_tree_constructor;
152    
153     delete $self->{parse_error}; # remove loop
154    
155     return $self->{document};
156     } # parse_char_stream
157    
158     sub new ($) {
159     my $class = shift;
160     my $self = bless {
161     level => {must => 'm',
162     should => 's',
163     warn => 'w',
164     info => 'i',
165     uncertain => 'u'},
166     }, $class;
167     $self->{set_nc} = sub {
168     $self->{nc} = -1;
169     };
170     $self->{parse_error} = sub {
171     #
172     };
173     $self->{change_encoding} = sub {
174     # if ($_[0] is a supported encoding) {
175     # run "change the encoding" algorithm;
176     # throw Whatpm::HTML::RestartParser (charset => $new_encoding);
177     # }
178     };
179     $self->{application_cache_selection} = sub {
180     #
181     };
182 wakaba 1.2
183     $self->{is_xml} = 1;
184    
185 wakaba 1.1 return $self;
186     } # new
187    
188     sub _initialize_tree_constructor ($) {
189     my $self = shift;
190     ## NOTE: $self->{document} MUST be specified before this method is called
191     $self->{document}->strict_error_checking (0);
192     ## TODO: Turn mutation events off # MUST
193     $self->{document}->dom_config
194     ->{'http://suika.fam.cx/www/2006/dom-config/strict-document-children'}
195     = 0;
196     $self->{document}->manakai_is_html (0);
197     $self->{document}->set_user_data (manakai_source_line => 1);
198     $self->{document}->set_user_data (manakai_source_column => 1);
199     } # _initialize_tree_constructor
200    
201     sub _terminate_tree_constructor ($) {
202     my $self = shift;
203     $self->{document}->strict_error_checking (1);
204     $self->{document}->dom_config
205     ->{'http://suika.fam.cx/www/2006/dom-config/strict-document-children'}
206     = 1;
207     ## TODO: Turn mutation events on
208     } # _terminate_tree_constructor
209    
210     ## Tree construction stage
211    
212    
213     ## NOTE: Differences from the XML5 draft are marked as "XML5:".
214    
215     ## XML5: No namespace support.
216    
217     ## XML5: XML5 has "empty tag token". In this implementation, it is
218     ## represented as a start tag token with $token->{self_closing} flag
219     ## set to true.
220    
221     ## XML5: XML5 has "short end tag token". In this implementation, it
222     ## is represented as an end tag token with $token->{tag_name} flag set
223     ## to an empty string.
224    
225     ## XML5: Start, main, end phases. In this implementation, they are
226     ## represented by insertion modes.
227    
228     ## Insertion modes
229     sub INITIAL_IM () { 0 }
230     sub BEFORE_ROOT_ELEMENT_IM () { 1 }
231     sub IN_ELEMENT_IM () { 2 }
232     sub AFTER_ROOT_ELEMENT_IM () { 3 }
233    
234     {
235     my $token; ## TODO: change to $self->{t}
236    
237     sub _construct_tree ($) {
238     my ($self) = @_;
239    
240     !!!next-token;
241    
242     delete $self->{tainted};
243     $self->{open_elements} = [];
244     $self->{insertion_mode} = INITIAL_IM;
245    
246     while (1) {
247     if ($self->{insertion_mode} == IN_ELEMENT_IM) {
248     $self->_tree_in_element;
249     } elsif ($self->{insertion_mode} == AFTER_ROOT_ELEMENT_IM) {
250     $self->_tree_after_root_element;
251     } elsif ($self->{insertion_mode} == BEFORE_ROOT_ELEMENT_IM) {
252     $self->_tree_before_root_element;
253     } elsif ($self->{insertion_mode} == INITIAL_IM) {
254     $self->_tree_initial;
255     } else {
256     die "$0: Unknown XML insertion mode: $self->{insertion_mode}";
257     }
258    
259     last if $token->{type} == ABORT_TOKEN;
260     }
261     } # _construct_tree
262    
263     sub _tree_initial ($) {
264     my $self = shift;
265    
266     B: while (1) {
267     if ($token->{type} == DOCTYPE_TOKEN) {
268     ## XML5: No "DOCTYPE" token.
269    
270     my $doctype = $self->{document}->create_document_type_definition
271     (defined $token->{name} ? $token->{name} : '');
272    
273     ## NOTE: Default value for both |public_id| and |system_id| attributes
274     ## are empty strings, so that we don't set any value in missing cases.
275     $doctype->public_id ($token->{public_identifier})
276     if defined $token->{public_identifier};
277     $doctype->system_id ($token->{system_identifier})
278     if defined $token->{system_identifier};
279    
280     ## TODO: internal_subset
281    
282     $self->{document}->append_child ($doctype);
283    
284     $self->{insertion_mode} = BEFORE_ROOT_ELEMENT_IM;
285     !!!next-token;
286     return;
287     } elsif ($token->{type} == START_TAG_TOKEN or
288     $token->{type} == END_OF_FILE_TOKEN) {
289     $self->{insertion_mode} = BEFORE_ROOT_ELEMENT_IM;
290     ## Reprocess.
291     return;
292     } elsif ($token->{type} == COMMENT_TOKEN) {
293     my $comment = $self->{document}->create_comment ($token->{data});
294     $self->{document}->append_child ($comment);
295    
296     ## Stay in the mode.
297     !!!next-token;
298     next B;
299     } elsif ($token->{type} == PI_TOKEN) {
300     my $pi = $self->{document}->create_processing_instruction
301     ($token->{target}, $token->{data});
302     $self->{document}->append_child ($pi);
303    
304     ## Stay in the mode.
305     !!!next-token;
306     next B;
307     } elsif ($token->{type} == CHARACTER_TOKEN) {
308     if (not $self->{tainted} and
309     $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
310     #
311     }
312    
313     if (length $token->{data}) {
314     ## XML5: Ignore the token.
315    
316     unless ($self->{tainted}) {
317     !!!parse-error (type => 'text outside of root element',
318     token => $token);
319     $self->{tainted} = 1;
320     }
321    
322     $self->{document}->manakai_append_text ($token->{data});
323     }
324    
325     ## Stay in the mode.
326     !!!next-token;
327     next B;
328     } elsif ($token->{type} == END_TAG_TOKEN) {
329     !!!parse-error (type => 'unmatched end tag',
330     text => $token->{tag_name},
331     token => $token);
332     ## Ignore the token.
333    
334     ## Stay in the mode.
335     !!!next-token;
336     next B;
337     } elsif ($token->{type} == ABORT_TOKEN) {
338     return;
339     } else {
340     die "$0: XML parser initial: Unknown token type $token->{type}";
341     }
342     } # B
343     } # _tree_initial
344    
345     sub _tree_before_root_element ($) {
346     my $self = shift;
347    
348     B: while (1) {
349     if ($token->{type} == START_TAG_TOKEN) {
350     my $ns; ## TODO:
351     my $el = $self->{document}->create_element_ns ($ns, $token->{tag_name});
352     $self->{document}->append_child ($el);
353    
354     if ($token->{self_closing}) {
355     !!!ack ('ack');
356     $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
357     } else {
358     push @{$self->{open_elements}}, [$el, $token->{tag_name}];
359     $self->{insertion_mode} = IN_ELEMENT_IM;
360     }
361    
362     #delete $self->{tainted};
363    
364     !!!next-token;
365     return;
366     } elsif ($token->{type} == COMMENT_TOKEN) {
367     my $comment = $self->{document}->create_comment ($token->{data});
368     $self->{document}->append_child ($comment);
369    
370     ## Stay in the mode.
371     !!!next-token;
372     next B;
373     } elsif ($token->{type} == PI_TOKEN) {
374     my $pi = $self->{document}->create_processing_instruction
375     ($token->{target}, $token->{data});
376     $self->{document}->append_child ($pi);
377    
378     ## Stay in the mode.
379     !!!next-token;
380     next B;
381     } elsif ($token->{type} == CHARACTER_TOKEN) {
382     if (not $self->{tainted} and
383     $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
384     #
385     }
386    
387     if (length $token->{data}) {
388     ## XML5: Ignore the token.
389    
390     unless ($self->{tainted}) {
391     !!!parse-error (type => 'text outside of root element',
392     token => $token);
393     $self->{tainted} = 1;
394     }
395    
396     $self->{document}->manakai_append_text ($token->{data});
397     }
398    
399     ## Stay in the mode.
400     !!!next-token;
401     next B;
402     } elsif ($token->{type} == END_OF_FILE_TOKEN) {
403     !!!parse-error (type => 'no root element',
404     token => $token);
405    
406     $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
407     ## Reprocess.
408     return;
409     } elsif ($token->{type} == END_TAG_TOKEN) {
410     !!!parse-error (type => 'unmatched end tag',
411     text => $token->{tag_name},
412     token => $token);
413     ## Ignore the token.
414    
415     ## Stay in the mode.
416     !!!next-token;
417     next B;
418     } elsif ($token->{type} == DOCTYPE_TOKEN) {
419     !!!parse-error (type => 'in html:#doctype',
420     token => $token);
421     ## Ignore the token.
422    
423     ## Stay in the mode.
424     !!!next-token;
425     next B;
426     } elsif ($token->{type} == ABORT_TOKEN) {
427     return;
428     } else {
429     die "$0: XML parser initial: Unknown token type $token->{type}";
430     }
431     } # B
432     } # _tree_before_root_element
433    
434     sub _tree_in_element ($) {
435     my $self = shift;
436    
437     B: while (1) {
438     if ($token->{type} == CHARACTER_TOKEN) {
439     $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
440    
441     ## Stay in the mode.
442     !!!next-token;
443     next B;
444     } elsif ($token->{type} == START_TAG_TOKEN) {
445     my $ns; ## TODO:
446     my $el = $self->{document}->create_element_ns ($ns, $token->{tag_name});
447     $self->{open_elements}->[-1]->[0]->append_child ($el);
448    
449     if ($token->{self_closing}) {
450     !!!ack ('ack');
451     } else {
452     push @{$self->{open_elements}}, [$el, $token->{tag_name}];
453     }
454    
455     ## Stay in the mode.
456     !!!next-token;
457     next B;
458     } elsif ($token->{type} == END_TAG_TOKEN) {
459 wakaba 1.2 if ($token->{tag_name} eq '') {
460 wakaba 1.1 ## Short end tag token.
461     pop @{$self->{open_elements}};
462     } elsif ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
463     pop @{$self->{open_elements}};
464     } else {
465     !!!parse-error (type => 'unmatched end tag',
466     text => $token->{tag_name},
467     token => $token);
468    
469     ## Has an element in scope
470 wakaba 1.2 INSCOPE: for my $i (reverse 0..$#{$self->{open_elements}}) {
471 wakaba 1.1 if ($self->{open_elements}->[$i]->[1] eq $token->{tag_name}) {
472     splice @{$self->{open_elements}}, $i;
473     last INSCOPE;
474     }
475     } # INSCOPE
476     }
477    
478     unless (@{$self->{open_elements}}) {
479     $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
480     !!!next-token;
481     return;
482     } else {
483     ## Stay in the state.
484     !!!next-token;
485     redo B;
486     }
487     } elsif ($token->{type} == COMMENT_TOKEN) {
488     my $comment = $self->{document}->create_comment ($token->{data});
489     $self->{open_elements}->[-1]->[0]->append_child ($comment);
490    
491     ## Stay in the mode.
492     !!!next-token;
493     next B;
494     } elsif ($token->{type} == PI_TOKEN) {
495     my $pi = $self->{document}->create_processing_instruction
496     ($token->{target}, $token->{data});
497     $self->{open_elements}->[-1]->[0]->append_child ($pi);
498    
499     ## Stay in the mode.
500     !!!next-token;
501     next B;
502     } elsif ($token->{type} == END_OF_FILE_TOKEN) {
503     !!!parse-error (type => 'in body:#eof',
504     token => $token);
505    
506     $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
507     !!!next-token;
508     return;
509     } elsif ($token->{type} == DOCTYPE_TOKEN) {
510     !!!parse-error (type => 'in html:#doctype',
511     token => $token);
512     ## Ignore the token.
513    
514     ## Stay in the mode.
515     !!!next-token;
516     next B;
517     } elsif ($token->{type} == ABORT_TOKEN) {
518     return;
519     } else {
520     die "$0: XML parser initial: Unknown token type $token->{type}";
521     }
522     } # B
523     } # _tree_in_element
524    
525     sub _tree_after_root_element ($) {
526     my $self = shift;
527    
528     B: while (1) {
529     if ($token->{type} == START_TAG_TOKEN) {
530     !!!parse-error (type => 'second root element',
531     token => $token);
532    
533     ## XML5: Ignore the token.
534    
535     my $ns; ## TODO:
536     my $el = $self->{document}->create_element_ns ($ns, $token->{tag_name});
537     $self->{document}->append_child ($el);
538    
539     if ($token->{self_closing}) {
540     !!!ack ('ack');
541     ## Stay in the mode.
542     } else {
543     push @{$self->{open_elements}}, [$el, $token->{tag_name}];
544     $self->{insertion_mode} = IN_ELEMENT_IM;
545     }
546    
547     #delete $self->{tainted};
548    
549     !!!next-token;
550     return;
551     } elsif ($token->{type} == COMMENT_TOKEN) {
552     my $comment = $self->{document}->create_comment ($token->{data});
553     $self->{document}->append_child ($comment);
554    
555     ## Stay in the mode.
556     !!!next-token;
557     next B;
558     } elsif ($token->{type} == PI_TOKEN) {
559     my $pi = $self->{document}->create_processing_instruction
560     ($token->{target}, $token->{data});
561     $self->{document}->append_child ($pi);
562    
563     ## Stay in the mode.
564     !!!next-token;
565     next B;
566     } elsif ($token->{type} == CHARACTER_TOKEN) {
567     if (not $self->{tainted} and
568     $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
569     #
570     }
571    
572     if (length $token->{data}) {
573     ## XML5: Ignore the token.
574    
575     unless ($self->{tainted}) {
576     !!!parse-error (type => 'text outside of root element',
577     token => $token);
578     $self->{tainted} = 1;
579     }
580    
581     $self->{document}->manakai_append_text ($token->{data});
582     }
583    
584     ## Stay in the mode.
585     !!!next-token;
586     next B;
587     } elsif ($token->{type} == END_OF_FILE_TOKEN) {
588     ## Stop parsing.
589    
590     ## TODO: implement "stop parsing".
591    
592     $token = {type => ABORT_TOKEN};
593     return;
594     } elsif ($token->{type} == END_TAG_TOKEN) {
595     !!!parse-error (type => 'unmatched end tag',
596     text => $token->{tag_name},
597     token => $token);
598     ## Ignore the token.
599    
600     ## Stay in the mode.
601     !!!next-token;
602     next B;
603     } elsif ($token->{type} == DOCTYPE_TOKEN) {
604     !!!parse-error (type => 'in html:#doctype',
605     token => $token);
606     ## Ignore the token.
607    
608     ## Stay in the mode.
609     !!!next-token;
610     next B;
611     } elsif ($token->{type} == ABORT_TOKEN) {
612     return;
613     } else {
614     die "$0: XML parser initial: Unknown token type $token->{type}";
615     }
616     } # B
617     } # _tree_after_root_element
618    
619     }
620    
621     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24