Java Code Examples for antlr.collections.AST#setText()

The following examples show how to use antlr.collections.AST#setText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LiteralProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void processConstant(AST constant, boolean resolveIdent) throws SemanticException {
	// If the constant is an IDENT, figure out what it means...
	boolean isIdent = ( constant.getType() == IDENT || constant.getType() == WEIRD_IDENT );
	if ( resolveIdent && isIdent && isAlias( constant.getText() ) ) {
		// IDENT is a class alias in the FROM.
		IdentNode ident = (IdentNode) constant;
		// Resolve to an identity column.
		ident.resolve( false, true );
	}
	else {
		// IDENT might be the name of a class.
		Queryable queryable = walker.getSessionFactoryHelper().findQueryableUsingImports( constant.getText() );
		if ( isIdent && queryable != null ) {
			constant.setText( queryable.getDiscriminatorSQLValue() );
		}
		// Otherwise, it's a literal.
		else {
			processLiteral( constant );
		}
	}
}
 
Example 2
Source File: ColumnHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates the scalar column AST nodes for a given array of SQL columns
 */
public static void generateScalarColumns(HqlSqlWalkerNode node, String[] sqlColumns, int i) {
	if ( sqlColumns.length == 1 ) {
		generateSingleScalarColumn( node, i );
	}
	else {
		ASTFactory factory = node.getASTFactory();
		AST n = node;
		n.setText( sqlColumns[0] );	// Use the DOT node to emit the first column name.
		// Create the column names, folled by the column aliases.
		for ( int j = 0; j < sqlColumns.length; j++ ) {
			if ( j > 0 ) {
				n = ASTUtil.createSibling( factory, SqlTokenTypes.SQL_TOKEN, sqlColumns[j], n );
			}
			n = ASTUtil.createSibling( factory, SqlTokenTypes.SELECT_COLUMNS, " as " + NameGenerator.scalarName( i, j ), n );
		}
	}
}
 
Example 3
Source File: LiteralProcessor.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void processConstant(AST constant, boolean resolveIdent) throws SemanticException {
	// If the constant is an IDENT, figure out what it means...
	boolean isIdent = ( constant.getType() == IDENT || constant.getType() == WEIRD_IDENT );
	if ( resolveIdent && isIdent && isAlias( constant.getText() ) ) { // IDENT is a class alias in the FROM.
		IdentNode ident = ( IdentNode ) constant;
		// Resolve to an identity column.
		ident.resolve(false, true);
	}
	else {	// IDENT might be the name of a class.
		Queryable queryable = walker.getSessionFactoryHelper().findQueryableUsingImports( constant.getText() );
		if ( isIdent && queryable != null ) {
			constant.setText( queryable.getDiscriminatorSQLValue() );
		}
		// Otherwise, it's a literal.
		else {
			processLiteral( constant );
		}
	}
}
 
Example 4
Source File: ColumnHelper.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Generates the scalar column AST nodes for a given array of SQL columns
 */
public static void generateScalarColumns(HqlSqlWalkerNode node, String sqlColumns[], int i) {
	if ( sqlColumns.length == 1 ) {
		generateSingleScalarColumn( node, i );
	}
	else {
		ASTFactory factory = node.getASTFactory();
		AST n = node;
		n.setText( sqlColumns[0] );	// Use the DOT node to emit the first column name.
		// Create the column names, folled by the column aliases.
		for ( int j = 0; j < sqlColumns.length; j++ ) {
			if ( j > 0 ) {
				n = ASTUtil.createSibling( factory, SqlTokenTypes.SQL_TOKEN, sqlColumns[j], n );
			}
			n = ASTUtil.createSibling( factory, SqlTokenTypes.SELECT_COLUMNS, " as " + NameGenerator.scalarName( i, j ), n );
		}
	}
}
 
Example 5
Source File: QueryTranslatorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void handleDotStructure(AST dotStructureRoot) {
	final String expression = ASTUtil.getPathText( dotStructureRoot );
	final Object constant = ReflectHelper.getConstantValue( expression, factory );
	if ( constant != null ) {
		dotStructureRoot.setFirstChild( null );
		dotStructureRoot.setType( HqlTokenTypes.JAVA_CONSTANT );
		dotStructureRoot.setText( expression );
	}
}
 
Example 6
Source File: LiteralProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void processLiteral(AST constant) {
	String replacement = (String) walker.getTokenReplacements().get( constant.getText() );
	if ( replacement != null ) {
		if ( LOG.isDebugEnabled() ) {
			LOG.debugf( "processConstant() : Replacing '%s' with '%s'", constant.getText(), replacement );
		}
		constant.setText( replacement );
	}
}
 
