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

The following examples show how to use org.antlr.v4.runtime.tree.ParseTree#accept() . 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: NodeUsesProvider.java    From sonar-tsql-plugin with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object visitChildren(final RuleNode node) {

	final int n = node.getChildCount();

	for (int i = 0; i < n; i++) {
		final ParseTree c = node.getChild(i);
		c.accept(this);

	}
	final String textToFind = node.getText();
	if (StringUtils.containsIgnoreCase(textToFind, tempText)
			|| StringUtils.containsIgnoreCase(tempText, textToFind)) {
		nodes.add(new ParsedNode(node));
	}
	return null;

}
 
Example 2
Source File: Visitor.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public String visitChildren(RuleNode node, List<Integer> withoutNodes) {
    if(node == null) return "";
    String result = this.defaultResult();
    int n = node.getChildCount();

    for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) {
        if(withoutNodes != null && withoutNodes.contains(i)) continue;
        ParseTree c = node.getChild(i);
        String childResult = c instanceof TerminalNode ? printTerminalNode((TerminalNode) c) : c.accept(this);
        result = this.aggregateResult(result, childResult);
    }

    return result;
}
 
Example 3
Source File: Visitor.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public String visitWithoutTerminals(RuleNode node) {
    String result = this.defaultResult();
    int n = node.getChildCount();

    for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) {
        ParseTree c = node.getChild(i);
        if(c instanceof TerminalNode) continue;
        String childResult = c.accept(this);
        result = this.aggregateResult(result, childResult);
    }

    return result;
}
 
Example 4
Source File: Visitor.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public String visitWithoutStrings(RuleNode node, String string) {
    String result = this.defaultResult();
    int n = node.getChildCount();

    for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) {
        ParseTree c = node.getChild(i);
        if(string.contains(c.getText())) continue;
        String childResult = c instanceof TerminalNode ? printTerminalNode((TerminalNode) c) : c.accept(this);
        result = this.aggregateResult(result, childResult);
    }

    return result;
}
 
Example 5
Source File: Visitor.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public String visitWithoutClasses(RuleNode node, Class nodeType) {
    String result = this.defaultResult();
    int n = node.getChildCount();

    for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) {
        ParseTree c = node.getChild(i);
        if(c.getClass() == nodeType) continue;
        String childResult = c instanceof TerminalNode ? printTerminalNode((TerminalNode) c) : c.accept(this);
        result = this.aggregateResult(result, childResult);
    }

    return result;
}
 
Example 6
Source File: ShellCommand.java    From cloudml with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static ShellCommand parse(ANTLRInputStream input) throws RecognitionException {
    ShellCommandsLexer lexer = new ShellCommandsLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ShellCommandsParser parser = new ShellCommandsParser(tokens);
    ParseTree tree = parser.script();
    return tree.accept(new ShellCommandBuilder());
}
 
Example 7
Source File: CoreDslManager.java    From theta with Apache License 2.0 4 votes vote down vote up
public Type parseType(final String string) {
	checkNotNull(string);
	final CoreDslParser parser = createParserForString(string);
	final ParseTree tree = parser.type();
	return tree.accept(TypeCreatorVisitor.getInstance());
}
 
Example 8
Source File: CoreDslManager.java    From theta with Apache License 2.0 4 votes vote down vote up
public Expr<?> parseExpr(final String string) {
	checkNotNull(string);
	final CoreDslParser parser = createParserForString(string);
	final ParseTree tree = parser.expr();
	return tree.accept(new ExprCreatorVisitor(scope));
}
 
Example 9
Source File: CoreDslManager.java    From theta with Apache License 2.0 4 votes vote down vote up
public Stmt parseStmt(final String string) {
	checkNotNull(string);
	final CoreDslParser parser = createParserForString(string);
	final ParseTree tree = parser.stmt();
	return tree.accept(new StmtCreatorVisitor(scope));
}
 
Example 10
Source File: JetTemplateCodeVisitor.java    From jetbrick-template-1x with Apache License 2.0 4 votes vote down vote up
@Override
public Code visitBlock(BlockContext ctx) {
    int size = ctx.getChildCount();
    BlockCode code = scopeCode.createBlockCode(size);
    if (size == 0) return code;

    for (int i = 0; i < size; i++) {
        ParseTree node = ctx.children.get(i);
        Code c = node.accept(this);

        if (node instanceof TextContext) {
            // 文本节点
            TextCode textCode = (TextCode) c;

            if (trimDirectiveLine || trimDirectiveComments) {
                ParseTree prev = (i > 0) ? ctx.children.get(i - 1) : null;
                ParseTree next = (i < size - 1) ? ctx.children.get(i + 1) : null;

                boolean trimLeft;
                boolean keepLeftNewLine = false;
                if (prev == null) {
                    trimLeft = !(ctx.getParent() instanceof TemplateContext);
                } else {
                    trimLeft = prev instanceof DirectiveContext;
                    if (trimLeft) {
                        // inline directive, 对于一个内联的 #if, #for 指令,后面有要求保留一个 NewLine
                        // @see https://github.com/subchen/jetbrick-template/issues/25
                        ParserRuleContext directive = (ParserRuleContext) ((DirectiveContext) prev).getChild(0);
                        if (directive instanceof If_directiveContext || directive instanceof For_directiveContext) {
                            if (directive.getStart().getLine() == directive.getStop().getLine()) {
                                keepLeftNewLine = true; // 保留一个 NewLine
                            }
                        }
                    }
                }

                boolean trimRight;
                if (next == null) {
                    trimRight = !(ctx.getParent() instanceof TemplateContext);
                } else {
                    trimRight = (next instanceof DirectiveContext);
                }

                // trim 指令两边的注释
                if (trimDirectiveComments) {
                    textCode.trimComments(trimLeft, trimRight, commentsPrefix, commentsSuffix);
                }
                // trim 指令两边的空白内容
                if (trimDirectiveLine) {
                    textCode.trimEmptyLine(trimLeft, trimRight, keepLeftNewLine);
                }

                // trim 掉 #tag 和 #macro 指令最后一个多余的 '\n'
                if (next == null) {
                    if (ctx.getParent() instanceof Tag_directiveContext || ctx.getParent() instanceof Macro_directiveContext) {
                        textCode.trimLastNewLine();
                    }
                }
            }

            if (!textCode.isEmpty()) {
                // 如果有相同内容的Text,则从缓存中读取
                TextCode old = textCache.get(textCode.getText());
                if (old == null) {
                    old = textCode;
                    textCache.put(textCode.getText(), textCode);
                    // add text into field
                    tcc.addField(textCode.getId(), textCode.getText());
                }
                code.addLine(old.toString());
            }
        } else {
            code.addChild(c);
        }
    }
    return code;
}