From: <fwi...@us...> - 2009-01-09 03:19:54
|
Revision: 5894 http://jython.svn.sourceforge.net/jython/?rev=5894&view=rev Author: fwierzbicki Date: 2009-01-09 03:19:43 +0000 (Fri, 09 Jan 2009) Log Message: ----------- groundwork for dict and set comprehensions Modified Paths: -------------- branches/jy3k/grammar/Python.g Modified: branches/jy3k/grammar/Python.g =================================================================== --- branches/jy3k/grammar/Python.g 2009-01-08 22:54:04 UTC (rev 5893) +++ branches/jy3k/grammar/Python.g 2009-01-09 03:19:43 UTC (rev 5894) @@ -1226,9 +1226,8 @@ ) RBRACK | LCURLY - (dictmaker - -> ^(LCURLY<Dict>[$LCURLY, actions.castExprs($dictmaker.keys), - actions.castExprs($dictmaker.values)]) + (dictorsetmaker[$LCURLY] + -> dictorsetmaker | -> ^(LCURLY<Dict>[$LCURLY, new ArrayList<expr>(), new ArrayList<expr>()]) ) @@ -1250,8 +1249,8 @@ //listmaker: test ( list_for | (',' test)* [','] ) listmaker[Token lbrack] @init { + expr etype = null; List gens = new ArrayList(); - expr etype = null; } @after { $listmaker.tree = etype; @@ -1287,7 +1286,7 @@ ^(COMMA<Tuple>[$testlist_comp.start, actions.castExprs($t), $expr::ctype]) -> test ) - | (gen_for[gens] + | (comp_for[gens] { Collections.reverse(gens); List<comprehension> c = gens; @@ -1409,15 +1408,22 @@ | test[ctype] ; -//dictmaker: test ':' test (',' test ':' test)* [','] -dictmaker returns [List keys, List values] +//dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) | +// (test (comp_for | (',' test)* [','])) ) +dictorsetmaker[Token lcurly] +@init { + expr etype = null; + List gens = new ArrayList(); +} +@after { + $dictorsetmaker.tree = etype; +} : k+=test[expr_contextType.Load] COLON v+=test[expr_contextType.Load] (options {k=2;}:COMMA k+=test[expr_contextType.Load] COLON v+=test[expr_contextType.Load])* (COMMA)? - { - $keys = $k; - $values= $v; - } + { + etype = new Dict($lcurly, actions.castExprs($k), actions.castExprs($v)); + } ; //classdef: 'class' NAME ['(' [testlist] ')'] ':' suite @@ -1444,7 +1450,9 @@ } ; -//arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) +//arglist: (argument ',')* (argument [','] +// |'*' test (',' argument)* [',' '**' test] +// |'**' test) arglist returns [List args, List keywords, expr starargs, expr kwargs] @init { List arguments = new ArrayList(); @@ -1477,7 +1485,7 @@ } ; -//argument: test [gen_for] | test '=' test # Really [keyword '='] test +//argument: test [comp_for] | test '=' test # Really [keyword '='] test argument[List arguments, List kws, List gens, boolean first] returns [boolean genarg] : t1=test[expr_contextType.Load] ((ASSIGN t2=test[expr_contextType.Load]) @@ -1487,10 +1495,10 @@ exprs.add(actions.castExpr($t2.tree)); $kws.add(exprs); } - | gen_for[$gens] + | comp_for[$gens] { if (!first) { - actions.errorGenExpNotSoleArg($gen_for.tree); + actions.errorGenExpNotSoleArg($comp_for.tree); } $genarg = true; Collections.reverse($gens); @@ -1532,27 +1540,27 @@ } ; -//gen_iter: gen_for | gen_if -gen_iter [List gens, List ifs] - : gen_for[gens] - | gen_if[gens, ifs] +//comp_iter: comp_for | comp_if +comp_iter [List gens, List ifs] + : comp_for[gens] + | comp_if[gens, ifs] ; -//gen_for: 'for' exprlist 'in' or_test [gen_iter] -gen_for [List gens] +//comp_for: 'for' exprlist 'in' or_test [comp_iter] +comp_for [List gens] @init { List ifs = new ArrayList(); } - : FOR exprlist[expr_contextType.Store] IN or_test[expr_contextType.Load] gen_iter[gens, ifs]? + : FOR exprlist[expr_contextType.Store] IN or_test[expr_contextType.Load] comp_iter[gens, ifs]? { Collections.reverse(ifs); gens.add(new comprehension($FOR, $exprlist.etype, actions.castExpr($or_test.tree), ifs)); } ; -//gen_if: 'if' old_test [gen_iter] -gen_if[List gens, List ifs] - : IF test[expr_contextType.Load] gen_iter[gens, ifs]? +//comp_if: 'if' test_nocond [comp_iter] +comp_if[List gens, List ifs] + : IF test[expr_contextType.Load] comp_iter[gens, ifs]? { ifs.add(actions.castExpr($test.tree)); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |