Java Code Examples for org.antlr.v4.tool.ast.GrammarAST#getFirstDescendantWithType()

The following examples show how to use org.antlr.v4.tool.ast.GrammarAST#getFirstDescendantWithType() . 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: LabelElementPair.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public LabelElementPair(Grammar g, GrammarAST label, GrammarAST element, int labelOp) {
    this.label = label;
    this.element = element;
    // compute general case for label type
    if ( element.getFirstDescendantWithType(tokenTypeForTokens)!=null ) {
        if ( labelOp==ANTLRParser.ASSIGN ) type = LabelType.TOKEN_LABEL;
        else type = LabelType.TOKEN_LIST_LABEL;
    }
    else if ( element.getFirstDescendantWithType(ANTLRParser.RULE_REF)!=null ) {
        if ( labelOp==ANTLRParser.ASSIGN ) type = LabelType.RULE_LABEL;
        else type = LabelType.RULE_LIST_LABEL;
    }

    // now reset if lexer and string
    if ( g.isLexer() ) {
        if ( element.getFirstDescendantWithType(ANTLRParser.STRING_LITERAL)!=null ) {
            if ( labelOp==ANTLRParser.ASSIGN ) type = LabelType.LEXER_STRING_LABEL;
        }
    }
}
 
Example 2
Source File: SemanticPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
boolean hasTypeOrMoreCommand(Rule r) {
	GrammarAST ast = r.ast;
	if (ast == null) {
		return false;
	}

	GrammarAST altActionAst = (GrammarAST)ast.getFirstDescendantWithType(ANTLRParser.LEXER_ALT_ACTION);
	if (altActionAst == null) {
		// the rule isn't followed by any commands
		return false;
	}

	// first child is the alt itself, subsequent are the actions
	for (int i = 1; i < altActionAst.getChildCount(); i++) {
		GrammarAST node = (GrammarAST)altActionAst.getChild(i);
		if (node.getType() == ANTLRParser.LEXER_ACTION_CALL) {
			if ("type".equals(node.getChild(0).getText())) {
				return true;
			}
		}
		else if ("more".equals(node.getText())) {
			return true;
		}
	}

	return false;
}