From: <fwi...@us...> - 2007-12-15 21:04:28
|
Revision: 3808 http://jython.svn.sourceforge.net/jython/?rev=3808&view=rev Author: fwierzbicki Date: 2007-12-15 13:04:26 -0800 (Sat, 15 Dec 2007) Log Message: ----------- somewhat better Bases handling for ClassDef -- though I want a better solution at some point (see comments in PythonWalker.g) Modified Paths: -------------- trunk/sandbox/ast/grammar/Python.g trunk/sandbox/ast/grammar/PythonWalker.g Modified: trunk/sandbox/ast/grammar/Python.g =================================================================== --- trunk/sandbox/ast/grammar/Python.g 2007-12-15 15:29:32 UTC (rev 3807) +++ trunk/sandbox/ast/grammar/Python.g 2007-12-15 21:04:26 UTC (rev 3808) @@ -667,7 +667,7 @@ //classdef: 'class' NAME ['(' testlist ')'] ':' suite classdef: 'class' NAME (LPAREN testlist RPAREN)? COLON suite - -> ^(ClassDef ^(Name NAME) ^(Bases testlist?) ^(Body suite)) + -> ^(ClassDef ^(Name NAME) ^(Bases testlist)? ^(Body suite)) ; //arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) Modified: trunk/sandbox/ast/grammar/PythonWalker.g =================================================================== --- trunk/sandbox/ast/grammar/PythonWalker.g 2007-12-15 15:29:32 UTC (rev 3807) +++ trunk/sandbox/ast/grammar/PythonWalker.g 2007-12-15 21:04:26 UTC (rev 3808) @@ -47,6 +47,7 @@ import org.python.antlr.ast.Str; import org.python.antlr.ast.Yield; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -683,7 +684,8 @@ $etype = new Name($NAME, $NAME.text, ctype); } | ^(DOT NAME test[ctype]) { - $etype = new Attribute($DOT, $atom.etype, $NAME.text, expr_contextType.Load); + debug("matched DOT in atom: " + $test.etype + "###" + $NAME.text); + $etype = new Attribute($DOT, $test.etype, $NAME.text, expr_contextType.Load); } | ^(SubscriptList subscriptlist test[ctype]) | ^(Num INT) {$etype = makeNum($INT);} @@ -718,8 +720,14 @@ ; classdef - : ^(ClassDef ^(Name classname=NAME) bases ^(Body stmts)) { - $stmts::statements.add(makeClassDef($ClassDef, $classname, $bases.names, $stmts.stypes)); + : ^(ClassDef ^(Name classname=NAME) (^(Bases bases))? ^(Body stmts)) { + List b; + if ($Bases != null) { + b = $bases.names; + } else { + b = new ArrayList(); + } + $stmts::statements.add(makeClassDef($ClassDef, $classname, b, $stmts.stypes)); } ; @@ -727,13 +735,29 @@ @init { List nms = new ArrayList(); } - :^(Bases base[nms]*) { - $names = nms; + : base[nms] { + //The instanceof and tuple unpack here is gross. I *should* be able to detect + //"Tuple or Tuple DOWN or some such in a syntactic predicate in the "base" rule + //instead, but I haven't been able to get it to work. + if (nms.get(0) instanceof Tuple) { + debug("TUPLE"); + Tuple t = (Tuple)nms.get(0); + $names = Arrays.asList(t.elts); + } else { + debug("NOT TUPLE"); + $names = nms; + } } ; +//FIXME: right now test matches a Tuple from Python.g output -- I'd rather +// unpack the tuple here instead of in bases, otherwise this rule +// should just get absorbed back into bases, since it is never matched +// more than once as it is now. base[List names] - : test[expr_contextType.Store] {names.add($test.etype);} + : test[expr_contextType.Load] { + names.add($test.etype); + } ; arglist returns [List args, List keywords, exprType starargs, exprType kwargs] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |