From: <fwi...@us...> - 2009-01-07 21:21:54
|
Revision: 5870 http://jython.svn.sourceforge.net/jython/?rev=5870&view=rev Author: fwierzbicki Date: 2009-01-07 21:21:48 +0000 (Wed, 07 Jan 2009) Log Message: ----------- Use new arg node in function defs. Modified Paths: -------------- branches/jy3k/grammar/Python.g branches/jy3k/src/org/python/antlr/ErrorHandler.java branches/jy3k/src/org/python/antlr/FailFastHandler.java branches/jy3k/src/org/python/antlr/GrammarActions.java branches/jy3k/src/org/python/antlr/ListErrorHandler.java branches/jy3k/src/org/python/compiler/ArgListCompiler.java Added Paths: ----------- branches/jy3k/src/org/python/antlr/ast/ErrorArg.java Modified: branches/jy3k/grammar/Python.g =================================================================== --- branches/jy3k/grammar/Python.g 2009-01-07 18:12:46 UTC (rev 5869) +++ branches/jy3k/grammar/Python.g 2009-01-07 21:21:48 UTC (rev 5870) @@ -455,20 +455,21 @@ ; //not in CPython's Grammar file -tdefparameter[List defaults] returns [expr etype] +tdefparameter[List defaults] returns [arg etype] @after { $tdefparameter.tree = $etype; } - : vfpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? + : tfpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? { - $etype = actions.castExpr($vfpdef.tree); + $etype = actions.castArg($tfpdef.tree); if ($ASSIGN != null) { defaults.add($test.tree); } else if (!defaults.isEmpty()) { - throw new ParseException("non-default argument follows default argument", $vfpdef.tree); + throw new ParseException("non-default argument follows default argument", $tfpdef.tree); } } ; + //typedargslist: ((tfpdef ['=' test] ',')* // ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) // | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) @@ -497,11 +498,8 @@ //tfpdef: NAME [':' test] tfpdef[expr_contextType ctype] -@after { - actions.checkAssign(actions.castExpr($tfpdef.tree)); -} - : NAME (':' test[ctype])? - -> ^(NAME<Name>[$NAME, $NAME.text, ctype]) + : NAME (COLON test[ctype])? + -> ^(NAME<arg>[$NAME, $NAME.text, null]) ; //not in CPython's Grammar file Modified: branches/jy3k/src/org/python/antlr/ErrorHandler.java =================================================================== --- branches/jy3k/src/org/python/antlr/ErrorHandler.java 2009-01-07 18:12:46 UTC (rev 5869) +++ branches/jy3k/src/org/python/antlr/ErrorHandler.java 2009-01-07 21:21:48 UTC (rev 5870) @@ -5,6 +5,7 @@ import org.antlr.runtime.IntStream; import org.antlr.runtime.Lexer; import org.antlr.runtime.RecognitionException; +import org.python.antlr.ast.arg; import org.python.antlr.base.expr; import org.python.antlr.base.mod; import org.python.antlr.base.slice; @@ -27,11 +28,12 @@ Object recoverFromMismatchedToken(BaseRecognizer br, IntStream input, int ttype, BitSet follow) throws RecognitionException; - //expr, mod, slice, stmt + //expr, mod, slice, stmt, arg expr errorExpr(PythonTree t); mod errorMod(PythonTree t); slice errorSlice(PythonTree t); stmt errorStmt(PythonTree t); + arg errorArg(PythonTree t); //exceptions void error(String message, PythonTree t); Modified: branches/jy3k/src/org/python/antlr/FailFastHandler.java =================================================================== --- branches/jy3k/src/org/python/antlr/FailFastHandler.java 2009-01-07 18:12:46 UTC (rev 5869) +++ branches/jy3k/src/org/python/antlr/FailFastHandler.java 2009-01-07 21:21:48 UTC (rev 5870) @@ -6,6 +6,7 @@ import org.antlr.runtime.Lexer; import org.antlr.runtime.MismatchedTokenException; import org.antlr.runtime.RecognitionException; +import org.python.antlr.ast.arg; import org.python.antlr.base.expr; import org.python.antlr.base.mod; import org.python.antlr.base.slice; @@ -52,6 +53,10 @@ throw new ParseException("Bad Stmt Node", t); } + public arg errorArg(PythonTree t) { + throw new ParseException("Bad Arg Node", t); + } + public void error(String message, PythonTree t) { throw new ParseException(message, t); } Modified: branches/jy3k/src/org/python/antlr/GrammarActions.java =================================================================== --- branches/jy3k/src/org/python/antlr/GrammarActions.java 2009-01-07 18:12:46 UTC (rev 5869) +++ branches/jy3k/src/org/python/antlr/GrammarActions.java 2009-01-07 21:21:48 UTC (rev 5870) @@ -156,13 +156,22 @@ List<arg> result = new ArrayList<arg>(); if (args != null) { for (Object o : args) { - if (o instanceof arg) { - result.add((arg)o); - } + result.add(castArg(o)); } } return result; } + + arg castArg(Object o) { + if (o instanceof arg) { + return (arg)o; + } + if (o instanceof PythonTree) { + return errorHandler.errorArg((PythonTree)o); + } + errorHandler.error("Bad Arg node", null); + return null; + } List<stmt> makeElse(List elseSuite, PythonTree elif) { if (elseSuite != null) { @@ -660,7 +669,6 @@ } void checkDelete(expr e) { - //System.out.println("trying to del " + e); if (e instanceof Call) { errorHandler.error("can't delete function call", e); } else if (e instanceof Num) { Modified: branches/jy3k/src/org/python/antlr/ListErrorHandler.java =================================================================== --- branches/jy3k/src/org/python/antlr/ListErrorHandler.java 2009-01-07 18:12:46 UTC (rev 5869) +++ branches/jy3k/src/org/python/antlr/ListErrorHandler.java 2009-01-07 21:21:48 UTC (rev 5870) @@ -5,6 +5,8 @@ import org.antlr.runtime.IntStream; import org.antlr.runtime.Lexer; import org.antlr.runtime.RecognitionException; +import org.python.antlr.ast.arg; +import org.python.antlr.ast.ErrorArg; import org.python.antlr.ast.ErrorMod; import org.python.antlr.ast.ErrorExpr; import org.python.antlr.ast.ErrorSlice; @@ -52,6 +54,10 @@ return new ErrorStmt(t); } + public arg errorArg(PythonTree t) { + return new ErrorArg(t); + } + public void error(String message, PythonTree t) { System.err.println(message); } Added: branches/jy3k/src/org/python/antlr/ast/ErrorArg.java =================================================================== --- branches/jy3k/src/org/python/antlr/ast/ErrorArg.java (rev 0) +++ branches/jy3k/src/org/python/antlr/ast/ErrorArg.java 2009-01-07 21:21:48 UTC (rev 5870) @@ -0,0 +1,38 @@ +package org.python.antlr.ast; +import org.python.antlr.PythonTree; +import org.antlr.runtime.CommonToken; +import org.antlr.runtime.Token; +import java.io.DataOutputStream; +import java.io.IOException; + +public class ErrorArg extends arg { + + public ErrorArg(PythonTree tree) { + super(tree, null, null); + } + + public String toString() { + return "ErrorArg"; + } + + public String toStringTree() { + return "ErrorArg"; + } + + public int getLineno() { + return getLine(); + } + + public int getCol_offset() { + return getCharPositionInLine(); + } + + public <R> R accept(VisitorIF<R> visitor) { + return null; + } + + public void traverse(VisitorIF visitor) throws Exception { + //no op. + } + +} Modified: branches/jy3k/src/org/python/compiler/ArgListCompiler.java =================================================================== --- branches/jy3k/src/org/python/compiler/ArgListCompiler.java 2009-01-07 18:12:46 UTC (rev 5869) +++ branches/jy3k/src/org/python/compiler/ArgListCompiler.java 2009-01-07 21:21:48 UTC (rev 5870) @@ -11,6 +11,7 @@ import org.python.antlr.ast.Name; import org.python.antlr.ast.Suite; import org.python.antlr.ast.Tuple; +import org.python.antlr.ast.arg; import org.python.antlr.ast.arguments; import org.python.antlr.ast.expr_contextType; import org.python.antlr.base.expr; @@ -47,9 +48,19 @@ return defaults; } + public Object visitArg(arg node) throws Exception { + if (fpnames.contains(node.getInternalArg())) { + throw new ParseException("duplicate argument name found: " + + node.getInternalArg(), node); + } + fpnames.add(node.getInternalArg()); + return node.getInternalArg(); + } + + public void visitArgs(arguments args) throws Exception { for (int i = 0; i < args.getInternalArgs().size(); i++) { - String name = (String) visit(args.getInternalArgs().get(i)); + String name = (String) visitArg(args.getInternalArgs().get(i)); names.add(name); } if (args.getInternalVararg() != null) { @@ -85,6 +96,7 @@ return node.getInternalId(); } + /* @Override public Object visitTuple(Tuple node) throws Exception { StringBuffer name = new StringBuffer("("); @@ -97,4 +109,5 @@ name.append(")"); return name.toString(); } + */ } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |