Search in sources :

Example 71 with Recognizer

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

the class BasePHPTest method execModule.

public String execModule(String fileName) {
    String phpPath = locatePhp();
    String runtimePath = locateRuntime();
    String modulePath = new File(getTempTestDir(), fileName).getAbsolutePath();
    String inputPath = new File(getTempTestDir(), "input").getAbsolutePath();
    Path outputPath = getTempTestDir().toPath().resolve("output").toAbsolutePath();
    try {
        ProcessBuilder builder = new ProcessBuilder(phpPath, modulePath, inputPath, outputPath.toString());
        builder.environment().put("RUNTIME", runtimePath);
        builder.directory(getTempTestDir());
        Process process = builder.start();
        StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
        StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
        stdoutVacuum.start();
        stderrVacuum.start();
        process.waitFor();
        stdoutVacuum.join();
        stderrVacuum.join();
        String output = stdoutVacuum.toString();
        if (output.length() == 0) {
            output = null;
        }
        if (stderrVacuum.toString().length() > 0) {
            setParseErrors(stderrVacuum.toString());
        }
        return output;
    } catch (Exception e) {
        System.err.println("can't exec recognizer");
        e.printStackTrace(System.err);
    }
    return null;
}
Also used : Path(java.nio.file.Path) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) IOException(java.io.IOException)

Example 72 with Recognizer

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

the class BasePythonTest method execModule.

public String execModule(String fileName) {
    String pythonPath = locatePython();
    String runtimePath = locateRuntime();
    File tmpdirFile = new File(getTempDirPath());
    String modulePath = new File(tmpdirFile, fileName).getAbsolutePath();
    String inputPath = new File(tmpdirFile, "input").getAbsolutePath();
    Path outputPath = tmpdirFile.toPath().resolve("output").toAbsolutePath();
    try {
        ProcessBuilder builder = new ProcessBuilder(pythonPath, modulePath, inputPath, outputPath.toString());
        builder.environment().put("PYTHONPATH", runtimePath);
        builder.environment().put("PYTHONIOENCODING", "utf-8");
        builder.directory(tmpdirFile);
        Process process = builder.start();
        StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
        stderrVacuum.start();
        process.waitFor();
        stderrVacuum.join();
        String output = TestOutputReading.read(outputPath);
        if (stderrVacuum.toString().length() > 0) {
            setParseErrors(stderrVacuum.toString());
        }
        return output;
    } catch (Exception e) {
        System.err.println("can't exec recognizer");
        e.printStackTrace(System.err);
    }
    return null;
}
Also used : Path(java.nio.file.Path) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 73 with Recognizer

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

the class DefaultErrorStrategy method singleTokenDeletion.

/**
 * This method implements the single-token deletion inline error recovery
 * strategy. It is called by {@link #recoverInline} to attempt to recover
 * from mismatched input. If this method returns null, the parser and error
 * handler state will not have changed. If this method returns non-null,
 * {@code recognizer} will <em>not</em> be in error recovery mode since the
 * returned token was a successful match.
 *
 * <p>If the single-token deletion is successful, this method calls
 * {@link #reportUnwantedToken} to report the error, followed by
 * {@link Parser#consume} to actually "delete" the extraneous token. Then,
 * before returning {@link #reportMatch} is called to signal a successful
 * match.</p>
 *
 * @param recognizer the parser instance
 * @return the successfully matched {@link Token} instance if single-token
 * deletion successfully recovers from the mismatched input, otherwise
 * {@code null}
 */
