From 297c9ac2909454ebd37df28a04559fd5bbd621c6 Mon Sep 17 00:00:00 2001 From: nikic Date: Sun, 14 Aug 2011 14:52:24 +0200 Subject: [PATCH] Use moriyoshi's fork of kmyacc, which fixes most of the issues of kmyacc with PHP --- grammar/README.md | 12 ++++-- grammar/{php.kmyacc => kmyacc.php.parser} | 0 grammar/rebuildParser.php | 46 ++++------------------- lib/PHPParser/Parser.php | 2 +- lib/PHPParser/ParserDebug.php | 4 +- 5 files changed, 20 insertions(+), 44 deletions(-) rename grammar/{php.kmyacc => kmyacc.php.parser} (100%) diff --git a/grammar/README.md b/grammar/README.md index 6fb7907..922e0f2 100644 --- a/grammar/README.md +++ b/grammar/README.md @@ -5,10 +5,10 @@ What do all those files mean? * `zend_language_parser.phpy`: PHP grammer written in a pseudo language * `analyzer.php`: Analyzes the `.phpy`-grammer and outputs some info about it * `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 -========================= +===================== The `.phpy` file is a normal grammer in `kmyacc` (`yacc`) style, with some transformations applied to it: @@ -16,4 +16,10 @@ applied to it: * Nodes are created using the syntax `Name[subNode1: ..., subNode2: ...]`. This is transformed into `new PHPParser_Node_Name(array('subNode1' => ..., 'subNode2' => ...), $line, $docComment)` * `Name::abc` is transformed to `PHPParser_Node_Name::abc` - * Some function-like constructs are resolved (see `rebuildParser.php` for a list) \ No newline at end of file + * 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. \ No newline at end of file diff --git a/grammar/php.kmyacc b/grammar/kmyacc.php.parser similarity index 100% rename from grammar/php.kmyacc rename to grammar/kmyacc.php.parser diff --git a/grammar/rebuildParser.php b/grammar/rebuildParser.php index 616a017..c1af4fd 100644 --- a/grammar/rebuildParser.php +++ b/grammar/rebuildParser.php @@ -1,6 +1,8 @@ &1`), + trim(shell_exec('kmyacc -l -m kmyacc.php.parser -p PHPParser_Parser ' . TMP_FILE . ' 2>&1')), '"', "\n"; -$code = file_get_contents('y.tab.c'); -$code = str_replace( - array( - '"$EOF"', - '#', - ), - array( - '\'$EOF\'', - '$', - ), - $code -); - -file_put_contents(dirname(__DIR__) . '/lib/PHPParser/Parser.php', $code); -unlink(__DIR__ . '/y.tab.c'); +rename(RESULT_FILE, '../lib/PHPParser/Parser.php'); 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"; -$code = file_get_contents('y.tab.c'); -$code = str_replace( - array( - '"$EOF"', - '"$start : start"', - '#', - ), - array( - '\'$EOF\'', - '\'$start : start\'', - '$', - ), - $code -); +rename(RESULT_FILE, '../lib/PHPParser/ParserDebug.php'); -file_put_contents(dirname(__DIR__) . '/lib/PHPParser/ParserDebug.php', $code); -unlink(__DIR__ . '/y.tab.c'); - -unlink($tmpGrammarFile); +unlink(TMP_FILE); echo 'The following temporary preproprocessed grammar file was used:', "\n", $grammarCode; diff --git a/lib/PHPParser/Parser.php b/lib/PHPParser/Parser.php index 3e9c06d..82d67ba 100644 --- a/lib/PHPParser/Parser.php +++ b/lib/PHPParser/Parser.php @@ -147,7 +147,7 @@ class PHPParser_Parser // }}} private static $yyterminals = array( - '$EOF', + "EOF", "error", "T_INCLUDE", "T_INCLUDE_ONCE", diff --git a/lib/PHPParser/ParserDebug.php b/lib/PHPParser/ParserDebug.php index eb14488..01080f0 100644 --- a/lib/PHPParser/ParserDebug.php +++ b/lib/PHPParser/ParserDebug.php @@ -147,7 +147,7 @@ class PHPParser_ParserDebug // }}} private static $yyterminals = array( - '$EOF', + "EOF", "error", "T_INCLUDE", "T_INCLUDE_ONCE", @@ -296,7 +296,7 @@ class PHPParser_ParserDebug ); private static $yyproduction = array( - '$start : start', + "start : start", "start : top_statement_list", "top_statement_list : top_statement_list top_statement", "top_statement_list : /* empty */",