From: <fwi...@us...> - 2008-10-01 02:01:37
|
Revision: 5353 http://jython.svn.sourceforge.net/jython/?rev=5353&view=rev Author: fwierzbicki Date: 2008-10-01 02:01:33 +0000 (Wed, 01 Oct 2008) Log Message: ----------- Added castSlice and castSlices to allow optional error nodes for sliceType. Also renamed makeExpr(s) and makeStmt(s) to castExpr(s) and castStmt(s). 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-26 17:01:58 UTC (rev 5352) +++ trunk/jython/grammar/Python.g 2008-10-01 02:01:33 UTC (rev 5353) @@ -251,7 +251,9 @@ errorHandler.reportError(this, nva); errorHandler.recover(this, nva); // throw out current char and try again } catch (FailedPredicateException fp) { - //XXX: added this for failed STRINGPART + //XXX: added this for failed STRINGPART -- the FailedPredicateException + // hides a NoViableAltException. This should be the only + // FailedPredicateException that gets thrown by the lexer. errorHandler.reportError(this, fp); errorHandler.recover(this, fp); // throw out current char and try again } catch (RecognitionException re) { @@ -274,10 +276,10 @@ mtype = new Interactive($single_input.start, new stmtType[0]); } | simple_stmt NEWLINE* EOF { - mtype = new Interactive($single_input.start, actions.makeStmts($simple_stmt.stypes)); + mtype = new Interactive($single_input.start, actions.castStmts($simple_stmt.stypes)); } | compound_stmt NEWLINE+ EOF { - mtype = new Interactive($single_input.start, actions.makeStmts($compound_stmt.tree)); + mtype = new Interactive($single_input.start, actions.castStmts($compound_stmt.tree)); } ; @@ -303,7 +305,7 @@ : (NEWLINE | stmt {stypes.addAll($stmt.stypes);} )* EOF { - mtype = new Module($file_input.start, actions.makeStmts(stypes)); + mtype = new Module($file_input.start, actions.castStmts(stypes)); } ; @@ -316,7 +318,7 @@ $eval_input.tree = mtype; } : LEADING_WS? (NEWLINE)* testlist[expr_contextType.Load] (NEWLINE)* EOF { - mtype = new Expression($eval_input.start, actions.makeExpr($testlist.tree)); + mtype = new Expression($eval_input.start, actions.castExpr($testlist.tree)); } ; @@ -428,7 +430,7 @@ } : fpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? { - $etype = actions.makeExpr($fpdef.tree); + $etype = actions.castExpr($fpdef.tree); if ($ASSIGN != null) { defaults.add($test.tree); } else if (!defaults.isEmpty()) { @@ -466,12 +468,12 @@ //fpdef: NAME | '(' fplist ')' fpdef[expr_contextType ctype] @after { - actions.checkAssign(actions.makeExpr($fpdef.tree)); + actions.checkAssign(actions.castExpr($fpdef.tree)); } : NAME -> ^(NAME<Name>[$NAME, $NAME.text, ctype]) | (LPAREN fpdef[null] COMMA) => LPAREN fplist RPAREN - -> ^(LPAREN<Tuple>[$fplist.start, actions.makeExprs($fplist.etypes), expr_contextType.Store]) + -> ^(LPAREN<Tuple>[$fplist.start, actions.castExprs($fplist.etypes), expr_contextType.Store]) | LPAREN fplist RPAREN -> fplist ; @@ -533,31 +535,31 @@ : ((testlist[null] augassign) => lhs=testlist[expr_contextType.AugStore] ( (aay=augassign y1=yield_expr { - actions.checkAssign(actions.makeExpr($lhs.tree)); - stype = new AugAssign($lhs.tree, actions.makeExpr($lhs.tree), $aay.op, actions.makeExpr($y1.tree)); + actions.checkAssign(actions.castExpr($lhs.tree)); + stype = new AugAssign($lhs.tree, actions.castExpr($lhs.tree), $aay.op, actions.castExpr($y1.tree)); } ) | (aat=augassign rhs=testlist[expr_contextType.Load] { - actions.checkAssign(actions.makeExpr($lhs.tree)); - stype = new AugAssign($lhs.tree, actions.makeExpr($lhs.tree), $aat.op, actions.makeExpr($rhs.tree)); + actions.checkAssign(actions.castExpr($lhs.tree)); + stype = new AugAssign($lhs.tree, actions.castExpr($lhs.tree), $aat.op, actions.castExpr($rhs.tree)); } ) ) | (testlist[null] ASSIGN) => lhs=testlist[expr_contextType.Store] ( | ((at=ASSIGN t+=testlist[expr_contextType.Store])+ - -> ^(ASSIGN<Assign>[$lhs.start, actions.makeAssignTargets(actions.makeExpr($lhs.tree), $t), + -> ^(ASSIGN<Assign>[$lhs.start, actions.makeAssignTargets(actions.castExpr($lhs.tree), $t), actions.makeAssignValue($t)]) ) | ((ay=ASSIGN y2+=yield_expr)+ - -> ^(ASSIGN<Assign>[$lhs.start, actions.makeAssignTargets(actions.makeExpr($lhs.tree), $y2), + -> ^(ASSIGN<Assign>[$lhs.start, actions.makeAssignTargets(actions.castExpr($lhs.tree), $y2), actions.makeAssignValue($y2)]) ) ) | lhs=testlist[expr_contextType.Load] { - stype = new Expr($lhs.start, actions.makeExpr($lhs.tree)); + stype = new Expr($lhs.start, actions.castExpr($lhs.tree)); } ) ; @@ -584,9 +586,9 @@ print_stmt : PRINT (t1=printlist - -> ^(PRINT<Print>[$PRINT, null, actions.makeExprs($t1.elts), $t1.newline]) + -> ^(PRINT<Print>[$PRINT, null, actions.castExprs($t1.elts), $t1.newline]) | RIGHTSHIFT t2=printlist2 - -> ^(PRINT<Print>[$PRINT, actions.makeExpr($t2.elts.get(0)), actions.makeExprs($t2.elts, 1), $t2.newline]) + -> ^(PRINT<Print>[$PRINT, actions.castExpr($t2.elts.get(0)), actions.castExprs($t2.elts, 1), $t2.newline]) | -> ^(PRINT<Print>[$PRINT, null, new exprType[0\], false]) ) @@ -636,7 +638,7 @@ //del_stmt: 'del' exprlist del_stmt : DELETE del_list - -> ^(DELETE<Delete>[$DELETE, actions.makeExprs($del_list.etypes)]) + -> ^(DELETE<Delete>[$DELETE, actions.castExprs($del_list.etypes)]) ; //pass_stmt: 'pass' @@ -670,7 +672,7 @@ return_stmt : RETURN (testlist[expr_contextType.Load] - -> ^(RETURN<Return>[$RETURN, actions.makeExpr($testlist.tree)]) + -> ^(RETURN<Return>[$RETURN, actions.castExpr($testlist.tree)]) | -> ^(RETURN<Return>[$RETURN, null]) ) @@ -678,14 +680,14 @@ //yield_stmt: yield_expr yield_stmt - : yield_expr -> ^(YIELD<Expr>[$yield_expr.start, actions.makeExpr($yield_expr.tree)]) + : yield_expr -> ^(YIELD<Expr>[$yield_expr.start, actions.castExpr($yield_expr.tree)]) ; //raise_stmt: 'raise' [test [',' test [',' test]]] raise_stmt : RAISE (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load] (COMMA t3=test[expr_contextType.Load])?)?)? - -> ^(RAISE<Raise>[$RAISE, actions.makeExpr($t1.tree), actions.makeExpr($t2.tree), actions.makeExpr($t3.tree)]) + -> ^(RAISE<Raise>[$RAISE, actions.castExpr($t1.tree), actions.castExpr($t2.tree), actions.castExpr($t3.tree)]) ; //import_stmt: import_name | import_from @@ -778,14 +780,14 @@ : EXEC expr[expr_contextType.Load] (IN t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? { - stype = new Exec($EXEC, actions.makeExpr($expr.tree), actions.makeExpr($t1.tree), actions.makeExpr($t2.tree)); + stype = new Exec($EXEC, actions.castExpr($expr.tree), actions.castExpr($t1.tree), actions.castExpr($t2.tree)); } ; //assert_stmt: 'assert' test [',' test] assert_stmt : ASSERT t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])? - -> ^(ASSERT<Assert>[$ASSERT, actions.makeExpr($t1.tree), actions.makeExpr($t2.tree)]) + -> ^(ASSERT<Assert>[$ASSERT, actions.castExpr($t1.tree), actions.castExpr($t2.tree)]) ; //compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef @@ -802,7 +804,7 @@ //if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] if_stmt : IF test[expr_contextType.Load] COLON ifsuite=suite elif_clause[$test.start]? - -> ^(IF<If>[$IF, actions.makeExpr($test.tree), actions.makeStmts($ifsuite.stypes), + -> ^(IF<If>[$IF, actions.castExpr($test.tree), actions.castStmts($ifsuite.stypes), actions.makeElse($elif_clause.stypes, $elif_clause.tree)]) ; @@ -813,9 +815,9 @@ } | 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.castExpr($test.tree), actions.castStmts($suite.stypes), actions.makeElse($e2.stypes, $e2.tree)]) | - -> ^(ELIF<If>[$iftest, actions.makeExpr($test.tree), actions.makeStmts($suite.stypes), new stmtType[0\]]) + -> ^(ELIF<If>[$iftest, actions.castExpr($test.tree), actions.castStmts($suite.stypes), new stmtType[0\]]) ) ; @@ -836,7 +838,7 @@ } : WHILE test[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { - stype = actions.makeWhile($WHILE, actions.makeExpr($test.tree), $s1.stypes, $s2.stypes); + stype = actions.makeWhile($WHILE, actions.castExpr($test.tree), $s1.stypes, $s2.stypes); } ; @@ -851,7 +853,7 @@ : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { - stype = actions.makeFor($FOR, $exprlist.etype, actions.makeExpr($testlist.tree), $s1.stypes, $s2.stypes); + stype = actions.makeFor($FOR, $exprlist.etype, actions.castExpr($testlist.tree), $s1.stypes, $s2.stypes); } ; @@ -889,8 +891,8 @@ } : WITH test[expr_contextType.Load] (with_var)? COLON suite { - stype = new With($WITH, actions.makeExpr($test.tree), $with_var.etype, - actions.makeStmts($suite.stypes)); + stype = new With($WITH, actions.castExpr($test.tree), $with_var.etype, + actions.castStmts($suite.stypes)); } ; @@ -898,15 +900,15 @@ with_var returns [exprType etype] : (AS | NAME) expr[expr_contextType.Store] { - $etype = actions.makeExpr($expr.tree); + $etype = actions.castExpr($expr.tree); } ; //except_clause: 'except' [test [',' test]] except_clause : EXCEPT (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Store])?)? COLON suite - -> ^(EXCEPT<excepthandlerType>[$EXCEPT, actions.makeExpr($t1.tree), actions.makeExpr($t2.tree), - actions.makeStmts($suite.stypes), $EXCEPT.getLine(), $EXCEPT.getCharPositionInLine()]) + -> ^(EXCEPT<excepthandlerType>[$EXCEPT, actions.castExpr($t1.tree), actions.castExpr($t2.tree), + actions.castStmts($suite.stypes), $EXCEPT.getLine(), $EXCEPT.getCharPositionInLine()]) ; //suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT @@ -930,7 +932,7 @@ test[expr_contextType ctype] :o1=or_test[ctype] ( (IF or_test[null] ORELSE) => IF o2=or_test[ctype] ORELSE e=test[expr_contextType.Load] - -> ^(IF<IfExp>[$o1.start, actions.makeExpr($o2.tree), actions.makeExpr($o1.tree), actions.makeExpr($e.tree)]) + -> ^(IF<IfExp>[$o1.start, actions.castExpr($o2.tree), actions.castExpr($o1.tree), actions.castExpr($e.tree)]) | -> or_test ) @@ -970,7 +972,7 @@ //not_test: 'not' not_test | comparison not_test[expr_contextType ctype] : NOT nt=not_test[ctype] - -> ^(NOT<UnaryOp>[$NOT, unaryopType.Not, actions.makeExpr($nt.tree)]) + -> ^(NOT<UnaryOp>[$NOT, unaryopType.Not, actions.castExpr($nt.tree)]) | comparison[ctype] ; @@ -981,8 +983,8 @@ } @after { if (!cmps.isEmpty()) { - $comparison.tree = new Compare($left.start, actions.makeExpr($left.tree), actions.makeCmpOps(cmps), - actions.makeExprs($right)); + $comparison.tree = new Compare($left.start, actions.castExpr($left.tree), actions.makeCmpOps(cmps), + actions.castExprs($right)); } } : left=expr[ctype] @@ -1140,7 +1142,7 @@ : PLUS p=factor {$etype = new UnaryOp($PLUS, unaryopType.UAdd, $p.etype);} | MINUS m=factor {$etype = actions.negate($MINUS, $m.etype);} | TILDE t=factor {$etype = new UnaryOp($TILDE, unaryopType.Invert, $t.etype);} - | power {$etype = actions.makeExpr($power.tree);} + | power {$etype = actions.castExpr($power.tree);} ; //power: atom trailer* ['**' factor] @@ -1151,7 +1153,7 @@ : atom (t+=trailer[$atom.start, $atom.tree])* (options {greedy=true;}:d=DOUBLESTAR factor)? { //XXX: This could be better. - $etype = actions.makeExpr($atom.tree); + $etype = actions.castExpr($atom.tree); if ($t != null) { for(Object o : $t) { if ($etype instanceof Context) { @@ -1204,14 +1206,14 @@ RBRACK | LCURLY (dictmaker - -> ^(LCURLY<Dict>[$LCURLY, actions.makeExprs($dictmaker.keys), - actions.makeExprs($dictmaker.values)]) + -> ^(LCURLY<Dict>[$LCURLY, actions.castExprs($dictmaker.keys), + actions.castExprs($dictmaker.values)]) | -> ^(LCURLY<Dict>[$LCURLY, new exprType[0\], new exprType[0\]]) ) RCURLY | lb=BACKQUOTE testlist[expr_contextType.Load] rb=BACKQUOTE - -> ^(BACKQUOTE<Repr>[$lb, actions.makeExpr($testlist.tree)]) + -> ^(BACKQUOTE<Repr>[$lb, actions.castExpr($testlist.tree)]) | NAME -> ^(NAME<Name>[$NAME, $NAME.text, $expr::ctype]) | INT @@ -1241,11 +1243,11 @@ Collections.reverse(gens); comprehensionType[] c = (comprehensionType[])gens.toArray(new comprehensionType[gens.size()]); - etype = new ListComp($listmaker.start, actions.makeExpr($t.get(0)), c); + etype = new ListComp($listmaker.start, actions.castExpr($t.get(0)), c); } | (options {greedy=true;}:COMMA t+=test[$expr::ctype])* { - etype = new org.python.antlr.ast.List($lbrack, actions.makeExprs($t), $expr::ctype); + etype = new org.python.antlr.ast.List($lbrack, actions.castExprs($t), $expr::ctype); } ) (COMMA)? ; @@ -1264,18 +1266,18 @@ : t+=test[$expr::ctype] ( ((options {k=2;}: c1=COMMA t+=test[$expr::ctype])* (c2=COMMA)? -> { $c1 != null || $c2 != null }? - ^(COMMA<Tuple>[$testlist_gexp.start, actions.makeExprs($t), $expr::ctype]) + ^(COMMA<Tuple>[$testlist_gexp.start, actions.castExprs($t), $expr::ctype]) -> test ) | (gen_for[gens] { Collections.reverse(gens); comprehensionType[] c = (comprehensionType[])gens.toArray(new comprehensionType[gens.size()]); - exprType e = actions.makeExpr($t.get(0)); + exprType e = actions.castExpr($t.get(0)); if (e instanceof Context) { ((Context)e).setContext(expr_contextType.Load); } - etype = new GeneratorExp($testlist_gexp.start, actions.makeExpr($t.get(0)), c); + etype = new GeneratorExp($testlist_gexp.start, actions.castExpr($t.get(0)), c); } ) ) @@ -1295,7 +1297,7 @@ if (a == null) { a = new argumentsType($LAMBDA, new exprType[0], null, null, new exprType[0]); } - etype = new Lambda($LAMBDA, a, actions.makeExpr($test.tree)); + etype = new Lambda($LAMBDA, a, actions.castExpr($test.tree)); } ; @@ -1303,16 +1305,16 @@ trailer [Token begin, PythonTree tree] : LPAREN (arglist - -> ^(LPAREN<Call>[$begin, actions.makeExpr($tree), actions.makeExprs($arglist.args), + -> ^(LPAREN<Call>[$begin, actions.castExpr($tree), actions.castExprs($arglist.args), actions.makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs]) | - -> ^(LPAREN<Call>[$begin, actions.makeExpr($tree), new exprType[0\], new keywordType[0\], null, null]) + -> ^(LPAREN<Call>[$begin, actions.castExpr($tree), new exprType[0\], new keywordType[0\], null, null]) ) RPAREN | LBRACK subscriptlist[$begin] RBRACK - -> ^(LBRACK<Subscript>[$begin, actions.makeExpr($tree), (sliceType)$subscriptlist.tree, $expr::ctype]) + -> ^(LBRACK<Subscript>[$begin, actions.castExpr($tree), actions.castSlice($subscriptlist.tree), $expr::ctype]) | DOT attr - -> ^(DOT<Attribute>[$begin, actions.makeExpr($tree), $attr.text, $expr::ctype]) + -> ^(DOT<Attribute>[$begin, actions.castExpr($tree), $attr.text, $expr::ctype]) ; //subscriptlist: subscript (',' subscript)* [','] @@ -1349,7 +1351,7 @@ $sltype = actions.makeSubscript(null, $c2, $upper2.tree, $sliceop.tree); } | test[expr_contextType.Load] - -> ^(LPAREN<Index>[$test.start, actions.makeExpr($test.tree)]) + -> ^(LPAREN<Index>[$test.start, actions.castExpr($test.tree)]) ; //sliceop: ':' [test] @@ -1364,11 +1366,11 @@ exprlist[expr_contextType ctype] returns [exprType etype] : (expr[null] COMMA) => e+=expr[ctype] (options {k=2;}: COMMA e+=expr[ctype])* (COMMA)? { - $etype = new Tuple($exprlist.start, actions.makeExprs($e), ctype); + $etype = new Tuple($exprlist.start, actions.castExprs($e), ctype); } | expr[ctype] { - $etype = actions.makeExpr($expr.tree); + $etype = actions.castExpr($expr.tree); } ; @@ -1385,7 +1387,7 @@ testlist[expr_contextType ctype] : (test[null] COMMA) => t+=test[ctype] (options {k=2;}: COMMA t+=test[ctype])* (COMMA)? - -> ^(COMMA<Tuple>[$testlist.start, actions.makeExprs($t), ctype]) + -> ^(COMMA<Tuple>[$testlist.start, actions.castExprs($t), ctype]) | test[ctype] ; @@ -1410,8 +1412,8 @@ } : CLASS NAME (LPAREN testlist[expr_contextType.Load]? RPAREN)? COLON suite { - stype = new ClassDef($CLASS, actions.cantBeNone($NAME), actions.makeBases(actions.makeExpr($testlist.tree)), - actions.makeStmts($suite.stypes)); + stype = new ClassDef($CLASS, actions.cantBeNone($NAME), actions.makeBases(actions.castExpr($testlist.tree)), + actions.castStmts($suite.stypes)); } ; @@ -1434,17 +1436,17 @@ } $args=arguments; $keywords=kws; - $starargs=actions.makeExpr($s.tree); - $kwargs=actions.makeExpr($k.tree); + $starargs=actions.castExpr($s.tree); + $kwargs=actions.castExpr($k.tree); } | STAR s=test[expr_contextType.Load] (COMMA DOUBLESTAR k=test[expr_contextType.Load])? { - $starargs=actions.makeExpr($s.tree); - $kwargs=actions.makeExpr($k.tree); + $starargs=actions.castExpr($s.tree); + $kwargs=actions.castExpr($k.tree); } | DOUBLESTAR k=test[expr_contextType.Load] { - $kwargs=actions.makeExpr($k.tree); + $kwargs=actions.castExpr($k.tree); } ; @@ -1453,7 +1455,7 @@ : t1=test[expr_contextType.Load] ((ASSIGN t2=test[expr_contextType.Load]) { - $kws.add(new exprType[]{actions.makeExpr($t1.tree), actions.makeExpr($t2.tree)}); + $kws.add(new exprType[]{actions.castExpr($t1.tree), actions.castExpr($t2.tree)}); } | gen_for[$gens] { @@ -1463,7 +1465,7 @@ $genarg = true; Collections.reverse($gens); comprehensionType[] c = (comprehensionType[])$gens.toArray(new comprehensionType[$gens.size()]); - arguments.add(new GeneratorExp($t1.start, actions.makeExpr($t1.tree), c)); + arguments.add(new GeneratorExp($t1.start, actions.castExpr($t1.tree), c)); } | { if (kws.size() > 0) { @@ -1492,7 +1494,7 @@ } else { e = new exprType[0]; } - gens.add(new comprehensionType($FOR, $exprlist.etype, actions.makeExpr($testlist.tree), e)); + gens.add(new comprehensionType($FOR, $exprlist.etype, actions.castExpr($testlist.tree), e)); } ; @@ -1500,7 +1502,7 @@ list_if[List gens] returns [exprType etype] : IF test[expr_contextType.Load] (list_iter[gens])? { - $etype = actions.makeExpr($test.tree); + $etype = actions.castExpr($test.tree); } ; @@ -1523,7 +1525,7 @@ } else { e = new exprType[0]; } - gens.add(new comprehensionType($FOR, $exprlist.etype, actions.makeExpr($or_test.tree), e)); + gens.add(new comprehensionType($FOR, $exprlist.etype, actions.castExpr($or_test.tree), e)); } ; @@ -1531,14 +1533,14 @@ gen_if[List gens] returns [exprType etype] : IF test[expr_contextType.Load] gen_iter[gens]? { - $etype = actions.makeExpr($test.tree); + $etype = actions.castExpr($test.tree); } ; //yield_expr: 'yield' [testlist] yield_expr : YIELD testlist[expr_contextType.Load]? - -> ^(YIELD<Yield>[$YIELD, actions.makeExpr($testlist.tree)]) + -> ^(YIELD<Yield>[$YIELD, actions.castExpr($testlist.tree)]) ; AS : 'as' ; Modified: trunk/jython/src/org/python/antlr/GrammarActions.java =================================================================== --- trunk/jython/src/org/python/antlr/GrammarActions.java 2008-09-26 17:01:58 UTC (rev 5352) +++ trunk/jython/src/org/python/antlr/GrammarActions.java 2008-10-01 02:01:33 UTC (rev 5353) @@ -151,7 +151,7 @@ errorHandler.error("Generator expression must be parenthesized if not sole argument", t); } - exprType makeExpr(Object o) { + exprType castExpr(Object o) { if (o instanceof exprType) { return (exprType)o; } @@ -162,11 +162,11 @@ } - exprType[] makeExprs(List exprs) { - return makeExprs(exprs, 0); + exprType[] castExprs(List exprs) { + return castExprs(exprs, 0); } - exprType[] makeExprs(List exprs, int start) { + exprType[] castExprs(List exprs, int start) { if (exprs != null) { List<exprType> result = new ArrayList<exprType>(); for (int i=start; i<exprs.size(); i++) { @@ -184,14 +184,14 @@ stmtType[] makeElse(List elseSuite, PythonTree elif) { if (elseSuite != null) { - return makeStmts(elseSuite); + return castStmts(elseSuite); } else if (elif == null) { return new stmtType[0]; } return new stmtType[]{(stmtType)elif}; } - stmtType makeStmt(Object o) { + stmtType castStmt(Object o) { if (o instanceof stmtType) { return (stmtType)o; } else if (o instanceof PythonParser.stmt_return) { @@ -202,15 +202,15 @@ return null; } - stmtType[] makeStmts(PythonTree t) { + stmtType[] castStmts(PythonTree t) { return new stmtType[]{(stmtType)t}; } - stmtType[] makeStmts(List stmts) { + stmtType[] castStmts(List stmts) { if (stmts != null) { List<stmtType> result = new ArrayList<stmtType>(); for (Object o:stmts) { - result.add(makeStmt(o)); + result.add(castStmt(o)); } return (stmtType[])result.toArray(new stmtType[result.size()]); } @@ -231,8 +231,8 @@ if (test == null) { return errorHandler.errorStmt(new PythonTree(t)); } - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new While(t, test, b, o); } @@ -242,27 +242,27 @@ } cantBeNone(target); - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new For(t, target, iter, b, o); } stmtType makeTryExcept(Token t, List body, List handlers, List orelse, List finBody) { - stmtType[] b = makeStmts(body); + stmtType[] b = castStmts(body); excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); - stmtType[] o = makeStmts(orelse); + stmtType[] o = castStmts(orelse); stmtType te = new TryExcept(t, b, e, o); if (finBody == null) { return te; } - stmtType[] f = makeStmts(finBody); + stmtType[] f = castStmts(finBody); stmtType[] mainBody = new stmtType[]{te}; return new TryFinally(t, mainBody, f); } TryFinally makeTryFinally(Token t, List body, List finBody) { - stmtType[] b = makeStmts(body); - stmtType[] f = makeStmts(finBody); + stmtType[] b = castStmts(body); + stmtType[] f = castStmts(finBody); return new TryFinally(t, b, f); } @@ -277,8 +277,8 @@ } else { a = new argumentsType(t, new exprType[0], null, null, new exprType[0]); } - stmtType[] s = makeStmts(funcStatements); - exprType[] d = makeExprs(decorators); + stmtType[] s = castStmts(funcStatements); + exprType[] d = castExprs(decorators); return new FunctionDef(t, nameToken.getText(), a, s, d); } @@ -287,7 +287,7 @@ checkAssign(lhs); e[0] = lhs; for(int i=0;i<rhs.size() - 1;i++) { - exprType r = makeExpr(rhs.get(i)); + exprType r = castExpr(rhs.get(i)); checkAssign(r); e[i + 1] = r; } @@ -295,7 +295,7 @@ } exprType makeAssignValue(List rhs) { - exprType value = makeExpr(rhs.get(rhs.size() -1)); + exprType value = castExpr(rhs.get(rhs.size() -1)); recurseSetContext(value, expr_contextType.Load); return value; } @@ -320,8 +320,8 @@ argumentsType makeArgumentsType(Token t, List params, Token snameToken, Token knameToken, List defaults) { - exprType[] p = makeExprs(params); - exprType[] d = makeExprs(defaults); + exprType[] p = castExprs(params); + exprType[] d = castExprs(defaults); String s; String k; if (snameToken == null) { @@ -338,7 +338,7 @@ } exprType[] extractArgs(List args) { - return makeExprs(args); + return castExprs(args); } keywordType[] makeKeywords(List args) { @@ -472,7 +472,7 @@ //FROM Walker: modType makeMod(PythonTree t, List stmts) { - stmtType[] s = makeStmts(stmts); + stmtType[] s = castStmts(stmts); return new Module(t, s); } @@ -481,7 +481,7 @@ } modType makeInteractive(PythonTree t, List stmts) { - stmtType[] s = makeStmts(stmts); + stmtType[] s = castStmts(stmts); return new Interactive(t, s); } @@ -490,16 +490,16 @@ return errorHandler.errorStmt(t); } cantBeNone(nameToken); - exprType[] b = makeExprs(bases); - stmtType[] s = makeStmts(body); + exprType[] b = castExprs(bases); + stmtType[] s = castStmts(body); return new ClassDef(t, nameToken.getText(), b, s); } argumentsType makeArgumentsType(PythonTree t, List params, PythonTree snameToken, PythonTree knameToken, List defaults) { - exprType[] p = makeExprs(params); - exprType[] d = makeExprs(defaults); + exprType[] p = castExprs(params); + exprType[] d = castExprs(defaults); String s; String k; if (snameToken == null) { @@ -516,22 +516,22 @@ } stmtType makeTryExcept(PythonTree t, List body, List handlers, List orelse, List finBody) { - stmtType[] b = makeStmts(body); + stmtType[] b = castStmts(body); excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); - stmtType[] o = makeStmts(orelse); + stmtType[] o = castStmts(orelse); stmtType te = new TryExcept(t, b, e, o); if (finBody == null) { return te; } - stmtType[] f = makeStmts(finBody); + stmtType[] f = castStmts(finBody); stmtType[] mainBody = new stmtType[]{te}; return new TryFinally(t, mainBody, f); } TryFinally makeTryFinally(PythonTree t, List body, List finBody) { - stmtType[] b = makeStmts(body); - stmtType[] f = makeStmts(finBody); + stmtType[] b = castStmts(body); + stmtType[] f = castStmts(finBody); return new TryFinally(t, b, f); } @@ -539,8 +539,8 @@ if (test == null) { return errorHandler.errorStmt(t); } - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new If(t, test, b, o); } @@ -548,8 +548,8 @@ if (test == null) { return errorHandler.errorStmt(t); } - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new While(t, test, b, o); } @@ -558,8 +558,8 @@ return errorHandler.errorStmt(t); } cantBeNone(target); - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new For(t, target, iter, b, o); } @@ -572,7 +572,7 @@ return errorHandler.errorExpr(new PythonTree(t)); } keywordType[] k = makeKeywords(keywords); - exprType[] a = makeExprs(args); + exprType[] a = castExprs(args); return new Call(t, func, a, k, starargs, kwargs); } @@ -661,18 +661,18 @@ exprType e = null; exprType o = null; if (lower != null) { - s = makeExpr(lower); + s = castExpr(lower); } if (colon != null) { isSlice = true; if (upper != null) { - e = makeExpr(upper); + e = castExpr(upper); } } if (sliceop != null) { isSlice = true; if (sliceop != null) { - o = makeExpr(sliceop); + o = castExpr(sliceop); } else { o = new Name(sliceop, "None", expr_contextType.Load); } @@ -705,28 +705,46 @@ List values = new ArrayList(); values.add(left); values.addAll(right); - return new BoolOp(left, op, makeExprs(values)); + return new BoolOp(left, op, castExprs(values)); } BinOp makeBinOp(PythonTree left, operatorType op, List rights) { - BinOp current = new BinOp(left, makeExpr(left), op, makeExpr(rights.get(0))); + BinOp current = new BinOp(left, castExpr(left), op, castExpr(rights.get(0))); for (int i = 1; i< rights.size(); i++) { - exprType right = makeExpr(rights.get(i)); + exprType right = castExpr(rights.get(i)); current = new BinOp(left, current, op, right); } return current; } BinOp makeBinOp(PythonTree left, List ops, List rights) { - BinOp current = new BinOp(left, makeExpr(left), (operatorType)ops.get(0), makeExpr(rights.get(0))); + BinOp current = new BinOp(left, castExpr(left), (operatorType)ops.get(0), castExpr(rights.get(0))); for (int i = 1; i< rights.size(); i++) { - exprType right = makeExpr(rights.get(i)); + exprType right = castExpr(rights.get(i)); operatorType op = (operatorType)ops.get(i); current = new BinOp(left, current, op, right); } return current; } + sliceType[] castSlices(List slices) { + if (slices != null) { + List<sliceType> result = new ArrayList<sliceType>(); + for (Object o:slices) { + result.add(castSlice(o)); + } + return (sliceType[])result.toArray(new sliceType[result.size()]); + } + return new sliceType[0]; + } + + sliceType castSlice(Object o) { + if (o instanceof sliceType) { + return (sliceType)o; + } + return errorHandler.errorSlice((PythonTree)o); + } + sliceType makeSliceType(Token begin, Token c1, Token c2, List sltypes) { boolean isTuple = false; if (c1 != null || c2 != null) { @@ -753,12 +771,12 @@ s = new Index(begin, t); } } else if (sltypes.size() == 1) { - s = (sliceType)sltypes.get(0); + s = castSlice(sltypes.get(0)); } else if (sltypes.size() != 0) { extslice = true; } if (extslice) { - sliceType[] st = (sliceType[])sltypes.toArray(new sliceType[sltypes.size()]); + sliceType[] st = castSlices(sltypes);//.toArray(new sliceType[sltypes.size()]); s = new ExtSlice(begin, st); } return s; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |