Search in sources :

Example 61 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project ballerina by ballerina-lang.

the class SiddhiCompiler method parseTimeConstantDefinition.

public static TimeConstant parseTimeConstantDefinition(String source) throws SiddhiParserException {
    ANTLRInputStream input = new ANTLRInputStream(source);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.time_value();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (TimeConstant) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SiddhiQLBaseVisitorImpl(org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) TimeConstant(org.ballerinalang.siddhi.query.api.expression.constant.TimeConstant)

Example 62 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project ballerina by ballerina-lang.

the class SiddhiCompiler method parsePartition.

public static Partition parsePartition(String source) throws SiddhiParserException {
    ANTLRInputStream input = new ANTLRInputStream(source);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.partition_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (Partition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) SiddhiQLBaseVisitorImpl(org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 63 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project ballerina by ballerina-lang.

the class SiddhiCompiler method parseFunctionDefinition.

public static FunctionDefinition parseFunctionDefinition(String source) throws SiddhiParserException {
    ANTLRInputStream input = new ANTLRInputStream(source);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.definition_function_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (FunctionDefinition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) FunctionDefinition(org.ballerinalang.siddhi.query.api.definition.FunctionDefinition) SiddhiQLBaseVisitorImpl(org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 64 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project ballerina by ballerina-lang.

the class BLangWSPreservingParserListener method exitEveryRule.

@Override
public void exitEveryRule(ParserRuleContext parserRuleContext) {
    if (getWSWasCalledOn == parserRuleContext) {
        return;
    }
    int rangeEndTokenIndex;
    if (parserRuleContext.stop == null) {
        ParseTree child = parserRuleContext.getChild(1);
        if (child instanceof TerminalNode) {
            // This is needed to handle A + B + C case of BinaryAddSubExpression
            rangeEndTokenIndex = ((TerminalNode) child).getSymbol().getTokenIndex();
        } else {
            rangesOfRuleContext.pop();
            return;
        }
    } else {
        rangeEndTokenIndex = parserRuleContext.stop.getTokenIndex() + 1;
    }
    closeLastRange(rangeEndTokenIndex);
    Stack<TokenRange> tokenRanges = new Stack<>();
    // TODO: check why rangesOfRuleContext become empty before closing the last range.
    if (!rangesOfRuleContext.isEmpty()) {
        tokenRanges = rangesOfRuleContext.pop();
    }
    Stack<Whitespace> ws = new Stack<>();
    for (TokenRange range : tokenRanges) {
        addWSFromRange(ws, range);
    }
    wsSinceLastNode.addAll(ws);
    if (!rangesOfRuleContext.isEmpty()) {
        rangesOfRuleContext.peek().add(new TokenRange(rangeEndTokenIndex));
    }
}
Also used : TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Whitespace(org.ballerinalang.model.Whitespace) Stack(java.util.Stack)

Example 65 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project ballerina by ballerina-lang.

the class TomlProcessor method parseTomlContent.

/**
 * Generate the proxy object by passing in the toml file.
 *
 * @param stream charstream object containing the content
 * @return proxy object
 */
public static ParseTree parseTomlContent(CharStream stream) {
    TomlLexer lexer = new TomlLexer(stream);
    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Pass the tokens to the parser
    TomlParser parser = new TomlParser(tokens);
    return parser.toml();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TomlLexer(org.ballerinalang.toml.antlr4.TomlLexer) TomlParser(org.ballerinalang.toml.antlr4.TomlParser)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)311 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)104 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)60 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)49 AddedParseTree (org.eclipse.titan.common.parsers.AddedParseTree)46 CharStream (org.antlr.v4.runtime.CharStream)43 Test (org.junit.Test)43 CommonToken (org.antlr.v4.runtime.CommonToken)35 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)35 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 File (java.io.File)26 ArrayList (java.util.ArrayList)22 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)22 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 Grammar (org.antlr.v4.tool.Grammar)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 IOException (java.io.IOException)19 LexerGrammar (org.antlr.v4.tool.LexerGrammar)16