/[A3C]/lib/A3C/PHP.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/A3C/PHP.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 152 - (show annotations)
Tue Jun 3 19:40:12 2008 UTC (11 years, 10 months ago) by dpavlin
File size: 2922 byte(s)
fix pod
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 use Encode qw/decode/;
8
9 =head1 NAME
10
11 A3C::PHP
12
13 =head1 DESCRIPTION
14
15 For integration with php application there is simple php config
16 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
26
27 =cut
28
29 our $debug = 0;
30 our $charset = 'iso-8859-2';
31
32 our $perl = '';
33 our $data;
34 my $grammar =
35
36 <<'GRAMMAR';
37
38 php_vars: php_start statement(s) php_end
39
40 php_start: /\s*<\?(php)?\s*/
41
42 php_end: /\s*\?>\s*/
43
44 statement: comment | assignment | function
45
46 comment: /\s*(\#|\/\/).*/
47
48 function: /(include)\(.*?\)\s*;/
49 {
50 $return = "## ignored $1\n";
51 }
52
53 assignment: ( var_assign | hash_assign | array_assign | constant ) /;/
54 {
55 $A3C::PHP::perl .= $item[1];
56 }
57
58 var_assign: variable /=/ scalar
59 {
60 $item[1] =~ s/^\$//;
61 $return = "\$data->{ $item[1] } = $item[3]; # scalar\n";
62 }
63
64 hash_assign: variable /=/ /Array\s*\(/i pair(s /,/) /\s*(,\s*)?\)/
65 {
66 $item[1] =~ s/^\$//;
67 $return = "\$data->{ $item[1] } = $item[4]; # hash\n";
68 $return = "\%{\$data->{ $item[1] }} =(" . join( ',', @{$item[4]} ) . ");\t# hash\n";
69 }
70
71 array_assign: variable /=/ /Array\s*\(/i element(s /,/) /\s*(,\s*)?\)/
72 {
73 $item[1] =~ s/^\$//;
74 $return = "\@{\$data->{ $item[1] }}=(" . join( ',', @{$item[4]} ) . ");\t# array\n"
75 }
76
77 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_]*/
91
92 number: /-?[0-9.]+/
93
94 string: double_quoted | single_quoted
95
96 double_quoted: /"(\\"|[^"])*?"/
97 {
98 $item[1] =~ s/\@/\\\@/g;
99 $return = $item[1];
100 }
101
102 single_quoted: /'(\\'|[^'])*?'/
103
104 element: scalar | bareword
105
106 pair: scalar /=>/ ( scalar | bareword )
107 {
108 $return = $item[1] . '=>' . $item[3];
109 }
110
111 bareword: /[0-9a-zA-Z_]+/
112
113 constant: /define\s*\(/ string /,/ scalar /\)/
114 {
115 $return = "use constant $item[2] => $item[4];\n";
116 }
117
118 comments: /^#.*$/
119
120 whitespace: /^\s+$/
121
122 GRAMMAR
123
124 =head1 parse
125
126 my $data = A3C::PHP->parse( $php_code );
127
128 =cut
129
130 sub parse {
131 my $self = shift;
132 my $php = shift or die "no php?";
133 $perl = '';
134 if ( $debug ) {
135 $::RD_TRACE = 1;
136 warn "PHP [$charset]: $php\n";
137 }
138 my $parser = Parse::RecDescent->new( $grammar );
139 $parser->php_vars( decode($charset,$php) );
140 warn "## GENERATED PERL:\n\n", $perl, "\n\n" if $debug;
141 my $data;
142 eval $perl;
143 die "$@" if $@;
144 return $data;
145 }
146
147 =head1 parse_file
148
149 my $data = A3C::PHP->parse_file( '/path/to/code.php' );
150
151 =cut
152
153 sub parse_file {
154 my $self = shift;
155 $self->parse( scalar read_file( shift ));
156 }
157
158 =head1 SEE ALSO
159
160 =over 4
161
162 =item * PHP::Include
163
164 =item * Parse::RecDescent
165
166 =back
167
168 =head1 AUTHORS
169
170 =over 4
171
172 =item * Ed Summers <ehs@pobox.com> wrote L<PHP::Include> on which this code is based
173
174 =cut
175
176 1;

  ViewVC Help
Powered by ViewVC 1.1.26