| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | |||
| 5 | my $FIELD = join( '|', qw( parent first_child last_child prev_sibling next_sibling pcdata cdata ent data target cdata pcdata comment flushed)); | ||
| 6 | my $PRIVATE = join( '|', qw( parent first_child last_child prev_sibling next_sibling pcdata cdata comment | ||
| 7 | extra_data_in_pcdata extra_data_before_end_tag | ||
| 8 | ) | ||
| 9 | ); # _$private is inlined | ||
| 10 | my $FORMER = join( '|', qw( parent prev_sibling next_sibling)); # former_$former is inlined | ||
| 11 | my $SET_FIELD = join( '|', qw( first_child next_sibling ent data pctarget comment flushed)); | ||
| 12 | my $SET_NOT_EMPTY= join( '|', qw( pcdata cdata comment)); # set the field | ||
| 13 | |||
| 14 | my $var= '(\$[a-z_]+(?:\[\d\])?|\$t(?:wig)?->root|\$t(?:wig)?->twig_current|\$t(?:wig)?->\{\'?twig_root\'?\}|\$t(?:wig)?->\{\'?twig_current\'?\})'; | ||
| 15 | |||
| 16 | my $set_to = '(?:undef|\$\w+|\$\w+->\{\w+\}|\$\w+->\w+|\$\w+->\w+\([^)]+\))'; | ||
| 17 | my $elt = '\$(?:elt|new_elt|child|cdata|ent|_?parent|twig_current|next_sibling|first_child|prev_sibling|last_child|ref|elt->_parent)'; | ||
| 18 | |||
| 19 | |||
| 20 | my %gi2index=( '', 0, PCDATA => 1, CDATA => 2, PI => 3, COMMENT => 4, ENT => 5); | ||
| 21 | |||
| 22 | (my $version= $])=~ s{\.}{}g; | ||
| 23 | |||
| 24 | my $in_pod = 0; # do not change the POD! | ||
| 25 | |||
| 26 | while( <>) | ||
| 27 | { | ||
| 28 | $in_pod = 1 if m{^__END__}; | ||
| 29 | # do not fix the pod | ||
| 30 | if ($in_pod) { | ||
| 31 | print $_; | ||
| 32 | next; | ||
| 33 | } | ||
| 34 | |||
| 35 | if( /=/) | ||
| 36 | { s/$var->_children/do { my \$elt= $1; my \@children=(); my \$child= \$elt->_first_child; while( \$child) { push \@children, \$child; \$child= \$child->_next_sibling; } \@children; }/; } | ||
| 37 | |||
| 38 | s/$var->set_gi\(\s*(PCDATA|CDATA|PI|COMMENT|ENT)\s*\)/$1\->{gi}= $gi2index{$2}/; | ||
| 39 | |||
| 40 | s/$var->del_(twig_current)/delete $1\->{'$2'}/g; | ||
| 41 | s/$var->set_(twig_current)/$1\->{'$2'}=1/g; | ||
| 42 | s/$var->_del_(flushed)/delete $1\->{'$2'}/g; | ||
| 43 | s/$var->_set_(flushed)/$1\->{'$2'}=1/g; | ||
| 44 | s/$var->_(flushed)/$1\->{'$2'}/g; | ||
| 45 | |||
| 46 | s/$var->set_($SET_FIELD)\(([^)]*)\)/$1\->\{$2\}= $3/g; | ||
| 47 | s/$var->($FIELD)\b(?!\()/$1\->\{$2\}/g; | ||
| 48 | s/$var->_($PRIVATE)\b(\s*\(\s*\))?(?!\s*\()/$1\->\{$2\}/g; | ||
| 49 | |||
| 50 | s{($elt)->former_($FORMER)}{($1\->{former} && $1\->{former}\->{$2})}g; | ||
| 51 | |||
| 52 | s{($elt)->set_(parent|prev_sibling)\(\s*($set_to)\s*\)}{$1\->\{$2\}=$3; weaken( $1\->\{$2\}); }g; | ||
| 53 | s{($elt)->set_(first_child)\(\s*($set_to)\s*\)}{ $1\->set_not_empty; $1\->\{$2\}=$3; }g; | ||
| 54 | s{($elt)->set_(next_sibling)\(\s*($set_to)\s*\)}{ $1\->\{$2\}=$3; }g; | ||
| 55 | s{($elt)->set_(last_child)\(\s*($set_to)\s*\)}{ $1\->set_not_empty; $1\->\{$2\}=$3; weaken( $1\->\{$2\}); }g; | ||
| 56 | |||
| 57 | s/$var->atts/$1\->{att}/g; | ||
| 58 | |||
| 59 | s/$var->append_(pcdata|cdata)\(([^)]*)\)/$1\->\{$2\}.= $3/g; | ||
| 60 | s/$var->set_($SET_NOT_EMPTY)\(([^)]*)\)/$1\->\{$2\}= (delete $1->\{empty\} || 1) && $3/g; | ||
| 61 | s/$var->_set_($SET_NOT_EMPTY)\s*\(([^)]*)\)/$1\->{$2}= $3/g; | ||
| 62 | |||
| 63 | s/(\$[a-z][a-z_]*(?:\[\d\])?)->gi/\$XML::Twig::index2gi\[$1\->{'gi'}\]/g; | ||
| 64 | |||
| 65 | s/$var->id/$1\->{'att'}->{\$ID}/g; | ||
| 66 | s/$var->att\(\s*([^)]+)\)/$1\->{'att'}->\{$2\}/g; | ||
| 67 | |||
| 68 | s/(\$[a-z][a-z_]*(?:\[\d\])?)->is_pcdata/(exists $1\->{'pcdata'})/g; | ||
| 69 | s/(\$[a-z][a-z_]*(?:\[\d\])?)->is_cdata/(exists $1\->{'cdata'})/g; | ||
| 70 | s/$var->is_pi/(exists $1\->{'target'})/g; | ||
| 71 | s/$var->is_comment/(exists $1\->{'comment'})/g; | ||
| 72 | s/$var->is_ent/(exists $1\->{'ent'})/g; | ||
| 73 | s/(\$,a-z][a-z_]*(?:\[\d\])?)->is_text/((exists $1\->{'pcdata'}) || (exists $1\->{'cdata'}))/g; | ||
| 74 | |||
| 75 | s/$var->is_empty/$1\->{'empty'}/g; | ||
| 76 | s/$var->set_empty(?:\(([^)]*)\))?(?!_)/"$1\->{empty}= " . ($2 || 1)/ge; | ||
| 77 | s/$var->set_not_empty/delete $1\->{empty}/g; | ||
| 78 | |||
| 79 | s/$var->_is_private/( (substr( \$XML::Twig::index2gi\[$1\->{'gi'}\], 0, 1) eq '#') && (substr( \$XML::Twig::index2gi\[$1\->{'gi'}\], 0, 9) ne '#default:') )/g; | ||
| 80 | s/_is_private_name\(\s*$var\s*\)/( $1=~ m{^#(?!default:)} )/g; | ||
| 81 | |||
| 82 | s{_is_fh\(\s*$var\)}{isa( $1, 'GLOB') || isa( $1, 'IO::Scalar')}g; | ||
| 83 | |||
| 84 | s/$var->set_gi\s*\(\s*([^)]*)\s*\)/$1\->{gi}=\$XML::Twig::gi2index{$2} or $1->set_gi( $2)/g; | ||
| 85 | |||
| 86 | s/$var->xml_string/$1->sprint( 1)/g; | ||
| 87 | |||
| 88 | print $_ ; | ||
| 89 | } | ||
| 90 |