antlr.collections.AST Java Examples

The following examples show how to use antlr.collections.AST. 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: QueryTranslatorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private HqlParser parse(boolean filter) throws TokenStreamException {
	// Parse the query string into an HQL AST.
	final HqlParser parser = HqlParser.getInstance( hql );
	parser.setFilter( filter );

	LOG.debugf( "parse() - HQL: %s", hql );
	try {
		parser.statement();
	}
	catch (RecognitionException e) {
		throw new HibernateException( "Unexpected error parsing HQL", e );
	}

	final AST hqlAst = parser.getAST();
	parser.getParseErrorHandler().throwQueryException();

	final NodeTraverser walker = new NodeTraverser( new JavaConstantConverter( factory ) );
	walker.traverseDepthFirst( hqlAst );

	showHqlAst( hqlAst );

	return parser;
}
 
Example #2
Source File: QueryTranslatorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void generate(AST sqlAst) throws QueryException, RecognitionException {
	if ( sql == null ) {
		final SqlGenerator gen = new SqlGenerator( factory );
		gen.statement( sqlAst );
		sql = gen.getSQL();
		if ( LOG.isDebugEnabled() ) {
			LOG.debugf( "HQL: %s", hql );
			LOG.debugf( "SQL: %s", sql );
		}
		gen.getParseErrorHandler().throwQueryException();
		if ( collectedParameterSpecifications == null ) {
			collectedParameterSpecifications = gen.getCollectedParameters();
		}
		else {
			collectedParameterSpecifications.addAll( gen.getCollectedParameters() );
		}
	}
}
 
Example #3
Source File: CodeGenTreeWalker.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public final void attrScope(AST _t) throws RecognitionException {
	
	GrammarAST attrScope_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;
	
	try {      // for error handling
		AST __t8 = _t;
		GrammarAST tmp12_AST_in = (GrammarAST)_t;
		match(_t,SCOPE);
		_t = _t.getFirstChild();
		GrammarAST tmp13_AST_in = (GrammarAST)_t;
		match(_t,ID);
		_t = _t.getNextSibling();
		GrammarAST tmp14_AST_in = (GrammarAST)_t;
		match(_t,ACTION);
		_t = _t.getNextSibling();
		_t = __t8;
		_t = _t.getNextSibling();
	}
	catch (RecognitionException ex) {
		reportError(ex);
		if (_t!=null) {_t = _t.getNextSibling();}
	}
	_retTree = _t;
}
 
Example #4
Source File: StructVisitor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private XmlSchemaElement createXmlSchemaElement(AST memberNode,
                                                XmlSchemaType schemaType,
                                                Scope fqName) {
    // xmlschema:member
    XmlSchemaElement member = new XmlSchemaElement(schema, false);
    String memberName = memberNode.toString();
    member.setName(memberName);
    member.setSchemaType(schemaType);
    if (schemaType != null) {
        member.setSchemaTypeName(schemaType.getQName());
        if (schemaType.getQName().equals(ReferenceConstants.WSADDRESSING_TYPE)) {
            member.setNillable(true);
        }
    } else {
        wsdlVisitor.getDeferredActions().
            add(fqName, new StructDeferredAction(member));
    }
    return member;
}
 
Example #5
Source File: QueryNode.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public final OrderByClause getOrderByClause() {
	if ( orderByClause == null ) {
		orderByClause = locateOrderByClause();

		// if there is no order by, make one
		if ( orderByClause == null ) {
			LOG.debug( "getOrderByClause() : Creating a new ORDER BY clause" );
			orderByClause = (OrderByClause) getWalker().getASTFactory().create( SqlTokenTypes.ORDER, "ORDER" );

			// Find the WHERE; if there is no WHERE, find the FROM...
			AST prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.WHERE );
			if ( prevSibling == null ) {
				prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.FROM );
			}

			// Now, inject the newly built ORDER BY into the tree
			orderByClause.setNextSibling( prevSibling.getNextSibling() );
			prevSibling.setNextSibling( orderByClause );
		}
	}
	return orderByClause;
}
 
Example #6
Source File: AssignTokenTypesWalker.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public final void attrScope(AST _t) throws RecognitionException {
	
	GrammarAST attrScope_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;
	
	try {      // for error handling
		AST __t17 = _t;
		GrammarAST tmp13_AST_in = (GrammarAST)_t;
		match(_t,SCOPE);
		_t = _t.getFirstChild();
		GrammarAST tmp14_AST_in = (GrammarAST)_t;
		match(_t,ID);
		_t = _t.getNextSibling();
		GrammarAST tmp15_AST_in = (GrammarAST)_t;
		match(_t,ACTION);
		_t = _t.getNextSibling();
		_t = __t17;
		_t = _t.getNextSibling();
	}
	catch (RecognitionException ex) {
		reportError(ex);
		if (_t!=null) {_t = _t.getNextSibling();}
	}
	_retTree = _t;
}
 
Example #7
Source File: HqlSqlWalker.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private EntityJoinFromElement createEntityJoin(
		EntityPersister entityPersister,
		AST aliasNode,
		int joinType,
		AST propertyFetch,
		AST with) throws SemanticException {
	final String alias = aliasNode == null ? null : aliasNode.getText();
	LOG.debugf( "Creating entity-join FromElement [%s -> %s]", alias, entityPersister.getEntityName() );
	EntityJoinFromElement join = new EntityJoinFromElement(
			this,
			getCurrentFromClause(),
			entityPersister,
			JoinProcessor.toHibernateJoinType( joinType ),
			propertyFetch != null,
			alias
	);

	if ( with != null ) {
		handleWithFragment( join, with );
	}

	return join;
}
 
Example #8
Source File: HqlSqlWalker.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void validateMapPropertyExpression(AST node) throws SemanticException {
	try {
		FromReferenceNode fromReferenceNode = (FromReferenceNode) node;
		QueryableCollection collectionPersister = fromReferenceNode.getFromElement().getQueryableCollection();
		if ( !Map.class.isAssignableFrom( collectionPersister.getCollectionType().getReturnedClass() ) ) {
			throw new SemanticException( "node did not reference a map" );
		}
	}
	catch (SemanticException se) {
		throw se;
	}
	catch (Throwable t) {
		throw new SemanticException( "node did not reference a map" );
	}
}
 
Example #9
Source File: ConstrTypeSpecVisitor.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void visit(AST node) {
    // <constr_type_spec> ::= <struct_type>
    //                      | <union_type>
    //                      | <enum_type>

    Visitor visitor = null;

    if (StructVisitor.accept(node)) {
        visitor = new StructVisitor(getScope(), definition, schema, wsdlVisitor);
    }

    if (UnionVisitor.accept(node)) {
        visitor = new UnionVisitor(getScope(), definition, schema, wsdlVisitor);
    }

    if (EnumVisitor.accept(node)) {
        visitor = new EnumVisitor(getScope(), definition, schema, wsdlVisitor);
    }

    if (visitor != null) {
        visitor.visit(node);

        setSchemaType(visitor.getSchemaType());
        setCorbaType(visitor.getCorbaType());
    }
}
 
Example #10
Source File: DotNode.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void setPropertyNameAndPath(AST parent) {
	if ( isDotNode( parent ) ) {
		DotNode dotNode = (DotNode) parent;
		AST lhs = dotNode.getFirstChild();
		AST rhs = lhs.getNextSibling();
		propertyName = rhs.getText();
		propertyPath = propertyPath + "." + propertyName; // Append the new property name onto the unresolved path.
		dotNode.propertyPath = propertyPath;
		LOG.debugf( "Unresolved property path is now '%s'", dotNode.propertyPath );
	}
	else {
		LOG.debugf( "Terminal getPropertyPath = [%s]", propertyPath );
	}
}
 
Example #11
Source File: ProEval.java    From proparse with Eclipse Public License 2.0 5 votes vote down vote up
public final void displaystate(AST _t) throws RecognitionException {
	
	AST displaystate_AST_in = (_t == ASTNULL) ? null : (AST)_t;
	
	AST __t14 = _t;
	AST tmp10_AST_in = (AST)_t;
	match(_t,DISPLAY);
	_t = _t.getFirstChild();
	{
	_loop16:
	do {
		if (_t==null) _t=ASTNULL;
		if ((_t.getType()==Form_item)) {
			formitem(_t);
			_t = _retTree;
		}
		else {
			break _loop16;
		}
		
	} while (true);
	}
	AST tmp11_AST_in = (AST)_t;
	match(_t,PERIOD);
	_t = _t.getNextSibling();
	_t = __t14;
	_t = _t.getNextSibling();
	_retTree = _t;
}
 
Example #12
Source File: HqlSqlWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void lookupAlias(AST aliasRef)
		throws SemanticException {
	FromElement alias = currentFromClause.getFromElement( aliasRef.getText() );
	FromReferenceNode aliasRefNode = (FromReferenceNode) aliasRef;
	aliasRefNode.setFromElement( alias );
}
 
