1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-27 04:14:44 +01:00

Use moriyoshi's fork of kmyacc, which fixes most of the issues of kmyacc with PHP

This commit is contained in:
nikic 2011-08-14 14:52:24 +02:00
parent e0fe21287d
commit 297c9ac290
5 changed files with 20 additions and 44 deletions

View File

@ -5,10 +5,10 @@ What do all those files mean?
* `zend_language_parser.phpy`: PHP grammer written in a pseudo language * `zend_language_parser.phpy`: PHP grammer written in a pseudo language
* `analyzer.php`: Analyzes the `.phpy`-grammer and outputs some info about it * `analyzer.php`: Analyzes the `.phpy`-grammer and outputs some info about it
* `rebuildParser.php`: Preprocesses the `.phpy`-grammar and builds the parser using `kmyacc` * `rebuildParser.php`: Preprocesses the `.phpy`-grammar and builds the parser using `kmyacc`
* `php.kmyacc`: A `kmyacc` parser prototype file for PHP * `kmyacc.php.parser`: A `kmyacc` parser prototype file for PHP
.phpy pseudo language .phpy pseudo language
========================= =====================
The `.phpy` file is a normal grammer in `kmyacc` (`yacc`) style, with some transformations The `.phpy` file is a normal grammer in `kmyacc` (`yacc`) style, with some transformations
applied to it: applied to it:
@ -17,3 +17,9 @@ applied to it:
`new PHPParser_Node_Name(array('subNode1' => ..., 'subNode2' => ...), $line, $docComment)` `new PHPParser_Node_Name(array('subNode1' => ..., 'subNode2' => ...), $line, $docComment)`
* `Name::abc` is transformed to `PHPParser_Node_Name::abc` * `Name::abc` is transformed to `PHPParser_Node_Name::abc`
* Some function-like constructs are resolved (see `rebuildParser.php` for a list) * Some function-like constructs are resolved (see `rebuildParser.php` for a list)
Building the parser
===================
In order to rebuild the parser, you need [moriyoshi's fork of kmyacc](https://github.com/moriyoshi/kmyacc-forked).
After you compiled/installed it, run the `rebuildParser.php` file.

View File

@ -1,6 +1,8 @@
<?php <?php
const GRAMMAR_FILE = './zend_language_parser.phpy'; const GRAMMAR_FILE = './zend_language_parser.phpy';
const TMP_FILE = './tmp_parser.phpy';
const RESULT_FILE = './tmp_parser.php';
/////////////////////////////// ///////////////////////////////
/// Utility regex constants /// /// Utility regex constants ///
@ -30,54 +32,22 @@ $grammarCode = file_get_contents(GRAMMAR_FILE);
$grammarCode = preg_replace('~[A-Z][a-zA-Z_]++::~', 'PHPParser_Node_$0', $grammarCode); $grammarCode = preg_replace('~[A-Z][a-zA-Z_]++::~', 'PHPParser_Node_$0', $grammarCode);
$grammarCode = resolveNodes($grammarCode); $grammarCode = resolveNodes($grammarCode);
$grammarCode = resolveMacros($grammarCode); $grammarCode = resolveMacros($grammarCode);
$grammarCode = preg_replace('~\$([a-zA-Z_]++)~', '#$1', $grammarCode);
$tmpGrammarFile = tempnam('.', 'tmpGrammarFile'); file_put_contents(TMP_FILE, $grammarCode);
file_put_contents($tmpGrammarFile, $grammarCode);
echo 'Building parser. Output: "', echo 'Building parser. Output: "',
trim(`kmyacc -l -L c -m php.kmyacc -p PHPParser_Parser $tmpGrammarFile 2>&1`), trim(shell_exec('kmyacc -l -m kmyacc.php.parser -p PHPParser_Parser ' . TMP_FILE . ' 2>&1')),
'"', "\n"; '"', "\n";
$code = file_get_contents('y.tab.c'); rename(RESULT_FILE, '../lib/PHPParser/Parser.php');
$code = str_replace(
array(
'"$EOF"',
'#',
),
array(
'\'$EOF\'',
'$',
),
$code
);
file_put_contents(dirname(__DIR__) . '/lib/PHPParser/Parser.php', $code);
unlink(__DIR__ . '/y.tab.c');
echo 'Building debug parser. Output: "', echo 'Building debug parser. Output: "',
trim(`kmyacc -l -t -L c -m php.kmyacc -p PHPParser_ParserDebug $tmpGrammarFile 2>&1`), trim(shell_exec('kmyacc -t -l -m kmyacc.php.parser -p PHPParser_ParserDebug ' . TMP_FILE . ' 2>&1')),
'"', "\n"; '"', "\n";
$code = file_get_contents('y.tab.c'); rename(RESULT_FILE, '../lib/PHPParser/ParserDebug.php');
$code = str_replace(
array(
'"$EOF"',
'"$start : start"',
'#',
),
array(
'\'$EOF\'',
'\'$start : start\'',
'$',
),
$code
);
file_put_contents(dirname(__DIR__) . '/lib/PHPParser/ParserDebug.php', $code); unlink(TMP_FILE);
unlink(__DIR__ . '/y.tab.c');
unlink($tmpGrammarFile);
echo 'The following temporary preproprocessed grammar file was used:', "\n", $grammarCode; echo 'The following temporary preproprocessed grammar file was used:', "\n", $grammarCode;

View File

@ -147,7 +147,7 @@ class PHPParser_Parser
// }}} // }}}
private static $yyterminals = array( private static $yyterminals = array(
'$EOF', "EOF",
"error", "error",
"T_INCLUDE", "T_INCLUDE",
"T_INCLUDE_ONCE", "T_INCLUDE_ONCE",

View File

@ -147,7 +147,7 @@ class PHPParser_ParserDebug
// }}} // }}}
private static $yyterminals = array( private static $yyterminals = array(
'$EOF', "EOF",
"error", "error",
"T_INCLUDE", "T_INCLUDE",
"T_INCLUDE_ONCE", "T_INCLUDE_ONCE",
@ -296,7 +296,7 @@ class PHPParser_ParserDebug
); );
private static $yyproduction = array( private static $yyproduction = array(
'$start : start', "start : start",
"start : top_statement_list", "start : top_statement_list",
"top_statement_list : top_statement_list top_statement", "top_statement_list : top_statement_list top_statement",
"top_statement_list : /* empty */", "top_statement_list : /* empty */",