Search in sources :

Example 41 with Tree

use of org.antlr.v4.runtime.tree.Tree in project drools by kiegroup.

the class FEELImpl method compileExpressionList.

public CompiledExpression compileExpressionList(String expression, CompilerContext ctx) {
    FEEL_1_1Parser parser = FEELParser.parse(getEventsManager(ctx.getListeners()), expression, ctx.getInputVariableTypes(), ctx.getInputVariables(), ctx.getFEELFunctions(), profiles);
    ParseTree tree = parser.expressionList();
    ASTBuilderVisitor v = new ASTBuilderVisitor(ctx.getInputVariableTypes());
    BaseNode expr = v.visit(tree);
    CompiledExpression ce = new CompiledExpressionImpl(expr);
    return ce;
}
Also used : ASTBuilderVisitor(org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) FEEL_1_1Parser(org.kie.dmn.feel.parser.feel11.FEEL_1_1Parser) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 42 with Tree

use of org.antlr.v4.runtime.tree.Tree in project incubator-systemml by apache.

the class DMLParserWrapper method doParse.

/**
 * This function is supposed to be called directly only from DmlSyntacticValidator when it encounters 'import'
 * @param fileName script file name
 * @param dmlScript script file contents
 * @param sourceNamespace namespace from source statement
 * @param argVals script arguments
 * @return dml program, or null if at least one error
 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace, Map<String, String> argVals) {
    DMLProgram dmlPgm = null;
    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = readDMLScript(fileName, LOG);
        }
        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new ANTLRInputStream(stream);
    } catch (FileNotFoundException e) {
        throw new ParseException("Cannot find file/resource: " + fileName, e);
    } catch (IOException e) {
        throw new ParseException("Cannot open file: " + fileName, e);
    } catch (LanguageException e) {
        throw new ParseException(e.getMessage(), e);
    }
    ProgramrootContext ast = null;
    CustomErrorListener errorListener = new CustomErrorListener();
    try {
        DmlLexer lexer = new DmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        DmlParser antlr4Parser = new DmlParser(tokens);
        // For now no optimization, since it is not able to parse integer value.
        boolean tryOptimizedParsing = false;
        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.programroot();
            // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.setCurrentFileName(fileName);
                } else {
                    errorListener.setCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.programroot();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.setCurrentFileName(fileName);
            // Now do the parsing
            ast = antlr4Parser.programroot();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
    }
    // Now convert the parse tree into DMLProgram
    // Do syntactic validation while converting
    ParseTree tree = ast;
    // And also do syntactic validation
    ParseTreeWalker walker = new ParseTreeWalker();
    // Get list of function definitions which take precedence over built-in functions if same name
    DmlPreprocessor prep = new DmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    DmlSyntacticValidator validator = new DmlSyntacticValidator(errorListener, argVals, sourceNamespace, prep.getFunctionDefs());
    walker.walk(validator, tree);
    errorListener.unsetCurrentFileName();
    this.parseIssues = errorListener.getParseIssues();
    this.atLeastOneWarning = errorListener.isAtLeastOneWarning();
    this.atLeastOneError = errorListener.isAtLeastOneError();
    if (atLeastOneError) {
        throw new ParseException(parseIssues, dmlScript);
    }
    if (atLeastOneWarning) {
        LOG.warn(CustomErrorListener.generateParseIssuesMessage(dmlScript, parseIssues));
    }
    dmlPgm = createDMLProgram(ast, sourceNamespace);
    return dmlPgm;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CustomErrorListener(org.apache.sysml.parser.common.CustomErrorListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) FileNotFoundException(java.io.FileNotFoundException) ProgramrootContext(org.apache.sysml.parser.dml.DmlParser.ProgramrootContext) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.sysml.parser.ParseException) LanguageException(org.apache.sysml.parser.LanguageException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) DMLProgram(org.apache.sysml.parser.DMLProgram) ParseException(org.apache.sysml.parser.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 43 with Tree

use of org.antlr.v4.runtime.tree.Tree in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method parseJavadocAsParseTree.

/**
     * Parses block comment content as javadoc comment.
     * @param blockComment
     *        block comment content.
     * @return parse tree
     */
private ParseTree parseJavadocAsParseTree(String blockComment) {
    final ANTLRInputStream input = new ANTLRInputStream(blockComment);
    final JavadocLexer lexer = new JavadocLexer(input);
    // remove default error listeners
    lexer.removeErrorListeners();
    // add custom error listener that logs parsing errors
    lexer.addErrorListener(errorListener);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final JavadocParser parser = new JavadocParser(tokens);
    // remove default error listeners
    parser.removeErrorListeners();
    // add custom error listener that logs syntax errors
    parser.addErrorListener(errorListener);
    // This strategy stops parsing when parser error occurs.
    // By default it uses Error Recover Strategy which is slow and useless.
    parser.setErrorHandler(new BailErrorStrategy());
    return parser.javadoc();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) JavadocParser(com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) JavadocLexer(com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 44 with Tree

use of org.antlr.v4.runtime.tree.Tree in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method parseJavadocAsDetailNode.

/**
     * Parses Javadoc comment as DetailNode tree.
     * @param javadocCommentAst
     *        DetailAST of Javadoc comment
     * @return DetailNode tree of Javadoc comment
     */
public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
    blockCommentLineNumber = javadocCommentAst.getLineNo();
    final String javadocComment = JavadocUtils.getJavadocCommentContent(javadocCommentAst);
    // Use a new error listener each time to be able to use
    // one check instance for multiple files to be checked
    // without getting side effects.
    errorListener = new DescriptiveErrorListener();
    // Log messages should have line number in scope of file,
    // not in scope of Javadoc comment.
    // Offset is line number of beginning of Javadoc comment.
    errorListener.setOffset(javadocCommentAst.getLineNo() - 1);
    final ParseStatus result = new ParseStatus();
    try {
        final ParseTree parseTree = parseJavadocAsParseTree(javadocComment);
        final DetailNode tree = convertParseTreeToDetailNode(parseTree);
        // adjust first line to indent of /**
        adjustFirstLineToJavadocIndent(tree, javadocCommentAst.getColumnNo() + JAVADOC_START.length());
        result.setTree(tree);
    } catch (ParseCancellationException | IllegalArgumentException ex) {
        // If syntax error occurs then message is printed by error listener
        // and parser throws this runtime exception to stop parsing.
        // Just stop processing current Javadoc comment.
        ParseErrorMessage parseErrorMessage = errorListener.getErrorMessage();
        // There are cases when antlr error listener does not handle syntax error
        if (parseErrorMessage == null) {
            parseErrorMessage = new ParseErrorMessage(javadocCommentAst.getLineNo(), MSG_KEY_UNRECOGNIZED_ANTLR_ERROR, javadocCommentAst.getColumnNo(), ex.getMessage());
        }
        result.setParseErrorMessage(parseErrorMessage);
    }
    return result;
}
Also used : ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 45 with Tree

use of org.antlr.v4.runtime.tree.Tree in project presto by prestodb.

the class TypeCalculation method parseTypeCalculation.

private static ParserRuleContext parseTypeCalculation(String calculation) {
    TypeCalculationLexer lexer = new TypeCalculationLexer(new CaseInsensitiveStream(new ANTLRInputStream(calculation)));
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    TypeCalculationParser parser = new TypeCalculationParser(tokenStream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(ERROR_LISTENER);
    parser.removeErrorListeners();
    parser.addErrorListener(ERROR_LISTENER);
    ParserRuleContext tree;
    try {
        // first, try parsing with potentially faster SLL mode
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        tree = parser.typeCalculation();
    } catch (ParseCancellationException ex) {
        // if we fail, parse with LL mode
        // rewind input stream
        tokenStream.reset();
        parser.reset();
        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        tree = parser.typeCalculation();
    }
    return tree;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) CaseInsensitiveStream(com.facebook.presto.sql.parser.CaseInsensitiveStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)30 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)21 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)15 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)12 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)9 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)6 Tree (org.antlr.v4.runtime.tree.Tree)6 IOException (java.io.IOException)5 Tree (org.antlr.runtime.tree.Tree)4 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)4 RecognitionException (org.antlr.v4.runtime.RecognitionException)4 Rectangle2D (java.awt.geom.Rectangle2D)3 FileInputStream (java.io.FileInputStream)3 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)3