Search in sources :

Example 26 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project groovy by apache.

the class DescriptiveErrorStrategy method createNoViableAlternativeErrorMessage.

protected String createNoViableAlternativeErrorMessage(Parser recognizer, NoViableAltException e) {
    TokenStream tokens = recognizer.getInputStream();
    String input;
    if (tokens != null) {
        if (e.getStartToken().getType() == Token.EOF) {
            input = "<EOF>";
        } else {
            input = charStream.getText(Interval.of(e.getStartToken().getStartIndex(), e.getOffendingToken().getStopIndex()));
        }
    } else {
        input = "<unknown input>";
    }
    return "Unexpected input: " + escapeWSAndQuote(input);
}
Also used : TokenStream(org.antlr.v4.runtime.TokenStream)

Example 27 with Recognizer

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

the class BaseNodeTest method execModule.

public String execModule(String fileName) {
    try {
        String npmPath = locateNpm();
        if (!TestContext.isCI()) {
            installRuntime(npmPath);
            registerRuntime(npmPath);
        }
        String modulePath = new File(getTempTestDir(), fileName).getAbsolutePath();
        linkRuntime(npmPath);
        String nodejsPath = locateNodeJS();
        String inputPath = new File(getTempTestDir(), "input").getAbsolutePath();
        ProcessBuilder builder = new ProcessBuilder(nodejsPath, modulePath, inputPath);
        builder.environment().put("NODE_PATH", getTempDirPath());
        builder.directory(getTempTestDir());
        Process process = builder.start();
        StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
        StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
        stdoutVacuum.start();
        stderrVacuum.start();
        // TODO switch to jdk 8
        process.waitFor();
        // if(!process.waitFor(1L, TimeUnit.MINUTES))
        // process.destroyForcibly();
        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);
        System.err.println();
        return null;
    }
}
Also used : BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) File(java.io.File) IOException(java.io.IOException)

Example 28 with Recognizer

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

the class BaseGoTest method execModule.

public String execModule(String fileName) {
    String goExecutable = locateGo();
    String modulePath = new File(getTempTestDir(), fileName).getAbsolutePath();
    String inputPath = new File(getTempTestDir(), "input").getAbsolutePath();
    try {
        ProcessBuilder builder = new ProcessBuilder(goExecutable, "run", modulePath, inputPath);
        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 : BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 29 with Recognizer

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

the class TestVisitors method testVisitErrorNode.

/**
 * This test verifies the basic behavior of visitors, with an emphasis on
 * {@link AbstractParseTreeVisitor#visitErrorNode}.
 */
@Test
public void testVisitErrorNode() {
    String input = "";
    VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
    VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));
    final List<String> errors = new ArrayList<>();
    parser.removeErrorListeners();
    parser.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            errors.add("line " + line + ":" + charPositionInLine + " " + msg);
        }
    });
    VisitorBasicParser.SContext context = parser.s();
    Assert.assertEquals("(s <missing 'A'> <EOF>)", context.toStringTree(parser));
    Assert.assertEquals(1, errors.size());
    Assert.assertEquals("line 1:0 missing 'A' at '<EOF>'", errors.get(0));
    VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {

        @Override
        public String visitErrorNode(ErrorNode node) {
            return "Error encountered: " + node.getSymbol();
        }

        @Override
        protected String defaultResult() {
            return "";
        }

        @Override
        protected String aggregateResult(String aggregate, String nextResult) {
            return aggregate + nextResult;
        }
    };
    String result = listener.visit(context);
    String expected = "Error encountered: [@-1,-1:-1='<missing 'A'>',<1>,1:0]";
    Assert.assertEquals(expected, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) ArrayList(java.util.ArrayList) ErrorNode(org.antlr.v4.runtime.tree.ErrorNode) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) RecognitionException(org.antlr.v4.runtime.RecognitionException) Test(org.junit.Test)

Example 30 with Recognizer

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

the class BaseCSharpTest method execTest.

public String execTest() {
    String exec = locateExec();
    try {
        File tmpdirFile = new File(getTempDirPath());
        Path output = tmpdirFile.toPath().resolve("output");
        Path errorOutput = tmpdirFile.toPath().resolve("error-output");
        String[] args = getExecTestArgs(exec, output, errorOutput);
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.directory(tmpdirFile);
        Process process = pb.start();
        StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
        StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
        stdoutVacuum.start();
        stderrVacuum.start();
        process.waitFor();
        stdoutVacuum.join();
        stderrVacuum.join();
        String writtenOutput = TestOutputReading.read(output);
        setParseErrors(TestOutputReading.read(errorOutput));
        int exitValue = process.exitValue();
        String stdoutString = stdoutVacuum.toString().trim();
        String stderrString = stderrVacuum.toString().trim();
        if (exitValue != 0) {
            System.err.println("execTest command: " + Utils.join(args, " "));
            System.err.println("execTest exitValue: " + exitValue);
        }
        if (!stdoutString.isEmpty()) {
            System.err.println("execTest stdoutVacuum: " + stdoutString);
        }
        if (!stderrString.isEmpty()) {
            System.err.println("execTest stderrVacuum: " + stderrString);
        }
        return writtenOutput;
    } 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)

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