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

The following examples show how to use org.antlr.v4.runtime.tree.ParseTree#getClass() . 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: Node.java    From gyro with Apache License 2.0 6 votes vote down vote up
public static Node create(ParseTree context) {
    Class<? extends ParseTree> contextClass = context.getClass();
    Function<ParseTree, Node> nodeConstructor = NODE_CONSTRUCTORS.get(contextClass);
    Node node;

    if (nodeConstructor != null) {
        node = nodeConstructor.apply(context);

    } else if (TerminalNode.class.isAssignableFrom(contextClass)) {
        node = new ValueNode((TerminalNode) context);

    } else {
        throw new Bug(String.format(
            "@|bold %s|@ isn't a known node type!",
            contextClass.getName()));
    }

    return node;
}
 
Example 2
Source File: InstructionGenerator.java    From antsdb with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static public Generator<ParseTree> getGenerator(ParseTree ctx) throws OrcaException {
    Class<?> klass = ctx.getClass();
    Generator<ParseTree> generator = _generatorByName.get(klass);
    if (generator == null) {
        String key = StringUtils.removeStart(klass.getSimpleName(), "MysqlParser$");
        key = StringUtils.removeEnd(key, "Context");
        key += "Generator";
        try {
            key = InstructionGenerator.class.getPackage().getName() + "." + key;
            Class<?> generatorClass = Class.forName(key);
            generator = (Generator<ParseTree>)generatorClass.newInstance();
            _generatorByName.put(klass, generator);
        }
        catch (Exception x) {
            throw new OrcaException("instruction geneartor is not found: " + key, x);
        }
    }
    return generator;
}
 
Example 3
Source File: InstructionGenerator.java    From antsdb with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static public Generator<ParseTree> getGenerator(ParseTree ctx) throws OrcaException {
    Class<?> klass = ctx.getClass();
    Generator<ParseTree> generator = _generatorByName.get(klass);
    if (generator == null) {
        String key = StringUtils.removeStart(klass.getSimpleName(), "FishParser$");
        key = StringUtils.removeEnd(key, "Context");
        key += "Generator";
        try {
            key = InstructionGenerator.class.getPackage().getName() + "." + key;
            Class<?> generatorClass = Class.forName(key);
            generator = (Generator<ParseTree>)generatorClass.newInstance();
            _generatorByName.put(klass, generator);
        }
        catch (Exception x) {
            throw new OrcaException("instruction geneartor is not found: " + key, x);
        }
    }
    return generator;
}
 
Example 4
Source File: ComplexityVisitor.java    From sonar-tsql-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void apply(final ParseTree tree) {
	final Class<? extends ParseTree> classz = tree.getClass();
	if (Sql_unionContext.class.equals(classz)) {
		this.complexity++;
	}
	if (Function_callContext.class.isAssignableFrom(classz)) {
		this.complexity++;
	}
	if (Join_partContext.class.equals(classz)) {
		this.complexity++;
	}
	if (Order_by_expressionContext.class.equals(classz)) {
		this.complexity++;
	}
	if (Select_list_elemContext.class.equals(classz)) {
		this.complexity++;
	}
	if (Search_condition_notContext.class.equals(classz)) {
		this.complexity++;
	}
	if (Dml_clauseContext.class.equals(classz)) {
		this.complexity++;
	}

}
 
Example 5
Source File: Filter.java    From gyro with Apache License 2.0 5 votes vote down vote up
public static Filter create(ParseTree context) {
    Class<? extends ParseTree> cc = context.getClass();

    if (cc.equals(GyroParser.AndFilterContext.class)) {
        return new AndFilter((GyroParser.AndFilterContext) context);

    } else if (cc.equals(GyroParser.OrFilterContext.class)) {
        return new OrFilter((GyroParser.OrFilterContext) context);

    } else if (cc.equals(GyroParser.ComparisonFilterContext.class)) {
        return new ComparisonFilter((GyroParser.ComparisonFilterContext) context);
    }

    return null;
}
 
Example 6
Source File: ProgramParser.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
private Location toLocation(Scope scope, ParseTree node) {
    Token start;
    if (node instanceof ParserRuleContext) {
        start = ((ParserRuleContext) node).start;
    } else if (node instanceof TerminalNode) {
        start = ((TerminalNode) node).getSymbol();
    } else {
        throw new ProgramCompileException("Location is not available for type " + node.getClass());
    }
    Location location = new Location(scope != null ? scope.programName : "<string>", start.getLine(), start.getCharPositionInLine());
    return location;
}
 
Example 7
Source File: WalkerUtil.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public static boolean has(Class nodeType, ParseTree node) {
    if(node == null) return false;
    if(node.getClass() == nodeType) return true;
    for(int i = 0; i < node.getChildCount(); i++) {
        if(has(nodeType, node.getChild(i))) return true;
    }
    return false;
}
 
Example 8
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 9
Source File: ProgramParser.java    From vespa with Apache License 2.0 5 votes vote down vote up
private Location toLocation(Scope scope, ParseTree node) {
    Token start;
    if (node instanceof ParserRuleContext) {
        start = ((ParserRuleContext)node).start;
    } else if (node instanceof TerminalNode) {
        start = ((TerminalNode)node).getSymbol();
    } else {
    	throw new ProgramCompileException("Location is not available for type " + node.getClass());
    }
    Location location = new Location(scope != null? scope.programName: "<string>", start.getLine(), start.getCharPositionInLine());
    return location;
}
 
Example 10
Source File: CComplexityVisitor.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void apply(final ParseTree tree) {
	final Class<? extends ParseTree> classz = tree.getClass();
	if (Search_condition_notContext.class.equals(classz)) {
		complexity++;
	}

	if (Try_catch_statementContext.class.equals(classz)) {
		complexity++;
	}

}
 
Example 11
Source File: WalkerUtil.java    From swift-js-transpiler with MIT License 4 votes vote down vote up
public static boolean isDirectDescendant(Class nodeType, ParseTree node) {
    if(node == null) return false;
    if(node.getClass() == nodeType) return true;
    if(node.getChildCount() != 1) return false;
    return isDirectDescendant(nodeType, node.getChild(0));
}
 
Example 12
Source File: WalkerUtil.java    From swift-js-transpiler with MIT License 4 votes vote down vote up
public static boolean isRightMostDescendant(Class nodeType, ParseTree node) {
    if(node == null) return false;
    if(node.getClass() == nodeType) return true;
    return isRightMostDescendant(nodeType, node.getChild(node.getChildCount() - 1));
}
 
Example 13
Source File: GroovydocManager.java    From groovy with Apache License 2.0 4 votes vote down vote up
private String findDocCommentByNode(ParserRuleContext node) {
    if (!asBoolean(node)) {
        return null;
    }

    if (node instanceof GroovyParser.ClassBodyContext) {
        return null;
    }

    ParserRuleContext parentContext = node.getParent();

    if (!asBoolean(parentContext)) {
        return null;
    }

    String docCommentNodeText = null;
    boolean sameTypeNodeBefore = false;
    for (ParseTree child : parentContext.children) {

        if (node == child) {
            // if no doc comment node found and no siblings of same type before the node,
            // try to find doc comment node of its parent
            if (!asBoolean((Object) docCommentNodeText) && !sameTypeNodeBefore) {
                return findDocCommentByNode(parentContext);
            }

            return docCommentNodeText;
        }

        if (node.getClass() == child.getClass()) { // e.g. ClassBodyDeclarationContext == ClassBodyDeclarationContext
            docCommentNodeText = null;
            sameTypeNodeBefore = true;
            continue;
        }

        if (!(child instanceof GroovyParser.NlsContext || child instanceof GroovyParser.SepContext)) {
            continue;
        }

        // doc comments are treated as NL
        List<? extends TerminalNode> nlList =
                child instanceof GroovyParser.NlsContext
                        ? ((GroovyParser.NlsContext) child).NL()
                        : ((GroovyParser.SepContext) child).NL();

        int nlListSize = nlList.size();
        if (0 == nlListSize) {
            continue;
        }

        for (int i = nlListSize - 1; i >= 0; i--) {
            String text = nlList.get(i).getText();

            if (matches(text, SPACES_PATTERN)) {
                continue;
            }

            if (text.startsWith(GROOVYDOC_PREFIX)) {
                docCommentNodeText = text;
            } else {
                docCommentNodeText = null;
            }

            break;
        }
    }

    throw new GroovyBugError("node can not be found: " + node.getText()); // The exception should never be thrown!
}