Search in sources :

Example 6 with org.antlr.v4.runtime

use of org.antlr.v4.runtime in project antlr4 by antlr.

the class BaseGoTest method cacheGoRuntime.

private static void cacheGoRuntime(File tmpPackageDir) throws Exception {
    String goExecutable = locateGo();
    ProcessBuilder pb = new ProcessBuilder(goExecutable, "install", "-x");
    pb.directory(tmpPackageDir);
    pb.environment().put("GOPATH", tmpGopath.getPath());
    pb.redirectErrorStream(true);
    Process process = pb.start();
    StreamVacuum sucker = new StreamVacuum(process.getInputStream());
    sucker.start();
    int exit = process.waitFor();
    sucker.join();
    if (exit != 0) {
        throw new Exception("Non-zero exit while caching go runtime, output: " + sucker.toString());
    }
}
Also used : StreamVacuum(org.antlr.v4.test.runtime.StreamVacuum) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) IOException(java.io.IOException)

Example 7 with org.antlr.v4.runtime

use of org.antlr.v4.runtime in project antlr4 by antlr.

the class ParserATNFactory method createATN.

@Override
public ATN createATN() {
    _createATN(g.rules.values());
    assert atn.maxTokenType == g.getMaxTokenType();
    addRuleFollowLinks();
    addEOFTransitionToStartRules();
    ATNOptimizer.optimize(g, atn);
    for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonClosureBlocks) {
        LL1Analyzer analyzer = new LL1Analyzer(atn);
        ATNState blkStart = pair.b;
        ATNState blkStop = pair.c;
        IntervalSet lookahead = analyzer.LOOK(blkStart, blkStop, null);
        if (lookahead.contains(org.antlr.v4.runtime.Token.EPSILON)) {
            ErrorType errorType = pair.a instanceof LeftRecursiveRule ? ErrorType.EPSILON_LR_FOLLOW : ErrorType.EPSILON_CLOSURE;
            g.tool.errMgr.grammarError(errorType, g.fileName, ((GrammarAST) pair.a.ast.getChild(0)).getToken(), pair.a.name);
        }
    }
    optionalCheck: for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonOptionalBlocks) {
        int bypassCount = 0;
        for (int i = 0; i < pair.b.getNumberOfTransitions(); i++) {
            ATNState startState = pair.b.transition(i).target;
            if (startState == pair.c) {
                bypassCount++;
                continue;
            }
            LL1Analyzer analyzer = new LL1Analyzer(atn);
            if (analyzer.LOOK(startState, pair.c, null).contains(org.antlr.v4.runtime.Token.EPSILON)) {
                g.tool.errMgr.grammarError(ErrorType.EPSILON_OPTIONAL, g.fileName, ((GrammarAST) pair.a.ast.getChild(0)).getToken(), pair.a.name);
                continue optionalCheck;
            }
        }
        if (bypassCount != 1) {
            throw new UnsupportedOperationException("Expected optional block with exactly 1 bypass alternative.");
        }
    }
    return atn;
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Triple(org.antlr.v4.runtime.misc.Triple) LL1Analyzer(org.antlr.v4.runtime.atn.LL1Analyzer) ErrorType(org.antlr.v4.tool.ErrorType) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 8 with org.antlr.v4.runtime

use of org.antlr.v4.runtime in project oxTrust by GluuFederation.

the class ScimFilterParser method criteria.

public final CriteriaContext criteria() throws RecognitionException {
    CriteriaContext _localctx = new CriteriaContext(_ctx, getState());
    enterRule(_localctx, 4, RULE_criteria);
    try {
        int _alt;
        enterOuterAlt(_localctx, 1);
        {
            setState(139);
            match(T__0);
            setState(141);
            _errHandler.sync(this);
            _alt = 1 + 1;
            do {
                switch(_alt) {
                    case 1 + 1:
                        {
                            {
                                setState(140);
                                matchWildcard();
                            }
                        }
                        break;
                    default:
                        throw new NoViableAltException(this);
                }
                setState(143);
                _errHandler.sync(this);
                _alt = getInterpreter().adaptivePredict(_input, 20, _ctx);
            } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
            setState(145);
            match(T__0);
        }
    } catch (RecognitionException re) {
        _localctx.exception = re;
        _errHandler.reportError(this, re);
        _errHandler.recover(this, re);
    } finally {
        exitRule();
    }
    return _localctx;
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 9 with org.antlr.v4.runtime

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

the class PyDMLParserWrapper method doParse.

/**
 * This function is supposed to be called directly only from PydmlSyntacticValidator 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 org.antlr.v4.runtime.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 {
        PydmlLexer lexer = new PydmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PydmlParser antlr4Parser = new PydmlParser(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
    PydmlPreprocessor prep = new PydmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    PydmlSyntacticValidator validator = new PydmlSyntacticValidator(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 : FileNotFoundException(java.io.FileNotFoundException) LanguageException(org.apache.sysml.parser.LanguageException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) DMLProgram(org.apache.sysml.parser.DMLProgram) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CustomErrorListener(org.apache.sysml.parser.common.CustomErrorListener) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ProgramrootContext(org.apache.sysml.parser.pydml.PydmlParser.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) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParseException(org.apache.sysml.parser.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 10 with org.antlr.v4.runtime

use of org.antlr.v4.runtime 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)

Aggregations

BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)8 File (java.io.File)6 URL (java.net.URL)6 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)6 STGroupString (org.stringtemplate.v4.STGroupString)6 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)3 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)3 InputStream (java.io.InputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 Pair (org.antlr.v4.runtime.misc.Pair)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 ST (org.stringtemplate.v4.ST)2 STGroup (org.stringtemplate.v4.STGroup)2 STGroupFile (org.stringtemplate.v4.STGroupFile)2 StringRenderer (org.stringtemplate.v4.StringRenderer)2