Initial commit

This commit is contained in:
nikic 2011-04-18 19:02:30 +02:00
commit aea5e43349
145 changed files with 25284 additions and 0 deletions

27
README Normal file
View File

@ -0,0 +1,27 @@
This is a PHP parser written in PHP.
This project is work in progress, but it basically works and was tested on a large code base.
Usage:
$parser = new Parser();
$parser->yyparse(
new Lexer('<?php // some code'),
function($stmt) {
// this is the accept callback
// it is fired in case of a successful parse
// $stmts contains an array of nodes
// to print them do:
foreach ($stmts as $stmt) {
echo $stmt, "\n";
}
},
function($msg) {
// this is the error callback
// it is fired in case of an error (currently
// errors abort the parse)
echo $msg, "\n";
}
);
// see example usage in test.php

149
grammar/analyzer.php Normal file
View File

@ -0,0 +1,149 @@
<?php
const IN = './zend_language_parser.pre.phpy';
const OUT = './zend_language_parser.phpy';
const LIB = '(?(DEFINE)
(?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
(?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
(?<string>(?&singleQuotedString)|(?&doubleQuotedString))
(?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
(?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
)';
const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?&params)\][^[\]]*+)*+)\]';
const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
const RULE_BLOCK = '(?<name>[a-z_]++):(?<rules>[^\'"/{};]*+(?:(?:(?&string)|(?&comment)|(?&code)|/|})[^\'"/{};]*+)*+);';
$tokensToExtract = array_flip(array(
'T_VARIABLE', 'T_STRING', 'T_INLINE_HTML', 'T_ENCAPSED_AND_WHITESPACE',
'T_LNUMBER', 'T_DNUMBER', 'T_CONSTANT_ENCAPSED_STRING', 'T_STRING_VARNAME', 'T_NUM_STRING'
));
function regex($regex) {
return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~';
}
function magicSplit($regex, $string) {
$pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string);
foreach ($pieces as &$piece) {
$piece = trim($piece);
}
return array_filter($pieces);
}
function generateNodeFilesFromSignatures($signatures, $dir) {
foreach ($signatures as $nodeName => $signature) {
$code = <<<EOC
<?php
class Node_{$nodeName} extends NodeAbstract
{
}
EOC;
;
if (!file_exists($dir . '/' . $nodeName . '.php')) {
file_put_contents($dir . '/' . $nodeName . '.php', $code);
}
}
}
echo '<pre>';
////////////////////
////////////////////
////////////////////
list($defs, $ruleBlocks) = magicSplit('%%', file_get_contents(IN));
if ('' !== trim(preg_replace(regex(RULE_BLOCK), '', $ruleBlocks))) {
die('Not all rule blocks were properly recognized!');
}
$nodeSignatures = array();
preg_match_all(regex(RULE_BLOCK), $ruleBlocks, $ruleBlocksMatches, PREG_SET_ORDER);
foreach ($ruleBlocksMatches as $match) {
$ruleBlockName = $match['name'];
$rules = magicSplit('\|', $match['rules']);
foreach ($rules as &$rule) {
$parts = magicSplit('\s+', $rule);
$usedParts = array();
foreach ($parts as $part) {
if ('{' === $part[0]) {
preg_match_all('~\$([0-9]+)~', $part, $backReferencesMatches, PREG_SET_ORDER);
foreach ($backReferencesMatches as $match) {
$usedParts[$match[1]] = true;
}
preg_match_all('~(?<name>[A-Z][a-zA-Z]++)' . PARAMS . '~', $part, $nodeMatches, PREG_SET_ORDER);
foreach ($nodeMatches as $match) {
$signature =& $nodeSignatures[$match['name']];
$params = magicSplit('(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,', $match['params']);
if (!isset($signature)) {
$signature = array();
foreach ($params as $i => $param) {
list($name, ) = explode(': ', $param, 2);
$signature[$i] = $name;
}
} else {
foreach ($params as $i => $param) {
list($name, ) = explode(': ', $param, 2);
if ($signature[$i] != $name) {
die('Signature mismatch for "' . $match['name'] . '"');
}
}
}
}
}
}
$i = 1;
foreach ($parts as &$part) {
if ('/' === $part[0]) {
continue;
}
if (isset($usedParts[$i])) {
if ('\'' === $part[0] || '{' === $part[0]) {
$part = '<span style="background-color: red; color: white;">' . $part . '</span>';
} elseif ('T' === $part[0] && !isset($tokensToExtract[$part])) {
$part = '<span style="background-color: green; color: white;">' . $part . '</span>';
} else {
$part = '<strong><em>' . $part . '</em></strong>';
}
} elseif (ctype_lower($part[0])) {
$part = '<span style="background-color: blue; color: white;">' . $part . '</span>';
} elseif ('T' === $part[0] && isset($tokensToExtract[$part])) {
$part = '<span style="background-color: yellow;">' . $part . '</span>';
}
++$i;
}
$rule = implode(' ', $parts);
}
echo $ruleBlockName, ':', "\n", ' ', implode("\n" . ' | ', $rules), "\n", ';', "\n\n";
}
var_dump($nodeSignatures);
var_dump(array_keys($nodeSignatures));
$names = array();
foreach ($nodeSignatures as $params) {
foreach ($params as $param) {
$names[$param] = true;
}
}
var_dump(array_keys($names));
generateNodeFilesFromSignatures($nodeSignatures, './lib/Node');

View File

@ -0,0 +1,342 @@
<?php
$meta @
@semval($) $this->yyval
@semval($,%t) $this->yyval
@semval(%n) $this->yyastk[$this->yysp-(%l-%n)]
@semval(%n,%t) $this->yyastk[$this->yysp-(%l-%n)]
@include;
/* Prototype file of classed PHP parser.
* Written by Moriyoshi Koizumi, based on the work by Masato Bito.
* This file is PUBLIC DOMAIN.
*/
@if -p
class @(-p)
@endif
@ifnot -p
class YYParser
@endif
{
const YYBADCH = @(YYBADCH);
const YYMAXLEX = @(YYMAXLEX);
const YYTERMS = @(YYTERMS);
const YYNONTERMS = @(YYNONTERMS);
const YYLAST = @(YYLAST);
const YY2TBLSTATE = @(YY2TBLSTATE);
const YYGLAST = @(YYGLAST);
const YYSTATES = @(YYSTATES);
const YYNLSTATES = @(YYNLSTATES);
const YYINTERRTOK = @(YYINTERRTOK);
const YYUNEXPECTED = @(YYUNEXPECTED);
const YYDEFAULT = @(YYDEFAULT);
// {{{ Tokens
@tokenval
const %s = %n;
@endtokenval
// }}}
protected $yyval;
protected $yyastk;
protected $yysp;
protected $yyaccept;
@if -t
/** Debug mode flag **/
public $yydebug = true;
@endif
@if -t
private static $yyterminals = array(
@listvar terminals
, "???"
);
private static $yyproduction = array(
@production-strings;
);
@endif
private static $yytranslate = array(
@listvar yytranslate
);
private static $yyaction = array(
@listvar yyaction
);
private static $yycheck = array(
@listvar yycheck
);
private static $yybase = array(
@listvar yybase
);
private static $yydefault = array(
@listvar yydefault
);
private static $yygoto = array(
@listvar yygoto
);
private static $yygcheck = array(
@listvar yygcheck
);
private static $yygbase = array(
@listvar yygbase
);
private static $yygdefault = array(
@listvar yygdefault
);
private static $yylhs = array(
@listvar yylhs
);
private static $yylen = array(
@listvar yylen
);
protected function yyprintln($msg) {
echo $msg, "\n";
}
protected function error($sym) {
$errorCallback = $this->errorCallback;
$errorCallback(
'Parse error:'
. ' Unexpected token ' . self::$yyterminals[$sym]
. ' on line ' . $this->lex->getLine()
);
}
protected function accept() {
$acceptCallback = $this->acceptCallback;
$acceptCallback($this->yyval);
}
protected function yyaccept() {
$this->yyaccept = 1;
}
protected function yyabort() {
$this->yyaccept = 2;
}
@if -t
/* Traditional Debug Mode */
private function YYTRACE_NEWSTATE($state, $sym) {
if ($this->yydebug) {
$this->yyprintln("% State " . $state . ", Lookahead "
. ($sym < 0 ? "--none--" : self::$yyterminals[$sym]));
}
}
private function YYTRACE_READ($sym) {
if ($this->yydebug)
$this->yyprintln("% Reading " . self::$yyterminals[$sym]);
}
private function YYTRACE_SHIFT($sym) {
if ($this->yydebug)
$this->yyprintln("% Shift " . self::$yyterminals[$sym]);
}
private function YYTRACE_ACCEPT() {
if ($this->yydebug)
$this->yyprintln("% Accepted.");
}
private function YYTRACE_REDUCE($n) {
if ($this->yydebug)
$this->yyprintln("% Reduce by (" . $n . ") " . self::$yyproduction[$n]);
}
private function YYTRACE_POP($state) {
if ($this->yydebug)
$this->yyprintln("% Recovering, uncovers state " . $state);
}
private function YYTRACE_DISCARD($sym) {
if ($this->yydebug)
$this->yyprintln("% Discard " . self::$yyterminals[$sym]);
}
@endif
/**
* Parser entry point
*/
public function yyparse($lex, $acceptCallback, $errorCallback) {
$this->lex = $lex;
$this->acceptCallback = $acceptCallback;
$this->errorCallback = $errorCallback;
$this->yyastk = array();
$yysstk = array();
$this->yysp = 0;
$yystate = 0;
$yychar = -1;
$yylval = null;
$yysstk[$this->yysp] = 0;
$yyerrflag = 0;
for (;;) {
@if -t
$this->YYTRACE_NEWSTATE($yystate, $yychar);
@endif
if (self::$yybase[$yystate] == 0) {
$yyn = self::$yydefault[$yystate];
} else {
if ($yychar < 0) {
if (($yychar = $lex->yylex($yylval)) < 0)
$yychar = 0;
$yychar = $yychar < self::YYMAXLEX ?
self::$yytranslate[$yychar] : self::YYBADCH;
@if -t
$this->YYTRACE_READ($yychar);
@endif
}
if ((($yyn = self::$yybase[$yystate] + $yychar) >= 0
&& $yyn < self::YYLAST && self::$yycheck[$yyn] == $yychar
|| ($yystate < self::YY2TBLSTATE
&& ($yyn = self::$yybase[$yystate + self::YYNLSTATES]
+ $yychar) >= 0
&& $yyn < self::YYLAST
&& self::$yycheck[$yyn] == $yychar))
&& ($yyn = self::$yyaction[$yyn]) != self::YYDEFAULT) {
/*
* >= YYNLSTATE: shift and reduce
* > 0: shift
* = 0: accept
* < 0: reduce
* = -YYUNEXPECTED: error
*/
if ($yyn > 0) {
/* shift */
@if -t
$this->YYTRACE_SHIFT($yychar);
@endif
$this->yysp++;
$yysstk[$this->yysp] = $yystate = $yyn;
$this->yyastk[$this->yysp] = $yylval;
$yychar = -1;
if ($yyerrflag > 0)
$yyerrflag--;
if ($yyn < self::YYNLSTATES)
continue;
/* $yyn >= YYNLSTATES means shift-and-reduce */
$yyn -= self::YYNLSTATES;
} else {
$yyn = -$yyn;
}
} else {
$yyn = self::$yydefault[$yystate];
}
}
for (;;) {
/* reduce/error */
if ($yyn == 0) {
/* accept */
@if -t
$this->YYTRACE_ACCEPT();
@endif
$this->accept();
return $this->yyaccept - 1;
} elseif ($yyn != self::YYUNEXPECTED) {
/* reduce */
$yyl = self::$yylen[$yyn];
$n = $this->yysp-$yyl+1;
$yyval = isset($this->yyastk[$n]) ? $this->yyastk[$n] : null;
@if -t
$this->YYTRACE_REDUCE($yyn);
@endif
$this->{'yyn' . $yyn}();
if ($this->yyaccept) {
$yyn = self::YYNLSTATES;
} else {
/* Goto - shift nonterminal */
$this->yysp -= $yyl;
$yyn = self::$yylhs[$yyn];
if (($yyp = self::$yygbase[$yyn] + $yysstk[$this->yysp]) >= 0
&& $yyp < self::YYGLAST
&& self::$yygcheck[$yyp] == $yyn) {
$yystate = self::$yygoto[$yyp];
} else {
$yystate = self::$yygdefault[$yyn];
}
$this->yysp++;
$yysstk[$this->yysp] = $yystate;
$this->yyastk[$this->yysp] = $this->yyval;
}
} else {
/* error */
switch ($yyerrflag) {
case 0:
$this->error($yychar);
case 1:
case 2:
$yyerrflag = 3;
/* Pop until error-expecting state uncovered */
while (!(($yyn = self::$yybase[$yystate] + self::YYINTERRTOK) >= 0
&& $yyn < self::YYLAST
&& self::$yycheck[$yyn] == self::YYINTERRTOK
|| ($yystate < self::YY2TBLSTATE
&& ($yyn = self::$yybase[$yystate + self::YYNLSTATES] + self::YYINTERRTOK) >= 0
&& $yyn < self::YYLAST
&& self::$yycheck[$yyn] == self::YYINTERRTOK))) {
if ($this->yysp <= 0) {
return 1;
}
$yystate = $yysstk[--$this->yysp];
@if -t
$this->YYTRACE_POP($yystate);
@endif
}
$yyn = self::$yyaction[$yyn];
@if -t
$this->YYTRACE_SHIFT(self::YYINTERRTOK);
@endif
$yysstk[++$this->yysp] = $yystate = $yyn;
break;
case 3:
@if -t
$this->YYTRACE_DISCARD($yychar);
@endif
if ($yychar == 0) {
return 1;
}
$yychar = -1;
break;
}
}
if ($yystate < self::YYNLSTATES)
break;
/* >= YYNLSTATES means shift-and-reduce */
$yyn = $yystate - self::YYNLSTATES;
}
}
}
@reduce
private function yyn%n() {
%b
}
@noact
private function yyn%n() {}
@endreduce
}
@tailcode;

145
grammar/preprocessor.php Normal file
View File

@ -0,0 +1,145 @@
<?php
const IN = './zend_language_parser.pre.phpy';
const OUT = './zend_language_parser.phpy';
const LIB = '(?(DEFINE)
(?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
(?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
(?<string>(?&singleQuotedString)|(?&doubleQuotedString))
(?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
(?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
)';
const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?&params)\][^[\]]*+)*+)\]';
const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
function regex($regex) {
return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~';
}
function magicSplit($regex, $string) {
$pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string);
foreach ($pieces as &$piece) {
$piece = trim($piece);
}
return array_filter($pieces);
}
echo '<pre>';
////////////////////
////////////////////
////////////////////
$code = file_get_contents(IN);
$code = preg_replace('~[A-Z][a-zA-Z]++::~', 'Node_$0', $code);
$code = resolveNodes($code);
$code = resolveMacros($code);
file_put_contents(OUT, $code);
echo $code;
function resolveNodes($code) {
return preg_replace_callback(
'~(?<name>[A-Z][a-zA-Z]++)' . PARAMS . '~',
function($matches) {
// recurse
$matches['params'] = resolveNodes($matches['params']);
$params = magicSplit(
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
$matches['params']
);
$paramCodes = array();
foreach ($params as $param) {
list($key, $value) = explode(': ', $param, 2);
$paramCodes[] = '\'' . $key . '\' => ' . $value;
}
return 'new Node_' . $matches['name'] . '(array(' . implode(', ', $paramCodes) . '))';
},
$code
);
}
function resolveMacros($code) {
return preg_replace_callback(
'~(?<name>init|push|pushNormalizing|toArray|parse(?:Var|Encapsed|LNumber|DNumber|String))' . ARGS . '~',
function($matches) {
// recurse
$matches['args'] = resolveMacros($matches['args']);
$name = $matches['name'];
$args = magicSplit(
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
$matches['args']
);
if ('init' == $name) {
return '$$ = array(' . implode(', ', $args) . ')';
}
if ('push' == $name) {
assertArgs(2, $args, $name);
return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0];
}
if ('pushNormalizing' == $name) {
assertArgs(2, $args, $name);
return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); } else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
}
if ('toArray' == $name) {
assertArgs(1, $args, $name);
return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')';
}
if ('parseVar' == $name) {
assertArgs(1, $args, $name);
return 'substr(' . $args[0] . ', 1)';
}
if ('parseEncapsed' == $name) {
assertArgs(1, $args, $name);
return 'stripcslashes(' . $args[0] . ')';
}
if ('parseLNumber' == $name) {
assertArgs(1, $args, $name);
return '(int) ' . $args[0];
}
if ('parseDNumber' == $name) {
assertArgs(1, $args, $name);
return '(double) ' . $args[0];
}
if ('parseString' == $name) {
assertArgs(1, $args, $name);
return 'str_replace(array(\'\\\\\\\'\', \'\\\\\\\\\'), array(\'\\\'\', \'\\\\\'), substr(' . $args[0] . ', 1, -1))';
}
},
$code
);
}
function assertArgs($num, $args, $name) {
if ($num != count($args)) {
die('Wrong argument count for ' . $name . '().');
}
}

