Skip to content

Commit

Permalink
Fixed #95: PHP 5.4 array syntax is not supported in property init
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelpichler committed Oct 11, 2012
1 parent 95e057b commit f6ee217
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 124 deletions.
2 changes: 1 addition & 1 deletion setup
Submodule setup updated from 64bc46 to 126806
154 changes: 52 additions & 102 deletions src/main/php/PHP/Depend/Parser.php
Expand Up @@ -429,13 +429,22 @@ protected function reset($modifiers = 0)
$this->modifiers = $modifiers;
}

/**
* Tests if the given token type is a reserved keyword in the supported PHP
* version.
*
* @param $tokenType
* @return boolean
* @since 1.1.1
*/
abstract protected function isKeyword($tokenType);

/**
* Will return <b>true</b> if the given <b>$tokenType</b> is a valid class
* name part.
*
* @param integer $tokenType The type of a parsed token.
*
* @return string
* @return boolean
* @since 0.10.6
*/
protected abstract function isClassName($tokenType);
Expand Down Expand Up @@ -4818,13 +4827,14 @@ protected abstract function parseIntegerNumber();
* @return PHP_Depend_Code_ASTArray
* @since 1.0.0
*/
private function doParseArray()
private function doParseArray($static = false)
{
$this->tokenStack->push();

return $this->setNodePositionsAndReturn(
$this->parseArray(
$this->builder->buildAstArray()
$this->builder->buildAstArray(),
$static
)
);
}
Expand All @@ -4841,29 +4851,32 @@ protected abstract function isArrayStartDelimiter();
/**
* Parses a php array declaration.
*
* @param PHP_Depend_Code_ASTArray $array The context array node.
* @param PHP_Depend_Code_ASTArray $array
* @param boolean $static
*
* @return PHP_Depend_Code_ASTArray
* @since 1.0.0
*/
protected abstract function parseArray(PHP_Depend_Code_ASTArray $array);
protected abstract function parseArray(PHP_Depend_Code_ASTArray $array, $static = false);

/**
* Parses all elements in an array.
*
* @param PHP_Depend_Code_ASTArray $array The context array node.
* @param integer $endDelimiter The version specific delimiter.
* @param PHP_Depend_Code_ASTArray $array
* @param integer $endDelimiter
* @param boolean $static
*
* @return PHP_Depend_Code_ASTArray
* @since 1.0.0
*/
protected function parseArrayElements(
PHP_Depend_Code_ASTArray $array,
$endDelimiter
$endDelimiter,
$static = false
) {
$this->consumeComments();
while ($endDelimiter !== $this->tokenizer->peek()) {
$array->addChild($this->parseArrayElement());
$array->addChild($this->parseArrayElement($static));

$this->consumeComments();
if (self::T_COMMA === $this->tokenizer->peek()) {
Expand All @@ -4880,19 +4893,39 @@ protected function parseArrayElements(
* An array element can have a simple value, a key/value pair, a value by
* reference or a key/value pair with a referenced value.
*
* @param boolean $static
* @return PHP_Depend_Code_ASTArrayElement
* @since 1.0.0
*/
protected function parseArrayElement()
protected function parseArrayElement($static = false)
{
$this->consumeComments();

$this->tokenStack->push();

$element = $this->builder->buildAstArrayElement();
if ($this->parseOptionalByReference()) {

if ($static) {
$tokens = $this->tokenStack->pop();

throw new PHP_Depend_Parser_UnexpectedTokenException(
end($tokens),
$this->sourceFile->getFileName()
);
}

$element->setByReference();
}

$this->consumeComments();
if ($this->isKeyword($this->tokenizer->peek())) {
throw new PHP_Depend_Parser_UnexpectedTokenException(
$this->tokenizer->next(),
$this->sourceFile->getFileName()
);
}

$element->addChild($this->parseExpression());

$this->consumeComments();
Expand Down Expand Up @@ -6182,8 +6215,14 @@ private function parseVariableDeclarator()
private function parseStaticValueOrStaticArray()
{
$this->consumeComments();
if ($this->tokenizer->peek() === self::T_ARRAY) {
return $this->parseStaticArray();
if ($this->isArrayStartDelimiter()) {
// TODO: Use default value as value!
$defaultValue = $this->doParseArray(true);

$value = new PHP_Depend_Code_Value();
$value->setValue(array());

return $value;
}
return $this->parseStaticValue();
}
Expand Down Expand Up @@ -6304,95 +6343,6 @@ private function parseStaticValue()
throw new PHP_Depend_Parser_TokenStreamEndException($this->tokenizer);
}

/**
* This method parses an array as it is used for for parameter or property
* default values.
*
* Note: At the moment the implementation of this method only returns an
* empty array, but consumes all tokens that belong to the array
* declaration.
*
* TODO: Implement array content/value handling, but how should we handle
* constant values like array(self::FOO, FOOBAR)?
*
* @return array
* @since 0.9.5
*/
private function parseStaticArray()
{
$staticValue = array();

// Fetch all tokens that belong to this array
$this->consumeToken(self::T_ARRAY);
$this->consumeComments();
$this->consumeToken(self::T_PARENTHESIS_OPEN);

$parenthesis = 1;

$tokenType = $this->tokenizer->peek();
while ($tokenType !== self::T_EOF) {

switch ($tokenType) {

case self::T_PARENTHESIS_CLOSE:
if (--$parenthesis === 0) {
break 2;
}
$this->consumeToken(self::T_PARENTHESIS_CLOSE);
break;

case self::T_PARENTHESIS_OPEN:
$this->consumeToken(self::T_PARENTHESIS_OPEN);
++$parenthesis;
break;

case self::T_DIR:
case self::T_NULL:
case self::T_TRUE:
case self::T_FILE:
case self::T_LINE:
case self::T_NS_C:
case self::T_PLUS:
case self::T_SELF:
case self::T_ARRAY:
case self::T_FALSE:
case self::T_EQUAL:
case self::T_COMMA:
case self::T_MINUS:
case self::T_COMMENT:
case self::T_DOC_COMMENT:
case self::T_DOUBLE_COLON:
case self::T_STRING:
case self::T_BACKSLASH:
case self::T_DNUMBER:
case self::T_LNUMBER:
case self::T_FUNC_C:
case self::T_CLASS_C:
case self::T_METHOD_C:
case self::T_STATIC:
case self::T_PARENT:
case self::T_NUM_STRING:
case self::T_DOUBLE_ARROW:
case self::T_CONSTANT_ENCAPSED_STRING:
$this->consumeToken($tokenType);
break;

default:
break 2;
}

$tokenType = $this->tokenizer->peek();
}

// Read closing parenthesis
$this->consumeToken(self::T_PARENTHESIS_CLOSE);

$defaultValue = new PHP_Depend_Code_Value();
$defaultValue->setValue($staticValue);

return $defaultValue;
}

/**
* Checks if the given expression is a read/write variable as defined in
* the PHP zend_language_parser.y definition.
Expand Down
57 changes: 37 additions & 20 deletions src/main/php/PHP/Depend/Parser/VersionAllParser.php
Expand Up @@ -64,6 +64,24 @@
*/
class PHP_Depend_Parser_VersionAllParser extends PHP_Depend_Parser
{
/**
* Tests if the given token type is a reserved keyword in the supported PHP
* version.
*
* @param $tokenType
* @return boolean
* @since 1.1.1
*/
protected function isKeyword($tokenType)
{
switch ($tokenType) {
case self::T_CLASS:
case self::T_INTERFACE:
case self::T_FUNCTION:
return true;
}
return false;
}

/**
* Will return <b>true</b> if the given <b>$tokenType</b> is a valid class
Expand All @@ -77,21 +95,20 @@ class PHP_Depend_Parser_VersionAllParser extends PHP_Depend_Parser
protected function isClassName($tokenType)
{
switch ($tokenType) {

case self::T_DIR:
case self::T_USE:
case self::T_GOTO:
case self::T_NULL:
case self::T_NS_C:
case self::T_TRUE:
case self::T_CLONE:
case self::T_FALSE:
case self::T_TRAIT:
case self::T_STRING:
case self::T_TRAIT_C:
case self::T_INSTEADOF:
case self::T_NAMESPACE:
return true;
case self::T_DIR:
case self::T_USE:
case self::T_GOTO:
case self::T_NULL:
case self::T_NS_C:
case self::T_TRUE:
case self::T_CLONE:
case self::T_FALSE:
case self::T_TRAIT:
case self::T_STRING:
case self::T_TRAIT_C:
case self::T_INSTEADOF:
case self::T_NAMESPACE:
return true;
}
return false;
}
Expand Down Expand Up @@ -324,26 +341,26 @@ protected function isArrayStartDelimiter()
/**
* Parses a php array declaration.
*
* @param PHP_Depend_Code_ASTArray $array The context array node.
*
* @param PHP_Depend_Code_ASTArray $array
* @param boolean $static
* @return PHP_Depend_Code_ASTArray
* @since 1.0.0
*/
protected function parseArray(PHP_Depend_Code_ASTArray $array)
protected function parseArray(PHP_Depend_Code_ASTArray $array, $static = false)
{
switch ($this->tokenizer->peek()) {

case self::T_ARRAY:
$this->consumeToken(self::T_ARRAY);
$this->consumeComments();
$this->consumeToken(self::T_PARENTHESIS_OPEN);
$this->parseArrayElements($array, self::T_PARENTHESIS_CLOSE);
$this->parseArrayElements($array, self::T_PARENTHESIS_CLOSE, $static);
$this->consumeToken(self::T_PARENTHESIS_CLOSE);
break;

default:
$this->consumeToken(self::T_SQUARED_BRACKET_OPEN);
$this->parseArrayElements($array, self::T_SQUARED_BRACKET_CLOSE);
$this->parseArrayElements($array, self::T_SQUARED_BRACKET_CLOSE, $static);
$this->consumeToken(self::T_SQUARED_BRACKET_CLOSE);
break;
}
Expand Down
9 changes: 9 additions & 0 deletions src/site/docx/changes.xml
Expand Up @@ -9,6 +9,15 @@
</properties>

<body>
<release version="1.1.1"
date=""
description="">

<action date="2b23ff9" dev="mapi" type="fix" due-to="Karol" issue="95" system="github">
PHP 5.4 array syntax is not supported in property initialization.
</action>
</release>

<release version="1.1.0"
date="2012/09/12"
description="This release closes a critical issue in the context
Expand Down

0 comments on commit f6ee217

Please sign in to comment.