protected Token singleTokenDeletion(Parser recognizer) {
    int nextTokenType = recognizer.getInputStream().LA(2);
    IntervalSet expecting = getExpectedTokens(recognizer);
    if (expecting.contains(nextTokenType)) {
        reportUnwantedToken(recognizer);
        /*
			System.err.println("recoverFromMismatchedToken deleting "+
							   ((TokenStream)recognizer.getInputStream()).LT(1)+
							   " since "+((TokenStream)recognizer.getInputStream()).LT(2)+
							   " is what we want");
			*/
        // simply delete extra token
        recognizer.consume();
        // we want to return the token we're actually matching
        Token matchedSymbol = recognizer.getCurrentToken();
        // we know current token is correct
        reportMatch(recognizer);
        return matchedSymbol;
    }
    return null;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

Example 74 with Recognizer

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

the class DefaultErrorStrategy method singleTokenInsertion.

/**
 * This method implements the single-token insertion inline error recovery
 * strategy. It is called by {@link #recoverInline} if the single-token
 * deletion strategy fails to recover from the mismatched input. If this
 * method returns {@code true}, {@code recognizer} will be in error recovery
 * mode.
 *
 * <p>This method determines whether or not single-token insertion is viable by
 * checking if the {@code LA(1)} input symbol could be successfully matched
 * if it were instead the {@code LA(2)} symbol. If this method returns
 * {@code true}, the caller is responsible for creating and inserting a
 * token with the correct type to produce this behavior.</p>
 *
 * @param recognizer the parser instance
 * @return {@code true} if single-token insertion is a viable recovery
 * strategy for the current mismatched input, otherwise {@code false}
 */
protected boolean singleTokenInsertion(Parser recognizer) {
    int currentSymbolType = recognizer.getInputStream().LA(1);
    // if current token is consistent with what could come after current
    // ATN state, then we know we're missing a token; error recovery
    // is free to conjure up and insert the missing token
    ATNState currentState = recognizer.getInterpreter().atn.states.get(recognizer.getState());
    ATNState next = currentState.transition(0).target;
    ATN atn = recognizer.getInterpreter().atn;
    IntervalSet expectingAtLL2 = atn.nextTokens(next, recognizer._ctx);
    // System.out.println("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames()));
    if (expectingAtLL2.contains(currentSymbolType)) {
        reportMissingToken(recognizer);
        return true;
    }
    return false;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) ATN(org.antlr.v4.runtime.atn.ATN) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 75 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project CFLint by cflint.

the class CFLint method syntaxError.

@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, int line, int charPositionInLine, final String msg, final org.antlr.v4.runtime.RecognitionException re) {
    String expression = null;
    int offset = charPositionInLine;
    int startLine = 0;
    int startOffset = 0;
    final Context context = new Context(currentFile, currentElement, null, true, null, null);
    if (currentElement != null) {
        startOffset = context.offset();
        if (context.startLine() != 1) {
            startLine = currentElement.getSource().getRow(startOffset) - 1;
        }
    }
    if (offendingSymbol instanceof Token && re != null) {
        // grab the first non-comment previous token, which is actually the cause of the syntax error theoretically
        CommonTokenStream tokenStream = (CommonTokenStream) recognizer.getInputStream();
        Token previousToken = tokenStream.get(re.getOffendingToken().getTokenIndex() - 1);
        if (previousToken != null) {
            while (previousToken.getChannel() == Token.HIDDEN_CHANNEL && tokenStream.get(previousToken.getTokenIndex() - 1) != null) {
                previousToken = tokenStream.get(previousToken.getTokenIndex() - 1);
            }
            line = previousToken.getLine();
            offset = previousToken.getStopIndex();
            expression = previousToken.getText();
        } else {
            expression = re.getOffendingToken().getText();
        }
        if (expression.length() > 50) {
            expression = expression.substring(1, 40) + "...";
        }
    }
    offset += startOffset;
    line += startLine;
    if (recognizer instanceof Parser && ((Parser) recognizer).isExpectedToken(CFSCRIPTParser.SEMICOLON)) {
        final ContextMessage cm = new ContextMessage(MISSING_SEMI, expression, null, line, offset);
        reportRule(currentElement, null, context, null, cm);
    } else {
        final ContextMessage cm = new ContextMessage(PARSE_ERROR, expression, null, line, offset);
        reportRule(currentElement, null, context, null, cm);
    }
}
Also used : Context(com.cflint.plugins.Context) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ContextMessage(com.cflint.plugins.Context.ContextMessage) Token(org.antlr.v4.runtime.Token) CFMLParser(cfml.parsing.CFMLParser) Parser(org.antlr.v4.runtime.Parser) CFSCRIPTParser(cfml.CFSCRIPTParser)

Aggregations

IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)24 Token (org.antlr.v4.runtime.Token)22 RecognitionException (org.antlr.v4.runtime.RecognitionException)19 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)15 File (java.io.File)11 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)10 ATNState (org.antlr.v4.runtime.atn.ATNState)9 IOException (java.io.IOException)8 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)8 Parser (org.antlr.v4.runtime.Parser)8 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)8 ArrayList (java.util.ArrayList)7 ATN (org.antlr.v4.runtime.atn.ATN)6 Pair (com.abubusoft.kripton.common.Pair)5 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)5 TokenStream (org.antlr.v4.runtime.TokenStream)5 BeetlException (org.beetl.core.exception.BeetlException)5 STGroupString (org.stringtemplate.v4.STGroupString)5 CommonToken (org.antlr.v4.runtime.CommonToken)4