Beispiel #1
0
 public function setAsNameToken(Token $aAsNameToken)
 {
     if ($aAsNameToken->tokenType() != T_STRING) {
         throw new ClassCompileException(null, $aAsNameToken, "参数 \$aToken 必须是一个 T_STRING 类型的Token对象");
     }
     $this->aAsNameToken = $aAsNameToken;
 }
Beispiel #2
0
 public function setAbstractToken(Token $aAbstractToken)
 {
     if ($aAbstractToken->tokenType() !== T_ABSTRACT) {
         throw new ClassCompileException(null, $aAbstractToken, "参数 \$aAbstractToken 必须为 T_ABSTRACT 类型的Token对象");
     }
     $this->aAbstractToken = $aAbstractToken;
 }
Beispiel #3
0
 public function __construct(Token $aToken)
 {
     if ($aToken->tokenType() !== T_DOC_COMMENT) {
         throw new ClassCompileException(null, $aToken, "参数 \$aToken 必须为 T_DOC_COMMENT 类型的Token对象");
     }
     $this->cloneOf($aToken);
 }
Beispiel #4
0
 /**
  * 设置定义class名称的token
  */
 public function setNameToken(Token $aTokenName)
 {
     if ($aTokenName->tokenType() !== T_STRING) {
         throw new ClassCompileException(null, $aTokenName, "参数 \$aTokenName 必须是一个 T_STRING 类型 token 对象");
     }
     $this->aTokenName = $aTokenName;
 }
Beispiel #5
0
 public function __construct(Token $aToken)
 {
     if ($aToken->tokenType() != T_NAMESPACE) {
         throw new ClassCompileException(null, $aToken, "参数 \$aToken 必须是一个 T_NAMESPACE 类型的Token对象");
     }
     $this->cloneOf($aToken);
     $this->setBelongsNamespace($this);
 }
Beispiel #6
0
 public function __construct(Token $aToken, Token $aTokenName, Token $aArgv = null)
 {
     //自身必须是"new" token
     if ($aToken->tokenType() !== T_NEW) {
         throw new ClassCompileException(null, $aToken, "参数 \$aToken 必须是一个 T_NEW 类型的Token对象");
     }
     //name token 必须是 命名空间,变量,字符串中的一种
     if ($aTokenName->tokenType() !== T_NAMESPACE && $aTokenName->tokenType() !== T_VARIABLE && $aTokenName->tokenType() !== T_STRING) {
         throw new ClassCompileException(null, $aTokenName, "参数 \$aTokenName 必须是一个合法的类名Token对象");
     }
     $this->cloneOf($aToken);
     if ($aArgv !== null) {
         $this->setArgvToken($aArgv);
     }
 }
Beispiel #7
0
 public function generateTargetCode(TokenPool $aTokenPool, Token $aToken)
 {
     if (!$aToken instanceof FunctionDefine) {
         return;
     }
     if (!($aBodyToken = $aToken->bodyToken())) {
         return;
     }
     // 函数开始
     $aBodyToken->setTargetCode($aBodyToken->targetCode() . "\r\n\t// ---------------------------------------------------------------------------------" . "\r\n\t// ALERT: 此文件由 JeCat Class Compiler 自动生成和维护,请不要**直接编辑**此文件!" . "\r\n\t//   对此文件的任何改动,都会在下次生成时被新生成的文件覆盖。" . "\r\n");
     // 函数结束
     $aBodyEndToken = $aBodyToken->theOther();
     if (!$aBodyEndToken) {
         throw new ClassCompileException(null, $aBodyToken, "函数 %s 的函数体没有闭合", $aToken->name());
     }
     $aBodyEndToken->setTargetCode("\r\n\t// ALERT: 此文件由 JeCat Class Compiler 自动生成和维护,请不要**直接编辑**此文件!" . "\r\n\t//   对此文件的任何改动,都会在下次生成时被新生成的文件覆盖。" . "\r\n\t// ---------------------------------------------------------------------------------" . "\r\n\t" . $aBodyEndToken->targetCode());
 }
Beispiel #8
0
 public function cloneOf(self $aOther)
 {
     $this->nType = $aOther->nType;
     $this->setSourceCode($aOther->sourceCode());
     $this->setTargetCode($aOther->targetCode());
     $this->setPosition($aOther->position());
     $this->setBelongsClass($aOther->belongsClass());
     $this->setBelongsFunction($aOther->belongsFunction());
     $this->setBelongsNamespace($aOther->belongsNamespace());
     $this->setLine($aOther->line());
     $this->sType = $this->tokenTypeName();
 }
Beispiel #9
0
 public function __construct(Token $aToken)
 {
     if (!array_key_exists($aToken->tokenType(), self::$arrClosureObjectBeginTypes) and !array_key_exists($aToken->tokenType(), self::$arrClosureObjectEndTypes)) {
         throw new ClassCompileException(null, $aToken, "参数 \$aToken 传入的不是一个有效的闭合token(%s:%s)。该参数只接受以下类型的token:" . implode(', ', self::closureSymbols()), array($aToken->tokenTypeName(), $aToken->sourceCode()));
     }
     $this->cloneOf($aToken);
 }
 public function matchExecutionPoint(Token $aToken)
 {
     $bIsPattern = $this->weaveMethodIsPattern();
     // 模糊匹配每个方法
     if ($bIsPattern and $aToken instanceof FunctionDefine) {
         // 必须是一个类方法
         if (!($aClass = $aToken->belongsClass())) {
             return false;
         }
         if (!$this->matchClass($aClass->fullName())) {
             return false;
         }
         return preg_match($this->weaveMethodNameRegexp(), $aToken->name()) ? true : false;
     } else {
         if (!$bIsPattern and $aToken instanceof ClosureToken) {
             // 必须是一个 "}"
             if ($aToken->tokenType() != Token::T_BRACE_CLOSE) {
                 return false;
             }
             // 必须成对
             if (!$aToken->theOther()) {
                 return false;
             }
             $aClass = $aToken->theOther()->belongsClass();
             if (null === $aClass) {
                 return false;
             }
             // 必须做为 class 的结束边界
             if ($aToken->theOther() !== $aClass->bodyToken()) {
                 return false;
             }
             if (!$this->matchClass($aClass->fullName())) {
                 return false;
             }
             return true;
         }
     }
 }
Beispiel #11
0
 public function matchExecutionPoint(Token $aToken)
 {
     return preg_match($this->newObjectRegexp(), $aToken->sourceCode()) ? true : false;
 }
Beispiel #12
0
 public function matchWeaveMethod(Token $aToken)
 {
     if (!($aClass = $aToken->belongsClass()) or $aMethod = $aToken->belongsFunction()) {
         return false;
     }
     if ($aClass->fullName() != $this->weaveClass()) {
         return false;
     }
     return preg_match($this->weaveMethodNameRegexp(), $aMethod->name());
 }
 /**
  * 复制一个函数的参数表token
  */
 private function cloneFunctionArgvLst(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();
     $arrNewTokens = array();
     while ($aToken = $aIter->current() and $aToken !== $aArgLstEnd) {
         $aNewToken = new Token(0, '', 0);
         $aNewToken->cloneOf($aToken);
         $arrNewTokens[] = $aNewToken;
         $aIter->next();
     }
     return $arrNewTokens;
 }
	/**
	
		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) ;
	}