From: <fwi...@us...> - 2007-12-08 18:10:08
|
Revision: 3776 http://jython.svn.sourceforge.net/jython/?rev=3776&view=rev Author: fwierzbicki Date: 2007-12-08 10:10:06 -0800 (Sat, 08 Dec 2007) Log Message: ----------- Adding FunctionDef to output. Modified Paths: -------------- trunk/sandbox/ast/grammar/Python.g trunk/sandbox/ast/grammar/PythonWalker.g Added Paths: ----------- trunk/sandbox/ast/t3.py Modified: trunk/sandbox/ast/grammar/Python.g =================================================================== --- trunk/sandbox/ast/grammar/Python.g 2007-12-08 17:22:05 UTC (rev 3775) +++ trunk/sandbox/ast/grammar/Python.g 2007-12-08 18:10:06 UTC (rev 3776) @@ -214,7 +214,7 @@ //funcdef: [decorators] 'def' NAME parameters ':' suite funcdef : decorators? 'def' NAME parameters COLON suite - -> ^(FunctionDef ^(Name NAME) ^(Arguments parameters) ^(Body suite) ^(Decorators decorators)?) + -> ^(FunctionDef ^(Name NAME) ^(Arguments parameters) ^(Body suite) ^(Decorators decorators?)) ; //parameters: '(' [varargslist] ')' Modified: trunk/sandbox/ast/grammar/PythonWalker.g =================================================================== --- trunk/sandbox/ast/grammar/PythonWalker.g 2007-12-08 17:22:05 UTC (rev 3775) +++ trunk/sandbox/ast/grammar/PythonWalker.g 2007-12-08 18:10:06 UTC (rev 3776) @@ -69,19 +69,27 @@ return new Module(t, s); } - private FunctionDef makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements) { + private FunctionDef makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements, List decorators) { argumentsType a; + debug("Matched FunctionDef"); if (args != null) { a = args; } else { a = new argumentsType(t, new exprType[0], null, null, new exprType[0]); } stmtType[] s = (stmtType[])funcStatements.toArray(new stmtType[funcStatements.size()]); - return new FunctionDef(t, nameToken.getText(), a, s, null); + exprType[] d; + if (decorators != null) { + d = (exprType[])decorators.toArray(new exprType[decorators.size()]); + } else { + d = new exprType[0]; + } + return new FunctionDef(t, nameToken.getText(), a, s, d); } private argumentsType makeArgumentsType(PythonTree t, List params, PythonTree snameToken, PythonTree knameToken, List defaults) { + debug("Matched Arguments"); exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); @@ -191,29 +199,51 @@ } ; -decorator: ^(Decorator dotted_name ^(ArgList arglist?)) - ; +funcdef + : ^(FunctionDef ^(Name NAME) ^(Arguments varargslist?) ^(Body stmts) ^(Decorators decorators?)) { + $stmts::statements.add(makeFunctionDef($FunctionDef, $NAME, $varargslist.args, $stmts.stypes, $decorators.etypes)); + } + ; -decorators: decorator+ - ; - -funcdef : ^(FunctionDef ^(Name NAME) ^(Arguments varargslist?) ^(Body suite) (^(Decorators decorators))? ) - ; - -varargslist - : ^(Args defparameter*) (^(StarArgs sname=NAME))? (^(KWArgs kname=NAME))? { +varargslist returns [argumentsType args] +@init { + List params = new ArrayList(); + List defaults = new ArrayList(); +} + : ^(Args defparameter[params, defaults]+) (^(StarArgs sname=NAME))? (^(KWArgs kname=NAME))? { + $args = makeArgumentsType($Args,params, $sname, $kname, defaults); } | ^(StarArgs sname=NAME) (^(KWArgs kname=NAME))? { + $args = makeArgumentsType($StarArgs,params, $sname, $kname, defaults); } | ^(KWArgs NAME) { + $args = makeArgumentsType($KWArgs, params, null, $NAME, defaults); } ; -defparameter +defparameter[List params, List defaults] : NAME (ASSIGN test[expr_contextType.Load] )? { + params.add(new Name($NAME, $NAME.text, org.python.antlr.ast.Name.Param)); + if ($test.etype != null) { + defaults.add($test.etype); + } } ; +decorator + : ^(Decorator dotted_name ^(ArgList arglist?)) + ; + +decorators returns [List etypes] +@init { + List decs = new ArrayList(); +} + : decorator+ { + $etypes = decs; + } + ; + + stmts returns [List stypes] scope { List statements; @@ -222,8 +252,13 @@ $stmts::statements = new ArrayList(); } : stmt+ { + debug("Matched stmts"); $stypes = $stmts::statements; } + | INDENT stmt+ DEDENT { + debug("Matched stmts"); + $stypes = $stmts::statements; + } ; stmt //combines simple_stmt and compound_stmt from Python.g @@ -304,6 +339,7 @@ pass_stmt : Pass { + debug("Matched Pass"); } ; @@ -477,7 +513,7 @@ | ^(Parens test[ctype]*) {} | ^(Dict test[ctype]*) {} | ^(Repr test[ctype]*) {} - | ^(Name NAME) {$etype = new Name($NAME, $NAME.text, ctype);} + | ^(Name NAME) {$etype = new Name($NAME, $NAME.text, ctype); debug("Matched Name");} | ^(Num INT) {$etype = makeNum($INT);} | ^(Num LONGINT) {$etype = makeNum($LONGINT);} | ^(Num FLOAT) Added: trunk/sandbox/ast/t3.py =================================================================== --- trunk/sandbox/ast/t3.py (rev 0) +++ trunk/sandbox/ast/t3.py 2007-12-08 18:10:06 UTC (rev 3776) @@ -0,0 +1,2 @@ +def hello(spam, *eggs, **foo): + pass This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |