Ejemplo n.º 1
0
 /**
  * @param int $token
  * @param string $value
  *
  * @return bool
  * @throws LexerException
  */
 protected function token($token, $value)
 {
     switch ($token) {
         case T_NAMESPACE:
             $namespaceSource = NamespaceLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->namespaceList[$namespaceSource->name] = $namespaceSource;
             break;
         case T_USE:
             $useSource = UseLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->useList[$useSource->alias ?: $useSource->use] = $useSource;
             break;
         case T_CLASS:
             foreach ($this->reflectionTokenizer->getTokenStack()->last(2) as $stackToken) {
                 if ($stackToken[0] === T_DOUBLE_COLON) {
                     break 2;
                 }
             }
             $classSource = ClassLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->classList[$classSource->name] = $classSource;
             break;
         case T_TRAIT:
             $traitSource = TraitLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->traitList[$traitSource->name] = $traitSource;
             break;
         case T_INTERFACE:
             $interfaceSource = InterfaceLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->interfaceList[$interfaceSource->name] = $interfaceSource;
             break;
         case T_FUNCTION:
             $functionSource = FunctionLexer::build($this->reflectionTokenizer->setContinue());
             /**
              * we want only common functions, ignoring lambdas
              */
             if ($functionSource->name) {
                 $this->source->functionList[$functionSource->name] = $functionSource;
             }
             break;
     }
 }
Ejemplo n.º 2
0
 /**
  * @param int $token
  * @param string $value
  *
  * @return bool if true is returned, lexer will end its loop
  * @throws LexerException
  */
 protected function token($token, $value)
 {
     switch ($token) {
         case T_NAMESPACE:
             /**
              * it's good to know that we meet that keyword....
              */
             $this->source = new \stdClass();
             $this->source->name = null;
             $this->source->annotation = null;
             $this->source->useList = [];
             $this->source->classList = [];
             $this->source->interfaceList = [];
             $this->source->traitList = [];
             $this->source->functionList = [];
             foreach ($this->reflectionTokenizer->getTokenStack() as $stackToken) {
                 list($innerToken, $innerValue) = $stackToken;
                 switch ($innerToken) {
                     case T_DOC_COMMENT:
                         $this->source->annotation = $innerValue;
                         break 2;
                 }
             }
             $this->reflectionTokenizer->next();
             foreach ($this->reflectionTokenizer->setContinue() as $innerToken => $innerValue) {
                 switch ($innerToken) {
                     case ';':
                     case T_CURLY_OPEN:
                     case '{':
                         $this->curly++;
                         break 2;
                     case T_WHITESPACE:
                         break;
                     default:
                         $this->source->name .= $innerValue;
                 }
             }
             break;
         case T_USE:
             $this->use = true;
             if ($this->source === null) {
                 throw new LexerException('Found T_USE, but it seems, there is no namespace.');
             }
             $useSource = UseLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->useList[$useSource->alias ?: $useSource->use] = $useSource;
             break;
         case ',':
             if ($this->use) {
                 /**
                  * move out of "," or UseLexer will skip parsing
                  */
                 $this->reflectionTokenizer->next();
                 $useSource = UseLexer::build($this->reflectionTokenizer->setContinue());
                 $this->source->useList[$useSource->alias ?: $useSource->use] = $useSource;
             }
             break;
         case ';':
             $this->use = false;
             break;
         case T_INTERFACE:
             if ($this->source === null) {
                 throw new LexerException(sprintf('Requested interface parsing without namespace context - source [%d: %s].', $this->reflectionTokenizer->getLine(), $this->reflectionTokenizer->getUri()));
             }
             $interfaceSource = InterfaceLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->interfaceList[$interfaceSource->name] = $interfaceSource;
             break;
         case T_TRAIT:
             if ($this->source === null) {
                 throw new LexerException(sprintf('Requested trait parsing without namespace context - source [%d: %s].', $this->reflectionTokenizer->getLine(), $this->reflectionTokenizer->getUri()));
             }
             $traitSource = TraitLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->traitList[$traitSource->name] = $traitSource;
             break;
         case T_CLASS:
             /** blah::class -> skip */
             foreach ($this->reflectionTokenizer->getTokenStack()->last(2) as $stackToken) {
                 if ($stackToken[0] === T_DOUBLE_COLON) {
                     break 2;
                 }
             }
             if ($this->source === null) {
                 throw new LexerException(sprintf('Requested class parsing without namespace context - source [%d: %s].', $this->reflectionTokenizer->getLine(), $this->reflectionTokenizer->getUri()));
             }
             $classSource = ClassLexer::build($this->reflectionTokenizer->setContinue());
             $this->source->classList[$classSource->name] = $classSource;
             break;
         case T_FUNCTION:
             $functionSource = FunctionLexer::build($this->reflectionTokenizer->setContinue());
             /**
              * we want only common functions, ignoring lambdas
              */
             if ($functionSource->name) {
                 $this->source->functionList[$functionSource->name] = $functionSource;
             }
             break;
         case T_CURLY_OPEN:
         case '{':
             $this->curly++;
             break;
         case '}':
             if (--$this->curly <= 0) {
                 return true;
             }
             break;
     }
     return false;
 }