Example 7
Source File: LiteralProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void processNumeric(AST literal) {
	if ( literal.getType() == NUM_INT
			|| literal.getType() == NUM_LONG
			|| literal.getType() == NUM_BIG_INTEGER ) {
		literal.setText( determineIntegerRepresentation( literal.getText(), literal.getType() ) );
	}
	else if ( literal.getType() == NUM_FLOAT
			|| literal.getType() == NUM_DOUBLE
			|| literal.getType() == NUM_BIG_DECIMAL ) {
		literal.setText( determineDecimalRepresentation( literal.getText(), literal.getType() ) );
	}
	else {
		LOG.unexpectedLiteralTokenType( literal.getType() );
	}
}
 
Example 8
Source File: QueryTranslatorImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void handleDotStructure(AST dotStructureRoot) {
	String expression = ASTUtil.getPathText( dotStructureRoot );
	Object constant = ReflectHelper.getConstantValue( expression );
	if ( constant != null ) {
		dotStructureRoot.setFirstChild( null );
		dotStructureRoot.setType( HqlTokenTypes.JAVA_CONSTANT );
		dotStructureRoot.setText( expression );
	}
}
 
Example 9
Source File: LiteralProcessor.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void processBoolean(AST constant) {
	// TODO: something much better - look at the type of the other expression!
	// TODO: Have comparisonExpression and/or arithmeticExpression rules complete the resolution of boolean nodes.
	String replacement = ( String ) walker.getTokenReplacements().get( constant.getText() );
	if ( replacement != null ) {
		constant.setText( replacement );
	}
	else {
		boolean bool = "true".equals( constant.getText().toLowerCase() );
		Dialect dialect = walker.getSessionFactoryHelper().getFactory().getDialect();
		constant.setText( dialect.toBooleanValueString(bool) );
	}
}
 
Example 10
Source File: LiteralProcessor.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void processLiteral(AST constant) {
	String replacement = ( String ) walker.getTokenReplacements().get( constant.getText() );
	if ( replacement != null ) {
		if ( log.isDebugEnabled() ) {
			log.debug( "processConstant() : Replacing '" + constant.getText() + "' with '" + replacement + "'" );
		}
		constant.setText( replacement );
	}
}
 
Example 11
Source File: LiteralProcessor.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void processNumeric(AST literal) {
	if ( literal.getType() == NUM_INT || literal.getType() == NUM_LONG ) {
		literal.setText( determineIntegerRepresentation( literal.getText(), literal.getType() ) );
	}
	else if ( literal.getType() == NUM_FLOAT || literal.getType() == NUM_DOUBLE ) {
		literal.setText( determineDecimalRepresentation( literal.getText(), literal.getType() ) );
	}
	else {
		log.warn( "Unexpected literal token type [" + literal.getType() + "] passed for numeric processing" );
	}
}
 
Example 12
Source File: LiteralProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void processBoolean(AST constant) {
	String replacement = (String) walker.getTokenReplacements().get( constant.getText() );
	if ( replacement != null ) {
		constant.setText( replacement );
	}
}
 
Example 13
Source File: LiteralProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void processNull(AST constant) {
	constant.setText( "null" );
}
 