Example #13
Source File: SimpleCaseNode.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Type getDataType() {
	final AST expression = getFirstChild();
	// option is used to hold each WHEN/ELSE in turn
	AST option = expression.getNextSibling();
	while ( option != null ) {
		final AST result;
		if ( option.getType() == HqlSqlTokenTypes.WHEN ) {
			result = option.getFirstChild().getNextSibling();
		}
		else if ( option.getType() == HqlSqlTokenTypes.ELSE ) {
			result = option.getFirstChild();
		}
		else {
			throw new QueryException(
					"Unexpected node type :" +
							ASTUtil.getTokenTypeName( HqlSqlTokenTypes.class, option.getType() ) +
							"; expecting WHEN or ELSE"
			);
		}

		if ( SqlNode.class.isInstance( result ) ) {
			final Type nodeDataType = ( (SqlNode) result ).getDataType();
			if ( nodeDataType != null ) {
				return nodeDataType;
			}
		}

		option = option.getNextSibling();
	}
	return null;
}
 
Example #14
Source File: ArrayVisitor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public ArrayVisitor(Scope scope,
                    Definition defn,
                    XmlSchema schemaRef,
                    WSDLASTVisitor wsdlVisitor,
                    AST identifierNodeRef,
                    Scope fqName) {
    super(scope, defn, schemaRef, wsdlVisitor);
    setFullyQualifiedName(fqName);
    identifierNode = identifierNodeRef;
}
 
Example #15
Source File: OrderByFragmentParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected AST quotedString(AST ident) {
	/*
	 * Semantic action used during recognition of quoted strings (string literals)
	 */
	return getASTFactory().create( OrderByTemplateTokenTypes.IDENT, context.getDialect().quote( ident.getText() ) );
}
 
Example #16
Source File: DefineGrammarItemsWalker.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public final void rules(AST _t) throws RecognitionException {
	
	GrammarAST rules_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;
	
	try {      // for error handling
		{
		int _cnt38=0;
		_loop38:
		do {
			if (_t==null) _t=ASTNULL;
			if ((_t.getType()==RULE)) {
				rule(_t);
				_t = _retTree;
			}
			else {
				if ( _cnt38>=1 ) { break _loop38; } else {throw new NoViableAltException(_t);}
			}
			
			_cnt38++;
		} while (true);
		}
	}
	catch (RecognitionException ex) {
		if (inputState.guessing==0) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		} else {
		  throw ex;
		}
	}
	_retTree = _t;
}
 
Example #17
Source File: ASTIterator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public AST nextNode() {
	current = next;
	if ( next != null ) {
		AST nextSibling = next.getNextSibling();
		if ( nextSibling == null ) {
			next = pop();
		}
		else {
			next = nextSibling;
			down();
		}
	}
	return current;
}
 
Example #18
Source File: OrderByFragmentParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected AST quotedIdentifier(AST ident) {
	/*
	 * Semantic action used during recognition of quoted identifiers (quoted column names)
	 */
	final String columnName = context.getDialect().quote( '`' + ident.getText() + '`' );
	columnReferences.add( columnName );
	final String marker = '{' + columnName + '}';
	return getASTFactory().create( OrderByTemplateTokenTypes.IDENT, marker );
}
 
Example #19
Source File: ASTUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find the previous sibling in the parent for the given child.
 *
 * @param parent the parent node
 * @param child the child to find the previous sibling of
 *
 * @return the previous sibling of the child
 */
public static AST findPreviousSibling(AST parent, AST child) {
	AST prev = null;
	AST n = parent.getFirstChild();
	while ( n != null ) {
		if ( n == child ) {
			return prev;
		}
		prev = n;
		n = n.getNextSibling();
	}
	throw new IllegalArgumentException( "Child not found in parent!" );
}
 
Example #20
Source File: ASTParentsFirstIterator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private AST pop() {
	if ( parents.size() == 0 ) {
		return null;
	}
	else {
		return parents.removeFirst();
	}
}
 
Example #21
Source File: FromElementFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private FromElement createFromElement(EntityPersister entityPersister) {
	Joinable joinable = (Joinable) entityPersister;
	String text = joinable.getTableName();
	AST ast = createFromElement( text );
	FromElement element = (FromElement) ast;
	return element;
}
 
Example #22
Source File: CompilationUnitBuilder.java    From testability-explorer with Apache License 2.0 5 votes vote down vote up
public Visibility visibility(AST modifiers) {
  if (contains(modifiers, "private"))
    return Visibility.PRIVATE;
  if (contains(modifiers, "protected"))
    return Visibility.PROTECTED;
  if (contains(modifiers, "public"))
    return Visibility.PUBLIC;
  return Visibility.PACKAGE;
}
 
Example #23
Source File: StructVisitor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private boolean addRecursiveScopedName(AST identifierNode) {
    String structName = identifierNode.toString();
    Scope structScope = new Scope(getScope(), structName);

    ScopeNameCollection scopedNames = wsdlVisitor.getScopedNames();
    if (scopedNames.getScope(structScope) == null) {
        scopedNames.add(structScope);
        return true;
    }
    return false;
}
 
