From: <fwi...@us...> - 2008-09-11 19:48:37
|
Revision: 5320 http://jython.svn.sourceforge.net/jython/?rev=5320&view=rev Author: fwierzbicki Date: 2008-09-11 19:48:35 +0000 (Thu, 11 Sep 2008) Log Message: ----------- Transform if/elif/else into the correct If nodes in the grammar file instead of as a code post-processing step. Modified Paths: -------------- trunk/jython/grammar/Python.g trunk/jython/src/org/python/antlr/GrammarActions.java Modified: trunk/jython/grammar/Python.g =================================================================== --- trunk/jython/grammar/Python.g 2008-09-11 01:33:39 UTC (rev 5319) +++ trunk/jython/grammar/Python.g 2008-09-11 19:48:35 UTC (rev 5320) @@ -797,18 +797,31 @@ //if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] if_stmt - : IF test[expr_contextType.Load] COLON ifsuite=suite elifs+=elif_clause* - (ORELSE COLON elsesuite=suite)? + : IF test[expr_contextType.Load] COLON ifsuite=suite elif_clause[$test.start]? -> ^(IF<If>[$IF, actions.makeExpr($test.tree), actions.makeStmts($ifsuite.stypes), - actions.makeElses($elsesuite.stypes, $elifs)]) + actions.makeElse($elif_clause.stypes, $elif_clause.tree)]) ; //not in CPython's Grammar file -elif_clause - : ELIF test[expr_contextType.Load] COLON suite - -> ^(ELIF<If>[$test.start, actions.makeExpr($test.tree), actions.makeStmts($suite.stypes), new stmtType[0\]]) +elif_clause [Token iftest] returns [List stypes] + : else_clause { + $stypes = $else_clause.stypes; + } + | ELIF test[expr_contextType.Load] COLON suite + (e2=elif_clause[$iftest] + -> ^(ELIF<If>[$iftest, actions.makeExpr($test.tree), actions.makeStmts($suite.stypes), actions.makeElse($e2.stypes, $e2.tree)]) + | + -> ^(ELIF<If>[$iftest, actions.makeExpr($test.tree), actions.makeStmts($suite.stypes), new stmtType[0\]]) + ) ; +//not in CPython's Grammar file +else_clause returns [List stypes] + : ORELSE COLON elsesuite=suite { + $stypes = $suite.stypes; + } + ; + //while_stmt: 'while' test ':' suite ['else' ':' suite] while_stmt @init { Modified: trunk/jython/src/org/python/antlr/GrammarActions.java =================================================================== --- trunk/jython/src/org/python/antlr/GrammarActions.java 2008-09-11 01:33:39 UTC (rev 5319) +++ trunk/jython/src/org/python/antlr/GrammarActions.java 2008-09-11 19:48:35 UTC (rev 5320) @@ -182,18 +182,13 @@ return new exprType[0]; } - stmtType[] makeElses(List elseSuite, List elifs) { - stmtType[] o; - o = makeStmts(elseSuite); - if (elifs != null) { - ListIterator iter = elifs.listIterator(elifs.size()); - while (iter.hasPrevious()) { - If elif = (If)iter.previous(); - elif.orelse = o; - o = new stmtType[]{elif}; - } + stmtType[] makeElse(List elseSuite, PythonTree elif) { + if (elseSuite != null) { + return makeStmts(elseSuite); + } else if (elif == null) { + return new stmtType[0]; } - return o; + return new stmtType[]{(stmtType)elif}; } stmtType[] makeStmts(PythonTree t) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |