Java Code Examples for org.antlr.v4.runtime.tree.ParseTree#getParent()

The following examples show how to use org.antlr.v4.runtime.tree.ParseTree#getParent() . 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: Step.java    From yauaa with Apache License 2.0 6 votes vote down vote up
protected final ParseTree up(ParseTree tree) {
    if (tree == null) {
        return null;
    }

    ParseTree parent = tree.getParent();

    // Needed because of the way the ANTLR rules have been defined.
    if (parent instanceof UserAgentParser.ProductNameContext ||
        parent instanceof UserAgentParser.ProductVersionContext ||
        parent instanceof UserAgentParser.ProductVersionWithCommasContext
        ) {
        return up(parent);
    }
    return parent;
}
 
Example 2
Source File: ANTLRUtils.java    From proleap-vb6-parser with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends ASGElement> T findParent(final Class<? extends ASGElement> type, final ParseTree from,
		final ASGElementRegistry asgElementRegistry) {
	T result = null;

	ParseTree currentCtx = from;

	while (result == null && currentCtx != null) {
		currentCtx = currentCtx.getParent();

		final ASGElement asgElement = asgElementRegistry.getASGElement(currentCtx);

		if (asgElement != null && type.isAssignableFrom(asgElement.getClass())) {
			result = (T) asgElement;
		}
	}

	return result;
}
 
Example 3
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Return the index 0..n-1 of t as child of t.parent.
 *  If t is index 0, always return 0.
 *  If t is a repeated subtree root and index within
 *  sibling list > 0, return CHILD_INDEX_LIST_ELEMENT.
 *  In all other cases, return the actual index of t. That means for a
 *  sibling list starting at child index 5, the first sibling will return
 *  5 but 2nd and beyond in list will return CHILD_INDEX_LIST_ELEMENT.
 */
public static int getChildIndexOrListMembership(ParseTree t) {
	if ( t==null ) return -1;
	ParseTree parent = t.getParent();
	if ( parent==null ) {
		return -1;
	}
	// we know we have a parent now
	// check to see if we are 2nd or beyond element in a sibling list
	if ( t instanceof ParserRuleContext ) {
		List<ParserRuleContext> siblings =
			((ParserRuleContext)parent).getRuleContexts(((ParserRuleContext)t).getClass());
		if ( siblings.size()>1 && siblings.indexOf(t)>0 ) {
			return CHILD_INDEX_REPEATED_ELEMENT;
		}
	}
	// check to see if we are 2nd or beyond repeated token
	if ( t instanceof TerminalNode ) {
		List<TerminalNode> repeatedTokens =
			((ParserRuleContext) parent).getTokens(((TerminalNode) t).getSymbol().getType());
		if ( repeatedTokens.size()>1 && repeatedTokens.indexOf(t)>0 ) {
			return CHILD_INDEX_REPEATED_ELEMENT;
		}
	}

	return getChildIndex(t);
}
 
Example 4
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static int getChildIndex(ParseTree t) {
	if ( t==null ) return -1;
	ParseTree parent = t.getParent();
	if ( parent==null ) {
		return -1;
	}
	// Figure out which child index t is of parent
	for (int i = 0; i<parent.getChildCount(); i++) {
		if ( parent.getChild(i)==t ) {
			return i;
		}
	}
	return -1;
}
 
Example 5
Source File: Cache.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public ParseTree findNearestAncestorBlock(ParseTree node) {
    boolean isBlock =
            node instanceof SwiftParser.Top_levelContext ||
            node instanceof SwiftParser.Code_blockContext ||
            node instanceof SwiftParser.Closure_expressionContext ||
            node instanceof SwiftParser.Explicit_closure_expressionContext ||
            isStructureBlock(node);
    if(isBlock) return node;
    if(node == null || node.getParent() == null || node.getParent() == node) return null;
    return findNearestAncestorBlock(node.getParent());
}
 
Example 6
Source File: Cache.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public ParseTree findNearestAncestorFunctionBlock(ParseTree node) {
    boolean isBlock =
            node instanceof SwiftParser.Top_levelContext ||
            node instanceof SwiftParser.Function_bodyContext ||
            node instanceof SwiftParser.Initializer_bodyContext;
    if(isBlock) return node;
    if(node == null || node.getParent() == null || node.getParent() == node) return null;
    return findNearestAncestorFunctionBlock(node.getParent());
}
 
Example 7
Source File: ParsedNode.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
public IParsedNode[] getParents() {
	List<IParsedNode> nodes = new ArrayList<>();
	if (this.item == null) {
		return nodes.toArray(new IParsedNode[0]);
	}
	ParseTree parseTreeItem = this.item.getParent();
	int i = 0;
	while (parseTreeItem != null) {
		i++;
		nodes.add(new ParsedNode(parseTreeItem, i, 1, -1));
		parseTreeItem = parseTreeItem.getParent();
	}

	return nodes.toArray(new IParsedNode[0]);
}
 
Example 8
Source File: ParsedNode.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IParsedNode getControlFlowParent() {
	if (this.item == null) {
		return new ParsedNode(null);
	}
	ParseTree parent1 = item.getParent();
	while (parent1 != null) {
		if (parent1 instanceof Cfl_statementContext) {
			return new ParsedNode(parent1);
		}
		parent1 = parent1.getParent();
	}
	return new ParsedNode(null);
}
 
Example 9
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ParseTree getAncestorWithType(ParseTree t, Class<? extends ParseTree> clazz) {
	if ( t==null || clazz==null || t.getParent()==null ) return null;
	Tree p = t.getParent();
	while ( p!=null ) {
		if ( p.getClass()==clazz ) return (ParseTree)p;
		p = p.getParent();
	}
	return null;
}
 
Example 10
Source File: UserAgentTreeFlattener.java    From yauaa with Apache License 2.0 4 votes vote down vote up
public String calculatePath(PathType type, boolean fakeChild) {
    ParseTree node = ctx;
    path = name;
    if (node == null) {
        return path;
    }
    State parentState = null;

    while (parentState == null) {
        node = node.getParent();
        if (node == null) {
            return path;
        }
        parentState = state.get(node);
    }

    long counter = 0;
    switch (type) {
        case CHILD:
            if (!fakeChild) {
                parentState.child++;
            }
            counter = parentState.child;
            break;
        case COMMENT:
            if (!fakeChild) {
                parentState.comment++;
            }
            counter = parentState.comment;
            break;
        case VERSION:
            if (!fakeChild) {
                parentState.version++;
            }
            counter = parentState.version;
            break;
        default:
    }

    this.path = parentState.path + ".(" + counter + ')' + name;

    return this.path;
}
 
Example 11
Source File: Cache.java    From swift-js-transpiler with MIT License 4 votes vote down vote up
public CacheBlockAndObject findNearestAncestorStructure(ParseTree node) {
    if(isStructureBlock(node)) return getClassDefinition(node);
    if(node == null || node.getParent() == null || node.getParent() == node) return null;
    return findNearestAncestorStructure(node.getParent());
}
 
Example 12
Source File: InlineRuleAction.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
	PsiElement el = MyActionUtils.getSelectedPsiElement(e);
	if ( el==null ) return;

	final String ruleName = el.getText();

	final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
	if ( psiFile==null ) return;

	final Project project = e.getProject();

	Editor editor = e.getData(PlatformDataKeys.EDITOR);
	if ( editor==null ) return;
	final Document doc = editor.getDocument();

	String grammarText = psiFile.getText();
	ParsingResult results = ParsingUtils.parseANTLRGrammar(grammarText);
	Parser parser = results.parser;
	ParseTree tree = results.tree;

	final CommonTokenStream tokens = (CommonTokenStream) parser.getTokenStream();

	// find all parser and lexer rule refs
	final List<TerminalNode> rrefNodes = RefactorUtils.getAllRuleRefNodes(parser, tree, ruleName);
	if ( rrefNodes==null ) return;

	// find rule def
	ParseTree ruleDefNameNode = RefactorUtils.getRuleDefNameNode(parser, tree, ruleName);
	if ( ruleDefNameNode==null ) return;

	// identify rhs of rule
	final ParserRuleContext ruleDefNode = (ParserRuleContext) ruleDefNameNode.getParent();
	String ruleText_ = RefactorUtils.getRuleText(tokens, ruleDefNode);

	// if rule has outermost alt, must add (...) around insertion
	// Look for ruleBlock, lexerRuleBlock
	if ( RefactorUtils.ruleHasMultipleOutermostAlts(parser, ruleDefNode) ) {
		ruleText_ = "("+ruleText_+")";
	}
	final String ruleText = ruleText_; // we ref from inner class; requires final

	// replace rule refs with rule text
	WriteCommandAction setTextAction = new WriteCommandAction(project) {
		@Override
		protected void run(final Result result) throws Throwable {
			// do in a single action so undo works in one go
			replaceRuleRefs(doc,tokens,ruleName,rrefNodes,ruleText);
		}
	};
	setTextAction.execute();
}