| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Jython
Revision: 5264
Author: fwierzbicki
Date: 29 Aug 2008 09:47:16
Changes:Merged revisions 5210,5216-5220,5223-5226,5228,5230,5236-5261 via svnmerge from
https://jython.svn.sourceforge.net/svnroot/jython/branches/nowalker
........
r5210 | fwierzbicki | 2008-08-19 17:43:06 -0400 (Tue, 19 Aug 2008) | 5 lines
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.
........
r5216 | fwierzbicki | 2008-08-20 08:10:26 -0400 (Wed, 20 Aug 2008) | 2 lines
Much better BoolOp.
........
r5217 | fwierzbicki | 2008-08-20 10:06:30 -0400 (Wed, 20 Aug 2008) | 2 lines
Fix "del".
........
r5218 | fwierzbicki | 2008-08-20 10:39:21 -0400 (Wed, 20 Aug 2008) | 2 lines
Dict support.
........
r5219 | fwierzbicki | 2008-08-20 11:23:14 -0400 (Wed, 20 Aug 2008) | 2 lines
Conditional Expression support.
........
r5220 | fwierzbicki | 2008-08-20 11:37:02 -0400 (Wed, 20 Aug 2008) | 2 lines
repr support.
........
r5223 | fwierzbicki | 2008-08-20 15:57:39 -0400 (Wed, 20 Aug 2008) | 2 lines
Start of list comprehensions and generator expressions.
........
r5224 | fwierzbicki | 2008-08-20 15:58:17 -0400 (Wed, 20 Aug 2008) | 2 lines
oops revert build.xml
........
r5225 | fwierzbicki | 2008-08-20 16:57:09 -0400 (Wed, 20 Aug 2008) | 2 lines
re-remove PythonWalker.g from build
........
r5226 | fwierzbicki | 2008-08-20 16:59:44 -0400 (Wed, 20 Aug 2008) | 2 lines
gen expressions and list comps now actually work...
........
r5228 | fwierzbicki | 2008-08-20 22:58:24 -0400 (Wed, 20 Aug 2008) | 2 lines
debugging of some expr_contextType values.
........
r5230 | fwierzbicki | 2008-08-20 23:47:32 -0400 (Wed, 20 Aug 2008) | 2 lines
bugfix on exception and on assign value expr_contextType.
........
r5236 | fwierzbicki | 2008-08-22 19:03:45 -0400 (Fri, 22 Aug 2008) | 2 lines
Bugfix for expr_contextType on assign values.
........
r5237 | fwierzbicki | 2008-08-22 22:33:48 -0400 (Fri, 22 Aug 2008) | 2 lines
Add expr types to BaseTree children.
........
r5238 | fwierzbicki | 2008-08-23 15:19:16 -0400 (Sat, 23 Aug 2008) | 2 lines
BinOp now works in one pass.
........
r5239 | fwierzbicki | 2008-08-23 15:43:19 -0400 (Sat, 23 Aug 2008) | 2 lines
Fine tuning List and Assign col position.
........
r5240 | fwierzbicki | 2008-08-23 19:03:59 -0400 (Sat, 23 Aug 2008) | 5 lines
Make PythonTree's toStringTree more easily comparable with ast/astview.py
output. This will help with the walkerless Python.g, as it doesn't quite
parse ast/astview.py yet.
........
r5241 | fwierzbicki | 2008-08-23 22:04:43 -0400 (Sat, 23 Aug 2008) | 2 lines
Fix lambda without args.
........
r5242 | fwierzbicki | 2008-08-24 05:38:22 -0400 (Sun, 24 Aug 2008) | 2 lines
check for PythonTree in makeStmts.
........
r5243 | fwierzbicki | 2008-08-25 10:21:29 -0400 (Mon, 25 Aug 2008) | 2 lines
Fix generator expressions, rename all "stmts" in Python.g to "stypes".
........
r5244 | fwierzbicki | 2008-08-25 11:25:24 -0400 (Mon, 25 Aug 2008) | 2 lines
Clean up creation of stmtType[] and exprType[].
........
r5245 | fwierzbicki | 2008-08-25 14:44:46 -0400 (Mon, 25 Aug 2008) | 3 lines
Fix build.xml (oops)
Fix Generator Expression assignments and multi-statement simple_stmt.
........
r5246 | fwierzbicki | 2008-08-25 16:36:04 -0400 (Mon, 25 Aug 2008) | 3 lines
Fixed fplist, subscripts (especially extslice) and enable Interactive
and Expression (single and eval). regrtest.py is now running.
........
r5247 | fwierzbicki | 2008-08-25 17:18:03 -0400 (Mon, 25 Aug 2008) | 2 lines
Fix **
........
r5248 | fwierzbicki | 2008-08-25 22:04:27 -0400 (Mon, 25 Aug 2008) | 3 lines
Cleanup: removal of comments, re-format of Python.g and
removal of PYNODE.
........
r5249 | fwierzbicki | 2008-08-26 08:21:42 -0400 (Tue, 26 Aug 2008) | 3 lines
Remove unnecessary constructor on BinOp (now it is just the generated version again)
Remove some whitespace in Python.g
........
r5250 | fwierzbicki | 2008-08-26 11:05:19 -0400 (Tue, 26 Aug 2008) | 2 lines
Moved makeSliceType code to GrammarActions.
........
r5251 | fwierzbicki | 2008-08-26 12:51:15 -0400 (Tue, 26 Aug 2008) | 2 lines
Moved Subscript creation up a rule - fixed a bunch of unit tests.
........
r5252 | fwierzbicki | 2008-08-26 15:37:34 -0400 (Tue, 26 Aug 2008) | 2 lines
Another expr_contextType adjustment.
........
r5253 | fwierzbicki | 2008-08-26 16:17:50 -0400 (Tue, 26 Aug 2008) | 2 lines
checking for bad assignment statements.
........
r5254 | fwierzbicki | 2008-08-26 20:44:35 -0400 (Tue, 26 Aug 2008) | 3 lines
Fix illegal Lambda statements and more illegal assigns. Also raise exceptions
on illegal gen expression arguments.
........
r5255 | fwierzbicki | 2008-08-26 22:38:35 -0400 (Tue, 26 Aug 2008) | 2 lines
Fix for FunctionDef and Call problems.
........
r5256 | fwierzbicki | 2008-08-26 22:47:56 -0400 (Tue, 26 Aug 2008) | 2 lines
whitespace
........
r5257 | fwierzbicki | 2008-08-27 12:07:35 -0400 (Wed, 27 Aug 2008) | 2 lines
revert build.xml after accidently checking in my local copy.
........
r5258 | fwierzbicki | 2008-08-27 15:51:16 -0400 (Wed, 27 Aug 2008) | 2 lines
for not_test, replacing manual tree construction with ->.
........
r5259 | fwierzbicki | 2008-08-27 16:09:39 -0400 (Wed, 27 Aug 2008) | 2 lines
TODO comment.
........
r5260 | fwierzbicki | 2008-08-27 16:35:54 -0400 (Wed, 27 Aug 2008) | 3 lines
Removed XXX comments that no longer apply (special testlists don't
seem to be needed). Removed unused c1 and c2 alias.
........
r5261 | fwierzbicki | 2008-08-27 16:51:19 -0400 (Wed, 27 Aug 2008) | 2 lines
simplify testlist.
........
| ... | ...@@ -16,6 +16,7 @@ | |
| 16 | 16 | public Print(Token token, exprType dest, exprType[] values, boolean nl) { |
| 17 | 17 | super(token); |
| 18 | 18 | this.dest = dest; |
| 19 | addChild(dest); | |
| 19 | 20 | this.values = values; |
| 20 | 21 | if (values != null) { |
| 21 | 22 | for(int ivalues=0;ivalues<values.length;ivalues++) { |
| ... | ...@@ -29,6 +30,7 @@ | |
| 29 | 30 | boolean nl) { |
| 30 | 31 | super(ttype, token); |
| 31 | 32 | this.dest = dest; |
| 33 | addChild(dest); | |
| 32 | 34 | this.values = values; |
| 33 | 35 | if (values != null) { |
| 34 | 36 | for(int ivalues=0;ivalues<values.length;ivalues++) { |
| ... | ...@@ -42,6 +44,7 @@ | |
| 42 | 44 | { |
| 43 | 45 | super(tree); |
| 44 | 46 | this.dest = dest; |
| 47 | addChild(dest); | |
| 45 | 48 | this.values = values; |
| 46 | 49 | if (values != null) { |
| 47 | 50 | for(int ivalues=0;ivalues<values.length;ivalues++) { |
| ... | ...@@ -38,22 +38,23 @@ | |
| 38 | 38 | PythonParser parser = new PythonParser(tokens); |
| 39 | 39 | parser.setErrorHandler(eh); |
| 40 | 40 | parser.setTreeAdaptor(new PythonTreeAdaptor()); |
| 41 | Tree r = null; | |
| 41 | PythonTree r = null; | |
| 42 | 42 | switch (_block) { |
| 43 | 43 | case MODULE : |
| 44 | r = (Tree)parser.file_input().tree; | |
| 44 | r = (PythonTree)parser.file_input().tree; | |
| 45 | 45 | break; |
| 46 | 46 | case INTERACTIVE : |
| 47 | r = (Tree)parser.single_input().tree; | |
| 47 | r = (PythonTree)parser.single_input().tree; | |
| 48 | 48 | break; |
| 49 | 49 | case EXPRESSION : |
| 50 | r = (Tree)parser.eval_input().tree; | |
| 50 | r = (PythonTree)parser.eval_input().tree; | |
| 51 | 51 | break; |
| 52 | 52 | } |
| 53 | 53 | if (args.length > 1) { |
| 54 | System.out.println((r).toStringTree()); | |
| 54 | System.out.println(r.toStringTree()); | |
| 55 | 55 | } |
| 56 | 56 | if (!isParseOnly()) { |
| 57 | /* | |
| 57 | 58 | CommonTreeNodeStream nodes = new CommonTreeNodeStream(r); |
| 58 | 59 | nodes.setTokenStream(tokens); |
| 59 | 60 | PythonWalker walker = new PythonWalker(nodes); |
| ... | ...@@ -73,6 +74,7 @@ | |
| 73 | 74 | if (args.length > 1) { |
| 74 | 75 | System.out.println(result.toStringTree()); |
| 75 | 76 | } |
| 77 | */ | |
| 76 | 78 | } |
| 77 | 79 | return result; |
| 78 | 80 | } |
| ... | ...@@ -16,18 +16,21 @@ | |
| 16 | 16 | super(token); |
| 17 | 17 | this.op = op; |
| 18 | 18 | this.operand = operand; |
| 19 | addChild(operand); | |
| 19 | 20 | } |
| 20 | 21 | |
| 21 | 22 | public UnaryOp(int ttype, Token token, unaryopType op, exprType operand) { |
| 22 | 23 | super(ttype, token); |
| 23 | 24 | this.op = op; |
| 24 | 25 | this.operand = operand; |
| 26 | addChild(operand); | |
| 25 | 27 | } |
| 26 | 28 | |
| 27 | 29 | public UnaryOp(PythonTree tree, unaryopType op, exprType operand) { |
| 28 | 30 | super(tree); |
| 29 | 31 | this.op = op; |
| 30 | 32 | this.operand = operand; |
| 33 | addChild(operand); | |
| 31 | 34 | } |
| 32 | 35 | |
| 33 | 36 | public String toString() { |
| ... | ...@@ -17,6 +17,7 @@ | |
| 17 | 17 | ctx) { |
| 18 | 18 | super(token); |
| 19 | 19 | this.value = value; |
| 20 | addChild(value); | |
| 20 | 21 | this.attr = attr; |
| 21 | 22 | this.ctx = ctx; |
| 22 | 23 | } |
| ... | ...@@ -25,6 +26,7 @@ | |
| 25 | 26 | expr_contextType ctx) { |
| 26 | 27 | super(ttype, token); |
| 27 | 28 | this.value = value; |
| 29 | addChild(value); | |
| 28 | 30 | this.attr = attr; |
| 29 | 31 | this.ctx = ctx; |
| 30 | 32 | } |
| ... | ...@@ -33,6 +35,7 @@ | |
| 33 | 35 | expr_contextType ctx) { |
| 34 | 36 | super(tree); |
| 35 | 37 | this.value = value; |
| 38 | addChild(value); | |
| 36 | 39 | this.attr = attr; |
| 37 | 40 | this.ctx = ctx; |
| 38 | 41 | } |
| ... | ...@@ -16,24 +16,33 @@ | |
| 16 | 16 | public Raise(Token token, exprType type, exprType inst, exprType tback) { |
| 17 | 17 | super(token); |
| 18 | 18 | this.type = type; |
| 19 | addChild(type); | |
| 19 | 20 | this.inst = inst; |
| 21 | addChild(inst); | |
| 20 | 22 | this.tback = tback; |
| 23 | addChild(tback); | |
| 21 | 24 | } |
| 22 | 25 | |
| 23 | 26 | public Raise(int ttype, Token token, exprType type, exprType inst, exprType |
| 24 | 27 | tback) { |
| 25 | 28 | super(ttype, token); |
| 26 | 29 | this.type = type; |
| 30 | addChild(type); | |
| 27 | 31 | this.inst = inst; |
| 32 | addChild(inst); | |
| 28 | 33 | this.tback = tback; |
| 34 | addChild(tback); | |
| 29 | 35 | } |
| 30 | 36 | |
| 31 | 37 | public Raise(PythonTree tree, exprType type, exprType inst, exprType tback) |
| 32 | 38 | { |
| 33 | 39 | super(tree); |
| 34 | 40 | this.type = type; |
| 41 | addChild(type); | |
| 35 | 42 | this.inst = inst; |
| 43 | addChild(inst); | |
| 36 | 44 | this.tback = tback; |
| 45 | addChild(tback); | |
| 37 | 46 | } |
| 38 | 47 | |
| 39 | 48 | public String toString() { |
| ... | ...@@ -14,16 +14,19 @@ | |
| 14 | 14 | public Yield(Token token, exprType value) { |
| 15 | 15 | super(token); |
| 16 | 16 | this.value = value; |
| 17 | addChild(value); | |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | 20 | public Yield(int ttype, Token token, exprType value) { |
| 20 | 21 | super(ttype, token); |
| 21 | 22 | this.value = value; |
| 23 | addChild(value); | |
| 22 | 24 | } |
| 23 | 25 | |
| 24 | 26 | public Yield(PythonTree tree, exprType value) { |
| 25 | 27 | super(tree); |
| 26 | 28 | this.value = value; |
| 29 | addChild(value); | |
| 27 | 30 | } |
| 28 | 31 | |
| 29 | 32 | public String toString() { |
| ... | ...@@ -260,12 +260,15 @@ | |
| 260 | 260 | if f.typedef is not None and f.typedef.simple: |
| 261 | 261 | not_simple = False |
| 262 | 262 | #For now ignoring String -- will want to revisit |
| 263 | if f.seq and not_simple and not fparg.startswith("String"): | |
| 264 | self.emit("if (%s != null) {" % f.name, depth+1); | |
| 265 | self.emit("for(int i%(name)s=0;i%(name)s<%(name)s.length;i%(name)s++) {" % {"name":f.name}, depth+2) | |
| 266 | self.emit("addChild(%s[i%s]);" % (f.name, f.name), depth+3) | |
| 267 | self.emit("}", depth+2) | |
| 268 | self.emit("}", depth+1) | |
| 263 | if not_simple and not fparg.startswith("String"): | |
| 264 | if f.seq: | |
| 265 | self.emit("if (%s != null) {" % f.name, depth+1); | |
| 266 | self.emit("for(int i%(name)s=0;i%(name)s<%(name)s.length;i%(name)s++) {" % {"name":f.name}, depth+2) | |
| 267 | self.emit("addChild(%s[i%s]);" % (f.name, f.name), depth+3) | |
| 268 | self.emit("}", depth+2) | |
| 269 | self.emit("}", depth+1) | |
| 270 | elif str(f.type) == "expr": | |
| 271 | self.emit("addChild(%s);" % (f.name), depth+1) | |
| 269 | 272 | |
| 270 | 273 | def javaMethods(self, type, clsname, ctorname, fields, depth): |
| 271 | 274 | # The java ctors |
| ... | ...@@ -20,6 +20,7 @@ | |
| 20 | 20 | keywords, exprType starargs, exprType kwargs) { |
| 21 | 21 | super(token); |
| 22 | 22 | this.func = func; |
| 23 | addChild(func); | |
| 23 | 24 | this.args = args; |
| 24 | 25 | if (args != null) { |
| 25 | 26 | for(int iargs=0;iargs<args.length;iargs++) { |
| ... | ...@@ -33,13 +34,16 @@ | |
| 33 | 34 | } |
| 34 | 35 | } |
| 35 | 36 | this.starargs = starargs; |
| 37 | addChild(starargs); | |
| 36 | 38 | this.kwargs = kwargs; |
| 39 | addChild(kwargs); | |
| 37 | 40 | } |
| 38 | 41 | |
| 39 | 42 | public Call(int ttype, Token token, exprType func, exprType[] args, |
| 40 | 43 | keywordType[] keywords, exprType starargs, exprType kwargs) { |
| 41 | 44 | super(ttype, token); |
| 42 | 45 | this.func = func; |
| 46 | addChild(func); | |
| 43 | 47 | this.args = args; |
| 44 | 48 | if (args != null) { |
| 45 | 49 | for(int iargs=0;iargs<args.length;iargs++) { |
| ... | ...@@ -53,13 +57,16 @@ | |
| 53 | 57 | } |
| 54 | 58 | } |
| 55 | 59 | this.starargs = starargs; |
| 60 | addChild(starargs); | |
| 56 | 61 | this.kwargs = kwargs; |
| 62 | addChild(kwargs); | |
| 57 | 63 | } |
| 58 | 64 | |
| 59 | 65 | public Call(PythonTree tree, exprType func, exprType[] args, keywordType[] |
| 60 | 66 | keywords, exprType starargs, exprType kwargs) { |
| 61 | 67 | super(tree); |
| 62 | 68 | this.func = func; |
| 69 | addChild(func); | |
| 63 | 70 | this.args = args; |
| 64 | 71 | if (args != null) { |
| 65 | 72 | for(int iargs=0;iargs<args.length;iargs++) { |
| ... | ...@@ -73,7 +80,9 @@ | |
| 73 | 80 | } |
| 74 | 81 | } |
| 75 | 82 | this.starargs = starargs; |
| 83 | addChild(starargs); | |
| 76 | 84 | this.kwargs = kwargs; |
| 85 | addChild(kwargs); | |
| 77 | 86 | } |
| 78 | 87 | |
| 79 | 88 | public String toString() { |
| ... | ...@@ -17,7 +17,9 @@ | |
| 17 | 17 | exprType[] ifs) { |
| 18 | 18 | super(token); |
| 19 | 19 | this.target = target; |
| 20 | addChild(target); | |
| 20 | 21 | this.iter = iter; |
| 22 | addChild(iter); | |
| 21 | 23 | this.ifs = ifs; |
| 22 | 24 | if (ifs != null) { |
| 23 | 25 | for(int iifs=0;iifs<ifs.length;iifs++) { |
| ... | ...@@ -30,7 +32,9 @@ | |
| 30 | 32 | iter, exprType[] ifs) { |
| 31 | 33 | super(ttype, token); |
| 32 | 34 | this.target = target; |
| 35 | addChild(target); | |
| 33 | 36 | this.iter = iter; |
| 37 | addChild(iter); | |
| 34 | 38 | this.ifs = ifs; |
| 35 | 39 | if (ifs != null) { |
| 36 | 40 | for(int iifs=0;iifs<ifs.length;iifs++) { |
| ... | ...@@ -43,7 +47,9 @@ | |
| 43 | 47 | exprType[] ifs) { |
| 44 | 48 | super(tree); |
| 45 | 49 | this.target = target; |
| 50 | addChild(target); | |
| 46 | 51 | this.iter = iter; |
| 52 | addChild(iter); | |
| 47 | 53 | this.ifs = ifs; |
| 48 | 54 | if (ifs != null) { |
| 49 | 55 | for(int iifs=0;iifs<ifs.length;iifs++) { |
| ... | ...@@ -1,1286 +0,0 @@ | |
| 1 | /* | |
| 2 | Copyright (c) 2007-2008 Frank Wierzbicki | |
| 3 | Licensed to PSF under a Contributor Agreement. | |
| 4 | */ | |
| 5 | tree grammar PythonWalker; | |
| 6 | ||
| 7 | options { | |
| 8 | tokenVocab=Python; | |
| 9 | ASTLabelType=PythonTree; | |
| 10 | } | |
| 11 | ||
| 12 | @header { | |
| 13 | package org.python.antlr; | |
| 14 | ||
| 15 | import org.python.core.Py; | |
| 16 | import org.python.core.PyObject; | |
| 17 | import org.python.core.PyString; | |
| 18 | import org.python.antlr.ParseException; | |
| 19 | import org.python.antlr.ast.aliasType; | |
| 20 | import org.python.antlr.ast.argumentsType; | |
| 21 | import org.python.antlr.ast.boolopType; | |
| 22 | import org.python.antlr.ast.comprehensionType; | |
| 23 | import org.python.antlr.ast.cmpopType; | |
| 24 | import org.python.antlr.ast.excepthandlerType; | |
| 25 | import org.python.antlr.ast.exprType; | |
| 26 | import org.python.antlr.ast.expr_contextType; | |
| 27 | import org.python.antlr.ast.keywordType; | |
| 28 | import org.python.antlr.ast.modType; | |
| 29 | import org.python.antlr.ast.operatorType; | |
| 30 | import org.python.antlr.ast.sliceType; | |
| 31 | import org.python.antlr.ast.stmtType; | |
| 32 | import org.python.antlr.ast.unaryopType; | |
| 33 | import org.python.antlr.ast.Assert; | |
| 34 | import org.python.antlr.ast.Assign; | |
| 35 | import org.python.antlr.ast.Attribute; | |
| 36 | import org.python.antlr.ast.AugAssign; | |
| 37 | import org.python.antlr.ast.BinOp; | |
| 38 | import org.python.antlr.ast.BoolOp; | |
| 39 | import org.python.antlr.ast.Break; | |
| 40 | import org.python.antlr.ast.Call; | |
| 41 | import org.python.antlr.ast.ClassDef; | |
| 42 | import org.python.antlr.ast.Compare; | |
| 43 | import org.python.antlr.ast.Continue; | |
| 44 | import org.python.antlr.ast.Delete; | |
| 45 | import org.python.antlr.ast.Dict; | |
| 46 | import org.python.antlr.ast.Ellipsis; | |
| 47 | import org.python.antlr.ast.ErrorStmt; | |
| 48 | import org.python.antlr.ast.Exec; | |
| 49 | import org.python.antlr.ast.Expr; | |
| 50 | import org.python.antlr.ast.Expression; | |
| 51 | import org.python.antlr.ast.ExtSlice; | |
| 52 | import org.python.antlr.ast.For; | |
| 53 | import org.python.antlr.ast.FunctionDef; | |
| 54 | import org.python.antlr.ast.GeneratorExp; | |
| 55 | import org.python.antlr.ast.Global; | |
| 56 | import org.python.antlr.ast.If; | |
| 57 | import org.python.antlr.ast.IfExp; | |
| 58 | import org.python.antlr.ast.Index; | |
| 59 | import org.python.antlr.ast.Import; | |
| 60 | import org.python.antlr.ast.ImportFrom; | |
| 61 | import org.python.antlr.ast.Interactive; | |
| 62 | import org.python.antlr.ast.Lambda; | |
| 63 | import org.python.antlr.ast.ListComp; | |
| 64 | import org.python.antlr.ast.Module; | |
| 65 | import org.python.antlr.ast.Name; | |
| 66 | import org.python.antlr.ast.Num; | |
| 67 | import org.python.antlr.ast.Slice; | |
| 68 | import org.python.antlr.ast.Subscript; | |
| 69 | import org.python.antlr.ast.TryExcept; | |
| 70 | import org.python.antlr.ast.TryFinally; | |
| 71 | import org.python.antlr.ast.Tuple; | |
| 72 | import org.python.antlr.ast.Pass; | |
| 73 | import org.python.antlr.ast.Print; | |
| 74 | import org.python.antlr.ast.Raise; | |
| 75 | import org.python.antlr.ast.Repr; | |
| 76 | import org.python.antlr.ast.Return; | |
| 77 | import org.python.antlr.ast.Str; | |
| 78 | import org.python.antlr.ast.UnaryOp; | |
| 79 | import org.python.antlr.ast.With; | |
| 80 | import org.python.antlr.ast.While; | |
| 81 | import org.python.antlr.ast.Yield; | |
| 82 | import java.math.BigInteger; | |
| 83 | import java.util.Arrays; | |
| 84 | import java.util.Collections; | |
| 85 | import java.util.HashMap; | |
| 86 | import java.util.Iterator; | |
| 87 | import java.util.ListIterator; | |
| 88 | import java.util.Map; | |
| 89 | import java.util.Set; | |
| 90 | } | |
| 91 | @members { | |
| 92 | boolean debugOn = false; | |
| 93 | private ErrorHandler errorHandler; | |
| 94 | private GrammarActions actions = new GrammarActions(); | |
| 95 | ||
| 96 | public void setErrorHandler(ErrorHandler eh) { | |
| 97 | this.errorHandler = eh; | |
| 98 | actions.setErrorHandler(eh); | |
| 99 | } | |
| 100 | ||
| 101 | public void debug(String message) { | |
| 102 | if (debugOn) { | |
| 103 | System.out.println(message); | |
| 104 | } | |
| 105 | } | |
| 106 | ||
| 107 | protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { | |
| 108 | throw new MismatchedTokenException(ttype, input); | |
| 109 | } | |
| 110 | ||
| 111 | protected void mismatch(IntStream input, RecognitionException e, BitSet follow) throws RecognitionException { | |
| 112 | throw e; | |
| 113 | } | |
| 114 | ||
| 115 | String name = "Test"; | |
| 116 | ||
| 117 | //XXX: Not sure I need any below... | |
| 118 | String filename = "test.py"; | |
| 119 | boolean linenumbers = true; | |
| 120 | boolean setFile = true; | |
| 121 | boolean printResults = false; | |
| 122 | //CompilerFlags cflags = Py.getCompilerFlags(); | |
| 123 | ||
| 124 | } | |
| 125 | ||
| 126 | @rulecatch { | |
| 127 | catch (RecognitionException re) { | |
| 128 | errorHandler.reportError(this, re); | |
| 129 | errorHandler.recover(this, input,re); | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | ||
| 134 | expression returns [modType mod] | |
| 135 | : ^(Expression test[expr_contextType.Load]) { $mod = actions.makeExpression($Expression, $test.etype); } | |
| 136 | ; | |
| 137 | ||
| 138 | interactive returns [modType mod] | |
| 139 | : ^(Interactive stmts?) { $mod = actions.makeInteractive($Interactive, $stmts.stypes); } | |
| 140 | ; | |
| 141 | ||
| 142 | module returns [modType mod] | |
| 143 | : ^(Module | |
| 144 | ( stmts {$mod = actions.makeMod($Module, $stmts.stypes); } | |
| 145 | | {$mod = actions.makeMod($Module, null);} | |
| 146 | ) | |
| 147 | ) | |
| 148 | ; | |
| 149 | ||
| 150 | funcdef | |
| 151 | : ^(DEF NAME ^(Arguments varargslist?) ^(Body stmts) ^(Decorators decorators?)) { | |
| 152 | $stmts::statements.add(actions.makeFunctionDef($DEF, $NAME, $varargslist.args, $stmts.stypes, $decorators.etypes)); | |
| 153 | } | |
| 154 | ; | |
| 155 | ||
| 156 | varargslist returns [argumentsType args] | |
| 157 | @init { | |
| 158 | List params = new ArrayList(); | |
| 159 | List defaults = new ArrayList(); | |
| 160 | } | |
| 161 | : ^(Args defparameter[params, defaults]+) (^(StarArgs sname=NAME))? (^(KWArgs kname=NAME))? { | |
| 162 | $args = actions.makeArgumentsType($Args, params, $sname, $kname, defaults); | |
| 163 | } | |
| 164 | | ^(StarArgs sname=NAME) (^(KWArgs kname=NAME))? { | |
| 165 | $args = actions.makeArgumentsType($StarArgs,params, $sname, $kname, defaults); | |
| 166 | } | |
| 167 | | ^(KWArgs NAME) { | |
| 168 | $args = actions.makeArgumentsType($KWArgs, params, null, $NAME, defaults); | |
| 169 | } | |
| 170 | ; | |
| 171 | ||
| 172 | defparameter[List params, List defaults] | |
| 173 | : fpdef[expr_contextType.Param, null] (ASSIGN test[expr_contextType.Load])? { | |
| 174 | params.add($fpdef.etype); | |
| 175 | if ($ASSIGN != null) { | |
| 176 | defaults.add($test.etype); | |
| 177 | } else if (!defaults.isEmpty()) { | |
| 178 | throw new ParseException( | |
| 179 | "non-default argument follows default argument", | |
| 180 | $fpdef.start); | |
| 181 | } | |
| 182 | } | |
| 183 | ; | |
| 184 | ||
| 185 | fpdef [expr_contextType ctype, List nms] returns [exprType etype] | |
| 186 | : NAME { | |
| 187 | exprType e = new Name($NAME, $NAME.text, ctype); | |
| 188 | actions.checkAssign(e); | |
| 189 | if (nms == null) { | |
| 190 | $etype = e; | |
| 191 | } else { | |
| 192 | nms.add(e); | |
| 193 | } | |
| 194 | } | |
| 195 | | ^(FpList fplist) { | |
| 196 | exprType[] e = (exprType[])$fplist.etypes.toArray(new exprType[$fplist.etypes.size()]); | |
| 197 | Tuple t = new Tuple($fplist.start, e, expr_contextType.Store); | |
| 198 | if (nms == null) { | |
| 199 | $etype = t; | |
| 200 | } else { | |
| 201 | nms.add(t); | |
| 202 | } | |
| 203 | } | |
| 204 | ; | |
| 205 | ||
| 206 | fplist returns [List etypes] | |
| 207 | @init { | |
| 208 | List nms = new ArrayList(); | |
| 209 | } | |
| 210 | : fpdef[expr_contextType.Store, nms]+ { | |
| 211 | $etypes = nms; | |
| 212 | } | |
| 213 | ; | |
| 214 | ||
| 215 | decorators returns [List etypes] | |
| 216 | @init { | |
| 217 | List decs = new ArrayList(); | |
| 218 | } | |
| 219 | : decorator[decs]+ { | |
| 220 | $etypes = decs; | |
| 221 | } | |
| 222 | ; | |
| 223 | ||
| 224 | decorator [List decs] | |
| 225 | : ^(AT dotted_attr (^(Call (^(Args arglist))?))?) { | |
| 226 | if ($Call == null) { | |
| 227 | decs.add($dotted_attr.etype); | |
| 228 | } else { | |
| 229 | exprType c; | |
| 230 | if ($Args == null) { | |
| 231 | c = actions.makeCall($Call, $dotted_attr.etype); | |
| 232 | } else { | |
| 233 | c = actions.makeCall($Call, $dotted_attr.etype, $arglist.args, $arglist.keywords, $arglist.starargs, $arglist.kwargs); | |
| 234 | } | |
| 235 | c.setCharStopIndex($Call.getCharStopIndex()); | |
| 236 | decs.add(c); | |
| 237 | } | |
| 238 | } | |
| 239 | ; | |
| 240 | ||
| 241 | dotted_attr returns [exprType etype, PythonTree marker] | |
| 242 | : attr { | |
| 243 | $etype = new Name($attr.start, $attr.text, expr_contextType.Load); | |
| 244 | $marker = $attr.start; | |
| 245 | debug("matched NAME in dotted_attr");} | |
| 246 | | ^(DOT n1=dotted_attr n2=dotted_attr) { | |
| 247 | $etype = new Attribute($n1.marker, $n1.etype, $n2.text, expr_contextType.Load); | |
| 248 | $marker = $n1.marker; | |
| 249 | } | |
| 250 | ; | |
| 251 | ||
| 252 | //attr is here for Java compatibility. A Java foo.getIf() can be called from Jython as foo.if | |
| 253 | // so we need to support any keyword as an attribute. | |
| 254 | attr | |
| 255 | : NAME | |
| 256 | | AND | |
| 257 | | AS | |
| 258 | | ASSERT | |
| 259 | | BREAK | |
| 260 | | CLASS | |
| 261 | | CONTINUE | |
| 262 | | DEF | |
| 263 | | DELETE | |
| 264 | | ELIF | |
| 265 | | EXCEPT | |
| 266 | | EXEC | |
| 267 | | FINALLY | |
| 268 | | FROM | |
| 269 | | FOR | |
| 270 | | GLOBAL | |
| 271 | | IF | |
| 272 | | IMPORT | |
| 273 | | IN | |
| 274 | | IS | |
| 275 | | LAMBDA | |
| 276 | | NOT | |
| 277 | | OR | |
| 278 | | ORELSE | |
| 279 | | PASS | |
| 280 | ||
| 281 | | RAISE | |
| 282 | | RETURN | |
| 283 | | TRY | |
| 284 | | WHILE | |
| 285 | | WITH | |
| 286 | | YIELD | |
| 287 | ; | |
| 288 | ||
| 289 | stmts returns [List stypes] | |
| 290 | scope { | |
| 291 | List statements; | |
| 292 | } | |
| 293 | @init { | |
| 294 | $stmts::statements = new ArrayList(); | |
| 295 | } | |
| 296 | : stmt+ { | |
| 297 | debug("Matched stmts"); | |
| 298 | $stypes = $stmts::statements; | |
| 299 | } | |
| 300 | | INDENT stmt+ DEDENT { | |
| 301 | debug("Matched stmts"); | |
| 302 | $stypes = $stmts::statements; | |
| 303 | } | |
| 304 | ; | |
| 305 | ||
| 306 | stmt //combines simple_stmt and compound_stmt from Python.g | |
| 307 | : expr_stmt | |
| 308 | | print_stmt | |
| 309 | | del_stmt | |
| 310 | | pass_stmt | |
| 311 | | flow_stmt | |
| 312 | | import_stmt | |
| 313 | | global_stmt | |
| 314 | | exec_stmt | |
| 315 | | assert_stmt | |
| 316 | | if_stmt | |
| 317 | | while_stmt | |
| 318 | | for_stmt | |
| 319 | | try_stmt | |
| 320 | | with_stmt | |
| 321 | | funcdef | |
| 322 | | classdef | |
| 323 | ; | |
| 324 | ||
| 325 | expr_stmt | |
| 326 | : test[expr_contextType.Load] { | |
| 327 | debug("matched expr_stmt:test " + $test.etype); | |
| 328 | $stmts::statements.add(new Expr($test.marker, $test.etype)); | |
| 329 | } | |
| 330 | | ^(augassign targ=test[expr_contextType.AugStore] value=test[expr_contextType.Load]) { | |
| 331 | actions.checkAssign($targ.etype); | |
| 332 | AugAssign a = new AugAssign($targ.marker, $targ.etype, $augassign.op, $value.etype); | |
| 333 | $stmts::statements.add(a); | |
| 334 | } | |
| 335 | | ^(Assign targets ^(Value value=test[expr_contextType.Load])) { | |
| 336 | debug("Matched Assign"); | |
| 337 | exprType[] e = new exprType[$targets.etypes.size()]; | |
| 338 | for(int i=0;i<$targets.etypes.size();i++) { | |
| 339 | e[i] = (exprType)$targets.etypes.get(i); | |
| 340 | } | |
| 341 | debug("exprs: " + e.length); | |
| 342 | Assign a = new Assign($Assign, e, $value.etype); | |
| 343 | $stmts::statements.add(a); | |
| 344 | } | |
| 345 | ; | |
| 346 | ||
| 347 | call_expr returns [exprType etype, PythonTree marker] | |
| 348 | : ^(Call (^(Args arglist))? test[expr_contextType.Load]) { | |
| 349 | exprType c; | |
| 350 | if ($Args == null) { | |
| 351 | c = actions.makeCall($test.marker, $test.etype); | |
| 352 | } else { | |
| 353 | c = actions.makeCall($test.marker, $test.etype, $arglist.args, $arglist.keywords, $arglist.starargs, $arglist.kwargs); | |
| 354 | } | |
| 355 | c.setCharStopIndex($Call.getCharStopIndex()); | |
| 356 | $etype = c; | |
| 357 | } | |
| 358 | ; | |
| 359 | ||
| 360 | targets returns [List etypes] | |
| 361 | @init { | |
| 362 | List targs = new ArrayList(); | |
| 363 | } | |
| 364 | : target[targs]+ { | |
| 365 | $etypes = targs; | |
| 366 | } | |
| 367 | ; | |
| 368 | ||
| 369 | target[List etypes] | |
| 370 | : ^(Target atom[expr_contextType.Store]) { | |
| 371 | actions.checkAssign($atom.etype); | |
| 372 | etypes.add($atom.etype); | |
| 373 | } | |
| 374 | ; | |
| 375 | ||
| 376 | augassign returns [operatorType op] | |
| 377 | : PLUSEQUAL {$op = operatorType.Add;} | |
| 378 | | MINUSEQUAL {$op = operatorType.Sub;} | |
| 379 | | STAREQUAL {$op = operatorType.Mult;} | |
| 380 | | SLASHEQUAL {$op = operatorType.Div;} | |
| 381 | | PERCENTEQUAL {$op = operatorType.Mod;} | |
| 382 | | AMPEREQUAL {$op = operatorType.BitAnd;} | |
| 383 | | VBAREQUAL {$op = operatorType.BitOr;} | |
| 384 | | CIRCUMFLEXEQUAL {$op = operatorType.BitXor;} | |
| 385 | | LEFTSHIFTEQUAL {$op = operatorType.LShift;} | |
| 386 | | RIGHTSHIFTEQUAL {$op = operatorType.RShift;} | |
| 387 | | DOUBLESTAREQUAL {$op = operatorType.Pow;} | |
| 388 | | DOUBLESLASHEQUAL {$op = operatorType.FloorDiv;} | |
| 389 | ; | |
| 390 | ||
| 391 | binop returns [operatorType op] | |
| 392 | : PLUS {$op = operatorType.Add;} | |
| 393 | | MINUS {$op = operatorType.Sub;} | |
| 394 | | STAR {$op = operatorType.Mult;} | |
| 395 | | SLASH {$op = operatorType.Div;} | |
| 396 | | PERCENT {$op = operatorType.Mod;} | |
| 397 | | AMPER {$op = operatorType.BitAnd;} | |
| 398 | | VBAR {$op = operatorType.BitOr;} | |
| 399 | | CIRCUMFLEX {$op = operatorType.BitXor;} | |
| 400 | | LEFTSHIFT {$op = operatorType.LShift;} | |
| 401 | | RIGHTSHIFT {$op = operatorType.RShift;} | |
| 402 | | DOUBLESTAR {$op = operatorType.Pow;} | |
| 403 | | DOUBLESLASH {$op = operatorType.FloorDiv;} | |
| 404 | ; | |
| 405 | ||
| 406 | ||
| 407 | print_stmt | |
| 408 | : ^(PRINT (^(Dest RIGHTSHIFT))? (^(Values ^(Elts elts[expr_contextType.Load])))? (Newline)?) { | |
| 409 | exprType[] values; | |
| 410 | ||
| 411 | exprType dest = null; | |
| 412 | boolean hasdest = false; | |
| 413 | ||
| 414 | boolean newline = false; | |
| 415 | if ($Newline != null) { | |
| 416 | newline = true; | |
| 417 | } | |
| 418 | ||
| 419 | if ($Dest != null) { | |
| 420 | hasdest = true; | |
| 421 | } | |
| 422 | if ($Values != null) { | |
| 423 | exprType[] t = (exprType[])$elts.etypes.toArray(new exprType[$elts.etypes.size()]); | |
| 424 | if (hasdest) { | |
| 425 | dest = t[0]; | |
| 426 | values = new exprType[t.length - 1]; | |
| 427 | System.arraycopy(t, 1, values, 0, values.length); | |
| 428 | } else { | |
| 429 | values = t; | |
| 430 | } | |
| 431 | } else { | |
| 432 | values = new exprType[0]; | |
| 433 | } | |
| 434 | Print p = new Print($PRINT, dest, values, newline); | |
| 435 | $stmts::statements.add(p); | |
| 436 | } | |
| 437 | ; | |
| 438 | ||
| 439 | del_stmt | |
| 440 | : ^(DELETE elts[expr_contextType.Del]) { | |
| 441 | exprType[] t = (exprType[])$elts.etypes.toArray(new exprType[$elts.etypes.size()]); | |
| 442 | actions.checkDelete(t); | |
| 443 | $stmts::statements.add(new Delete($DELETE, t)); | |
| 444 | } | |
| 445 | ; | |
| 446 | ||
| 447 | pass_stmt | |
| 448 | : PASS { | |
| 449 | debug("Matched Pass"); | |
| 450 | $stmts::statements.add(new Pass($PASS)); | |
| 451 | } | |
| 452 | ; | |
| 453 | ||
| 454 | flow_stmt | |
| 455 | : break_stmt | |
| 456 | | continue_stmt | |
| 457 | | return_stmt | |
| 458 | | raise_stmt | |
| 459 | ; | |
| 460 | ||
| 461 | break_stmt | |
| 462 | : BREAK { | |
| 463 | $stmts::statements.add($BREAK); | |
| 464 | } | |
| 465 | ; | |
| 466 | ||
| 467 | continue_stmt | |
| 468 | : CONTINUE { | |
| 469 | $stmts::statements.add($CONTINUE); | |
| 470 | } | |
| 471 | ; | |
| 472 | ||
| 473 | return_stmt | |
| 474 | : ^(RETURN (^(Value test[expr_contextType.Load]))?) { | |
| 475 | exprType v = null; | |