List of usage examples for org.antlr.v4.runtime Parser setErrorHandler
public void setErrorHandler(ANTLRErrorStrategy handler)
From source file:es.ucm.fdi.ac.parser.AntlrTokenizer.java
License:Open Source License
public void tokenize(String source, String sourceFile, PrintWriter out) { Writer debugWriter = null;// w w w. j av a2s . c o m try { Lexer lexer = (Lexer) language.lexerConstructor.newInstance(new ANTLRInputStream(source)); final CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.fill(); if (log.isDebugEnabled()) { try { debugWriter = new BufferedWriter( new FileWriter(Files.createTempFile("tokens-" + NDC.get() + "-", ".txt").toFile())); } catch (IOException ioe) { log.warn("Could not create debugWriter", ioe); } } for (final Token tok : tokens.getTokens()) { out.print(tokenToString(tok)); if (log.isDebugEnabled()) { log.debug(tok); if (debugWriter != null) { debugWriter.write(tokenToString(tok)); } } } if (parse) { Parser parser = (Parser) language.parserConstructor.newInstance(tokens); parser.setErrorHandler(new BailErrorStrategy()); ParserRuleContext parserRuleContext = (ParserRuleContext) language.parserMethod.invoke(parser); if (log.isDebugEnabled()) { log.debug(Trees.toStringTree(parserRuleContext, parser)); } } } catch (Exception e) { e.printStackTrace(); throw new IllegalArgumentException("Bad token in source, or failed to parse", e); } finally { out.flush(); if (log.isDebugEnabled() && debugWriter != null) { try { debugWriter.close(); } catch (IOException ioe) { log.warn("Could not close debugWriter", ioe); } } } }
From source file:org.tvl.goworks.editor.go.parser.GoParserFactory.java
License:Open Source License
protected void configureParser(@NonNull Parser parser, @NonNull ParserConfiguration configuration) { ParserATNSimulator interpreter = parser.getInterpreter(); // common configuration interpreter.force_global_context = false; interpreter.always_try_local_context = true; interpreter.optimize_tail_calls = true; parser.setBuildParseTree(true);/*from w w w. j a va 2 s . c om*/ parser.removeErrorListeners(); switch (configuration) { case FASTEST: interpreter.setPredictionMode(PredictionMode.SLL); interpreter.tail_call_preserves_sll = false; interpreter.treat_sllk1_conflict_as_ambiguity = true; parser.setErrorHandler(new BailErrorStrategy()); break; case SLL: throw new UnsupportedOperationException( "The tail_call_preserves_sll flag cannot change within a single ATN instance."); //interpreter.setPredictionMode(PredictionMode.SLL); //interpreter.tail_call_preserves_sll = true; //interpreter.treat_sllk1_conflict_as_ambiguity = true; //parser.setErrorHandler(new BailErrorStrategy<Token>()); //break; case HYBRID: interpreter.setPredictionMode(PredictionMode.LL); interpreter.tail_call_preserves_sll = false; interpreter.treat_sllk1_conflict_as_ambiguity = true; parser.setErrorHandler(new BailErrorStrategy()); break; case HYBRID_SLL: throw new UnsupportedOperationException( "The tail_call_preserves_sll flag cannot change within a single ATN instance."); //interpreter.setPredictionMode(PredictionMode.LL); //interpreter.tail_call_preserves_sll = true; //interpreter.treat_sllk1_conflict_as_ambiguity = true; //parser.setErrorHandler(new BailErrorStrategy<Token>()); //break; case PRECISE: interpreter.setPredictionMode(PredictionMode.LL); interpreter.tail_call_preserves_sll = false; interpreter.treat_sllk1_conflict_as_ambiguity = false; parser.setErrorHandler(new DefaultErrorStrategy()); parser.addErrorListener(DescriptiveErrorListener.INSTANCE); break; default: throw new IllegalArgumentException("Invalid configuration."); } }