/** * Parses the parameter default value. * * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * @return \TokenReflection\ReflectionParameter * @throws \TokenReflection\Exception\ParseException If the default value could not be determined. */ private function parseDefaultValue(Stream $tokenStream) { if ($tokenStream->is('=')) { $tokenStream->skipWhitespaces(true); $level = 0; while (null !== ($type = $tokenStream->getType())) { switch ($type) { case ')': if (0 === $level) { break 2; } case '}': case ']': $level--; break; case '(': case '{': case '[': $level++; break; case ',': if (0 === $level) { break 2; } break; default: break; } $this->defaultValueDefinition[] = $tokenStream->current(); $tokenStream->next(); } if (')' !== $type && ',' !== $type) { throw new Exception\ParseException($this, $tokenStream, 'The property default value is not terminated properly. Expected "," or ")".', Exception\ParseException::UNEXPECTED_TOKEN); } } return $this; }
/** * Saves the end line number. * * @param \TokenReflection\Stream\StreamBase $tokenStream Token susbtream * @return \TokenReflection\ReflectionElement */ private final function parseEndLine(Stream $tokenStream) { $token = $tokenStream->current(); $this->endLine = $token[2]; $this->endPosition = $tokenStream->key(); return $this; }
/** * Parses static variables. * * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * * @return \TokenReflection\ReflectionFunctionBase * @throws \TokenReflection\Exception\ParseException If static variables could not be parsed. */ protected final function parseStaticVariables(Stream $tokenStream) { $type = $tokenStream->getType(); if ('{' === $type) { if ($this->getBroker()->isOptionSet(Broker::OPTION_PARSE_FUNCTION_BODY)) { $tokenStream->skipWhitespaces(true); while ('}' !== ($type = $tokenStream->getType())) { switch ($type) { case T_STATIC: $type = $tokenStream->skipWhitespaces(true)->getType(); if (T_VARIABLE !== $type) { // Late static binding break; } while (T_VARIABLE === $type) { $variableName = $tokenStream->getTokenValue(); $variableDefinition = array(); $type = $tokenStream->skipWhitespaces(true)->getType(); if ('=' === $type) { $type = $tokenStream->skipWhitespaces(true)->getType(); $level = 0; while ($tokenStream->valid()) { switch ($type) { case '(': case '[': case '{': case T_CURLY_OPEN: case T_DOLLAR_OPEN_CURLY_BRACES: $level++; break; case ')': case ']': case '}': $level--; break; case ';': case ',': if (0 === $level) { break 2; } default: break; } $variableDefinition[] = $tokenStream->current(); $type = $tokenStream->skipWhitespaces(true)->getType(); } if (!$tokenStream->valid()) { throw new Exception\ParseException($this, $tokenStream, 'Invalid end of token stream.', Exception\ParseException::READ_BEYOND_EOS); } } $this->staticVariablesDefinition[substr($variableName, 1)] = $variableDefinition; if (',' === $type) { $type = $tokenStream->skipWhitespaces(true)->getType(); } else { break; } } break; case T_FUNCTION: // Anonymous function -> skip to its end if (!$tokenStream->find('{')) { throw new Exception\ParseException($this, $tokenStream, 'Could not find beginning of the anonymous function.', Exception\ParseException::UNEXPECTED_TOKEN); } // Break missing intentionally // Break missing intentionally case '{': case '[': case '(': case T_CURLY_OPEN: case T_DOLLAR_OPEN_CURLY_BRACES: $tokenStream->findMatchingBracket()->skipWhitespaces(true); break; default: $tokenStream->skipWhitespaces(); break; } } } else { $tokenStream->findMatchingBracket(); } } elseif (';' !== $type) { throw new Exception\ParseException($this, $tokenStream, 'Unexpected token found.', Exception\ParseException::UNEXPECTED_TOKEN); } return $this; }
/** * Parses the constant value. * * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * @param \TokenReflection\IReflection $parent Parent reflection object * @return \TokenReflection\ReflectionConstant * @throws \TokenReflection\Exception\ParseException If the constant value could not be determined. */ private function parseValue(Stream $tokenStream, IReflection $parent) { if (!$tokenStream->is('=')) { throw new Exception\ParseException($this, $tokenStream, 'Could not find the definition start.', Exception\ParseException::UNEXPECTED_TOKEN); } $tokenStream->skipWhitespaces(true); static $acceptedTokens = array('-' => true, '+' => true, T_STRING => true, T_NS_SEPARATOR => true, T_CONSTANT_ENCAPSED_STRING => true, T_DNUMBER => true, T_LNUMBER => true, T_DOUBLE_COLON => true, T_CLASS_C => true, T_DIR => true, T_FILE => true, T_FUNC_C => true, T_LINE => true, T_METHOD_C => true, T_NS_C => true, T_TRAIT_C => true); while (null !== ($type = $tokenStream->getType())) { if (T_START_HEREDOC === $type) { $this->valueDefinition[] = $tokenStream->current(); while (null !== $type && T_END_HEREDOC !== $type) { $tokenStream->next(); $this->valueDefinition[] = $tokenStream->current(); $type = $tokenStream->getType(); } $tokenStream->next(); } elseif (isset($acceptedTokens[$type])) { $this->valueDefinition[] = $tokenStream->current(); $tokenStream->next(); } elseif ($tokenStream->isWhitespace(true)) { $tokenStream->skipWhitespaces(true); } else { break; } } if (empty($this->valueDefinition)) { throw new Exception\ParseException($this, $tokenStream, 'Value definition is empty.', Exception\ParseException::LOGICAL_ERROR); } $value = $tokenStream->getTokenValue(); if (null === $type || ',' !== $value && ';' !== $value) { throw new Exception\ParseException($this, $tokenStream, 'Invalid value definition.', Exception\ParseException::LOGICAL_ERROR); } return $this; }