private void setupPicky(PainlessParser parser) { // Diagnostic listener invokes syntaxError on other listeners for ambiguity issues, parser.addErrorListener(new DiagnosticErrorListener(true)); // a second listener to fail the test when the above happens. parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(final Recognizer<?,?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) { throw new AssertionError("line: " + line + ", offset: " + charPositionInLine + ", symbol:" + offendingSymbol + " " + msg); } }); // Enable exact ambiguity detection (costly). we enable exact since its the default for // DiagnosticErrorListener, life is too short to think about what 'inexact ambiguity' might mean. parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); }
private void parseInputStream(CharStream inputStream, OboParseResultListener listener) { final OboLexer l = new OboLexer(inputStream); final Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l)); p.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("Failed to parse at line " + line + " due to " + msg, e); } }); if (debug) { p.addErrorListener(new DiagnosticErrorListener()); } p.addParseListener(new OboParserListener(listener)); p.oboFile(); }
public static <P extends Parser> P getParser(Class<? extends Lexer> lexerClass, Class<P> parserClass, String source) { Lexer lexer = getLexer(lexerClass, source); TokenStream tokens = new CommonTokenStream(lexer); P parser; try { parser = parserClass.getConstructor(TokenStream.class).newInstance(tokens); } catch (Exception e) { throw new IllegalArgumentException("couldn't invoke parser constructor", e); } parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); parser.removeErrorListeners(); // don't spit to stderr parser.addErrorListener(new DiagnosticErrorListener()); parser.addErrorListener(new AntlrFailureListener()); return parser; }
/** * Build and return {@link Antlr4OboParser} for a given <code>text</code>. * * @param text String with the text to parse. * @param mode Name of the mode to use. * @return {@link Antlr4OboParser}, readily setup for parsing the OBO file. */ protected Antlr4OboParser buildParser(String text, String mode) { final CodePointCharStream inputStream = CharStreams.fromString(text); final OboLexer l = new OboLexer(inputStream); for (int i = 0; i < l.getModeNames().length; ++i) { if (mode.equals(l.getModeNames()[i])) { l.mode(i); } } Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l)); p.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); p.addErrorListener(new DiagnosticErrorListener()); p.addParseListener(outerListener); return p; }
private static TrinityParser createParser(ANTLRInputStream input) throws IOException { TrinityLexer lexer = new TrinityLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); TrinityParser parser = new TrinityParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(new DiagnosticErrorListener()); parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); return parser; }
public static void addDebugListeners(FusionTablesSqlParser parser) { parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); parser.removeErrorListeners(); parser.addErrorListener(new DiagnosticErrorListener()); parser.addErrorListener(new VerboseErrorListener()); }
protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, InvocationTargetException, PrintException { try { ANTLRInputStream input = new ANTLRInputStream(r); lexer.setInputStream(input); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.fill(); if ( showTokens ) { for (Object tok : tokens.getTokens()) { System.out.println(tok); } } if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return; if ( diagnostics ) { parser.addErrorListener(new DiagnosticErrorListener()); parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); } if ( printTree || gui || psFile!=null ) { parser.setBuildParseTree(true); } if ( SLL ) { // overrides diagnostics parser.getInterpreter().setPredictionMode(PredictionMode.SLL); } parser.setTokenStream(tokens); parser.setTrace(trace); try { Method startRule = parserClass.getMethod(startRuleName); ParserRuleContext tree = (ParserRuleContext)startRule.invoke(parser, (Object[])null); if ( printTree ) { System.out.println(tree.toStringTree(parser)); } if ( gui ) { Trees.inspect(tree, parser); } if ( psFile!=null ) { Trees.save(tree, parser, psFile); // Generate postscript } } catch (NoSuchMethodException nsme) { System.err.println("No method for rule "+startRuleName+" or it has arguments"); } } finally { if ( r!=null ) r.close(); if ( is!=null ) is.close(); } }
protected SpecificationContext parse(Lexer lexer, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, PrintException { ANTLRInputStream input = new ANTLRInputStream(r); lexer.setInputStream(input); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.fill(); if (options.showTokens) { for (Object tok : tokens.getTokens()) { System.out.println(tok); } } if (options.diagnostics) { parser.addErrorListener(new DiagnosticErrorListener()); parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); } parser.setBuildParseTree(true); // SLL overrides diagnostics // %%% Not sure what it really is though. if (options.sll) { parser.getInterpreter().setPredictionMode(PredictionMode.SLL); } parser.setTokenStream(tokens); parser.setTrace(options.trace); SpecificationContext tree = ((WebkitIDLParser)parser).specification(); if (options.printTree) { System.out.println(tree.toStringTree(parser)); } if (options.gui) { tree.inspect(parser); } if (options.psFile != null) { tree.save(parser, options.psFile); // Generate postscript } return tree; }
protected SpecificationContext parse(Lexer lexer, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, PrintException { ANTLRInputStream input = new ANTLRInputStream(r); lexer.setInputStream(input); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.fill(); if (options.showTokens) { for (Object tok : tokens.getTokens()) { System.out.println(tok); } } if (options.diagnostics) { parser.addErrorListener(new DiagnosticErrorListener()); parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); } parser.setBuildParseTree(true); // SLL overrides diagnostics // %%% Not sure what it really is though. if (options.sll) { parser.getInterpreter().setPredictionMode(PredictionMode.SLL); } parser.setTokenStream(tokens); parser.setTrace(options.trace); SpecificationContext tree = ((GeckoIDLParser)parser).specification(); if (options.printTree) { System.out.println(tree.toStringTree(parser)); } if (options.gui) { tree.inspect(parser); } if (options.psFile != null) { tree.save(parser, options.psFile); // Generate postscript } return tree; }
protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, InvocationTargetException, PrintException { try { ANTLRInputStream input = new ANTLRInputStream(r); lexer.setInputStream(input); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.fill(); if ( showTokens ) { for (Object tok : tokens.getTokens()) { System.out.println(tok); } } if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return; if ( diagnostics ) { parser.addErrorListener(new DiagnosticErrorListener()); parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); } if ( printTree || gui || psFile!=null ) { parser.setBuildParseTree(true); } if ( SLL ) { // overrides diagnostics parser.getInterpreter().setPredictionMode(PredictionMode.SLL); } parser.setTokenStream(tokens); parser.setTrace(trace); try { Method startRule = parserClass.getMethod(startRuleName); ParserRuleContext tree = (ParserRuleContext)startRule.invoke(parser, (Object[])null); if ( printTree ) { System.out.println(tree.toStringTree(parser)); } if ( gui ) { tree.inspect(parser); } if ( psFile!=null ) { tree.save(parser, psFile); // Generate postscript } } catch (NoSuchMethodException nsme) { System.err.println("No method for rule "+startRuleName+" or it has arguments"); } } finally { if ( r!=null ) r.close(); if ( is!=null ) is.close(); } }