Adds to nextString() method to ConfigStaticManifest

This is used to get the classname out of the tokens, rather than assuming that
the class name is a single T_STRING.
This commit is contained in:
Simon Welsh 2014-05-05 20:55:59 +10:00
parent f2b2ee8a68
commit 3602ce2db8
2 changed files with 106 additions and 6 deletions

View File

@ -186,7 +186,7 @@ class SS_ConfigStaticManifest_Parser {
* Get the next token to process, incrementing the pointer
*
* @param bool $ignoreWhitespace - if true will skip any whitespace tokens & only return non-whitespace ones
* @return null | int - Either the next token or null if there isn't one
* @return null | mixed - Either the next token or null if there isn't one
*/
protected function next($ignoreWhitespace = true) {
do {
@ -198,6 +198,40 @@ class SS_ConfigStaticManifest_Parser {
return $next;
}
/**
* Get the next set of tokens that form a string to process,
* incrementing the pointer
*
* @param bool $ignoreWhitespace - if true will skip any whitespace tokens
* & only return non-whitespace ones
* @return null|string - Either the next string or null if there isn't one
*/
protected function nextString($ignoreWhitespace = true) {
static $stop = array('{', '}', '(', ')', '[', ']');
$string = '';
while ($this->pos < $this->length) {
$next = $this->tokens[$this->pos];
if (is_string($next)) {
if (!in_array($next, $stop)) {
$string .= $next;
} else {
break;
}
} else if ($next[0] == T_STRING) {
$string .= $next[1];
} else if ($next[0] != T_WHITESPACE || !$ignoreWhitespace) {
break;
}
$this->pos++;
}
if ($string === '') {
return null;
} else {
return $string;
}
}
/**
* Parse the given file to find the static variables declared in it, along with their access & values
*/
@ -208,12 +242,12 @@ class SS_ConfigStaticManifest_Parser {
$type = is_array($token) ? $token[0] : $token;
if($type == T_CLASS) {
$next = $this->next();
if($next[0] != T_STRING) {
$next = $this->nextString();
if($next === null) {
user_error("Couldn\'t parse {$this->path} when building config static manifest", E_USER_ERROR);
}
$class = $next[1];
$class = $next;
}
else if($type == T_NAMESPACE) {
$namespace = '';
@ -227,11 +261,11 @@ class SS_ConfigStaticManifest_Parser {
$next = $this->next();
}
if($next[0] != T_STRING) {
if(!is_string($next) && $next[0] != T_STRING) {
user_error("Couldn\'t parse {$this->path} when building config static manifest", E_USER_ERROR);
}
$namespace .= $next[1];
$namespace .= is_string($next) ? $next : $next[1];
}
}
else if($type == '{' || $type == T_CURLY_OPEN || $type == T_DOLLAR_OPEN_CURLY_BRACES){

View File

@ -200,4 +200,70 @@ DOC;
$this->assertEquals($expectedValue, $statics['config\staticmanifest\NamespaceTest']['db']['value']);
}
public function testParsingMultyStringClass() {
static $tokens = array(
array(T_OPEN_TAG, "<?php\n", 1),
array(T_WHITESPACE, "\n", 2),
array(T_CLASS, 'class', 3),
array(T_WHITESPACE, ' ', 3),
':',
array(T_STRING, 'ss', 3),
':',
array(T_STRING, 'test2', 3),
array(T_WHITESPACE, ' ', 3),
array(T_EXTENDS, 'extends', 3),
array(T_WHITESPACE, ' ', 3),
':',
array(T_STRING, 'ss', 3),
':',
array(T_STRING, 'test', 3),
array(T_WHITESPACE, ' ', 3),
array(T_IMPLEMENTS, 'implements', 3),
array(T_WHITESPACE, ' ', 3),
array(T_STRING, 'TestOnly', 3),
array(T_WHITESPACE, ' ', 3),
'{',
array(T_WHITESPACE, "\n\t", 3),
array(T_PRIVATE, 'private', 4),
array(T_WHITESPACE, ' ', 4),
array(T_STATIC, 'static', 4),
array(T_WHITESPACE, ' ', 4),
array(T_VARIABLE, '$test', 4),
array(T_WHITESPACE, ' ', 4),
'=',
array(T_WHITESPACE, ' ', 4),
array(T_ARRAY, 'array', 4),
'(',
array(T_LNUMBER, '3', 4),
')',
';',
array(T_WHITESPACE, "\n", 4),
'}',
array(T_WHITESPACE, "\n", 5),
);
$parser = new ConfigStaticManifestTest_Parser($tokens);
$parser->parse();
$statics = $parser->getStatics();
$expected = array(
'test' => array(
'access' => T_PRIVATE,
'value' => array(3)
)
);
$this->assertEquals($expected, $statics[':ss:test2']);
}
}
class ConfigStaticManifestTest_Parser extends SS_ConfigStaticManifest_Parser implements TestOnly {
public function __construct($tokens) {
$this->path = __FILE__;
$this->tokens = $tokens;
$this->length = count($this->tokens);
$this->pos = 0;
}
}