Drop name magic from rebuildParser.php

Now using explicitly imported names, instead of magically
prepending Name\.

Also remove trailing whitespace from generated file.
This commit is contained in:
Nikita Popov 2015-04-30 19:18:42 +02:00
parent 272ab6c8d8
commit 57ac7e39bf
4 changed files with 483 additions and 467 deletions

View File

@ -4,9 +4,9 @@ $meta #
#semval($,%t) $this->semValue #semval($,%t) $this->semValue
#semval(%n) $this->stackPos-(%l-%n) #semval(%n) $this->stackPos-(%l-%n)
#semval(%n,%t) $this->stackPos-(%l-%n) #semval(%n,%t) $this->stackPos-(%l-%n)
#include;
namespace PhpParser; namespace PhpParser;
#include;
/* This is an automatically GENERATED file, which should not be manually edited. /* This is an automatically GENERATED file, which should not be manually edited.
* Instead edit one of the following: * Instead edit one of the following:

View File

@ -39,7 +39,6 @@ echo 'Building temporary preproprocessed grammar file.', "\n";
$grammarCode = file_get_contents($grammarFile); $grammarCode = file_get_contents($grammarFile);
$grammarCode = resolveConstants($grammarCode);
$grammarCode = resolveNodes($grammarCode); $grammarCode = resolveNodes($grammarCode);
$grammarCode = resolveMacros($grammarCode); $grammarCode = resolveMacros($grammarCode);
$grammarCode = resolveArrays($grammarCode); $grammarCode = resolveArrays($grammarCode);
@ -53,7 +52,12 @@ echo "Building parser.\n";
$output = trim(shell_exec("$kmyacc $additionalArgs -l -m $skeletonFile $tmpGrammarFile 2>&1")); $output = trim(shell_exec("$kmyacc $additionalArgs -l -m $skeletonFile $tmpGrammarFile 2>&1"));
echo "Output: \"$output\"\n"; echo "Output: \"$output\"\n";
moveFileWithDirCheck($tmpResultFile, $parserResultFile); $resultCode = file_get_contents($tmpResultFile);
$resultCode = removeTrailingWhitespace($resultCode);
ensureDirExists(dirname($parserResultFile));
file_put_contents($parserResultFile, $resultCode);
unlink($tmpResultFile);
if (!$optionKeepTmpGrammar) { if (!$optionKeepTmpGrammar) {
unlink($tmpGrammarFile); unlink($tmpGrammarFile);
@ -63,10 +67,6 @@ if (!$optionKeepTmpGrammar) {
/// Preprocessing functions /// /// Preprocessing functions ///
/////////////////////////////// ///////////////////////////////
function resolveConstants($code) {
return preg_replace('~[A-Z][a-zA-Z_\\\\]++::~', 'Node\\\\$0', $code);
}
function resolveNodes($code) { function resolveNodes($code) {
return preg_replace_callback( return preg_replace_callback(
'~(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~', '~(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
@ -84,7 +84,7 @@ function resolveNodes($code) {
$paramCode .= $param . ', '; $paramCode .= $param . ', ';
} }
return 'new Node\\' . $matches['name'] . '(' . $paramCode . 'attributes())'; return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())';
}, },
$code $code
); );
@ -197,12 +197,16 @@ function resolveStackAccess($code) {
return $code; return $code;
} }
function moveFileWithDirCheck($fromPath, $toPath) { function removeTrailingWhitespace($code) {
$dir = dirname($toPath); $lines = explode("\n", $code);
$lines = array_map('rtrim', $lines);
return implode("\n", $lines);
}
function ensureDirExists($dir) {
if (!is_dir($dir)) { if (!is_dir($dir)) {
mkdir($dir, 0777, true); mkdir($dir, 0777, true);
} }
rename($fromPath, $toPath);
} }
////////////////////////////// //////////////////////////////

View File

@ -111,6 +111,13 @@
%token T_NS_SEPARATOR %token T_NS_SEPARATOR
%token T_ELLIPSIS %token T_ELLIPSIS
%{
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
%}
%% %%
start: start:
@ -164,7 +171,7 @@ constant_declaration_list:
; ;
constant_declaration: constant_declaration:
T_STRING '=' static_scalar { $$ = Const_[$1, $3]; } T_STRING '=' static_scalar { $$ = Node\Const_[$1, $3]; }
; ;
inner_statement_list: inner_statement_list:
@ -385,9 +392,9 @@ non_empty_parameter_list:
parameter: parameter:
optional_param_type optional_ref optional_ellipsis T_VARIABLE optional_param_type optional_ref optional_ellipsis T_VARIABLE
{ $$ = Param[parseVar($4), null, $1, $2, $3]; } { $$ = Node\Param[parseVar($4), null, $1, $2, $3]; }
| optional_param_type optional_ref optional_ellipsis T_VARIABLE '=' static_scalar | optional_param_type optional_ref optional_ellipsis T_VARIABLE '=' static_scalar
{ $$ = Param[parseVar($4), $6, $1, $2, $3]; } { $$ = Node\Param[parseVar($4), $6, $1, $2, $3]; }
; ;
type: type:
@ -409,7 +416,7 @@ optional_return_type:
argument_list: argument_list:
'(' ')' { $$ = array(); } '(' ')' { $$ = array(); }
| '(' non_empty_argument_list ')' { $$ = $2; } | '(' non_empty_argument_list ')' { $$ = $2; }
| '(' yield_expr ')' { $$ = array(Arg[$2, false, false]); } | '(' yield_expr ')' { $$ = array(Node\Arg[$2, false, false]); }
; ;
non_empty_argument_list: non_empty_argument_list:
@ -418,9 +425,9 @@ non_empty_argument_list:
; ;
argument: argument:
expr { $$ = Arg[$1, false, false]; } expr { $$ = Node\Arg[$1, false, false]; }
| '&' variable { $$ = Arg[$2, true, false]; } | '&' variable { $$ = Node\Arg[$2, true, false]; }
| T_ELLIPSIS expr { $$ = Arg[$2, false, true]; } | T_ELLIPSIS expr { $$ = Node\Arg[$2, false, true]; }
; ;
global_var_list: global_var_list:

File diff suppressed because it is too large Load Diff