Java Code Examples for org.antlr.runtime.tree.CommonTree#getToken()

The following examples show how to use org.antlr.runtime.tree.CommonTree#getToken() . 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: SemanticException.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, CommonTree tree, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = tree.getToken();
    this.index = tree.getTokenStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 2
Source File: SemanticException.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, CommonTree tree, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = tree.getToken();
    this.index = tree.getTokenStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 3
Source File: GrammarTransformPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void augmentTokensWithOriginalPosition(final Grammar g, GrammarAST tree) {
	if ( tree==null ) return;

	List<GrammarAST> optionsSubTrees = tree.getNodesWithType(ANTLRParser.ELEMENT_OPTIONS);
	for (int i = 0; i < optionsSubTrees.size(); i++) {
		GrammarAST t = optionsSubTrees.get(i);
		CommonTree elWithOpt = t.parent;
		if ( elWithOpt instanceof GrammarASTWithOptions ) {
			Map<String, GrammarAST> options = ((GrammarASTWithOptions) elWithOpt).getOptions();
			if ( options.containsKey(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME) ) {
				GrammarToken newTok = new GrammarToken(g, elWithOpt.getToken());
				newTok.originalTokenIndex = Integer.valueOf(options.get(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME).getText());
				elWithOpt.token = newTok;

				GrammarAST originalNode = g.ast.getNodeWithTokenIndex(newTok.getTokenIndex());
				if (originalNode != null) {
					// update the AST node start/stop index to match the values
					// of the corresponding node in the original parse tree.
					elWithOpt.setTokenStartIndex(originalNode.getTokenStartIndex());
					elWithOpt.setTokenStopIndex(originalNode.getTokenStopIndex());
				}
				else {
					// the original AST node could not be located by index;
					// make sure to assign valid values for the start/stop
					// index so toTokenString will not throw exceptions.
					elWithOpt.setTokenStartIndex(newTok.getTokenIndex());
					elWithOpt.setTokenStopIndex(newTok.getTokenIndex());
				}
			}
		}
	}
}
 
Example 4
Source File: SemanticException.java    From AppTroy with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, CommonTree tree, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = tree.getToken();
    this.index = tree.getTokenStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 5
Source File: SemanticException.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, CommonTree tree, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = tree.getToken();
    this.index = tree.getTokenStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 6
Source File: SemanticException.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, CommonTree tree, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = tree.getToken();
    this.index = tree.getTokenStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 7
Source File: FilterSimpleExpressionCu.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Compile a simple filter expression compilation unit from a tree
 *
 * @param tree
 *            The input tree
 * @return The simple filter expression compilation unit
 */
public static @Nullable FilterSimpleExpressionCu compile(CommonTree tree) {
    if (tree.getToken() == null) {
        return null;
    }

    int childCount = tree.getChildCount();
    switch (tree.getToken().getType()) {
    case FilterParserParser.CONSTANT:
    case FilterParserParser.PAR_CONSTANT:
        StringBuilder paragraph = new StringBuilder();
        extractParagraph(tree, paragraph, 0, childCount);
        return new FilterSimpleExpressionCu(IFilterStrings.WILDCARD, IFilterStrings.MATCHES, paragraph.toString().trim());
    case FilterParserParser.OPERATION:
        String left = Objects.requireNonNull(tree.getChild(0).getText());
        String op = Objects.requireNonNull(tree.getChild(1).getText());
        String right = tree.getChild(2).getText();
        return new FilterSimpleExpressionCu(left, op, right);
    case FilterParserParser.OPERATION1:
        String left1 = Objects.requireNonNull(tree.getChild(0).getText());
        String op1 = Objects.requireNonNull(tree.getChild(1).getText());
        String right1 = null;
        return new FilterSimpleExpressionCu(left1, op1, right1);
    case FilterParserParser.OPERATION2:
    case FilterParserParser.OPERATION4:
    case FilterParserParser.OPERATION5:
        StringBuilder builder = new StringBuilder();
        int index = extractParagraph(tree, builder, 0, childCount);
        String left2 = builder.toString().trim();
        String op2 = Objects.requireNonNull(tree.getChild(index++).getText());
        builder = new StringBuilder();
        extractParagraph(tree, builder, index, childCount);
        String right2 = builder.toString().trim();
        return new FilterSimpleExpressionCu(left2, op2, right2);
    case FilterParserParser.OPERATION3:
        StringBuilder builder1 = new StringBuilder();
        int index1 = extractParagraph(tree, builder1, 0, childCount);
        String left3 = builder1.toString().trim();
        String op3 = Objects.requireNonNull(tree.getChild(index1).getText());
        String right3 = null;
        return new FilterSimpleExpressionCu(left3, op3, right3);
    case FilterParserParser.ROOT2:
        if (childCount == 0 || (childCount == 2 && tree.getChild(1).getType() != FilterParserParser.CONSTANT)) {
            return null;
        }

        boolean negate = tree.getChild(0).getText().equals(IFilterStrings.NOT);
        CommonTree expression = Objects.requireNonNull((CommonTree) tree.getChild(childCount - 1));
        FilterSimpleExpressionCu compiled = negate ? FilterSimpleExpressionNotCu.compile(expression) : FilterSimpleExpressionCu.compile(expression);
        return compiled;
    default:
        break;
    }
    return null;
}