From: <fwi...@us...> - 2008-06-04 17:50:57
|
Revision: 4529 http://jython.svn.sourceforge.net/jython/?rev=4529&view=rev Author: fwierzbicki Date: 2008-06-04 10:50:54 -0700 (Wed, 04 Jun 2008) Log Message: ----------- function support Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-04 17:46:25 UTC (rev 4528) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-04 17:50:54 UTC (rev 4529) @@ -78,7 +78,6 @@ INDENT; DEDENT; - ModuleTok; Interactive; Expression; Test; @@ -90,8 +89,8 @@ Body; ClassDef; Bases; - FunctionDef; - Arguments; + FunctionDefTok; + ArgumentsTok; Args; Arg; Keyword; @@ -191,11 +190,13 @@ import org.python.antlr.ParseException; import org.python.antlr.PythonTree; +import org.python.antlr.ast.argumentsType; import org.python.antlr.ast.Attribute; import org.python.antlr.ast.Context; import org.python.antlr.ast.Expr; import org.python.antlr.ast.exprType; import org.python.antlr.ast.expr_contextType; +import org.python.antlr.ast.FunctionDef; import org.python.antlr.ast.modType; import org.python.antlr.ast.Module; import org.python.antlr.ast.Name; @@ -241,14 +242,42 @@ if (stmts != null) { List<stmtType> result = new ArrayList<stmtType>(); for (int i=0; i<stmts.size(); i++) { - stmt_return s = (stmt_return)stmts.get(i); - result.add((stmtType)s.tree); + result.add((stmtType)stmts.get(i)); } return (stmtType[])result.toArray(new stmtType[result.size()]); } return new stmtType[0]; } + private exprType makeDottedAttr(Token nameToken, List attrs) { + exprType current = new Name(nameToken, nameToken.getText(), expr_contextType.Load); + for (int i=attrs.size() - 1; i > -1; i--) { + Token t = ((PythonTree)attrs.get(i)).token; + current = new Attribute(t, current, t.getText(), + expr_contextType.Load); + } + return current; + } + + 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()]); + 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); + } + + Object makeFloat(Token t) { debug("makeFloat matched " + t.getText()); return Py.newFloat(Double.valueOf(t.getText())); @@ -416,7 +445,7 @@ @lexer::header { package org.python.antlr; -} +} @lexer::members { /** Handles context-sensitive lexing of implicit line joining such as @@ -466,8 +495,10 @@ //file_input: (NEWLINE | stmt)* ENDMARKER file_input returns [modType mod] - : (NEWLINE | s+=stmt)* {debug("parsed file_input");} - -> ^(ModuleTok<Module>[$file_input.start, makeStmts($s)]) + : (NEWLINE | s+=stmt)+ { + $mod = new Module($file_input.start, makeStmts($s)); + } + | { $mod = new Module($file_input.start, new stmtType[0]); } ; //eval_input: testlist NEWLINE* ENDMARKER @@ -475,32 +506,44 @@ ; //decorators: decorator+ -decorators: decorator+ - ; +decorators returns [List etypes] + : d+=decorator+ {$etypes = $d;} + ; //decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE -decorator: AT dotted_attr - ( (LPAREN arglist? RPAREN) -> ^(Decorator dotted_attr ^(Call ^(Args arglist)?)) - | -> ^(Decorator dotted_attr) - ) NEWLINE - ; +decorator returns [exprType etype] +@after { + $decorator.tree = $etype; +} + : AT dotted_attr + //XXX: ignoring the arglist and Call generation right now. + ( (LPAREN arglist? RPAREN) { $etype = $dotted_attr.etype; } + // ^(Decorator dotted_attr ^(Call ^(Args arglist)?)) + | { $etype = $dotted_attr.etype; } + ) NEWLINE + ; -dotted_attr - : NAME (DOT^ NAME)* +dotted_attr returns [exprType etype] + : n1=NAME + ( (DOT n2+=dotted_attr)+ { $etype = makeDottedAttr($n1, $n2); } + | { $etype = new Name($NAME, $NAME.text, expr_contextType.Load); } + ) ; //funcdef: [decorators] 'def' NAME parameters ':' suite -funcdef : decorators? 'def' NAME parameters COLON suite - -> ^(FunctionDef 'def' ^(NameTok NAME) parameters ^(Body suite) ^(Decorators decorators?)) +funcdef : decorators? tok='def' NAME parameters COLON suite + -> ^(FunctionDefTok<FunctionDef>[$tok, $NAME.text, $parameters.args, makeStmts($suite.stmts), + makeExprs($decorators.etypes)]) ; //parameters: '(' [varargslist] ')' -parameters : LPAREN - (varargslist -> ^(Arguments varargslist) - | -> ^(Arguments) - ) - RPAREN - ; +parameters returns [argumentsType args] + : LPAREN + (varargslist -> ^(ArgumentsTok varargslist) + | {$args = new argumentsType($parameters.start, new exprType[0], null, null, new exprType[0]);} + ) + RPAREN + ; //varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [','] varargslist : defparameter (options {greedy=true;}:COMMA defparameter)* @@ -823,9 +866,10 @@ ; //suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT -suite : simple_stmt - | NEWLINE! INDENT (stmt)+ DEDENT - ; +suite returns [List stmts] + : s+=simple_stmt {$stmts = $s;} + | NEWLINE! INDENT (s+=stmt)+ DEDENT {$stmts = $s;} + ; //test: or_test ['if' or_test 'else' test] | lambdef test[expr_contextType ect] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-04 18:41:29
|
Revision: 4532 http://jython.svn.sourceforge.net/jython/?rev=4532&view=rev Author: fwierzbicki Date: 2008-06-04 11:41:24 -0700 (Wed, 04 Jun 2008) Log Message: ----------- Handle most of arglist Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-04 18:06:23 UTC (rev 4531) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-04 18:41:24 UTC (rev 4532) @@ -231,7 +231,6 @@ for (int i=start; i<exprs.size(); i++) { exprType e = (exprType)exprs.get(i); result.add(e); - //result.add((exprType)s.tree); } return (exprType[])result.toArray(new exprType[result.size()]); } @@ -277,7 +276,29 @@ return new FunctionDef(t, nameToken.getText(), a, s, d); } + private argumentsType makeArgumentsType(Token t, List params, Token snameToken, + Token knameToken, List defaults) { + debug("Matched Arguments"); + exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); + exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); + String s; + String k; + if (snameToken == null) { + s = null; + } else { + s = snameToken.getText(); + } + if (knameToken == null) { + k = null; + } else { + k = knameToken.getText(); + } + return new argumentsType(t, p, s, k, d); + } + + + Object makeFloat(Token t) { debug("makeFloat matched " + t.getText()); return Py.newFloat(Double.valueOf(t.getText())); @@ -539,29 +560,46 @@ //parameters: '(' [varargslist] ')' parameters returns [argumentsType args] : LPAREN - (varargslist -> ^(ArgumentsTok varargslist) + (varargslist {$args = $varargslist.args;} | {$args = new argumentsType($parameters.start, new exprType[0], null, null, new exprType[0]);} ) RPAREN ; //varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [','] -varargslist : defparameter (options {greedy=true;}:COMMA defparameter)* - (COMMA - ( STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)? - | DOUBLESTAR kwargs=NAME - )? - )? {debug("parsed varargslist");} - -> ^(Args defparameter+) ^(StarArgs $starargs)? ^(KWArgs $kwargs)? - | STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)?{debug("parsed varargslist STARARGS");} - -> ^(StarArgs $starargs) ^(KWArgs $kwargs)? - | DOUBLESTAR kwargs=NAME {debug("parsed varargslist KWS");} - -> ^(KWArgs $kwargs) - ; +varargslist returns [argumentsType args] +@init { + List defaults = new ArrayList(); +} + : d+=defparameter[defaults] (options {greedy=true;}:COMMA d+=defparameter[defaults])* + (COMMA + ( STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)? + | DOUBLESTAR kwargs=NAME + )? + )? + {$args = makeArgumentsType($varargslist.start, $d, $starargs, $kwargs, defaults);} + | STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)?{debug("parsed varargslist STARARGS");} + {$args = makeArgumentsType($varargslist.start, $d, $starargs, $kwargs, defaults);} + | DOUBLESTAR kwargs=NAME {debug("parsed varargslist KWS");} + {$args = makeArgumentsType($varargslist.start, $d, null, $kwargs, defaults);} + ; //not in CPython's Grammar file -defparameter : fpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? {debug("parsed defparameter");} - ; +defparameter[List defaults] returns [exprType etype] +@after { + $defparameter.tree = $etype; +} + : fpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? { + $etype = (exprType)$fpdef.tree; + if ($ASSIGN != null) { + defaults.add($test); + } else if (!defaults.isEmpty()) { + throw new ParseException( + "non-default argument follows default argument", + $fpdef.tree); + } + } +; //fpdef: NAME | '(' fplist ')' fpdef[expr_contextType ctype] : NAME This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-04 18:54:59
|
Revision: 4533 http://jython.svn.sourceforge.net/jython/?rev=4533&view=rev Author: fwierzbicki Date: 2008-06-04 11:54:52 -0700 (Wed, 04 Jun 2008) Log Message: ----------- pass,break,continue,return Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-04 18:41:24 UTC (rev 4532) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-04 18:54:52 UTC (rev 4533) @@ -108,15 +108,15 @@ OrElse; Elif; While; - Pass; - Break; - Continue; + PassTok; + BreakTok; + ContinueTok; PrintTok; TryExcept; TryFinally; ExceptHandler; For; - Return; + ReturnTok; Yield; StrTok; NumTok; @@ -192,7 +192,9 @@ import org.python.antlr.PythonTree; import org.python.antlr.ast.argumentsType; import org.python.antlr.ast.Attribute; +import org.python.antlr.ast.Break; import org.python.antlr.ast.Context; +import org.python.antlr.ast.Continue; import org.python.antlr.ast.Expr; import org.python.antlr.ast.exprType; import org.python.antlr.ast.expr_contextType; @@ -201,7 +203,9 @@ import org.python.antlr.ast.Module; import org.python.antlr.ast.Name; import org.python.antlr.ast.Num; +import org.python.antlr.ast.Pass; import org.python.antlr.ast.Print; +import org.python.antlr.ast.Return; import org.python.antlr.ast.stmtType; import org.python.antlr.ast.Str; import org.python.core.Py; @@ -746,8 +750,8 @@ ; //pass_stmt: 'pass' -pass_stmt : 'pass' - -> ^(Pass 'pass') +pass_stmt : tok='pass' + -> ^(PassTok<Pass>[$tok]) ; //flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt @@ -759,18 +763,18 @@ ; //break_stmt: 'break' -break_stmt : 'break' - -> ^(Break 'break') +break_stmt : tok='break' + -> ^(BreakTok<Break>[$tok]) ; //continue_stmt: 'continue' -continue_stmt : 'continue' - -> ^(Continue 'continue') +continue_stmt : tok='continue' + -> ^(ContinueTok<Continue>[$tok]) ; //return_stmt: 'return' [testlist] -return_stmt : 'return' (testlist[expr_contextType.Load])? - -> ^(Return 'return' ^(Value testlist)?) +return_stmt : tok='return' (testlist[expr_contextType.Load])? + -> ^(ReturnTok<Return>[$tok, (exprType)$testlist.tree]) ; //yield_stmt: yield_expr This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-22 10:06:07
|
Revision: 4702 http://jython.svn.sourceforge.net/jython/?rev=4702&view=rev Author: fwierzbicki Date: 2008-06-20 09:16:57 -0700 (Fri, 20 Jun 2008) Log Message: ----------- BinOp support for +/- Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-20 16:08:29 UTC (rev 4701) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-20 16:16:57 UTC (rev 4702) @@ -160,6 +160,7 @@ import org.python.antlr.ast.argumentsType; import org.python.antlr.ast.Assign; import org.python.antlr.ast.Attribute; +import org.python.antlr.ast.BinOp; import org.python.antlr.ast.Break; import org.python.antlr.ast.Context; import org.python.antlr.ast.Continue; @@ -173,6 +174,7 @@ import org.python.antlr.ast.Num; import org.python.antlr.ast.Pass; import org.python.antlr.ast.Print; +import org.python.antlr.ast.operatorType; import org.python.antlr.ast.Return; import org.python.antlr.ast.stmtType; import org.python.antlr.ast.Str; @@ -641,8 +643,8 @@ ) ( (augassign yield_expr -> ^(augassign $lhs yield_expr)) | (augassign rhs=testlist[expr_contextType.Load] -> ^(augassign $lhs $rhs)) - | ((ASSIGN t+=testlist[expr_contextType.Store])+ -> ^(ASSIGN<Assign>[$ASSIGN, makeAssignTargets((exprType)$lhs.tree, $t), makeAssignValue($t)])) - //| ((ASSIGN yield_expr)+) + | ((at=ASSIGN t+=testlist[expr_contextType.Store])+ -> ^(AssignTok<Assign>[$at, makeAssignTargets((exprType)$lhs.tree, $t), makeAssignValue($t)])) + | ((ay=ASSIGN y+=yield_expr)+ -> ^(AssignTok<Assign>[$ay, makeAssignTargets((exprType)$lhs.tree, $y), makeAssignValue($y)])) | -> ExprTok<Expr>[$lhs.start, (exprType)$lhs.tree] ) ; @@ -929,7 +931,21 @@ @init { $expr::ctype = ect; } - +@after { + if ($expr.tree instanceof BinOp) { + BinOp b = (BinOp)$expr.tree; + b.left = (exprType)b.getChild(0); + b.right = (exprType)b.getChild(1); + switch (b.getType()) { + case PLUS: + b.op = operatorType.Add; + break; + case MINUS: + b.op = operatorType.Sub; + break; + } + } +} : xor_expr (VBAR^ xor_expr)* ; @@ -946,7 +962,9 @@ ; //arith_expr: term (('+'|'-') term)* -arith_expr: term ((PLUS^|MINUS^) term)* + +arith_expr + :term ((PLUS<BinOp>^|MINUS<BinOp>^) term)* ; //term: factor (('*'|'/'|'%'|'//') factor)* This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-22 10:19:36
|
Revision: 4701 http://jython.svn.sourceforge.net/jython/?rev=4701&view=rev Author: fwierzbicki Date: 2008-06-20 09:08:29 -0700 (Fri, 20 Jun 2008) Log Message: ----------- merged with asm, attempting to add BinOp support. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-20 08:29:23 UTC (rev 4700) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-20 16:08:29 UTC (rev 4701) @@ -78,16 +78,15 @@ INDENT; DEDENT; + Module; Interactive; Expression; + ExprTok; + NameTok; Test; Msg; - Import; - ImportFrom; Level; - NameTok; Body; - ClassDef; Bases; FunctionDefTok; ArgumentsTok; @@ -96,80 +95,49 @@ Keyword; StarArgs; KWArgs; - Assign; + AssignTok; AugAssign; - Compare; - ExprTok; Tuple; List; Dict; - If; IfExp; - OrElse; - Elif; - While; - PassTok; - BreakTok; - ContinueTok; - PrintTok; TryExcept; TryFinally; ExceptHandler; - For; - ReturnTok; - Yield; StrTok; NumTok; IsNot; - In; NotIn; - Raise; Type; Inst; Tback; - Global; - Exec; Globals; Locals; - Assert; Ellipsis; - Comprehension; ListComp; - Lambda; Repr; - BinOp; Subscript; SubscriptList; Index; Target; - Targets; Value; Lower; Upper; Step; - UnaryOp; UAdd; USub; Invert; - Delete; - Default; Alias; Asname; - Decorator; Decorators; - With; GeneratorExp; - Id; - Iter; Ifs; Elts; - Ctx; - Attr; Call; Dest; Values; Newline; - //The tokens below are not represented in the 2.5 Python.asdl + FpList; StepOp; UpperOp; @@ -178,7 +146,6 @@ GenIf; ListFor; ListIf; - FinalBody; Parens; Brackets; } @@ -191,6 +158,7 @@ import org.python.antlr.ParseException; import org.python.antlr.PythonTree; import org.python.antlr.ast.argumentsType; +import org.python.antlr.ast.Assign; import org.python.antlr.ast.Attribute; import org.python.antlr.ast.Break; import org.python.antlr.ast.Context; @@ -280,11 +248,36 @@ return new FunctionDef(t, nameToken.getText(), a, s, d); } + private exprType[] makeAssignTargets(exprType lhs, List rhs) { + exprType[] e = new exprType[rhs.size()]; + e[0] = lhs; + for(int i=0;i<rhs.size() - 1;i++) { + testlist_return r = (testlist_return)rhs.get(i); + e[i] = (exprType)r.getTree(); + } + return e; + } + + private exprType makeAssignValue(List rhs) { + testlist_return r = (testlist_return)rhs.get(rhs.size() -1); + exprType value = (exprType)r.getTree(); + if (value instanceof Context) { + //XXX: for Tuples, etc -- will need to recursively set to expr_contextType.Load. + ((Context)value).setContext(expr_contextType.Load); + } + return value; + } + private argumentsType makeArgumentsType(Token t, List params, Token snameToken, Token knameToken, List defaults) { debug("Matched Arguments"); - exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); + exprType[] p; + if (params == null) { + p = new exprType[0]; + } else { + p = (exprType[])params.toArray(new exprType[params.size()]); + } exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); String s; String k; @@ -301,8 +294,6 @@ return new argumentsType(t, p, s, k, d); } - - Object makeFloat(Token t) { debug("makeFloat matched " + t.getText()); return Py.newFloat(Double.valueOf(t.getText())); @@ -463,8 +454,8 @@ } @rulecatch { -catch (RecognitionException e) { - throw e; +catch (RecognitionException r) { + throw new ParseException(r); } } @@ -482,28 +473,28 @@ int startPos=-1; public Token nextToken() { - while (true) { - state.token = null; - state.channel = Token.DEFAULT_CHANNEL; - state.tokenStartCharIndex = input.index(); - state.tokenStartCharPositionInLine = input.getCharPositionInLine(); - state.tokenStartLine = input.getLine(); - state.text = null; - if ( input.LA(1)==CharStream.EOF ) { - return Token.EOF_TOKEN; - } - try { - mTokens(); - if ( state.token==null ) { - emit(); - } - else if ( state.token==Token.SKIP_TOKEN ) { - continue; - } - return state.token; - } + while (true) { + state.token = null; + state.channel = Token.DEFAULT_CHANNEL; + state.tokenStartCharIndex = input.index(); + state.tokenStartCharPositionInLine = input.getCharPositionInLine(); + state.tokenStartLine = input.getLine(); + state.text = null; + if ( input.LA(1)==CharStream.EOF ) { + return Token.EOF_TOKEN; + } + try { + mTokens(); + if ( state.token==null ) { + emit(); + } + else if ( state.token==Token.SKIP_TOKEN ) { + continue; + } + return state.token; + } catch (RecognitionException re) { - throw new ParseException(getErrorMessage(re, this.getTokenNames())); + throw new ParseException(re); } } } @@ -530,9 +521,12 @@ eval_input : (NEWLINE)* testlist[expr_contextType.Load] (NEWLINE)* -> ^(Expression testlist) ; -//decorators: decorator+ -decorators returns [List etypes] - : d+=decorator+ {$etypes = $d;} +//not in CPython's Grammar file +dotted_attr returns [exprType etype] + : n1=NAME + ( (DOT n2+=dotted_attr)+ { $etype = makeDottedAttr($n1, $n2); } + | { $etype = new Name($NAME, $NAME.text, expr_contextType.Load); } + ) ; //decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE @@ -548,16 +542,14 @@ ) NEWLINE ; -dotted_attr returns [exprType etype] - : n1=NAME - ( (DOT n2+=dotted_attr)+ { $etype = makeDottedAttr($n1, $n2); } - | { $etype = new Name($NAME, $NAME.text, expr_contextType.Load); } - ) +//decorators: decorator+ +decorators returns [List etypes] + : d+=decorator+ {$etypes = $d;} ; //funcdef: [decorators] 'def' NAME parameters ':' suite -funcdef : decorators? tok='def' NAME parameters COLON suite - -> ^(FunctionDefTok<FunctionDef>[$tok, $NAME.text, $parameters.args, makeStmts($suite.stmts), +funcdef : decorators? DEF NAME parameters COLON suite + -> ^(DEF<FunctionDef>[$DEF, $NAME.text, $parameters.args, makeStmts($suite.stmts), makeExprs($decorators.etypes)]) ; @@ -570,7 +562,26 @@ RPAREN ; -//varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [','] +//not in CPython's Grammar file +defparameter[List defaults] returns [exprType etype] +@after { + $defparameter.tree = $etype; +} + : fpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? { + $etype = (exprType)$fpdef.tree; + if ($ASSIGN != null) { + //defaults.add(test); + } else if (!defaults.isEmpty()) { + throw new ParseException( + "non-default argument follows default argument", + $fpdef.tree); + } + } +; + +//varargslist: ((fpdef ['=' test] ',')* +// ('*' NAME [',' '**' NAME] | '**' NAME) | +// fpdef ['=' test] (',' fpdef ['=' test])* [',']) varargslist returns [argumentsType args] @init { List defaults = new ArrayList(); @@ -588,23 +599,6 @@ {$args = makeArgumentsType($varargslist.start, $d, null, $kwargs, defaults);} ; -//not in CPython's Grammar file -defparameter[List defaults] returns [exprType etype] -@after { - $defparameter.tree = $etype; -} - : fpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? { - $etype = (exprType)$fpdef.tree; - if ($ASSIGN != null) { - defaults.add($test); - } else if (!defaults.isEmpty()) { - throw new ParseException( - "non-default argument follows default argument", - $fpdef.tree); - } - } -; - //fpdef: NAME | '(' fplist ')' fpdef[expr_contextType ctype] : NAME -> ^(NameTok<Name>[$NAME, $NAME.text, ctype]) @@ -627,8 +621,8 @@ simple_stmt : small_stmt (options {greedy=true;}:SEMI small_stmt)* (SEMI)? NEWLINE -> small_stmt+ ; - -//small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | exec_stmt | assert_stmt +//small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | +// import_stmt | global_stmt | exec_stmt | assert_stmt) small_stmt : expr_stmt | print_stmt | del_stmt @@ -640,49 +634,21 @@ | assert_stmt ; -//expr_stmt: testlist (augassign testlist | ('=' testlist)*) -expr_stmt : lhs=testlist[expr_contextType.Store] +//expr_stmt: testlist (augassign (yield_expr|testlist) | +// ('=' (yield_expr|testlist))*) +expr_stmt : ((testlist[expr_contextType.Load] (ASSIGN|augassign)) => lhs=testlist[expr_contextType.Store] + | lhs=testlist[expr_contextType.Load] + ) ( (augassign yield_expr -> ^(augassign $lhs yield_expr)) | (augassign rhs=testlist[expr_contextType.Load] -> ^(augassign $lhs $rhs)) - | ((assigns) {debug("matched assigns");} -> ^(Assign ^(Target $lhs) assigns)) - | {System.out.println("expr:" + $lhs.tree.getClass());} -> ^(ExprTok<Expr>[$lhs.start, (exprType)$lhs.tree]) + | ((ASSIGN t+=testlist[expr_contextType.Store])+ -> ^(ASSIGN<Assign>[$ASSIGN, makeAssignTargets((exprType)$lhs.tree, $t), makeAssignValue($t)])) + //| ((ASSIGN yield_expr)+) + | -> ExprTok<Expr>[$lhs.start, (exprType)$lhs.tree] ) ; -//not in CPython's Grammar file -assigns - @after { - PythonTree pt = ((PythonTree)$assigns.tree); - int children = pt.getChildCount(); - PythonTree child; - if (children == 1) { - child = pt; - pt.token = new CommonToken(Value, "Value"); - } else { - child = (PythonTree)pt.getChild(children - 1); - child.token = new CommonToken(Value, "Value"); - } - System.out.println("Changing" + child); - child.token = new CommonToken(Value, "Value"); - if (child instanceof Context) { - ((Context)child).setContext(expr_contextType.Load); - } - } - : assign_testlist+ - | assign_yield+ - ; - -//not in CPython's Grammar file -assign_testlist - : ASSIGN testlist[expr_contextType.Store] -> ^(Target testlist) - ; - -//not in CPython's Grammar file -assign_yield - : ASSIGN yield_expr -> ^(Value yield_expr) - ; - -//augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//=' +//augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | +// '<<=' | '>>=' | '**=' | '//=') augassign : PLUSEQUAL | MINUSEQUAL | STAREQUAL @@ -697,14 +663,15 @@ | DOUBLESLASHEQUAL ; -//print_stmt: 'print' ( [ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ] ) -print_stmt : tok='print' +//print_stmt: 'print' ( [ test (',' test)* [','] ] | +// '>>' test [ (',' test)+ [','] ] ) +print_stmt : PRINT ( t1=printlist - -> ^(PrintTok<Print>[$tok, null, makeExprs($t1.elts), $t1.newline]) + -> ^(PRINT<Print>[$PRINT, null, makeExprs($t1.elts), $t1.newline]) | RIGHTSHIFT t2=printlist2 - -> ^(PrintTok<Print>[$tok, (exprType)$t2.elts.get(0), makeExprs($t2.elts, 1), $t2.newline]) + -> ^(PRINT<Print>[$PRINT, (exprType)$t2.elts.get(0), makeExprs($t2.elts, 1), $t2.newline]) | - -> ^(PrintTok<Print>[$tok, null, new exprType[0\], false]) + -> ^(PRINT<Print>[$PRINT, null, new exprType[0\], false]) ) ; @@ -745,13 +712,13 @@ //del_stmt: 'del' exprlist -del_stmt : 'del' exprlist2 - -> ^(Delete 'del' exprlist2) +del_stmt : DELETE exprlist2 + -> ^(DELETE exprlist2) ; //pass_stmt: 'pass' -pass_stmt : tok='pass' - -> ^(PassTok<Pass>[$tok]) +pass_stmt : PASS + -> ^(PASS<Pass>[$PASS]) ; //flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt @@ -763,18 +730,18 @@ ; //break_stmt: 'break' -break_stmt : tok='break' - -> ^(BreakTok<Break>[$tok]) +break_stmt : BREAK + -> ^(BREAK<Break>[$BREAK]) ; //continue_stmt: 'continue' -continue_stmt : tok='continue' - -> ^(ContinueTok<Continue>[$tok]) +continue_stmt : CONTINUE + -> ^(CONTINUE<Continue>[$CONTINUE]) ; //return_stmt: 'return' [testlist] -return_stmt : tok='return' (testlist[expr_contextType.Load])? - -> ^(ReturnTok<Return>[$tok, (exprType)$testlist.tree]) +return_stmt : RETURN (testlist[expr_contextType.Load])? + -> ^(RETURN ^(Value testlist)?) ; //yield_stmt: yield_expr @@ -782,8 +749,8 @@ ; //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' ^(Type $t1)? ^(Inst $t2)? ^(Tback $t3)?) +raise_stmt: RAISE (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load] (COMMA t3=test[expr_contextType.Load])?)?)? + -> ^(RAISE ^(Type $t1)? ^(Inst $t2)? ^(Tback $t3)?) ; //import_stmt: import_name | import_from @@ -792,19 +759,19 @@ ; //import_name: 'import' dotted_as_names -import_name : 'import' dotted_as_names - -> ^(Import 'import' dotted_as_names) +import_name : IMPORT dotted_as_names + -> ^(IMPORT dotted_as_names) ; //import_from: ('from' ('.'* dotted_name | '.'+) // 'import' ('*' | '(' import_as_names ')' | import_as_names)) -import_from: 'from' (DOT* dotted_name | DOT+) 'import' +import_from: FROM (DOT* dotted_name | DOT+) IMPORT (STAR - -> ^(ImportFrom 'from' ^(Level DOT*)? ^(NameTok dotted_name)? ^(Import STAR)) + -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT STAR)) | import_as_names - -> ^(ImportFrom 'from' ^(Level DOT*)? ^(NameTok dotted_name)? ^(Import import_as_names)) + -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT import_as_names)) | LPAREN import_as_names RPAREN - -> ^(ImportFrom 'from' ^(Level DOT*)? ^(NameTok dotted_name)? ^(Import import_as_names)) + -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT import_as_names)) ) ; @@ -834,18 +801,18 @@ ; //global_stmt: 'global' NAME (',' NAME)* -global_stmt : 'global' NAME (COMMA NAME)* - -> ^(Global 'global' NAME+) +global_stmt : GLOBAL NAME (COMMA NAME)* + -> ^(GLOBAL NAME+) ; //exec_stmt: 'exec' expr ['in' test [',' test]] -exec_stmt : keyEXEC expr ('in' t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? - -> ^(Exec keyEXEC expr ^(Globals $t1)? ^(Locals $t2)?) +exec_stmt : keyEXEC expr[expr_contextType.Load] ('in' t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? + -> ^(keyEXEC expr ^(Globals $t1)? ^(Locals $t2)?) ; //assert_stmt: 'assert' test [',' test] -assert_stmt : 'assert' t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])? - -> ^(Assert 'assert' ^(Test $t1) ^(Msg $t2)?) +assert_stmt : ASSERT t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])? + -> ^(ASSERT ^(Test $t1) ^(Msg $t2)?) ; //compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef @@ -859,52 +826,52 @@ ; //if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] -if_stmt: 'if' test[expr_contextType.Load] COLON ifsuite=suite elif_clause* ('else' COLON elsesuite=suite)? - -> ^(If 'if' test $ifsuite elif_clause* ^(OrElse $elsesuite)?) +if_stmt: IF test[expr_contextType.Load] COLON ifsuite=suite elif_clause* (ORELSE COLON elsesuite=suite)? + -> ^(IF test $ifsuite elif_clause* ^(ORELSE $elsesuite)?) ; //not in CPython's Grammar file -elif_clause : 'elif' test[expr_contextType.Load] COLON suite - -> ^(Elif test suite) +elif_clause : ELIF test[expr_contextType.Load] COLON suite + -> ^(ELIF test suite) ; //while_stmt: 'while' test ':' suite ['else' ':' suite] -while_stmt : 'while' test[expr_contextType.Load] COLON s1=suite ('else' COLON s2=suite)? - -> ^(While 'while' test ^(Body $s1) ^(OrElse $s2)?) +while_stmt : WHILE test[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? + -> ^(WHILE test ^(Body $s1) ^(ORELSE $s2)?) ; //for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] -for_stmt : 'for' exprlist[expr_contextType.Store] 'in' testlist[expr_contextType.Load] COLON s1=suite ('else' COLON s2=suite)? - -> ^(For 'for' ^(Target exprlist) ^(Iter testlist) ^(Body $s1) ^(OrElse $s2)?) +for_stmt : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? + -> ^(FOR ^(Target exprlist) ^(IN testlist) ^(Body $s1) ^(ORELSE $s2)?) ; //try_stmt: ('try' ':' suite // ((except_clause ':' suite)+ // ['else' ':' suite] // ['finally' ':' suite] | -// 'finally' ':' suite)) +// 'finally' ':' suite)) try_stmt : 'try' COLON trysuite=suite - ( (except_clause+ ('else' COLON elsesuite=suite)? ('finally' COLON finalsuite=suite)? - -> ^(TryExcept 'try' ^(Body $trysuite) except_clause+ ^(OrElse $elsesuite)? ^(FinalBody 'finally' $finalsuite)?)) - | ('finally' COLON finalsuite=suite - -> ^(TryFinally 'try' ^(Body $trysuite) ^(FinalBody $finalsuite))) + ( (except_clause+ (ORELSE COLON elsesuite=suite)? (FINALLY COLON finalsuite=suite)? + -> ^(TryExcept 'try' ^(Body $trysuite) except_clause+ ^(ORELSE $elsesuite)? ^(FINALLY $finalsuite)?)) + | (FINALLY COLON finalsuite=suite + -> ^(TryFinally 'try' ^(Body $trysuite) ^(FINALLY $finalsuite))) ) ; //with_stmt: 'with' test [ with_var ] ':' suite -with_stmt: 'with' test[expr_contextType.Load] (with_var)? COLON suite - -> ^(With test with_var? ^(Body suite)) +with_stmt: WITH test[expr_contextType.Load] (with_var)? COLON suite + -> ^(WITH test with_var? ^(Body suite)) ; //with_var: ('as' | NAME) expr -with_var: (keyAS | NAME) expr +with_var: (keyAS | NAME) expr[expr_contextType.Load] ; //except_clause: 'except' [test [',' test]] except_clause : 'except' (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? COLON suite //Note: passing the 'except' keyword on so we can pass the same offset // as CPython. - -> ^(ExceptHandler 'except' ^(Type $t1)? ^(NameTok $t2)? ^(Body suite)) + -> ^(ExceptHandler 'except' ^(Type $t1)? ^(Value $t2)? ^(Body suite)) ; //suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT @@ -914,37 +881,31 @@ ; //test: or_test ['if' or_test 'else' test] | lambdef -test[expr_contextType ect] -scope { - expr_contextType ctype; -} -@init { - $test::ctype = ect; -} - :o1=or_test - ( ('if' or_test 'else') => 'if' o2=or_test 'else' test[expr_contextType.Load] - -> ^(IfExp ^(Test $o2) ^(Body $o1) ^(OrElse test)) +test[expr_contextType ctype] + :o1=or_test[ctype] + ( (IF or_test[expr_contextType.Load] ORELSE) => IF o2=or_test[ctype] ORELSE test[expr_contextType.Load] + -> ^(IfExp ^(Test $o2) ^(Body $o1) ^(ORELSE test)) | -> or_test ) | lambdef {debug("parsed lambdef");} ; //or_test: and_test ('or' and_test)* -or_test : and_test (OR^ and_test)* +or_test[expr_contextType ctype] : and_test[ctype] (OR^ and_test[ctype])* ; //and_test: not_test ('and' not_test)* -and_test : not_test (AND^ not_test)* +and_test[expr_contextType ctype] : not_test[ctype] (AND^ not_test[ctype])* ; //not_test: 'not' not_test | comparison -not_test : NOT^ not_test - | comparison +not_test[expr_contextType ctype] : NOT^ not_test[ctype] + | comparison[ctype] ; //comparison: expr (comp_op expr)* -comparison: expr (comp_op^ expr)* - ; +comparison[expr_contextType ctype]: expr[ctype] (comp_op^ expr[ctype])* + ; //comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' comp_op : LESS @@ -961,9 +922,17 @@ ; //expr: xor_expr ('|' xor_expr)* -expr : xor_expr (VBAR^ xor_expr)* - ; +expr[expr_contextType ect] +scope { + expr_contextType ctype; +} +@init { + $expr::ctype = ect; +} + : xor_expr (VBAR^ xor_expr)* + ; + //xor_expr: and_expr ('^' and_expr)* xor_expr : and_expr (CIRCUMFLEX^ and_expr)* ; @@ -978,7 +947,7 @@ //arith_expr: term (('+'|'-') term)* arith_expr: term ((PLUS^|MINUS^) term)* - ; + ; //term: factor (('*'|'/'|'%'|'//') factor)* term : factor ((STAR^ | SLASH^ | PERCENT^ | DOUBLESLASH^ ) factor)* @@ -1013,13 +982,13 @@ RBRACK | LCURLY (dictmaker)? RCURLY -> ^(Dict LCURLY ^(Elts dictmaker)?) | BACKQUOTE testlist[expr_contextType.Load] BACKQUOTE -> ^(Repr BACKQUOTE testlist) - | NAME {System.out.println("NAME: " + $test::ctype);} -> ^(NameTok<Name>[$NAME, $NAME.text, $test::ctype]) + | NAME -> ^(NameTok<Name>[$NAME, $NAME.text, $expr::ctype]) | INT -> ^(NumTok<Num>[$INT, makeInt($INT)]) | LONGINT -> ^(NumTok<Num>[$LONGINT, makeInt($LONGINT)]) | FLOAT -> ^(NumTok<Num>[$FLOAT, makeFloat($FLOAT)]) | COMPLEX -> ^(NumTok<Num>[$COMPLEX, makeComplex($COMPLEX)]) - | (s+=STRING)+ {debug("s+: " + $s);} - -> ^(StrTok<Str>[extractStringToken($s), extractStrings($s)]) + | (S+=STRING)+ {debug("S+: " + $S);} + -> ^(StrTok<Str>[extractStringToken($S), extractStrings($S)]) ; //listmaker: test ( list_for | (',' test)* [','] ) @@ -1039,8 +1008,8 @@ ; //lambdef: 'lambda' [varargslist] ':' test -lambdef: 'lambda' (varargslist)? COLON test[expr_contextType.Load] {debug("parsed lambda");} - -> ^(Lambda 'lambda' varargslist? ^(Body test)) +lambdef: LAMBDA (varargslist)? COLON test[expr_contextType.Load] {debug("parsed lambda");} + -> ^(LAMBDA varargslist? ^(Body test)) ; //trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME @@ -1066,13 +1035,13 @@ ; //exprlist: expr (',' expr)* [','] -exprlist[expr_contextType ctype]: (expr COMMA) => expr (options {k=2;}: COMMA expr)* (COMMA)? -> ^(Tuple ^(Elts expr+)) - | expr +exprlist[expr_contextType ctype]: (expr[expr_contextType.Load] COMMA) => expr[ctype] (options {k=2;}: COMMA expr[ctype])* (COMMA)? -> ^(Tuple ^(Elts expr+)) + | expr[ctype] ; //XXX: I'm hoping I can get rid of this -- but for now I need an exprlist that does not produce tuples // at least for del_stmt -exprlist2 : expr (options {k=2;}: COMMA expr)* (COMMA)? +exprlist2 : expr[expr_contextType.Load] (options {k=2;}: COMMA expr[expr_contextType.Load])* (COMMA)? -> expr+ ; @@ -1093,8 +1062,8 @@ ; //classdef: 'class' NAME ['(' [testlist] ')'] ':' suite -classdef: 'class' NAME (LPAREN testlist[expr_contextType.Load]? RPAREN)? COLON suite - -> ^(ClassDef 'class' ^(NameTok NAME) ^(Bases testlist)? ^(Body suite)) +classdef: CLASS NAME (LPAREN testlist[expr_contextType.Load]? RPAREN)? COLON suite + -> ^(CLASS NAME ^(Bases testlist)? ^(Body suite)) ; //arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) @@ -1125,12 +1094,12 @@ ; //list_for: 'for' exprlist 'in' testlist_safe [list_iter] -list_for : 'for' exprlist[expr_contextType.Load] 'in' testlist[expr_contextType.Load] (list_iter)? - -> ^(ListFor ^(Target exprlist) ^(Iter testlist) ^(Ifs list_iter)?) +list_for : FOR exprlist[expr_contextType.Load] IN testlist[expr_contextType.Load] (list_iter)? + -> ^(ListFor ^(Target exprlist) ^(IN testlist) ^(Ifs list_iter)?) ; //list_if: 'if' test [list_iter] -list_if : 'if' test[expr_contextType.Load] (list_iter)? +list_if : IF test[expr_contextType.Load] (list_iter)? -> ^(ListIf ^(Target test) (Ifs list_iter)?) ; @@ -1140,18 +1109,18 @@ ; //gen_for: 'for' exprlist 'in' or_test [gen_iter] -gen_for: 'for' exprlist[expr_contextType.Load] 'in' or_test gen_iter? - -> ^(GenFor ^(Target exprlist) ^(Iter or_test) ^(Ifs gen_iter)?) +gen_for: FOR exprlist[expr_contextType.Load] IN or_test[expr_contextType.Load] gen_iter? + -> ^(GenFor ^(Target exprlist) ^(IN or_test) ^(Ifs gen_iter)?) ; //gen_if: 'if' old_test [gen_iter] -gen_if: 'if' test[expr_contextType.Load] gen_iter? +gen_if: IF test[expr_contextType.Load] gen_iter? -> ^(GenIf ^(Target test) ^(Ifs gen_iter)?) ; //yield_expr: 'yield' [testlist] -yield_expr : 'yield' testlist[expr_contextType.Load]? - -> ^(Yield 'yield' ^(Value testlist)?) +yield_expr : YIELD testlist[expr_contextType.Load]? + -> ^(YIELD ^(Value testlist)?) ; //XXX: @@ -1182,6 +1151,29 @@ //keyWITH : {input.LT(1).getText().equals("with")}? NAME ; //keyYIELD : {input.LT(1).getText().equals("yield")}? NAME ; +DEF : 'def' ; +CLASS : 'class' ; +PRINT : 'print' ; +BREAK : 'break' ; +CONTINUE : 'continue' ; +RETURN : 'return' ; +RAISE : 'raise' ; +PASS : 'pass' ; +IMPORT : 'import' ; +FROM : 'from' ; +FOR : 'for' ; +ORELSE : 'else' ; +ELIF : 'elif' ; +IN : 'in' ; +IF : 'if' ; +WHILE : 'while' ; +WITH : 'with' ; +LAMBDA : 'lambda' ; +GLOBAL : 'global' ; +YIELD : 'yield' ; +ASSERT : 'assert' ; +FINALLY : 'finally' ; +DELETE : 'del' ; LPAREN : '(' {implicitLineJoiningLevel++;} ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-25 03:04:10
|
Revision: 4759 http://jython.svn.sourceforge.net/jython/?rev=4759&view=rev Author: fwierzbicki Date: 2008-06-24 20:04:08 -0700 (Tue, 24 Jun 2008) Log Message: ----------- no arg Call in one pass. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 00:11:28 UTC (rev 4758) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 03:04:08 UTC (rev 4759) @@ -133,7 +133,7 @@ GeneratorExp; Ifs; Elts; - Call; + CallTok; Dest; Values; Newline; @@ -162,12 +162,15 @@ import org.python.antlr.ast.Attribute; import org.python.antlr.ast.BinOp; import org.python.antlr.ast.Break; +import org.python.antlr.ast.Call; import org.python.antlr.ast.Context; import org.python.antlr.ast.Continue; import org.python.antlr.ast.Expr; import org.python.antlr.ast.exprType; import org.python.antlr.ast.expr_contextType; import org.python.antlr.ast.FunctionDef; +import org.python.antlr.ast.Index; +import org.python.antlr.ast.keywordType; import org.python.antlr.ast.modType; import org.python.antlr.ast.Module; import org.python.antlr.ast.Name; @@ -553,7 +556,7 @@ : AT dotted_attr //XXX: ignoring the arglist and Call generation right now. ( (LPAREN arglist? RPAREN) { $etype = $dotted_attr.etype; } - // ^(Decorator dotted_attr ^(Call ^(Args arglist)?)) + // ^(Decorator dotted_attr ^(CallTok ^(Args arglist)?)) | { $etype = $dotted_attr.etype; } ) NEWLINE ; @@ -993,8 +996,31 @@ ; //power: atom trailer* ['**' factor] -power : atom (trailer^)* (options {greedy=true;}:DOUBLESTAR^ factor)? - ; +power returns [exprType etype] +@after { + if ($etype != null) { + $power.tree = $etype; + } +} + : atom (t+=trailer)* (options {greedy=true;}:DOUBLESTAR factor)? { + if ($t != null) { + exprType current = (exprType)$atom.tree; + for(int i = $t.size() - 1; i > -1; i--) { + Object o = $t.get(i); + //XXX: good place for an interface to avoid all of this instanceof + if (o instanceof Call) { + Call c = (Call)o; + c.func = current; + current = c; + } else if (o instanceof Index) { + Index c = (Index)o; + c.value = current; + } + $etype = (exprType)o; + } + } + } + ; //atom: ('(' [yield_expr|testlist_gexp] ')' | // '[' [listmaker] ']' | @@ -1045,7 +1071,10 @@ ; //trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME -trailer : LPAREN (arglist)? RPAREN -> ^(Call ^(Args arglist)?) +trailer : LPAREN (arglist -> ^(CallTok ^(Args arglist)?) + | -> ^(LPAREN<Call>[$LPAREN, null, new exprType[0\], new keywordType[0\], null, null]) + ) + RPAREN | LBRACK subscriptlist RBRACK -> ^(SubscriptList subscriptlist) | DOT^ NAME {debug("motched DOT^ NAME");} ; @@ -1059,7 +1088,7 @@ subscript : DOT DOT DOT -> Ellipsis | (test[expr_contextType.Load] COLON) => t1=test[expr_contextType.Load] (COLON (t2=test[expr_contextType.Load])? (sliceop)?)? -> ^(Subscript ^(Lower $t1) ^(Upper COLON ^(UpperOp $t2)?)? sliceop?) | (COLON) => COLON (test[expr_contextType.Load])? (sliceop)? -> ^(Subscript ^(Upper COLON ^(UpperOp test)?)? sliceop?) - | test[expr_contextType.Load] -> ^(Index test) + | test[expr_contextType.Load] -> ^(Subscript<Index>[$test.start, (exprType)$test.tree]) ; //sliceop: ':' [test] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-25 13:06:12
|
Revision: 4760 http://jython.svn.sourceforge.net/jython/?rev=4760&view=rev Author: fwierzbicki Date: 2008-06-25 06:06:09 -0700 (Wed, 25 Jun 2008) Log Message: ----------- support one pass for Index. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 03:04:08 UTC (rev 4759) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 13:06:09 UTC (rev 4760) @@ -117,7 +117,6 @@ ListComp; Repr; Subscript; - SubscriptList; Index; Target; Value; @@ -168,6 +167,7 @@ import org.python.antlr.ast.Expr; import org.python.antlr.ast.exprType; import org.python.antlr.ast.expr_contextType; +import org.python.antlr.ast.ExtSlice; import org.python.antlr.ast.FunctionDef; import org.python.antlr.ast.Index; import org.python.antlr.ast.keywordType; @@ -179,8 +179,11 @@ import org.python.antlr.ast.Print; import org.python.antlr.ast.operatorType; import org.python.antlr.ast.Return; +import org.python.antlr.ast.sliceType; import org.python.antlr.ast.stmtType; import org.python.antlr.ast.Str; +import org.python.antlr.ast.Subscript; +import org.python.antlr.ast.Tuple; import org.python.core.Py; import org.python.core.PyString; import org.python.core.PyUnicode; @@ -1012,12 +1015,13 @@ Call c = (Call)o; c.func = current; current = c; - } else if (o instanceof Index) { - Index c = (Index)o; + } else if (o instanceof Subscript) { + Subscript c = (Subscript)o; c.value = current; + current = c; } - $etype = (exprType)o; } + $etype = (exprType)current; } } ; @@ -1075,14 +1079,45 @@ | -> ^(LPAREN<Call>[$LPAREN, null, new exprType[0\], new keywordType[0\], null, null]) ) RPAREN - | LBRACK subscriptlist RBRACK -> ^(SubscriptList subscriptlist) + | LBRACK s=subscriptlist RBRACK -> $s | DOT^ NAME {debug("motched DOT^ NAME");} ; //subscriptlist: subscript (',' subscript)* [','] -subscriptlist : subscript (options {greedy=true;}:COMMA subscript)* (COMMA)? - -> subscript+ - ; +subscriptlist returns [exprType etype] +@after { + $subscriptlist.tree = $etype; +} + : sub+=subscript (options {greedy=true;}:COMMA sub+=subscript)* (COMMA)? { + sliceType s; + List sltypes = $sub; + if (sltypes.size() == 0) { + s = null; + } else if (sltypes.size() == 1){ + s = (sliceType)sltypes.get(0); + } else { + sliceType[] st; + //FIXME: here I am using ClassCastException to decide if sltypes is populated with Index + // only. Clearly this is not the best way to do this but it's late. Somebody do + // something better please :) -- (hopefully a note to self) + try { + Iterator iter = sltypes.iterator(); + List etypes = new ArrayList(); + while (iter.hasNext()) { + Index i = (Index)iter.next(); + etypes.add(i.value); + } + exprType[] es = (exprType[])etypes.toArray(new exprType[etypes.size()]); + exprType t = new Tuple($subscriptlist.start, es, expr_contextType.Load); + s = new Index($subscriptlist.start, t); + } catch (ClassCastException cc) { + st = (sliceType[])sltypes.toArray(new sliceType[sltypes.size()]); + s = new ExtSlice($subscriptlist.start, st); + } + } + $etype = new Subscript($subscriptlist.start, null, s, $expr::ctype); + } + ; //subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] subscript : DOT DOT DOT -> Ellipsis This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-25 19:26:17
|
Revision: 4766 http://jython.svn.sourceforge.net/jython/?rev=4766&view=rev Author: fwierzbicki Date: 2008-06-25 12:26:09 -0700 (Wed, 25 Jun 2008) Log Message: ----------- Start of function arguments in one pass. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 19:05:09 UTC (rev 4765) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 19:26:09 UTC (rev 4766) @@ -193,6 +193,10 @@ } @members { + //If you want to use antlr's default error recovery mechanisms change this + //and the same one in the lexer to true. + public boolean antlrErrorHandling = false; + //XXX: only used for single_input -- seems kludgy. public boolean inSingle = false; private boolean seenSingleOuterSuite = false; @@ -430,57 +434,34 @@ protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { - throw new MismatchedTokenException(ttype, input); + if (antlrErrorHandling) { + super.mismatch(input, ttype, follow); + } else { + throw new MismatchedTokenException(ttype, input); + } } - protected void mismatch(IntStream input, RecognitionException e, BitSet follow) throws RecognitionException { - throw e; - } - protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException { + if (antlrErrorHandling) { + return super.recoverFromMismatchedToken(input, ttype, follow); + } mismatch(input, ttype, follow); return null; } - - /** - * A list holding the error message(s) encountered during parse. - */ - private List<String> _errors = new ArrayList<String>(); - - /** - * @return <code>true</code> if the parser collected one or more error messages, - * <code>false</code> otherwise. - */ - public boolean hasErrors() { - return getErrors().size() > 0; - } - - /** - * @return A list of the error message(s) collected during parse. - */ - public List<String> getErrors() { - return _errors; - } - - /** - * Overridden to be able to collect error messages. - * <p> - * Since we do not want to lose the recovery mechanism and the verbose messages. - */ - @Override - public void emitErrorMessage(String msg) { - super.emitErrorMessage(msg); - getErrors().add(msg); - } - } @rulecatch { -catch (RecognitionException r) { - throw new ParseException(r); +catch (RecognitionException re) { + if (antlrErrorHandling) { + reportError(re); + recover(input,re); + retval.tree = (PythonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + } else { + throw new ParseException(re); + } } } @@ -494,12 +475,20 @@ * a = [3, * 4] */ + +//If you want to use antlr's default error recovery mechanisms change this +//and the same one in the parser to true. +public boolean antlrErrorHandling = false; + //XXX: Hopefully we can remove inSingle when we get PyCF_DONT_IMPLY_DEDENT support. public boolean inSingle = false; int implicitLineJoiningLevel = 0; int startPos=-1; public Token nextToken() { + if (antlrErrorHandling) { + return super.nextToken(); + } while (true) { state.token = null; state.channel = Token.DEFAULT_CHANNEL; @@ -1075,7 +1064,7 @@ ; //trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME -trailer : LPAREN (arglist -> ^(CallTok ^(Args arglist)?) +trailer : LPAREN (arglist -> ^(LPAREN<Call>[$LPAREN, null, new exprType[0\], new keywordType[0\], null, null]) | -> ^(LPAREN<Call>[$LPAREN, null, new exprType[0\], new keywordType[0\], null, null]) ) RPAREN @@ -1163,26 +1152,37 @@ ; //arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) -arglist : argument (COMMA argument)* - ( COMMA - ( STAR starargs=test[expr_contextType.Load] (COMMA DOUBLESTAR kwargs=test[expr_contextType.Load])? - | DOUBLESTAR kwargs=test[expr_contextType.Load] - )? - )? - -> ^(Args argument+) ^(StarArgs $starargs)? ^(KWArgs $kwargs)? - | STAR starargs=test[expr_contextType.Load] (COMMA DOUBLESTAR kwargs=test[expr_contextType.Load])? - -> ^(StarArgs $starargs) ^(KWArgs $kwargs)? - | DOUBLESTAR kwargs=test[expr_contextType.Load] - -> ^(KWArgs $kwargs) +arglist returns [List args, exprType starargs, exprType kwargs] + : a+=argument (COMMA a+=argument)* + ( COMMA + ( STAR s=test[expr_contextType.Load] (COMMA DOUBLESTAR k=test[expr_contextType.Load])? + | DOUBLESTAR k=test[expr_contextType.Load] + )? + )? { + $args=$a; + $starargs=(exprType)$s.tree; + $kwargs=(exprType)$k.tree; + } + | STAR s=test[expr_contextType.Load] (COMMA DOUBLESTAR k=test[expr_contextType.Load])? { + $starargs=(exprType)$s.tree; + $kwargs=(exprType)$k.tree; + } + | DOUBLESTAR k=test[expr_contextType.Load] { + $kwargs=(exprType)$k.tree; + } ; //argument: test [gen_for] | test '=' test # Really [keyword '='] test -argument : t1=test[expr_contextType.Load] - ( (ASSIGN t2=test[expr_contextType.Load]) -> ^(Keyword ^(Arg $t1) ^(Value $t2)?) - | gen_for -> ^(GenFor $t1 gen_for) - | -> ^(Arg $t1) - ) - ; +argument returns [exprType arg, exprType value] + : t1=test[expr_contextType.Load] + ( (ASSIGN t2=test[expr_contextType.Load]) { + $arg=(exprType)$t1.tree; + $value=(exprType)$t2.tree; + } + | gen_for //FIXME + | {$arg=(exprType)$t1.tree;} + ) + ; //list_iter: list_for | list_if list_iter : list_for This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-25 22:46:16
|
Revision: 4773 http://jython.svn.sourceforge.net/jython/?rev=4773&view=rev Author: fwierzbicki Date: 2008-06-25 15:46:10 -0700 (Wed, 25 Jun 2008) Log Message: ----------- one pass support for arguments and keywords for Call. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 20:48:27 UTC (rev 4772) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 22:46:10 UTC (rev 4773) @@ -220,7 +220,7 @@ exprType e = (exprType)exprs.get(i); result.add(e); } - return (exprType[])result.toArray(new exprType[result.size()]); + return result.toArray(new exprType[result.size()]); } return new exprType[0]; } @@ -315,6 +315,23 @@ return new argumentsType(t, p, s, k, d); } + exprType[] extractArgs(List args) { + if (args == null) { + return new exprType[0]; + } + return (exprType[])args.toArray(new exprType[args.size()]); + } + + keywordType[] makeKeywords(List args) { + List<keywordType> k = new ArrayList<keywordType>(); + for(int i=0;i<args.size();i++) { + exprType[] e = (exprType[])args.get(i); + Name arg = (Name)e[0]; + k.add(new keywordType(arg, arg.id, e[1])); + } + return k.toArray(new keywordType[k.size()]); + } + Object makeFloat(Token t) { debug("makeFloat matched " + t.getText()); return Py.newFloat(Double.valueOf(t.getText())); @@ -1064,7 +1081,7 @@ ; //trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME -trailer : LPAREN (arglist -> ^(LPAREN<Call>[$LPAREN, null, new exprType[0\], new keywordType[0\], null, null]) +trailer : LPAREN (arglist -> ^(LPAREN<Call>[$LPAREN, null, makeExprs($arglist.args), makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs]) | -> ^(LPAREN<Call>[$LPAREN, null, new exprType[0\], new keywordType[0\], null, null]) ) RPAREN @@ -1152,14 +1169,19 @@ ; //arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) -arglist returns [List args, exprType starargs, exprType kwargs] - : a+=argument (COMMA a+=argument)* +arglist returns [List args, List keywords, exprType starargs, exprType kwargs] +@init { + List arguments = new ArrayList(); + List kws = new ArrayList(); +} + : argument[arguments, kws] (COMMA argument[arguments, kws])* ( COMMA ( STAR s=test[expr_contextType.Load] (COMMA DOUBLESTAR k=test[expr_contextType.Load])? | DOUBLESTAR k=test[expr_contextType.Load] )? )? { - $args=$a; + $args=arguments; + $keywords=kws; $starargs=(exprType)$s.tree; $kwargs=(exprType)$k.tree; } @@ -1173,14 +1195,13 @@ ; //argument: test [gen_for] | test '=' test # Really [keyword '='] test -argument returns [exprType arg, exprType value] +argument[List arguments, List kws] : t1=test[expr_contextType.Load] ( (ASSIGN t2=test[expr_contextType.Load]) { - $arg=(exprType)$t1.tree; - $value=(exprType)$t2.tree; + $kws.add(new exprType[]{(exprType)$t1.tree, (exprType)$t2.tree}); } | gen_for //FIXME - | {$arg=(exprType)$t1.tree;} + | {$arguments.add($t1.tree);} ) ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-25 23:11:18
|
Revision: 4775 http://jython.svn.sourceforge.net/jython/?rev=4775&view=rev Author: fwierzbicki Date: 2008-06-25 16:11:12 -0700 (Wed, 25 Jun 2008) Log Message: ----------- one pass Return and Decorators. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 23:08:30 UTC (rev 4774) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 23:11:12 UTC (rev 4775) @@ -563,9 +563,12 @@ $decorator.tree = $etype; } : AT dotted_attr - //XXX: ignoring the arglist and Call generation right now. - ( (LPAREN arglist? RPAREN) { $etype = $dotted_attr.etype; } - // ^(Decorator dotted_attr ^(CallTok ^(Args arglist)?)) + ( LPAREN (arglist + {$etype = new Call($LPAREN, $dotted_attr.etype, makeExprs($arglist.args), + makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs);} + | {$etype = $dotted_attr.etype;} + ) + RPAREN | { $etype = $dotted_attr.etype; } ) NEWLINE ; @@ -768,8 +771,10 @@ ; //return_stmt: 'return' [testlist] -return_stmt : RETURN (testlist[expr_contextType.Load])? - -> ^(RETURN ^(Value testlist)?) +return_stmt : RETURN + (testlist[expr_contextType.Load] -> ^(RETURN<Return>[$RETURN, (exprType)$testlist.tree]) + | -> ^(RETURN<Return>[$RETURN, null]) + ) ; //yield_stmt: yield_expr This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-26 00:07:45
|
Revision: 4776 http://jython.svn.sourceforge.net/jython/?rev=4776&view=rev Author: fwierzbicki Date: 2008-06-25 17:07:44 -0700 (Wed, 25 Jun 2008) Log Message: ----------- one pass for param defaults, some work on fplist and tuples. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-25 23:11:12 UTC (rev 4775) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-26 00:07:44 UTC (rev 4776) @@ -97,7 +97,7 @@ KWArgs; AssignTok; AugAssign; - Tuple; + TupleTok; List; Dict; IfExp; @@ -217,6 +217,7 @@ if (exprs != null) { List<exprType> result = new ArrayList<exprType>(); for (int i=start; i<exprs.size(); i++) { + //System.out.println(exprs.get(i).getClass().getName()); exprType e = (exprType)exprs.get(i); result.add(e); } @@ -563,12 +564,14 @@ $decorator.tree = $etype; } : AT dotted_attr + //XXX: ignoring the arglist and Call generation right now. ( LPAREN (arglist {$etype = new Call($LPAREN, $dotted_attr.etype, makeExprs($arglist.args), makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs);} | {$etype = $dotted_attr.etype;} ) RPAREN + // ^(Decorator dotted_attr ^(CallTok ^(Args arglist)?)) | { $etype = $dotted_attr.etype; } ) NEWLINE ; @@ -601,7 +604,7 @@ : fpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? { $etype = (exprType)$fpdef.tree; if ($ASSIGN != null) { - //defaults.add(test); + defaults.add($test.tree); } else if (!defaults.isEmpty()) { throw new ParseException( "non-default argument follows default argument", @@ -633,15 +636,15 @@ //fpdef: NAME | '(' fplist ')' fpdef[expr_contextType ctype] : NAME -> ^(NameTok<Name>[$NAME, $NAME.text, ctype]) - | LPAREN fplist RPAREN {debug("parsed fpdef:fplist");} - -> ^(FpList fplist) + | LPAREN fplist RPAREN + -> ^(LPAREN<Tuple>[$fplist.start, makeExprs($fplist.etypes), expr_contextType.Store]) ; //fplist: fpdef (',' fpdef)* [','] -fplist : fpdef[expr_contextType.Store] (options {greedy=true;}:COMMA fpdef[expr_contextType.Store])* (COMMA)? - {debug("parsed fplist");} - -> fpdef+ - ; +fplist returns [List etypes] + : f+=fpdef[expr_contextType.Store] (options {greedy=true;}:COMMA f+=fpdef[expr_contextType.Store])* (COMMA)? + {$etypes = $f;} + ; //stmt: simple_stmt | compound_stmt stmt : simple_stmt @@ -1045,7 +1048,7 @@ atom : LPAREN ( yield_expr -> ^(Parens LPAREN yield_expr) | testlist_gexp {debug("parsed testlist_gexp");} -> ^(Parens LPAREN testlist_gexp) - | -> ^(Tuple) + | -> ^(TupleTok) ) RPAREN | LBRACK @@ -1073,7 +1076,7 @@ //testlist_gexp: test ( gen_for | (',' test)* [','] ) testlist_gexp - : test[expr_contextType.Load] ( ((options {k=2;}: c1=COMMA test[expr_contextType.Load])* (c2=COMMA)? -> { $c1 != null || $c2 != null }? ^(Tuple ^(Elts test+)) + : test[expr_contextType.Load] ( ((options {k=2;}: c1=COMMA test[expr_contextType.Load])* (c2=COMMA)? -> { $c1 != null || $c2 != null }? ^(TupleTok ^(Elts test+)) -> test ) | ( gen_for -> ^(GeneratorExp test gen_for)) @@ -1142,7 +1145,7 @@ ; //exprlist: expr (',' expr)* [','] -exprlist[expr_contextType ctype]: (expr[expr_contextType.Load] COMMA) => expr[ctype] (options {k=2;}: COMMA expr[ctype])* (COMMA)? -> ^(Tuple ^(Elts expr+)) +exprlist[expr_contextType ctype]: (expr[expr_contextType.Load] COMMA) => expr[ctype] (options {k=2;}: COMMA expr[ctype])* (COMMA)? -> ^(TupleTok ^(Elts expr+)) | expr[ctype] ; @@ -1154,8 +1157,8 @@ //testlist: test (',' test)* [','] testlist[expr_contextType ctype] - : test[ctype] (options {k=2;}: c1=COMMA test[ctype])* (c2=COMMA)? - -> { $c1 != null || $c2 != null }? ^(Tuple ^(Elts test+)) + : t+=test[ctype] (options {k=2;}: c1=COMMA t+=test[ctype])* (c2=COMMA)? + -> { $c1 != null || $c2 != null }? ^(TupleTok<Tuple>[$testlist.start, makeExprs($t), ctype]) -> test ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 02:36:47
|
Revision: 4812 http://jython.svn.sourceforge.net/jython/?rev=4812&view=rev Author: fwierzbicki Date: 2008-06-27 19:36:45 -0700 (Fri, 27 Jun 2008) Log Message: ----------- Lists, Tuples, start of ClassDef Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-27 19:02:36 UTC (rev 4811) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 02:36:45 UTC (rev 4812) @@ -98,7 +98,7 @@ AssignTok; AugAssign; TupleTok; - List; + ListTok; Dict; IfExp; TryExcept; @@ -162,8 +162,10 @@ import org.python.antlr.ast.BinOp; import org.python.antlr.ast.Break; import org.python.antlr.ast.Call; +import org.python.antlr.ast.ClassDef; import org.python.antlr.ast.Context; import org.python.antlr.ast.Continue; +import org.python.antlr.ast.Delete; import org.python.antlr.ast.Expr; import org.python.antlr.ast.exprType; import org.python.antlr.ast.expr_contextType; @@ -209,6 +211,17 @@ } } + private exprType[] makeBases(exprType etype) { + if (etype != null) { + if (etype instanceof Tuple) { + return ((Tuple)etype).elts; + } + return new exprType[]{etype}; + } + return new exprType[0]; + } + + private exprType[] makeExprs(List exprs) { return makeExprs(exprs, 0); } @@ -217,9 +230,12 @@ if (exprs != null) { List<exprType> result = new ArrayList<exprType>(); for (int i=start; i<exprs.size(); i++) { - //System.out.println(exprs.get(i).getClass().getName()); - exprType e = (exprType)exprs.get(i); - result.add(e); + Object o = exprs.get(i); + if (o instanceof exprType) { + result.add((exprType)o); + } else if (o instanceof test_return) { + result.add((exprType)((test_return)o).tree); + } } return result.toArray(new exprType[result.size()]); } @@ -232,7 +248,7 @@ for (int i=0; i<stmts.size(); i++) { Object o = stmts.get(i); if (o instanceof stmtType) { - result.add((stmtType)stmts.get(i)); + result.add((stmtType)o); } else { result.add((stmtType)((stmt_return)o).tree); } @@ -747,7 +763,7 @@ //del_stmt: 'del' exprlist del_stmt : DELETE exprlist2 - -> ^(DELETE exprlist2) + -> ^(DELETE<Delete>[$DELETE, makeExprs($exprlist2.etypes)]) ; //pass_stmt: 'pass' @@ -1047,13 +1063,13 @@ // NAME | NUMBER | STRING+) atom : LPAREN ( yield_expr -> ^(Parens LPAREN yield_expr) - | testlist_gexp {debug("parsed testlist_gexp");} -> ^(Parens LPAREN testlist_gexp) - | -> ^(TupleTok) + | testlist_gexp -> testlist_gexp + | -> ^(TupleTok<Tuple>[$LPAREN, new exprType[0\], $expr::ctype]) ) RPAREN | LBRACK - (listmaker -> ^(Brackets LBRACK listmaker) - | -> ^(Brackets LBRACK ^(List)) + (listmaker -> listmaker + | -> ^(Brackets LBRACK ^(ListTok)) ) RBRACK | LCURLY (dictmaker)? RCURLY -> ^(Dict LCURLY ^(Elts dictmaker)?) @@ -1068,19 +1084,28 @@ ; //listmaker: test ( list_for | (',' test)* [','] ) -listmaker : test[expr_contextType.Load] +listmaker returns [exprType etype] +@after { + $listmaker.tree = $etype; +} + : t+=test[expr_contextType.Load] ( list_for -> ^(ListComp test list_for) - | (options {greedy=true;}:COMMA test[expr_contextType.Load])* -> ^(List ^(Elts test+)) + | (options {greedy=true;}:COMMA t+=test[expr_contextType.Load])* { + $etype = new org.python.antlr.ast.List($listmaker.start, makeExprs($t), $expr::ctype); + } ) (COMMA)? ; //testlist_gexp: test ( gen_for | (',' test)* [','] ) testlist_gexp - : test[expr_contextType.Load] ( ((options {k=2;}: c1=COMMA test[expr_contextType.Load])* (c2=COMMA)? -> { $c1 != null || $c2 != null }? ^(TupleTok ^(Elts test+)) - -> test - ) - | ( gen_for -> ^(GeneratorExp test gen_for)) - ) + : t+=test[expr_contextType.Load] + ( ((options {k=2;}: c1=COMMA t+=test[expr_contextType.Load])* (c2=COMMA)? + -> { $c1 != null || $c2 != null }? ^(TupleTok<Tuple>[$testlist_gexp.start, makeExprs($t), $expr::ctype]) + -> test + ) + | ( gen_for -> ^(GeneratorExp test gen_for) + ) + ) ; //lambdef: 'lambda' [varargslist] ':' test @@ -1145,21 +1170,30 @@ ; //exprlist: expr (',' expr)* [','] -exprlist[expr_contextType ctype]: (expr[expr_contextType.Load] COMMA) => expr[ctype] (options {k=2;}: COMMA expr[ctype])* (COMMA)? -> ^(TupleTok ^(Elts expr+)) - | expr[ctype] - ; +exprlist[expr_contextType ctype] returns [exprType etype] + : (expr[expr_contextType.Load] COMMA) => e+=expr[ctype] (options {k=2;}: COMMA e+=expr[ctype])* (COMMA)? { + $etype = new Tuple($exprlist.start, makeExprs($e), ctype); + } + | expr[ctype] { + $etype = (exprType)$expr.tree; + } + ; //XXX: I'm hoping I can get rid of this -- but for now I need an exprlist that does not produce tuples // at least for del_stmt -exprlist2 : expr[expr_contextType.Load] (options {k=2;}: COMMA expr[expr_contextType.Load])* (COMMA)? - -> expr+ - ; +exprlist2 returns [List etypes] + : e+=expr[expr_contextType.Load] (options {k=2;}: COMMA e+=expr[expr_contextType.Load])* (COMMA)? + {$etypes = $e;} + ; //testlist: test (',' test)* [','] -testlist[expr_contextType ctype] - : t+=test[ctype] (options {k=2;}: c1=COMMA t+=test[ctype])* (c2=COMMA)? - -> { $c1 != null || $c2 != null }? ^(TupleTok<Tuple>[$testlist.start, makeExprs($t), ctype]) - -> test +testlist[expr_contextType ctype] returns [exprType etype] + : (test[expr_contextType.Load] COMMA) => t+=test[ctype] (options {k=2;}: c1=COMMA t+=test[ctype])* (c2=COMMA)? { + $etype = new Tuple($testlist.start, makeExprs($t), ctype); + } + | test[ctype] { + $etype = (exprType)$test.tree; + } ; //XXX: @@ -1172,8 +1206,13 @@ ; //classdef: 'class' NAME ['(' [testlist] ')'] ':' suite -classdef: CLASS NAME (LPAREN testlist[expr_contextType.Load]? RPAREN)? COLON suite - -> ^(CLASS NAME ^(Bases testlist)? ^(Body suite)) +classdef returns [stmtType stype] +@after { + $classdef.tree = $stype; +} + :CLASS NAME (LPAREN testlist[expr_contextType.Load]? RPAREN)? COLON suite { + $stype = new ClassDef($CLASS, $NAME.getText(), makeBases($testlist.etype), makeStmts($suite.stmts)); + } ; //arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 13:01:35
|
Revision: 4813 http://jython.svn.sourceforge.net/jython/?rev=4813&view=rev Author: fwierzbicki Date: 2008-06-28 06:01:34 -0700 (Sat, 28 Jun 2008) Log Message: ----------- simple imports in one pass. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 02:36:45 UTC (rev 4812) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 13:01:34 UTC (rev 4813) @@ -156,6 +156,7 @@ import org.python.antlr.ParseException; import org.python.antlr.PythonTree; +import org.python.antlr.ast.aliasType; import org.python.antlr.ast.argumentsType; import org.python.antlr.ast.Assign; import org.python.antlr.ast.Attribute; @@ -171,6 +172,7 @@ import org.python.antlr.ast.expr_contextType; import org.python.antlr.ast.ExtSlice; import org.python.antlr.ast.FunctionDef; +import org.python.antlr.ast.Import; import org.python.antlr.ast.Index; import org.python.antlr.ast.keywordType; import org.python.antlr.ast.modType; @@ -812,7 +814,7 @@ //import_name: 'import' dotted_as_names import_name : IMPORT dotted_as_names - -> ^(IMPORT dotted_as_names) + -> ^(IMPORT<Import>[$IMPORT, $dotted_as_names.atypes]) ; //import_from: ('from' ('.'* dotted_name | '.'+) @@ -841,13 +843,22 @@ // 'as' to be a method name for Java integration). //dotted_as_name: dotted_name [('as' | NAME) NAME] -dotted_as_name : dotted_name (keyAS asname=NAME)? - -> ^(Alias dotted_name ^(Asname NAME)?) - ; +dotted_as_name returns [aliasType atype] +@after { + $dotted_as_name.tree = $atype; +} + : dotted_name (keyAS NAME)? { + $atype = new aliasType($NAME, $dotted_name.text, $NAME.text); + } + ; + //dotted_as_names: dotted_as_name (',' dotted_as_name)* -dotted_as_names : dotted_as_name (COMMA! dotted_as_name)* - ; +dotted_as_names returns [aliasType[\] atypes] + : d+=dotted_as_name (COMMA! d+=dotted_as_name)* { + $atypes = (aliasType[])$d.toArray(new aliasType[$d.size()]); + } + ; //dotted_name: NAME ('.' NAME)* dotted_name : NAME (DOT NAME)* ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 15:31:26
|
Revision: 4814 http://jython.svn.sourceforge.net/jython/?rev=4814&view=rev Author: fwierzbicki Date: 2008-06-28 08:31:25 -0700 (Sat, 28 Jun 2008) Log Message: ----------- The rest of import in one pass. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 13:01:34 UTC (rev 4813) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 15:31:25 UTC (rev 4814) @@ -173,6 +173,7 @@ import org.python.antlr.ast.ExtSlice; import org.python.antlr.ast.FunctionDef; import org.python.antlr.ast.Import; +import org.python.antlr.ast.ImportFrom; import org.python.antlr.ast.Index; import org.python.antlr.ast.keywordType; import org.python.antlr.ast.modType; @@ -213,6 +214,37 @@ } } + private String makeFromText(List dots, String name) { + StringBuffer d = new StringBuffer(); + if (dots != null) { + for (int i=0;i<dots.size();i++) { + d.append("."); + } + } + if (name != null) { + d.append(name); + } + return d.toString(); + } + + private int makeLevel(List lev) { + if (lev == null) { + return 0; + } + return lev.size(); + } + + private aliasType[] makeStarAlias(Token t) { + return new aliasType[]{new aliasType(t, "*", null)}; + } + + private aliasType[] makeAliases(aliasType[] atypes) { + if (atypes == null) { + return new aliasType[0]; + } + return atypes; + } + private exprType[] makeBases(exprType etype) { if (etype != null) { if (etype instanceof Tuple) { @@ -819,24 +851,32 @@ //import_from: ('from' ('.'* dotted_name | '.'+) // 'import' ('*' | '(' import_as_names ')' | import_as_names)) -import_from: FROM (DOT* dotted_name | DOT+) IMPORT +import_from: FROM (d+=DOT* dotted_name | d+=DOT+) IMPORT (STAR - -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT STAR)) - | import_as_names - -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT import_as_names)) - | LPAREN import_as_names RPAREN - -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT import_as_names)) + -> ^(FROM<ImportFrom>[$FROM, makeFromText($d, $dotted_name.text), makeStarAlias($STAR), makeLevel($d)]) + | i1=import_as_names + -> ^(FROM<ImportFrom>[$FROM, makeFromText($d, $dotted_name.text), makeAliases($i1.atypes), makeLevel($d)]) + | LPAREN i2=import_as_names RPAREN + -> ^(FROM<ImportFrom>[$FROM, makeFromText($d, $dotted_name.text), makeAliases($i2.atypes), makeLevel($d)]) ) ; //import_as_names: import_as_name (',' import_as_name)* [','] -import_as_names : import_as_name (COMMA! import_as_name)* (COMMA!)? - ; +import_as_names returns [aliasType[\] atypes] + : n+=import_as_name (COMMA! n+=import_as_name)* (COMMA!)? { + $atypes = (aliasType[])$n.toArray(new aliasType[$n.size()]); + } + ; //import_as_name: NAME [('as' | NAME) NAME] -import_as_name : name=NAME (keyAS asname=NAME)? - -> ^(Alias $name ^(Asname $asname)?) - ; +import_as_name returns [aliasType atype] +@after { + $import_as_name.tree = $atype; +} + : name=NAME (keyAS asname=NAME)? { + $atype = new aliasType($name, $name.text, $asname.text); + } + ; //XXX: when does CPython Grammar match "dotted_name NAME NAME"? This may be a big // problem because of the keyAS rule, which matches NAME (needed to allow @@ -859,6 +899,7 @@ $atypes = (aliasType[])$d.toArray(new aliasType[$d.size()]); } ; + //dotted_name: NAME ('.' NAME)* dotted_name : NAME (DOT NAME)* ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 15:39:11
|
Revision: 4815 http://jython.svn.sourceforge.net/jython/?rev=4815&view=rev Author: fwierzbicki Date: 2008-06-28 08:39:10 -0700 (Sat, 28 Jun 2008) Log Message: ----------- partial merge with asm branch. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 15:31:25 UTC (rev 4814) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 15:39:10 UTC (rev 4815) @@ -256,6 +256,10 @@ } + private void throwGenExpNotSoleArg(PythonTree t) { + throw new ParseException("Generator expression must be parenthesized if not sole argument", t); + } + private exprType[] makeExprs(List exprs) { return makeExprs(exprs, 0); } @@ -686,6 +690,8 @@ //fpdef: NAME | '(' fplist ')' fpdef[expr_contextType ctype] : NAME -> ^(NameTok<Name>[$NAME, $NAME.text, ctype]) + | (LPAREN fpdef[expr_contextType.Load] COMMA) => LPAREN fplist RPAREN + -> fplist | LPAREN fplist RPAREN -> ^(LPAREN<Tuple>[$fplist.start, makeExprs($fplist.etypes), expr_contextType.Store]) ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 17:03:29
|
Revision: 4816 http://jython.svn.sourceforge.net/jython/?rev=4816&view=rev Author: fwierzbicki Date: 2008-06-28 10:03:28 -0700 (Sat, 28 Jun 2008) Log Message: ----------- one pass Raise. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 15:39:10 UTC (rev 4815) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 17:03:28 UTC (rev 4816) @@ -182,6 +182,7 @@ import org.python.antlr.ast.Num; import org.python.antlr.ast.Pass; import org.python.antlr.ast.Print; +import org.python.antlr.ast.Raise; import org.python.antlr.ast.operatorType; import org.python.antlr.ast.Return; import org.python.antlr.ast.sliceType; @@ -842,7 +843,7 @@ //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 ^(Type $t1)? ^(Inst $t2)? ^(Tback $t3)?) + -> ^(RAISE<Raise>[$RAISE, (exprType)$t1.tree, (exprType)$t2.tree, (exprType)$t3.tree]) ; //import_stmt: import_name | import_from This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 18:11:33
|
Revision: 4817 http://jython.svn.sourceforge.net/jython/?rev=4817&view=rev Author: fwierzbicki Date: 2008-06-28 11:11:31 -0700 (Sat, 28 Jun 2008) Log Message: ----------- Global, Exec, Assert in one pass. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 17:03:28 UTC (rev 4816) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 18:11:31 UTC (rev 4817) @@ -158,6 +158,7 @@ import org.python.antlr.PythonTree; import org.python.antlr.ast.aliasType; import org.python.antlr.ast.argumentsType; +import org.python.antlr.ast.Assert; import org.python.antlr.ast.Assign; import org.python.antlr.ast.Attribute; import org.python.antlr.ast.BinOp; @@ -167,11 +168,13 @@ import org.python.antlr.ast.Context; import org.python.antlr.ast.Continue; import org.python.antlr.ast.Delete; +import org.python.antlr.ast.Exec; import org.python.antlr.ast.Expr; import org.python.antlr.ast.exprType; import org.python.antlr.ast.expr_contextType; import org.python.antlr.ast.ExtSlice; import org.python.antlr.ast.FunctionDef; +import org.python.antlr.ast.Global; import org.python.antlr.ast.Import; import org.python.antlr.ast.ImportFrom; import org.python.antlr.ast.Index; @@ -256,6 +259,13 @@ return new exprType[0]; } + private String[] makeNames(List names) { + List<String> s = new ArrayList<String>(); + for(int i=0;i<names.size();i++) { + s.add(((Token)names.get(i)).getText()); + } + return s.toArray(new String[s.size()]); + } private void throwGenExpNotSoleArg(PythonTree t) { throw new ParseException("Generator expression must be parenthesized if not sole argument", t); @@ -912,18 +922,23 @@ ; //global_stmt: 'global' NAME (',' NAME)* -global_stmt : GLOBAL NAME (COMMA NAME)* - -> ^(GLOBAL NAME+) +global_stmt : GLOBAL n+=NAME (COMMA n+=NAME)* + -> ^(GLOBAL<Global>[$GLOBAL, makeNames($n)]) ; //exec_stmt: 'exec' expr ['in' test [',' test]] -exec_stmt : keyEXEC expr[expr_contextType.Load] ('in' t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? - -> ^(keyEXEC expr ^(Globals $t1)? ^(Locals $t2)?) - ; +exec_stmt returns [stmtType stype] +@after { + $exec_stmt.tree = $stype; +} + : keyEXEC expr[expr_contextType.Load] ('in' t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? { + $stype = new Exec($expr.start, (exprType)$expr.tree, (exprType)$t1.tree, (exprType)$t2.tree); + } + ; //assert_stmt: 'assert' test [',' test] assert_stmt : ASSERT t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])? - -> ^(ASSERT ^(Test $t1) ^(Msg $t2)?) + -> ^(ASSERT<Assert>[$ASSERT, (exprType)$t1.tree, (exprType)$t2.tree]) ; //compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 19:35:16
|
Revision: 4818 http://jython.svn.sourceforge.net/jython/?rev=4818&view=rev Author: fwierzbicki Date: 2008-06-28 12:35:08 -0700 (Sat, 28 Jun 2008) Log Message: ----------- one pass if/else Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 18:11:31 UTC (rev 4817) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 19:35:08 UTC (rev 4818) @@ -175,6 +175,7 @@ import org.python.antlr.ast.ExtSlice; import org.python.antlr.ast.FunctionDef; import org.python.antlr.ast.Global; +import org.python.antlr.ast.If; import org.python.antlr.ast.Import; import org.python.antlr.ast.ImportFrom; import org.python.antlr.ast.Index; @@ -199,6 +200,7 @@ import java.math.BigInteger; import java.util.Iterator; +import java.util.ListIterator; } @members { @@ -290,6 +292,24 @@ } return new exprType[0]; } + + private stmtType[] makeElses(List elseSuite, List elifs) { + stmtType[] o; + if (elseSuite != null) { + o = makeStmts(elseSuite); + } else { + o = new stmtType[0]; + } + if (elifs != null) { + ListIterator iter = elifs.listIterator(elifs.size()); + while (iter.hasPrevious()) { + If elif = (If)iter.previous(); + elif.orelse = o; + o = new stmtType[]{elif}; + } + } + return o; + } private stmtType[] makeStmts(List stmts) { if (stmts != null) { @@ -952,13 +972,13 @@ ; //if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] -if_stmt: IF test[expr_contextType.Load] COLON ifsuite=suite elif_clause* (ORELSE COLON elsesuite=suite)? - -> ^(IF test $ifsuite elif_clause* ^(ORELSE $elsesuite)?) +if_stmt: IF test[expr_contextType.Load] COLON ifsuite=suite elifs+=elif_clause* (ORELSE COLON elsesuite=suite)? + -> ^(IF<If>[$IF, (exprType)$test.tree, makeStmts($ifsuite.stmts), makeElses($elsesuite.stmts, $elifs)]) ; //not in CPython's Grammar file elif_clause : ELIF test[expr_contextType.Load] COLON suite - -> ^(ELIF test suite) + -> ^(ELIF<If>[$ELIF, (exprType)$test.tree, makeStmts($suite.stmts), new stmtType[0\]]) ; //while_stmt: 'while' test ':' suite ['else' ':' suite] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 19:56:19
|
Revision: 4819 http://jython.svn.sourceforge.net/jython/?rev=4819&view=rev Author: fwierzbicki Date: 2008-06-28 12:56:18 -0700 (Sat, 28 Jun 2008) Log Message: ----------- One pass while, start of one pass For. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 19:35:08 UTC (rev 4818) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 19:56:18 UTC (rev 4819) @@ -173,6 +173,7 @@ import org.python.antlr.ast.exprType; import org.python.antlr.ast.expr_contextType; import org.python.antlr.ast.ExtSlice; +import org.python.antlr.ast.For; import org.python.antlr.ast.FunctionDef; import org.python.antlr.ast.Global; import org.python.antlr.ast.If; @@ -194,6 +195,7 @@ import org.python.antlr.ast.Str; import org.python.antlr.ast.Subscript; import org.python.antlr.ast.Tuple; +import org.python.antlr.ast.While; import org.python.core.Py; import org.python.core.PyString; import org.python.core.PyUnicode; @@ -295,11 +297,7 @@ private stmtType[] makeElses(List elseSuite, List elifs) { stmtType[] o; - if (elseSuite != null) { - o = makeStmts(elseSuite); - } else { - o = new stmtType[0]; - } + o = makeStmts(elseSuite); if (elifs != null) { ListIterator iter = elifs.listIterator(elifs.size()); while (iter.hasPrevious()) { @@ -337,6 +335,18 @@ return current; } + private While makeWhile(Token t, exprType test, List body, List orelse) { + stmtType[] o = makeStmts(orelse); + stmtType[] b = makeStmts(body); + return new While(t, test, b, o); + } + + private For makeFor(Token t, exprType target, exprType iter, List body, List orelse) { + stmtType[] o = makeStmts(orelse); + stmtType[] b = makeStmts(body); + return new For(t, target, iter, b, o); + } + private FunctionDef makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements, List decorators) { argumentsType a; debug("Matched FunctionDef"); @@ -982,14 +992,24 @@ ; //while_stmt: 'while' test ':' suite ['else' ':' suite] -while_stmt : WHILE test[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? - -> ^(WHILE test ^(Body $s1) ^(ORELSE $s2)?) - ; +while_stmt returns [stmtType stype] +@after { + $while_stmt.tree = $stype; +} + : WHILE test[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { + $stype = makeWhile($WHILE, (exprType)$test.tree, $s1.stmts, $s2.stmts); + } + ; //for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] -for_stmt : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? - -> ^(FOR ^(Target exprlist) ^(IN testlist) ^(Body $s1) ^(ORELSE $s2)?) - ; +for_stmt returns [stmtType stype] +@after { + $for_stmt.tree = $stype; +} + : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { + $stype = makeFor($FOR, $exprlist.etype, (exprType)$testlist.tree, $s2.stmts, $s2.stmts); + } + ; //try_stmt: ('try' ':' suite // ((except_clause ':' suite)+ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-06-28 21:17:50
|
Revision: 4820 http://jython.svn.sourceforge.net/jython/?rev=4820&view=rev Author: fwierzbicki Date: 2008-06-28 14:17:46 -0700 (Sat, 28 Jun 2008) Log Message: ----------- Start on try/except/finally in one pass. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 19:56:18 UTC (rev 4819) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-28 21:17:46 UTC (rev 4820) @@ -168,6 +168,7 @@ import org.python.antlr.ast.Context; import org.python.antlr.ast.Continue; import org.python.antlr.ast.Delete; +import org.python.antlr.ast.excepthandlerType; import org.python.antlr.ast.Exec; import org.python.antlr.ast.Expr; import org.python.antlr.ast.exprType; @@ -194,8 +195,11 @@ import org.python.antlr.ast.stmtType; import org.python.antlr.ast.Str; import org.python.antlr.ast.Subscript; +import org.python.antlr.ast.TryExcept; +import org.python.antlr.ast.TryFinally; import org.python.antlr.ast.Tuple; import org.python.antlr.ast.While; +import org.python.antlr.ast.With; import org.python.core.Py; import org.python.core.PyString; import org.python.core.PyUnicode; @@ -346,7 +350,32 @@ stmtType[] b = makeStmts(body); return new For(t, target, iter, b, o); } + + private stmtType makeTryExcept(Token t, List body, List handlers, List orelse, List finBody) { + stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); + excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); + stmtType[] o; + if (orelse != null) { + o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); + } else { + o = new stmtType[0]; + } + stmtType te = new TryExcept(t, b, e, o); + if (finBody == null) { + return te; + } + stmtType[] f = (stmtType[])finBody.toArray(new stmtType[finBody.size()]); + stmtType[] mainBody = new stmtType[]{te}; + return new TryFinally(t, mainBody, f); + } + + private TryFinally makeTryFinally(Token t, List body, List finBody) { + stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); + stmtType[] f = (stmtType[])finBody.toArray(new stmtType[finBody.size()]); + return new TryFinally(t, b, f); + } + private FunctionDef makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements, List decorators) { argumentsType a; debug("Matched FunctionDef"); @@ -1016,28 +1045,40 @@ // ['else' ':' suite] // ['finally' ':' suite] | // 'finally' ':' suite)) -try_stmt : 'try' COLON trysuite=suite - ( (except_clause+ (ORELSE COLON elsesuite=suite)? (FINALLY COLON finalsuite=suite)? - -> ^(TryExcept 'try' ^(Body $trysuite) except_clause+ ^(ORELSE $elsesuite)? ^(FINALLY $finalsuite)?)) - | (FINALLY COLON finalsuite=suite - -> ^(TryFinally 'try' ^(Body $trysuite) ^(FINALLY $finalsuite))) - ) - ; +try_stmt returns [stmtType stype] +@after { + $try_stmt.tree = $stype; +} + : TRY COLON trysuite=suite + ( e+=except_clause+ (ORELSE COLON elsesuite=suite)? (FINALLY COLON finalsuite=suite)? { + $stype = makeTryExcept($TRY, $trysuite.stmts, $e, $elsesuite.stmts, $finalsuite.stmts); + } + | FINALLY COLON finalsuite=suite { + $stype = makeTryFinally($TRY, $trysuite.stmts, $finalsuite.stmts); + } + ) + ; //with_stmt: 'with' test [ with_var ] ':' suite -with_stmt: WITH test[expr_contextType.Load] (with_var)? COLON suite - -> ^(WITH test with_var? ^(Body suite)) - ; +with_stmt returns [stmtType stype] +@after { + $with_stmt.tree = $stype; +} + :WITH test[expr_contextType.Load] (with_var)? COLON suite { + $stype = new With($WITH, (exprType)$test.tree, $with_var.etype, makeStmts($suite.stmts)); + } + ; //with_var: ('as' | NAME) expr -with_var: (keyAS | NAME) expr[expr_contextType.Load] - ; +with_var returns [exprType etype] + : (keyAS | NAME) expr[expr_contextType.Load] { + $etype = (exprType)$expr.tree; + } + ; //except_clause: 'except' [test [',' test]] -except_clause : 'except' (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? COLON suite - //Note: passing the 'except' keyword on so we can pass the same offset - // as CPython. - -> ^(ExceptHandler 'except' ^(Type $t1)? ^(Value $t2)? ^(Body suite)) +except_clause : EXCEPT (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? COLON suite + -> ^(EXCEPT<excepthandlerType>[$EXCEPT, (exprType)$t1.tree, (exprType)$t2.tree, makeStmts($suite.stmts), $EXCEPT.getLine(), $EXCEPT.getCharPositionInLine()]) ; //suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT @@ -1452,6 +1493,8 @@ ASSERT : 'assert' ; FINALLY : 'finally' ; DELETE : 'del' ; +EXCEPT : 'except' ; +TRY : 'try' ; LPAREN : '(' {implicitLineJoiningLevel++;} ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |