From: <fwi...@us...> - 2008-06-27 17:47:54
|
Revision: 4810 http://jython.svn.sourceforge.net/jython/?rev=4810&view=rev Author: fwierzbicki Date: 2008-06-27 10:47:50 -0700 (Fri, 27 Jun 2008) Log Message: ----------- Cleaned up Call node creation, and now gen expressions are checked from both sides so that: foo(x for x in range(10), 100) foo(100, x for x in range(10)) properly fail. Modified Paths: -------------- branches/asm/grammar/Python.g branches/asm/grammar/PythonWalker.g Modified: branches/asm/grammar/Python.g =================================================================== --- branches/asm/grammar/Python.g 2008-06-27 07:24:14 UTC (rev 4809) +++ branches/asm/grammar/Python.g 2008-06-27 17:47:50 UTC (rev 4810) @@ -198,6 +198,10 @@ } } + private void throwGenExpNotSoleArg(PythonTree t) { + throw new ParseException("Generator expression must be parenthesized if not sole argument", t); + } + private exprType[] makeExprs(List exprs) { return makeExprs(exprs, 0); } @@ -1032,12 +1036,22 @@ ; //arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) -arglist : argument (COMMA argument)* +arglist : a1=argument[true] (COMMA a2+=argument[false])* ( COMMA ( STAR starargs=test[expr_contextType.Load] (COMMA DOUBLESTAR kwargs=test[expr_contextType.Load])? | DOUBLESTAR kwargs=test[expr_contextType.Load] )? - )? + )? { if ($a2 != null) { + if ($a1.gen) { + throwGenExpNotSoleArg($a1.tree); + } + for (int i=0;i<$a2.size();i++) { + if (((argument_return)$a2.get(i)).gen) { + throwGenExpNotSoleArg(((argument_return)$a2.get(i)).tree); + } + } + } + } -> ^(Args argument+) ^(StarArgs $starargs)? ^(KWArgs $kwargs)? | STAR starargs=test[expr_contextType.Load] (COMMA DOUBLESTAR kwargs=test[expr_contextType.Load])? -> ^(StarArgs $starargs) ^(KWArgs $kwargs)? @@ -1046,9 +1060,15 @@ ; //argument: test [gen_for] | test '=' test # Really [keyword '='] test -argument : t1=test[expr_contextType.Load] +argument[boolean first] returns [boolean gen] + : t1=test[expr_contextType.Load] ( (ASSIGN t2=test[expr_contextType.Load]) -> ^(Keyword ^(Arg $t1) ^(Value $t2)?) - | gen_for -> ^(GenFor $t1 gen_for) + | gen_for { if (!first) { + throwGenExpNotSoleArg($gen_for.tree); + } + $gen = true; + } + -> ^(GenFor $t1 gen_for) | -> ^(Arg $t1) ) ; Modified: branches/asm/grammar/PythonWalker.g =================================================================== --- branches/asm/grammar/PythonWalker.g 2008-06-27 07:24:14 UTC (rev 4809) +++ branches/asm/grammar/PythonWalker.g 2008-06-27 17:47:50 UTC (rev 4810) @@ -248,6 +248,26 @@ return new For(t, target, iter, b, o); } + private Call makeCall(PythonTree t, exprType func) { + return makeCall(t, func, null, null, null, null); + } + + private Call makeCall(PythonTree t, exprType func, List args, List keywords, exprType starargs, exprType kwargs) { + exprType[] a; + keywordType[] k; + if (args == null) { + a = new exprType[0]; + } else { + a = (exprType[])args.toArray(new exprType[args.size()]); + } + if (keywords == null) { + k = new keywordType[0]; + } else { + k = (keywordType[])keywords.toArray(new keywordType[keywords.size()]); + } + return new Call(t, func, a, k, starargs, kwargs); + } + //FIXME: just calling __neg__ for now - can be better. Also does not parse expressions like // --2 correctly (should give ^(USub -2) but gives 2). private exprType negate(PythonTree t, exprType o) { @@ -363,20 +383,12 @@ if ($Call == null) { decs.add($dotted_attr.etype); } else { - exprType[] args; - keywordType[] keywords; - exprType starargs = null; - exprType kwargs = null; - if ($Args != null) { - args = (exprType[])$arglist.args.toArray(new exprType[$arglist.args.size()]); - keywords = (keywordType[])$arglist.keywords.toArray(new keywordType[$arglist.keywords.size()]); - starargs = $arglist.starargs; - kwargs = $arglist.kwargs; + Call c; + if ($Args == null) { + c = makeCall($Call, $dotted_attr.etype); } else { - args = new exprType[0]; - keywords = new keywordType[0]; + c = makeCall($Call, $dotted_attr.etype, $arglist.args, $arglist.keywords, $arglist.starargs, $arglist.kwargs); } - Call c = new Call($Call, $dotted_attr.etype, args, keywords, starargs, kwargs); decs.add(c); } } @@ -454,13 +466,9 @@ : ^(Call (^(Args arglist))? test[expr_contextType.Load]) { Call c; if ($Args == null) { - c = new Call($test.marker, $test.etype, new exprType[0], new keywordType[0], null, null); - debug("Matched Call site no args"); + c = makeCall($test.marker, $test.etype); } else { - debug("Matched Call w/ args"); - exprType[] args = (exprType[])$arglist.args.toArray(new exprType[$arglist.args.size()]); - keywordType[] keywords = (keywordType[])$arglist.keywords.toArray(new keywordType[$arglist.keywords.size()]); - c = new Call($test.marker, $test.etype, args, keywords, $arglist.starargs, $arglist.kwargs); + c = makeCall($test.marker, $test.etype, $arglist.args, $arglist.keywords, $arglist.starargs, $arglist.kwargs); } $etype = c; } @@ -1318,11 +1326,6 @@ arguments.add($test.etype); } | ^(GenFor test[expr_contextType.Load] gen_for[gens]) { - if (!arguments.isEmpty()) { - throw new ParseException( - "Generator expression must be parenthesized if not sole argument", - $test.start); - } Collections.reverse(gens); comprehensionType[] c = (comprehensionType[])gens.toArray(new comprehensionType[gens.size()]); arguments.add(new GeneratorExp($GenFor, $test.etype, c)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |