From: <fwi...@us...> - 2008-05-26 02:27:12
|
Revision: 4452 http://jython.svn.sourceforge.net/jython/?rev=4452&view=rev Author: fwierzbicki Date: 2008-05-25 19:27:07 -0700 (Sun, 25 May 2008) Log Message: ----------- Merged revisions 4445 via svnmerge from https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython ........ r4445 | fwierzbicki | 2008-05-25 08:47:37 -0400 (Sun, 25 May 2008) | 3 lines Fail on first syntax error. Use antlr's RecognitionException to handle syntax errors. ........ Modified Paths: -------------- branches/asm/ast/astview.py branches/asm/grammar/Python.g branches/asm/grammar/PythonWalker.g branches/asm/src/org/python/antlr/PythonGrammar.java branches/asm/src/org/python/core/antlr.java Property Changed: ---------------- branches/asm/ Property changes on: branches/asm ___________________________________________________________________ Name: svnmerge-integrated - /trunk/jython:1-4443 + /trunk/jython:1-4445 Modified: branches/asm/ast/astview.py =================================================================== --- branches/asm/ast/astview.py 2008-05-25 23:28:57 UTC (rev 4451) +++ branches/asm/ast/astview.py 2008-05-26 02:27:07 UTC (rev 4452) @@ -70,7 +70,10 @@ yield node def tree(pyfile): - ast = compile(open(pyfile).read(), pyfile, "exec", _ast.PyCF_ONLY_AST) + try: + ast = compile(open(pyfile).read(), pyfile, "exec", _ast.PyCF_ONLY_AST) + except SyntaxError: + return "SyntaxError", return lispify_ast(ast) if __name__ == '__main__': Modified: branches/asm/grammar/Python.g =================================================================== --- branches/asm/grammar/Python.g 2008-05-25 23:28:57 UTC (rev 4451) +++ branches/asm/grammar/Python.g 2008-05-26 02:27:07 UTC (rev 4452) @@ -199,6 +199,14 @@ } } + protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { + throw new MismatchedTokenException(ttype, input); + } + + protected void mismatch(IntStream input, RecognitionException e, BitSet follow) throws RecognitionException { + throw e; + } + /** * A list holding the error message(s) encountered during parse. */ @@ -229,8 +237,15 @@ super.emitErrorMessage(msg); getErrors().add(msg); } + } +@rulecatch { +catch (RecognitionException e) { + throw e; +} +} + @lexer::header { package org.python.antlr; } @@ -392,7 +407,7 @@ ( t1=printlist -> {$t1.newline}? ^(Print 'print' ^(Values $t1) ^(Newline)) -> ^(Print 'print' ^(Values $t1)) | RIGHTSHIFT t2=printlist -> {$t2.newline}? ^(Print 'print' ^(Dest RIGHTSHIFT) ^(Values $t2) ^(Newline)) - -> ^(Print 'print' ^(Dest RIGHTSHIFT) ^(Values $t2)) + -> ^(Print 'print' ^(Dest RIGHTSHIFT) ^(Values $t2)) | -> ^(Print 'print' ^(Newline)) ) ; Modified: branches/asm/grammar/PythonWalker.g =================================================================== --- branches/asm/grammar/PythonWalker.g 2008-05-25 23:28:57 UTC (rev 4451) +++ branches/asm/grammar/PythonWalker.g 2008-05-26 02:27:07 UTC (rev 4452) @@ -553,8 +553,8 @@ ; target[List etypes] - : ^(Target test[expr_contextType.Store]) { - etypes.add($test.etype); + : ^(Target atom[expr_contextType.Store]) { + etypes.add($atom.etype); } ; Modified: branches/asm/src/org/python/antlr/PythonGrammar.java =================================================================== --- branches/asm/src/org/python/antlr/PythonGrammar.java 2008-05-25 23:28:57 UTC (rev 4451) +++ branches/asm/src/org/python/antlr/PythonGrammar.java 2008-05-26 02:27:07 UTC (rev 4452) @@ -67,7 +67,7 @@ } //XXX: factor out common code. - public modType file_input() { + public modType file_input() throws RecognitionException { modType tree = null; PythonLexer lexer = new PyLexer(this.charStream); CommonTokenStream tokens = new CommonTokenStream(lexer); @@ -77,27 +77,22 @@ PythonParser parser = new PythonParser(tokens); parser.setTreeAdaptor(pyadaptor); - try { - Object rx = parser.file_input(); - PythonParser.file_input_return r = (PythonParser.file_input_return)rx; - CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); - nodes.setTokenStream(tokens); - PythonWalker walker = new PythonWalker(nodes); - tree = walker.module(); - if (tree == null) { - //XXX: seems like I should be able to get antlr to give me an empty Module instead - // of null so I wouldn't need to build an empty Module by hand here... - return new Module(new PythonTree(new CommonToken(PyLexer.Module)), new stmtType[0]); - } - } catch (RecognitionException e) { - // FIXME: - System.err.println("FIXME: don't eat exceptions:" + e); + Object rx = parser.file_input(); + PythonParser.file_input_return r = (PythonParser.file_input_return)rx; + CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); + nodes.setTokenStream(tokens); + PythonWalker walker = new PythonWalker(nodes); + tree = walker.module(); + if (tree == null) { + //XXX: seems like I should be able to get antlr to give me an empty Module instead + // of null so I wouldn't need to build an empty Module by hand here... + return new Module(new PythonTree(new CommonToken(PyLexer.Module)), new stmtType[0]); } return tree; } //XXX: factor out common code. - public modType single_input() { + public modType single_input() throws RecognitionException { modType tree = null; PythonLexer lexer = new PyLexer(this.charStream); CommonTokenStream tokens = new CommonTokenStream(lexer); @@ -106,18 +101,12 @@ tokens = new CommonTokenStream(indentedSource); PythonParser parser = new PythonParser(tokens); parser.setTreeAdaptor(pyadaptor); - try { - Object rx = parser.single_input(); - PythonParser.single_input_return r = (PythonParser.single_input_return)rx; - CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); - nodes.setTokenStream(tokens); - PythonWalker walker = new PythonWalker(nodes); - tree = walker.interactive(); - } catch (RecognitionException e) { - // FIXME: - System.err.println("FIXME: don't eat exceptions:" + e); - } + Object rx = parser.single_input(); + PythonParser.single_input_return r = (PythonParser.single_input_return)rx; + CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); + nodes.setTokenStream(tokens); + PythonWalker walker = new PythonWalker(nodes); + tree = walker.interactive(); return tree; } - } Modified: branches/asm/src/org/python/core/antlr.java =================================================================== --- branches/asm/src/org/python/core/antlr.java 2008-05-25 23:28:57 UTC (rev 4451) +++ branches/asm/src/org/python/core/antlr.java 2008-05-26 02:27:07 UTC (rev 4452) @@ -12,9 +12,11 @@ import org.antlr.runtime.ANTLRReaderStream; import org.antlr.runtime.CharStream; import org.antlr.runtime.RecognitionException; +import org.antlr.runtime.*; import org.python.antlr.ExpressionParser; import org.python.antlr.LeadingSpaceSkippingStream; import org.python.antlr.PythonGrammar; +import org.python.antlr.PythonParser; import org.python.core.util.StringUtil; import org.python.antlr.IParserHost; import org.python.antlr.PythonTree; @@ -58,34 +60,96 @@ if (t instanceof RecognitionException) { RecognitionException e = (RecognitionException)t; - //FJW Token tok = e.currentToken; - int col=0; - int line=0; - //FJW if (tok != null && tok.next != null) { - //FJW col = tok.next.beginColumn; - //FJW line = tok.next.beginLine; - //FJW } - String text=getLine(reader, line); - return new PySyntaxError(e.getMessage(), line, col, + String msg = e.getMessage(); + String tokenNames[] = PythonParser.tokenNames; + /* XXX: think the first two are new in antlr 3.1 + if ( e instanceof UnwantedTokenException ) { + UnwantedTokenException ute = (UnwantedTokenException)e; + String tokenName="<unknown>"; + if ( ute.expecting== Token.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[ute.expecting]; + } + msg = "extraneous input "+getTokenErrorDisplay(ute.getUnexpectedToken())+ + " expecting "+tokenName; + } + else if ( e instanceof MissingTokenException ) { + MissingTokenException mte = (MissingTokenException)e; + String tokenName="<unknown>"; + if ( mte.expecting== Token.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[mte.expecting]; + } + msg = "missing "+tokenName+" at "+getTokenErrorDisplay(e.token); + } + */ + if ( e instanceof MismatchedTokenException ) { + MismatchedTokenException mte = (MismatchedTokenException)e; + String tokenName="<unknown>"; + if ( mte.expecting== Token.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[mte.expecting]; + } + msg = "mismatched input "+getTokenErrorDisplay(e.token)+ + " expecting "+tokenName; + } + else if ( e instanceof MismatchedTreeNodeException ) { + MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e; + String tokenName="<unknown>"; + if ( mtne.expecting==Token.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[mtne.expecting]; + } + msg = "mismatched tree node: "+mtne.node+ + " expecting "+tokenName; + } + else if ( e instanceof NoViableAltException ) { + NoViableAltException nvae = (NoViableAltException)e; + // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>" + // and "(decision="+nvae.decisionNumber+") and + // "state "+nvae.stateNumber + msg = "no viable alternative at input "+getTokenErrorDisplay(e.token); + } + else if ( e instanceof EarlyExitException ) { + EarlyExitException eee = (EarlyExitException)e; + // for development, can add "(decision="+eee.decisionNumber+")" + msg = "required (...)+ loop did not match anything at input "+ + getTokenErrorDisplay(e.token); + } + else if ( e instanceof MismatchedSetException ) { + MismatchedSetException mse = (MismatchedSetException)e; + msg = "mismatched input "+getTokenErrorDisplay(e.token)+ + " expecting set "+mse.expecting; + } + else if ( e instanceof MismatchedNotSetException ) { + MismatchedNotSetException mse = (MismatchedNotSetException)e; + msg = "mismatched input "+getTokenErrorDisplay(e.token)+ + " expecting set "+mse.expecting; + } + else if ( e instanceof FailedPredicateException ) { + FailedPredicateException fpe = (FailedPredicateException)e; + msg = "rule "+fpe.ruleName+" failed predicate: {"+ + fpe.predicateText+"}?"; + } + String text=getLine(reader, e.line); + return new PySyntaxError(msg, e.line, e.charPositionInLine, text, filename); } - /* FJW FJW FJW - if (t instanceof TokenMgrError) { - TokenMgrError e = (TokenMgrError)t; - boolean eofSeen = e.EOFSeen; - - int col = e.errorColumn; - int line = e.errorLine; - String text = getLine(reader, line); - if (eofSeen) - col -= 1; - return new PySyntaxError(e.getMessage(), line, col, - text, filename); - } - */ else return Py.JavaError(t); } + private static String getTokenErrorDisplay(Token t) { + return t.getText(); + } + public static PythonTree parse(String string, String kind) { return parse(new ByteArrayInputStream(StringUtil.toBytes(string)), kind, "<string>", null); @@ -158,7 +222,7 @@ } private static modType doparse(String kind, CompilerFlags cflags, - PythonGrammar g) throws /*Parse*/Exception //FJW + PythonGrammar g) throws RecognitionException { modType node = null; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |