| 1 | package PPI::Token::BOM; | ||||||||||
| 2 | |||||||||||
| 3 | =pod | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | PPI::Token::BOM - Tokens representing Unicode byte order marks | ||||||||||
| 8 | |||||||||||
| 9 | =head1 INHERITANCE | ||||||||||
| 10 | |||||||||||
| 11 | PPI::Token::BOM | ||||||||||
| 12 | isa PPI::Token | ||||||||||
| 13 | isa PPI::Element | ||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | This is a special token in that it can only occur at the beginning of | ||||||||||
| 18 | documents. If a BOM byte mark occurs elsewhere in a file, it should | ||||||||||
| 19 | be treated as L<PPI::Token::Whitespace>. We recognize the byte order | ||||||||||
| 20 | marks identified at this URL: | ||||||||||
| 21 | L<http://www.unicode.org/faq/utf_bom.html#BOM> | ||||||||||
| 22 | |||||||||||
| 23 | UTF-32, big-endian 00 00 FE FF | ||||||||||
| 24 | UTF-32, little-endian FF FE 00 00 | ||||||||||
| 25 | UTF-16, big-endian FE FF | ||||||||||
| 26 | UTF-16, little-endian FF FE | ||||||||||
| 27 | UTF-8 EF BB BF | ||||||||||
| 28 | |||||||||||
| 29 | Note that as of this writing, PPI only has support for UTF-8 | ||||||||||
| 30 | (namely, in POD and strings) and no support for UTF-16 or UTF-32. We | ||||||||||
| 31 | support the BOMs of the latter two for completeness only. | ||||||||||
| 32 | |||||||||||
| 33 | The BOM is considered non-significant, like white space. | ||||||||||
| 34 | |||||||||||
| 35 | =head1 METHODS | ||||||||||
| 36 | |||||||||||
| 37 | There are no additional methods beyond those provided by the parent | ||||||||||
| 38 | L<PPI::Token> and L<PPI::Element> classes. | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | 62 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 62 | ▶ | use PPI::Token (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | 62 | ▶ | use vars qw{$VERSION @ISA}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | BEGIN { | ||||||||||
| 47 | 62 | ▶ | $VERSION = '1.236'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 62 | @ISA = 'PPI::Token'; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | sub significant() { '' } | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | ##################################################################### | ||||||||||
| 58 | # Parsing Methods | ||||||||||
| 59 | |||||||||||
| 60 | 62 | my %bom_types = ( | |||||||||
| 61 | "\x00\x00\xfe\xff" => 'UTF-32', | ||||||||||
| 62 | "\xff\xfe\x00\x00" => 'UTF-32', | ||||||||||
| 63 | "\xfe\xff" => 'UTF-16', | ||||||||||
| 64 | "\xff\xfe" => 'UTF-16', | ||||||||||
| 65 | "\xef\xbb\xbf" => 'UTF-8', | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | sub __TOKENIZER__on_line_start { | ||||||||||
| 69 | 15740 | ▶ | my $t = $_[1]; | ||||||||
|
Subroutine
__TOKENIZER__on_line_start: called CC=4 SCAR=14.1
| |||||||||||
| 70 | 15740 | $_ = $t->{line}; | |||||||||
| 71 | |||||||||||
| 72 | 15740 | ▶ | if (m/^(\x00\x00\xfe\xff | # UTF-32, big-endian | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | \xff\xfe\x00\x00 | # UTF-32, little-endian | ||||||||||
| 74 | \xfe\xff | # UTF-16, big-endian | ||||||||||
| 75 | \xff\xfe | # UTF-16, little-endian | ||||||||||
| 76 | \xef\xbb\xbf) # UTF-8 | ||||||||||
| 77 | /xs) { | ||||||||||
| 78 | 2 | my $bom = $1; | |||||||||
| 79 | |||||||||||
| 80 | 2 | ▶ | if ($bom_types{$bom} ne 'UTF-8') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | return $t->_error("$bom_types{$bom} is not supported"); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 2 | ▶ | $t->_new_token('BOM', $bom) or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 2 | $t->{line_cursor} += length $bom; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | # Continue just as if there was no BOM | ||||||||||
| 89 | 15740 | $t->{class} = 'PPI::Token::Whitespace'; | |||||||||
| 90 | 15740 | return $t->{class}->__TOKENIZER__on_line_start($t); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 62 | 1; | |||||||||
| 94 | |||||||||||
| 95 | =pod | ||||||||||
| 96 | |||||||||||
| 97 | =head1 SUPPORT | ||||||||||
| 98 | |||||||||||
| 99 | See the L<support section|PPI/SUPPORT> in the main module | ||||||||||
| 100 | |||||||||||
| 101 | =head1 AUTHOR | ||||||||||
| 102 | |||||||||||
| 103 | Chris Dolan E<lt>cdolan@cpan.orgE<gt> | ||||||||||
| 104 | |||||||||||
| 105 | =head1 COPYRIGHT | ||||||||||
| 106 | |||||||||||
| 107 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||
| 108 | |||||||||||
| 109 | This program is free software; you can redistribute | ||||||||||
| 110 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 111 | |||||||||||
| 112 | The full text of the license can be found in the | ||||||||||
| 113 | LICENSE file included with this module. | ||||||||||
| 114 | |||||||||||
| 115 | =cut | ||||||||||