From: <otm...@us...> - 2007-12-19 00:10:20
|
Revision: 3840 http://jython.svn.sourceforge.net/jython/?rev=3840&view=rev Author: otmarhumbel Date: 2007-12-18 16:10:18 -0800 (Tue, 18 Dec 2007) Log Message: ----------- preserve antlr recovery, including verbose error messages Modified Paths: -------------- trunk/sandbox/ast/grammar/Python.g trunk/sandbox/ast/src/org/python/antlr/PythonTreeWalker.java Modified: trunk/sandbox/ast/grammar/Python.g =================================================================== --- trunk/sandbox/ast/grammar/Python.g 2007-12-18 23:17:30 UTC (rev 3839) +++ trunk/sandbox/ast/grammar/Python.g 2007-12-19 00:10:18 UTC (rev 3840) @@ -189,26 +189,36 @@ } /** - * Overridden to throw an exception on a mismatched token (see p. 252) - */ - protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { - throw new MismatchedTokenException(ttype, input); + * A list holding the error message(s) encountered during parse. + */ + private List<String> _errors = new ArrayList<String>(); + + /** + * @return <code>true</code> if the parser collected one or more error messages, + * <code>false</code> otherwise. + */ + public boolean hasErrors() { + return getErrors().size() > 0; } /** - * Overridden to throw an exception on a mismatched token (see p. 252) - */ - public void recoverFromMismatchedSet(IntStream input, RecognitionException e, BitSet follow) throws RecognitionException { - throw e; + * @return A list of the error message(s) collected during parse. + */ + public List<String> getErrors() { + return _errors; } -} -// really throw recognition errors (see p. 252) -@rulecatch { - catch (RecognitionException e) { - throw e; + /** + * Overridden to be able to collect error messages. + * <p> + * Since we do not want to lose the recovery mechanism and the verbose messages. + */ + @Override + public void emitErrorMessage(String msg) { + super.emitErrorMessage(msg); + getErrors().add(msg); } -} +} @lexer::header { package org.python.antlr; Modified: trunk/sandbox/ast/src/org/python/antlr/PythonTreeWalker.java =================================================================== --- trunk/sandbox/ast/src/org/python/antlr/PythonTreeWalker.java 2007-12-18 23:17:30 UTC (rev 3839) +++ trunk/sandbox/ast/src/org/python/antlr/PythonTreeWalker.java 2007-12-19 00:10:18 UTC (rev 3840) @@ -36,6 +36,15 @@ parser.setTreeAdaptor(pyadaptor); try { PythonParser.file_input_return r = parser.file_input(); + if (parser.hasErrors()) { + // handle errors swallowed by antlr recovery + String errors = parser.getErrors().toString(); + if (isTolerant()) { + System.err.println(errors); + } else { + throw new RuntimeException(errors); + } + } if (args.length > 1) { System.out.println(((Tree) r.tree).toStringTree()); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |