org.antlr.v4.runtime.tree.Trees Java Examples

The following examples show how to use org.antlr.v4.runtime.tree.Trees. 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: VbParseTestRunnerImpl.java    From proleap-vb6-parser with MIT License 6 votes vote down vote up
protected void doCompareParseTree(final File treeFile, final StartRuleContext startRule,
		final VisualBasic6Parser parser) throws IOException {

	final String treeFileData = Files.readString(treeFile.toPath(), StandardCharsets.UTF_8);

	if (treeFileData != null && !treeFileData.isEmpty()) {
		LOG.info("Comparing parse tree with file {}.", treeFile.getName());

		final String inputFileTree = Trees.toStringTree(startRule, parser);
		final String cleanedInputFileTree = io.proleap.vb6.util.VbTestStringUtils.cleanFileTree(inputFileTree);
		final String cleanedTreeFileData = io.proleap.vb6.util.VbTestStringUtils.cleanFileTree(treeFileData);

		assertEquals(cleanedTreeFileData, cleanedInputFileTree);
	} else {
		LOG.info("Ignoring empty parse tree file {}.", treeFile.getName());
	}
}
 
Example #2
Source File: Tool.java    From bookish with MIT License 5 votes vote down vote up
/** check all references to entities in all files
 * track list of entity refs for each paragraph
 */
public void verifyAllEntityRefs(Artifact artifact) {
	for (ChapDocInfo doc : artifact.docs) {
		Collection<ParseTree> refNodes =
			XPath.findAll(doc.tree, "//REF", doc.parser);
		for (ParseTree t : refNodes) {
			String label = stripQuotes(t.getText());
			EntityDef d = doc.getEntity(label);
			if ( d==null ) {
				Token tok = ((TerminalNode) t).getSymbol();
				System.err.printf("%s line %d:%d unknown label '%s'\n",
				                  doc.getSourceName(),
				                  tok.getLine(),
				                  tok.getCharPositionInLine(),
				                  label);
				continue;
			}
			d.refCount++;
			List<? extends Tree> ancestors = Trees.getAncestors(t);
			Tree p = ParrtCollections.findLast(ancestors, (Tree a) -> a instanceof BookishParser.ParagraphContext);
			// track all side items ref'd within this paragraph
			if ( d.refCount==1 && p!=null && d.isSideItem() ) {
				// for first ref, annotate paragraph if this ref is inside a paragraph
				((BookishParser.ParagraphContext)p).entitiesRefd.add(d);
			}
		}
	}
}
 
Example #3
Source File: VisitSiblingLists.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void enterEveryRule(ParserRuleContext ctx) {
	// Find sibling lists that are children of this parent node
	Set<Class> completed = new HashSet<>(); // only count sibling list for each subtree type once
	for (int i = 0; i<ctx.getChildCount(); i++) {
		ParseTree child = ctx.getChild(i);

		if ( completed.contains(child.getClass()) ) continue; // avoid counting repeatedly
		completed.add(child.getClass());
		if ( child instanceof TerminalNode ) continue; // tokens are separators at most not siblings

		// found subtree child
		List<? extends ParserRuleContext> siblings =
			ctx.getRuleContexts(((ParserRuleContext) child).getClass());
		if ( siblings.size()>1 ) { // we found a list
			// check for separator by looking between first two siblings (assume all are same)
			ParserRuleContext first = siblings.get(0);
			ParserRuleContext second = siblings.get(1);
			List<Tree> children = Trees.getChildren(ctx);

			int firstIndex = children.indexOf(first);
			int secondIndex = children.indexOf(second);

			if ( firstIndex+1 == secondIndex ) continue; // nothing between first and second so no separator

			ParseTree between = ctx.getChild(firstIndex+1);
			if ( between instanceof TerminalNode ) { // is it a token?
				Token separator = ((TerminalNode) between).getSymbol();
				visitNonSingletonWithSeparator(ctx, siblings, separator);
			}
		}
	}
}
 
Example #4
Source File: VisitSiblingLists.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static List<Tree> getSeparators(ParserRuleContext ctx, List<? extends ParserRuleContext> siblings) {
	ParserRuleContext first = siblings.get(0);
	ParserRuleContext last = siblings.get(siblings.size()-1);
	int start = BuffUtils.indexOf(ctx, first);
	int end = BuffUtils.indexOf(ctx, last);
	List<Tree> elements = Trees.getChildren(ctx).subList(start, end+1);
	return BuffUtils.filter(elements, c -> c instanceof TerminalNode);
}
 
Example #5
Source File: TreeUtils.java    From proleap-vb6-parser with MIT License 5 votes vote down vote up
/**
 * @see org.antlr.v4.runtime.tree.Trees.toStringTree(Tree, List<String>)
 */
public static String toStringTree(final Tree t, final List<String> ruleNames, final int depth) {
	String s = Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false);

	if (t.getChildCount() == 0) {
		return s;
	}

	final StringBuilder buf = new StringBuilder();

	if (depth > 0) {
		buf.append(NEWLINE);
	}

	buf.append(indent(depth));
	buf.append("(");
	s = Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false);
	buf.append(s);
	buf.append(' ');

	for (int i = 0; i < t.getChildCount(); i++) {
		if (i > 0) {
			buf.append(' ');
		}

		buf.append(toStringTree(t.getChild(i), ruleNames, depth + 1));
	}

	buf.append(")");

	return buf.toString();
}
 
Example #6
Source File: AltLabelTextProvider.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String getText(Tree node) {
	if ( node instanceof PreviewInterpreterRuleContext) {
		PreviewInterpreterRuleContext inode = (PreviewInterpreterRuleContext)node;
		Rule r = g.getRule(inode.getRuleIndex());
		String[] altLabels = getAltLabels(r);
		String name = r.name;
		int outerAltNum = inode.getOuterAltNum();
		if ( altLabels!=null ) {
			if ( outerAltNum>=0 && outerAltNum<altLabels.length ) {
				return name+":"+altLabels[outerAltNum];
			}
			else {
				return name;
			}
		}
		else if ( r.getOriginalNumberOfAlts()>1 ) {
			return name + ":" +outerAltNum;
		}
		else {
			return name; // don't display an alternative number if there's only one
		}
	} else if (node instanceof TerminalNode) {
		return getLabelForToken( ((TerminalNode)node).getSymbol() );
	}
	return Trees.getNodeText(node, Arrays.asList(parser.getRuleNames()));
}
 
Example #7
Source File: ParsingUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ParseTree getParseTreeNodeWithToken(ParseTree tree, Token token) {
    if ( tree==null || token==null ) {
        return null;
    }

    Collection<ParseTree> tokenNodes = Trees.findAllTokenNodes(tree, token.getType());
    for (ParseTree t : tokenNodes) {
        TerminalNode tnode = (TerminalNode)t;
        if ( tnode.getPayload() == token ) {
            return tnode;
        }
    }
    return null;
}
 
Example #8
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Given a token index into buffer, find surrounding rule then return
	 *  char position of start of next rule.
	 */
	public static int getCharIndexOfNextRuleStart(ParserRuleContext tree, int tokenIndex) {
		final ParserRuleContext selNode =
			Trees.getRootOfSubtreeEnclosingRegion(tree, tokenIndex, tokenIndex);
		final ParserRuleContext ruleRoot = (ParserRuleContext)
			getAncestorWithType(selNode, ANTLRv4Parser.RuleSpecContext.class);

		return ruleRoot.getStop().getStopIndex() + 2; // insert after '\n' following ';'

//		int ruleIndex = childIndexOf(ruleRoot.getParent(), ruleRoot);
//		ParserRuleContext nextRuleRoot = (ParserRuleContext)ruleRoot.getParent().getChild(ruleIndex+1);
//		if ( nextRuleRoot==null ) { // this rule must be last in grammar; put after ';' of this rule
//		}
//		return nextRuleRoot.getStart().getStartIndex();
	}
 
Example #9
Source File: GrammarParserInterpreter.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** Return a list of parse trees, one for each alternative in a decision
 *  given the same input.
 *
 *  Very similar to {@link #getAllPossibleParseTrees} except
 *  that it re-parses the input for every alternative in a decision,
 *  not just the ambiguous ones (there is no alts parameter here).
 *  This method also tries to reduce the size of the parse trees
 *  by stripping away children of the tree that are completely out of range
 *  of startIndex..stopIndex. Also, because errors are expected, we
 *  use a specialized error handler that more or less bails out
 *  but that also consumes the first erroneous token at least. This
 *  ensures that an error node will be in the parse tree for display.
 *
 *  NOTES:
   // we must parse the entire input now with decision overrides
// we cannot parse a subset because it could be that a decision
// above our decision of interest needs to read way past
// lookaheadInfo.stopIndex. It seems like there is no escaping
// the use of a full and complete token stream if we are
// resetting to token index 0 and re-parsing from the start symbol.
// It's not easy to restart parsing somewhere in the middle like a
// continuation because our call stack does not match the
// tree stack because of left recursive rule rewriting. grrrr!
 *
 * @since 4.5.1
 */
public static List<ParserRuleContext> getLookaheadParseTrees(Grammar g,
															 ParserInterpreter originalParser,
															 TokenStream tokens,
															 int startRuleIndex,
															 int decision,
															 int startIndex,
															 int stopIndex)
{
	List<ParserRuleContext> trees = new ArrayList<ParserRuleContext>();
	// Create a new parser interpreter to parse the ambiguous subphrase
	ParserInterpreter parser = deriveTempParserInterpreter(g, originalParser, tokens);
	BailButConsumeErrorStrategy errorHandler = new BailButConsumeErrorStrategy();
	parser.setErrorHandler(errorHandler);

	DecisionState decisionState = originalParser.getATN().decisionToState.get(decision);

	for (int alt=1; alt<=decisionState.getTransitions().length; alt++) {
		// re-parse entire input for all ambiguous alternatives
		// (don't have to do first as it's been parsed, but do again for simplicity
		//  using this temp parser.)
		parser.reset();
		parser.addDecisionOverride(decision, startIndex, alt);
		ParserRuleContext tt = parser.parse(startRuleIndex);
		int stopTreeAt = stopIndex;
		if ( errorHandler.firstErrorTokenIndex>=0 ) {
			stopTreeAt = errorHandler.firstErrorTokenIndex; // cut off rest at first error
		}
		ParserRuleContext subtree =
			Trees.getRootOfSubtreeEnclosingRegion(tt,
												  startIndex,
												  stopTreeAt);
		// Use higher of overridden decision tree or tree enclosing all tokens
		if ( Trees.isAncestorOf(parser.getOverrideDecisionRoot(), subtree) ) {
			subtree = parser.getOverrideDecisionRoot();
		}
		Trees.stripChildrenOutOfRange(subtree, parser.getOverrideDecisionRoot(), startIndex, stopTreeAt);
		trees.add(subtree);
	}

	return trees;
}
 
Example #10
Source File: TreeViewer.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public String getText(Tree node) {
	return String.valueOf(Trees.getNodeText(node, ruleNames));
}
 
Example #11
Source File: ParsingUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Tree findOverriddenDecisionRoot(Tree ctx) {
	return Trees.findNodeSuchThat(
			ctx,
			t -> t instanceof PreviewInterpreterRuleContext && ((PreviewInterpreterRuleContext) t).isDecisionOverrideRoot()
	);
}