18395
grammar/y.output Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,756 @@
%pure_parser
%expect 2
%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
%left ','
%left T_LOGICAL_OR
%left T_LOGICAL_XOR
%left T_LOGICAL_AND
%right T_PRINT
%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL
%left '?' ':'
%left T_BOOLEAN_OR
%left T_BOOLEAN_AND
%left '|'
%left '^'
%left '&'
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
%left T_SL T_SR
%left '+' '-' '.'
%left '*' '/' '%'
%right '!'
%nonassoc T_INSTANCEOF
%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
%right '['
%nonassoc T_NEW T_CLONE
%token T_EXIT
%token T_IF
%left T_ELSEIF
%left T_ELSE
%left T_ENDIF
%token T_LNUMBER
%token T_DNUMBER
%token T_STRING
%token T_STRING_VARNAME
%token T_VARIABLE
%token T_NUM_STRING
%token T_INLINE_HTML
%token T_CHARACTER
%token T_BAD_CHARACTER
%token T_ENCAPSED_AND_WHITESPACE
%token T_CONSTANT_ENCAPSED_STRING
%token T_ECHO
%token T_DO
%token T_WHILE
%token T_ENDWHILE
%token T_FOR
%token T_ENDFOR
%token T_FOREACH
%token T_ENDFOREACH
%token T_DECLARE
%token T_ENDDECLARE
%token T_AS
%token T_SWITCH
%token T_ENDSWITCH
%token T_CASE
%token T_DEFAULT
%token T_BREAK
%token T_CONTINUE
%token T_GOTO
%token T_FUNCTION
%token T_CONST
%token T_RETURN
%token T_TRY
%token T_CATCH
%token T_THROW
%token T_USE
%token T_GLOBAL
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
%token T_VAR
%token T_UNSET
%token T_ISSET
%token T_EMPTY
%token T_HALT_COMPILER
%token T_CLASS
%token T_INTERFACE
%token T_EXTENDS
%token T_IMPLEMENTS
%token T_OBJECT_OPERATOR
%token T_DOUBLE_ARROW
%token T_LIST
%token T_ARRAY
%token T_CLASS_C
%token T_METHOD_C
%token T_FUNC_C
%token T_LINE
%token T_FILE
%token T_COMMENT
%token T_DOC_COMMENT
%token T_OPEN_TAG
%token T_OPEN_TAG_WITH_ECHO
%token T_CLOSE_TAG
%token T_WHITESPACE
%token T_START_HEREDOC
%token T_END_HEREDOC
%token T_DOLLAR_OPEN_CURLY_BRACES
%token T_CURLY_OPEN
%token T_PAAMAYIM_NEKUDOTAYIM
%token T_NAMESPACE
%token T_NS_C
%token T_DIR
%token T_NS_SEPARATOR
%%
start:
top_statement_list { $$ = $1; }
;
top_statement_list:
top_statement_list top_statement { if (is_array($2)) { $$ = array_merge($1, $2); } else { $1[] = $2; $$ = $1; }; }
| /* empty */ { $$ = array(); }
;
namespace_name:
namespace_name_sub { $$ = new Node_Name(array('parts' => $1)); }
;
namespace_name_sub:
T_STRING { $$ = array($1); }
| namespace_name_sub T_NS_SEPARATOR T_STRING { $1[] = $3; $$ = $1; }
;
top_statement:
statement { $$ = $1; }
| function_declaration_statement { $$ = $1; }
| class_declaration_statement { $$ = $1; }
| T_HALT_COMPILER '(' ')' ';' { YYACCEPT; }
| T_NAMESPACE namespace_name ';' { $$ = new Node_NamespaceStmt(array('ns' => $2)); }
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = array(new Node_NamespaceStmt(array('ns' => $2)), $4); }
| T_NAMESPACE '{' top_statement_list '}' { $$ = array(new Node_NamespaceStmt(array('ns' => null)), $3); }
| T_USE use_declarations ';' { $$ = new Node_UseStmt(array('uses' => $2)); }
| constant_declaration ';' { $$ = new Node_ConstStmt(array('consts' => $1)); }
;
use_declarations:
use_declarations ',' use_declaration { $1[] = $3; $$ = $1; }
| use_declaration { $$ = array($1); }
;
use_declaration:
namespace_name { $$ = new Node_UseStmtUse(array('ns' => $1, 'alias' => null)); }
| namespace_name T_AS T_STRING { $$ = new Node_UseStmtUse(array('ns' => $1, 'alias' => $3)); }
| T_NS_SEPARATOR namespace_name { $$ = new Node_UseStmtUse(array('ns' => $2, 'alias' => null)); }
| T_NS_SEPARATOR namespace_name T_AS T_STRING { $$ = new Node_UseStmtUse(array('ns' => $2, 'alias' => $4)); }
;
constant_declaration:
constant_declaration ',' T_STRING '=' static_scalar { $1[] = new Node_ConstStmtConst(array('name' => $3, 'value' => $5)); $$ = $1; }
| T_CONST T_STRING '=' static_scalar { $$ = array(new Node_ConstStmtConst(array('name' => $2, 'value' => $4))); }
;
inner_statement_list:
inner_statement_list inner_statement { if (is_array($2)) { $$ = array_merge($1, $2); } else { $1[] = $2; $$ = $1; }; }
| /* empty */ { $$ = array(); }
;
inner_statement:
statement { $$ = $1; }
| function_declaration_statement { $$ = $1; }
| class_declaration_statement { $$ = $1; }
| T_HALT_COMPILER '(' ')' ';' { error('__halt_compiler() can only be used from the outermost scope'); }
;
statement:
'{' inner_statement_list '}' { $$ = $2; }
| T_IF '(' expr ')' statement elseif_list else_single { $$ = new Node_IfStmt(array('cond' => $3, 'stmts' => is_array($5) ? $5 : array($5), 'elseifList' => $6, 'else' => $7)); }
| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
{ $$ = new Node_IfStmt(array('cond' => $3, 'stmts' => $6, 'elseifList' => $7, 'else' => $8)); }
| T_WHILE '(' expr ')' while_statement { $$ = new Node_WhileStmt(array('cond' => $3, 'stmts' => is_array($5) ? $5 : array($5))); }
| T_DO statement T_WHILE '(' expr ')' ';' { $$ = new Node_DoStmt(array('stmts' => is_array($2) ? $2 : array($2), 'cond' => $5)); }
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
{ $$ = new Node_ForStmt(array('init' => $3, 'cond' => $5, 'loop' => $7, 'stmts' => is_array($9) ? $9 : array($9))); }
| T_SWITCH '(' expr ')' switch_case_list { $$ = new Node_SwitchStmt(array('cond' => $3, 'caseList' => $5)); }
| T_BREAK ';' { $$ = new Node_BreakStmt(array('num' => null)); }
| T_BREAK expr ';' { $$ = new Node_BreakStmt(array('num' => $2)); }
| T_CONTINUE ';' { $$ = new Node_ContinueStmt(array('num' => null)); }
| T_CONTINUE expr ';' { $$ = new Node_ContinueStmt(array('num' => $2)); }
| T_RETURN ';' { $$ = new Node_ReturnStmt(array('expr' => null)); }
| T_RETURN expr_without_variable ';' { $$ = new Node_ReturnStmt(array('expr' => $2)); }
| T_RETURN variable ';' { $$ = new Node_ReturnStmt(array('expr' => $2)); }
| T_GLOBAL global_var_list ';' { $$ = new Node_GlobalStmt(array('vars' => $2)); }
| T_STATIC static_var_list ';' { $$ = new Node_StaticStmt(array('vars' => $2)); }
| T_ECHO expr_list ';' { $$ = new Node_EchoStmt(array('exprs' => $2)); }
| T_INLINE_HTML { $$ = new Node_InlineHTMLStmt(array('value' => $1)); }
| expr ';' { $$ = $1; }
| T_UNSET '(' variables_list ')' ';' { $$ = new Node_UnsetStmt(array('vars' => $3)); }
| T_FOREACH '(' expr T_AS variable ')' foreach_statement
{ $$ = new Node_ForeachStmt(array('expr' => $3, 'keyVar' => null, 'byRef' => false, 'valueVar' => $5, 'stmts' => is_array($7) ? $7 : array($7))); }
| T_FOREACH '(' expr T_AS '&' variable ')' foreach_statement
{ $$ = new Node_ForeachStmt(array('expr' => $3, 'keyVar' => null, 'byRef' => true, 'valueVar' => $6, 'stmts' => is_array($8) ? $8 : array($8))); }
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW optional_ref variable ')' foreach_statement
{ $$ = new Node_ForeachStmt(array('expr' => $3, 'keyVar' => $5, 'byRef' => $7, 'valueVar' => $8, 'stmts' => is_array($10) ? $10 : array($10))); }
| T_DECLARE '(' declare_list ')' declare_statement { $$ = new Node_DeclareStmt(array('declares' => $3, 'stmts' => is_array($5) ? $5 : array($5))); }
| ';' { $$ = new Node_NoopStmt(array()); }
| T_TRY '{' inner_statement_list '}' catches { $$ = new Node_TryCatchStmt(array('stmts' => $3, 'catches' => $5)); }
| T_THROW expr ';' { $$ = new Node_ThrowStmt(array('expr' => $2)); }
| T_GOTO T_STRING ';' { $$ = new Node_GotoStmt(array('name' => $2)); }
| T_STRING ':' { $$ = new Node_LabelStmt(array('name' => $1)); }
;
catches:
catch { $$ = array($1); }
| catches catch { $1[] = $2; $$ = $1; }
;
catch:
T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
{ $$ = new Node_CatchStmt(array('type' => $3, 'var' => substr($4, 1), 'stmts' => $7)); }
;
variables_list:
variable { $$ = array($1); }
| variables_list ',' variable { $1[] = $3; $$ = $1; }
;
optional_ref:
/* empty */ { $$ = false; }
| '&' { $$ = true; }
;
function_declaration_statement:
T_FUNCTION optional_ref T_STRING '(' parameter_list ')' '{' inner_statement_list '}'
{ $$ = new Node_FuncStmt(array('byRef' => $2, 'name' => $3, 'params' => $5, 'stmts' => $8)); }
;
class_declaration_statement:
class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}'
{ $$ = new Node_ClassStmt(array('type' => $1, 'name' => $2, 'extends' => $3, 'implements' => $4, 'stmts' => $6)); }
| T_INTERFACE T_STRING interface_extends_list '{' class_statement_list '}'
{ $$ = new Node_InterfaceStmt(array('name' => $2, 'extends' => $3, 'stmts' => $5)); }
;
class_entry_type:
T_CLASS { $$ = 0; }
| T_ABSTRACT T_CLASS { $$ = Node_ClassStmt::MODIFIER_ABSTRACT; }
| T_FINAL T_CLASS { $$ = Node_ClassStmt::MODIFIER_FINAL; }
;
extends_from:
/* empty */ { $$ = null; }
| T_EXTENDS name { $$ = $2; }
;
interface_extends_list:
/* empty */ { $$ = array(); }
| T_EXTENDS interface_list { $$ = $2; }
;
implements_list:
/* empty */ { $$ = array(); }
| T_IMPLEMENTS interface_list { $$ = $2; }
;
interface_list:
name { $$ = array($1); }
| interface_list ',' name { $1[] = $3; $$ = $1; }
;
for_statement:
statement { $$ = $1; }
| ':' inner_statement_list T_ENDFOR ';' { $$ = $2; }
;
foreach_statement:
statement { $$ = $1; }
| ':' inner_statement_list T_ENDFOREACH ';' { $$ = $2; }
;
declare_statement:
statement { $$ = $1; }
| ':' inner_statement_list T_ENDDECLARE ';' { $$ = $2; }
;
declare_list:
T_STRING '=' static_scalar { $$ = array(new Node_DeclareStmtDeclare(array('key' => $1, 'value' => $3))); }
| declare_list ',' T_STRING '=' static_scalar { $1[] = new Node_DeclareStmtDeclare(array('key' => $3, 'value' => $5)); $$ = $1; }
;
switch_case_list:
'{' case_list '}' { $$ = $2; }
| '{' ';' case_list '}' { $$ = $3; }
| ':' case_list T_ENDSWITCH ';' { $$ = $2; }
| ':' ';' case_list T_ENDSWITCH ';' { $$ = $3; }
;
case_list:
/* empty */ { $$ = array(); }
| case_list T_CASE expr case_separator inner_statement_list
{ $1[] = new Node_CaseStmt(array('cond' => $3, 'stmts' => $5)); $$ = $1; }
| case_list T_DEFAULT case_separator inner_statement_list
{ $1[] = new Node_CaseStmt(array('cond' => null, 'stmts' => $4)); $$ = $1; }
;
case_separator:
':'
| ';'
;
while_statement:
statement { $$ = $1; }
| ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; }
;
elseif_list:
/* empty */ { $$ = array();}
| elseif_list T_ELSEIF '(' expr ')' statement { $1[] = new Node_ElseIfStmt(array('cond' => $4, 'stmts' => is_array($6) ? $6 : array($6))); $$ = $1; }
;
new_elseif_list:
/* empty */ { $$ = array(); }
| new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list
{ $1[] = new Node_ElseIfStmt(array('cond' => $4, 'stmts' => $7)); $$ = $1; }
;
else_single:
/* empty */ { $$ = null; }
| T_ELSE statement { $$ = new Node_ElseStmt(array('stmts' => is_array($2) ? $2 : array($2))); }
;
new_else_single:
/* empty */ { $$ = null; }
| T_ELSE ':' inner_statement_list { $$ = new Node_ElseStmt(array('stmts' => $3)); }
;
parameter_list:
non_empty_parameter_list { $$ = $1; }
| /* empty */ { $$ = array(); }
;
non_empty_parameter_list:
optional_class_type optional_ref T_VARIABLE
{ $$ = array(new Node_FuncStmtParam(array('type' => $1, 'name' => substr($3, 1), 'byRef' => $2, 'default' => null))); }
| optional_class_type optional_ref T_VARIABLE '=' static_scalar
{ $$ = array(new Node_FuncStmtParam(array('type' => $1, 'name' => substr($3, 1), 'byRef' => $2, 'default' => $5))); }
| non_empty_parameter_list ',' optional_class_type optional_ref T_VARIABLE
{ $1[] = new Node_FuncStmtParam(array('type' => $3, 'name' => substr($5, 1), 'byRef' => $4, 'default' => null)); $$ = $1; }
| non_empty_parameter_list ',' optional_class_type optional_ref T_VARIABLE '=' static_scalar
{ $1[] = new Node_FuncStmtParam(array('type' => $3, 'name' => substr($5, 1), 'byRef' => $4, 'default' => $7)); $$ = $1; }
;
optional_class_type:
/* empty */ { $$ = null; }
| name { $$ = $1; }
| T_ARRAY { $$ = 'array'; }
;
function_call_parameter_list:
non_empty_function_call_parameter_list { $$ = $1; }
| /* empty */ { $$ = array(); }
;
non_empty_function_call_parameter_list:
expr_without_variable { $$ = array(new Node_FuncCallStmtParam(array('value' => $1, 'byRef' => false))); }
| variable { $$ = array(new Node_FuncCallStmtParam(array('value' => $1, 'byRef' => false))); }
| '&' variable { $$ = array(new Node_FuncCallStmtParam(array('value' => $2, 'byRef' => true))); }
| non_empty_function_call_parameter_list ',' expr_without_variable
{ $1[] = new Node_FuncCallStmtParam(array('value' => $3, 'byRef' => false)); $$ = $1; }
| non_empty_function_call_parameter_list ',' variable
{ $1[] = new Node_FuncCallStmtParam(array('value' => $3, 'byRef' => false)); $$ = $1; }
| non_empty_function_call_parameter_list ',' '&' variable
{ $1[] = new Node_FuncCallStmtParam(array('value' => $4, 'byRef' => true)); $$ = $1; }
;
global_var_list:
global_var_list ',' global_var { $1[] = $3; $$ = $1; }
| global_var { $$ = array($1); }
;
global_var:
T_VARIABLE { $$ = new Node_Variable(array('name' => substr($1, 1))); }
| '$' variable { $$ = new Node_Variable(array('name' => $2)); }
| '$' '{' expr '}' { $$ = new Node_Variable(array('name' => $3)); }
;
static_var_list:
static_var_list ',' T_VARIABLE { $1[] = new Node_StaticStmtVar(array('name' => substr($3, 1), 'default' => null)); $$ = $1; }
| static_var_list ',' T_VARIABLE '=' static_scalar { $1[] = new Node_StaticStmtVar(array('name' => substr($3, 1), 'default' => $5)); $$ = $1; }
| T_VARIABLE { $$ = array(new Node_StaticStmtVar(array('name' => substr($1, 1), 'default' => null))); }
| T_VARIABLE '=' static_scalar { $$ = array(new Node_StaticStmtVar(array('name' => substr($1, 1), 'default' => $3))); }
;
class_statement_list:
class_statement_list class_statement { $1[] = $2; $$ = $1; }
| /* empty */ { $$ = array(); }
;
class_statement:
variable_modifiers class_variable_declaration ';' { $$ = new Node_PropertyStmt(array('type' => $1, 'props' => $2)); }
| class_constant_declaration ';' { $$ = new Node_ClassConstStmt(array('consts' => $1)); }
| method_modifiers T_FUNCTION optional_ref T_STRING '(' parameter_list ')' method_body
{ $$ = new Node_ClassMethodStmt(array('type' => $1, 'byRef' => $3, 'name' => $4, 'params' => $6, 'stmts' => $8)); }
;
method_body:
';' /* abstract method */ { $$ = null; }
| '{' inner_statement_list '}' { $$ = $2; }
;
variable_modifiers:
non_empty_member_modifiers { $$ = $1; }
| T_VAR { $$ = Node_ClassStmt::MODIFIER_PUBLIC; }
;
method_modifiers:
/* empty */ { $$ = Node_ClassStmt::MODIFIER_PUBLIC; }
| non_empty_member_modifiers { $$ = $1; }
;
non_empty_member_modifiers:
member_modifier { $$ = $1; }
| non_empty_member_modifiers member_modifier { Node_ClassStmt::verifyModifier($1, $2); $$ = $1 | $2; }
;
member_modifier:
T_PUBLIC { $$ = Node_ClassStmt::MODIFIER_PUBLIC; }
| T_PROTECTED { $$ = Node_ClassStmt::MODIFIER_PROTECTED; }
| T_PRIVATE { $$ = Node_ClassStmt::MODIFIER_PRIVATE; }
| T_STATIC { $$ = Node_ClassStmt::MODIFIER_STATIC; }
| T_ABSTRACT { $$ = Node_ClassStmt::MODIFIER_ABSTRACT; }
| T_FINAL { $$ = Node_ClassStmt::MODIFIER_FINAL; }
;
class_variable_declaration:
class_variable_declaration ',' T_VARIABLE
{ $1[] = new Node_PropertyStmtProperty(array('name' => substr($3, 1), 'default' => null)); $$ = $1; }
| class_variable_declaration ',' T_VARIABLE '=' static_scalar
{ $1[] = new Node_PropertyStmtProperty(array('name' => substr($3, 1), 'default' => $5)); $$ = $1; }
| T_VARIABLE
{ $$ = array(new Node_PropertyStmtProperty(array('name' => substr($1, 1), 'default' => null))); }
| T_VARIABLE '=' static_scalar
{ $$ = array(new Node_PropertyStmtProperty(array('name' => substr($1, 1), 'default' => $3))); }
;
class_constant_declaration:
class_constant_declaration ',' T_STRING '=' static_scalar
{ $1[] = new Node_ClassConstStmtConst(array('name' => $3, 'value' => $5)); $$ = $1; }
| T_CONST T_STRING '=' static_scalar { $$ = array(new Node_ClassConstStmtConst(array('name' => $2, 'value' => $4))); }
;
expr_list:
expr_list ',' expr { $1[] = $3; $$ = $1; }
| expr { $$ = array($1); }
;
for_expr:
/* empty */ { $$ = array(); }
| expr_list { $$ = $1; }
;
expr_without_variable:
T_LIST '(' assignment_list ')' '=' expr { $$ = new Node_ListExpr(array('assignList' => $3, 'expr' => $6)); }
| variable '=' expr { $$ = new Node_AssignExpr(array('var' => $1, 'expr' => $3)); }
| variable '=' '&' variable { $$ = new Node_AssignRefExpr(array('var' => $1, 'refVar' => $4)); }
| variable '=' '&' T_NEW class_name_reference ctor_arguments
{ $$ = new Node_AssignExpr(array('var' => $1, 'expr' => new Node_NewExpr(array('class' => $5, 'args' => $6)))); }
/* reference dropped intentially, TODO: Throw error? */
| T_NEW class_name_reference ctor_arguments { $$ = new Node_NewExpr(array('class' => $2, 'args' => $3)); }
| T_CLONE expr { $$ = new Node_CloneExpr(array('expr' => $2)); }
| variable T_PLUS_EQUAL expr { $$ = new Node_AssignPlusExpr(array('var' => $1, 'expr' => $3)); }
| variable T_MINUS_EQUAL expr { $$ = new Node_AssignMinusExpr(array('var' => $1, 'expr' => $3)); }
| variable T_MUL_EQUAL expr { $$ = new Node_AssignMulExpr(array('var' => $1, 'expr' => $3)); }
| variable T_DIV_EQUAL expr { $$ = new Node_AssignDivExpr(array('var' => $1, 'expr' => $3)); }
| variable T_CONCAT_EQUAL expr { $$ = new Node_AssignConcatExpr(array('var' => $1, 'expr' => $3)); }
| variable T_MOD_EQUAL expr { $$ = new Node_AssignModExpr(array('var' => $1, 'expr' => $3)); }
| variable T_AND_EQUAL expr { $$ = new Node_AssignBinAndExpr(array('var' => $1, 'expr' => $3)); }
| variable T_OR_EQUAL expr { $$ = new Node_AssignBinOrExpr(array('var' => $1, 'expr' => $3)); }
| variable T_XOR_EQUAL expr { $$ = new Node_AssignBinXorExpr(array('var' => $1, 'expr' => $3)); }
| variable T_SL_EQUAL expr { $$ = new Node_AssignShiftLeftExpr(array('var' => $1, 'expr' => $3)); }
| variable T_SR_EQUAL expr { $$ = new Node_AssignShiftRightExpr(array('var' => $1, 'expr' => $3)); }
| variable T_INC { $$ = new Node_PostIncExpr(array('var' => $1)); }
| T_INC variable { $$ = new Node_PreIncExpr(array('var' => $2)); }
| variable T_DEC { $$ = new Node_PostDecExpr(array('var' => $1)); }
| T_DEC variable { $$ = new Node_PreDecExpr(array('var' => $2)); }
| expr T_BOOLEAN_OR expr { $$ = new Node_BooleanOrExpr(array('left' => $1, 'right' => $3)); }
| expr T_BOOLEAN_AND expr { $$ = new Node_BooleanAndExpr(array('left' => $1, 'right' => $3)); }
| expr T_LOGICAL_OR expr { $$ = new Node_LogicalOrExpr(array('left' => $1, 'right' => $3)); }
| expr T_LOGICAL_AND expr { $$ = new Node_LogicalAndExpr(array('left' => $1, 'right' => $3)); }
| expr T_LOGICAL_XOR expr { $$ = new Node_LogicalXorExpr(array('left' => $1, 'right' => $3)); }
| expr '|' expr { $$ = new Node_BinaryOrExpr(array('left' => $1, 'right' => $3)); }
| expr '&' expr { $$ = new Node_BinaryAndExpr(array('left' => $1, 'right' => $3)); }
| expr '^' expr { $$ = new Node_BinaryXorExpr(array('left' => $1, 'right' => $3)); }
| expr '.' expr { $$ = new Node_ConcatExpr(array('left' => $1, 'right' => $3)); }
| expr '+' expr { $$ = new Node_PlusExpr(array('left' => $1, 'right' => $3)); }
| expr '-' expr { $$ = new Node_MinusExpr(array('left' => $1, 'right' => $3)); }
| expr '*' expr { $$ = new Node_MulExpr(array('left' => $1, 'right' => $3)); }
| expr '/' expr { $$ = new Node_DivExpr(array('left' => $1, 'right' => $3)); }
| expr '%' expr { $$ = new Node_ModExpr(array('left' => $1, 'right' => $3)); }
| expr T_SL expr { $$ = new Node_ShiftLeftExpr(array('left' => $1, 'right' => $3)); }
| expr T_SR expr { $$ = new Node_ShiftRightExpr(array('left' => $1, 'right' => $3)); }
| '+' expr %prec T_INC { $$ = new Node_UnaryPlusExpr(array('expr' => $2)); }
| '-' expr %prec T_INC { $$ = new Node_UnaryMinusExpr(array('expr' => $2)); }
| '!' expr { $$ = new Node_BooleanNotExpr(array('expr' => $2)); }
| '~' expr { $$ = new Node_BinaryNotExpr(array('expr' => $2)); }
| expr T_IS_IDENTICAL expr { $$ = new Node_IdenticalExpr(array('left' => $1, 'right' => $3)); }
| expr T_IS_NOT_IDENTICAL expr { $$ = new Node_NotIdenticalExpr(array('left' => $1, 'right' => $3)); }
| expr T_IS_EQUAL expr { $$ = new Node_EqualExpr(array('left' => $1, 'right' => $3)); }
| expr T_IS_NOT_EQUAL expr { $$ = new Node_NotEqualExpr(array('left' => $1, 'right' => $3)); }
| expr '<' expr { $$ = new Node_SmallerExpr(array('left' => $1, 'right' => $3)); }
| expr T_IS_SMALLER_OR_EQUAL expr { $$ = new Node_SmallerOrEqualExpr(array('left' => $1, 'right' => $3)); }
| expr '>' expr { $$ = new Node_GreaterExpr(array('left' => $1, 'right' => $3)); }
| expr T_IS_GREATER_OR_EQUAL expr { $$ = new Node_GreaterOrEqualExpr(array('left' => $1, 'right' => $3)); }
| expr T_INSTANCEOF class_name_reference { $$ = new Node_InstanceOfExpr(array('expr' => $1, 'class' => $3)); }
| '(' expr ')' { $$ = $2; }
| expr '?' expr ':' expr { $$ = new Node_TernaryExpr(array('cond' => $1, 'if' => $3, 'else' => $5)); }
| expr '?' ':' expr { $$ = new Node_TernaryExpr(array('cond' => $1, 'if' => null, 'else' => $4)); }
| T_ISSET '(' variables_list ')' { $$ = new Node_IssetExpr(array('vars' => $3)); }
| T_EMPTY '(' variable ')' { $$ = new Node_EmptyExpr(array('var' => $3)); }
| T_INCLUDE expr { $$ = new Node_IncludeExpr(array('expr' => $2, 'type' => Node_IncludeExpr::TYPE_INCLUDE)); }
| T_INCLUDE_ONCE expr { $$ = new Node_IncludeExpr(array('expr' => $2, 'type' => Node_IncludeExpr::TYPE_INCLUDE_ONCE)); }
| T_EVAL '(' expr ')' { $$ = new Node_EvalExpr(array('expr' => $3)); }
| T_REQUIRE expr { $$ = new Node_IncludeExpr(array('expr' => $2, 'type' => Node_IncludeExpr::TYPE_REQUIRE)); }
| T_REQUIRE_ONCE expr { $$ = new Node_IncludeExpr(array('expr' => $2, 'type' => Node_IncludeExpr::TYPE_REQUIRE_ONCE)); }
| T_INT_CAST expr { $$ = new Node_IntCastExpr(array('expr' => $2)); }
| T_DOUBLE_CAST expr { $$ = new Node_DoubleCastExpr(array('expr' => $2)); }
| T_STRING_CAST expr { $$ = new Node_StringCastExpr(array('expr' => $2)); }
| T_ARRAY_CAST expr { $$ = new Node_ArrayCastExpr(array('expr' => $2)); }
| T_OBJECT_CAST expr { $$ = new Node_ObjectCastExpr(array('expr' => $2)); }
| T_BOOL_CAST expr { $$ = new Node_BoolCastExpr(array('expr' => $2)); }
| T_UNSET_CAST expr { $$ = new Node_UnsetCastExpr(array('expr' => $2)); }
| T_EXIT exit_expr { $$ = new Node_ExitExpr(array('expr' => $2)); }
| '@' expr { $$ = new Node_ErrorSupressExpr(array('expr' => $2)); }
| scalar { $$ = $1; }
| T_ARRAY '(' array_pair_list ')' { $$ = new Node_ArrayExpr(array('items' => $3)); }
| '`' backticks_expr '`' { $$ = new Node_ShellExecExpr(array('expr' => $2)); }
| T_PRINT expr { $$ = new Node_PrintExpr(array('expr' => $2)); }
| T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
{ $$ = new Node_LambdaFuncExpr(array('byRef' => $2, 'params' => $4, 'useVars' => $6, 'stmts' => $8)); }
;
lexical_vars:
/* empty */ { $$ = array(); }
| T_USE '(' lexical_var_list ')' { $$ = $3; }
;
lexical_var_list:
lexical_var_list ',' optional_ref T_VARIABLE
{ $1[] = new Node_LambdaFuncExprUse(array('var' => substr($4, 1), 'byRef' => $3)); $$ = $1; }
| optional_ref T_VARIABLE
{ $$ = array(new Node_LambdaFuncExprUse(array('var' => substr($2, 1), 'byRef' => $1))); }
;
function_call:
name '(' function_call_parameter_list ')' { $$ = new Node_FuncCallExpr(array('func' => $1, 'args' => $3)); }
| class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' function_call_parameter_list ')'
{ $$ = new Node_StaticCallExpr(array('class' => $1, 'func' => $3, 'args' => $5)); }
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects '(' function_call_parameter_list ')'
{ $$ = new Node_StaticCallExpr(array('class' => $1, 'func' => $3, 'args' => $5)); }
| reference_variable T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' function_call_parameter_list ')'
{ $$ = new Node_StaticCallExpr(array('class' => $1, 'func' => $3, 'args' => $5)); }
| reference_variable T_PAAMAYIM_NEKUDOTAYIM variable_without_objects '(' function_call_parameter_list ')'
{ $$ = new Node_StaticCallExpr(array('class' => $1, 'func' => $3, 'args' => $5)); }
| variable_without_objects '(' function_call_parameter_list ')'
{ $$ = new Node_FuncCallExpr(array('func' => $1, 'args' => $3)); }
;
class_name:
T_STATIC { $$ = 'static'; }
| name { $$ = $1; }
;
name:
namespace_name { $$ = $1; }
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $3->resolveType(Node_Name::RELATIVE); $$ = $3; }
| T_NS_SEPARATOR namespace_name { $2->resolveType(Node_Name::ABSOLUTE); $$ = $2; }
;
class_name_reference:
class_name { $$ = $1; }
| dynamic_class_name_reference { $$ = $1; }
;
dynamic_class_name_reference:
dynamic_class_name_reference T_OBJECT_OPERATOR object_property
{ $$ = new Node_PropertyFetchExpr(array('var' => $1, 'name' => $3)); }
| base_variable { $$ = $1; }
;
exit_expr:
/* empty */ { $$ = null; }
| '(' ')' { $$ = null; }
| '(' expr ')' { $$ = $2; }
;
backticks_expr:
/* empty */ { $$ = null; }
| T_ENCAPSED_AND_WHITESPACE { $$ = stripcslashes($1); }
| encaps_list { $$ = $1; }
;
ctor_arguments:
/* empty */ { $$ = array(); }
| '(' function_call_parameter_list ')' { $$ = $2; }
;
common_scalar:
T_LNUMBER { $$ = new Node_LNumberScalar(array('value' => (int) $1)); }
| T_DNUMBER { $$ = new Node_DNumberScalar(array('value' => (double) $1)); }
| T_CONSTANT_ENCAPSED_STRING { $$ = new Node_StringScalar(array('value' => str_replace(array('\\\'', '\\\\'), array('\'', '\\'), substr($1, 1, -1)))); }
| T_LINE { $$ = new Node_LineConstScalar(array()); }
| T_FILE { $$ = new Node_FileConstScalar(array()); }
| T_DIR { $$ = new Node_DirConstScalar(array()); }
| T_CLASS_C { $$ = new Node_ClassConstScalar(array()); }
| T_METHOD_C { $$ = new Node_MethodConstScalar(array()); }
| T_FUNC_C { $$ = new Node_FuncConstScalar(array()); }
| T_NS_C { $$ = new Node_NSConstScalar(array()); }
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
{ $$ = new Node_StringScalar(array('value' => stripcslashes($2))); }
| T_START_HEREDOC T_END_HEREDOC { $$ = new Node_StringScalar(array('value' => '')); }
;
static_scalar: /* compile-time evaluated scalars */
common_scalar { $$ = $1; }
| name { $$ = new Node_ConstFetchExpr(array('name' => $1)); }
| '+' static_scalar { $$ = new Node_UnaryPlusExpr(array('expr' => $2)); }
| '-' static_scalar { $$ = new Node_UnaryMinusExpr(array('expr' => $2)); }
| T_ARRAY '(' static_array_pair_list ')' { $$ = new Node_ArrayExpr(array('items' => $3)); }
| class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = new Node_ClassConstFetchExpr(array('class' => $1, 'name' => $3)); }
;
scalar:
T_STRING_VARNAME { $$ = new Node_StringScalar(array('value' => $1)); }
| class_constant { $$ = $1; }
| name { $$ = new Node_ConstFetchExpr(array('name' => $1)); }
| common_scalar { $$ = $1; }
| '"' encaps_list '"' { $$ = new Node_EncapsedString(array('parts' => $2)); }
| T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = new Node_EncapsedString(array('parts' => $2)); }
;
static_array_pair_list:
/* empty */ { $$ = array(); }
| non_empty_static_array_pair_list optional_comma { $$ = $1; }
;
optional_comma:
/* empty */
| ','
;
non_empty_static_array_pair_list:
non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW static_scalar
{ $1[] = new Node_ArrayExprItem(array('key' => $3, 'value' => $5, 'byRef' => false)); $$ = $1; }
| non_empty_static_array_pair_list ',' static_scalar { $1[] = new Node_ArrayExprItem(array('key' => null, 'value' => $3, 'byRef' => false)); $$ = $1; }
| static_scalar T_DOUBLE_ARROW static_scalar { $$ = array(new Node_ArrayExprItem(array('key' => $1, 'value' => $3, 'byRef' => false))); }
| static_scalar { $$ = array(new Node_ArrayExprItem(array('key' => null, 'value' => $1, 'byRef' => false))); }
;
expr:
variable { $$ = $1; }
| expr_without_variable { $$ = $1; }
;
variable:
variable T_OBJECT_OPERATOR object_property '(' function_call_parameter_list ')'
{ $$ = new Node_MethodCallExpr(array('var' => $1, 'name' => $3, 'args' => $5)); }
| variable T_OBJECT_OPERATOR object_property
{ $$ = new Node_PropertyFetchExpr(array('var' => $1, 'name' => $3)); }
| base_variable { $$ = $1; }
| function_call { $$ = $1; }
;
variable_without_objects:
reference_variable { $$ = $1; }
| '$' reference_variable { $$ = new Node_Variable(array('name' => $2)); }
;
base_variable:
variable_without_objects { $$ = $1; }
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects
{ $$ = new Node_StaticPropertyFetchExpr(array('class' => $1, 'name' => $3)); }
| reference_variable T_PAAMAYIM_NEKUDOTAYIM variable_without_objects
{ $$ = new Node_StaticPropertyFetchExpr(array('class' => $1, 'name' => $3)); }
;
reference_variable:
reference_variable '[' dim_offset ']' { $$ = new Node_ArrayDimFetchExpr(array('var' => $1, 'dim' => $3)); }
| reference_variable '{' expr '}' { $$ = new Node_ArrayDimFetchExpr(array('var' => $1, 'dim' => $3)); }
| T_VARIABLE { $$ = new Node_Variable(array('name' => substr($1, 1))); }
| '$' '{' expr '}' { $$ = new Node_Variable(array('name' => $3)); }
;
dim_offset:
/* empty */ { $$ = null; }
| expr { $$ = $1; }
;
object_property:
object_dim_list { $$ = $1; }
| variable_without_objects { $$ = $1; }
;
object_dim_list:
object_dim_list '[' dim_offset ']' { $$ = new Node_ArrayDimFetchExpr(array('var' => $1, 'dim' => $3)); }
| object_dim_list '{' expr '}' { $$ = new Node_ArrayDimFetchExpr(array('var' => $1, 'dim' => $3)); }
| T_STRING { $$ = $1; }
| '{' expr '}' { $$ = $2; }
;
assignment_list:
assignment_list ',' assignment_list_element { $1[] = $3; $$ = $1; }
| assignment_list_element { $$ = array($1); }
;
assignment_list_element:
variable { $$ = $1; }
| T_LIST '(' assignment_list ')' { $$ = $3; }
| /* empty */ { $$ = null; }
;
array_pair_list:
/* empty */ { $$ = array(); }
| non_empty_array_pair_list optional_comma { $$ = $1; }
;
non_empty_array_pair_list:
non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
{ $1[] = new Node_ArrayExprItem(array('key' => $3, 'value' => $5, 'byRef' => false)); $$ = $1; }
| non_empty_array_pair_list ',' expr { $1[] = new Node_ArrayExprItem(array('key' => null, 'value' => $3, 'byRef' => false)); $$ = $1; }
| expr T_DOUBLE_ARROW expr { $$ = array(new Node_ArrayExprItem(array('key' => $1, 'value' => $3, 'byRef' => false))); }
| expr { $$ = array(new Node_ArrayExprItem(array('key' => null, 'value' => $1, 'byRef' => false))); }
| non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' variable
{ $1[] = new Node_ArrayExprItem(array('key' => $3, 'value' => $6, 'byRef' => true)); $$ = $1; }
| non_empty_array_pair_list ',' '&' variable { $1[] = new Node_ArrayExprItem(array('key' => null, 'value' => $4, 'byRef' => true)); $$ = $1; }
| expr T_DOUBLE_ARROW '&' variable { $$ = array(new Node_ArrayExprItem(array('key' => $1, 'value' => $4, 'byRef' => true))); }
| '&' variable { $$ = array(new Node_ArrayExprItem(array('key' => null, 'value' => $2, 'byRef' => true))); }
;
encaps_list:
encaps_list encaps_var { $1[] = $2; $$ = $1; }
| encaps_list T_ENCAPSED_AND_WHITESPACE { $1[] = stripcslashes($2); $$ = $1; }
| encaps_var { $$ = array($1); }
| T_ENCAPSED_AND_WHITESPACE encaps_var { $$ = array(stripcslashes($1), $2); }
;
encaps_var:
T_VARIABLE { $$ = new Node_Variable(array('name' => substr($1, 1))); }
| T_VARIABLE '[' encaps_var_offset ']' { $$ = new Node_ArrayDimFetchExpr(array('var' => new Node_Variable(array('name' => substr($1, 1))), 'dim' => $3)); }
| T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = new Node_PropertyFetchExpr(array('var' => new Node_Variable(array('name' => substr($1, 1))), 'name' => $3)); }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = new Node_Variable(array('name' => $2)); }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
{ $$ = new Node_ArrayDimFetchExpr(array('var' => new Node_Variable(array('name' => $2)), 'dim' => $4)); }
| T_CURLY_OPEN variable '}' { $$ = $2; }
;
encaps_var_offset:
T_STRING { $$ = new Node_StringScalar(array('value' => $1)); }
| T_NUM_STRING { $$ = new Node_LNumberScalar(array('value' => (int) $1)); }
| T_VARIABLE { $$ = new Node_Variable(array('name' => substr($1, 1))); }
;
class_constant:
class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = new Node_ClassConstFetchExpr(array('class' => $1, 'name' => $3)); }
| reference_variable T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = new Node_ClassConstFetchExpr(array('class' => $1, 'name' => $3)); }
;
%%

View File

@ -0,0 +1,756 @@
%pure_parser
%expect 2
%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
%left ','
%left T_LOGICAL_OR
%left T_LOGICAL_XOR
%left T_LOGICAL_AND
%right T_PRINT
%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL
%left '?' ':'
%left T_BOOLEAN_OR
%left T_BOOLEAN_AND
%left '|'
%left '^'
%left '&'
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
%left T_SL T_SR
%left '+' '-' '.'
%left '*' '/' '%'
%right '!'
%nonassoc T_INSTANCEOF
%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
%right '['
%nonassoc T_NEW T_CLONE
%token T_EXIT
%token T_IF
%left T_ELSEIF
%left T_ELSE
%left T_ENDIF
%token T_LNUMBER
%token T_DNUMBER
%token T_STRING
%token T_STRING_VARNAME
%token T_VARIABLE
%token T_NUM_STRING
%token T_INLINE_HTML
%token T_CHARACTER
%token T_BAD_CHARACTER
%token T_ENCAPSED_AND_WHITESPACE
%token T_CONSTANT_ENCAPSED_STRING
%token T_ECHO
%token T_DO
%token T_WHILE
%token T_ENDWHILE
%token T_FOR
%token T_ENDFOR
%token T_FOREACH
%token T_ENDFOREACH
%token T_DECLARE
%token T_ENDDECLARE
%token T_AS
%token T_SWITCH
%token T_ENDSWITCH
%token T_CASE
%token T_DEFAULT
%token T_BREAK
%token T_CONTINUE
%token T_GOTO
%token T_FUNCTION
%token T_CONST
%token T_RETURN
%token T_TRY
%token T_CATCH
%token T_THROW
%token T_USE
%token T_GLOBAL
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
%token T_VAR
%token T_UNSET
%token T_ISSET
%token T_EMPTY
%token T_HALT_COMPILER
%token T_CLASS
%token T_INTERFACE
%token T_EXTENDS
%token T_IMPLEMENTS
%token T_OBJECT_OPERATOR
%token T_DOUBLE_ARROW
%token T_LIST
%token T_ARRAY
%token T_CLASS_C
%token T_METHOD_C
%token T_FUNC_C
%token T_LINE
%token T_FILE
%token T_COMMENT
%token T_DOC_COMMENT
%token T_OPEN_TAG
%token T_OPEN_TAG_WITH_ECHO
%token T_CLOSE_TAG
%token T_WHITESPACE
%token T_START_HEREDOC
%token T_END_HEREDOC
%token T_DOLLAR_OPEN_CURLY_BRACES
%token T_CURLY_OPEN
%token T_PAAMAYIM_NEKUDOTAYIM
%token T_NAMESPACE
%token T_NS_C
%token T_DIR
%token T_NS_SEPARATOR
%%
start:
top_statement_list { $$ = $1; }
;
top_statement_list:
top_statement_list top_statement { pushNormalizing($1, $2); }
| /* empty */ { init(); }
;
namespace_name:
namespace_name_sub { $$ = Name[parts: $1]; }
;
namespace_name_sub:
T_STRING { init($1); }
| namespace_name_sub T_NS_SEPARATOR T_STRING { push($1, $3); }
;
top_statement:
statement { $$ = $1; }
| function_declaration_statement { $$ = $1; }
| class_declaration_statement { $$ = $1; }
| T_HALT_COMPILER '(' ')' ';' { YYACCEPT; }
| T_NAMESPACE namespace_name ';' { $$ = NamespaceStmt[ns: $2]; }
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = array(NamespaceStmt[ns: $2], $4); }
| T_NAMESPACE '{' top_statement_list '}' { $$ = array(NamespaceStmt[ns: null], $3); }
| T_USE use_declarations ';' { $$ = UseStmt[uses: $2]; }
| constant_declaration ';' { $$ = ConstStmt[consts: $1]; }
;
use_declarations:
use_declarations ',' use_declaration { push($1, $3); }
| use_declaration { init($1); }
;
use_declaration:
namespace_name { $$ = UseStmtUse[ns: $1, alias: null]; }
| namespace_name T_AS T_STRING { $$ = UseStmtUse[ns: $1, alias: $3]; }
| T_NS_SEPARATOR namespace_name { $$ = UseStmtUse[ns: $2, alias: null]; }
| T_NS_SEPARATOR namespace_name T_AS T_STRING { $$ = UseStmtUse[ns: $2, alias: $4]; }
;
constant_declaration:
constant_declaration ',' T_STRING '=' static_scalar { push($1, ConstStmtConst[name: $3, value: $5]); }
| T_CONST T_STRING '=' static_scalar { init(ConstStmtConst[name: $2, value: $4]); }
;
inner_statement_list:
inner_statement_list inner_statement { pushNormalizing($1, $2); }
| /* empty */ { init(); }
;
inner_statement:
statement { $$ = $1; }
| function_declaration_statement { $$ = $1; }
| class_declaration_statement { $$ = $1; }
| T_HALT_COMPILER '(' ')' ';' { error('__halt_compiler() can only be used from the outermost scope'); }
;
statement:
'{' inner_statement_list '}' { $$ = $2; }
| T_IF '(' expr ')' statement elseif_list else_single { $$ = IfStmt[cond: $3, stmts: toArray($5), elseifList: $6, else: $7]; }
| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
{ $$ = IfStmt[cond: $3, stmts: $6, elseifList: $7, else: $8]; }
| T_WHILE '(' expr ')' while_statement { $$ = WhileStmt[cond: $3, stmts: toArray($5)]; }
| T_DO statement T_WHILE '(' expr ')' ';' { $$ = DoStmt[stmts: toArray($2), cond: $5]; }
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
{ $$ = ForStmt[init: $3, cond: $5, loop: $7, stmts: toArray($9)]; }
| T_SWITCH '(' expr ')' switch_case_list { $$ = SwitchStmt[cond: $3, caseList: $5]; }
| T_BREAK ';' { $$ = BreakStmt[num: null]; }
| T_BREAK expr ';' { $$ = BreakStmt[num: $2]; }
| T_CONTINUE ';' { $$ = ContinueStmt[num: null]; }
| T_CONTINUE expr ';' { $$ = ContinueStmt[num: $2]; }
| T_RETURN ';' { $$ = ReturnStmt[expr: null]; }
| T_RETURN expr_without_variable ';' { $$ = ReturnStmt[expr: $2]; }
| T_RETURN variable ';' { $$ = ReturnStmt[expr: $2]; }
| T_GLOBAL global_var_list ';' { $$ = GlobalStmt[vars: $2]; }
| T_STATIC static_var_list ';' { $$ = StaticStmt[vars: $2]; }
| T_ECHO expr_list ';' { $$ = EchoStmt[exprs: $2]; }
| T_INLINE_HTML { $$ = InlineHTMLStmt[value: $1]; }
| expr ';' { $$ = $1; }
| T_UNSET '(' variables_list ')' ';' { $$ = UnsetStmt[vars: $3]; }
| T_FOREACH '(' expr T_AS variable ')' foreach_statement
{ $$ = ForeachStmt[expr: $3, keyVar: null, byRef: false, valueVar: $5, stmts: toArray($7)]; }
| T_FOREACH '(' expr T_AS '&' variable ')' foreach_statement
{ $$ = ForeachStmt[expr: $3, keyVar: null, byRef: true, valueVar: $6, stmts: toArray($8)]; }
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW optional_ref variable ')' foreach_statement
{ $$ = ForeachStmt[expr: $3, keyVar: $5, byRef: $7, valueVar: $8, stmts: toArray($10)]; }
| T_DECLARE '(' declare_list ')' declare_statement { $$ = DeclareStmt[declares: $3, stmts: toArray($5)]; }
| ';' { $$ = NoopStmt[]; }
| T_TRY '{' inner_statement_list '}' catches { $$ = TryCatchStmt[stmts: $3, catches: $5]; }
| T_THROW expr ';' { $$ = ThrowStmt[expr: $2]; }
| T_GOTO T_STRING ';' { $$ = GotoStmt[name: $2]; }
| T_STRING ':' { $$ = LabelStmt[name: $1]; }
;
catches:
catch { init($1); }
| catches catch { push($1, $2); }
;
catch:
T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
{ $$ = CatchStmt[type: $3, var: parseVar($4), stmts: $7]; }
;
variables_list:
variable { init($1); }
| variables_list ',' variable { push($1, $3); }
;
optional_ref:
/* empty */ { $$ = false; }
| '&' { $$ = true; }
;
function_declaration_statement:
T_FUNCTION optional_ref T_STRING '(' parameter_list ')' '{' inner_statement_list '}'
{ $$ = FuncStmt[byRef: $2, name: $3, params: $5, stmts: $8]; }
;
class_declaration_statement:
class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}'
{ $$ = ClassStmt[type: $1, name: $2, extends: $3, implements: $4, stmts: $6]; }
| T_INTERFACE T_STRING interface_extends_list '{' class_statement_list '}'
{ $$ = InterfaceStmt[name: $2, extends: $3, stmts: $5]; }
;
class_entry_type:
T_CLASS { $$ = 0; }
| T_ABSTRACT T_CLASS { $$ = ClassStmt::MODIFIER_ABSTRACT; }
| T_FINAL T_CLASS { $$ = ClassStmt::MODIFIER_FINAL; }
;
extends_from:
/* empty */ { $$ = null; }
| T_EXTENDS name { $$ = $2; }
;
interface_extends_list:
/* empty */ { $$ = array(); }
| T_EXTENDS interface_list { $$ = $2; }
;
implements_list:
/* empty */ { $$ = array(); }
| T_IMPLEMENTS interface_list { $$ = $2; }
;
interface_list:
name { init($1); }
| interface_list ',' name { push($1, $3); }
;
for_statement:
statement { $$ = $1; }
| ':' inner_statement_list T_ENDFOR ';' { $$ = $2; }
;
foreach_statement:
statement { $$ = $1; }
| ':' inner_statement_list T_ENDFOREACH ';' { $$ = $2; }
;
declare_statement:
statement { $$ = $1; }
| ':' inner_statement_list T_ENDDECLARE ';' { $$ = $2; }
;
declare_list:
T_STRING '=' static_scalar { init(DeclareStmtDeclare[key: $1, value: $3]); }
| declare_list ',' T_STRING '=' static_scalar { push($1, DeclareStmtDeclare[key: $3, value: $5]); }
;
switch_case_list:
'{' case_list '}' { $$ = $2; }
| '{' ';' case_list '}' { $$ = $3; }
| ':' case_list T_ENDSWITCH ';' { $$ = $2; }
| ':' ';' case_list T_ENDSWITCH ';' { $$ = $3; }
;
case_list:
/* empty */ { init(); }
| case_list T_CASE expr case_separator inner_statement_list
{ push($1, CaseStmt[cond: $3, stmts: $5]); }
| case_list T_DEFAULT case_separator inner_statement_list
{ push($1, CaseStmt[cond: null, stmts: $4]); }
;
case_separator:
':'
| ';'
;
while_statement:
statement { $$ = $1; }
| ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; }
;
elseif_list:
/* empty */ { init();}
| elseif_list T_ELSEIF '(' expr ')' statement { push($1, ElseIfStmt[cond: $4, stmts: toArray($6)]); }
;
new_elseif_list:
/* empty */ { init(); }
| new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list
{ push($1, ElseIfStmt[cond: $4, stmts: $7]); }
;
else_single:
/* empty */ { $$ = null; }
| T_ELSE statement { $$ = ElseStmt[stmts: toArray($2)]; }
;
new_else_single:
/* empty */ { $$ = null; }
| T_ELSE ':' inner_statement_list { $$ = ElseStmt[stmts: $3]; }
;
parameter_list:
non_empty_parameter_list { $$ = $1; }
| /* empty */ { $$ = array(); }
;
non_empty_parameter_list:
optional_class_type optional_ref T_VARIABLE
{ init(FuncStmtParam[type: $1, name: parseVar($3), byRef: $2, default: null]); }
| optional_class_type optional_ref T_VARIABLE '=' static_scalar
{ init(FuncStmtParam[type: $1, name: parseVar($3), byRef: $2, default: $5]); }
| non_empty_parameter_list ',' optional_class_type optional_ref T_VARIABLE
{ push($1, FuncStmtParam[type: $3, name: parseVar($5), byRef: $4, default: null]); }
| non_empty_parameter_list ',' optional_class_type optional_ref T_VARIABLE '=' static_scalar
{ push($1, FuncStmtParam[type: $3, name: parseVar($5), byRef: $4, default: $7]); }
;
optional_class_type:
/* empty */ { $$ = null; }
| name { $$ = $1; }
| T_ARRAY { $$ = 'array'; }
;
function_call_parameter_list:
non_empty_function_call_parameter_list { $$ = $1; }
| /* empty */ { $$ = array(); }
;
non_empty_function_call_parameter_list:
expr_without_variable { init(FuncCallStmtParam[value: $1, byRef: false]); }
| variable { init(FuncCallStmtParam[value: $1, byRef: false]); }
| '&' variable { init(FuncCallStmtParam[value: $2, byRef: true]); }
| non_empty_function_call_parameter_list ',' expr_without_variable
{ push($1, FuncCallStmtParam[value: $3, byRef: false]); }
| non_empty_function_call_parameter_list ',' variable
{ push($1, FuncCallStmtParam[value: $3, byRef: false]); }
| non_empty_function_call_parameter_list ',' '&' variable
{ push($1, FuncCallStmtParam[value: $4, byRef: true]); }
;
global_var_list:
global_var_list ',' global_var { push($1, $3); }
| global_var { init($1); }
;
global_var:
T_VARIABLE { $$ = Variable[name: parseVar($1)]; }
| '$' variable { $$ = Variable[name: $2]; }
| '$' '{' expr '}' { $$ = Variable[name: $3]; }
;
static_var_list:
static_var_list ',' T_VARIABLE { push($1, StaticStmtVar[name: parseVar($3), default: null]); }
| static_var_list ',' T_VARIABLE '=' static_scalar { push($1, StaticStmtVar[name: parseVar($3), default: $5]); }
| T_VARIABLE { init(StaticStmtVar[name: parseVar($1), default: null]); }
| T_VARIABLE '=' static_scalar { init(StaticStmtVar[name: parseVar($1), default: $3]); }
;
class_statement_list:
class_statement_list class_statement { push($1, $2); }
| /* empty */ { init(); }
;
class_statement:
variable_modifiers class_variable_declaration ';' { $$ = PropertyStmt[type: $1, props: $2]; }
| class_constant_declaration ';' { $$ = ClassConstStmt[consts: $1]; }
| method_modifiers T_FUNCTION optional_ref T_STRING '(' parameter_list ')' method_body
{ $$ = ClassMethodStmt[type: $1, byRef: $3, name: $4, params: $6, stmts: $8]; }
;
method_body:
';' /* abstract method */ { $$ = null; }
| '{' inner_statement_list '}' { $$ = $2; }
;
variable_modifiers:
non_empty_member_modifiers { $$ = $1; }
| T_VAR { $$ = ClassStmt::MODIFIER_PUBLIC; }
;
method_modifiers:
/* empty */ { $$ = ClassStmt::MODIFIER_PUBLIC; }
| non_empty_member_modifiers { $$ = $1; }
;
non_empty_member_modifiers:
member_modifier { $$ = $1; }
| non_empty_member_modifiers member_modifier { ClassStmt::verifyModifier($1, $2); $$ = $1 | $2; }
;
member_modifier:
T_PUBLIC { $$ = ClassStmt::MODIFIER_PUBLIC; }
| T_PROTECTED { $$ = ClassStmt::MODIFIER_PROTECTED; }
| T_PRIVATE { $$ = ClassStmt::MODIFIER_PRIVATE; }
| T_STATIC { $$ = ClassStmt::MODIFIER_STATIC; }
| T_ABSTRACT { $$ = ClassStmt::MODIFIER_ABSTRACT; }
| T_FINAL { $$ = ClassStmt::MODIFIER_FINAL; }
;
class_variable_declaration:
class_variable_declaration ',' T_VARIABLE
{ push($1,PropertyStmtProperty[name: parseVar($3), default: null]); }
| class_variable_declaration ',' T_VARIABLE '=' static_scalar
{ push($1, PropertyStmtProperty[name: parseVar($3), default: $5]); }
| T_VARIABLE
{ init(PropertyStmtProperty[name: parseVar($1), default: null]); }
| T_VARIABLE '=' static_scalar
{ init(PropertyStmtProperty[name: parseVar($1), default: $3]); }
;
class_constant_declaration:
class_constant_declaration ',' T_STRING '=' static_scalar
{ push($1, ClassConstStmtConst[name: $3, value: $5]); }
| T_CONST T_STRING '=' static_scalar { init(ClassConstStmtConst[name: $2, value: $4]); }
;
expr_list:
expr_list ',' expr { push($1, $3); }
| expr { init($1); }
;
for_expr:
/* empty */ { $$ = array(); }
| expr_list { $$ = $1; }
;
expr_without_variable:
T_LIST '(' assignment_list ')' '=' expr { $$ = ListExpr[assignList: $3, expr: $6]; }
| variable '=' expr { $$ = AssignExpr[var: $1, expr: $3]; }
| variable '=' '&' variable { $$ = AssignRefExpr[var: $1, refVar: $4]; }
| variable '=' '&' T_NEW class_name_reference ctor_arguments
{ $$ = AssignExpr[var: $1, expr: NewExpr[class: $5, args: $6]]; }
/* reference dropped intentially, TODO: Throw error? */
| T_NEW class_name_reference ctor_arguments { $$ = NewExpr[class: $2, args: $3]; }
| T_CLONE expr { $$ = CloneExpr[expr: $2]; }
| variable T_PLUS_EQUAL expr { $$ = AssignPlusExpr[var: $1, expr: $3]; }
| variable T_MINUS_EQUAL expr { $$ = AssignMinusExpr[var: $1, expr: $3]; }
| variable T_MUL_EQUAL expr { $$ = AssignMulExpr[var: $1, expr: $3]; }
| variable T_DIV_EQUAL expr { $$ = AssignDivExpr[var: $1, expr: $3]; }
| variable T_CONCAT_EQUAL expr { $$ = AssignConcatExpr[var: $1, expr: $3]; }
| variable T_MOD_EQUAL expr { $$ = AssignModExpr[var: $1, expr: $3]; }
| variable T_AND_EQUAL expr { $$ = AssignBinAndExpr[var: $1, expr: $3]; }
| variable T_OR_EQUAL expr { $$ = AssignBinOrExpr[var: $1, expr: $3]; }
| variable T_XOR_EQUAL expr { $$ = AssignBinXorExpr[var: $1, expr: $3]; }
| variable T_SL_EQUAL expr { $$ = AssignShiftLeftExpr[var: $1, expr: $3]; }
| variable T_SR_EQUAL expr { $$ = AssignShiftRightExpr[var: $1, expr: $3]; }
| variable T_INC { $$ = PostIncExpr[var: $1]; }
| T_INC variable { $$ = PreIncExpr[var: $2]; }
| variable T_DEC { $$ = PostDecExpr[var: $1]; }
| T_DEC variable { $$ = PreDecExpr[var: $2]; }
| expr T_BOOLEAN_OR expr { $$ = BooleanOrExpr[left: $1, right: $3]; }
| expr T_BOOLEAN_AND expr { $$ = BooleanAndExpr[left: $1, right: $3]; }
| expr T_LOGICAL_OR expr { $$ = LogicalOrExpr[left: $1, right: $3]; }
| expr T_LOGICAL_AND expr { $$ = LogicalAndExpr[left: $1, right: $3]; }
| expr T_LOGICAL_XOR expr { $$ = LogicalXorExpr[left: $1, right: $3]; }
| expr '|' expr { $$ = BinaryOrExpr[left: $1, right: $3]; }
| expr '&' expr { $$ = BinaryAndExpr[left: $1, right: $3]; }
| expr '^' expr { $$ = BinaryXorExpr[left: $1, right: $3]; }
| expr '.' expr { $$ = ConcatExpr[left: $1, right: $3]; }
| expr '+' expr { $$ = PlusExpr[left: $1, right: $3]; }
| expr '-' expr { $$ = MinusExpr[left: $1, right: $3]; }
| expr '*' expr { $$ = MulExpr[left: $1, right: $3]; }
| expr '/' expr { $$ = DivExpr[left: $1, right: $3]; }
| expr '%' expr { $$ = ModExpr[left: $1, right: $3]; }
| expr T_SL expr { $$ = ShiftLeftExpr[left: $1, right: $3]; }
| expr T_SR expr { $$ = ShiftRightExpr[left: $1, right: $3]; }
| '+' expr %prec T_INC { $$ = UnaryPlusExpr[expr: $2]; }
| '-' expr %prec T_INC { $$ = UnaryMinusExpr[expr: $2]; }
| '!' expr { $$ = BooleanNotExpr[expr: $2]; }
| '~' expr { $$ = BinaryNotExpr[expr: $2]; }
| expr T_IS_IDENTICAL expr { $$ = IdenticalExpr[left: $1, right: $3]; }
| expr T_IS_NOT_IDENTICAL expr { $$ = NotIdenticalExpr[left: $1, right: $3]; }
| expr T_IS_EQUAL expr { $$ = EqualExpr[left: $1, right: $3]; }
| expr T_IS_NOT_EQUAL expr { $$ = NotEqualExpr[left: $1, right: $3]; }
| expr '<' expr { $$ = SmallerExpr[left: $1, right: $3]; }
| expr T_IS_SMALLER_OR_EQUAL expr { $$ = SmallerOrEqualExpr[left: $1, right: $3]; }
| expr '>' expr { $$ = GreaterExpr[left: $1, right: $3]; }
| expr T_IS_GREATER_OR_EQUAL expr { $$ = GreaterOrEqualExpr[left: $1, right: $3]; }
| expr T_INSTANCEOF class_name_reference { $$ = InstanceOfExpr[expr: $1, class: $3]; }
| '(' expr ')' { $$ = $2; }
| expr '?' expr ':' expr { $$ = TernaryExpr[cond: $1, if: $3, else: $5]; }
| expr '?' ':' expr { $$ = TernaryExpr[cond: $1, if: null, else: $4]; }
| T_ISSET '(' variables_list ')' { $$ = IssetExpr[vars: $3]; }
| T_EMPTY '(' variable ')' { $$ = EmptyExpr[var: $3]; }
| T_INCLUDE expr { $$ = IncludeExpr[expr: $2, type: IncludeExpr::TYPE_INCLUDE]; }
| T_INCLUDE_ONCE expr { $$ = IncludeExpr[expr: $2, type: IncludeExpr::TYPE_INCLUDE_ONCE]; }
| T_EVAL '(' expr ')' { $$ = EvalExpr[expr: $3]; }
| T_REQUIRE expr { $$ = IncludeExpr[expr: $2, type: IncludeExpr::TYPE_REQUIRE]; }
| T_REQUIRE_ONCE expr { $$ = IncludeExpr[expr: $2, type: IncludeExpr::TYPE_REQUIRE_ONCE]; }
| T_INT_CAST expr { $$ = IntCastExpr[expr: $2]; }
| T_DOUBLE_CAST expr { $$ = DoubleCastExpr[expr: $2]; }
| T_STRING_CAST expr { $$ = StringCastExpr[expr: $2]; }
| T_ARRAY_CAST expr { $$ = ArrayCastExpr[expr: $2]; }
| T_OBJECT_CAST expr { $$ = ObjectCastExpr[expr: $2]; }
| T_BOOL_CAST expr { $$ = BoolCastExpr[expr: $2]; }
| T_UNSET_CAST expr { $$ = UnsetCastExpr[expr: $2]; }
| T_EXIT exit_expr { $$ = ExitExpr[expr: $2]; }
| '@' expr { $$ = ErrorSupressExpr[expr: $2]; }
| scalar { $$ = $1; }
| T_ARRAY '(' array_pair_list ')' { $$ = ArrayExpr[items: $3]; }
| '`' backticks_expr '`' { $$ = ShellExecExpr[expr: $2]; }
| T_PRINT expr { $$ = PrintExpr[expr: $2]; }
| T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
{ $$ = LambdaFuncExpr[byRef: $2, params: $4, useVars: $6, stmts: $8]; }
;
lexical_vars:
/* empty */ { $$ = array(); }
| T_USE '(' lexical_var_list ')' { $$ = $3; }
;
lexical_var_list:
lexical_var_list ',' optional_ref T_VARIABLE
{ push($1, LambdaFuncExprUse[var: parseVar($4), byRef: $3]); }
| optional_ref T_VARIABLE
{ init(LambdaFuncExprUse[var: parseVar($2), byRef: $1]); }
;
function_call:
name '(' function_call_parameter_list ')' { $$ = FuncCallExpr[func: $1, args: $3]; }
| class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' function_call_parameter_list ')'
{ $$ = StaticCallExpr[class: $1, func: $3, args: $5]; }
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects '(' function_call_parameter_list ')'
{ $$ = StaticCallExpr[class: $1, func: $3, args: $5]; }
| reference_variable T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' function_call_parameter_list ')'
{ $$ = StaticCallExpr[class: $1, func: $3, args: $5]; }
| reference_variable T_PAAMAYIM_NEKUDOTAYIM variable_without_objects '(' function_call_parameter_list ')'
{ $$ = StaticCallExpr[class: $1, func: $3, args: $5]; }
| variable_without_objects '(' function_call_parameter_list ')'
{ $$ = FuncCallExpr[func: $1, args: $3]; }
;
class_name:
T_STATIC { $$ = 'static'; }
| name { $$ = $1; }
;
name:
namespace_name { $$ = $1; }
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $3->resolveType(Name::RELATIVE); $$ = $3; }
| T_NS_SEPARATOR namespace_name { $2->resolveType(Name::ABSOLUTE); $$ = $2; }
;
class_name_reference:
class_name { $$ = $1; }
| dynamic_class_name_reference { $$ = $1; }
;
dynamic_class_name_reference:
dynamic_class_name_reference T_OBJECT_OPERATOR object_property
{ $$ = PropertyFetchExpr[var: $1, name: $3]; }
| base_variable { $$ = $1; }
;
exit_expr:
/* empty */ { $$ = null; }
| '(' ')' { $$ = null; }
| '(' expr ')' { $$ = $2; }
;
backticks_expr:
/* empty */ { $$ = null; }
| T_ENCAPSED_AND_WHITESPACE { $$ = parseEncapsed($1); }
| encaps_list { $$ = $1; }
;
ctor_arguments:
/* empty */ { $$ = array(); }
| '(' function_call_parameter_list ')' { $$ = $2; }
;
common_scalar:
T_LNUMBER { $$ = LNumberScalar[value: parseLNumber($1)]; }
| T_DNUMBER { $$ = DNumberScalar[value: parseDNumber($1)]; }
| T_CONSTANT_ENCAPSED_STRING { $$ = StringScalar[value: parseString($1)]; }
| T_LINE { $$ = LineConstScalar[]; }
| T_FILE { $$ = FileConstScalar[]; }
| T_DIR { $$ = DirConstScalar[]; }
| T_CLASS_C { $$ = ClassConstScalar[]; }
| T_METHOD_C { $$ = MethodConstScalar[]; }
| T_FUNC_C { $$ = FuncConstScalar[]; }
| T_NS_C { $$ = NSConstScalar[]; }
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
{ $$ = StringScalar[value: parseEncapsed($2)]; }
| T_START_HEREDOC T_END_HEREDOC { $$ = StringScalar[value: '']; }
;
static_scalar: /* compile-time evaluated scalars */
common_scalar { $$ = $1; }
| name { $$ = ConstFetchExpr[name: $1]; }
| '+' static_scalar { $$ = UnaryPlusExpr[expr: $2]; }
| '-' static_scalar { $$ = UnaryMinusExpr[expr: $2]; }
| T_ARRAY '(' static_array_pair_list ')' { $$ = ArrayExpr[items: $3]; }
| class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = ClassConstFetchExpr[class: $1, name: $3]; }
;
scalar:
T_STRING_VARNAME { $$ = StringScalar[value: $1]; }
| class_constant { $$ = $1; }
| name { $$ = ConstFetchExpr[name: $1]; }
| common_scalar { $$ = $1; }
| '"' encaps_list '"' { $$ = EncapsedString[parts: $2]; }
| T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = EncapsedString[parts: $2]; }
;
static_array_pair_list:
/* empty */ { $$ = array(); }
| non_empty_static_array_pair_list optional_comma { $$ = $1; }
;
optional_comma:
/* empty */
| ','
;
non_empty_static_array_pair_list:
non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW static_scalar
{ push($1, ArrayExprItem[key: $3, value: $5, byRef: false]); }
| non_empty_static_array_pair_list ',' static_scalar { push($1, ArrayExprItem[key: null, value: $3, byRef: false]); }
| static_scalar T_DOUBLE_ARROW static_scalar { init(ArrayExprItem[key: $1, value: $3, byRef: false]); }
| static_scalar { init(ArrayExprItem[key: null, value: $1, byRef: false]); }
;
expr:
variable { $$ = $1; }
| expr_without_variable { $$ = $1; }
;
variable:
variable T_OBJECT_OPERATOR object_property '(' function_call_parameter_list ')'
{ $$ = MethodCallExpr[var: $1, name: $3, args: $5]; }
| variable T_OBJECT_OPERATOR object_property
{ $$ = PropertyFetchExpr[var: $1, name: $3]; }
| base_variable { $$ = $1; }
| function_call { $$ = $1; }
;
variable_without_objects:
reference_variable { $$ = $1; }
| '$' reference_variable { $$ = Variable[name: $2]; }
;
base_variable:
variable_without_objects { $$ = $1; }
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects
{ $$ = StaticPropertyFetchExpr[class: $1, name: $3]; }
| reference_variable T_PAAMAYIM_NEKUDOTAYIM variable_without_objects
{ $$ = StaticPropertyFetchExpr[class: $1, name: $3]; }
;
reference_variable:
reference_variable '[' dim_offset ']' { $$ = ArrayDimFetchExpr[var: $1, dim: $3]; }
| reference_variable '{' expr '}' { $$ = ArrayDimFetchExpr[var: $1, dim: $3]; }
| T_VARIABLE { $$ = Variable[name: parseVar($1)]; }
| '$' '{' expr '}' { $$ = Variable[name: $3]; }
;
dim_offset:
/* empty */ { $$ = null; }
| expr { $$ = $1; }
;
object_property:
object_dim_list { $$ = $1; }
| variable_without_objects { $$ = $1; }
;
object_dim_list:
object_dim_list '[' dim_offset ']' { $$ = ArrayDimFetchExpr[var: $1, dim: $3]; }
| object_dim_list '{' expr '}' { $$ = ArrayDimFetchExpr[var: $1, dim: $3]; }
| T_STRING { $$ = $1; }
| '{' expr '}' { $$ = $2; }
;
assignment_list:
assignment_list ',' assignment_list_element { push($1, $3); }
| assignment_list_element { init($1); }
;
assignment_list_element:
variable { $$ = $1; }
| T_LIST '(' assignment_list ')' { $$ = $3; }
| /* empty */ { $$ = null; }
;
array_pair_list:
/* empty */ { $$ = array(); }
| non_empty_array_pair_list optional_comma { $$ = $1; }
;
non_empty_array_pair_list:
non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
{ push($1, ArrayExprItem[key: $3, value: $5, byRef: false]); }
| non_empty_array_pair_list ',' expr { push($1, ArrayExprItem[key: null, value: $3, byRef: false]); }
| expr T_DOUBLE_ARROW expr { init(ArrayExprItem[key: $1, value: $3, byRef: false]); }
| expr { init(ArrayExprItem[key: null, value: $1, byRef: false]); }
| non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' variable
{ push($1, ArrayExprItem[key: $3, value: $6, byRef: true]); }
| non_empty_array_pair_list ',' '&' variable { push($1, ArrayExprItem[key: null, value: $4, byRef: true]); }
| expr T_DOUBLE_ARROW '&' variable { init(ArrayExprItem[key: $1, value: $4, byRef: true]); }
| '&' variable { init(ArrayExprItem[key: null, value: $2, byRef: true]); }
;
encaps_list:
encaps_list encaps_var { push($1, $2); }
| encaps_list T_ENCAPSED_AND_WHITESPACE { push($1, parseEncapsed($2)); }
| encaps_var { init($1); }
| T_ENCAPSED_AND_WHITESPACE encaps_var { init(parseEncapsed($1), $2); }
;
encaps_var:
T_VARIABLE { $$ = Variable[name: parseVar($1)]; }
| T_VARIABLE '[' encaps_var_offset ']' { $$ = ArrayDimFetchExpr[var: Variable[name: parseVar($1)], dim: $3]; }
| T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = PropertyFetchExpr[var: Variable[name: parseVar($1)], name: $3]; }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = Variable[name: $2]; }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
{ $$ = ArrayDimFetchExpr[var: Variable[name: $2], dim: $4]; }
| T_CURLY_OPEN variable '}' { $$ = $2; }
;
encaps_var_offset:
T_STRING { $$ = StringScalar[value: $1]; }
| T_NUM_STRING { $$ = LNumberScalar[value: parseLNumber($1)]; }
| T_VARIABLE { $$ = Variable[name: parseVar($1)]; }
;
class_constant:
class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = ClassConstFetchExpr[class: $1, name: $3]; }
| reference_variable T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = ClassConstFetchExpr[class: $1, name: $3]; }
;
%%

