| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Jython
Revision: 5210
Author: fwierzbicki
Date: 19 Aug 2008 17:43:06
Changes:Work to date on the one pass grammar. Not ready for others to look at really.
If you really want to try it note that you need to comment out parts of site.py
and os.py to get things to work. They *almost* parse and compile, but not
quite, and they are basic dependencies for Jython to run.
| ... | ...@@ -39,11 +39,11 @@ | |
| 39 | 39 | |
| 40 | 40 | try { |
| 41 | 41 | PythonParser.single_input_return r = parser.single_input(); |
| 42 | CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); | |
| 43 | nodes.setTokenStream(tokens); | |
| 44 | PythonWalker walker = new PythonWalker(nodes); | |
| 45 | walker.setErrorHandler(errorHandler); | |
| 46 | tree = walker.interactive(); | |
| 42 | //CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); | |
| 43 | //nodes.setTokenStream(tokens); | |
| 44 | //PythonWalker walker = new PythonWalker(nodes); | |
| 45 | //walker.setErrorHandler(errorHandler); | |
| 46 | //tree = (modType)walker.interactive(); | |
| 47 | 47 | } catch (RecognitionException e) { |
| 48 | 48 | //I am only throwing ParseExceptions, but "throws RecognitionException" still gets |
| 49 | 49 | //into the generated code. |
| ... | ...@@ -34,11 +34,11 @@ | |
| 34 | 34 | |
| 35 | 35 | try { |
| 36 | 36 | PythonParser.eval_input_return r = parser.eval_input(); |
| 37 | CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); | |
| 38 | nodes.setTokenStream(tokens); | |
| 39 | PythonWalker walker = new PythonWalker(nodes); | |
| 40 | walker.setErrorHandler(errorHandler); | |
| 41 | tree = walker.expression(); | |
| 37 | //CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); | |
| 38 | //nodes.setTokenStream(tokens); | |
| 39 | //PythonWalker walker = new PythonWalker(nodes); | |
| 40 | //walker.setErrorHandler(errorHandler); | |
| 41 | //tree = (modType)walker.expression(); | |
| 42 | 42 | } catch (RecognitionException e) { |
| 43 | 43 | //XXX: this can't happen. Need to strip the throws from antlr |
| 44 | 44 | // generated code. |
| ... | ...@@ -75,74 +75,26 @@ | |
| 75 | 75 | INDENT; |
| 76 | 76 | DEDENT; |
| 77 | 77 | |
| 78 | Module; | |
| 78 | PYNODE; | |
| 79 | 79 | Interactive; |
| 80 | 80 | Expression; |
| 81 | NameTok; | |
| 82 | 81 | Test; |
| 83 | Msg; | |
| 84 | Level; | |
| 85 | 82 | Body; |
| 86 | Bases; | |
| 87 | Arguments; | |
| 88 | Args; | |
| 89 | Arg; | |
| 90 | Keyword; | |
| 91 | StarArgs; | |
| 92 | KWArgs; | |
| 93 | Assign; | |
| 94 | AugAssign; | |
| 95 | Tuple; | |
| 96 | List; | |
| 97 | 83 | Dict; |
| 98 | 84 | IfExp; |
| 99 | TryExcept; | |
| 100 | TryFinally; | |
| 101 | ExceptHandler; | |
| 102 | StrTok; | |
| 103 | NumTok; | |
| 104 | IsNot; | |
| 105 | NotIn; | |
| 106 | Type; | |
| 107 | Inst; | |
| 108 | Tback; | |
| 109 | Globals; | |
| 110 | Locals; | |
| 111 | 85 | Ellipsis; |
| 112 | 86 | ListComp; |
| 113 | 87 | Repr; |
| 114 | Subscript; | |
| 115 | SubscriptList; | |
| 116 | Index; | |
| 117 | 88 | Target; |
| 118 | Value; | |
| 119 | Lower; | |
| 120 | Upper; | |
| 121 | Step; | |
| 122 | UAdd; | |
| 123 | USub; | |
| 124 | Invert; | |
| 125 | Alias; | |
| 126 | Asname; | |
| 127 | Decorators; | |
| 128 | 89 | GeneratorExp; |
| 129 | 90 | Ifs; |
| 130 | 91 | Elts; |
| 131 | Call; | |
| 132 | Dest; | |
| 133 | Values; | |
| 134 | Newline; | |
| 135 | ||
| 136 | FpList; | |
| 137 | StepOp; | |
| 138 | UpperOp; | |
| 139 | 92 | |
| 140 | 93 | GenFor; |
| 141 | 94 | GenIf; |
| 142 | 95 | ListFor; |
| 143 | 96 | ListIf; |
| 144 | Parens; | |
| 145 | Brackets; | |
| 97 | ||
| 146 | 98 | } |
| 147 | 99 | |
| 148 | 100 | @header { |
| ... | ...@@ -152,29 +104,68 @@ | |
| 152 | 104 | |
| 153 | 105 | import org.python.antlr.ParseException; |
| 154 | 106 | import org.python.antlr.PythonTree; |
| 107 | import org.python.antlr.ast.aliasType; | |
| 155 | 108 | import org.python.antlr.ast.argumentsType; |
| 109 | import org.python.antlr.ast.Assert; | |
| 110 | import org.python.antlr.ast.Assign; | |
| 156 | 111 | import org.python.antlr.ast.Attribute; |
| 112 | import org.python.antlr.ast.AugAssign; | |
| 113 | import org.python.antlr.ast.BinOp; | |
| 114 | import org.python.antlr.ast.BoolOp; | |
| 115 | import org.python.antlr.ast.boolopType; | |
| 157 | 116 | import org.python.antlr.ast.Break; |
| 117 | import org.python.antlr.ast.Call; | |
| 118 | import org.python.antlr.ast.ClassDef; | |
| 119 | import org.python.antlr.ast.cmpopType; | |
| 120 | import org.python.antlr.ast.Compare; | |
| 121 | import org.python.antlr.ast.comprehensionType; | |
| 158 | 122 | import org.python.antlr.ast.Context; |
| 159 | 123 | import org.python.antlr.ast.Continue; |
| 124 | import org.python.antlr.ast.Delete; | |
| 125 | import org.python.antlr.ast.excepthandlerType; | |
| 126 | import org.python.antlr.ast.Exec; | |
| 160 | 127 | import org.python.antlr.ast.Expr; |
| 161 | 128 | import org.python.antlr.ast.exprType; |
| 162 | 129 | import org.python.antlr.ast.expr_contextType; |
| 130 | import org.python.antlr.ast.ExtSlice; | |
| 131 | import org.python.antlr.ast.For; | |
| 163 | 132 | import org.python.antlr.ast.FunctionDef; |
| 133 | import org.python.antlr.ast.Global; | |
| 134 | import org.python.antlr.ast.If; | |
| 135 | import org.python.antlr.ast.Import; | |
| 136 | import org.python.antlr.ast.ImportFrom; | |
| 137 | import org.python.antlr.ast.Index; | |
| 138 | import org.python.antlr.ast.keywordType; | |
| 139 | import org.python.antlr.ast.Lambda; | |
| 164 | 140 | import org.python.antlr.ast.modType; |
| 165 | 141 | import org.python.antlr.ast.Module; |
| 166 | 142 | import org.python.antlr.ast.Name; |
| 167 | 143 | import org.python.antlr.ast.Num; |
| 168 | 144 | import org.python.antlr.ast.Pass; |
| 169 | 145 | import org.python.antlr.ast.Print; |
| 146 | import org.python.antlr.ast.Raise; | |
| 147 | import org.python.antlr.ast.operatorType; | |
| 170 | 148 | import org.python.antlr.ast.Return; |
| 149 | import org.python.antlr.ast.Slice; | |
| 150 | import org.python.antlr.ast.sliceType; | |
| 171 | 151 | import org.python.antlr.ast.stmtType; |
| 172 | 152 | import org.python.antlr.ast.Str; |
| 153 | import org.python.antlr.ast.Subscript; | |
| 154 | import org.python.antlr.ast.TryExcept; | |
| 155 | import org.python.antlr.ast.TryFinally; | |
| 156 | import org.python.antlr.ast.Tuple; | |
| 157 | import org.python.antlr.ast.unaryopType; | |
| 158 | import org.python.antlr.ast.UnaryOp; | |
| 159 | import org.python.antlr.ast.While; | |
| 160 | import org.python.antlr.ast.With; | |
| 161 | import org.python.antlr.ast.Yield; | |
| 173 | 162 | import org.python.core.Py; |
| 174 | 163 | import org.python.core.PyString; |
| 175 | 164 | import org.python.core.PyUnicode; |
| 176 | 165 | |
| 177 | 166 | import java.math.BigInteger; |
| 167 | import java.util.Iterator; | |
| 168 | import java.util.ListIterator; | |
| 178 | 169 | } |
| 179 | 170 | |
| 180 | 171 | @members { |
| ... | ...@@ -297,17 +288,21 @@ | |
| 297 | 288 | ; |
| 298 | 289 | |
| 299 | 290 | //file_input: (NEWLINE | stmt)* ENDMARKER |
| 300 | file_input : (NEWLINE | stmt)* EOF | |
| 301 | -> ^(Module stmt*) | |
| 302 | ; | |
| 291 | file_input | |
| 292 | : (NEWLINE | s+=stmt)+ -> ^(PYNODE<Module>[$file_input.start, actions.makeStmts($s)]) | |
| 293 | | -> ^(PYNODE<Module>[$file_input.start, new stmtType[0\]]) | |
| 294 | ; | |
| 303 | 295 | |
| 304 | 296 | //eval_input: testlist NEWLINE* ENDMARKER |
| 305 | 297 | eval_input : LEADING_WS? (NEWLINE)* testlist[expr_contextType.Load] (NEWLINE)* EOF -> ^(Expression testlist) |
| 306 | 298 | ; |
| 307 | 299 | |
| 308 | 300 | //not in CPython's Grammar file |
| 309 | dotted_attr | |
| 310 | : NAME (DOT^ attr)* | |
| 301 | dotted_attr returns [exprType etype] | |
| 302 | : n1=NAME | |
| 303 | ( (DOT n2+=dotted_attr)+ { $etype = actions.makeDottedAttr($n1, $n2); } | |
| 304 | | { $etype = new Name($NAME, $NAME.text, expr_contextType.Load); } | |
| 305 | ) | |
| 311 | 306 | ; |
| 312 | 307 | |
| 313 | 308 | //attr is here for Java compatibility. A Java foo.getIf() can be called from Jython as foo.if |
| ... | ...@@ -349,62 +344,94 @@ | |
| 349 | 344 | ; |
| 350 | 345 | |
| 351 | 346 | //decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 352 | decorator: AT dotted_attr | |
| 353 | ( (LPAREN arglist? RPAREN) -> ^(AT dotted_attr ^(Call ^(Args arglist)?)) | |
| 354 | | -> ^(AT dotted_attr) | |
| 355 | ) NEWLINE | |
| 356 | ; | |
| 347 | decorator returns [exprType etype] | |
| 348 | @after { | |
| 349 | $decorator.tree = $etype; | |
| 350 | } | |
| 351 | : AT dotted_attr | |
| 352 | //XXX: ignoring the arglist and Call generation right now. | |
| 353 | ( LPAREN (arglist | |
| 354 | {$etype = new Call($LPAREN, $dotted_attr.etype, actions.makeExprs($arglist.args), | |
| 355 | actions.makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs);} | |
| 356 | | {$etype = $dotted_attr.etype;} | |
| 357 | ) | |
| 358 | RPAREN | |
| 359 | // ^(Decorator dotted_attr ^(CallTok ^(Args arglist)?)) | |
| 360 | | { $etype = $dotted_attr.etype; } | |
| 361 | ) NEWLINE | |
| 362 | ; | |
| 357 | 363 | |
| 358 | 364 | //decorators: decorator+ |
| 359 | decorators: decorator+ | |
| 360 | ; | |
| 365 | decorators returns [List etypes] | |
| 366 | : d+=decorator+ {$etypes = $d;} | |
| 367 | ; | |
| 361 | 368 | |
| 362 | 369 | //funcdef: [decorators] 'def' NAME parameters ':' suite |
| 363 | 370 | funcdef : decorators? DEF NAME parameters COLON suite |
| 364 | -> ^(DEF NAME parameters ^(Body suite) ^(Decorators decorators?)) | |
| 371 | -> ^(DEF<FunctionDef>[$DEF, $NAME.text, $parameters.args, actions.makeStmts($suite.stmts), | |
| 372 | actions.makeExprs($decorators.etypes)]) | |
| 365 | 373 | ; |
| 366 | 374 | |
| 367 | 375 | //parameters: '(' [varargslist] ')' |
| 368 | parameters : LPAREN | |
| 369 | (varargslist -> ^(Arguments varargslist) | |
| 370 | | -> ^(Arguments) | |
| 371 | ) | |
| 372 | RPAREN | |
| 373 | ; | |
| 376 | parameters returns [argumentsType args] | |
| 377 | : LPAREN | |
| 378 | (varargslist {$args = $varargslist.args;} | |
| 379 | | {$args = new argumentsType($parameters.start, new exprType[0], null, null, new exprType[0]);} | |
| 380 | ) | |
| 381 | RPAREN | |
| 382 | ; | |
| 374 | 383 | |
| 375 | 384 | //not in CPython's Grammar file |
| 376 | defparameter : fpdef (ASSIGN test[expr_contextType.Load])? {debug("parsed defparameter");} | |
| 377 | ; | |
| 385 | defparameter[List defaults] returns [exprType etype] | |
| 386 | @after { | |
| 387 | $defparameter.tree = $etype; | |
| 388 | } | |
| 389 | : fpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? { | |
| 390 | $etype = (exprType)$fpdef.tree; | |
| 391 | if ($ASSIGN != null) { | |
| 392 | defaults.add($test.tree); | |
| 393 | } else if (!defaults.isEmpty()) { | |
| 394 | throw new ParseException( | |
| 395 | "non-default argument follows default argument", | |
| 396 | $fpdef.tree); | |
| 397 | } | |
| 398 | } | |
| 399 | ; | |
| 378 | 400 | |
| 379 | 401 | //varargslist: ((fpdef ['=' test] ',')* |
| 380 | 402 | // ('*' NAME [',' '**' NAME] | '**' NAME) | |
| 381 | 403 | // fpdef ['=' test] (',' fpdef ['=' test])* [',']) |
| 382 | varargslist : defparameter (options {greedy=true;}:COMMA defparameter)* | |
| 383 | (COMMA | |
| 384 | ( STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)? | |
| 385 | | DOUBLESTAR kwargs=NAME | |
| 386 | )? | |
| 387 | )? {debug("parsed varargslist");} | |
| 388 | -> ^(Args defparameter+) ^(StarArgs $starargs)? ^(KWArgs $kwargs)? | |
| 389 | | STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)?{debug("parsed varargslist STARARGS");} | |
| 390 | -> ^(StarArgs $starargs) ^(KWArgs $kwargs)? | |
| 391 | | DOUBLESTAR kwargs=NAME {debug("parsed varargslist KWS");} | |
| 392 | -> ^(KWArgs $kwargs) | |
| 393 | ; | |
| 404 | varargslist returns [argumentsType args] | |
| 405 | @init { | |
| 406 | List defaults = new ArrayList(); | |
| 407 | } | |
| 408 | : d+=defparameter[defaults] (options {greedy=true;}:COMMA d+=defparameter[defaults])* | |
| 409 | (COMMA | |
| 410 | ( STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)? | |
| 411 | | DOUBLESTAR kwargs=NAME | |
| 412 | )? | |
| 413 | )? | |
| 414 | {$args = actions.makeArgumentsType($varargslist.start, $d, $starargs, $kwargs, defaults);} | |
| 415 | | STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)?{debug("parsed varargslist STARARGS");} | |
| 416 | {$args = actions.makeArgumentsType($varargslist.start, $d, $starargs, $kwargs, defaults);} | |
| 417 | | DOUBLESTAR kwargs=NAME {debug("parsed varargslist KWS");} | |
| 418 | {$args = actions.makeArgumentsType($varargslist.start, $d, null, $kwargs, defaults);} | |
| 419 | ; | |
| 394 | 420 | |
| 395 | 421 | //fpdef: NAME | '(' fplist ')' |
| 396 | fpdef : NAME {debug("parsed fpdef NAME");} | |
| 397 | | (LPAREN fpdef COMMA) => LPAREN fplist RPAREN | |
| 398 | -> ^(FpList fplist) | |
| 399 | | LPAREN fplist RPAREN | |
| 422 | fpdef[expr_contextType ctype] : NAME | |
| 423 | -> ^(PYNODE<Name>[$NAME, $NAME.text, ctype]) | |
| 424 | | (LPAREN fpdef[expr_contextType.Load] COMMA) => LPAREN fplist RPAREN | |
| 400 | 425 | -> fplist |
| 426 | | LPAREN fplist RPAREN | |
| 427 | -> ^(LPAREN<Tuple>[$fplist.start, actions.makeExprs($fplist.etypes), expr_contextType.Store]) | |
| 401 | 428 | ; |
| 402 | 429 | |
| 403 | 430 | //fplist: fpdef (',' fpdef)* [','] |
| 404 | fplist : fpdef (options {greedy=true;}:COMMA fpdef)* (COMMA)? | |
| 405 | {debug("parsed fplist");} | |
| 406 | -> fpdef+ | |
| 407 | ; | |
| 431 | fplist returns [List etypes] | |
| 432 | : f+=fpdef[expr_contextType.Store] (options {greedy=true;}:COMMA f+=fpdef[expr_contextType.Store])* (COMMA)? | |
| 433 | {$etypes = $f;} | |
| 434 | ; | |
| 408 | 435 | |
| 409 | 436 | //stmt: simple_stmt | compound_stmt |
| 410 | 437 | stmt : simple_stmt |
| ... | ...@@ -430,111 +457,103 @@ | |
| 430 | 457 | |
| 431 | 458 | //expr_stmt: testlist (augassign (yield_expr|testlist) | |
| 432 | 459 | // ('=' (yield_expr|testlist))*) |
| 433 | expr_stmt : lhs=testlist[expr_contextType.Store] | |
| 434 | ( (augassign yield_expr -> ^(augassign $lhs yield_expr)) | |
| 435 | | (augassign rhs=testlist[expr_contextType.Load] -> ^(augassign $lhs $rhs)) | |
| 436 | | ((assigns) {debug("matched assigns");} -> ^(Assign ^(Target $lhs) assigns)) | |
| 437 | | -> $lhs | |
| 438 | ) | |
| 439 | ; | |
| 440 | ||
| 441 | //not in CPython's Grammar file | |
| 442 | assigns | |
| 443 | @after { | |
| 444 | PythonTree pt = ((PythonTree)$assigns.tree); | |
| 445 | int children = pt.getChildCount(); | |
| 446 | PythonTree child; | |
| 447 | if (children == 1) { | |
| 448 | child = pt; | |
| 449 | pt.token = new CommonToken(Value, "Value"); | |
| 450 | } else { | |
| 451 | child = (PythonTree)pt.getChild(children - 1); | |
| 452 | child.token = new CommonToken(Value, "Value"); | |
| 453 | } | |
| 454 | child.token = new CommonToken(Value, "Value"); | |
| 455 | PythonTree targ = (PythonTree)child.getChild(0); | |
| 456 | if (targ instanceof Context) { | |
| 457 | ((Context)targ).setContext(expr_contextType.Load); | |
| 458 | } | |
| 460 | expr_stmt | |
| 461 | @init { | |
| 462 | stmtType stype = null; | |
| 463 | } | |
| 464 | @after { | |
| 465 | if (stype != null) { | |
| 466 | $expr_stmt.tree = stype; | |
| 467 | } | |
| 459 | 468 | } |
| 460 | : assign_testlist+ | |
| 461 | | assign_yield+ | |
| 462 | ; | |
| 463 | ||
| 464 | //not in CPython's Grammar file | |
| 465 | assign_testlist | |
| 466 | : ASSIGN testlist[expr_contextType.Store] -> ^(Target testlist) | |
| 467 | ; | |
| 468 | 469 | |
| 469 | //not in CPython's Grammar file | |
| 470 | assign_yield | |
| 471 | : ASSIGN yield_expr -> ^(Value yield_expr) | |
| 470 | : | |
| 471 | ((testlist[expr_contextType.Load] augassign) => lhs=testlist[expr_contextType.AugStore] | |
| 472 | ( (aay=augassign y1=yield_expr {stype = new AugAssign($lhs.tree, (exprType)$lhs.tree, $aay.op, (exprType)$y1.tree);}) | |
| 473 | | (aat=augassign rhs=testlist[expr_contextType.Load] {stype = new AugAssign($lhs.tree, (exprType)$lhs.tree, $aat.op, (exprType)$rhs.tree);}) | |
| 474 | ) | |
| 475 | |(testlist[expr_contextType.Load] ASSIGN) => lhs=testlist[expr_contextType.Store] | |
| 476 | ( | |
| 477 | | ((at=ASSIGN t+=testlist[expr_contextType.Store])+ -> ^(PYNODE<Assign>[$at, actions.makeAssignTargets((exprType)$lhs.tree, $t), actions.makeAssignValue($t)])) | |
| 478 | | ((ay=ASSIGN y2+=yield_expr)+ -> ^(PYNODE<Assign>[$ay, actions.makeAssignTargets((exprType)$lhs.tree, $y2), actions.makeAssignValue($y2)])) | |
| 479 | ) | |
| 480 | | lhs=testlist[expr_contextType.Load] -> PYNODE<Expr>[$lhs.start, (exprType)$lhs.tree] | |
| 481 | ) | |
| 472 | 482 | ; |
| 473 | 483 | |
| 474 | 484 | //augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 475 | 485 | // '<<=' | '>>=' | '**=' | '//=') |
| 476 | augassign : PLUSEQUAL | |
| 477 | | MINUSEQUAL | |
| 478 | | STAREQUAL | |
| 479 | | SLASHEQUAL | |
| 480 | | PERCENTEQUAL | |
| 481 | | AMPEREQUAL | |
| 482 | | VBAREQUAL | |
| 483 | | CIRCUMFLEXEQUAL | |
| 484 | | LEFTSHIFTEQUAL | |
| 485 | | RIGHTSHIFTEQUAL | |
| 486 | | DOUBLESTAREQUAL | |
| 487 | | DOUBLESLASHEQUAL | |
| 488 | ; | |
| 486 | augassign returns [operatorType op] | |
| 487 | : PLUSEQUAL {$op = operatorType.Add;} | |
| 488 | | MINUSEQUAL {$op = operatorType.Sub;} | |
| 489 | | STAREQUAL {$op = operatorType.Mult;} | |
| 490 | | SLASHEQUAL {$op = operatorType.Div;} | |
| 491 | | PERCENTEQUAL {$op = operatorType.Mod;} | |
| 492 | | AMPEREQUAL {$op = operatorType.BitAnd;} | |
| 493 | | VBAREQUAL {$op = operatorType.BitOr;} | |
| 494 | | CIRCUMFLEXEQUAL {$op = operatorType.BitXor;} | |
| 495 | | LEFTSHIFTEQUAL {$op = operatorType.LShift;} | |
| 496 | | RIGHTSHIFTEQUAL {$op = operatorType.RShift;} | |
| 497 | | DOUBLESTAREQUAL {$op = operatorType.Pow;} | |
| 498 | | DOUBLESLASHEQUAL {$op = operatorType.FloorDiv;} | |
| 499 | ; | |
| 489 | 500 | |
| 490 | 501 | //print_stmt: 'print' ( [ test (',' test)* [','] ] | |
| 491 | 502 | // '>>' test [ (',' test)+ [','] ] ) |
| 492 | print_stmt : PRINT | |
| 493 | ( t1=printlist -> {$t1.newline}? ^(PRINT ^(Values $t1) ^(Newline)) | |
| 494 | -> ^(PRINT ^(Values $t1)) | |
| 495 | | RIGHTSHIFT t2=printlist2 -> {$t2.newline}? ^(PRINT ^(Dest RIGHTSHIFT) ^(Values $t2) ^(Newline)) | |
| 496 | -> ^(PRINT ^(Dest RIGHTSHIFT) ^(Values $t2)) | |
| 497 | | -> ^(PRINT ^(Newline)) | |
| 503 | print_stmt : PRINT | |
| 504 | ( t1=printlist | |
| 505 | -> ^(PRINT<Print>[$PRINT, null, actions.makeExprs($t1.elts), $t1.newline]) | |
| 506 | | RIGHTSHIFT t2=printlist2 | |
| 507 | -> ^(PRINT<Print>[$PRINT, (exprType)$t2.elts.get(0), actions.makeExprs($t2.elts, 1), $t2.newline]) | |
| 508 | | | |
| 509 | -> ^(PRINT<Print>[$PRINT, null, new exprType[0\], false]) | |
| 498 | 510 | ) |
| 499 | 511 | ; |
| 500 | 512 | |
| 501 | 513 | //not in CPython's Grammar file |
| 502 | printlist returns [boolean newline] | |
| 503 | : (test[expr_contextType.Load] COMMA) => test[expr_contextType.Load] (options {k=2;}: COMMA test[expr_contextType.Load])* (trailcomma=COMMA)? | |
| 504 | { if ($trailcomma == null) { | |
| 505 | $newline = true; | |
| 514 | printlist returns [boolean newline, List elts] | |
| 515 | : (test[expr_contextType.Load] COMMA) => | |
| 516 | t+=test[expr_contextType.Load] (options {k=2;}: COMMA t+=test[expr_contextType.Load])* (trailcomma=COMMA)? | |
| 517 | { $elts=$t; | |
| 518 | if ($trailcomma == null) { | |
| 519 | $newline = true; | |
| 506 | 520 | } else { |
| 507 | $newline = false; | |
| 521 | $newline = false; | |
| 508 | 522 | } |
| 509 | 523 | } |
| 510 | -> ^(Elts test+) | |
| 511 | | test[expr_contextType.Load] {$newline = true;} | |
| 512 | -> ^(Elts test) | |
| 524 | | t+=test[expr_contextType.Load] { | |
| 525 | $elts=$t; | |
| 526 | $newline = true; | |
| 527 | } | |
| 513 | 528 | ; |
| 514 | 529 | |
| 530 | //XXX: would be nice if printlist and printlist2 could be merged. | |
| 515 | 531 | //not in CPython's Grammar file |
| 516 | printlist2 returns [boolean newline] | |
| 517 | : (test[expr_contextType.Load] COMMA test[expr_contextType.Load]) => test[expr_contextType.Load] (options {k=2;}: COMMA test[expr_contextType.Load])* (trailcomma=COMMA)? | |
| 518 | { if ($trailcomma == null) { | |
| 519 | $newline = true; | |
| 532 | printlist2 returns [boolean newline, List elts] | |
| 533 | : (test[expr_contextType.Load] COMMA test[expr_contextType.Load]) => | |
| 534 | t+=test[expr_contextType.Load] (options {k=2;}: COMMA t+=test[expr_contextType.Load])* (trailcomma=COMMA)? | |
| 535 | { $elts=$t; | |
| 536 | if ($trailcomma == null) { | |
| 537 | $newline = true; | |
| 520 | 538 | } else { |
| 521 | $newline = false; | |
| 539 | $newline = false; | |
| 522 | 540 | } |
| 523 | 541 | } |
| 524 | -> ^(Elts test+) | |
| 525 | | test[expr_contextType.Load] {$newline = true;} | |
| 526 | -> ^(Elts test) | |
| 542 | | t+=test[expr_contextType.Load] { | |
| 543 | $elts=$t; | |
| 544 | $newline = true; | |
| 545 | } | |
| 527 | 546 | ; |
| 528 | 547 | |
| 529 | 548 | |
| 530 | 549 | //del_stmt: 'del' exprlist |
| 531 | 550 | del_stmt : DELETE exprlist2 |
| 532 | -> ^(DELETE exprlist2) | |
| 551 | -> ^(DELETE<Delete>[$DELETE, actions.makeExprs($exprlist2.etypes)]) | |
| 533 | 552 | ; |
| 534 | 553 | |
| 535 | 554 | //pass_stmt: 'pass' |
| 536 | pass_stmt : PASS | |
| 537 | -> ^(PASS) | |
| 555 | pass_stmt : PASS | |
| 556 | -> ^(PASS<Pass>[$PASS]) | |
| 538 | 557 | ; |
| 539 | 558 | |
| 540 | 559 | //flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt |
| ... | ...@@ -556,17 +575,19 @@ | |
| 556 | 575 | ; |
| 557 | 576 | |
| 558 | 577 | //return_stmt: 'return' [testlist] |
| 559 | return_stmt : RETURN (testlist[expr_contextType.Load])? | |
| 560 | -> ^(RETURN ^(Value testlist)?) | |
| 578 | return_stmt : RETURN | |
| 579 | (testlist[expr_contextType.Load] -> ^(RETURN<Return>[$RETURN, (exprType)$testlist.tree]) | |
| 580 | | -> ^(RETURN<Return>[$RETURN, null]) | |
| 581 | ) | |
| 561 | 582 | ; |
| 562 | 583 | |
| 563 | 584 | //yield_stmt: yield_expr |
| 564 | yield_stmt : yield_expr | |
| 585 | yield_stmt : yield_expr -> ^(PYNODE<Expr>[$yield_expr.start, (exprType)$yield_expr.tree]) | |
| 565 | 586 | ; |
| 566 | 587 | |
| 567 | 588 | //raise_stmt: 'raise' [test [',' test [',' test]]] |
| 568 | 589 | raise_stmt: RAISE (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load] (COMMA t3=test[expr_contextType.Load])?)?)? |
| 569 | -> ^(RAISE ^(Type $t1)? ^(Inst $t2)? ^(Tback $t3)?) | |
| 590 | -> ^(RAISE<Raise>[$RAISE, (exprType)$t1.tree, (exprType)$t2.tree, (exprType)$t3.tree]) | |
| 570 | 591 | ; |
| 571 | 592 | |
| 572 | 593 | //import_stmt: import_name | import_from |
| ... | ...@@ -576,56 +597,82 @@ | |
| 576 | 597 | |
| 577 | 598 | //import_name: 'import' dotted_as_names |
| 578 | 599 | import_name : IMPORT dotted_as_names |
| 579 | -> ^(IMPORT dotted_as_names) | |
| 600 | -> ^(IMPORT<Import>[$IMPORT, $dotted_as_names.atypes]) | |
| 580 | 601 | ; |
| 581 | 602 | |
| 582 | 603 | //import_from: ('from' ('.'* dotted_name | '.'+) |
| 583 | 604 | // 'import' ('*' | '(' import_as_names ')' | import_as_names)) |
| 584 | import_from: FROM (DOT* dotted_name | DOT+) IMPORT | |
| 605 | import_from: FROM (d+=DOT* dotted_name | d+=DOT+) IMPORT | |
| 585 | 606 | (STAR |
| 586 | -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT STAR)) | |
| 587 | | import_as_names | |
| 588 | -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT import_as_names)) | |
| 589 | | LPAREN import_as_names COMMA? RPAREN | |
| 590 | -> ^(FROM ^(Level DOT*)? ^(Value dotted_name)? ^(IMPORT import_as_names)) | |
| 607 | -> ^(FROM<ImportFrom>[$FROM, actions.makeFromText($d, $dotted_name.text), actions.makeStarAlias($STAR), actions.makeLevel($d)]) | |
| 608 | | i1=import_as_names | |
| 609 | -> ^(FROM<ImportFrom>[$FROM, actions.makeFromText($d, $dotted_name.text), actions.makeAliases($i1.atypes), actions.makeLevel($d)]) | |
| 610 | | LPAREN i2=import_as_names COMMA? RPAREN | |
| 611 | -> ^(FROM<ImportFrom>[$FROM, actions.makeFromText($d, $dotted_name.text), actions.makeAliases($i2.atypes), actions.makeLevel($d)]) | |
| 591 | 612 | ) |
| 592 | 613 | ; |
| 593 | 614 | |
| 594 | 615 | //import_as_names: import_as_name (',' import_as_name)* [','] |
| 595 | import_as_names : import_as_name (COMMA! import_as_name)* | |
| 596 | ; | |
| 616 | import_as_names returns [aliasType[\] atypes] | |
| 617 | : n+=import_as_name (COMMA! n+=import_as_name)* { | |
| 618 | $atypes = (aliasType[])$n.toArray(new aliasType[$n.size()]); | |
| 619 | } | |
| 620 | ; | |
| 597 | 621 | |
| 598 | 622 | //import_as_name: NAME [('as' | NAME) NAME] |
| 599 | import_as_name : name=NAME (AS asname=NAME)? | |
| 600 | -> ^(Alias $name ^(Asname $asname)?) | |
| 601 | ; | |
| 623 | import_as_name returns [aliasType atype] | |
| 624 | @after { | |
| 625 | $import_as_name.tree = $atype; | |
| 626 | } | |
| 627 | : name=NAME (AS asname=NAME)? { | |
| 628 | $atype = new aliasType($name, $name.text, $asname.text); | |
| 629 | } | |
| 630 | ; | |
| 602 | 631 | |
| 603 | 632 | //XXX: when does CPython Grammar match "dotted_name NAME NAME"? |
| 604 | 633 | //dotted_as_name: dotted_name [('as' | NAME) NAME] |
| 605 | dotted_as_name : dotted_name (AS asname=NAME)? | |
| 606 | -> ^(Alias dotted_name ^(Asname NAME)?) | |
| 607 | ; | |
| 634 | dotted_as_name returns [aliasType atype] | |
| 635 | @after { | |
| 636 | $dotted_as_name.tree = $atype; | |
| 637 | } | |
| 638 | ||
| 639 | : dotted_name (AS NAME)? { | |
| 640 | $atype = new aliasType($NAME, $dotted_name.text, $NAME.text); | |
| 641 | } | |
| 642 | ; | |
| 608 | 643 | |
| 609 | 644 | //dotted_as_names: dotted_as_name (',' dotted_as_name)* |
| 610 | dotted_as_names : dotted_as_name (COMMA! dotted_as_name)* | |
| 611 | ; | |
| 645 | dotted_as_names returns [aliasType[\] atypes] | |
| 646 | : d+=dotted_as_name (COMMA! d+=dotted_as_name)* { | |
| 647 | $atypes = (aliasType[])$d.toArray(new aliasType[$d.size()]); | |
| 648 | } | |
| 649 | ; | |
| 650 | ||
| 612 | 651 | //dotted_name: NAME ('.' NAME)* |
| 613 | 652 | dotted_name : NAME (DOT attr)* |
| 614 | 653 | ; |
| 615 | 654 | |
| 616 | 655 | //global_stmt: 'global' NAME (',' NAME)* |
| 617 | global_stmt : GLOBAL NAME (COMMA NAME)* | |
| 618 | -> ^(GLOBAL NAME+) | |
| 656 | global_stmt : GLOBAL n+=NAME (COMMA n+=NAME)* | |
| 657 | -> ^(GLOBAL<Global>[$GLOBAL, actions.makeNames($n)]) | |
| 619 | 658 | ; |
| 620 | 659 | |
| 621 | 660 | //exec_stmt: 'exec' expr ['in' test [',' test]] |
| 622 | exec_stmt : EXEC expr[expr_contextType.Load] (IN t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? | |
| 623 | -> ^(EXEC expr ^(Globals $t1)? ^(Locals $t2)?) | |
| 624 | ; | |
| 661 | exec_stmt | |
| 662 | @init { | |
| 663 | stmtType stype = null; | |
| 664 | } | |
| 665 | @after { | |
| 666 | $exec_stmt.tree = stype; | |
| 667 | } | |
| 668 | : EXEC expr[expr_contextType.Load] (IN t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? { | |
| 669 | stype = new Exec($expr.start, (exprType)$expr.tree, (exprType)$t1.tree, (exprType)$t2.tree); | |
| 670 | } | |
| 671 | ; | |
| 625 | 672 | |
| 626 | 673 | //assert_stmt: 'assert' test [',' test] |
| 627 | 674 | assert_stmt : ASSERT t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])? |
| 628 | -> ^(ASSERT ^(Test $t1) ^(Msg $t2)?) | |
| 675 | -> ^(ASSERT<Assert>[$ASSERT, (exprType)$t1.tree, (exprType)$t2.tree]) | |
| 629 | 676 | ; |
| 630 | 677 | |
| 631 | 678 | //compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| ... | ...@@ -639,62 +686,142 @@ | |
| 639 | 686 | ; |
| 640 | 687 | |
| 641 | 688 | //if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 642 | if_stmt: IF test[expr_contextType.Load] COLON ifsuite=suite elif_clause* (ORELSE COLON elsesuite=suite)? | |
| 643 | -> ^(IF test $ifsuite elif_clause* ^(ORELSE $elsesuite)?) | |
| 689 | if_stmt: IF test[expr_contextType.Load] COLON ifsuite=suite elifs+=elif_clause* (ORELSE COLON elsesuite=suite)? | |
| 690 | -> ^(IF<If>[$IF, (exprType)$test.tree, actions.makeStmts($ifsuite.stmts), actions.makeElses($elsesuite.stmts, $elifs)]) | |
| 644 | 691 | ; |
| 645 | 692 | |
| 646 | 693 | //not in CPython's Grammar file |
| 647 | 694 | elif_clause : ELIF test[expr_contextType.Load] COLON suite |
| 648 | -> ^(ELIF test suite) | |
| 695 | -> ^(ELIF<If>[$ELIF, (exprType)$test.tree, actions.makeStmts($suite.stmts), new stmtType[0\]]) | |
| 649 | 696 | ; |
| 650 | 697 | |
| 651 | 698 | //while_stmt: 'while' test ':' suite ['else' ':' suite] |
| 652 | while_stmt : WHILE test[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? | |
| 653 | -> ^(WHILE test ^(Body $s1) ^(ORELSE $s2)?) | |
| 654 | ; | |
| 699 | while_stmt | |
| 700 | @init { | |
| 701 | stmtType stype = null; | |
| 702 | } | |
| 703 | @after { | |
| 704 | $while_stmt.tree = stype; | |
| 705 | } | |
| 706 | : WHILE test[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { | |
| 707 | stype = actions.makeWhile($WHILE, (exprType)$test.tree, $s1.stmts, $s2.stmts); | |
| 708 | } | |
| 709 | ; | |
| 655 | 710 | |
| 656 | 711 | //for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
| 657 | for_stmt : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? | |
| 658 | -> ^(FOR ^(Target exprlist) ^(IN testlist) ^(Body $s1) ^(ORELSE $s2)?) | |
| 659 | ; | |
| 712 | for_stmt | |
| 713 | @init { | |
| 714 | stmtType stype = null; | |
| 715 | } | |
| 716 | @after { | |
| 717 | $for_stmt.tree = stype; | |
| 718 | } | |
| 719 | : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { | |
| 720 | stype = actions.makeFor($FOR, $exprlist.etype, (exprType)$testlist.tree, $s1.stmts, $s2.stmts); | |
| 721 | } | |
| 722 | ; | |
| 660 | 723 | |
| 661 | 724 | //try_stmt: ('try' ':' suite |
| 662 | 725 | // ((except_clause ':' suite)+ |
| 663 | 726 | // ['else' ':' suite] |
| 664 | 727 | // ['finally' ':' suite] | |
| 665 | 728 | // 'finally' ':' suite)) |
| 666 | try_stmt : TRY COLON trysuite=suite | |
| 667 | ( (except_clause+ (ORELSE COLON elsesuite=suite)? (FINALLY COLON finalsuite=suite)? | |
| 668 | -> ^(TryExcept[$TRY] ^(Body $trysuite) except_clause+ ^(ORELSE $elsesuite)? ^(FINALLY $finalsuite)?)) | |
| 669 | | (FINALLY COLON finalsuite=suite | |
| 670 | -> ^(TryFinally[$TRY] ^(Body $trysuite) ^(FINALLY $finalsuite))) | |
| 671 | ) | |
| 672 | ; | |
| 729 | try_stmt | |
| 730 | @init { | |
| 731 | stmtType stype = null; | |
| 732 | } | |
| 733 | @after { | |
| 734 | $try_stmt.tree = stype; | |
| 735 | } | |
| 736 | : TRY COLON trysuite=suite | |
| 737 | ( e+=except_clause+ (ORELSE COLON elsesuite=suite)? (FINALLY COLON finalsuite=suite)? { | |
| 738 | stype = actions.makeTryExcept($TRY, $trysuite.stmts, $e, $elsesuite.stmts, $finalsuite.stmts); | |
| 739 | } | |
| 740 | | FINALLY COLON finalsuite=suite { | |
| 741 | stype = actions.makeTryFinally($TRY, $trysuite.stmts, $finalsuite.stmts); | |
| 742 | } | |
| 743 | ) | |
| 744 | ; | |
| 673 | 745 | |
| 674 | 746 | //with_stmt: 'with' test [ with_var ] ':' suite |
| 675 | with_stmt: WITH test[expr_contextType.Load] (with_var)? COLON suite | |
| 676 | -> ^(WITH test with_var? ^(Body suite)) | |
| 677 | ; | |
| 747 | with_stmt | |
| 748 | @init { | |
| 749 | stmtType stype = null; | |
| 750 | } | |
| 751 | @after { | |
| 752 | $with_stmt.tree = stype; | |
| 753 | } | |
| 754 | :WITH test[expr_contextType.Load] (with_var)? COLON suite { | |
| 755 | stype = new With($WITH, (exprType)$test.tree, $with_var.etype, actions.makeStmts($suite.stmts)); | |
| 756 | } | |
| 757 | ; | |
| 678 | 758 | |
| 679 | 759 | //with_var: ('as' | NAME) expr |
| 680 | with_var: (AS | NAME) expr[expr_contextType.Load] | |
| 681 | ; | |
| 760 | with_var returns [exprType etype] | |
| 761 | : (AS | NAME) expr[expr_contextType.Load] { | |
| 762 | $etype = (exprType)$expr.tree; | |
| 763 | } | |
| 764 | ; | |
| 682 | 765 | |
| 683 | 766 | //except_clause: 'except' [test [',' test]] |
| 684 | 767 | except_clause : EXCEPT (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? COLON suite |
| 685 | //Note: passing the 'except' keyword on so we can pass the same offset | |
| 686 | // as CPython. | |
| 687 | -> ^(EXCEPT ^(Type $t1)? ^(Value $t2)? ^(Body suite)) | |
| 768 | -> ^(EXCEPT<excepthandlerType>[$EXCEPT, (exprType)$t1.tree, (exprType)$t2.tree, actions.makeStmts($suite.stmts), $EXCEPT.getLine(), $EXCEPT.getCharPositionInLine()]) | |
| 688 | 769 | ; |
| 689 | 770 | |
| 690 | 771 | //suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
| 691 | suite | |
| 692 | : simple_stmt | |
| 693 | | NEWLINE! INDENT (stmt)+ DEDENT | |
| 772 | suite returns [List stmts] | |
| 773 | : ss+=simple_stmt {$stmts = $ss;} | |
| 774 | | NEWLINE! INDENT (s+=stmt)+ DEDENT {$stmts = $s;} | |
| 694 | 775 | ; |
| 695 | 776 | |
| 696 | 777 | //test: or_test ['if' or_test 'else' test] | lambdef |
| 697 | 778 | test[expr_contextType ctype] |
| 779 | @after { | |
| 780 | if ($test.tree instanceof BoolOp) { | |
| 781 | BoolOp b = (BoolOp)$test.tree; | |
| 782 | List values = new ArrayList(); | |
| 783 | ||
| 784 | exprType left = (exprType)b.getChild(0); | |
| 785 | exprType right = (exprType)b.getChild(1); | |
| 786 | ||
| 787 | exprType[] e; | |
| 788 | if (left.getType() == b.getType() && right.getType() == b.getType()) { | |
| 789 | BoolOp leftB = (BoolOp)left; | |
| 790 | BoolOp rightB = (BoolOp)right; | |
| 791 | int lenL = leftB.values.length; | |
| 792 | int lenR = rightB.values.length; | |
| 793 | e = new exprType[lenL + lenR]; | |
| 794 | System.arraycopy(leftB.values, 0, e, 0, lenL - 1); | |
| 795 | System.arraycopy(rightB.values, 0, e, lenL - 1, lenL + lenR); | |
| 796 | } else if (left.getType() == b.getType()) { | |
| 797 | BoolOp leftB = (BoolOp)left; | |
| 798 | e = new exprType[leftB.values.length + 1]; | |
| 799 | System.arraycopy(leftB.values, 0, e, 0, leftB.values.length); | |
| 800 | e[e.length - 1] = right; | |
| 801 | } else if (right.getType() == b.getType()) { | |
| 802 | BoolOp rightB = (BoolOp)right; | |
| 803 | e = new exprType[rightB.values.length + 1]; | |
| 804 | System.arraycopy(rightB.values, 0, e, 0, rightB.values.length); | |
| 805 | e[e.length - 1] = left; | |
| 806 | } else { | |
| 807 | e = new exprType[2]; | |
| 808 | e[0] = left; | |
| 809 | e[1] = right; | |
| 810 | } | |
| 811 | b.values = e; | |
| 812 | switch (b.getType()) { | |
| 813 | case AND: | |
| 814 | b.op = boolopType.And; | |
| 815 | break; | |
| 816 | case OR: | |
| 817 | b.op = boolopType.Or; | |
| 818 | break; | |
| 819 | default: | |
| 820 | b.op = boolopType.UNDEFINED; | |
| 821 | } | |
| 822 | } | |
| 823 | } | |
| 824 | ||