| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package MarpaX::Languages::Perl::PackUnpack; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 580 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 4 | 1 |  |  | 1 |  | 518 | use utf8; | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 5 | 1 |  |  | 1 |  | 32 | use warnings; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 6 | 1 |  |  | 1 |  | 4 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 37 |  | 
| 7 | 1 |  |  | 1 |  | 444 | use open     qw(:std :utf8); # Undeclared streams in UTF-8. | 
|  | 1 |  |  |  |  | 984 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 109 | use Config; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 52 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  |  |  | 9 | use Const::Exporter constants => | 
| 12 |  |  |  |  |  |  | [ | 
| 13 |  |  |  |  |  |  | nothing_is_fatal   => 0, # The default. | 
| 14 |  |  |  |  |  |  | debug              => 1, | 
| 15 |  |  |  |  |  |  | print_warnings     => 2, | 
| 16 |  |  |  |  |  |  | ambiguity_is_fatal => 4, | 
| 17 | 1 |  |  | 1 |  | 489 | ]; | 
|  | 1 |  |  |  |  | 14432 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 |  |  | 1 |  | 1300 | use Marpa::R2; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | use Moo; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | use Tree; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | use Types::Standard qw/Any ArrayRef HashRef Int Str/; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | use Try::Tiny; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | has bnf => | 
| 30 |  |  |  |  |  |  | ( | 
| 31 |  |  |  |  |  |  | default  => sub{return ''}, | 
| 32 |  |  |  |  |  |  | is       => 'rw', | 
| 33 |  |  |  |  |  |  | isa      => Any, | 
| 34 |  |  |  |  |  |  | required => 0, | 
| 35 |  |  |  |  |  |  | ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | has error_message => | 
| 38 |  |  |  |  |  |  | ( | 
| 39 |  |  |  |  |  |  | default  => sub{return ''}, | 
| 40 |  |  |  |  |  |  | is       => 'rw', | 
| 41 |  |  |  |  |  |  | isa      => Str, | 
| 42 |  |  |  |  |  |  | required => 0, | 
| 43 |  |  |  |  |  |  | ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | has error_number => | 
| 46 |  |  |  |  |  |  | ( | 
| 47 |  |  |  |  |  |  | default  => sub{return 0}, | 
| 48 |  |  |  |  |  |  | is       => 'rw', | 
| 49 |  |  |  |  |  |  | isa      => Int, | 
| 50 |  |  |  |  |  |  | required => 0, | 
| 51 |  |  |  |  |  |  | ); | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | has grammar => | 
| 54 |  |  |  |  |  |  | ( | 
| 55 |  |  |  |  |  |  | default  => sub {return ''}, | 
| 56 |  |  |  |  |  |  | is       => 'rw', | 
| 57 |  |  |  |  |  |  | isa      => Any, | 
| 58 |  |  |  |  |  |  | required => 0, | 
| 59 |  |  |  |  |  |  | ); | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | has known_events => | 
| 62 |  |  |  |  |  |  | ( | 
| 63 |  |  |  |  |  |  | default  => sub{return {} }, | 
| 64 |  |  |  |  |  |  | is       => 'rw', | 
| 65 |  |  |  |  |  |  | isa      => HashRef, | 
| 66 |  |  |  |  |  |  | required => 0, | 
| 67 |  |  |  |  |  |  | ); | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | has next_few_limit => | 
| 70 |  |  |  |  |  |  | ( | 
| 71 |  |  |  |  |  |  | default  => sub{return 20}, | 
| 72 |  |  |  |  |  |  | is       => 'rw', | 
| 73 |  |  |  |  |  |  | isa      => Int, | 
| 74 |  |  |  |  |  |  | required => 0, | 
| 75 |  |  |  |  |  |  | ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | has options => | 
| 78 |  |  |  |  |  |  | ( | 
| 79 |  |  |  |  |  |  | default  => sub{return 0}, | 
| 80 |  |  |  |  |  |  | is       => 'rw', | 
| 81 |  |  |  |  |  |  | isa      => Int, | 
| 82 |  |  |  |  |  |  | required => 0, | 
| 83 |  |  |  |  |  |  | ); | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | has recce => | 
| 86 |  |  |  |  |  |  | ( | 
| 87 |  |  |  |  |  |  | default  => sub{return ''}, | 
| 88 |  |  |  |  |  |  | is       => 'rw', | 
| 89 |  |  |  |  |  |  | isa      => Any, | 
| 90 |  |  |  |  |  |  | required => 0, | 
| 91 |  |  |  |  |  |  | ); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | has stack => | 
| 94 |  |  |  |  |  |  | ( | 
| 95 |  |  |  |  |  |  | default  => sub{return []}, | 
| 96 |  |  |  |  |  |  | is       => 'rw', | 
| 97 |  |  |  |  |  |  | isa      => ArrayRef, | 
| 98 |  |  |  |  |  |  | required => 0, | 
| 99 |  |  |  |  |  |  | ); | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | has template => | 
| 102 |  |  |  |  |  |  | ( | 
| 103 |  |  |  |  |  |  | default  => sub{return ''}, | 
| 104 |  |  |  |  |  |  | is       => 'rw', | 
| 105 |  |  |  |  |  |  | isa      => Str, | 
| 106 |  |  |  |  |  |  | required => 0, | 
| 107 |  |  |  |  |  |  | ); | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | has tree => | 
| 110 |  |  |  |  |  |  | ( | 
| 111 |  |  |  |  |  |  | default  => sub{return ''}, | 
| 112 |  |  |  |  |  |  | is       => 'rw', | 
| 113 |  |  |  |  |  |  | isa      => Any, | 
| 114 |  |  |  |  |  |  | required => 0, | 
| 115 |  |  |  |  |  |  | ); | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | our $VERSION = '1.00'; | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | sub BUILD | 
| 122 |  |  |  |  |  |  | { | 
| 123 |  |  |  |  |  |  | my($self) = @_; | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | # Policy: Event names are always the same as the name of the corresponding lexeme. | 
| 126 |  |  |  |  |  |  | # | 
| 127 |  |  |  |  |  |  | # Pack syntax reference: http://perldoc.perl.org/functions/pack.html. | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | my($bnf) = <<'END_OF_GRAMMAR'; | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | :default				::= action => [values] | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | lexeme default			= latm => 1 | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | :start					::= template | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | template				::= item+ | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | # The reason for the rank clauses is to handle cases like 'j'. Because that letter is in | 
| 140 |  |  |  |  |  |  | # both bang_or_endian_set and endian_only_set, the parse is ambiguous, and returns a forest | 
| 141 |  |  |  |  |  |  | # when there is no rank. In such a case, you get this: | 
| 142 |  |  |  |  |  |  | #	Error: Parse failed. value() called when recognizer is not in tree mode | 
| 143 |  |  |  |  |  |  | #	  The current mode is "forest" | 
| 144 |  |  |  |  |  |  | # Since here is does not matter which way we jump, I've arbitrarily used ranks 1 .. 4. | 
| 145 |  |  |  |  |  |  | # AFAIK any ranks will do, as long as they are different. | 
| 146 |  |  |  |  |  |  | # BTW: These ranks only work because I've used (ranking_method => 'high_rule_only') | 
| 147 |  |  |  |  |  |  | # in the call to the constructor Marpa::R2::Scanless::R -> new(). | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | item					::= prefix character | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | prefix					::= | 
| 152 |  |  |  |  |  |  | prefix					::= percent_literal number | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | character				::= basic_set										repeat_token	rank => 1 | 
| 155 |  |  |  |  |  |  | | bang_only_set			bang_token				repeat_token	rank => 2 | 
| 156 |  |  |  |  |  |  | | bang_or_endian_set	bang_or_endian_token	repeat_token	rank => 3 | 
| 157 |  |  |  |  |  |  | | endian_only_set		endian_token			repeat_token	rank => 4 | 
| 158 |  |  |  |  |  |  | | parentheses_set								repeat_special	rank => 5 | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | repeat_token			::= repeat_item | 
| 161 |  |  |  |  |  |  | | repeat_item slash_literal repeat_item | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | repeat_item				::= repeat_count | 
| 164 |  |  |  |  |  |  | repeat_item				::= open_bracket repeat_flag close_bracket | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | repeat_count			::= repeat_number | 
| 167 |  |  |  |  |  |  | | star | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | repeat_number			::= | 
| 170 |  |  |  |  |  |  | repeat_number			::= number | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | repeat_flag				::= repeat_count | 
| 173 |  |  |  |  |  |  | | character | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | repeat_special			::= repeat_number endian_token | 
| 176 |  |  |  |  |  |  | | endian_token repeat_number | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | bang_token				::= | 
| 179 |  |  |  |  |  |  | bang_token				::= bang_literal | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | bang_or_endian_set		::= bang_only_set | 
| 182 |  |  |  |  |  |  | | bang_and_endian_set | 
| 183 |  |  |  |  |  |  | | endian_only_set | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | bang_or_endian_token	::= | 
| 186 |  |  |  |  |  |  | bang_or_endian_token	::= bang_literal | 
| 187 |  |  |  |  |  |  | | endian_literal | 
| 188 |  |  |  |  |  |  | | bang_endian_literal | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | endian_token			::= | 
| 191 |  |  |  |  |  |  | endian_token			::= endian_literal | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | # Lexemes in alphabetical order. | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | :lexeme					~ bang_and_endian_set	pause => before		event => bang_and_endian_set | 
| 196 |  |  |  |  |  |  | bang_and_endian_set		~ [sSiIlL] | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | :lexeme					~ bang_endian_literal	pause => before		event => bang_endian_literal | 
| 199 |  |  |  |  |  |  | bang_endian_literal		~ '!<' | 
| 200 |  |  |  |  |  |  | bang_endian_literal		~ '!>' | 
| 201 |  |  |  |  |  |  | bang_endian_literal		~ ' | 
| 202 |  |  |  |  |  |  | bang_endian_literal		~ '>!' | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | :lexeme					~ bang_literal			pause => before		event => bang_literal | 
| 205 |  |  |  |  |  |  | bang_literal			~ '!' | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | :lexeme					~ bang_only_set			pause => before		event => bang_only_set | 
| 208 |  |  |  |  |  |  | bang_only_set			~ [xXnNvV@.] | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | :lexeme					~ basic_set				pause => before		event => basic_set | 
| 211 |  |  |  |  |  |  | basic_set				~ [aAZbBhHcCwWuU] | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | :lexeme					~ close_bracket			pause => before		event => close_bracket | 
| 214 |  |  |  |  |  |  | close_bracket			~ ']' | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | :lexeme					~ endian_literal		pause => before		event => endian_literal | 
| 217 |  |  |  |  |  |  | endian_literal			~ [><] | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | :lexeme					~ endian_only_set		pause => before		event => endian_only_set | 
| 220 |  |  |  |  |  |  | endian_only_set			~ [qQjJfFdDpP] | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | :lexeme					~ number				pause => before		event => number | 
| 223 |  |  |  |  |  |  | number					~ [\d]+ | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | :lexeme					~ open_bracket			pause => before		event => open_bracket | 
| 226 |  |  |  |  |  |  | open_bracket			~ '[' | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | :lexeme					~ parentheses_set		pause => before		event => parentheses_set | 
| 229 |  |  |  |  |  |  | parentheses_set			~ [()] | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | :lexeme					~ percent_literal		pause => before		event => percent_literal | 
| 232 |  |  |  |  |  |  | percent_literal			~ '%' | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | :lexeme					~ slash_literal			pause => before		event => slash_literal | 
| 235 |  |  |  |  |  |  | slash_literal			~ '/' | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | :lexeme					~ star					pause => before		event => star | 
| 238 |  |  |  |  |  |  | star					~ '*' | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | :discard				~ whitespace | 
| 241 |  |  |  |  |  |  | whitespace				~ [\s]+ | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | :discard				~ | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | # Hash comment handling copied from Marpa::R2's metag.bnf. | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | ~ | 
| 248 |  |  |  |  |  |  | | | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | ~ '#' | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | ~ '#' | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | ~ * | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | ~ [\x{0A}\x{0B}\x{0C}\x{0D}\x{2028}\x{2029}] | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | ~ [^\x{0A}\x{0B}\x{0C}\x{0D}\x{2028}\x{2029}] | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  | END_OF_GRAMMAR | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | $self -> bnf($bnf); | 
| 263 |  |  |  |  |  |  | $self -> grammar | 
| 264 |  |  |  |  |  |  | ( | 
| 265 |  |  |  |  |  |  | Marpa::R2::Scanless::G -> new | 
| 266 |  |  |  |  |  |  | ({ | 
| 267 |  |  |  |  |  |  | source => \$self -> bnf | 
| 268 |  |  |  |  |  |  | }) | 
| 269 |  |  |  |  |  |  | ); | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | my(%event); | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | for my $line (split(/\n/, $self -> bnf) ) | 
| 274 |  |  |  |  |  |  | { | 
| 275 |  |  |  |  |  |  | $event{$1} = 1 if ($line =~ /event\s+=>\s+(\w+)/); | 
| 276 |  |  |  |  |  |  | } | 
| 277 |  |  |  |  |  |  |  | 
| 278 |  |  |  |  |  |  | $self -> known_events(\%event); | 
| 279 |  |  |  |  |  |  |  | 
| 280 |  |  |  |  |  |  | } # End of BUILD. | 
| 281 |  |  |  |  |  |  |  | 
| 282 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 283 |  |  |  |  |  |  |  | 
| 284 |  |  |  |  |  |  | sub _add_daughter | 
| 285 |  |  |  |  |  |  | { | 
| 286 |  |  |  |  |  |  | my($self, $node_name, $lexeme_name, $lexeme) = @_; | 
| 287 |  |  |  |  |  |  | my($stack)  = $self -> stack; | 
| 288 |  |  |  |  |  |  | my($node)   = Tree -> new($node_name); | 
| 289 |  |  |  |  |  |  |  | 
| 290 |  |  |  |  |  |  | $node -> meta({lexeme => $lexeme_name, text => $lexeme}); | 
| 291 |  |  |  |  |  |  |  | 
| 292 |  |  |  |  |  |  | $$stack[$#$stack] -> add_child({}, $node); | 
| 293 |  |  |  |  |  |  |  | 
| 294 |  |  |  |  |  |  | } # End of _add_daughter. | 
| 295 |  |  |  |  |  |  |  | 
| 296 |  |  |  |  |  |  | # ----------------------------------------------- | 
| 297 |  |  |  |  |  |  |  | 
| 298 |  |  |  |  |  |  | sub format_node | 
| 299 |  |  |  |  |  |  | { | 
| 300 |  |  |  |  |  |  | my($self, $options, $node) = @_; | 
| 301 |  |  |  |  |  |  | my($s) = $node -> value; | 
| 302 |  |  |  |  |  |  | $s     .= '. Attributes: ' . $self -> hashref2string($node -> meta) if (! $$options{no_attributes}); | 
| 303 |  |  |  |  |  |  |  | 
| 304 |  |  |  |  |  |  | return $s; | 
| 305 |  |  |  |  |  |  |  | 
| 306 |  |  |  |  |  |  | } # End of format_node. | 
| 307 |  |  |  |  |  |  |  | 
| 308 |  |  |  |  |  |  | # ----------------------------------------------- | 
| 309 |  |  |  |  |  |  |  | 
| 310 |  |  |  |  |  |  | sub hashref2string | 
| 311 |  |  |  |  |  |  | { | 
| 312 |  |  |  |  |  |  | my($self, $hashref) = @_; | 
| 313 |  |  |  |  |  |  | $hashref ||= {}; | 
| 314 |  |  |  |  |  |  |  | 
| 315 |  |  |  |  |  |  | return '{' . join(', ', map{qq|$_ => "$$hashref{$_}"|} sort keys %$hashref) . '}'; | 
| 316 |  |  |  |  |  |  |  | 
| 317 |  |  |  |  |  |  | } # End of hashref2string. | 
| 318 |  |  |  |  |  |  |  | 
| 319 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 320 |  |  |  |  |  |  |  | 
| 321 |  |  |  |  |  |  | sub next_few_chars | 
| 322 |  |  |  |  |  |  | { | 
| 323 |  |  |  |  |  |  | my($self, $string, $offset) = @_; | 
| 324 |  |  |  |  |  |  | my($s) = substr($string, $offset, $self -> next_few_limit); | 
| 325 |  |  |  |  |  |  | $s     =~ tr/\n/ /; | 
| 326 |  |  |  |  |  |  | $s     =~ s/^\s+//; | 
| 327 |  |  |  |  |  |  | $s     =~ s/\s+$//; | 
| 328 |  |  |  |  |  |  |  | 
| 329 |  |  |  |  |  |  | return $s; | 
| 330 |  |  |  |  |  |  |  | 
| 331 |  |  |  |  |  |  | } # End of next_few_chars. | 
| 332 |  |  |  |  |  |  |  | 
| 333 |  |  |  |  |  |  | # ----------------------------------------------- | 
| 334 |  |  |  |  |  |  |  | 
| 335 |  |  |  |  |  |  | sub node2string | 
| 336 |  |  |  |  |  |  | { | 
| 337 |  |  |  |  |  |  | my($self, $options, $is_last_node, $node, $vert_dashes) = @_; | 
| 338 |  |  |  |  |  |  | my($depth)         = $node -> depth; | 
| 339 |  |  |  |  |  |  | my($sibling_count) = defined $node -> is_root ? 1 : scalar $node -> parent -> children; | 
| 340 |  |  |  |  |  |  | my($offset)        = ' ' x 4; | 
| 341 |  |  |  |  |  |  | my(@indent)        = map{$$vert_dashes[$_] || $offset} 0 .. $depth - 1; | 
| 342 |  |  |  |  |  |  | @$vert_dashes      = | 
| 343 |  |  |  |  |  |  | ( | 
| 344 |  |  |  |  |  |  | @indent, | 
| 345 |  |  |  |  |  |  | ($sibling_count == 0 ? $offset : '   |'), | 
| 346 |  |  |  |  |  |  | ); | 
| 347 |  |  |  |  |  |  |  | 
| 348 |  |  |  |  |  |  | $indent[1] = '    ' if ($is_last_node && ($depth > 1) ); | 
| 349 |  |  |  |  |  |  |  | 
| 350 |  |  |  |  |  |  | return join('', @indent[1 .. $#indent]) . ($depth ? '   |--- ' : '') . $self -> format_node($options, $node); | 
| 351 |  |  |  |  |  |  |  | 
| 352 |  |  |  |  |  |  | } # End of node2string. | 
| 353 |  |  |  |  |  |  |  | 
| 354 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 355 |  |  |  |  |  |  |  | 
| 356 |  |  |  |  |  |  | sub parse | 
| 357 |  |  |  |  |  |  | { | 
| 358 |  |  |  |  |  |  | my($self, $string) = @_; | 
| 359 |  |  |  |  |  |  |  | 
| 360 |  |  |  |  |  |  | $self -> stack([]); | 
| 361 |  |  |  |  |  |  | $self -> template($string) if (defined $string); | 
| 362 |  |  |  |  |  |  | $self -> recce | 
| 363 |  |  |  |  |  |  | ( | 
| 364 |  |  |  |  |  |  | Marpa::R2::Scanless::R -> new | 
| 365 |  |  |  |  |  |  | ({ | 
| 366 |  |  |  |  |  |  | grammar        => $self -> grammar, | 
| 367 |  |  |  |  |  |  | ranking_method => 'high_rule_only', | 
| 368 |  |  |  |  |  |  | }) | 
| 369 |  |  |  |  |  |  | ); | 
| 370 |  |  |  |  |  |  |  | 
| 371 |  |  |  |  |  |  | # Since $self -> stack has not been initialized yet, | 
| 372 |  |  |  |  |  |  | # we can't call _add_pack_daughter() until after this statement. | 
| 373 |  |  |  |  |  |  |  | 
| 374 |  |  |  |  |  |  | $self -> tree(Tree -> new('root') ); | 
| 375 |  |  |  |  |  |  | $self -> stack([$self -> tree]); | 
| 376 |  |  |  |  |  |  |  | 
| 377 |  |  |  |  |  |  | # Return 0 for success and 1 for failure. | 
| 378 |  |  |  |  |  |  |  | 
| 379 |  |  |  |  |  |  | my($result) = 0; | 
| 380 |  |  |  |  |  |  |  | 
| 381 |  |  |  |  |  |  | my($message); | 
| 382 |  |  |  |  |  |  |  | 
| 383 |  |  |  |  |  |  | try | 
| 384 |  |  |  |  |  |  | { | 
| 385 |  |  |  |  |  |  | if (defined (my $value = $self -> _process) ) | 
| 386 |  |  |  |  |  |  | { | 
| 387 |  |  |  |  |  |  | } | 
| 388 |  |  |  |  |  |  | else | 
| 389 |  |  |  |  |  |  | { | 
| 390 |  |  |  |  |  |  | $result = 1; | 
| 391 |  |  |  |  |  |  |  | 
| 392 |  |  |  |  |  |  | print "Error: Parse failed\n"; | 
| 393 |  |  |  |  |  |  | } | 
| 394 |  |  |  |  |  |  | } | 
| 395 |  |  |  |  |  |  | catch | 
| 396 |  |  |  |  |  |  | { | 
| 397 |  |  |  |  |  |  | $result = 1; | 
| 398 |  |  |  |  |  |  |  | 
| 399 |  |  |  |  |  |  | print "Error: Parse failed. ${_}"; | 
| 400 |  |  |  |  |  |  | }; | 
| 401 |  |  |  |  |  |  |  | 
| 402 |  |  |  |  |  |  | # Return 0 for success and 1 for failure. | 
| 403 |  |  |  |  |  |  |  | 
| 404 |  |  |  |  |  |  | return $result; | 
| 405 |  |  |  |  |  |  |  | 
| 406 |  |  |  |  |  |  | } # End of parse. | 
| 407 |  |  |  |  |  |  |  | 
| 408 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 409 |  |  |  |  |  |  |  | 
| 410 |  |  |  |  |  |  | sub _pop_stack | 
| 411 |  |  |  |  |  |  | { | 
| 412 |  |  |  |  |  |  | my($self)  = @_; | 
| 413 |  |  |  |  |  |  | my($stack) = $self -> stack; | 
| 414 |  |  |  |  |  |  |  | 
| 415 |  |  |  |  |  |  | pop @$stack; | 
| 416 |  |  |  |  |  |  |  | 
| 417 |  |  |  |  |  |  | $self -> stack($stack); | 
| 418 |  |  |  |  |  |  |  | 
| 419 |  |  |  |  |  |  | } # End of _pop_stack. | 
| 420 |  |  |  |  |  |  |  | 
| 421 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 422 |  |  |  |  |  |  |  | 
| 423 |  |  |  |  |  |  | sub _process | 
| 424 |  |  |  |  |  |  | { | 
| 425 |  |  |  |  |  |  | my($self)        = @_; | 
| 426 |  |  |  |  |  |  | my($string)      = $self -> template || ''; # Allow for undef. | 
| 427 |  |  |  |  |  |  | my($pos)         = 0; | 
| 428 |  |  |  |  |  |  | my($length)      = length($string); | 
| 429 |  |  |  |  |  |  | my($format)      = "%-20s    %5s    %5s    %5s    %-20s    %-20s\n"; | 
| 430 |  |  |  |  |  |  | my($last_event)  = ''; | 
| 431 |  |  |  |  |  |  | my(%token_event) = | 
| 432 |  |  |  |  |  |  | ( | 
| 433 |  |  |  |  |  |  | bang_and_endian_set => 1, | 
| 434 |  |  |  |  |  |  | bang_only_set       => 1, | 
| 435 |  |  |  |  |  |  | basic_set           => 1, | 
| 436 |  |  |  |  |  |  | endian_only_set     => 1, | 
| 437 |  |  |  |  |  |  | parentheses_set		=> 1, | 
| 438 |  |  |  |  |  |  | ); | 
| 439 |  |  |  |  |  |  |  | 
| 440 |  |  |  |  |  |  | if ($self -> options & debug) | 
| 441 |  |  |  |  |  |  | { | 
| 442 |  |  |  |  |  |  | print "Input: $string. Length: $length. \n"; | 
| 443 |  |  |  |  |  |  | print sprintf($format, 'Event', 'Start', 'Span', 'Pos', 'Lexeme', 'Comment'); | 
| 444 |  |  |  |  |  |  | } | 
| 445 |  |  |  |  |  |  |  | 
| 446 |  |  |  |  |  |  | my($event_name); | 
| 447 |  |  |  |  |  |  | my($lexeme); | 
| 448 |  |  |  |  |  |  | my($message); | 
| 449 |  |  |  |  |  |  | my($node_name); | 
| 450 |  |  |  |  |  |  | my($original_lexeme); | 
| 451 |  |  |  |  |  |  | my($span, $start); | 
| 452 |  |  |  |  |  |  | my($tos); | 
| 453 |  |  |  |  |  |  |  | 
| 454 |  |  |  |  |  |  | # We use read()/lexeme_read()/resume() because we pause at each lexeme. | 
| 455 |  |  |  |  |  |  | # Also, in read(), we use $pos and $length to avoid reading Ruby Slippers tokens (if any). | 
| 456 |  |  |  |  |  |  | # For the latter, see scripts/match.parentheses.02.pl in MarpaX::Demo::SampleScripts. | 
| 457 |  |  |  |  |  |  |  | 
| 458 |  |  |  |  |  |  | for | 
| 459 |  |  |  |  |  |  | ( | 
| 460 |  |  |  |  |  |  | $pos = $self -> recce -> read(\$string, $pos, $length); | 
| 461 |  |  |  |  |  |  | ($pos < $length); | 
| 462 |  |  |  |  |  |  | $pos = $self -> recce -> resume($pos) | 
| 463 |  |  |  |  |  |  | ) | 
| 464 |  |  |  |  |  |  | { | 
| 465 |  |  |  |  |  |  | ($start, $span)            = $self -> recce -> pause_span; | 
| 466 |  |  |  |  |  |  | ($event_name, $span, $pos) = $self -> _validate_event($string, $start, $span, $pos); | 
| 467 |  |  |  |  |  |  | $lexeme                    = $self -> recce -> literal($start, $span); | 
| 468 |  |  |  |  |  |  | $original_lexeme           = $lexeme; | 
| 469 |  |  |  |  |  |  | $pos                       = $self -> recce -> lexeme_read($event_name); | 
| 470 |  |  |  |  |  |  |  | 
| 471 |  |  |  |  |  |  | die "lexeme_read($event_name) rejected lexeme |$lexeme|\n" if (! defined $pos); | 
| 472 |  |  |  |  |  |  |  | 
| 473 |  |  |  |  |  |  | print sprintf($format, $event_name, $start, $span, $pos, $lexeme, '-') if ($self -> options & debug); | 
| 474 |  |  |  |  |  |  |  | 
| 475 |  |  |  |  |  |  | $node_name = $token_event{$event_name} ? 'token' : $event_name; | 
| 476 |  |  |  |  |  |  |  | 
| 477 |  |  |  |  |  |  | if ( ($node_name eq 'token') && ($#{$self -> stack} > 1) ) | 
| 478 |  |  |  |  |  |  | { | 
| 479 |  |  |  |  |  |  | $self -> _pop_stack; | 
| 480 |  |  |  |  |  |  | } | 
| 481 |  |  |  |  |  |  |  | 
| 482 |  |  |  |  |  |  | $self -> _add_daughter($node_name, $event_name, $lexeme); | 
| 483 |  |  |  |  |  |  |  | 
| 484 |  |  |  |  |  |  | $self -> _push_stack if ($node_name eq 'token'); | 
| 485 |  |  |  |  |  |  |  | 
| 486 |  |  |  |  |  |  | $last_event = $event_name; | 
| 487 |  |  |  |  |  |  | } | 
| 488 |  |  |  |  |  |  |  | 
| 489 |  |  |  |  |  |  | if (my $status = $self -> recce -> ambiguous) | 
| 490 |  |  |  |  |  |  | { | 
| 491 |  |  |  |  |  |  | my($terminals) = $self -> recce -> terminals_expected; | 
| 492 |  |  |  |  |  |  | $terminals     = ['(None)'] if ($#$terminals < 0); | 
| 493 |  |  |  |  |  |  | $message       = 'Ambiguous parse. Status: $status. Terminals expected: ' . join(', ', @$terminals); | 
| 494 |  |  |  |  |  |  |  | 
| 495 |  |  |  |  |  |  | $self -> error_message($message); | 
| 496 |  |  |  |  |  |  | $self -> error_number(1); | 
| 497 |  |  |  |  |  |  |  | 
| 498 |  |  |  |  |  |  | if ($self -> options & ambiguity_is_fatal) | 
| 499 |  |  |  |  |  |  | { | 
| 500 |  |  |  |  |  |  | # This 'die' is inside try {}catch{}, which adds the prefix 'Error: '. | 
| 501 |  |  |  |  |  |  |  | 
| 502 |  |  |  |  |  |  | die "$message\n"; | 
| 503 |  |  |  |  |  |  | } | 
| 504 |  |  |  |  |  |  | elsif ($self -> options & print_warnings) | 
| 505 |  |  |  |  |  |  | { | 
| 506 |  |  |  |  |  |  | $self -> error_number(-1); | 
| 507 |  |  |  |  |  |  |  | 
| 508 |  |  |  |  |  |  | print "Warning: $message\n"; | 
| 509 |  |  |  |  |  |  | } | 
| 510 |  |  |  |  |  |  | } | 
| 511 |  |  |  |  |  |  |  | 
| 512 |  |  |  |  |  |  | # Return a defined value for success and undef for failure. | 
| 513 |  |  |  |  |  |  |  | 
| 514 |  |  |  |  |  |  | return $self -> recce -> value; | 
| 515 |  |  |  |  |  |  |  | 
| 516 |  |  |  |  |  |  | } # End of _process. | 
| 517 |  |  |  |  |  |  |  | 
| 518 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 519 |  |  |  |  |  |  |  | 
| 520 |  |  |  |  |  |  | sub _push_stack | 
| 521 |  |  |  |  |  |  | { | 
| 522 |  |  |  |  |  |  | my($self)      = @_; | 
| 523 |  |  |  |  |  |  | my($stack)     = $self -> stack; | 
| 524 |  |  |  |  |  |  | my(@daughters) = $$stack[$#$stack] -> children; | 
| 525 |  |  |  |  |  |  |  | 
| 526 |  |  |  |  |  |  | push @$stack, $daughters[$#daughters]; | 
| 527 |  |  |  |  |  |  |  | 
| 528 |  |  |  |  |  |  | $self -> stack($stack); | 
| 529 |  |  |  |  |  |  |  | 
| 530 |  |  |  |  |  |  | } # End of _push_stack. | 
| 531 |  |  |  |  |  |  |  | 
| 532 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 533 |  |  |  |  |  |  |  | 
| 534 |  |  |  |  |  |  | sub size_report | 
| 535 |  |  |  |  |  |  | { | 
| 536 |  |  |  |  |  |  | my($self)             = @_; | 
| 537 |  |  |  |  |  |  | my($is_little_endian) = unpack('c',  pack('s', 1) ); | 
| 538 |  |  |  |  |  |  | my($is_big_endian)    = unpack('xc', pack('s', 1) ); | 
| 539 |  |  |  |  |  |  | my(%size)             = | 
| 540 |  |  |  |  |  |  | ( | 
| 541 |  |  |  |  |  |  | 1 => ['short',    's!', 'S!', $Config{shortsize}, '$Config{shortsize}'], | 
| 542 |  |  |  |  |  |  | 2 => ['int',      'i!', 'I!', $Config{intsize},   '$Config{intsize}'], | 
| 543 |  |  |  |  |  |  | 3 => ['long',     'l!', 'L!', $Config{longsize},  '$Config{longsize}'], | 
| 544 |  |  |  |  |  |  | 4 => ['longlong', 'q!', 'Q!', $Config{longlongsize} || 'Undef', '$Config{longlongsize}'], | 
| 545 |  |  |  |  |  |  | ); | 
| 546 |  |  |  |  |  |  |  | 
| 547 |  |  |  |  |  |  | print "Byte order: $Config{byteorder}. Little endian: $is_little_endian. Big endian: $is_big_endian. \n"; | 
| 548 |  |  |  |  |  |  | print "Some template codes and their size requirements: \n"; | 
| 549 |  |  |  |  |  |  |  | 
| 550 |  |  |  |  |  |  | my($format) = "%-6s  %-8s  %-10s  %-s  %-s\n"; | 
| 551 |  |  |  |  |  |  |  | 
| 552 |  |  |  |  |  |  | print sprintf($format, 'Signed', 'Unsigned', 'Name', 'Byte length in Perl', ''); | 
| 553 |  |  |  |  |  |  |  | 
| 554 |  |  |  |  |  |  | for my $key (1 .. 4) | 
| 555 |  |  |  |  |  |  | { | 
| 556 |  |  |  |  |  |  | print sprintf($format, $size{$key}[1], $size{$key}[2], $size{$key}[0], $size{$key}[3], $size{$key}[4]); | 
| 557 |  |  |  |  |  |  | } | 
| 558 |  |  |  |  |  |  |  | 
| 559 |  |  |  |  |  |  | } # End of size_report. | 
| 560 |  |  |  |  |  |  |  | 
| 561 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 562 |  |  |  |  |  |  |  | 
| 563 |  |  |  |  |  |  | sub template_report | 
| 564 |  |  |  |  |  |  | { | 
| 565 |  |  |  |  |  |  | my($self)     = @_; | 
| 566 |  |  |  |  |  |  | my($count)    = 0; | 
| 567 |  |  |  |  |  |  | my($previous) = ''; | 
| 568 |  |  |  |  |  |  | my($result)   = ''; | 
| 569 |  |  |  |  |  |  |  | 
| 570 |  |  |  |  |  |  | my($attributes); | 
| 571 |  |  |  |  |  |  | my($text); | 
| 572 |  |  |  |  |  |  |  | 
| 573 |  |  |  |  |  |  | for my $node ($self -> tree -> traverse) | 
| 574 |  |  |  |  |  |  | { | 
| 575 |  |  |  |  |  |  | next if ($node -> is_root); | 
| 576 |  |  |  |  |  |  |  | 
| 577 |  |  |  |  |  |  | $count++; | 
| 578 |  |  |  |  |  |  |  | 
| 579 |  |  |  |  |  |  | $attributes = $node -> meta; | 
| 580 |  |  |  |  |  |  | $text       = $$attributes{text}; | 
| 581 |  |  |  |  |  |  | $result     .= ' ' if ( ($count > 1) && ($previous ne '/') && ($node -> value eq 'token') ); | 
| 582 |  |  |  |  |  |  | $result     .= $text; | 
| 583 |  |  |  |  |  |  | $previous   = $text; | 
| 584 |  |  |  |  |  |  | } | 
| 585 |  |  |  |  |  |  |  | 
| 586 |  |  |  |  |  |  | return $result; | 
| 587 |  |  |  |  |  |  |  | 
| 588 |  |  |  |  |  |  | } # End of template_report. | 
| 589 |  |  |  |  |  |  |  | 
| 590 |  |  |  |  |  |  | # ----------------------------------------------- | 
| 591 |  |  |  |  |  |  |  | 
| 592 |  |  |  |  |  |  | sub tree2string | 
| 593 |  |  |  |  |  |  | { | 
| 594 |  |  |  |  |  |  | my($self, $options, $tree) = @_; | 
| 595 |  |  |  |  |  |  | $options                   ||= {}; | 
| 596 |  |  |  |  |  |  | $$options{no_attributes}   ||= 0; | 
| 597 |  |  |  |  |  |  | $tree                      ||= $self -> tree; | 
| 598 |  |  |  |  |  |  | my(@nodes)                 = $tree -> traverse; | 
| 599 |  |  |  |  |  |  |  | 
| 600 |  |  |  |  |  |  | my(@out); | 
| 601 |  |  |  |  |  |  | my(@vert_dashes); | 
| 602 |  |  |  |  |  |  |  | 
| 603 |  |  |  |  |  |  | for my $i (0 .. $#nodes) | 
| 604 |  |  |  |  |  |  | { | 
| 605 |  |  |  |  |  |  | push @out, $self -> node2string($options, $i == $#nodes, $nodes[$i], \@vert_dashes); | 
| 606 |  |  |  |  |  |  | } | 
| 607 |  |  |  |  |  |  |  | 
| 608 |  |  |  |  |  |  | return [@out]; | 
| 609 |  |  |  |  |  |  |  | 
| 610 |  |  |  |  |  |  | } # End of tree2string. | 
| 611 |  |  |  |  |  |  |  | 
| 612 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 613 |  |  |  |  |  |  |  | 
| 614 |  |  |  |  |  |  | sub _validate_event | 
| 615 |  |  |  |  |  |  | { | 
| 616 |  |  |  |  |  |  | my($self, $string, $start, $span, $pos) = @_; | 
| 617 |  |  |  |  |  |  | my(@event)         = @{$self -> recce -> events}; | 
| 618 |  |  |  |  |  |  | my($event_count)   = scalar @event; | 
| 619 |  |  |  |  |  |  | my(@event_name)    = sort map{$$_[0]} @event; | 
| 620 |  |  |  |  |  |  | my($event_name)    = $event_name[0]; # Default. | 
| 621 |  |  |  |  |  |  | my($lexeme)        = substr($string, $start, $span); | 
| 622 |  |  |  |  |  |  | my($line, $column) = $self -> recce -> line_column($start); | 
| 623 |  |  |  |  |  |  | my($literal)       = $self -> next_few_chars($string, $start + $span); | 
| 624 |  |  |  |  |  |  | my($message)       = "Location: ($line, $column). Lexeme: |$lexeme|. Next few chars: |$literal|"; | 
| 625 |  |  |  |  |  |  | $message           = "$message. Events: $event_count. Names: "; | 
| 626 |  |  |  |  |  |  |  | 
| 627 |  |  |  |  |  |  | print $message, join(', ', @event_name), "\n" if ($self -> options & debug); | 
| 628 |  |  |  |  |  |  |  | 
| 629 |  |  |  |  |  |  | my(%event_name); | 
| 630 |  |  |  |  |  |  |  | 
| 631 |  |  |  |  |  |  | @event_name{@event_name} = (1) x @event_name; | 
| 632 |  |  |  |  |  |  |  | 
| 633 |  |  |  |  |  |  | for (@event_name) | 
| 634 |  |  |  |  |  |  | { | 
| 635 |  |  |  |  |  |  | if (! ${$self -> known_events}{$_}) | 
| 636 |  |  |  |  |  |  | { | 
| 637 |  |  |  |  |  |  | $message = "Unexpected event name '$_'"; | 
| 638 |  |  |  |  |  |  |  | 
| 639 |  |  |  |  |  |  | $self -> error_message($message); | 
| 640 |  |  |  |  |  |  | $self -> error_number(2); | 
| 641 |  |  |  |  |  |  |  | 
| 642 |  |  |  |  |  |  | # This 'die' is inside try {}catch{}, which adds the prefix 'Error: '. | 
| 643 |  |  |  |  |  |  |  | 
| 644 |  |  |  |  |  |  | die "$message\n"; | 
| 645 |  |  |  |  |  |  | } | 
| 646 |  |  |  |  |  |  | } | 
| 647 |  |  |  |  |  |  |  | 
| 648 |  |  |  |  |  |  | if ($event_count > 1) | 
| 649 |  |  |  |  |  |  | { | 
| 650 |  |  |  |  |  |  | $message = join(', ', @event_name); | 
| 651 |  |  |  |  |  |  | $message = "The code does not handle these events simultaneously: $message"; | 
| 652 |  |  |  |  |  |  |  | 
| 653 |  |  |  |  |  |  | $self -> error_message($message); | 
| 654 |  |  |  |  |  |  | $self -> error_number(3); | 
| 655 |  |  |  |  |  |  |  | 
| 656 |  |  |  |  |  |  | # This 'die' is inside try {}catch{}, which adds the prefix 'Error: '. | 
| 657 |  |  |  |  |  |  |  | 
| 658 |  |  |  |  |  |  | die "$message\n"; | 
| 659 |  |  |  |  |  |  | } | 
| 660 |  |  |  |  |  |  |  | 
| 661 |  |  |  |  |  |  | return ($event_name, $span, $pos); | 
| 662 |  |  |  |  |  |  |  | 
| 663 |  |  |  |  |  |  | } # End of _validate_event. | 
| 664 |  |  |  |  |  |  |  | 
| 665 |  |  |  |  |  |  | # ------------------------------------------------ | 
| 666 |  |  |  |  |  |  |  | 
| 667 |  |  |  |  |  |  | 1; | 
| 668 |  |  |  |  |  |  |  | 
| 669 |  |  |  |  |  |  | =pod | 
| 670 |  |  |  |  |  |  |  | 
| 671 |  |  |  |  |  |  | =head1 NAME | 
| 672 |  |  |  |  |  |  |  | 
| 673 |  |  |  |  |  |  | C - Parse the templates used in pack() and unpack() | 
| 674 |  |  |  |  |  |  |  | 
| 675 |  |  |  |  |  |  | =head1 Synopsis | 
| 676 |  |  |  |  |  |  |  | 
| 677 |  |  |  |  |  |  | #!/usr/bin/env perl | 
| 678 |  |  |  |  |  |  |  | 
| 679 |  |  |  |  |  |  | use strict; | 
| 680 |  |  |  |  |  |  | use warnings; | 
| 681 |  |  |  |  |  |  |  | 
| 682 |  |  |  |  |  |  | use MarpaX::Languages::Perl::PackUnpack ':constants'; | 
| 683 |  |  |  |  |  |  |  | 
| 684 |  |  |  |  |  |  | # ----------- | 
| 685 |  |  |  |  |  |  |  | 
| 686 |  |  |  |  |  |  | my($parser) = MarpaX::Languages::Perl::PackUnpack -> new(options => print_warnings); | 
| 687 |  |  |  |  |  |  | my(@text)   = | 
| 688 |  |  |  |  |  |  | ( | 
| 689 |  |  |  |  |  |  | qq|n/a* # Newline | 
| 690 |  |  |  |  |  |  | w/a2|, | 
| 691 |  |  |  |  |  |  | q|a3/A A*|, | 
| 692 |  |  |  |  |  |  | q|i9pl|, | 
| 693 |  |  |  |  |  |  | ); | 
| 694 |  |  |  |  |  |  |  | 
| 695 |  |  |  |  |  |  | my($result); | 
| 696 |  |  |  |  |  |  |  | 
| 697 |  |  |  |  |  |  | for my $text (@text) | 
| 698 |  |  |  |  |  |  | { | 
| 699 |  |  |  |  |  |  | print "Parsing: $text. \n"; | 
| 700 |  |  |  |  |  |  |  | 
| 701 |  |  |  |  |  |  | $result = $parser -> parse($text); | 
| 702 |  |  |  |  |  |  |  | 
| 703 |  |  |  |  |  |  | print join("\n", @{$parser -> tree2string}), "\n"; | 
| 704 |  |  |  |  |  |  | print "Parse result: $result (0 is success)\n"; | 
| 705 |  |  |  |  |  |  | print 'Template: ', $parser -> template_report, ". \n"; | 
| 706 |  |  |  |  |  |  | print '-' x 50, "\n"; | 
| 707 |  |  |  |  |  |  | } | 
| 708 |  |  |  |  |  |  |  | 
| 709 |  |  |  |  |  |  | print "\n"; | 
| 710 |  |  |  |  |  |  |  | 
| 711 |  |  |  |  |  |  | $parser -> size_report; | 
| 712 |  |  |  |  |  |  |  | 
| 713 |  |  |  |  |  |  | See scripts/synopsis.pl. | 
| 714 |  |  |  |  |  |  |  | 
| 715 |  |  |  |  |  |  | This is the output of synopsis.pl: | 
| 716 |  |  |  |  |  |  |  | 
| 717 |  |  |  |  |  |  | Parsing: n/a* # Newline | 
| 718 |  |  |  |  |  |  | w/a2. | 
| 719 |  |  |  |  |  |  | root. Attributes: {} | 
| 720 |  |  |  |  |  |  | |--- token. Attributes: {lexeme => "bang_only_set", text => "n"} | 
| 721 |  |  |  |  |  |  | |   |--- slash_literal. Attributes: {lexeme => "slash_literal", text => "/"} | 
| 722 |  |  |  |  |  |  | |   |--- token. Attributes: {lexeme => "basic_set", text => "a"} | 
| 723 |  |  |  |  |  |  | |   |   |--- star. Attributes: {lexeme => "star", text => "*"} | 
| 724 |  |  |  |  |  |  | |   |--- token. Attributes: {lexeme => "basic_set", text => "w"} | 
| 725 |  |  |  |  |  |  | |   |   |--- slash_literal. Attributes: {lexeme => "slash_literal", text => "/"} | 
| 726 |  |  |  |  |  |  | |   |--- token. Attributes: {lexeme => "basic_set", text => "a"} | 
| 727 |  |  |  |  |  |  | |   |--- number. Attributes: {lexeme => "number", text => "2"} | 
| 728 |  |  |  |  |  |  | Parse result: 0 (0 is success) | 
| 729 |  |  |  |  |  |  | Template: n/a* w/a2. | 
| 730 |  |  |  |  |  |  | -------------------------------------------------- | 
| 731 |  |  |  |  |  |  | Parsing: a3/A A*. | 
| 732 |  |  |  |  |  |  | root. Attributes: {} | 
| 733 |  |  |  |  |  |  | |--- token. Attributes: {lexeme => "basic_set", text => "a"} | 
| 734 |  |  |  |  |  |  | |   |--- number. Attributes: {lexeme => "number", text => "3"} | 
| 735 |  |  |  |  |  |  | |   |--- slash_literal. Attributes: {lexeme => "slash_literal", text => "/"} | 
| 736 |  |  |  |  |  |  | |   |--- token. Attributes: {lexeme => "basic_set", text => "A"} | 
| 737 |  |  |  |  |  |  | |   |--- token. Attributes: {lexeme => "basic_set", text => "A"} | 
| 738 |  |  |  |  |  |  | |   |--- star. Attributes: {lexeme => "star", text => "*"} | 
| 739 |  |  |  |  |  |  | Parse result: 0 (0 is success) | 
| 740 |  |  |  |  |  |  | Template: a3/A A*. | 
| 741 |  |  |  |  |  |  | -------------------------------------------------- | 
| 742 |  |  |  |  |  |  | Parsing: i9pl. | 
| 743 |  |  |  |  |  |  | root. Attributes: {} | 
| 744 |  |  |  |  |  |  | |--- token. Attributes: {lexeme => "bang_and_endian_set", text => "i"} | 
| 745 |  |  |  |  |  |  | |   |--- number. Attributes: {lexeme => "number", text => "9"} | 
| 746 |  |  |  |  |  |  | |   |--- token. Attributes: {lexeme => "endian_only_set", text => "p"} | 
| 747 |  |  |  |  |  |  | |--- token. Attributes: {lexeme => "bang_and_endian_set", text => "l"} | 
| 748 |  |  |  |  |  |  | Parse result: 0 (0 is success) | 
| 749 |  |  |  |  |  |  | Template: i9 p l. | 
| 750 |  |  |  |  |  |  | -------------------------------------------------- | 
| 751 |  |  |  |  |  |  |  | 
| 752 |  |  |  |  |  |  | Size report: | 
| 753 |  |  |  |  |  |  | Byte order: 12345678. Little endian: 1. Big endian: 0. | 
| 754 |  |  |  |  |  |  | Some template codes and their size requirements: | 
| 755 |  |  |  |  |  |  | Signed  Unsigned  Name        Byte length in Perl | 
| 756 |  |  |  |  |  |  | s!      S!        short       2  $Config{shortsize} | 
| 757 |  |  |  |  |  |  | i!      I!        int         4  $Config{intsize} | 
| 758 |  |  |  |  |  |  | l!      L!        long        8  $Config{longsize} | 
| 759 |  |  |  |  |  |  | q!      Q!        longlong    8  $Config{longlongsize} | 
| 760 |  |  |  |  |  |  |  | 
| 761 |  |  |  |  |  |  | =head1 Description | 
| 762 |  |  |  |  |  |  |  | 
| 763 |  |  |  |  |  |  | L provides a L-based parser for parsing the | 
| 764 |  |  |  |  |  |  | templates used in pack() and unpack(). | 
| 765 |  |  |  |  |  |  |  | 
| 766 |  |  |  |  |  |  | The parsed details are stored in a L, and can be accessed via the methods | 
| 767 |  |  |  |  |  |  | L and L. The tree itself can be accessed | 
| 768 |  |  |  |  |  |  | with the method L. | 
| 769 |  |  |  |  |  |  |  | 
| 770 |  |  |  |  |  |  | Policy: Event names are always the same as the name of the corresponding lexeme. So any reference to | 
| 771 |  |  |  |  |  |  | 'event name' is the same as to 'lexeme name', and visa versa. This can be seen in the grammar where | 
| 772 |  |  |  |  |  |  | every lexeme which is not discarded is associated with an event of the same name. This matter is | 
| 773 |  |  |  |  |  |  | discussed in detail under the question L. | 
| 774 |  |  |  |  |  |  |  | 
| 775 |  |  |  |  |  |  | =head1 Distributions | 
| 776 |  |  |  |  |  |  |  | 
| 777 |  |  |  |  |  |  | This module is available as a Unix-style distro (*.tgz). | 
| 778 |  |  |  |  |  |  |  | 
| 779 |  |  |  |  |  |  | See L | 
| 780 |  |  |  |  |  |  | for help on unpacking and installing distros. | 
| 781 |  |  |  |  |  |  |  | 
| 782 |  |  |  |  |  |  | =head1 Installation | 
| 783 |  |  |  |  |  |  |  | 
| 784 |  |  |  |  |  |  | Install L as you would any C module: | 
| 785 |  |  |  |  |  |  |  | 
| 786 |  |  |  |  |  |  | Run: | 
| 787 |  |  |  |  |  |  |  | 
| 788 |  |  |  |  |  |  | cpanm MarpaX::Languages::Perl::PackUnpack | 
| 789 |  |  |  |  |  |  |  | 
| 790 |  |  |  |  |  |  | or run: | 
| 791 |  |  |  |  |  |  |  | 
| 792 |  |  |  |  |  |  | sudo cpan MarpaX::Languages::Perl::PackUnpack | 
| 793 |  |  |  |  |  |  |  | 
| 794 |  |  |  |  |  |  | or unpack the distro, and then either: | 
| 795 |  |  |  |  |  |  |  | 
| 796 |  |  |  |  |  |  | perl Build.PL | 
| 797 |  |  |  |  |  |  | ./Build | 
| 798 |  |  |  |  |  |  | ./Build test | 
| 799 |  |  |  |  |  |  | sudo ./Build install | 
| 800 |  |  |  |  |  |  |  | 
| 801 |  |  |  |  |  |  | or: | 
| 802 |  |  |  |  |  |  |  | 
| 803 |  |  |  |  |  |  | perl Makefile.PL | 
| 804 |  |  |  |  |  |  | make (or dmake or nmake) | 
| 805 |  |  |  |  |  |  | make test | 
| 806 |  |  |  |  |  |  | make install | 
| 807 |  |  |  |  |  |  |  | 
| 808 |  |  |  |  |  |  | =head1 Constructor and Initialization | 
| 809 |  |  |  |  |  |  |  | 
| 810 |  |  |  |  |  |  | C is called as C<< my($parser) = MarpaX::Languages::Perl::PackUnpack -> new(k1 => v1, k2 => v2, ...) >>. | 
| 811 |  |  |  |  |  |  |  | 
| 812 |  |  |  |  |  |  | It returns a new object of type C. | 
| 813 |  |  |  |  |  |  |  | 
| 814 |  |  |  |  |  |  | Key-value pairs accepted in the parameter list (see corresponding methods for details | 
| 815 |  |  |  |  |  |  | [e.g. L]): | 
| 816 |  |  |  |  |  |  |  | 
| 817 |  |  |  |  |  |  | =over 4 | 
| 818 |  |  |  |  |  |  |  | 
| 819 |  |  |  |  |  |  | =item o next_few_limit => $integer | 
| 820 |  |  |  |  |  |  |  | 
| 821 |  |  |  |  |  |  | This controls how many characters are printed when displaying 'the next few chars'. | 
| 822 |  |  |  |  |  |  |  | 
| 823 |  |  |  |  |  |  | It only affects debug output. | 
| 824 |  |  |  |  |  |  |  | 
| 825 |  |  |  |  |  |  | Default: 20. | 
| 826 |  |  |  |  |  |  |  | 
| 827 |  |  |  |  |  |  | =item o options => $bit_string | 
| 828 |  |  |  |  |  |  |  | 
| 829 |  |  |  |  |  |  | This allows you to turn on various options. | 
| 830 |  |  |  |  |  |  |  | 
| 831 |  |  |  |  |  |  | Default: 0 (nothing is fatal). | 
| 832 |  |  |  |  |  |  |  | 
| 833 |  |  |  |  |  |  | See the L for details. | 
| 834 |  |  |  |  |  |  |  | 
| 835 |  |  |  |  |  |  | =item o template => $string | 
| 836 |  |  |  |  |  |  |  | 
| 837 |  |  |  |  |  |  | Specify the string to be parsed. | 
| 838 |  |  |  |  |  |  |  | 
| 839 |  |  |  |  |  |  | Default: ''. | 
| 840 |  |  |  |  |  |  |  | 
| 841 |  |  |  |  |  |  | =back | 
| 842 |  |  |  |  |  |  |  | 
| 843 |  |  |  |  |  |  | =head1 Methods | 
| 844 |  |  |  |  |  |  |  | 
| 845 |  |  |  |  |  |  | =head2 bnf() | 
| 846 |  |  |  |  |  |  |  | 
| 847 |  |  |  |  |  |  | Returns a string containing the grammar. | 
| 848 |  |  |  |  |  |  |  | 
| 849 |  |  |  |  |  |  | =head2 error_message() | 
| 850 |  |  |  |  |  |  |  | 
| 851 |  |  |  |  |  |  | Returns the last error or warning message set. | 
| 852 |  |  |  |  |  |  |  | 
| 853 |  |  |  |  |  |  | Error messages always start with 'Error: '. Messages never end with "\n". | 
| 854 |  |  |  |  |  |  |  | 
| 855 |  |  |  |  |  |  | Parsing error strings is not a good idea, ever though this module's format for them is fixed. | 
| 856 |  |  |  |  |  |  |  | 
| 857 |  |  |  |  |  |  | See L. | 
| 858 |  |  |  |  |  |  |  | 
| 859 |  |  |  |  |  |  | =head2 error_number() | 
| 860 |  |  |  |  |  |  |  | 
| 861 |  |  |  |  |  |  | Returns the last error or warning number set. | 
| 862 |  |  |  |  |  |  |  | 
| 863 |  |  |  |  |  |  | Warnings have values < 0, and errors have values > 0. | 
| 864 |  |  |  |  |  |  |  | 
| 865 |  |  |  |  |  |  | If the value is > 0, the message has the prefix 'Error: ', and if the value is < 0, it has the | 
| 866 |  |  |  |  |  |  | prefix 'Warning: '. If this is not the case, it's a reportable bug. | 
| 867 |  |  |  |  |  |  |  | 
| 868 |  |  |  |  |  |  | Possible values for error_number() and error_message(): | 
| 869 |  |  |  |  |  |  |  | 
| 870 |  |  |  |  |  |  | =over 4 | 
| 871 |  |  |  |  |  |  |  | 
| 872 |  |  |  |  |  |  | =item o 0 => "" | 
| 873 |  |  |  |  |  |  |  | 
| 874 |  |  |  |  |  |  | This is the default value. | 
| 875 |  |  |  |  |  |  |  | 
| 876 |  |  |  |  |  |  | =item o 1/-1 => "Ambiguous parse. Status: $status. Terminals expected: a, b, ..." | 
| 877 |  |  |  |  |  |  |  | 
| 878 |  |  |  |  |  |  | This message is only produced when the parse is ambiguous. | 
| 879 |  |  |  |  |  |  |  | 
| 880 |  |  |  |  |  |  | If L returns 1, it's an error, and if it returns -1 it's a warning. | 
| 881 |  |  |  |  |  |  |  | 
| 882 |  |  |  |  |  |  | You can set the option C to make it fatal. | 
| 883 |  |  |  |  |  |  |  | 
| 884 |  |  |  |  |  |  | =item o 2 => "Unexpected event name 'xyz'" | 
| 885 |  |  |  |  |  |  |  | 
| 886 |  |  |  |  |  |  | Marpa has trigged an event and it's name is not in the hash of event names derived from the BNF. | 
| 887 |  |  |  |  |  |  |  | 
| 888 |  |  |  |  |  |  | This message can never be just a warning message. | 
| 889 |  |  |  |  |  |  |  | 
| 890 |  |  |  |  |  |  | =item o 3 => "The code does not handle these events simultaneously: a, b, ..." | 
| 891 |  |  |  |  |  |  |  | 
| 892 |  |  |  |  |  |  | The code is written to handle single events at a time, or in rare cases, 2 events at the same time. | 
| 893 |  |  |  |  |  |  | But here, multiple events have been triggered and the code cannot handle the given combination. | 
| 894 |  |  |  |  |  |  |  | 
| 895 |  |  |  |  |  |  | This message can never be just a warning message. | 
| 896 |  |  |  |  |  |  |  | 
| 897 |  |  |  |  |  |  | =back | 
| 898 |  |  |  |  |  |  |  | 
| 899 |  |  |  |  |  |  | See L. | 
| 900 |  |  |  |  |  |  |  | 
| 901 |  |  |  |  |  |  | =head2 format_node($options, $node) | 
| 902 |  |  |  |  |  |  |  | 
| 903 |  |  |  |  |  |  | Returns a string consisting of the node's name and, optionally, it's attributes. | 
| 904 |  |  |  |  |  |  |  | 
| 905 |  |  |  |  |  |  | Possible keys in the $options hashref: | 
| 906 |  |  |  |  |  |  |  | 
| 907 |  |  |  |  |  |  | =over 4 | 
| 908 |  |  |  |  |  |  |  | 
| 909 |  |  |  |  |  |  | =item o no_attributes => $Boolean | 
| 910 |  |  |  |  |  |  |  | 
| 911 |  |  |  |  |  |  | If 1, the node's attributes are not included in the string returned. | 
| 912 |  |  |  |  |  |  |  | 
| 913 |  |  |  |  |  |  | Default: 0 (include attributes). | 
| 914 |  |  |  |  |  |  |  | 
| 915 |  |  |  |  |  |  | =back | 
| 916 |  |  |  |  |  |  |  | 
| 917 |  |  |  |  |  |  | Calls L. | 
| 918 |  |  |  |  |  |  |  | 
| 919 |  |  |  |  |  |  | Called by L. | 
| 920 |  |  |  |  |  |  |  | 
| 921 |  |  |  |  |  |  | You would not normally call this method. | 
| 922 |  |  |  |  |  |  |  | 
| 923 |  |  |  |  |  |  | If you don't wish to supply options, use format_node({}, $node). | 
| 924 |  |  |  |  |  |  |  | 
| 925 |  |  |  |  |  |  | =head2 hashref2string($hashref) | 
| 926 |  |  |  |  |  |  |  | 
| 927 |  |  |  |  |  |  | Returns the given hashref as a string. | 
| 928 |  |  |  |  |  |  |  | 
| 929 |  |  |  |  |  |  | Called by L. | 
| 930 |  |  |  |  |  |  |  | 
| 931 |  |  |  |  |  |  | =head2 known_events() | 
| 932 |  |  |  |  |  |  |  | 
| 933 |  |  |  |  |  |  | Returns a hashref where the keys are event names and the values are 1. | 
| 934 |  |  |  |  |  |  |  | 
| 935 |  |  |  |  |  |  | =head2 new() | 
| 936 |  |  |  |  |  |  |  | 
| 937 |  |  |  |  |  |  | See L for details on the parameters accepted by L. | 
| 938 |  |  |  |  |  |  |  | 
| 939 |  |  |  |  |  |  | =head2 next_few_chars($string, $offset) | 
| 940 |  |  |  |  |  |  |  | 
| 941 |  |  |  |  |  |  | Returns a substring of $s, starting at $offset, for use in debug messages. | 
| 942 |  |  |  |  |  |  |  | 
| 943 |  |  |  |  |  |  | See L. | 
| 944 |  |  |  |  |  |  |  | 
| 945 |  |  |  |  |  |  | =head2 next_few_limit([$integer]) | 
| 946 |  |  |  |  |  |  |  | 
| 947 |  |  |  |  |  |  | Here, the [] indicate an optional parameter. | 
| 948 |  |  |  |  |  |  |  | 
| 949 |  |  |  |  |  |  | Get or set the number of characters called 'the next few chars', which are printed during debugging. | 
| 950 |  |  |  |  |  |  |  | 
| 951 |  |  |  |  |  |  | 'next_few_limit' is a parameter to L. See L for details. | 
| 952 |  |  |  |  |  |  |  | 
| 953 |  |  |  |  |  |  | =head2 node2string($options, $is_last_node, $node, $vert_dashes) | 
| 954 |  |  |  |  |  |  |  | 
| 955 |  |  |  |  |  |  | Returns a string of the node's name and attributes, with a leading indent, suitable for printing. | 
| 956 |  |  |  |  |  |  |  | 
| 957 |  |  |  |  |  |  | Possible keys in the $options hashref: | 
| 958 |  |  |  |  |  |  |  | 
| 959 |  |  |  |  |  |  | =over 4 | 
| 960 |  |  |  |  |  |  |  | 
| 961 |  |  |  |  |  |  | =item o no_attributes => $Boolean | 
| 962 |  |  |  |  |  |  |  | 
| 963 |  |  |  |  |  |  | If 1, the node's attributes are not included in the string returned. | 
| 964 |  |  |  |  |  |  |  | 
| 965 |  |  |  |  |  |  | Default: 0 (include attributes). | 
| 966 |  |  |  |  |  |  |  | 
| 967 |  |  |  |  |  |  | =back | 
| 968 |  |  |  |  |  |  |  | 
| 969 |  |  |  |  |  |  | Ignore the parameter $vert_dashes. The code uses it as temporary storage. | 
| 970 |  |  |  |  |  |  |  | 
| 971 |  |  |  |  |  |  | Calls L. | 
| 972 |  |  |  |  |  |  |  | 
| 973 |  |  |  |  |  |  | Called by L. | 
| 974 |  |  |  |  |  |  |  | 
| 975 |  |  |  |  |  |  | =head2 options([$bit_string]) | 
| 976 |  |  |  |  |  |  |  | 
| 977 |  |  |  |  |  |  | Here, the [] indicate an optional parameter. | 
| 978 |  |  |  |  |  |  |  | 
| 979 |  |  |  |  |  |  | Get or set the option flags. | 
| 980 |  |  |  |  |  |  |  | 
| 981 |  |  |  |  |  |  | For typical usage, see scripts/synopsis.pl. | 
| 982 |  |  |  |  |  |  |  | 
| 983 |  |  |  |  |  |  | See the L for details. | 
| 984 |  |  |  |  |  |  |  | 
| 985 |  |  |  |  |  |  | 'options' is a parameter to L. See L for details. | 
| 986 |  |  |  |  |  |  |  | 
| 987 |  |  |  |  |  |  | =head2 parse([$string]) | 
| 988 |  |  |  |  |  |  |  | 
| 989 |  |  |  |  |  |  | Here, the [] indicate an optional parameter. | 
| 990 |  |  |  |  |  |  |  | 
| 991 |  |  |  |  |  |  | This is the only method the user needs to call. All data can be supplied when calling L. | 
| 992 |  |  |  |  |  |  |  | 
| 993 |  |  |  |  |  |  | You can of course call other methods (e.g. L ) after calling L but | 
| 994 |  |  |  |  |  |  | before calling C. | 
| 995 |  |  |  |  |  |  |  | 
| 996 |  |  |  |  |  |  | Note: If a string is passed to C, it takes precedence over any string passed to | 
| 997 |  |  |  |  |  |  | C<< new(template => $string) >>, and over any string passed to L. Further, | 
| 998 |  |  |  |  |  |  | the string passed to C is passed to L, meaning any subsequent | 
| 999 |  |  |  |  |  |  | call to C returns the string passed to C. | 
| 1000 |  |  |  |  |  |  |  | 
| 1001 |  |  |  |  |  |  | See scripts/samples.pl. | 
| 1002 |  |  |  |  |  |  |  | 
| 1003 |  |  |  |  |  |  | Returns 0 for success and 1 for failure. | 
| 1004 |  |  |  |  |  |  |  | 
| 1005 |  |  |  |  |  |  | If the value is 1, you should call L to find out what happened. | 
| 1006 |  |  |  |  |  |  |  | 
| 1007 |  |  |  |  |  |  | =head2 size_report() | 
| 1008 |  |  |  |  |  |  |  | 
| 1009 |  |  |  |  |  |  | Prints some statistics for the sizes of various integers (short, int, long, etc). | 
| 1010 |  |  |  |  |  |  |  | 
| 1011 |  |  |  |  |  |  | See scripts/synopsis.pl. | 
| 1012 |  |  |  |  |  |  |  | 
| 1013 |  |  |  |  |  |  | =head2 template([$string]) | 
| 1014 |  |  |  |  |  |  |  | 
| 1015 |  |  |  |  |  |  | Here, the [] indicate an optional parameter. | 
| 1016 |  |  |  |  |  |  |  | 
| 1017 |  |  |  |  |  |  | Get or set the string to be parsed. | 
| 1018 |  |  |  |  |  |  |  | 
| 1019 |  |  |  |  |  |  | 'template' is a parameter to L. See L for details. | 
| 1020 |  |  |  |  |  |  |  | 
| 1021 |  |  |  |  |  |  | =head2 template_report | 
| 1022 |  |  |  |  |  |  |  | 
| 1023 |  |  |  |  |  |  | Get the string output from the parse. The code generates this string by walking the nodes of the | 
| 1024 |  |  |  |  |  |  | L returned by a call to C<< $self -> tree() >>. | 
| 1025 |  |  |  |  |  |  |  | 
| 1026 |  |  |  |  |  |  | Apart from perhaps spacing, it will be identical to the string passed in to be parsed. | 
| 1027 |  |  |  |  |  |  |  | 
| 1028 |  |  |  |  |  |  | See t/test.t. | 
| 1029 |  |  |  |  |  |  |  | 
| 1030 |  |  |  |  |  |  | =head2 tree() | 
| 1031 |  |  |  |  |  |  |  | 
| 1032 |  |  |  |  |  |  | Returns an object of type L, which holds the parsed data. | 
| 1033 |  |  |  |  |  |  |  | 
| 1034 |  |  |  |  |  |  | Obviously, it only makes sense to call C after calling L. | 
| 1035 |  |  |  |  |  |  |  | 
| 1036 |  |  |  |  |  |  | See scripts/traverse.pl for sample code which processes this tree's nodes. | 
| 1037 |  |  |  |  |  |  |  | 
| 1038 |  |  |  |  |  |  | If you wish to save the tree before calling L again, call: | 
| 1039 |  |  |  |  |  |  |  | 
| 1040 |  |  |  |  |  |  | my($tree) = $parser -> tree -> clone(); | 
| 1041 |  |  |  |  |  |  |  | 
| 1042 |  |  |  |  |  |  | Later you can then do this to process $tree instead of $parser's tree: | 
| 1043 |  |  |  |  |  |  |  | 
| 1044 |  |  |  |  |  |  | print join("\n", @{$parser -> tree2string({}, $tree)}), "\n"; | 
| 1045 |  |  |  |  |  |  |  | 
| 1046 |  |  |  |  |  |  | =head2 tree2string($options, [$some_tree]) | 
| 1047 |  |  |  |  |  |  |  | 
| 1048 |  |  |  |  |  |  | Here, the [] represent an optional parameter. | 
| 1049 |  |  |  |  |  |  |  | 
| 1050 |  |  |  |  |  |  | If $some_tree is not supplied, uses the calling object's tree ($self -> tree). | 
| 1051 |  |  |  |  |  |  |  | 
| 1052 |  |  |  |  |  |  | Returns an arrayref of lines, suitable for printing. These lines do not end in "\n". | 
| 1053 |  |  |  |  |  |  |  | 
| 1054 |  |  |  |  |  |  | Draws a nice ASCII-art representation of the tree structure. | 
| 1055 |  |  |  |  |  |  |  | 
| 1056 |  |  |  |  |  |  | The tree looks like: | 
| 1057 |  |  |  |  |  |  |  | 
| 1058 |  |  |  |  |  |  | Root. Attributes: {# => "0"} | 
| 1059 |  |  |  |  |  |  | |--- I. Attributes: {# => "1"} | 
| 1060 |  |  |  |  |  |  | |   |--- J. Attributes: {# => "3"} | 
| 1061 |  |  |  |  |  |  | |   |   |--- K. Attributes: {# => "3"} | 
| 1062 |  |  |  |  |  |  | |   |--- J. Attributes: {# => "4"} | 
| 1063 |  |  |  |  |  |  | |       |--- L. Attributes: {# => "5"} | 
| 1064 |  |  |  |  |  |  | |           |--- M. Attributes: {# => "5"} | 
| 1065 |  |  |  |  |  |  | |               |--- N. Attributes: {# => "5"} | 
| 1066 |  |  |  |  |  |  | |                   |--- O. Attributes: {# => "5"} | 
| 1067 |  |  |  |  |  |  | |--- H. Attributes: {# => "2"} | 
| 1068 |  |  |  |  |  |  | |   |--- J. Attributes: {# => "3"} | 
| 1069 |  |  |  |  |  |  | |   |   |--- K. Attributes: {# => "3"} | 
| 1070 |  |  |  |  |  |  | |   |--- J. Attributes: {# => "4"} | 
| 1071 |  |  |  |  |  |  | |       |--- L. Attributes: {# => "5"} | 
| 1072 |  |  |  |  |  |  | |           |--- M. Attributes: {# => "5"} | 
| 1073 |  |  |  |  |  |  | |               |--- N. Attributes: {# => "5"} | 
| 1074 |  |  |  |  |  |  | |                   |--- O. Attributes: {# => "5"} | 
| 1075 |  |  |  |  |  |  |  | 
| 1076 |  |  |  |  |  |  | Or, without attributes: | 
| 1077 |  |  |  |  |  |  |  | 
| 1078 |  |  |  |  |  |  | Root | 
| 1079 |  |  |  |  |  |  | |--- I | 
| 1080 |  |  |  |  |  |  | |   |--- J | 
| 1081 |  |  |  |  |  |  | |   |   |--- K | 
| 1082 |  |  |  |  |  |  | |   |--- J | 
| 1083 |  |  |  |  |  |  | |       |--- L | 
| 1084 |  |  |  |  |  |  | |           |--- M | 
| 1085 |  |  |  |  |  |  | |               |--- N | 
| 1086 |  |  |  |  |  |  | |                   |--- O | 
| 1087 |  |  |  |  |  |  | |--- H | 
| 1088 |  |  |  |  |  |  | |   |--- J | 
| 1089 |  |  |  |  |  |  | |   |   |--- K | 
| 1090 |  |  |  |  |  |  | |   |--- J | 
| 1091 |  |  |  |  |  |  | |       |--- L | 
| 1092 |  |  |  |  |  |  | |           |--- M | 
| 1093 |  |  |  |  |  |  | |               |--- N | 
| 1094 |  |  |  |  |  |  | |                   |--- O | 
| 1095 |  |  |  |  |  |  |  | 
| 1096 |  |  |  |  |  |  | See scripts/samples.pl. | 
| 1097 |  |  |  |  |  |  |  | 
| 1098 |  |  |  |  |  |  | Example usage: | 
| 1099 |  |  |  |  |  |  |  | 
| 1100 |  |  |  |  |  |  | print map("$_\n", @{$tree -> tree2string}); | 
| 1101 |  |  |  |  |  |  |  | 
| 1102 |  |  |  |  |  |  | Can be called with $some_tree set to any $node, and will print the tree assuming $node is the root. | 
| 1103 |  |  |  |  |  |  |  | 
| 1104 |  |  |  |  |  |  | If you don't wish to supply options, use tree2string({}, $node). | 
| 1105 |  |  |  |  |  |  |  | 
| 1106 |  |  |  |  |  |  | Possible keys in the $options hashref (which defaults to {}): | 
| 1107 |  |  |  |  |  |  |  | 
| 1108 |  |  |  |  |  |  | =over 4 | 
| 1109 |  |  |  |  |  |  |  | 
| 1110 |  |  |  |  |  |  | =item o no_attributes => $Boolean | 
| 1111 |  |  |  |  |  |  |  | 
| 1112 |  |  |  |  |  |  | If 1, the node's attributes are not included in the string returned. | 
| 1113 |  |  |  |  |  |  |  | 
| 1114 |  |  |  |  |  |  | Default: 0 (include attributes). | 
| 1115 |  |  |  |  |  |  |  | 
| 1116 |  |  |  |  |  |  | =back | 
| 1117 |  |  |  |  |  |  |  | 
| 1118 |  |  |  |  |  |  | Calls L. | 
| 1119 |  |  |  |  |  |  |  | 
| 1120 |  |  |  |  |  |  | =head1 FAQ | 
| 1121 |  |  |  |  |  |  |  | 
| 1122 |  |  |  |  |  |  | =head2 Where are the error messages and numbers described? | 
| 1123 |  |  |  |  |  |  |  | 
| 1124 |  |  |  |  |  |  | See L and L. | 
| 1125 |  |  |  |  |  |  |  | 
| 1126 |  |  |  |  |  |  | =head2 What are the possible values for the 'options' parameter to new()? | 
| 1127 |  |  |  |  |  |  |  | 
| 1128 |  |  |  |  |  |  | Firstly, to make these constants available, you must say: | 
| 1129 |  |  |  |  |  |  |  | 
| 1130 |  |  |  |  |  |  | use MarpaX::Languages::Perl::PackUnpack ':constants'; | 
| 1131 |  |  |  |  |  |  |  | 
| 1132 |  |  |  |  |  |  | Secondly, more detail on errors and warnings can be found at L. | 
| 1133 |  |  |  |  |  |  |  | 
| 1134 |  |  |  |  |  |  | Thirdly, for usage of these option flags, see scripts/*.pl. | 
| 1135 |  |  |  |  |  |  |  | 
| 1136 |  |  |  |  |  |  | Now the flags themselves: | 
| 1137 |  |  |  |  |  |  |  | 
| 1138 |  |  |  |  |  |  | =over 4 | 
| 1139 |  |  |  |  |  |  |  | 
| 1140 |  |  |  |  |  |  | =item o nothing_is_fatal | 
| 1141 |  |  |  |  |  |  |  | 
| 1142 |  |  |  |  |  |  | This is the default. | 
| 1143 |  |  |  |  |  |  |  | 
| 1144 |  |  |  |  |  |  | It's value is 0. | 
| 1145 |  |  |  |  |  |  |  | 
| 1146 |  |  |  |  |  |  | =item o debug | 
| 1147 |  |  |  |  |  |  |  | 
| 1148 |  |  |  |  |  |  | Print extra stuff if this flag is set. | 
| 1149 |  |  |  |  |  |  |  | 
| 1150 |  |  |  |  |  |  | It's value is 1. | 
| 1151 |  |  |  |  |  |  |  | 
| 1152 |  |  |  |  |  |  | =item o print_warnings | 
| 1153 |  |  |  |  |  |  |  | 
| 1154 |  |  |  |  |  |  | Print various warnings if this flag is set: | 
| 1155 |  |  |  |  |  |  |  | 
| 1156 |  |  |  |  |  |  | =over 4 | 
| 1157 |  |  |  |  |  |  |  | 
| 1158 |  |  |  |  |  |  | =item o The ambiguity status and terminals expected, if the parse is ambiguous | 
| 1159 |  |  |  |  |  |  |  | 
| 1160 |  |  |  |  |  |  | =item o See L for other warnings which might be printed | 
| 1161 |  |  |  |  |  |  |  | 
| 1162 |  |  |  |  |  |  | Ambiguity is not, in and of itself, an error. But see the C option, below. | 
| 1163 |  |  |  |  |  |  |  | 
| 1164 |  |  |  |  |  |  | =back | 
| 1165 |  |  |  |  |  |  |  | 
| 1166 |  |  |  |  |  |  | It's tempting to call this option C, but Perl already has C | 
| 1167 |  |  |  |  |  |  |  | 
| 1168 |  |  |  |  |  |  | It's value is 2. | 
| 1169 |  |  |  |  |  |  |  | 
| 1170 |  |  |  |  |  |  | =item o ambiguity_is_fatal | 
| 1171 |  |  |  |  |  |  |  | 
| 1172 |  |  |  |  |  |  | This makes L return 1 rather than -1. | 
| 1173 |  |  |  |  |  |  |  | 
| 1174 |  |  |  |  |  |  | It's value is 4. | 
| 1175 |  |  |  |  |  |  |  | 
| 1176 |  |  |  |  |  |  | =back | 
| 1177 |  |  |  |  |  |  |  | 
| 1178 |  |  |  |  |  |  | =head2 How do I print the tree built by the parser? | 
| 1179 |  |  |  |  |  |  |  | 
| 1180 |  |  |  |  |  |  | See L. | 
| 1181 |  |  |  |  |  |  |  | 
| 1182 |  |  |  |  |  |  | =head2 How do I make use of the tree built by the parser? | 
| 1183 |  |  |  |  |  |  |  | 
| 1184 |  |  |  |  |  |  | See scripts/traverse.pl. | 
| 1185 |  |  |  |  |  |  |  | 
| 1186 |  |  |  |  |  |  | =head2 How is the parsed data held in RAM? | 
| 1187 |  |  |  |  |  |  |  | 
| 1188 |  |  |  |  |  |  | The parsed output is held in a tree managed by L. | 
| 1189 |  |  |  |  |  |  |  | 
| 1190 |  |  |  |  |  |  | The tree always has a root node, which has nothing to do with the input data. So, even an empty | 
| 1191 |  |  |  |  |  |  | imput string will produce a tree with 1 node. This root has an empty hashref associated with it. | 
| 1192 |  |  |  |  |  |  |  | 
| 1193 |  |  |  |  |  |  | Nodes have a name (accessed with the C method) and a hashref of attributes (accessed | 
| 1194 |  |  |  |  |  |  | with the C method). | 
| 1195 |  |  |  |  |  |  |  | 
| 1196 |  |  |  |  |  |  | The name indicates the type of node. Names are one of these literals: | 
| 1197 |  |  |  |  |  |  |  | 
| 1198 |  |  |  |  |  |  | =over 4 | 
| 1199 |  |  |  |  |  |  |  | 
| 1200 |  |  |  |  |  |  | =item o 'token' | 
| 1201 |  |  |  |  |  |  |  | 
| 1202 |  |  |  |  |  |  | If the node's name is 'token', then the node represents one of the template characters listed in the | 
| 1203 |  |  |  |  |  |  | first table in L. Note: both | 
| 1204 |  |  |  |  |  |  | '(' and ')' are called 'token'. | 
| 1205 |  |  |  |  |  |  |  | 
| 1206 |  |  |  |  |  |  | =item o $lexeme_name | 
| 1207 |  |  |  |  |  |  |  | 
| 1208 |  |  |  |  |  |  | This means all other lexemes identified in the parse have as their node name the name of a lexeme | 
| 1209 |  |  |  |  |  |  | as given in the grammar returned by the L method. The actual lexeme in question is the one | 
| 1210 |  |  |  |  |  |  | used to identify a substring of the input template. | 
| 1211 |  |  |  |  |  |  |  | 
| 1212 |  |  |  |  |  |  | =back | 
| 1213 |  |  |  |  |  |  |  | 
| 1214 |  |  |  |  |  |  | See the following hashref for details. | 
| 1215 |  |  |  |  |  |  |  | 
| 1216 |  |  |  |  |  |  | For each node, the attributes hashref contains 2 keys: | 
| 1217 |  |  |  |  |  |  |  | 
| 1218 |  |  |  |  |  |  | =over 4 | 
| 1219 |  |  |  |  |  |  |  | 
| 1220 |  |  |  |  |  |  | =item o lexeme => $lexeme_name | 
| 1221 |  |  |  |  |  |  |  | 
| 1222 |  |  |  |  |  |  | This is always $lexeme_name (as just above), even in those cases where the node's name | 
| 1223 |  |  |  |  |  |  | is 'token'. | 
| 1224 |  |  |  |  |  |  |  | 
| 1225 |  |  |  |  |  |  | =item o text => $text | 
| 1226 |  |  |  |  |  |  |  | 
| 1227 |  |  |  |  |  |  | This is a substring from the template being parsed. The exact contents and length of this string | 
| 1228 |  |  |  |  |  |  | depend on which lexeme in the input template was recognised, which is identified by the value | 
| 1229 |  |  |  |  |  |  | of the 'lexeme' key. | 
| 1230 |  |  |  |  |  |  |  | 
| 1231 |  |  |  |  |  |  | =back | 
| 1232 |  |  |  |  |  |  |  | 
| 1233 |  |  |  |  |  |  | See scripts/traverse.pl, which prints a few trees differently than what happens when | 
| 1234 |  |  |  |  |  |  | L is called. | 
| 1235 |  |  |  |  |  |  |  | 
| 1236 |  |  |  |  |  |  | =head2 What is the homepage of Marpa? | 
| 1237 |  |  |  |  |  |  |  | 
| 1238 |  |  |  |  |  |  | L. | 
| 1239 |  |  |  |  |  |  |  | 
| 1240 |  |  |  |  |  |  | That page has a long list of links. | 
| 1241 |  |  |  |  |  |  |  | 
| 1242 |  |  |  |  |  |  | =head2 How do I run author tests? | 
| 1243 |  |  |  |  |  |  |  | 
| 1244 |  |  |  |  |  |  | This runs both standard and author tests: | 
| 1245 |  |  |  |  |  |  |  | 
| 1246 |  |  |  |  |  |  | shell> perl Build.PL; ./Build; ./Build authortest | 
| 1247 |  |  |  |  |  |  |  | 
| 1248 |  |  |  |  |  |  | =head1 See Also | 
| 1249 |  |  |  |  |  |  |  | 
| 1250 |  |  |  |  |  |  | L. | 
| 1251 |  |  |  |  |  |  |  | 
| 1252 |  |  |  |  |  |  | L. | 
| 1253 |  |  |  |  |  |  |  | 
| 1254 |  |  |  |  |  |  | L. | 
| 1255 |  |  |  |  |  |  |  | 
| 1256 |  |  |  |  |  |  | L and L. | 
| 1257 |  |  |  |  |  |  |  | 
| 1258 |  |  |  |  |  |  | =head1 Machine-Readable Change Log | 
| 1259 |  |  |  |  |  |  |  | 
| 1260 |  |  |  |  |  |  | The file Changes was converted into Changelog.ini by L. | 
| 1261 |  |  |  |  |  |  |  | 
| 1262 |  |  |  |  |  |  | =head1 Version Numbers | 
| 1263 |  |  |  |  |  |  |  | 
| 1264 |  |  |  |  |  |  | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | 
| 1265 |  |  |  |  |  |  |  | 
| 1266 |  |  |  |  |  |  | =head1 Repository | 
| 1267 |  |  |  |  |  |  |  | 
| 1268 |  |  |  |  |  |  | L | 
| 1269 |  |  |  |  |  |  |  | 
| 1270 |  |  |  |  |  |  | =head1 Support | 
| 1271 |  |  |  |  |  |  |  | 
| 1272 |  |  |  |  |  |  | Email the author, or log a bug on RT: | 
| 1273 |  |  |  |  |  |  |  | 
| 1274 |  |  |  |  |  |  | L. | 
| 1275 |  |  |  |  |  |  |  | 
| 1276 |  |  |  |  |  |  | =head1 Author | 
| 1277 |  |  |  |  |  |  |  | 
| 1278 |  |  |  |  |  |  | L was written by Ron Savage Iron@savage.net.auE> in 2015. | 
| 1279 |  |  |  |  |  |  |  | 
| 1280 |  |  |  |  |  |  | Marpa's homepage: L. | 
| 1281 |  |  |  |  |  |  |  | 
| 1282 |  |  |  |  |  |  | My homepage: L. | 
| 1283 |  |  |  |  |  |  |  | 
| 1284 |  |  |  |  |  |  | =head1 Copyright | 
| 1285 |  |  |  |  |  |  |  | 
| 1286 |  |  |  |  |  |  | Australian copyright (c) 2015, Ron Savage. | 
| 1287 |  |  |  |  |  |  |  | 
| 1288 |  |  |  |  |  |  | All Programs of mine are 'OSI Certified Open Source Software'; | 
| 1289 |  |  |  |  |  |  | you can redistribute them and/or modify them under the terms of | 
| 1290 |  |  |  |  |  |  | The Artistic License 2.0, a copy of which is available at: | 
| 1291 |  |  |  |  |  |  | http://opensource.org/licenses/alphabetical. | 
| 1292 |  |  |  |  |  |  |  | 
| 1293 |  |  |  |  |  |  | =cut |