4 |
use Parse::RecDescent; |
use Parse::RecDescent; |
5 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
6 |
use File::Slurp; |
use File::Slurp; |
7 |
|
use Encode qw/decode/; |
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
15 |
For integration with php application there is simple php config |
For integration with php application there is simple php config |
16 |
parser based on L<PHP::Include::Vars> |
parser based on L<PHP::Include::Vars> |
17 |
|
|
18 |
|
It currently ignores C<include> PHP function. |
19 |
|
|
20 |
|
=head1 CONFIG |
21 |
|
|
22 |
|
$A3C::PHP::debug = 0; |
23 |
|
$A3C::PHP::charset = 'iso-8859-2'; |
24 |
|
|
25 |
=head1 METHODS |
=head1 METHODS |
26 |
|
|
27 |
=cut |
=cut |
28 |
|
|
29 |
my $debug = 0; |
our $debug = 0; |
30 |
|
our $charset = 'iso-8859-2'; |
31 |
|
|
32 |
our $perl = ''; |
our $perl = ''; |
33 |
our $data; |
our $data; |
41 |
|
|
42 |
php_end: /\s*\?>\s*/ |
php_end: /\s*\?>\s*/ |
43 |
|
|
44 |
statement: comment | assignment |
statement: comment | assignment | function |
45 |
|
|
46 |
comment: /\s*(\#|\/\/).*/ |
comment: /\s*(\#|\/\/).*/ |
47 |
|
|
48 |
|
function: /(include)\(.*?\)\s*;/ |
49 |
|
{ |
50 |
|
$return = "## ignored $1\n"; |
51 |
|
} |
52 |
|
|
53 |
assignment: ( var_assign | hash_assign | array_assign | constant ) /;/ |
assignment: ( var_assign | hash_assign | array_assign | constant ) /;/ |
54 |
{ |
{ |
55 |
$A3C::PHP::perl .= $item[1]; |
$A3C::PHP::perl .= $item[1]; |
56 |
} |
} |
57 |
|
|
58 |
var_assign: variable /=/ scalar |
var_assign: variable /=/ scalar |
59 |
{ |
{ |
60 |
$item[1] =~ s/^\$//; |
$item[1] =~ s/^\$//; |
61 |
$return = "\$data->{ $item[1] } = $item[3]; # scalar\n"; |
$return = "\$data->{ $item[1] } = $item[3]; # scalar\n"; |
62 |
} |
} |
63 |
|
|
64 |
hash_assign: variable /=/ /Array\s*\(/i pair(s /,/) /\s*(,\s*)?\)/ |
hash_assign: variable /=/ /Array\s*\(/i pair(s /,/) /\s*(,\s*)?\)/ |
65 |
{ |
{ |
66 |
$item[1] =~ s/^\$//; |
$item[1] =~ s/^\$//; |
67 |
$return = "\$data->{ $item[1] } = $item[4]; # hash\n"; |
$return = "\$data->{ $item[1] } = $item[4]; # hash\n"; |
68 |
$return = "\%{\$data->{ $item[1] }} =(" . join( ',', @{$item[4]} ) . ");\t# hash\n"; |
$return = "\%{\$data->{ $item[1] }} =(" . join( ',', @{$item[4]} ) . ");\t# hash\n"; |
69 |
} |
} |
70 |
|
|
71 |
array_assign: variable /=/ /Array\s*\(/i element(s /,/) /\s*(,\s*)?\)/ |
array_assign: variable /=/ /Array\s*\(/i element(s /,/) /\s*(,\s*)?\)/ |
72 |
{ |
{ |
73 |
$item[1] =~ s/^\$//; |
$item[1] =~ s/^\$//; |
74 |
$return = "\@{\$data->{ $item[1] }}=(" . join( ',', @{$item[4]} ) . ");\t# array\n" |
$return = "\@{\$data->{ $item[1] }}=(" . join( ',', @{$item[4]} ) . ");\t# array\n" |
75 |
} |
} |
76 |
|
|
77 |
scalar: string | number |
scalar: string | number | constant |
78 |
|
|
79 |
|
constant: true | false |
80 |
|
|
81 |
|
true: 'true' |
82 |
|
{ |
83 |
|
$return = '1'; |
84 |
|
} |
85 |
|
false: 'false' |
86 |
|
{ |
87 |
|
$return = '0'; |
88 |
|
} |
89 |
|
|
90 |
variable: /\$[a-zA-Z_][0-9a-zA-Z_]*/ |
variable: /\$[a-zA-Z_][0-9a-zA-Z_]*/ |
91 |
|
|
94 |
string: double_quoted | single_quoted |
string: double_quoted | single_quoted |
95 |
|
|
96 |
double_quoted: /".*?"/ |
double_quoted: /".*?"/ |
97 |
|
{ |
98 |
|
$item[1] =~ s/\@/\\\@/g; |
99 |
|
$return = $item[1]; |
100 |
|
} |
101 |
|
|
102 |
single_quoted: /'.*?'/ |
single_quoted: /'.*?'/ |
103 |
|
|
105 |
|
|
106 |
pair: scalar /=>/ ( scalar | bareword ) |
pair: scalar /=>/ ( scalar | bareword ) |
107 |
{ |
{ |
108 |
$return = $item[1] . '=>' . $item[3]; |
$return = $item[1] . '=>' . $item[3]; |
109 |
} |
} |
110 |
|
|
111 |
bareword: /[0-9a-zA-Z_]+/ |
bareword: /[0-9a-zA-Z_]+/ |
112 |
|
|
113 |
constant: /define\s*\(/ string /,/ scalar /\)/ |
constant: /define\s*\(/ string /,/ scalar /\)/ |
114 |
{ |
{ |
115 |
$return = "use constant $item[2] => $item[4];\n"; |
$return = "use constant $item[2] => $item[4];\n"; |
116 |
} |
} |
117 |
|
|
118 |
comments: /^#.*$/ |
comments: /^#.*$/ |
130 |
sub parse { |
sub parse { |
131 |
my $self = shift; |
my $self = shift; |
132 |
my $php = shift or die "no php?"; |
my $php = shift or die "no php?"; |
133 |
$perl = ''; |
$perl = ''; |
134 |
$::RD_TRACE = 1 if $debug; |
if ( $debug ) { |
135 |
|
$::RD_TRACE = 1; |
136 |
|
warn "PHP [$charset]: $php\n"; |
137 |
|
} |
138 |
my $parser = Parse::RecDescent->new( $grammar ); |
my $parser = Parse::RecDescent->new( $grammar ); |
139 |
warn dump( $parser->php_vars( $php ) ); |
$parser->php_vars( decode($charset,$php) ); |
140 |
print STDERR "\n\nGENERATED PERL:\n\n", $perl, "\n\n" if $debug; |
warn "## GENERATED PERL:\n\n", $perl, "\n\n" if $debug; |
141 |
my $data; |
my $data; |
142 |
eval $perl; |
eval $perl; |
143 |
die "$@" if $@; |
die "$@" if $@; |