Chris@0: . Chris@0: */ Chris@0: Chris@0: namespace Doctrine\Common\Annotations; Chris@0: Chris@0: /** Chris@0: * Parses a file for namespaces/use/class declarations. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Christian Kaps Chris@0: */ Chris@0: class TokenParser Chris@0: { Chris@0: /** Chris@0: * The token list. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: private $tokens; Chris@0: Chris@0: /** Chris@0: * The number of tokens. Chris@0: * Chris@0: * @var int Chris@0: */ Chris@0: private $numTokens; Chris@0: Chris@0: /** Chris@0: * The current array pointer. Chris@0: * Chris@0: * @var int Chris@0: */ Chris@0: private $pointer = 0; Chris@0: Chris@0: /** Chris@0: * @param string $contents Chris@0: */ Chris@0: public function __construct($contents) Chris@0: { Chris@0: $this->tokens = token_get_all($contents); Chris@0: Chris@0: // The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it Chris@0: // saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored Chris@0: // doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a Chris@0: // docblock. If the first thing in the file is a class without a doc block this would cause calls to Chris@0: // getDocBlock() on said class to return our long lost doc_comment. Argh. Chris@0: // To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least Chris@0: // it's harmless to us. Chris@0: token_get_all("numTokens = count($this->tokens); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the next non whitespace and non comment token. Chris@0: * Chris@0: * @param boolean $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped. Chris@0: * If FALSE then only whitespace and normal comments are skipped. Chris@0: * Chris@0: * @return array|null The token if exists, null otherwise. Chris@0: */ Chris@0: public function next($docCommentIsComment = TRUE) Chris@0: { Chris@0: for ($i = $this->pointer; $i < $this->numTokens; $i++) { Chris@0: $this->pointer++; Chris@0: if ($this->tokens[$i][0] === T_WHITESPACE || Chris@0: $this->tokens[$i][0] === T_COMMENT || Chris@0: ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) { Chris@0: Chris@0: continue; Chris@0: } Chris@0: Chris@0: return $this->tokens[$i]; Chris@0: } Chris@0: Chris@0: return null; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a single use statement. Chris@0: * Chris@0: * @return array A list with all found class names for a use statement. Chris@0: */ Chris@0: public function parseUseStatement() Chris@0: { Chris@12: Chris@12: $groupRoot = ''; Chris@0: $class = ''; Chris@0: $alias = ''; Chris@0: $statements = array(); Chris@0: $explicitAlias = false; Chris@0: while (($token = $this->next())) { Chris@0: $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR; Chris@0: if (!$explicitAlias && $isNameToken) { Chris@0: $class .= $token[1]; Chris@0: $alias = $token[1]; Chris@0: } else if ($explicitAlias && $isNameToken) { Chris@0: $alias .= $token[1]; Chris@0: } else if ($token[0] === T_AS) { Chris@0: $explicitAlias = true; Chris@0: $alias = ''; Chris@0: } else if ($token === ',') { Chris@12: $statements[strtolower($alias)] = $groupRoot . $class; Chris@0: $class = ''; Chris@0: $alias = ''; Chris@0: $explicitAlias = false; Chris@0: } else if ($token === ';') { Chris@12: $statements[strtolower($alias)] = $groupRoot . $class; Chris@0: break; Chris@12: } else if ($token === '{' ) { Chris@12: $groupRoot = $class; Chris@12: $class = ''; Chris@12: } else if ($token === '}' ) { Chris@12: continue; Chris@0: } else { Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: return $statements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets all use statements. Chris@0: * Chris@0: * @param string $namespaceName The namespace name of the reflected class. Chris@0: * Chris@0: * @return array A list with all found use statements. Chris@0: */ Chris@0: public function parseUseStatements($namespaceName) Chris@0: { Chris@0: $statements = array(); Chris@0: while (($token = $this->next())) { Chris@0: if ($token[0] === T_USE) { Chris@0: $statements = array_merge($statements, $this->parseUseStatement()); Chris@0: continue; Chris@0: } Chris@0: if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: // Get fresh array for new namespace. This is to prevent the parser to collect the use statements Chris@0: // for a previous namespace with the same name. This is the case if a namespace is defined twice Chris@0: // or if a namespace with the same name is commented out. Chris@0: $statements = array(); Chris@0: } Chris@0: Chris@0: return $statements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the namespace. Chris@0: * Chris@0: * @return string The found namespace. Chris@0: */ Chris@0: public function parseNamespace() Chris@0: { Chris@0: $name = ''; Chris@0: while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) { Chris@0: $name .= $token[1]; Chris@0: } Chris@0: Chris@0: return $name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the class name. Chris@0: * Chris@0: * @return string The found class name. Chris@0: */ Chris@0: public function parseClass() Chris@0: { Chris@0: // Namespaces and class names are tokenized the same: T_STRINGs Chris@0: // separated by T_NS_SEPARATOR so we can use one function to provide Chris@0: // both. Chris@0: return $this->parseNamespace(); Chris@0: } Chris@0: }