File diff suppressed because it is too large Load Diff

73
lib/Lexer.php Normal file
View File

@ -0,0 +1,73 @@
<?php
class Lexer
{
protected $tokens;
protected $pos;
private static $tokenMap;
private static $dropTokens = array(
T_WHITESPACE => 1, T_COMMENT => 1, T_DOC_COMMENT => 1, T_OPEN_TAG => 1
);
public function __construct($code) {
self::initTokenMap();
$this->tokens = token_get_all($code);
$this->pos = -1;
}
public function yylex(&$yyLVal) {
while (isset($this->tokens[++$this->pos])) {
$token = $this->tokens[$this->pos];
if (is_string($token)) {
$yyLVal = $token;
return ord($token);
} elseif (!isset(self::$dropTokens[$token[0]])) {
$yyLVal = $token[1];
return self::$tokenMap[$token[0]];
}
}
return 0;
}
/**
* Returns the line the current token is in.
*
* @return int
*/
public function getLine() {
for ($i = $this->pos; $i > 0; --$i) {
if (is_array($this->tokens[$this->pos])) {
return $this->tokens[$this->pos][2];
}
}
return -1;
}
private static function initTokenMap() {
if (!self::$tokenMap) {
self::$tokenMap = array();
// 256 is the minimum possible token number, as everything below
// it is an ASCII value
for ($i = 256; $i < 1000; ++$i) {
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
if (T_DOUBLE_COLON === $i) {
self::$tokenMap[$i] = Parser::T_PAAMAYIM_NEKUDOTAYIM;
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
} elseif(T_OPEN_TAG_WITH_ECHO === $i) {
self::$tokenMap[$i] = Parser::T_ECHO;
// T_CLOSE_TAG is equivalent to ';'
} elseif(T_CLOSE_TAG === $i) {
self::$tokenMap[$i] = ord(';');
// and the others can be mapped directly
} elseif ('UNKNOWN' !== ($name = token_name($i))) {
self::$tokenMap[$i] = constant('Parser::' . $name);
}
}
}
}
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ArrayCastExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ArrayDimFetchExpr extends NodeAbstract
{
}

5
lib/Node/ArrayExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ArrayExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ArrayExprItem extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignBinAndExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignBinOrExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignBinXorExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignConcatExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignDivExpr extends NodeAbstract
{
}

5
lib/Node/AssignExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_AssignExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignMinusExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignModExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignMulExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignPlusExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignRefExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignShiftLeftExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_AssignShiftRightExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_BinaryAndExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_BinaryNotExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_BinaryOrExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_BinaryXorExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_BoolCastExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_BooleanAndExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_BooleanNotExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_BooleanOrExpr extends NodeAbstract
{
}

5
lib/Node/BreakStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_BreakStmt extends NodeAbstract
{
}

5
lib/Node/CaseStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_CaseStmt extends NodeAbstract
{
}

5
lib/Node/CatchStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_CatchStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ClassConstFetchExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ClassConstScalar extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ClassConstStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ClassConstStmtConst extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ClassMethodStmt extends NodeAbstract
{
}

15
lib/Node/ClassStmt.php Normal file
View File

@ -0,0 +1,15 @@
<?php
class Node_ClassStmt extends NodeAbstract
{
const MODIFIER_PUBLIC = 1;
const MODIFIER_PROTECTED = 2;
const MODIFIER_PRIVATE = 4;
const MODIFIER_STATIC = 8;
const MODIFIER_ABSTRACT = 16;
const MODIFIER_FINAL = 32;
public static function verifyModifier($a, $b) {
// TODO
}
}

5
lib/Node/CloneExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_CloneExpr extends NodeAbstract
{
}

5
lib/Node/ConcatExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ConcatExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ConstFetchExpr extends NodeAbstract
{
}

5
lib/Node/ConstScalar.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ConstScalar extends NodeAbstract
{
}

5
lib/Node/ConstStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ConstStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ConstStmtConst extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ContinueStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_DNumberScalar extends NodeAbstract
{
}

5
lib/Node/DeclareStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_DeclareStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_DeclareStmtDeclare extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_DirConstScalar extends NodeAbstract
{
}

5
lib/Node/DivExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_DivExpr extends NodeAbstract
{
}

5
lib/Node/DoStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_DoStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_DoubleCastExpr extends NodeAbstract
{
}

5
lib/Node/EchoStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_EchoStmt extends NodeAbstract
{
}

5
lib/Node/ElseIfStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ElseIfStmt extends NodeAbstract
{
}

5
lib/Node/ElseStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ElseStmt extends NodeAbstract
{
}

5
lib/Node/EmptyExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_EmptyExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_EncapsedString extends NodeAbstract
{
}

5
lib/Node/EqualExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_EqualExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_ErrorSupressExpr extends NodeAbstract
{
}

5
lib/Node/EvalExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_EvalExpr extends NodeAbstract
{
}

5
lib/Node/ExitExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ExitExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_FileConstScalar extends NodeAbstract
{
}

5
lib/Node/ForStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ForStmt extends NodeAbstract
{
}

5
lib/Node/ForeachStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ForeachStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_FuncCallExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_FuncCallStmtParam extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_FuncConstScalar extends NodeAbstract
{
}

5
lib/Node/FuncStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_FuncStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_FuncStmtParam extends NodeAbstract
{
}

5
lib/Node/GlobalStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_GlobalStmt extends NodeAbstract
{
}

5
lib/Node/GotoStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_GotoStmt extends NodeAbstract
{
}

5
lib/Node/GreaterExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_GreaterExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_GreaterOrEqualExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_IdenticalExpr extends NodeAbstract
{
}

5
lib/Node/IfStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_IfStmt extends NodeAbstract
{
}

9
lib/Node/IncludeExpr.php Normal file
View File

@ -0,0 +1,9 @@
<?php
class Node_IncludeExpr extends NodeAbstract
{
const TYPE_INCLUDE = 1;
const TYPE_INCLUDE_ONCE = 2;
const TYPE_REQUIRE = 3;
const TYPE_REQUIRE_ONCE = 4;
}

View File

@ -0,0 +1,5 @@
<?php
class Node_InlineHTMLStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_InstanceOfExpr extends NodeAbstract
{
}

5
lib/Node/IntCastExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_IntCastExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_InterfaceStmt extends NodeAbstract
{
}

5
lib/Node/IssetExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_IssetExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_LNumberScalar extends NodeAbstract
{
}

5
lib/Node/LabelStmt.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_LabelStmt extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_LambdaFuncExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_LambdaFuncExprUse extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_LineConstScalar extends NodeAbstract
{
}

5
lib/Node/ListExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ListExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_LogicalAndExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_LogicalOrExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_LogicalXorExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_MethodCallExpr extends NodeAbstract
{
}

View File

@ -0,0 +1,5 @@
<?php
class Node_MethodConstScalar extends NodeAbstract
{
}

5
lib/Node/MinusExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_MinusExpr extends NodeAbstract
{
}

5
lib/Node/ModExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_ModExpr extends NodeAbstract
{
}

5
lib/Node/MulExpr.php Normal file
View File

@ -0,0 +1,5 @@
<?php
class Node_MulExpr extends NodeAbstract
{
}

Some files were not shown because too many files have changed in this diff Show More