--- lib/A3C/PHP.pm 2008/05/30 20:05:27 135 +++ lib/A3C/PHP.pm 2008/05/30 20:53:14 136 @@ -14,11 +14,13 @@ For integration with php application there is simple php config parser based on L +It currently ignores C PHP function. + =head1 METHODS =cut -my $debug = 0; +our $debug = 0; our $perl = ''; our $data; @@ -32,35 +34,51 @@ php_end: /\s*\?>\s*/ -statement: comment | assignment +statement: comment | assignment | function + +comment: /\s*(\#|\/\/).*/ -comment: /\s*(\#|\/\/).*/ +function: /(include)\(.*?\)\s*;/ + { + $return = "## ignored $1\n"; + } assignment: ( var_assign | hash_assign | array_assign | constant ) /;/ { - $A3C::PHP::perl .= $item[1]; + $A3C::PHP::perl .= $item[1]; } var_assign: variable /=/ scalar { - $item[1] =~ s/^\$//; - $return = "\$data->{ $item[1] } = $item[3]; # scalar\n"; + $item[1] =~ s/^\$//; + $return = "\$data->{ $item[1] } = $item[3]; # scalar\n"; } hash_assign: variable /=/ /Array\s*\(/i pair(s /,/) /\s*(,\s*)?\)/ { - $item[1] =~ s/^\$//; - $return = "\$data->{ $item[1] } = $item[4]; # hash\n"; - $return = "\%{\$data->{ $item[1] }} =(" . join( ',', @{$item[4]} ) . ");\t# hash\n"; + $item[1] =~ s/^\$//; + $return = "\$data->{ $item[1] } = $item[4]; # hash\n"; + $return = "\%{\$data->{ $item[1] }} =(" . join( ',', @{$item[4]} ) . ");\t# hash\n"; } array_assign: variable /=/ /Array\s*\(/i element(s /,/) /\s*(,\s*)?\)/ { - $item[1] =~ s/^\$//; - $return = "\@{\$data->{ $item[1] }}=(" . join( ',', @{$item[4]} ) . ");\t# array\n" + $item[1] =~ s/^\$//; + $return = "\@{\$data->{ $item[1] }}=(" . join( ',', @{$item[4]} ) . ");\t# array\n" } -scalar: string | number +scalar: string | number | constant + +constant: true | false + +true: 'true' + { + $return = '1'; + } +false: 'false' + { + $return = '0'; + } variable: /\$[a-zA-Z_][0-9a-zA-Z_]*/ @@ -69,6 +87,10 @@ string: double_quoted | single_quoted double_quoted: /".*?"/ + { + $item[1] =~ s/\@/\\\@/g; + $return = $item[1]; + } single_quoted: /'.*?'/ @@ -76,14 +98,14 @@ pair: scalar /=>/ ( scalar | bareword ) { - $return = $item[1] . '=>' . $item[3]; + $return = $item[1] . '=>' . $item[3]; } bareword: /[0-9a-zA-Z_]+/ - + constant: /define\s*\(/ string /,/ scalar /\)/ { - $return = "use constant $item[2] => $item[4];\n"; + $return = "use constant $item[2] => $item[4];\n"; } comments: /^#.*$/ @@ -101,11 +123,14 @@ sub parse { my $self = shift; my $php = shift or die "no php?"; - $perl = ''; - $::RD_TRACE = 1 if $debug; + $perl = ''; + if ( $debug ) { + $::RD_TRACE = 1; + warn "PHP: $php\n"; + } my $parser = Parse::RecDescent->new( $grammar ); - warn dump( $parser->php_vars( $php ) ); - print STDERR "\n\nGENERATED PERL:\n\n", $perl, "\n\n" if $debug; + $parser->php_vars( $php ); + warn "## GENERATED PERL:\n\n", $perl, "\n\n" if $debug; my $data; eval $perl; die "$@" if $@;