Example 14
Source File: HqlParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
	 * Returns an equivalent tree for (NOT (a relop b) ), for example:<pre>
	 * (NOT (GT a b) ) => (LE a b)
	 * </pre>
	 *
	 * @param x The sub tree to transform, the parent is assumed to be NOT.
	 * @return AST - The equivalent sub-tree.
	 */
	public AST negateNode(AST x) {
		//TODO: switch statements are always evil! We already had bugs because 
		//      of forgotten token types. Use polymorphism for this!
		switch ( x.getType() ) {
			case OR:
				x.setType(AND);
				x.setText("{and}");
				negateNode( x.getFirstChild() );
				negateNode( x.getFirstChild().getNextSibling() );
				return x;
			case AND:
				x.setType(OR);
				x.setText("{or}");
				negateNode( x.getFirstChild() );
				negateNode( x.getFirstChild().getNextSibling() );
				return x;
			case EQ:
				x.setType( NE );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (EQ a b) ) => (NE a b)
			case NE:
				x.setType( EQ );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (NE a b) ) => (EQ a b)
			case GT:
				x.setType( LE );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (GT a b) ) => (LE a b)
			case LT:
				x.setType( GE );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (LT a b) ) => (GE a b)
			case GE:
				x.setType( LT );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (GE a b) ) => (LT a b)
			case LE:
				x.setType( GT );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (LE a b) ) => (GT a b)
			case LIKE:
				x.setType( NOT_LIKE );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (LIKE a b) ) => (NOT_LIKE a b)
			case NOT_LIKE:
				x.setType( LIKE );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (NOT_LIKE a b) ) => (LIKE a b)
			case IN:
				x.setType( NOT_IN );
				x.setText( "{not}" + x.getText() );
				return x;
			case NOT_IN:
				x.setType( IN );
				x.setText( "{not}" + x.getText() );
				return x;
			case IS_NULL:
				x.setType( IS_NOT_NULL );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (IS_NULL a b) ) => (IS_NOT_NULL a b)
			case IS_NOT_NULL:
				x.setType( IS_NULL );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (IS_NOT_NULL a b) ) => (IS_NULL a b)
			case BETWEEN:
				x.setType( NOT_BETWEEN );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (BETWEEN a b) ) => (NOT_BETWEEN a b)
			case NOT_BETWEEN:
				x.setType( BETWEEN );
				x.setText( "{not}" + x.getText() );
				return x;	// (NOT (NOT_BETWEEN a b) ) => (BETWEEN a b)
/* This can never happen because this rule will always eliminate the child NOT.
			case NOT:
				return x.getFirstChild();			// (NOT (NOT x) ) => (x)
*/
			default:
				return super.negateNode( x );		// Just add a 'not' parent.
		}
	}
 
Example 15
Source File: SequenceVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void visit(AST seq) {
    // <sequence_type> ::= "sequence" "<" <simple_type_spec> "," <positive_int_const> ">"
    //                   | "sequence" "<" <simple_type_spec> ">"


    AST simpleTypeSpecNode = seq.getFirstChild();
    // REVISIT: TypesUtils.getPrimitiveCorbaTypeNameNode should be renamed
    // to something more suitable and should be made more general.
    AST boundNode = TypesUtils.getCorbaTypeNameNode(simpleTypeSpecNode);
    //get chance to check if bound is symbol name which defined as const,
    //if so, replace the symbol name with defined const
    if (boundNode != null) {
        String constValue = TypesUtils.getConstValueByName(boundNode, typeMap);
        if (constValue != null) {
            boundNode.setText(constValue);
        }
    }

    SimpleTypeSpecVisitor visitor = new SimpleTypeSpecVisitor(new Scope(getScope(), identifierNode),
                                                              definition,
                                                              schema,
                                                              wsdlVisitor,
                                                              null);
    visitor.visit(simpleTypeSpecNode);

    XmlSchemaType stype = visitor.getSchemaType();
    CorbaType ctype = visitor.getCorbaType();
    Scope fullyQualifiedName = visitor.getFullyQualifiedName();


    long bound = -1;
    if (boundNode != null) {
        bound = Long.parseLong(boundNode.toString());
    }

    Scope scopedName = null;
    if (identifierNode == null) {
        // anonymous type
        scopedName = TypesUtils.generateAnonymousScopedName(getScope(), schema);
    } else {
        scopedName = new Scope(getScope(), identifierNode);
    }

    XmlSchemaType schemaType = null;

    // According to CORBA Binding for WSDL specification,
    // idl:sequence<octet> maps to xs:base64Binary by default.
    //
    // wsdlVisitor.getSequenceOctetType() returns the XmlSchema type
    // that idl:sequence<octet> should map to, as specified by the
    // -s command line option or the default type xsd:base64Binary.
    //
    if (stype != null) {
        if (!stype.getQName().equals(Constants.XSD_UNSIGNEDBYTE)) {
            schemaType = generateSchemaType(stype, scopedName, bound, fullyQualifiedName);
        } else {
            schemaType = wsdlVisitor.getSequenceOctetType();
        }
    } else {
        schemaType = generateSchemaType(null, scopedName, bound, fullyQualifiedName);
    }

    CorbaType corbaType = null;
    if (identifierNode == null) {
        corbaType = generateCorbaAnonsequence(ctype,
                                              schemaType,
                                              scopedName,
                                              bound,
                                              fullyQualifiedName);
    } else {
        corbaType = generateCorbaSequence(ctype,
                                          schemaType,
                                          scopedName,
                                          bound,
                                          fullyQualifiedName);
    }


    setSchemaType(schemaType);
    setCorbaType(corbaType);
    setFullyQualifiedName(fullyQualifiedName);
}