123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- <?php
-
-
- namespace Doctrine\Common\Annotations;
-
- use Closure;
- use ReflectionClass;
-
-
- final class DocParser
- {
-
-
- private static $classIdentifiers = array(DocLexer::T_IDENTIFIER, DocLexer::T_TRUE, DocLexer::T_FALSE, DocLexer::T_NULL);
-
-
-
- private $lexer;
-
-
-
- private $isNestedAnnotation = false;
-
-
-
- private $imports = array();
-
-
-
- private $classExists = array();
-
-
-
- private $isAnnotation = array();
-
-
-
- private $ignoreNotImportedAnnotations = false;
-
-
-
- private $ignoredAnnotationNames = array();
-
-
-
- private $namespaceAliases = array();
-
-
-
- private $context = '';
-
-
-
- private $creationFn = null;
-
-
-
- public function __construct()
- {
- $this->lexer = new DocLexer;
- }
-
-
-
- public function setIgnoredAnnotationNames(array $names)
- {
- $this->ignoredAnnotationNames = $names;
- }
-
-
-
- public function setAnnotationCreationFunction(\Closure $func)
- {
- $this->creationFn = $func;
- }
-
- public function setImports(array $imports)
- {
- $this->imports = $imports;
- }
-
- public function setIgnoreNotImportedAnnotations($bool)
- {
- $this->ignoreNotImportedAnnotations = (Boolean) $bool;
- }
-
- public function setAnnotationNamespaceAlias($namespace, $alias)
- {
- $this->namespaceAliases[$alias] = $namespace;
- }
-
-
-
- public function parse($input, $context = '')
- {
- if (false === $pos = strpos($input, '@')) {
- return array();
- }
-
-
- if ($pos > 0) {
- $pos -= 1;
- }
-
- $this->context = $context;
- $this->lexer->setInput(trim(substr($input, $pos), '* /'));
- $this->lexer->moveNext();
-
- return $this->Annotations();
- }
-
-
-
- private function match($token)
- {
- if ( ! $this->lexer->isNextToken($token) ) {
- $this->syntaxError($this->lexer->getLiteral($token));
- }
-
- return $this->lexer->moveNext();
- }
-
-
-
- private function matchAny(array $tokens)
- {
- if ( ! $this->lexer->isNextTokenAny($tokens)) {
- $this->syntaxError(implode(' or ', array_map(array($this->lexer, 'getLiteral'), $tokens)));
- }
-
- return $this->lexer->moveNext();
- }
-
-
-
- private function syntaxError($expected, $token = null)
- {
- if ($token === null) {
- $token = $this->lexer->lookahead;
- }
-
- $message = "Expected {$expected}, got ";
-
- if ($this->lexer->lookahead === null) {
- $message .= 'end of string';
- } else {
- $message .= "'{$token['value']}' at position {$token['position']}";
- }
-
- if (strlen($this->context)) {
- $message .= ' in ' . $this->context;
- }
-
- $message .= '.';
-
- throw AnnotationException::syntaxError($message);
- }
-
-
-
- private function classExists($fqcn)
- {
- if (isset($this->classExists[$fqcn])) {
- return $this->classExists[$fqcn];
- }
-
-
- if (class_exists($fqcn, false)) {
- return $this->classExists[$fqcn] = true;
- }
-
-
- return $this->classExists[$fqcn] = AnnotationRegistry::loadAnnotationClass($fqcn);
- }
-
-
-
- private function Annotations()
- {
- $annotations = array();
-
- while (null !== $this->lexer->lookahead) {
- if (DocLexer::T_AT !== $this->lexer->lookahead['type']) {
- $this->lexer->moveNext();
- continue;
- }
-
-
- if (null !== $this->lexer->token && $this->lexer->lookahead['position'] === $this->lexer->token['position'] + strlen($this->lexer->token['value'])) {
- $this->lexer->moveNext();
- continue;
- }
-
-
-
- if ((null === $peek = $this->lexer->glimpse())
- || (DocLexer::T_NAMESPACE_SEPARATOR !== $peek['type'] && !in_array($peek['type'], self::$classIdentifiers, true))
- || $peek['position'] !== $this->lexer->lookahead['position'] + 1) {
- $this->lexer->moveNext();
- continue;
- }
-
- $this->isNestedAnnotation = false;
- if (false !== $annot = $this->Annotation()) {
- $annotations[] = $annot;
- }
- }
-
- return $annotations;
- }
-
-
-
- private function Annotation()
- {
- $this->match(DocLexer::T_AT);
-
-
- if ($this->lexer->isNextTokenAny(self::$classIdentifiers)) {
- $this->lexer->moveNext();
- $name = $this->lexer->token['value'];
- } else if ($this->lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR)) {
- $name = '';
- } else {
- $this->syntaxError('namespace separator or identifier');
- }
-
- while ($this->lexer->lookahead['position'] === $this->lexer->token['position'] + strlen($this->lexer->token['value']) && $this->lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR)) {
- $this->match(DocLexer::T_NAMESPACE_SEPARATOR);
- $this->matchAny(self::$classIdentifiers);
- $name .= '\\'.$this->lexer->token['value'];
- }
-
- if (strpos($name, ":") !== false) {
- list ($alias, $name) = explode(':', $name);
-
- if ( ! isset($this->namespaceAliases[$alias])) {
- $this->lexer->skipUntil(DocLexer::T_AT);
- return false;
- }
- $name = $this->namespaceAliases[$alias] . $name;
- }
-
-
-
- $originalName = $name;
- if ('\\' !== $name[0]) {
- $alias = (false === $pos = strpos($name, '\\'))? $name : substr($name, 0, $pos);
-
- if (isset($this->imports[$loweredAlias = strtolower($alias)])) {
- if (false !== $pos) {
- $name = $this->imports[$loweredAlias].substr($name, $pos);
- } else {
- $name = $this->imports[$loweredAlias];
- }
- } elseif (isset($this->imports['__DEFAULT__']) && $this->classExists($this->imports['__DEFAULT__'].$name)) {
- $name = $this->imports['__DEFAULT__'].$name;
- } elseif (isset($this->imports['__NAMESPACE__']) && $this->classExists($this->imports['__NAMESPACE__'].'\\'.$name)) {
- $name = $this->imports['__NAMESPACE__'].'\\'.$name;
- } elseif (!$this->classExists($name)) {
- if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) {
- return false;
- }
-
- throw AnnotationException::semanticalError(sprintf('The annotation "@%s" in %s was never imported. Did you maybe forget to add a "use" statement for this annotation?', $name, $this->context));
- }
- }
-
- if (!$this->classExists($name)) {
- throw AnnotationException::semanticalError(sprintf('The annotation "@%s" in %s does not exist, or could not be auto-loaded.', $name, $this->context));
- }
-
- if (!$this->isAnnotation($name)) {
- return false;
- }
-
-
-
-
-
-
-
-
-
-
- $this->isNestedAnnotation = true;
-
- $values = array();
- if ($this->lexer->isNextToken(DocLexer::T_OPEN_PARENTHESIS)) {
- $this->match(DocLexer::T_OPEN_PARENTHESIS);
-
- if ( ! $this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) {
- $values = $this->Values();
- }
-
- $this->match(DocLexer::T_CLOSE_PARENTHESIS);
- }
-
- return $this->newAnnotation($name, $values);
- }
-
-
-
- private function isAnnotation($name)
- {
- if (!isset($this->isAnnotation[$name])) {
- if (is_subclass_of($name, 'Doctrine\Common\Annotations\Annotation')) {
- $this->isAnnotation[$name] = true;
- } else {
- $reflClass = new \ReflectionClass($name);
- $this->isAnnotation[$name] = strpos($reflClass->getDocComment(), "@Annotation") !== false;
- }
- }
- return $this->isAnnotation[$name];
- }
-
- private function newAnnotation($name, $values)
- {
- if ($this->creationFn !== null) {
- $fn = $this->creationFn;
- return $fn($name, $values);
- }
-
- return new $name($values);
- }
-
-
-
- private function Values()
- {
- $values = array();
-
-
- if ($this->lexer->isNextToken(DocLexer::T_OPEN_CURLY_BRACES)) {
- $values['value'] = $this->Value();
- return $values;
- }
-
- $values[] = $this->Value();
-
- while ($this->lexer->isNextToken(DocLexer::T_COMMA)) {
- $this->match(DocLexer::T_COMMA);
- $token = $this->lexer->lookahead;
- $value = $this->Value();
-
- if ( ! is_object($value) && ! is_array($value)) {
- $this->syntaxError('Value', $token);
- }
-
- $values[] = $value;
- }
-
- foreach ($values as $k => $value) {
- if (is_object($value) && $value instanceof \stdClass) {
- $values[$value->name] = $value->value;
- } else if ( ! isset($values['value'])){
- $values['value'] = $value;
- } else {
- if ( ! is_array($values['value'])) {
- $values['value'] = array($values['value']);
- }
-
- $values['value'][] = $value;
- }
-
- unset($values[$k]);
- }
-
- return $values;
- }
-
-
-
- private function Value()
- {
- $peek = $this->lexer->glimpse();
-
- if (DocLexer::T_EQUALS === $peek['type']) {
- return $this->FieldAssignment();
- }
-
- return $this->PlainValue();
- }
-
-
-
- private function PlainValue()
- {
- if ($this->lexer->isNextToken(DocLexer::T_OPEN_CURLY_BRACES)) {
- return $this->Arrayx();
- }
-
- if ($this->lexer->isNextToken(DocLexer::T_AT)) {
- return $this->Annotation();
- }
-
- switch ($this->lexer->lookahead['type']) {
- case DocLexer::T_STRING:
- $this->match(DocLexer::T_STRING);
- return $this->lexer->token['value'];
-
- case DocLexer::T_INTEGER:
- $this->match(DocLexer::T_INTEGER);
- return (int)$this->lexer->token['value'];
-
- case DocLexer::T_FLOAT:
- $this->match(DocLexer::T_FLOAT);
- return (float)$this->lexer->token['value'];
-
- case DocLexer::T_TRUE:
- $this->match(DocLexer::T_TRUE);
- return true;
-
- case DocLexer::T_FALSE:
- $this->match(DocLexer::T_FALSE);
- return false;
-
- case DocLexer::T_NULL:
- $this->match(DocLexer::T_NULL);
- return null;
-
- default:
- $this->syntaxError('PlainValue');
- }
- }
-
-
-
- private function FieldAssignment()
- {
- $this->match(DocLexer::T_IDENTIFIER);
- $fieldName = $this->lexer->token['value'];
-
- $this->match(DocLexer::T_EQUALS);
-
- $item = new \stdClass();
- $item->name = $fieldName;
- $item->value = $this->PlainValue();
-
- return $item;
- }
-
-
-
- private function Arrayx()
- {
- $array = $values = array();
-
- $this->match(DocLexer::T_OPEN_CURLY_BRACES);
- $values[] = $this->ArrayEntry();
-
- while ($this->lexer->isNextToken(DocLexer::T_COMMA)) {
- $this->match(DocLexer::T_COMMA);
- $values[] = $this->ArrayEntry();
- }
-
- $this->match(DocLexer::T_CLOSE_CURLY_BRACES);
-
- foreach ($values as $value) {
- list ($key, $val) = $value;
-
- if ($key !== null) {
- $array[$key] = $val;
- } else {
- $array[] = $val;
- }
- }
-
- return $array;
- }
-
-
-
- private function ArrayEntry()
- {
- $peek = $this->lexer->glimpse();
-
- if (DocLexer::T_EQUALS === $peek['type']) {
- $this->match(
- $this->lexer->isNextToken(DocLexer::T_INTEGER) ? DocLexer::T_INTEGER : DocLexer::T_STRING
- );
-
- $key = $this->lexer->token['value'];
- $this->match(DocLexer::T_EQUALS);
-
- return array($key, $this->PlainValue());
- }
-
- return array(null, $this->Value());
- }
- }
|