/[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 133 - (show annotations)
Fri May 30 20:04:23 2008 UTC (15 years, 10 months ago) by dpavlin
File size: 2472 byte(s)
Added Parse::RecDescent PHP parser based on PHP::Include

PHP::Include puts new variables in current scope, while we
prefer to get single hash with all defined values.
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 =head1 METHODS
18
19 =cut
20
21 my $debug = 0;
22
23 our $perl = '';
24 our $data;
25 my $grammar =
26
27 <<'GRAMMAR';
28
29 php_vars: php_start statement(s) php_end
30
31 php_start: /\s*<\?(php)?\s*/
32
33 php_end: /\s*\?>\s*/
34
35 statement: comment | assignment
36
37 comment: /\s*(\#|\/\/).*/
38
39 assignment: ( var_assign | hash_assign | array_assign | constant ) /;/
40 {
41 $A3C::PHP::perl .= $item[1];
42 }
43
44 var_assign: variable /=/ scalar
45 {
46 $item[1] =~ s/^\$//;
47 $return = "\$data->{ $item[1] } = $item[3]; # scalar\n";
48 }
49
50 hash_assign: variable /=/ /Array\s*\(/i pair(s /,/) /\s*(,\s*)?\)/
51 {
52 $item[1] =~ s/^\$//;
53 $return = "\$data->{ $item[1] } = $item[4]; # hash\n";
54 $return = "\%{\$data->{ $item[1] }} =(" . join( ',', @{$item[4]} ) . ");\t# hash\n";
55 }
56
57 array_assign: variable /=/ /Array\s*\(/i element(s /,/) /\s*(,\s*)?\)/
58 {
59 $item[1] =~ s/^\$//;
60 $return = "\@{\$data->{ $item[1] }}=(" . join( ',', @{$item[4]} ) . ");\t# array\n"
61 }
62
63 scalar: string | number
64
65 variable: /\$[a-zA-Z_][0-9a-zA-Z_]*/
66
67 number: /-?[0-9.]+/
68
69 string: double_quoted | single_quoted
70
71 double_quoted: /".*?"/
72
73 single_quoted: /'.*?'/
74
75 element: scalar | bareword
76
77 pair: scalar /=>/ ( scalar | bareword )
78 {
79 $return = $item[1] . '=>' . $item[3];
80 }
81
82 bareword: /[0-9a-zA-Z_]+/
83
84 constant: /define\s*\(/ string /,/ scalar /\)/
85 {
86 $return = "use constant $item[2] => $item[4];\n";
87 }
88
89 comments: /^#.*$/
90
91 whitespace: /^\s+$/
92
93 GRAMMAR
94
95 =head1 parse
96
97 my $data = A3C::PHP->parse( $php_code );
98
99 =cut
100
101 sub parse {
102 my $self = shift;
103 my $php = shift or die "no php?";
104 $perl = '';
105 $::RD_TRACE = 1 if $debug;
106 my $parser = Parse::RecDescent->new( $grammar );
107 warn dump( $parser->php_vars( $php ) );
108 print STDERR "\n\nGENERATED PERL:\n\n", $perl, "\n\n" if $debug;
109 my $data;
110 eval $perl;
111 die "$@" if $@;
112 return $data;
113 }
114
115 =head1 parse_file
116
117 my $data = A3C::PHP->parse_file( '/path/to/code.php' );
118
119 =cut
120
121 sub parse_file {
122 my $self = shift;
123 $self->parse( scalar read_file( shift ));
124 }
125
126 =head1 SEE ALSO
127
128 =over 4
129
130 =item * PHP::Include
131
132 =item * Parse::RecDescent
133
134 =head1 AUTHORS
135
136 =over 4
137
138 =item * Ed Summers <ehs@pobox.com> wrote L<PHP::Include> on which this code is based
139
140 =cut
141
142 1;

  ViewVC Help
Powered by ViewVC 1.1.26