aladdinparser.jjt

来自「mysql集群」· JJT 代码 · 共 2,022 行 · 第 1/4 页

JJT
2,022
字号
	   		if(columnName == null){
	   			columnName = tableName;
	   			tableName = schemaName;
	   			schemaName = null;
	   			table = tableAliasMap.get(tableName);
	   		}
	   	}
	   	column.setTable(table);
	   	column.setName(columnName);
	   	return column;
   }
}

String EntityName() #void:
{
	Token token = null;
}
{
 	token =  <IDENTIFIER>{
 		return token.image;
 	}
 	|
 	token =  <S_QUOTED_IDENTIFIER>{
 		return token.image.substring(1,token.image.length()-1);
 	}
 	| token = <S_COMMA_IDENTIFIER>{
 		return token.image.substring(1,token.image.length()-1);	
 	}
}

String Relop():
{
	Token token;
}
{
  (token = "=" | token = "!=" | token = "<>" | token = ">" | token = ">=" |token = "<" |token= "<="){
  	return token.image;
  }
  
}

Table TableName() :
{
	String schemaName = null;
	String tableName = null;
	Table table = null;
	Schema schema = null;
	
}
{
     (schemaName = EntityName() 
     ["." 
     	tableName= EntityName()
     ] ["PARTITION" "("EntityName()")"]){
     	if(tableName == null){
     		tableName = schemaName;
     		schemaName = null;	
     	}
     	table = new Table();
     	table.setName(tableName);
     	if(schemaName != null){
     		schema = new Schema();
     		schema.setName(schemaName);
     		table.setSchema(schema);	
     	}else{
     		table.setSchema(defaultSchema);	
     	}
     	tableAliasMap.put(table.getName(),table);
     	tableStack.push(table);
     	return table;
     }
}

Table TableReference():
{
	Table table = null;
	Token token = null;
}
{
    table = TableName() [
    ["AS"] token = <IDENTIFIER>{
    	table.setAlias(token.image);
    }]{
   	 if(table.getAlias() != null){
   	 	tableAliasMap.put(table.getAlias(),table);	
   	 }
   	 return table;
   	 }
}

void CaseStatement() :
{}
{
    "CASE" [SQLSimpleExpression()] WhenStatement() (WhenStatement())* [ElseStatement()] "END"
}


void WhenStatement() :
{}
{
	"WHEN" SQLExpression() "THEN" SQLSimpleExpression()
}

void ElseStatement() :
{}
{
	"ELSE" SQLSimpleExpression()
}


void ExtraClauses()  #void:
{}
{
    (
    	LOOKAHEAD(2) ForUpdateClause()
    	| LOOKAHEAD(2) ReadOnlyClause()
    	| OptimizeForClause()
    	| WithClause()
    	| QuerynoClause()
    	| FetchFirstClause()
    )
}

Expression FullSelectStatement():
{
	Expression expression = null;
	Expression unionedExpression = null;
}
{
	((expression = SubSelectStatement() [ UnionClause() unionedExpression = FullSelectStatement()])
	| "(" expression = FullSelectStatement() ")"){
		if(unionedExpression != null){
			if(expression == null){
				return unionedExpression;
			}else{
				expression = new OrExpression(expression);
				((OrExpression)expression).addExpression(unionedExpression);
				return expression;
			}
		}else{
			return expression;
		}
	}
}

void UnionClause():
{}
{
        ("UNION" ["ALL"]) | "INTERSECT" | "MINUS"
}

Expression SubSelectStatement():
{
	Expression expression = null;
	AndExpression andExpression = null;
}
{
    (SelectClause()
    [expression=FromClause(){
    	if(expression != null){
    		if(andExpression == null){
    			andExpression = new AndExpression();	
    		}
			andExpression.addExpression(expression);
		}else{
			
			
    	}	
    }]
    [ expression = WhereClause(){
    	if(expression != null){
    		if(andExpression == null){
    			andExpression = new AndExpression();	
    		}
			andExpression.addExpression(expression);
		}
    }]
    [ GroupByClause() ]
    [ OrderByClause() ]
    [<K_LIMIT> <INTEGER_LITERAL>["," <INTEGER_LITERAL>]])
    {
    	return andExpression;	
    }
}