Example #24
Source File: ScopedNameVisitor.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected static boolean findSchemaType(Scope scope,
                                        Definition defn,
                                        XmlSchema schemaRef,
                                        AST node,
                                        WSDLASTVisitor wsdlVisitor,
                                        VisitorTypeHolder holder) {
    return findSchemaType(scope, defn, schemaRef, node, wsdlVisitor, holder, false);
}
 
Example #25
Source File: NodeFactory.java    From proparse with Eclipse Public License 2.0 5 votes vote down vote up
/** Used for synthetic node creation by the Antlr generated parser. */
@Override
public AST create(int type) {
	ProToken token = new ProToken(filenameList, type, "");
	switch(type) {
		case NodeTypes.Field_ref:
			return new FieldRefNode(token);
		case NodeTypes.Program_root:
			return new ProgramRootNode(token);
		default:
			return new JPNode(token);
	}
}
 
Example #26
Source File: IntoClause.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void initializeColumns() {
	AST propertySpec = getFirstChild();
	List types = new ArrayList();
	visitPropertySpecNodes( propertySpec.getFirstChild(), types );
	this.types = ArrayHelper.toTypeArray( types );
	columnSpec = columnSpec.substring( 0, columnSpec.length() - 2 );
}
 
Example #27
Source File: HqlSqlWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void visit(AST node) {
	// todo : currently expects that the individual with expressions apply to the same sql table join.
	//      This may not be the case for joined-subclass where the property values
	//      might be coming from different tables in the joined hierarchy.  At some
	//      point we should expand this to support that capability.  However, that has
	//      some difficulties:
	//          1) the biggest is how to handle ORs when the individual comparisons are
	//              linked to different sql joins.
	//          2) here we would need to track each comparison individually, along with
	//              the join alias to which it applies and then pass that information
	//              back to the FromElement so it can pass it along to the JoinSequence

	if ( node instanceof DotNode ) {
		DotNode dotNode = ( DotNode ) node;
		FromElement fromElement = dotNode.getFromElement();
		if ( referencedFromElement != null ) {
			if ( fromElement != referencedFromElement ) {
				throw new HibernateException( "with-clause referenced two different from-clause elements" );
			}
		}
		else {
			referencedFromElement = fromElement;
			joinAlias = extractAppliedAlias( dotNode );
			// todo : temporary
			//      needed because currently persister is the one that
			//      creates and renders the join fragments for inheritence
			//      hierarchies...
			if ( !joinAlias.equals( referencedFromElement.getTableAlias() ) ) {
				throw new InvalidWithClauseException( "with clause can only reference columns in the driving table" );
			}
		}
	}
}
 
Example #28
Source File: ASTUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the last sibling of 'a'.
 *
 * @param a The sibling.
 *
 * @return The last sibling of 'a'.
 */
private static AST getLastSibling(AST a) {
	AST last = null;
	while ( a != null ) {
		last = a;
		a = a.getNextSibling();
	}
	return last;
}
 
Example #29
Source File: DefineGrammarItemsWalker.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public final void finallyClause(AST _t) throws RecognitionException {
	
	GrammarAST finallyClause_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;
	
	try {      // for error handling
		AST __t93 = _t;
		GrammarAST tmp56_AST_in = (GrammarAST)_t;
		match(_t,LITERAL_finally);
		_t = _t.getFirstChild();
		GrammarAST tmp57_AST_in = (GrammarAST)_t;
		match(_t,ACTION);
		_t = _t.getNextSibling();
		_t = __t93;
		_t = _t.getNextSibling();
		if ( inputState.guessing==0 ) {
			trackInlineAction(tmp57_AST_in);
		}
	}
	catch (RecognitionException ex) {
		if (inputState.guessing==0) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		} else {
		  throw ex;
		}
	}
	_retTree = _t;
}
 
Example #30
Source File: AssignTokenTypesWalker.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public final void tokensSpec(AST _t) throws RecognitionException {
	
	GrammarAST tokensSpec_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;
	
	try {      // for error handling
		AST __t36 = _t;
		GrammarAST tmp12_AST_in = (GrammarAST)_t;
		match(_t,TOKENS);
		_t = _t.getFirstChild();
		{
		int _cnt38=0;
		_loop38:
		do {
			if (_t==null) _t=ASTNULL;
			if ((_t.getType()==ASSIGN||_t.getType()==TOKEN_REF)) {
				tokenSpec(_t);
				_t = _retTree;
			}
			else {
				if ( _cnt38>=1 ) { break _loop38; } else {throw new NoViableAltException(_t);}
			}
			
			_cnt38++;
		} while (true);
		}
		_t = __t36;
		_t = _t.getNextSibling();
	}
	catch (RecognitionException ex) {
		reportError(ex);
		if (_t!=null) {_t = _t.getNextSibling();}
	}
	_retTree = _t;
}