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 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()); } }
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) ; }
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); } } } }