void SelectClause():
{}
{
	 "SELECT" [ "ALL" | "DISTINCT"|"DISTINCTROW" ]["HIGH_PRIORITY"]["STRAIGHT_JOIN"] ["SQL_SMALL_RESULT"] ["SQL_BIG_RESULT"] ["SQL_BUFFER_RESULT"] ["SQL_CACHE" | "SQL_NO_CACHE"] ["SQL_CALC_FOUND_ROWS"] SelectList()
}

/* Checks for whatever follows  SELECT */
void SelectList() :
{}
{
    "*"("," SelectItem())* | SelectItem() ("," SelectItem())*
}

void SelectItem():
{}
{
    (
        LOOKAHEAD(2) EntityName()".*"  /* table.* */
        |   LOOKAHEAD(4) EntityName()"." EntityName() ".*" /* schema.table.* */
        |   SQLSimpleExpression()    // Column or Expression
    )[("=" SQLSimpleExpression())] [(["AS"] <IDENTIFIER>)] // Column Alias
}


Expression FromClause() :
{
	Expression expression = null;
	AndExpression andExpression = null;
}
{
    "FROM" expression=FromItem(){
    	if(expression != null){
    		if(andExpression == null){
    			andExpression = new AndExpression();
    		}
			andExpression.addExpression(expression);
		}	
    } ( "," expression=FromItem(){
    	if(expression != null){
    		if(andExpression == null){
    			andExpression = new AndExpression();
    		}
			andExpression.addExpression(expression);
		}    	
    })*{
    	return andExpression;
    }
}


Expression FromItem() :
{
	Expression expression = null;
	AndExpression andExpression = null;
}
{
  expression=TableSpec(){
  	if(expression != null){
  			if(andExpression == null){
    			andExpression = new AndExpression();
    		}
			andExpression.addExpression(expression);
	}
  } (expression=JoinedTable(){
  	if(expression != null){
  			if(andExpression == null){
    			andExpression = new AndExpression();
    		}
			andExpression.addExpression(expression);
	}
  })*
  {
  	return andExpression;
  }
}


Expression JoinedTable() :
{
	Expression expression = null;
	AndExpression andExpression = null;
}
{
	(((["NATURAL"] (("LEFT"|"RIGHT") ["OUTER"])) | (["INNER" | "CROSS"])) "JOIN"
	(LOOKAHEAD(2) expression=TableSpec(){
		if(expression != null){
			if(andExpression == null){
    			andExpression = new AndExpression();
    		}
			andExpression.addExpression(expression);
		}	
	} (expression=JoinedTable(){
		if(expression != null){
			if(andExpression == null){
    			andExpression = new AndExpression();
    		}
			andExpression.addExpression(expression);
		}	
	})* | "(" expression=TableSpec(){
		if(expression != null){
			if(andExpression == null){
    			andExpression = new AndExpression();
    		}
			andExpression.addExpression(expression);
		}
	} (JoinedTable(){
		if(expression != null){
			if(andExpression == null){
    			andExpression = new AndExpression();
    		}
			andExpression.addExpression(expression);
		}	
	})* ")") JoinCondition()){
		return andExpression;
	}
}

Expression TableSpec():
{
	Expression expression = null;
	Table table = null;
}
{
	(table = TableReference()
	| (["TABLE"] "("expression= FullSelectStatement() ")" [CorrelationClause()])){
		return expression;	
	}

}

void JoinCondition():
{}
{
     ("ON" SQLExpression())|("USING" "(" SQLExpression() ")")
}

Expression WhereClause():
{
	Expression expression;
}
{
    "WHERE" expression = SQLExpression(){
    	return expression;
    }
}


void CorrelationClause()  #void:
{}
{
	["AS"] <IDENTIFIER> [ColumnNames()]
}

void ColumnNames() :
{}
{
	"(" (<IDENTIFIER>|<S_COMMA_IDENTIFIER>) ("," (<IDENTIFIER>|<S_COMMA_IDENTIFIER>))* ")"
}

void GroupByClause() :
{}
{
    "GROUP" "BY" GroupByColumn() ("," GroupByColumn())*
    ["HAVING" SQLExpression()]
}

void GroupByColumn():
{}
{
      SQLSimpleExpression()
}

