public function analyze(TokenPool $aObjectContainer) { $arrStacks = array(Token::T_BRACE_OPEN => new Stack(), Token::T_BRACE_SQUARE_OPEN => new Stack(), Token::T_BRACE_ROUND_OPEN => new Stack(), T_OPEN_TAG => new Stack()); $arrStacks[Token::T_BRACE_CLOSE] = $arrStacks[Token::T_BRACE_OPEN]; $arrStacks[Token::T_BRACE_SQUARE_CLOSE] = $arrStacks[Token::T_BRACE_SQUARE_OPEN]; $arrStacks[Token::T_BRACE_ROUND_CLOSE] = $arrStacks[Token::T_BRACE_ROUND_OPEN]; $arrStacks[T_OPEN_TAG_WITH_ECHO] = $arrStacks[T_OPEN_TAG]; $arrStacks[T_CLOSE_TAG] = $arrStacks[T_OPEN_TAG]; $arrStacks[T_DOLLAR_OPEN_CURLY_BRACES] = $arrStacks[Token::T_BRACE_OPEN]; $arrStacks[T_CURLY_OPEN] = $arrStacks[Token::T_BRACE_OPEN]; $aTokenIter = $aObjectContainer->iterator(); foreach ($aTokenIter as $aObject) { $nIdx = $aTokenIter->key(); $tokenType = $aObject->tokenType(); if (!in_array($tokenType, ClosureToken::openClosureTokens()) and !in_array($tokenType, ClosureToken::closeClosureTokens())) { continue; } $aNewToken = new ClosureToken($aObject); $aObjectContainer->replace($aObject, $aNewToken); if ($aNewToken->isOpen()) { $arrStacks[$tokenType]->put($aNewToken); } else { if (!($aOpenToken = $arrStacks[$tokenType]->out())) { throw new ClassCompileException(null, $aNewToken, "编译class时遇到了语法错误,闭合对象的结尾没有对应的开始:%s; on position %d", array($aNewToken->sourceCode(), $aNewToken->position())); } $aNewToken->setTheOther($aOpenToken); } } return; }
public function __construct(TokenPool $aTokenPool, Token $aToken = null, array &$arrAdvices = array(), array &$arrAspects = array()) { $this->aTokenPool = $aTokenPool; $this->aExecutePoint = $aToken; $this->arrAdvices =& $arrAdvices; $this->arrAspects =& $arrAspects; // 在 TokenPool 中记录 GenerateStat $arrAopWeavedStats = $aTokenPool->properties()->get('arrAopWeavedStats') ?: array(); $arrAopWeavedStats[] = $this; $aTokenPool->properties()->set('arrAopWeavedStats', $arrAopWeavedStats); }
public function parse(TokenPool $aTokenPool, INonlinearIterator $aTokenPoolIter, State $aState) { $aOriToken = $aTokenPoolIter->current(); if (!$aOriToken or $aOriToken->tokenType() != T_NAMESPACE) { return; } $aTokenPoolIter = clone $aTokenPoolIter; $aNewToken = new NamespaceDeclare($aOriToken); for ($aTokenPoolIter->next(); $aToken = $aTokenPoolIter->current() and $aToken->tokenType() != Token::T_SEMICOLON; $aTokenPoolIter->next()) { if ($aToken->tokenType() == T_STRING) { $aNewToken->addNameToken($aToken); } } $aTokenPool->replace($aOriToken, $aNewToken); $aState->setCurrentNamespace($aNewToken); }
public function analyze(TokenPool $aTokenPool) { $aState = new State(); $aTokenPoolIter = $aTokenPool->iterator(); foreach ($aTokenPoolIter as $aToken) { // 扫描php代码 $this->aPHPCodeParser->parse($aTokenPool, $aTokenPoolIter, $aState); if (!$aState->isPHPCode()) { continue; } foreach ($this->arrParsers as $aParser) { $aParser->parse($aTokenPool, $aTokenPoolIter, $aState); } $aToken->setBelongsNamespace($aState->currentNamespace()); $aToken->setBelongsClass($aState->currentClass()); $aToken->setBelongsFunction($aState->currentFunction()); } }
public function parse(TokenPool $aTokenPool, INonlinearIterator $aTokenPoolIter, State $aState) { $aTokenPoolIter = clone $aTokenPoolIter; $aToken = $aTokenPoolIter->current(); if ($aToken->tokenType() !== T_NEW) { return; } $aNewToken = $aToken; // 找到new后面的类名 do { $aTokenPoolIter->next(); } while ($aToken = $aTokenPoolIter->current() and !($aToken->tokenType() == T_VARIABLE or $aToken->tokenType() == T_STRING or $aToken->tokenType() == T_NAMESPACE)); if (!($aClassName = $aTokenPoolIter->current())) { return; } $aNewObject = new NewObject($aNewToken, $aClassName); // 置换 $aTokenPool->replace($aNewToken, $aNewObject); // 把属性列表"告诉"NewObject do { $aTokenPoolIter->next(); } while ($aToken = $aTokenPoolIter->current() and $aToken->tokenType() != Token::T_BRACE_ROUND_OPEN); $aNewObject->setArgvToken($aToken); }
public function parse(TokenPool $aTokenPool, INonlinearIterator $aTokenPoolIter, State $aState) { $aOriToken = $aTokenPoolIter->current(); if (!$aOriToken or $aOriToken->tokenType() != T_USE) { return; } $aTokenPoolIter = clone $aTokenPoolIter; $aNewToken = new UseDeclare($aOriToken); $bFoundAs = false; for ($aTokenPoolIter->next(); $aToken = $aTokenPoolIter->current() and $aToken->tokenType() != Token::T_SEMICOLON; $aTokenPoolIter->next()) { if ($aToken->tokenType() == T_STRING) { $aNewToken->addNameToken($aToken); } else { if ($aToken->tokenType() == T_AS) { $bFoundAs = true; break; } } } // 寻找 as if ($bFoundAs) { $aAsNameToken = null; for ($aTokenPoolIter->next(); $aToken = $aTokenPoolIter->current() and $aToken->tokenType() != Token::T_SEMICOLON; $aTokenPoolIter->next()) { if ($aToken->tokenType() == T_STRING) { $aAsNameToken = $aToken; break; } } if (!$aAsNameToken) { throw new ClassCompileException(null, $aNewToken, "编译class时遇到错误,as关键字后没有有效的名称"); } $aNewToken->setAsNameToken($aAsNameToken); } $aTokenPool->replace($aOriToken, $aNewToken); $aTokenPool->addUseDeclare($aNewToken); }
public function parse(TokenPool $aTokenPool, INonlinearIterator $aTokenPoolIter, State $aState) { $aTokenPoolIter = clone $aTokenPoolIter; $aToken = $aTokenPoolIter->current(); if ($aToken->tokenType() !== Token::T_BRACE_ROUND_OPEN) { return; } // 找参数括号前面的函数名 // --------------------------------------------- $aFinderIter = new CallbackFilterIterator(new ReverseIterator($aTokenPoolIter), function (IReversableIterator $aTokenPoolIter) { switch ($aTokenPoolIter->current()->tokenType()) { case T_WHITESPACE: // 过滤空白token return false; case T_STRING: // 预期的token ,函数名 return true; default: // 遇到意外的token $aTokenPoolIter->last(); return false; } }); $aFinderIter->next(); if (!($aCallFunctionName = $aFinderIter->current())) { return; } // 检查函数名前面的 function 声明 // --------------------------------------------- $aFinderIter = new CallbackFilterIterator(new ReverseIterator(clone $aTokenPoolIter), function (IReversableIterator $aTokenPoolIter) { switch ($aTokenPoolIter->current()->tokenType()) { case T_WHITESPACE: // 过滤空白token return false; case T_FUNCTION: // 预期的token return true; default: // 遇到意外的token $aTokenPoolIter->last(); return false; } }); // 找到 function 申明,这是函数定义 而不是函数调用 $aFinderIter->next(); if ($aFinderIter->current()) { return; } $aCallFunction = new CallFunction($aCallFunctionName, $aToken); // 置换 $aTokenPool->replace($aCallFunctionName, $aCallFunction); // 查找class成员访问符:-> 或 :: // --------------------------------------------- $aFinderIter = new CallbackFilterIterator(new ReverseIterator($aTokenPoolIter), function (IReversableIterator $aTokenPoolIter) { switch ($aTokenPoolIter->current()->tokenType()) { case T_WHITESPACE: // 过滤空白token return false; case T_OBJECT_OPERATOR: // 预期的token: "->" return true; case T_PAAMAYIM_NEKUDOTAYIM: // 预期的token: "::" // 预期的token: "::" case T_DOUBLE_COLON: return true; default: // 无效的token $aTokenPoolIter->last(); return false; } }); // 方法 $aFinderIter->next(); if ($aAccessSymbol = $aFinderIter->current()) { $aFinderIter = new CallbackFilterIterator(new ReverseIterator($aTokenPoolIter), function (IReversableIterator $aTokenPoolIter) { // 过滤空白token return $aTokenPoolIter->current()->tokenType() !== T_WHITESPACE; }); $aFinderIter->next(); $aClass = $aFinderIter->current(); $aCallFunction->setClassToken($aClass); $aCallFunction->setAccessToken($aAccessSymbol); } }
private function parseArgvs(TokenPool $aTokenPool, FunctionDefine $aOriFunctionDefine) { $aArgLstStart = $aOriFunctionDefine->argListToken(); $aArgLstEnd = $aArgLstStart->theOther(); $aIter = $aTokenPool->iterator(); $nPos = $aIter->search($aArgLstStart); if ($nPos === false) { return array(); } $aIter->seek($nPos); $aIter->next(); $arrArgvs = array(); while ($aToken = $aIter->current() and $aToken !== $aArgLstEnd) { // 跳过成对的token if ($aToken instanceof ClosureToken) { if (!($aTheOtherToken = $aToken->theOther())) { throw new Exception("ClosureToken Token没有正确配对。"); } if (($nPos = $aIter->search($aTheOtherToken)) === false) { throw new Exception("ClosureToken Token配对的token无效。"); } $aIter->seek($nPos); } if ($aToken->tokenType() === T_VARIABLE) { $sArgv = $aToken->targetCode(); $sArgvName = substr($sArgv, 0, 1) == '$' ? @substr($sArgv, 1) : $sArgv; $arrArgvs[$sArgvName] = $sArgv; } $aIter->next(); } return $arrArgvs; }
/** foreach($arr as $key=>$value){ code; } => for( key in arr){ value = arr[ key ]; code ; } */ private function transForeach(TokenPool $aTokenPool,Token $aObject) { // 定位迭代器 $aTokenIter = $aTokenPool->iterator() ; if( !$nPos=$aTokenIter->search($aObject) ) { Assert::wrong('提供的 $aObject 不再 $aTokenPool 中') ; } $aTokenIter->seek($nPos) ; // 找条件开始的 ”(“ do{ $aTokenIter->next() ; } while( $aToken=$aTokenIter->current() and $aToken->tokenType()!=Token::T_BRACE_ROUND_OPEN ) ; if(!$aToken) { throw new ClassCompileException($aObject, "foreach 后没有 ( ") ; } $aBraceRoundOpenToken = $aToken ; // 找 ( 到 as 之间的表达值 $arrExpressions = array() ; for( $aTokenIter->next(); $aToken=$aTokenIter->current() and $aToken->tokenType()!=T_AS; $aTokenIter->next() ) { $arrExpressions[] = $aToken ; } // 吐掉最后的空格 array_pop($arrExpressions); $aAsToken = $aToken ; // 吃掉as后的空格 $aTokenIter->next(); // as 到 闭合 ) 之间是否有 '=>' $aDoubleArrowToken = null ; $arrTokenListBeforeArrow = array(); $arrTokenListAfterArrow = array(); for( $aTokenIter->next() ; $aToken = $aTokenIter->current() and $aToken !== $aBraceRoundOpenToken->theOther() ; $aTokenIter->next() ){ if( $aToken->tokenType() === T_DOUBLE_ARROW ){ $aDoubleArrowToken = $aToken ; }else if( null === $aDoubleArrowToken ){ $arrTokenListBeforeArrow [] = $aToken ; }else{ $arrTokenListAfterArrow [] = $aToken ; } } // 清理最后的空格 if( end($arrTokenListBeforeArrow) ->tokenType() === T_WHITESPACE ){ array_pop( $arrTokenListBeforeArrow ); } if( end($arrTokenListAfterArrow) and end($arrTokenListAfterArrow)->tokenType() === T_WHITESPACE ){ array_pop( $arrTokenListAfterArrow ); } // 寻找开始的 '{' // 空体for循环 for( $aTokenIter->next() ; $aToken = $aTokenIter->current() and $aToken->tokenType() !== Token::T_BRACE_OPEN ; $aTokenIter->next() ); $aBraceOpenToken = $aToken ; // 开始替换 // foreach 替换成 for $aObject->setTargetCode('for') ; // arrExpressions 替换成 key reset($arrExpressions); if( null === $aDoubleArrowToken ){ $aToken = new Token(T_WHITESPACE,T_WHITESPACE); $aToken->setTargetCode(__FUNCTION__.'_key'); $aTokenPool->insertBefore( current($arrExpressions) , $aToken ); }else{ // insert key foreach($arrTokenListBeforeArrow as $aToken){ $aTokenPool->insertBefore( current($arrExpressions) , $aToken ); } // insert a space $aSpaceToken = new Token(T_WHITESPACE,' '); $aTokenPool->insertBefore( current($arrExpressions) , $aSpaceToken ); // remove arr do{ $aTokenPool->remove(current($arrExpressions) ); next($arrExpressions) ; }while( false !== current($arrExpressions) ); } // as 替换成 in $aAsToken->setTargetCode('in'); // in 后面是arr // 直接插入到闭合圆括号之前 foreach($arrExpressions as $aToken){ $aCloneToken = clone $aToken ; $this->generateTargetCode($aTokenPool , $aCloneToken ); $aTokenPool->insertBefore( $aBraceRoundOpenToken->theOther() , $aCloneToken ); } // '=>' 删掉 if( null !== $aDoubleArrowToken ){ $aDoubleArrowToken->setTargetCode(''); } // '{'之后是value // 先换行 $aToken = new Token(T_WHITESPACE,"\n"); $arrValueInsert = array($aToken); if( null === $aDoubleArrowToken ){ $arrValueInsert = array_merge( $arrValueInsert , $arrTokenListBeforeArrow ); }else{ $arrValueInsert = array_merge( $arrValueInsert , $arrTokenListAfterArrow ); } // value 之后是 =arr[key]; // = $aEqualToken = new Token(Token::T_EQUAL,'='); $arrValueInsert [] = $aEqualToken ; // arr foreach($arrExpressions as $aToken){ $aCloneToken = clone $aToken ; $arrValueInsert [] = $aToken ; } // [ $aBraceSquareOpenToken = new Token(Token::T_BRACE_SQUARE_OPEN,'['); $arrValueInsert [] = $aBraceSquareOpenToken ; // key if( null === $aDoubleArrowToken ){ $aToken = new Token(T_WHITESPACE,T_WHITESPACE); $aToken->setTargetCode(__FUNCTION__.'_key'); $arrValueInsert [] = $aToken ; }else{ // insert key foreach($arrTokenListBeforeArrow as $aToken){ $aCloneToken = clone $aToken ; $this->generateTargetCode($aTokenPool , $aCloneToken ); $arrValueInsert [] = $aCloneToken ; } } // ] $aBraceSquareCloseToken = new Token(Token::T_BRACE_SQUARE_CLOSE,']'); $arrValueInsert [] = $aBraceSquareCloseToken ; // ; $aSemicolonToken = new Token(Token::T_SEMICOLON,Token::T_SEMICOLON); $arrValueInsert [] = $aSemicolonToken ; // insert array_unshift($arrValueInsert , $aBraceOpenToken); call_user_func_array(array($aTokenPool,'insertAfter'),$arrValueInsert) ; }
protected function createMethod($sFuncName, $argLstToken = null, $sAccess, $bStatic = false, TokenPool $aTokenPool, Token $aTargetToken, $sWhere = 'insertAfter') { $aFuncToken = new FunctionDefine(new Token(T_FUNCTION, 'function')); // 函数体 $aBodyStart = new ClosureToken(new Token(T_STRING, '{')); $aBodyEnd = new ClosureToken(new Token(T_STRING, '}')); $aBodyStart->setTheOther($aBodyEnd); $aTokenPool->{$sWhere}($aTargetToken, $aBodyStart); $aTokenPool->insertAfter($aBodyStart, $aBodyEnd); $aTokenPool->insertAfter($aBodyEnd, new Token(T_WHITESPACE, "\r\n")); $aTokenPool->insertBefore($aBodyStart, new Token(T_WHITESPACE, "\r\n\t")); // static if ($bStatic) { $aStaticToken = new Token(T_PRIVATE, 'static'); $aTokenPool->insertBefore($aBodyStart, $aStaticToken); $aTokenPool->insertBefore($aBodyStart, new Token(T_WHITESPACE, "\r\n\t")); $aFuncToken->setStaticToken($aStaticToken); } // access $arrAccessTokenTypes = array('private' => T_PRIVATE, 'protected' => T_PROTECTED, 'public' => T_PUBLIC); $aAccessToken = new Token($arrAccessTokenTypes[$sAccess], $sAccess); $aTokenPool->insertBefore($aBodyStart, $aAccessToken); $aTokenPool->insertBefore($aBodyStart, new Token(T_WHITESPACE, ' ')); $aFuncToken->setAccessToken($aAccessToken); // function $aTokenPool->insertBefore($aBodyStart, $aFuncToken); $aTokenPool->insertBefore($aBodyStart, new Token(T_WHITESPACE, ' ')); $aFuncToken->setBodyToken($aBodyStart); // function name $aFuncNameToken = new Token(T_STRING, $sFuncName); $aTokenPool->insertBefore($aBodyStart, $aFuncNameToken); $aFuncToken->setNameToken($aFuncNameToken); // 参数表 $aArgvLstStart = new ClosureToken(new Token(T_STRING, '(')); $aArgvLstEnd = new ClosureToken(new Token(T_STRING, ')')); $aArgvLstStart->setTheOther($aArgvLstEnd); $aTokenPool->insertBefore($aBodyStart, $aArgvLstStart); if ($argLstToken) { $aTokenPool->insertBefore($aBodyStart, new Token(T_STRING, $argLstToken)); } $aTokenPool->insertBefore($aBodyStart, $aArgvLstEnd); $aTokenPool->insertBefore($aBodyStart, new Token(T_WHITESPACE, "\r\n\t")); $aFuncToken->setArgListToken($aArgvLstStart); return $aFuncToken; }
public function parse(TokenPool $aTokenPool, INonlinearIterator $aTokenPoolIter, State $aState) { if (!($aOriToken = $aTokenPoolIter->current())) { return; } // 已经处于 class 状态中 if ($aClass = $aState->currentClass()) { // 遇到 if ($aOriToken === $aClass->bodyToken()->theOther()) { $aState->setCurrentClass(null); } return; } else { if (T_INTERFACE !== $aOriToken->tokenType() && T_CLASS !== $aOriToken->tokenType()) { return; } $aTokenPoolIter = clone $aTokenPoolIter; $aNewToken = new ClassDefine($aOriToken); // class name do { $aTokenPoolIter->next(); } while ($aToken = $aTokenPoolIter->current() and $aToken->tokenType() != T_STRING); $aNewToken->setNameToken($aToken); $aNewToken->setBelongsNamespace($aState->currentNamespace()); // doc comment and abstract for ($aTokenPoolIter->prev(); $aToken = $aTokenPoolIter->current(); $aTokenPoolIter->prev()) { switch ($aToken->tokenType()) { case T_ABSTRACT: $aNewToken->setAbstract(true); break; case T_DOC_COMMENT: $aDocToken = new DocCommentDeclare($aToken); $aNewToken->setDocToken($aDocToken); $aTokenPool->replace($aToken, $aDocToken); break; case T_WHITESPACE: case T_CLASS: break; default: break 2; } } // init extends implements body $sState = 'init'; // parent/body $aClassBodyToken = $aParentNameToken = null; for ($aTokenPoolIter->next(); $aToken = $aTokenPoolIter->current(); $aTokenPoolIter->next()) { // 控制触发 addParentClassName 操作 $sAddParentClassName = false; // 分析 tokenType switch ($aToken->tokenType()) { case Token::T_BRACE_OPEN: $aClassBodyToken = $aToken; $sAddParentClassName = $sState; $sState = 'body'; break 2; case T_EXTENDS: $sAddParentClassName = $sState; $sState = 'extends'; break; case T_IMPLEMENTS: $sAddParentClassName = $sState; $sState = 'implements'; break; case T_STRING: case T_NS_SEPARATOR: switch ($sState) { case 'extends': case 'implements': if (!$aParentNameToken) { $aParentNameToken = new NamespaceString(0, ''); $aParentNameToken->setBelongsNamespace($aState->currentNamespace()); } $aParentNameToken->addNameToken($aToken); break; } break; case Token::T_COLON: case T_WHITESPACE: $sAddParentClassName = $sState; break; } // addParentClassName if ($sAddParentClassName && $aParentNameToken) { switch ($sAddParentClassName) { case 'init': break; case 'extends': $aNewToken->addParentClassName($aParentNameToken->findRealName($aTokenPool)); $aParentNameToken = null; break; case 'implements': $aNewToken->addImplementsInterfaceName($aParentNameToken->findRealName($aTokenPool)); $aParentNameToken = null; break; case 'body': break; } } } if (!$aClassBodyToken) { throw new ClassCompileException(null, $aOriToken, "编译class: %s时遇到了错误,class没有body", $aNewToken->name()); } $aNewToken->setBodyToken($aClassBodyToken); // 完成 $aTokenPool->replace($aOriToken, $aNewToken); $aState->setCurrentClass($aNewToken); $aTokenPool->addClass($aNewToken); } }
public function parse(TokenPool $aTokenPool, INonlinearIterator $aTokenPoolIter, State $aState) { if (!($aOriToken = $aTokenPoolIter->current())) { return; } // 已经处于 function 状态中 if ($aFunction = $aState->currentFunction()) { // 遇到 if ($aFunction->bodyToken() and $aFunction->bodyToken()->theOther() === $aOriToken) { $aState->setCurrentFunction(null); } return; } else { if ($aOriToken->tokenType() != T_FUNCTION) { return; } $aTokenPoolIter = clone $aTokenPoolIter; $aNewToken = new FunctionDefine($aOriToken); // function 修饰符 ------ for ($aTokenPoolIter->prev(); $aToken = $aTokenPoolIter->current(); $aTokenPoolIter->prev()) { switch ($aToken->tokenType()) { case T_PUBLIC: $aNewToken->setAccessToken($aToken); break; case T_PROTECTED: $aNewToken->setAccessToken($aToken); break; case T_PRIVATE: $aNewToken->setAccessToken($aToken); break; case T_STATIC: $aNewToken->setStaticToken($aToken); break; case T_ABSTRACT: $aNewToken->setAbstractToken($aToken); break; case T_DOC_COMMENT: $aDocToken = new DocCommentDeclare($aToken); $aNewToken->setDocToken($aDocToken); $aTokenPool->replace($aToken, $aDocToken); break 2; case T_WHITESPACE: break; default: break 2; } } // function name ------ for ($aTokenPoolIter->next(); $aToken = $aTokenPoolIter->current(); $aTokenPoolIter->next()) { switch ($aToken->tokenType()) { // 函数名称 case T_STRING: $aNewToken->setNameToken($aToken); break 2; // 匿名函数 // 匿名函数 case Token::T_BRACE_ROUND_OPEN: $aTokenPoolIter->prev(); break 2; case Token::T_BIT_AND: $aNewToken->setReturnByRef(true); break; } } // function argv list ------ do { $aTokenPoolIter->next(); } while ($aToken = $aTokenPoolIter->current() and $aToken->tokenType() != Token::T_BRACE_ROUND_OPEN); $aNewToken->setArgListToken($aToken); // 移动到参数表结束符号后 $nPosition = $aTokenPoolIter->search($aToken->theOther()); $aTokenPoolIter->seek($nPosition); // function body ------ do { $aTokenPoolIter->next(); } while ($aToken = $aTokenPoolIter->current() and !in_array($aToken->tokenType(), array(Token::T_BRACE_OPEN, Token::T_SEMICOLON))); if ($aToken) { if ($aToken->tokenType() == Token::T_BRACE_OPEN) { $aNewToken->setBodyToken($aToken); $aState->setCurrentFunction($aNewToken); } else { if ($aToken->tokenType() == Token::T_SEMICOLON) { $aNewToken->setEndToken($aToken); } } } $aTokenPool->replace($aOriToken, $aNewToken); $aNewToken->setBelongsNamespace($aState->currentNamespace()); $aNewToken->setBelongsClass($aState->currentClass()); $aTokenPool->addFunction($aNewToken); } }
public function parse(TokenPool $aTokenPool, INonlinearIterator $aTokenPoolIter, State $aState) { if (!($aOriToken = $aTokenPoolIter->current())) { return; } // 是否处在function中 if (!($aFunction = $aState->currentFunction())) { return; } // 是否是function的argListToken if ($aFunction->argListToken() !== $aOriToken) { return; } $aTokenPoolIter = clone $aTokenPoolIter; $aNewToken = null; for ($aTokenPoolIter->next(); $aToken = $aTokenPoolIter->current(); $aTokenPoolIter->next()) { switch ($aToken->tokenType()) { // 结束 case Token::T_BRACE_ROUND_CLOSE: if ($aToken === $aOriToken->theOther()) { if (null !== $aNewToken) { $aNewToken->setBelongsFunction($aFunction); $aFunction->addParameterToken($aNewToken); $aTokenPool->replace($aNewToken->nameToken(), $aNewToken); $aNewToken = null; } break 2; } break; // 类型 // 类型 case T_STRING: if (null === $aNewToken) { $aNewToken = new Parameter($aToken); } $aNewToken->setTypeToken($aToken); break; // 引用传递 // 引用传递 case Token::T_BIT_AND: if (null === $aNewToken) { $aNewToken = new Parameter($aToken); } $aNewToken->setReference(true); break; // 参数名 // 参数名 case T_VARIABLE: if (null === $aNewToken) { $aNewToken = new Parameter($aToken); } $aNewToken->setNameToken($aToken); break; // 默认值 // 默认值 case Token::T_EQUAL: if (null === $aNewToken) { $aNewToken = new Parameter($aToken); } $aDefaultValueToken = new ParameterDefaultValue(); //直接跳到结束(右括号)或下一个参数(逗号)之前 for ($aTokenPoolIter->next(); $aEscapeToken = $aTokenPoolIter->current(); $aTokenPoolIter->next()) { switch ($aEscapeToken->tokenType()) { // 在默认值中遇到左括号,直接跳过到它匹配的右括号后面 case Token::T_BRACE_ROUND_OPEN: //$nPosition = $aTokenPoolIter->search($aEscapeToken->theOther()) ; //$aTokenPoolIter->seek($nPosition) ; // 括号之间的全部加入DefaultValueToken $aDefaultValueToken->addSubToken($aEscapeToken); do { $aTokenPoolIter->next(); $aInsideBraceToken = $aTokenPoolIter->current(); $aDefaultValueToken->addSubToken($aInsideBraceToken); } while ($aInsideBraceToken !== $aEscapeToken->theOther()); break; // 跳到结束右括号之前,不加入DefaultValueToken // 跳到结束右括号之前,不加入DefaultValueToken case Token::T_BRACE_ROUND_CLOSE: if ($aEscapeToken === $aOriToken->theOther()) { $aTokenPoolIter->prev(); break 2; } break; // 跳到下一个参数的逗号之前,不加入DefaultValueToken // 跳到下一个参数的逗号之前,不加入DefaultValueToken case Token::T_COLON: $aTokenPoolIter->prev(); break 2; default: $aDefaultValueToken->addSubToken($aEscapeToken); break; } } $aNewToken->setDefaultValueToken($aDefaultValueToken); break; // 逗号,下一个参数 // 逗号,下一个参数 case Token::T_COLON: if (null !== $aNewToken) { $aNewToken->setBelongsFunction($aFunction); $aFunction->addParameterToken($aNewToken); $aTokenPool->replace($aNewToken->nameToken(), $aNewToken); $aNewToken = null; } break; } } }
public function generate(TokenPool $aTokenPool) { // 编译 foreach ($aTokenPool->iterator() as $aObject) { for ($sClassName = get_class($aObject); $sClassName; $sClassName = get_parent_class($sClassName)) { if (empty($this->mapGeneratorClasses[$sClassName])) { continue; } foreach ($this->mapGeneratorClasses[$sClassName] as $sGeneratorClass) { $aGenerator = $this->generator($sGeneratorClass); $aGenerator->generateTargetCode($aTokenPool, $aObject); } } } }