1 |
package A3C::PHP; |
2 |
use warnings; |
3 |
use strict; |
4 |
use Parse::RecDescent; |
5 |
use Data::Dump qw/dump/; |
6 |
use File::Slurp; |
7 |
|
8 |
=head1 NAME |
9 |
|
10 |
A3C::PHP |
11 |
|
12 |
=head1 DESCRIPTION |
13 |
|
14 |
For integration with php application there is simple php config |
15 |
parser based on L<PHP::Include::Vars> |
16 |
|
17 |
It currently ignores C<include> PHP function. |
18 |
|
19 |
=head1 METHODS |
20 |
|
21 |
=cut |
22 |
|
23 |
our $debug = 0; |
24 |
|
25 |
our $perl = ''; |
26 |
our $data; |
27 |
my $grammar = |
28 |
|
29 |
<<'GRAMMAR'; |
30 |
|
31 |
php_vars: php_start statement(s) php_end |
32 |
|
33 |
php_start: /\s*<\?(php)?\s*/ |
34 |
|
35 |
php_end: /\s*\?>\s*/ |
36 |
|
37 |
statement: comment | assignment | function |
38 |
|
39 |
comment: /\s*(\#|\/\/).*/ |
40 |
|
41 |
function: /(include)\(.*?\)\s*;/ |
42 |
{ |
43 |
$return = "## ignored $1\n"; |
44 |
} |
45 |
|
46 |
assignment: ( var_assign | hash_assign | array_assign | constant ) /;/ |
47 |
{ |
48 |
$A3C::PHP::perl .= $item[1]; |
49 |
} |
50 |
|
51 |
var_assign: variable /=/ scalar |
52 |
{ |
53 |
$item[1] =~ s/^\$//; |
54 |
$return = "\$data->{ $item[1] } = $item[3]; # scalar\n"; |
55 |
} |
56 |
|
57 |
hash_assign: variable /=/ /Array\s*\(/i pair(s /,/) /\s*(,\s*)?\)/ |
58 |
{ |
59 |
$item[1] =~ s/^\$//; |
60 |
$return = "\$data->{ $item[1] } = $item[4]; # hash\n"; |
61 |
$return = "\%{\$data->{ $item[1] }} =(" . join( ',', @{$item[4]} ) . ");\t# hash\n"; |
62 |
} |
63 |
|
64 |
array_assign: variable /=/ /Array\s*\(/i element(s /,/) /\s*(,\s*)?\)/ |
65 |
{ |
66 |
$item[1] =~ s/^\$//; |
67 |
$return = "\@{\$data->{ $item[1] }}=(" . join( ',', @{$item[4]} ) . ");\t# array\n" |
68 |
} |
69 |
|
70 |
scalar: string | number | constant |
71 |
|
72 |
constant: true | false |
73 |
|
74 |
true: 'true' |
75 |
{ |
76 |
$return = '1'; |
77 |
} |
78 |
false: 'false' |
79 |
{ |
80 |
$return = '0'; |
81 |
} |
82 |
|
83 |
variable: /\$[a-zA-Z_][0-9a-zA-Z_]*/ |
84 |
|
85 |
number: /-?[0-9.]+/ |
86 |
|
87 |
string: double_quoted | single_quoted |
88 |
|
89 |
double_quoted: /".*?"/ |
90 |
{ |
91 |
$item[1] =~ s/\@/\\\@/g; |
92 |
$return = $item[1]; |
93 |
} |
94 |
|
95 |
single_quoted: /'.*?'/ |
96 |
|
97 |
element: scalar | bareword |
98 |
|
99 |
pair: scalar /=>/ ( scalar | bareword ) |
100 |
{ |
101 |
$return = $item[1] . '=>' . $item[3]; |
102 |
} |
103 |
|
104 |
bareword: /[0-9a-zA-Z_]+/ |
105 |
|
106 |
constant: /define\s*\(/ string /,/ scalar /\)/ |
107 |
{ |
108 |
$return = "use constant $item[2] => $item[4];\n"; |
109 |
} |
110 |
|
111 |
comments: /^#.*$/ |
112 |
|
113 |
whitespace: /^\s+$/ |
114 |
|
115 |
GRAMMAR |
116 |
|
117 |
=head1 parse |
118 |
|
119 |
my $data = A3C::PHP->parse( $php_code ); |
120 |
|
121 |
=cut |
122 |
|
123 |
sub parse { |
124 |
my $self = shift; |
125 |
my $php = shift or die "no php?"; |
126 |
$perl = ''; |
127 |
if ( $debug ) { |
128 |
$::RD_TRACE = 1; |
129 |
warn "PHP: $php\n"; |
130 |
} |
131 |
my $parser = Parse::RecDescent->new( $grammar ); |
132 |
$parser->php_vars( $php ); |
133 |
warn "## GENERATED PERL:\n\n", $perl, "\n\n" if $debug; |
134 |
my $data; |
135 |
eval $perl; |
136 |
die "$@" if $@; |
137 |
return $data; |
138 |
} |
139 |
|
140 |
=head1 parse_file |
141 |
|
142 |
my $data = A3C::PHP->parse_file( '/path/to/code.php' ); |
143 |
|
144 |
=cut |
145 |
|
146 |
sub parse_file { |
147 |
my $self = shift; |
148 |
$self->parse( scalar read_file( shift )); |
149 |
} |
150 |
|
151 |
=head1 SEE ALSO |
152 |
|
153 |
=over 4 |
154 |
|
155 |
=item * PHP::Include |
156 |
|
157 |
=item * Parse::RecDescent |
158 |
|
159 |
=head1 AUTHORS |
160 |
|
161 |
=over 4 |
162 |
|
163 |
=item * Ed Summers <ehs@pobox.com> wrote L<PHP::Include> on which this code is based |
164 |
|
165 |
=cut |
166 |
|
167 |
1; |