void OrderByClause():
{}
{
    "ORDER" "BY" OrderByColumn() [(<IDENTIFIER>|<S_COMMA_IDENTIFIER>)]
        ("," OrderByColumn() [(<IDENTIFIER>|<S_COMMA_IDENTIFIER>)])*
}


void OrderByColumn():
{}
{
      SQLSimpleExpression()
}


Expression SQLExpression() :
{
	OrExpression orExpression = null;
	AndExpression andExpression = null;
	Expression expression = null;
	Token token;
	int count = 0;
}
{
    expression = SQLAndExpression(){
    	orExpression = new OrExpression(expression);
    }
    ("OR"
		expression =  SQLAndExpression(){
			count++;
			orExpression.addExpression(expression);
		}
    )*{
    	if(count >0){
    		return orExpression;
    	}else{
    		return expression;
    	}
    }
}

Expression SQLAndExpression() :
{
	AndExpression andExpression = null;;
	Expression expression;
	Expression currentExpression;
}
{
    expression = SQLUnaryLogicalExpression()
    ( "AND" 
    	currentExpression = SQLUnaryLogicalExpression(){
    	
    	if(andExpression == null && currentExpression != null){
    		andExpression = new AndExpression(expression);
    	}
    	if(currentExpression != null){
    		andExpression.addExpression(currentExpression);
    	}
    })*{
    	if(andExpression != null){
    		return andExpression;
    	}else{
    		return expression;
    	}
    	
    }
}

Expression SQLUnaryLogicalExpression() :
{
	Expression expression = null;
	boolean not = false;
}
{
    LOOKAHEAD(2) ExistsClause()
|   (["NOT"{not = true;}]expression= SQLRelationalExpression()){
		return reverseExpression(not,expression);
}
}

Expression ExistsClause() :
{
	Expression expression = null;
	boolean not = false;
}
{
    ["NOT"{not = true;}] "EXISTS" "(" expression = FullSelectStatement() ")"{
    	return reverseExpression(not,expression);
    }
}

Expression SQLRelationalExpression()  #void:
{
	Expression expression = null;
	Expression otherExpression = null;
	String functionName = null;
	int function = 0;
}
{
    /* Only after looking past "(", Expression() and "," we will know that
       it is expression list */

    (LOOKAHEAD("(" SQLSimpleExpression() ",")
     "(" SQLExpressionList() ")"
|
    (expression = SQLSimpleExpression()))
	
    /* Lookahead(2) is required because of NOT IN,NOT BETWEEN and NOT LIKE */
   (    functionName = Relop(){
    		function =	Comparative.getComparisonByIdent(functionName);
    	}
    	otherExpression = SQLRelationalOperatorExpression()
   			{
   				if(expression == null ||  otherExpression == null){
   					return null;	
   				}
   				
		   		if(expression instanceof ColumnExpression && !(otherExpression instanceof ColumnExpression)){
		   			ColumnExpression colExp = (ColumnExpression)expression;
		   			ComparisonExpression comparativeExpression = new ComparisonExpression();
		   			comparativeExpression.setComparison(function);
		   			comparativeExpression.setExpression(otherExpression);
		   			colExp.setExpression(comparativeExpression);
		   			return colExp;
		   		}else if(!(expression instanceof ColumnExpression) && (otherExpression instanceof ColumnExpression)){
		   			ColumnExpression colExp = (ColumnExpression)otherExpression;
		   			ComparisonExpression comparativeExpression = new ComparisonExpression();
		   			comparativeExpression.setComparison(Comparative.exchangeComparison(function));
		   			comparativeExpression.setExpression(expression);
		   			colExp.setExpression(comparativeExpression);
		   			return colExp;
		   		}else{
		   			return null;
		   		}
   			}
        |  LOOKAHEAD(2) (otherExpression=SQLInClause(expression)
	        {
	        	return otherExpression;
	        })
        |  LOOKAHEAD(2) (otherExpression=SQLBetweenClause(expression){
        	return otherExpression;
        })
        |  LOOKAHEAD(2) (otherExpression = SQLLikeClause()){
        	if(expression instanceof ColumnExpression){
    			ColumnExpression colExp = (ColumnExpression)expression;
    			colExp.setExpression((ComparisonExpression)otherExpression);
    			return colExp;
    		}else{
    			return null;	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?