├── .classpath ├── .gitignore ├── .project ├── JPython.iml ├── LICENSE ├── README.md ├── ast ├── Python.asdl ├── asdl.py ├── asdl_java.py └── spark.py ├── grammar └── Grammar ├── source └── test.py └── src └── pers └── xia └── jpython ├── ast ├── Assert.java ├── Assign.java ├── AsyncFor.java ├── AsyncFunctionDef.java ├── AsyncWith.java ├── Attribute.java ├── AugAssign.java ├── Await.java ├── BinOp.java ├── BoolOp.java ├── Break.java ├── Bytes.java ├── Call.java ├── ClassDef.java ├── Compare.java ├── Continue.java ├── Delete.java ├── Dict.java ├── DictComp.java ├── Ellipsis.java ├── ExceptHandler.java ├── Expr.java ├── Expression.java ├── ExtSlice.java ├── For.java ├── FunctionDef.java ├── GeneratorExp.java ├── Global.java ├── If.java ├── IfExp.java ├── Import.java ├── ImportFrom.java ├── Index.java ├── Interactive.java ├── Lambda.java ├── List.java ├── ListComp.java ├── Module.java ├── Name.java ├── NameConstant.java ├── Nonlocal.java ├── Num.java ├── Pass.java ├── Raise.java ├── Return.java ├── Set.java ├── SetComp.java ├── Slice.java ├── Starred.java ├── Str.java ├── Subscript.java ├── Suite.java ├── Try.java ├── Tuple.java ├── UnaryOp.java ├── VisitorIF.java ├── While.java ├── With.java ├── Yield.java ├── YieldFrom.java ├── aliasType.java ├── argType.java ├── argumentsType.java ├── boolopType.java ├── cmpopType.java ├── comprehensionType.java ├── excepthandlerType.java ├── exprType.java ├── expr_contextType.java ├── keywordType.java ├── modType.java ├── operatorType.java ├── sliceType.java ├── stmtType.java ├── unaryopType.java └── withitemType.java ├── compiler ├── BlockType.java ├── Compiler.java ├── CompilerUnit.java ├── PySTEntryObject.java └── Symtable.java ├── config └── Config.java ├── grammar ├── Arc.java ├── DFA.java ├── DFAType.java ├── GramInit.java ├── Grammar.java ├── Label.java ├── State.java └── pgen │ ├── DoubleS.java │ ├── Pgen.java │ ├── _Arc.java │ ├── _DFA.java │ ├── _Grammar.java │ ├── _Label.java │ ├── _Node.java │ ├── _NodeType.java │ └── _State.java ├── main └── Run.java ├── object ├── Py.java ├── PyBoolean.java ├── PyBytes.java ├── PyCodeObject.java ├── PyComplex.java ├── PyDict.java ├── PyDictComm.java ├── PyExceptions.java ├── PyFloat.java ├── PyList.java ├── PyLong.java ├── PyNone.java ├── PyNumber.java ├── PyObject.java ├── PySequence.java ├── PySet.java └── PyUnicode.java ├── parser ├── Ast.java ├── Node.java ├── ParseToken.java └── Parser.java └── tokenizer ├── ErrorCode.java ├── TokState.java ├── Token.java └── Tokenizer.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | *.swp 3 | *.pyc 4 | /.settings/ 5 | /test/ 6 | /out/ 7 | /.idea/ 8 | /.setting/ 9 | /JPython.iml 10 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | JPython 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /JPython.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JPython 2 | python interpreter by Java 3 | -------------------------------------------------------------------------------- /ast/Python.asdl: -------------------------------------------------------------------------------- 1 | -- ASDL's six builtin types are identifier, int, string, bytes, object, singleton 2 | 3 | module Python 4 | { 5 | mod = Module(stmt* body) 6 | | Interactive(stmt* body) 7 | | Expression(expr body) 8 | 9 | -- not really an actual node but useful in Jython's typesystem. 10 | | Suite(stmt* body) 11 | 12 | stmt = FunctionDef(identifier name, arguments args, 13 | stmt* body, expr* decorator_list, expr? returns) 14 | | AsyncFunctionDef(identifier name, arguments args, 15 | stmt* body, expr* decorator_list, expr? returns) 16 | 17 | | ClassDef(identifier name, 18 | expr* bases, 19 | keyword* keywords, 20 | stmt* body, 21 | expr* decorator_list) 22 | | Return(expr? value) 23 | 24 | | Delete(expr* targets) 25 | | Assign(expr* targets, expr value) 26 | | AugAssign(expr target, operator op, expr value) 27 | 28 | -- use 'orelse' because else is a keyword in target languages 29 | | For(expr target, expr iter, stmt* body, stmt* orelse) 30 | | AsyncFor(expr target, expr iter, stmt* body, stmt* orelse) 31 | | While(expr test, stmt* body, stmt* orelse) 32 | | If(expr test, stmt* body, stmt* orelse) 33 | | With(withitem* items, stmt* body) 34 | | AsyncWith(withitem* items, stmt* body) 35 | 36 | | Raise(expr? exc, expr? cause) 37 | | Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody) 38 | | Assert(expr test, expr? msg) 39 | 40 | | Import(alias* names) 41 | | ImportFrom(identifier? module, alias* names, int? level) 42 | 43 | | Global(identifier* names) 44 | | Nonlocal(identifier* names) 45 | | Expr(expr value) 46 | | Pass | Break | Continue 47 | 48 | -- XXX Jython will be different 49 | -- col_offset is the byte offset in the utf8 string the parser uses 50 | attributes (int lineno, int col_offset) 51 | 52 | -- BoolOp() can use left & right? 53 | expr = BoolOp(boolop op, expr* values) 54 | | BinOp(expr left, operator op, expr right) 55 | | UnaryOp(unaryop op, expr operand) 56 | | Lambda(arguments args, expr body) 57 | | IfExp(expr test, expr body, expr orelse) 58 | | Dict(expr* keys, expr* values) 59 | | Set(expr* elts) 60 | | ListComp(expr elt, comprehension* generators) 61 | | SetComp(expr elt, comprehension* generators) 62 | | DictComp(expr key, expr value, comprehension* generators) 63 | | GeneratorExp(expr elt, comprehension* generators) 64 | -- the grammar constrains where yield expressions can occur 65 | | Await(expr value) 66 | | Yield(expr? value) 67 | | YieldFrom(expr value) 68 | -- need sequences for compare to distinguish between 69 | -- x < 4 < 3 and (x < 4) < 3 70 | | Compare(expr left, cmpop* ops, expr* comparators) 71 | | Call(expr func, expr* args, keyword* keywords) 72 | | Num(object n) -- a number as a PyObject. 73 | | Str(string s) -- need to specify raw, unicode, etc? 74 | | Bytes(bytes s) 75 | | NameConstant(singleton value) 76 | | Ellipsis 77 | 78 | -- the following expression can appear in assignment context 79 | | Attribute(expr value, identifier attr, expr_context ctx) 80 | | Subscript(expr value, slice slice, expr_context ctx) 81 | | Starred(expr value, expr_context ctx) 82 | | Name(identifier id, expr_context ctx) 83 | | List(expr* elts, expr_context ctx) 84 | | Tuple(expr* elts, expr_context ctx) 85 | 86 | -- col_offset is the byte offset in the utf8 string the parser uses 87 | attributes (int lineno, int col_offset) 88 | 89 | expr_context = Load | Store | Del | AugLoad | AugStore | Param 90 | 91 | slice = Slice(expr? lower, expr? upper, expr? step) 92 | | ExtSlice(slice* dims) 93 | | Index(expr value) 94 | 95 | boolop = And | Or 96 | 97 | operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift 98 | | RShift | BitOr | BitXor | BitAnd | FloorDiv 99 | 100 | unaryop = Invert | Not | UAdd | USub 101 | 102 | cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn 103 | 104 | comprehension = (expr target, expr iter, expr* ifs) 105 | 106 | excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body) 107 | attributes (int lineno, int col_offset) 108 | 109 | arguments = (arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults, 110 | arg? kwarg, expr* defaults) 111 | 112 | arg = (identifier arg, expr? annotation) 113 | attributes (int lineno, int col_offset) 114 | 115 | -- keyword arguments supplied to call (NULL identifier for **kwargs) 116 | keyword = (identifier? arg, expr value) 117 | 118 | -- import name with optional 'as' alias. 119 | alias = (identifier name, identifier? asname) 120 | 121 | withitem = (expr context_expr, expr? optional_vars) 122 | } 123 | 124 | -------------------------------------------------------------------------------- /ast/asdl.py: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Parser for ASDL [1] definition files. Reads in an ASDL description and parses 3 | # it into an AST that describes it. 4 | # 5 | # The EBNF we're parsing here: Figure 1 of the paper [1]. Extended to support 6 | # modules and attributes after a product. Words starting with Capital letters 7 | # are terminals. Literal tokens are in "double quotes". Others are 8 | # non-terminals. Id is either TokenId or ConstructorId. 9 | # 10 | # module ::= "module" Id "{" [definitions] "}" 11 | # definitions ::= { TypeId "=" type } 12 | # type ::= product | sum 13 | # product ::= fields ["attributes" fields] 14 | # fields ::= "(" { field, "," } field ")" 15 | # field ::= TypeId ["?" | "*"] [Id] 16 | # sum ::= constructor { "|" constructor } ["attributes" fields] 17 | # constructor ::= ConstructorId [fields] 18 | # 19 | # [1] "The Zephyr Abstract Syntax Description Language" by Wang, et. al. See 20 | # http://asdl.sourceforge.net/ 21 | #------------------------------------------------------------------------------- 22 | from collections import namedtuple 23 | import re 24 | 25 | __all__ = [ 26 | 'builtin_types', 'parse', 'AST', 'Module', 'Type', 'Constructor', 27 | 'Field', 'Sum', 'Product', 'VisitorBase', 'Check', 'check'] 28 | 29 | # The following classes define nodes into which the ASDL description is parsed. 30 | # Note: this is a "meta-AST". ASDL files (such as Python.asdl) describe the AST 31 | # structure used by a programming language. But ASDL files themselves need to be 32 | # parsed. This module parses ASDL files and uses a simple AST to represent them. 33 | # See the EBNF at the top of the file to understand the logical connection 34 | # between the various node types. 35 | 36 | builtin_types = {'identifier', 'string', 'bytes', 'int', 'object', 'singleton'} 37 | 38 | class AST: 39 | def __repr__(self): 40 | raise NotImplementedError 41 | 42 | class Module(AST): 43 | def __init__(self, name, dfns): 44 | self.name = name 45 | self.dfns = dfns 46 | self.types = {type.name: type.value for type in dfns} 47 | 48 | def __repr__(self): 49 | return 'Module({0.name}, {0.dfns})'.format(self) 50 | 51 | class Type(AST): 52 | def __init__(self, name, value): 53 | self.name = name 54 | self.value = value 55 | 56 | def __repr__(self): 57 | return 'Type({0.name}, {0.value})'.format(self) 58 | 59 | class Constructor(AST): 60 | def __init__(self, name, fields=None): 61 | self.name = name 62 | self.fields = fields or [] 63 | 64 | def __repr__(self): 65 | return 'Constructor({0.name}, {0.fields})'.format(self) 66 | 67 | class Field(AST): 68 | def __init__(self, type, name=None, seq=False, opt=False): 69 | self.type = type 70 | self.name = name 71 | self.seq = seq 72 | self.opt = opt 73 | 74 | def __repr__(self): 75 | if self.seq: 76 | extra = ", seq=True" 77 | elif self.opt: 78 | extra = ", opt=True" 79 | else: 80 | extra = "" 81 | if self.name is None: 82 | return 'Field({0.type}{1})'.format(self, extra) 83 | else: 84 | return 'Field({0.type}, {0.name}{1})'.format(self, extra) 85 | 86 | class Sum(AST): 87 | def __init__(self, types, attributes=None): 88 | self.types = types 89 | self.attributes = attributes or [] 90 | 91 | def __repr__(self): 92 | if self.attributes: 93 | return 'Sum({0.types}, {0.attributes})'.format(self) 94 | else: 95 | return 'Sum({0.types})'.format(self) 96 | 97 | class Product(AST): 98 | def __init__(self, fields, attributes=None): 99 | self.fields = fields 100 | self.attributes = attributes or [] 101 | 102 | def __repr__(self): 103 | if self.attributes: 104 | return 'Product({0.fields}, {0.attributes})'.format(self) 105 | else: 106 | return 'Product({0.fields})'.format(self) 107 | 108 | # A generic visitor for the meta-AST that describes ASDL. This can be used by 109 | # emitters. Note that this visitor does not provide a generic visit method, so a 110 | # subclass needs to define visit methods from visitModule to as deep as the 111 | # interesting node. 112 | # We also define a Check visitor that makes sure the parsed ASDL is well-formed. 113 | 114 | class VisitorBase(object): 115 | """Generic tree visitor for ASTs.""" 116 | def __init__(self): 117 | self.cache = {} 118 | 119 | def visit(self, obj, *args): 120 | klass = obj.__class__ 121 | meth = self.cache.get(klass) 122 | if meth is None: 123 | methname = "visit" + klass.__name__ 124 | meth = getattr(self, methname, None) 125 | self.cache[klass] = meth 126 | if meth: 127 | try: 128 | meth(obj, *args) 129 | except Exception as e: 130 | print("Error visiting %r: %s" % (obj, e)) 131 | raise 132 | 133 | class Check(VisitorBase): 134 | """A visitor that checks a parsed ASDL tree for correctness. 135 | 136 | Errors are printed and accumulated. 137 | """ 138 | def __init__(self): 139 | super(Check, self).__init__() 140 | self.cons = {} 141 | self.errors = 0 142 | self.types = {} 143 | 144 | def visitModule(self, mod): 145 | for dfn in mod.dfns: 146 | self.visit(dfn) 147 | 148 | def visitType(self, type): 149 | self.visit(type.value, str(type.name)) 150 | 151 | def visitSum(self, sum, name): 152 | for t in sum.types: 153 | self.visit(t, name) 154 | 155 | def visitConstructor(self, cons, name): 156 | key = str(cons.name) 157 | conflict = self.cons.get(key) 158 | if conflict is None: 159 | self.cons[key] = name 160 | else: 161 | print('Redefinition of constructor {}'.format(key)) 162 | print('Defined in {} and {}'.format(conflict, name)) 163 | self.errors += 1 164 | for f in cons.fields: 165 | self.visit(f, key) 166 | 167 | def visitField(self, field, name): 168 | key = str(field.type) 169 | l = self.types.setdefault(key, []) 170 | l.append(name) 171 | 172 | def visitProduct(self, prod, name): 173 | for f in prod.fields: 174 | self.visit(f, name) 175 | 176 | def check(mod): 177 | """Check the parsed ASDL tree for correctness. 178 | 179 | Return True if success. For failure, the errors are printed out and False 180 | is returned. 181 | """ 182 | v = Check() 183 | v.visit(mod) 184 | 185 | for t in v.types: 186 | if t not in mod.types and not t in builtin_types: 187 | v.errors += 1 188 | uses = ", ".join(v.types[t]) 189 | print('Undefined type {}, used in {}'.format(t, uses)) 190 | return not v.errors 191 | 192 | # The ASDL parser itself comes next. The only interesting external interface 193 | # here is the top-level parse function. 194 | 195 | def parse(filename): 196 | """Parse ASDL from the given file and return a Module node describing it.""" 197 | with open(filename) as f: 198 | parser = ASDLParser() 199 | return parser.parse(f.read()) 200 | 201 | # Types for describing tokens in an ASDL specification. 202 | class TokenKind: 203 | """TokenKind is provides a scope for enumerated token kinds.""" 204 | (ConstructorId, TypeId, Equals, Comma, Question, Pipe, Asterisk, 205 | LParen, RParen, LBrace, RBrace) = range(11) 206 | 207 | operator_table = { 208 | '=': Equals, ',': Comma, '?': Question, '|': Pipe, '(': LParen, 209 | ')': RParen, '*': Asterisk, '{': LBrace, '}': RBrace} 210 | 211 | Token = namedtuple('Token', 'kind value lineno') 212 | 213 | class ASDLSyntaxError(Exception): 214 | def __init__(self, msg, lineno=None): 215 | self.msg = msg 216 | self.lineno = lineno or '' 217 | 218 | def __str__(self): 219 | return 'Syntax error on line {0.lineno}: {0.msg}'.format(self) 220 | 221 | def tokenize_asdl(buf): 222 | """Tokenize the given buffer. Yield Token objects.""" 223 | for lineno, line in enumerate(buf.splitlines(), 1): 224 | for m in re.finditer(r'\s*(\w+|--.*|.)', line.strip()): 225 | c = m.group(1) 226 | if c[0].isalpha(): 227 | # Some kind of identifier 228 | if c[0].isupper(): 229 | yield Token(TokenKind.ConstructorId, c, lineno) 230 | else: 231 | yield Token(TokenKind.TypeId, c, lineno) 232 | elif c[:2] == '--': 233 | # Comment 234 | break 235 | else: 236 | # Operators 237 | try: 238 | op_kind = TokenKind.operator_table[c] 239 | except KeyError: 240 | raise ASDLSyntaxError('Invalid operator %s' % c, lineno) 241 | yield Token(op_kind, c, lineno) 242 | 243 | class ASDLParser: 244 | """Parser for ASDL files. 245 | 246 | Create, then call the parse method on a buffer containing ASDL. 247 | This is a simple recursive descent parser that uses tokenize_asdl for the 248 | lexing. 249 | """ 250 | def __init__(self): 251 | self._tokenizer = None 252 | self.cur_token = None 253 | 254 | def parse(self, buf): 255 | """Parse the ASDL in the buffer and return an AST with a Module root. 256 | """ 257 | self._tokenizer = tokenize_asdl(buf) 258 | self._advance() 259 | return self._parse_module() 260 | 261 | def _parse_module(self): 262 | if self._at_keyword('module'): 263 | self._advance() 264 | else: 265 | raise ASDLSyntaxError( 266 | 'Expected "module" (found {})'.format(self.cur_token.value), 267 | self.cur_token.lineno) 268 | name = self._match(self._id_kinds) 269 | self._match(TokenKind.LBrace) 270 | defs = self._parse_definitions() 271 | self._match(TokenKind.RBrace) 272 | return Module(name, defs) 273 | 274 | def _parse_definitions(self): 275 | defs = [] 276 | while self.cur_token.kind == TokenKind.TypeId: 277 | typename = self._advance() 278 | self._match(TokenKind.Equals) 279 | type = self._parse_type() 280 | defs.append(Type(typename, type)) 281 | return defs 282 | 283 | def _parse_type(self): 284 | if self.cur_token.kind == TokenKind.LParen: 285 | # If we see a (, it's a product 286 | return self._parse_product() 287 | else: 288 | # Otherwise it's a sum. Look for ConstructorId 289 | sumlist = [Constructor(self._match(TokenKind.ConstructorId), 290 | self._parse_optional_fields())] 291 | while self.cur_token.kind == TokenKind.Pipe: 292 | # More constructors 293 | self._advance() 294 | sumlist.append(Constructor( 295 | self._match(TokenKind.ConstructorId), 296 | self._parse_optional_fields())) 297 | return Sum(sumlist, self._parse_optional_attributes()) 298 | 299 | def _parse_product(self): 300 | return Product(self._parse_fields(), self._parse_optional_attributes()) 301 | 302 | def _parse_fields(self): 303 | fields = [] 304 | self._match(TokenKind.LParen) 305 | while self.cur_token.kind == TokenKind.TypeId: 306 | typename = self._advance() 307 | is_seq, is_opt = self._parse_optional_field_quantifier() 308 | id = (self._advance() if self.cur_token.kind in self._id_kinds 309 | else None) 310 | fields.append(Field(typename, id, seq=is_seq, opt=is_opt)) 311 | if self.cur_token.kind == TokenKind.RParen: 312 | break 313 | elif self.cur_token.kind == TokenKind.Comma: 314 | self._advance() 315 | self._match(TokenKind.RParen) 316 | return fields 317 | 318 | def _parse_optional_fields(self): 319 | if self.cur_token.kind == TokenKind.LParen: 320 | return self._parse_fields() 321 | else: 322 | return None 323 | 324 | def _parse_optional_attributes(self): 325 | if self._at_keyword('attributes'): 326 | self._advance() 327 | return self._parse_fields() 328 | else: 329 | return None 330 | 331 | def _parse_optional_field_quantifier(self): 332 | is_seq, is_opt = False, False 333 | if self.cur_token.kind == TokenKind.Asterisk: 334 | is_seq = True 335 | self._advance() 336 | elif self.cur_token.kind == TokenKind.Question: 337 | is_opt = True 338 | self._advance() 339 | return is_seq, is_opt 340 | 341 | def _advance(self): 342 | """ Return the value of the current token and read the next one into 343 | self.cur_token. 344 | """ 345 | cur_val = None if self.cur_token is None else self.cur_token.value 346 | try: 347 | self.cur_token = next(self._tokenizer) 348 | except StopIteration: 349 | self.cur_token = None 350 | return cur_val 351 | 352 | _id_kinds = (TokenKind.ConstructorId, TokenKind.TypeId) 353 | 354 | def _match(self, kind): 355 | """The 'match' primitive of RD parsers. 356 | 357 | * Verifies that the current token is of the given kind (kind can 358 | be a tuple, in which the kind must match one of its members). 359 | * Returns the value of the current token 360 | * Reads in the next token 361 | """ 362 | if (isinstance(kind, tuple) and self.cur_token.kind in kind or 363 | self.cur_token.kind == kind 364 | ): 365 | value = self.cur_token.value 366 | self._advance() 367 | return value 368 | else: 369 | raise ASDLSyntaxError( 370 | 'Unmatched {} (found {})'.format(kind, self.cur_token.kind), 371 | self.cur_token.lineno) 372 | 373 | def _at_keyword(self, keyword): 374 | return (self.cur_token.kind == TokenKind.TypeId and 375 | self.cur_token.value == keyword) 376 | -------------------------------------------------------------------------------- /ast/asdl_java.py: -------------------------------------------------------------------------------- 1 | """Generate Java code from an ASDL description.""" 2 | 3 | # TO DO 4 | # handle fields that have a type but no name 5 | 6 | import os 7 | import sys 8 | import traceback 9 | 10 | import asdl 11 | 12 | TABSIZE = 4 13 | MAX_COL = 76 14 | 15 | 16 | def reflow_lines(s, depth): 17 | """Reflow the line s indented depth tabs. 18 | 19 | Return a sequence of lines where no line extends beyond MAX_COL 20 | when properly indented. The first line is properly indented based 21 | exclusively on depth * TABSIZE. All following lines -- these are 22 | the reflowed lines generated by this function -- start at the same 23 | column as the first character beyond the opening { in the first 24 | line. 25 | """ 26 | size = MAX_COL - depth * TABSIZE 27 | if len(s) < size: 28 | return [s] 29 | 30 | lines = [] 31 | cur = s 32 | padding = "" 33 | while len(cur) > size: 34 | i = cur.rfind(' ', 0, size) 35 | assert i != -1, "Impossible line to reflow: %s" % s.repr() 36 | lines.append(padding + cur[:i]) 37 | if len(lines) == 1: 38 | # find new size based on brace 39 | j = cur.find('{', 0, i) 40 | if j >= 0: 41 | j += 2 # account for the brace and the space after it 42 | size -= j 43 | padding = " " * j 44 | cur = cur[i+1:] 45 | else: 46 | lines.append(padding + cur) 47 | return lines 48 | 49 | 50 | class EmitVisitor(asdl.VisitorBase): 51 | """Visit that emits lines""" 52 | 53 | def __init__(self, direction): 54 | self.direction = direction 55 | super(EmitVisitor, self).__init__() 56 | 57 | def open(self, name, refersToPythonNode=1, useDataOutput=0): 58 | self.file = open(self.direction + "%s.java" % name, "w") 59 | print >> self.file, "// Autogenerated AST node" 60 | print >> self.file, 'package pers.xia.jpython.ast;' 61 | print >> self.file, 'import pers.xia.jpython.object.PyObject;' 62 | # if refersToPythonNode: 63 | # print >> self.file, 'import pers.xia.jpython.parser.Node;' 64 | if useDataOutput: 65 | print >> self.file, 'import java.io.DataOutputStream;' 66 | print >> self.file, 'import java.io.IOException;' 67 | print >> self.file 68 | 69 | def close(self): 70 | self.file.close() 71 | 72 | def emit(self, s, depth): 73 | # XXX reflow long lines? 74 | lines = reflow_lines(s, depth) 75 | for line in lines: 76 | line = (" " * TABSIZE * depth) + line + "\n" 77 | self.file.write(line) 78 | 79 | 80 | # This step will add a 'simple' boolean attribute to all Sum and Product 81 | # nodes and add a 'typedef' link to each Field node that points to the 82 | # Sum or Product node that defines the field. 83 | 84 | 85 | class AnalyzeVisitor(EmitVisitor): 86 | index = 0 87 | 88 | def makeIndex(self): 89 | self.index += 1 90 | return self.index 91 | 92 | def visitModule(self, mod): 93 | self.types = {} 94 | for dfn in mod.dfns: 95 | self.types[str(dfn.name)] = dfn.value 96 | for dfn in mod.dfns: 97 | self.visit(dfn) 98 | 99 | def visitType(self, type, depth=0): 100 | self.visit(type.value, type.name, depth) 101 | 102 | def visitSum(self, sum, name, depth): 103 | sum.simple = 1 104 | for t in sum.types: 105 | if t.fields: 106 | sum.simple = 0 107 | break 108 | for t in sum.types: 109 | if not sum.simple: 110 | t.index = self.makeIndex() 111 | self.visit(t, name, depth) 112 | 113 | def visitProduct(self, product, name, depth): 114 | product.simple = 0 115 | product.index = self.makeIndex() 116 | for f in product.fields: 117 | self.visit(f, depth + 1) 118 | 119 | def visitConstructor(self, cons, name, depth): 120 | for f in cons.fields: 121 | self.visit(f, depth + 1) 122 | 123 | def visitField(self, field, depth): 124 | field.typedef = self.types.get(str(field.type)) 125 | 126 | 127 | # The code generator itself. 128 | 129 | class JavaVisitor(EmitVisitor): 130 | def visitModule(self, mod): 131 | for dfn in mod.dfns: 132 | self.visit(dfn) 133 | 134 | def visitType(self, type, depth=0): 135 | self.visit(type.value, type.name, depth) 136 | 137 | def visitSum(self, sum, name, depth): 138 | if sum.simple: 139 | self.simple_sum(sum, name, depth) 140 | else: 141 | self.sum_with_constructor(sum, name, depth) 142 | 143 | def simple_sum(self, sum, name, depth): 144 | self.open("%sType" % name, refersToPythonNode=0) 145 | self.emit("public enum %(name)sType {" % locals(), depth) 146 | self.emit('UNDEFINED,', depth+1) 147 | for type in sum.types: 148 | self.emit('%s,' % type.name, depth+1) 149 | self.emit("}", depth) 150 | self.close() 151 | 152 | def sum_with_constructor(self, sum, name, depth): 153 | self.open("%sType" % name) 154 | # self.emit("public abstract class %(name)sType extends Node {" % 155 | self.emit("public abstract class %(name)sType{" % 156 | locals(), depth) 157 | for attribute in sum.attributes: 158 | self.emit('public %s %s;' % (attribute.type, attribute.name), depth + 1) 159 | self.emit("}", depth) 160 | self.close() 161 | for t in sum.types: 162 | self.visit(t, name, depth, sum.attributes) 163 | 164 | def visitProduct(self, product, name, depth): 165 | self.open("%sType" % name, useDataOutput=1) 166 | # self.emit("public class %(name)sType extends Node {" % locals(), 167 | self.emit("public class %(name)sType{" % locals(), 168 | depth) 169 | for f in product.fields: 170 | self.visit(f, depth + 1) 171 | for attr in product.attributes: 172 | self.visit(attr, depth + 1) 173 | self.emit("", depth) 174 | 175 | self.javaMethods(product, name, "%sType" % name, product.fields, 176 | depth+1, product.attributes) 177 | 178 | self.emit("}", depth) 179 | self.close() 180 | 181 | def visitConstructor(self, cons, name, depth, attributes): 182 | self.open(cons.name, useDataOutput=1) 183 | enums = [] 184 | for f in cons.fields: 185 | if f.typedef and f.typedef.simple: 186 | enums.append("%sType" % f.type) 187 | # if enums: 188 | if False: 189 | s = "implements %s " % ", ".join(enums) 190 | else: 191 | s = "" 192 | self.emit("public class %s extends %sType %s{" % 193 | (cons.name, name, s), depth) 194 | for f in cons.fields: 195 | self.visit(f, depth + 1) 196 | self.emit("", depth) 197 | 198 | self.javaMethods(cons, cons.name, cons.name, cons.fields, depth+1, attributes) 199 | 200 | self.emit("}", depth) 201 | self.close() 202 | 203 | def javaMethods(self, type, clsname, ctorname, fields, depth, attributes): 204 | # The java ctors 205 | fpargs = ", ".join([self.fieldDef(f) for f in fields]) 206 | if attributes: 207 | if fpargs: 208 | fpargs += "," 209 | else: 210 | fpargs = "" 211 | fpargs += ", ".join([self.fieldDef(a) for a in attributes]) 212 | 213 | self.emit("public %s(%s) {" % (ctorname, fpargs), depth) 214 | for f in fields: 215 | self.emit("this.%s = %s;" % (f.name, f.name), depth+1) 216 | if attributes: 217 | for attr in attributes: 218 | self.emit("this.%s = %s;" % (attr.name, attr.name), depth+1) 219 | self.emit("}", depth) 220 | self.emit("", 0) 221 | 222 | # if fpargs: 223 | # fpargs += ", " 224 | # self.emit("public %s(%sNode parent) {" % 225 | # (ctorname, fpargs), depth) 226 | # self.emit("this(%s);" % 227 | # ", ".join([str(f.name) for f in fields]), depth+1) 228 | # self.emit("this.lineno = parent.lineNo;", depth+1) 229 | # self.emit("this.col_offset = parent.colOffset;", depth+1) 230 | # self.emit("}", depth) 231 | # self.emit("", 0) 232 | 233 | # The toString() method 234 | self.emit("public String toString() {", depth) 235 | self.emit("return \"%s\";" % ctorname, depth+1) 236 | self.emit("}", depth) 237 | self.emit("", 0) 238 | 239 | # The pickle() method 240 | self.emit("public void pickle(DataOutputStream ostream) " + 241 | "throws IOException {", depth) 242 | # self.emit("pickleThis(%s, ostream);" % type.index, depth+1) 243 | # for f in fields: 244 | # self.emit("pickleThis(this.%s, ostream);" % f.name, depth+1) 245 | self.emit("}", depth) 246 | self.emit("", 0) 247 | 248 | # The accept() method 249 | self.emit("public Object accept(VisitorIF visitor) throws Exception {", 250 | depth) 251 | if clsname == ctorname: 252 | self.emit('return visitor.visit%s(this);' % clsname, depth+1) 253 | else: 254 | self.emit('traverse(visitor);', depth+1) 255 | self.emit('return null;', depth+1) 256 | self.emit("}", depth) 257 | self.emit("", 0) 258 | 259 | # The visitChildren() method 260 | self.emit("public void traverse(VisitorIF visitor) throws Exception {", 261 | depth) 262 | # for f in fields: 263 | # if self.bltinnames.has_key(str(f.type)): 264 | # continue 265 | # if f.typedef and f.typedef.simple: 266 | # continue 267 | # if f.seq: 268 | # self.emit('if (%s != null) {' % f.name, depth+1) 269 | # self.emit('for (int i = 0; i < %s.length; i++) {' % f.name, 270 | # depth+2) 271 | # self.emit('if (%s[i] != null)' % f.name, depth+3) 272 | # self.emit('%s[i].accept(visitor);' % f.name, depth+4) 273 | # self.emit('}', depth+2) 274 | # self.emit('}', depth+1) 275 | # else: 276 | # self.emit('if (%s != null)' % f.name, depth+1) 277 | # self.emit('%s.accept(visitor);' % f.name, depth+2) 278 | self.emit('}', depth) 279 | self.emit("", 0) 280 | 281 | def visitField(self, field, depth): 282 | self.emit("public %s;" % self.fieldDef(field), depth) 283 | 284 | bltinnames = { 285 | 'int': 'int', 286 | 'identifier': 'String', 287 | 'string': 'PyObject', 288 | 'object': 'PyObject', # was PyObject 289 | 'bytes': 'PyObject', 290 | 'singleton': 'PyObject', 291 | } 292 | 293 | def fieldDef(self, field): 294 | jtype = str(field.type) 295 | # if field.typedef and field.typedef.simple: 296 | # jtype = 'int' 297 | # else: 298 | jtype = self.bltinnames.get(jtype, jtype + 'Type') 299 | name = field.name 300 | # seq = field.seq and "[]" or "" 301 | seq = "" 302 | if field.seq: 303 | return "java.util.List<%s> %s" % (jtype, name) 304 | else: 305 | return "%(jtype)s%(seq)s %(name)s" % locals() 306 | 307 | 308 | class VisitorVisitor(EmitVisitor): 309 | def __init__(self, direction): 310 | EmitVisitor.__init__(self, direction) 311 | self.ctors = [] 312 | 313 | def visitModule(self, mod): 314 | for dfn in mod.dfns: 315 | self.visit(dfn) 316 | self.open("VisitorIF", refersToPythonNode=0) 317 | self.emit('public interface VisitorIF {', 0) 318 | for ctor in self.ctors: 319 | self.emit("public Object visit%s(%s node) throws Exception;" % 320 | (ctor, ctor), 1) 321 | self.emit('}', 0) 322 | self.close() 323 | 324 | ''' 325 | self.open("VisitorBase") 326 | self.emit('public abstract class VisitorBase implements VisitorIF {', 0) 327 | for ctor in self.ctors: 328 | self.emit("public Object visit%s(%s node) throws Exception {" % 329 | (ctor, ctor), 1) 330 | self.emit("Object ret = unhandled_node(node);", 2) 331 | self.emit("traverse(node);", 2) 332 | self.emit("return ret;", 2) 333 | self.emit('}', 1) 334 | self.emit('', 0) 335 | 336 | self.emit("abstract protected Object unhandled_node(Node node) throws Exception;", 1) 337 | self.emit("abstract public void traverse(Node node) throws Exception;", 1) 338 | self.emit('}', 0) 339 | self.close() 340 | ''' 341 | 342 | def visitType(self, type, depth=1): 343 | self.visit(type.value, type.name, depth) 344 | 345 | def visitSum(self, sum, name, depth): 346 | if not sum.simple: 347 | for t in sum.types: 348 | self.visit(t, name, depth) 349 | 350 | def visitProduct(self, product, name, depth): 351 | pass 352 | 353 | def visitConstructor(self, cons, name, depth): 354 | self.ctors.append(cons.name) 355 | 356 | 357 | class ChainOfVisitors: 358 | def __init__(self, *visitors): 359 | self.visitors = visitors 360 | 361 | def visit(self, object): 362 | for v in self.visitors: 363 | v.visit(object) 364 | 365 | if __name__ == "__main__": 366 | mod = asdl.parse(sys.argv[1]) 367 | direction = "../src/pers/xia/jpython/ast/" 368 | if len(sys.argv) > 2: 369 | direction = sys.argv[2] 370 | if not asdl.check(mod): 371 | sys.exit(1) 372 | c = ChainOfVisitors(AnalyzeVisitor(direction), 373 | JavaVisitor(direction), 374 | VisitorVisitor(direction)) 375 | c.visit(mod) 376 | -------------------------------------------------------------------------------- /grammar/Grammar: -------------------------------------------------------------------------------- 1 | # Grammar for Python 2 | 3 | # Note: Changing the grammar specified in this file will most likely 4 | # require corresponding changes in the parser module 5 | # (../Modules/parsermodule.c). If you can't make the changes to 6 | # that module yourself, please co-ordinate the required changes 7 | # with someone who can; ask around on python-dev for help. Fred 8 | # Drake will probably be listening there. 9 | 10 | # NOTE WELL: You should also follow all the steps listed at 11 | # https://docs.python.org/devguide/grammar.html 12 | 13 | # Start symbols for the grammar: 14 | # single_input is a single interactive statement; 15 | # file_input is a module or sequence of commands read from an input file; 16 | # eval_input is the input for the eval() functions. 17 | # NB: compound_stmt in single_input is followed by extra NEWLINE! 18 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE 19 | file_input: (NEWLINE | stmt)* ENDMARKER 20 | eval_input: testlist NEWLINE* ENDMARKER 21 | 22 | decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE 23 | decorators: decorator+ 24 | decorated: decorators (classdef | funcdef | async_funcdef) 25 | 26 | async_funcdef: ASYNC funcdef 27 | funcdef: 'def' NAME parameters ['->' test] ':' suite 28 | 29 | parameters: '(' [typedargslist] ')' 30 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' 31 | ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]] 32 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) 33 | tfpdef: NAME [':' test] 34 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' 35 | ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]] 36 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) 37 | vfpdef: NAME 38 | 39 | stmt: simple_stmt | compound_stmt 40 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE 41 | small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | 42 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt) 43 | expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) | 44 | ('=' (yield_expr|testlist_star_expr))*) 45 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] 46 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | 47 | '<<=' | '>>=' | '**=' | '//=') 48 | # For normal assignments, additional restrictions enforced by the interpreter 49 | del_stmt: 'del' exprlist 50 | pass_stmt: 'pass' 51 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt 52 | break_stmt: 'break' 53 | continue_stmt: 'continue' 54 | return_stmt: 'return' [testlist] 55 | yield_stmt: yield_expr 56 | raise_stmt: 'raise' [test ['from' test]] 57 | import_stmt: import_name | import_from 58 | import_name: 'import' dotted_as_names 59 | # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS 60 | import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) 61 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) 62 | import_as_name: NAME ['as' NAME] 63 | dotted_as_name: dotted_name ['as' NAME] 64 | import_as_names: import_as_name (',' import_as_name)* [','] 65 | dotted_as_names: dotted_as_name (',' dotted_as_name)* 66 | dotted_name: NAME ('.' NAME)* 67 | global_stmt: 'global' NAME (',' NAME)* 68 | nonlocal_stmt: 'nonlocal' NAME (',' NAME)* 69 | assert_stmt: 'assert' test [',' test] 70 | 71 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt 72 | async_stmt: ASYNC (funcdef | with_stmt | for_stmt) 73 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 74 | while_stmt: 'while' test ':' suite ['else' ':' suite] 75 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] 76 | try_stmt: ('try' ':' suite 77 | ((except_clause ':' suite)+ 78 | ['else' ':' suite] 79 | ['finally' ':' suite] | 80 | 'finally' ':' suite)) 81 | with_stmt: 'with' with_item (',' with_item)* ':' suite 82 | with_item: test ['as' expr] 83 | # NB compile.c makes sure that the default except clause is last 84 | except_clause: 'except' [test ['as' NAME]] 85 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT 86 | 87 | test: or_test ['if' or_test 'else' test] | lambdef 88 | test_nocond: or_test | lambdef_nocond 89 | lambdef: 'lambda' [varargslist] ':' test 90 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond 91 | or_test: and_test ('or' and_test)* 92 | and_test: not_test ('and' not_test)* 93 | not_test: 'not' not_test | comparison 94 | comparison: expr (comp_op expr)* 95 | # <> isn't actually a valid comparison operator in Python. It's here for the 96 | # sake of a __future__ import described in PEP 401 (which really works :-) 97 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 98 | star_expr: '*' expr 99 | expr: xor_expr ('|' xor_expr)* 100 | xor_expr: and_expr ('^' and_expr)* 101 | and_expr: shift_expr ('&' shift_expr)* 102 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* 103 | arith_expr: term (('+'|'-') term)* 104 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* 105 | factor: ('+'|'-'|'~') factor | power 106 | power: atom_expr ['**' factor] 107 | atom_expr: [AWAIT] atom trailer* 108 | atom: ('(' [yield_expr|testlist_comp] ')' | 109 | '[' [testlist_comp] ']' | 110 | '{' [dictorsetmaker] '}' | 111 | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False') 112 | testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) 113 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 114 | subscriptlist: subscript (',' subscript)* [','] 115 | subscript: test | [test] ':' [test] [sliceop] 116 | sliceop: ':' [test] 117 | exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] 118 | testlist: test (',' test)* [','] 119 | dictorsetmaker: ( ((test ':' test | '**' expr) 120 | (comp_for | (',' (test ':' test | '**' expr))* [','])) | 121 | ((test | star_expr) 122 | (comp_for | (',' (test | star_expr))* [','])) ) 123 | 124 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite 125 | 126 | arglist: argument (',' argument)* [','] 127 | 128 | # The reason that keywords are test nodes instead of NAME is that using NAME 129 | # results in an ambiguity. ast.c makes sure it's a NAME. 130 | # "test '=' test" is really "keyword '=' test", but we have no such token. 131 | # These need to be in a single rule to avoid grammar that is ambiguous 132 | # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, 133 | # we explicitly match '*' here, too, to give it proper precedence. 134 | # Illegal combinations and orderings are blocked in ast.c: 135 | # multiple (test comp_for) arguements are blocked; keyword unpackings 136 | # that precede iterable unpackings are blocked; etc. 137 | argument: ( test [comp_for] | 138 | test '=' test | 139 | '**' test | 140 | '*' test ) 141 | 142 | comp_iter: comp_for | comp_if 143 | comp_for: 'for' exprlist 'in' or_test [comp_iter] 144 | comp_if: 'if' test_nocond [comp_iter] 145 | 146 | # not used in grammar, but may appear in "node" passed from Parser to Compiler 147 | encoding_decl: NAME 148 | 149 | yield_expr: 'yield' [yield_arg] 150 | yield_arg: 'from' test | testlist 151 | -------------------------------------------------------------------------------- /source/test.py: -------------------------------------------------------------------------------- 1 | # hello world 2 | print("Hello world!") 3 | 4 | 5 | # call method 6 | def add(a, b): 7 | print("call add()") 8 | return a + b 9 | 10 | print(add(1, 2)) 11 | 12 | 13 | # splice two string 14 | print("abcdefghijklmnopqrstuvexyz" + '1234567890') 15 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Assert.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Assert extends stmtType { 8 | public exprType test; 9 | public exprType msg; 10 | 11 | public Assert(exprType test, exprType msg,int lineno, int col_offset) { 12 | this.test = test; 13 | this.msg = msg; 14 | this.lineno = lineno; 15 | this.col_offset = col_offset; 16 | } 17 | 18 | public String toString() { 19 | return "Assert"; 20 | } 21 | 22 | public void pickle(DataOutputStream ostream) throws IOException { 23 | } 24 | 25 | public Object accept(VisitorIF visitor) throws Exception { 26 | return visitor.visitAssert(this); 27 | } 28 | 29 | public void traverse(VisitorIF visitor) throws Exception { 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Assign.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Assign extends stmtType { 8 | public java.util.List targets; 9 | public exprType value; 10 | 11 | public Assign(java.util.List targets, exprType value,int 12 | lineno, int col_offset) { 13 | this.targets = targets; 14 | this.value = value; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "Assign"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitAssign(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/AsyncFor.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class AsyncFor extends stmtType { 8 | public exprType target; 9 | public exprType iter; 10 | public java.util.List body; 11 | public java.util.List orelse; 12 | 13 | public AsyncFor(exprType target, exprType iter, 14 | java.util.List body, java.util.List orelse,int 15 | lineno, int col_offset) { 16 | this.target = target; 17 | this.iter = iter; 18 | this.body = body; 19 | this.orelse = orelse; 20 | this.lineno = lineno; 21 | this.col_offset = col_offset; 22 | } 23 | 24 | public String toString() { 25 | return "AsyncFor"; 26 | } 27 | 28 | public void pickle(DataOutputStream ostream) throws IOException { 29 | } 30 | 31 | public Object accept(VisitorIF visitor) throws Exception { 32 | return visitor.visitAsyncFor(this); 33 | } 34 | 35 | public void traverse(VisitorIF visitor) throws Exception { 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/AsyncFunctionDef.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class AsyncFunctionDef extends stmtType { 8 | public String name; 9 | public argumentsType args; 10 | public java.util.List body; 11 | public java.util.List decorator_list; 12 | public exprType returns; 13 | 14 | public AsyncFunctionDef(String name, argumentsType args, 15 | java.util.List body, java.util.List decorator_list, 16 | exprType returns,int lineno, int col_offset) { 17 | this.name = name; 18 | this.args = args; 19 | this.body = body; 20 | this.decorator_list = decorator_list; 21 | this.returns = returns; 22 | this.lineno = lineno; 23 | this.col_offset = col_offset; 24 | } 25 | 26 | public String toString() { 27 | return "AsyncFunctionDef"; 28 | } 29 | 30 | public void pickle(DataOutputStream ostream) throws IOException { 31 | } 32 | 33 | public Object accept(VisitorIF visitor) throws Exception { 34 | return visitor.visitAsyncFunctionDef(this); 35 | } 36 | 37 | public void traverse(VisitorIF visitor) throws Exception { 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/AsyncWith.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class AsyncWith extends stmtType { 8 | public java.util.List items; 9 | public java.util.List body; 10 | 11 | public AsyncWith(java.util.List items, 12 | java.util.List body,int lineno, int col_offset) { 13 | this.items = items; 14 | this.body = body; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "AsyncWith"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitAsyncWith(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Attribute.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Attribute extends exprType { 8 | public exprType value; 9 | public String attr; 10 | public expr_contextType ctx; 11 | 12 | public Attribute(exprType value, String attr, expr_contextType ctx,int 13 | lineno, int col_offset) { 14 | this.value = value; 15 | this.attr = attr; 16 | this.ctx = ctx; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "Attribute"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitAttribute(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/AugAssign.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class AugAssign extends stmtType { 8 | public exprType target; 9 | public operatorType op; 10 | public exprType value; 11 | 12 | public AugAssign(exprType target, operatorType op, exprType value,int 13 | lineno, int col_offset) { 14 | this.target = target; 15 | this.op = op; 16 | this.value = value; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "AugAssign"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitAugAssign(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Await.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Await extends exprType { 8 | public exprType value; 9 | 10 | public Await(exprType value,int lineno, int col_offset) { 11 | this.value = value; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Await"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitAwait(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/BinOp.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class BinOp extends exprType { 8 | public exprType left; 9 | public operatorType op; 10 | public exprType right; 11 | 12 | public BinOp(exprType left, operatorType op, exprType right,int lineno, 13 | int col_offset) { 14 | this.left = left; 15 | this.op = op; 16 | this.right = right; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "BinOp"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitBinOp(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/BoolOp.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class BoolOp extends exprType { 8 | public boolopType op; 9 | public java.util.List values; 10 | 11 | public BoolOp(boolopType op, java.util.List values,int 12 | lineno, int col_offset) { 13 | this.op = op; 14 | this.values = values; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "BoolOp"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitBoolOp(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Break.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Break extends stmtType { 8 | 9 | public Break(int lineno, int col_offset) { 10 | this.lineno = lineno; 11 | this.col_offset = col_offset; 12 | } 13 | 14 | public String toString() { 15 | return "Break"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitBreak(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Bytes.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Bytes extends exprType { 8 | public PyObject s; 9 | 10 | public Bytes(PyObject s,int lineno, int col_offset) { 11 | this.s = s; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Bytes"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitBytes(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Call.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Call extends exprType { 8 | public exprType func; 9 | public java.util.List args; 10 | public java.util.List keywords; 11 | 12 | public Call(exprType func, java.util.List args, 13 | java.util.List keywords,int lineno, int col_offset) { 14 | this.func = func; 15 | this.args = args; 16 | this.keywords = keywords; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "Call"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitCall(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/ClassDef.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class ClassDef extends stmtType { 8 | public String name; 9 | public java.util.List bases; 10 | public java.util.List keywords; 11 | public java.util.List body; 12 | public java.util.List decorator_list; 13 | 14 | public ClassDef(String name, java.util.List bases, 15 | java.util.List keywords, java.util.List body, 16 | java.util.List decorator_list,int lineno, int col_offset) { 17 | this.name = name; 18 | this.bases = bases; 19 | this.keywords = keywords; 20 | this.body = body; 21 | this.decorator_list = decorator_list; 22 | this.lineno = lineno; 23 | this.col_offset = col_offset; 24 | } 25 | 26 | public String toString() { 27 | return "ClassDef"; 28 | } 29 | 30 | public void pickle(DataOutputStream ostream) throws IOException { 31 | } 32 | 33 | public Object accept(VisitorIF visitor) throws Exception { 34 | return visitor.visitClassDef(this); 35 | } 36 | 37 | public void traverse(VisitorIF visitor) throws Exception { 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Compare.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Compare extends exprType { 8 | public exprType left; 9 | public java.util.List ops; 10 | public java.util.List comparators; 11 | 12 | public Compare(exprType left, java.util.List ops, 13 | java.util.List comparators,int lineno, int col_offset) { 14 | this.left = left; 15 | this.ops = ops; 16 | this.comparators = comparators; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "Compare"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitCompare(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Continue.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Continue extends stmtType { 8 | 9 | public Continue(int lineno, int col_offset) { 10 | this.lineno = lineno; 11 | this.col_offset = col_offset; 12 | } 13 | 14 | public String toString() { 15 | return "Continue"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitContinue(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Delete.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Delete extends stmtType { 8 | public java.util.List targets; 9 | 10 | public Delete(java.util.List targets,int lineno, int 11 | col_offset) { 12 | this.targets = targets; 13 | this.lineno = lineno; 14 | this.col_offset = col_offset; 15 | } 16 | 17 | public String toString() { 18 | return "Delete"; 19 | } 20 | 21 | public void pickle(DataOutputStream ostream) throws IOException { 22 | } 23 | 24 | public Object accept(VisitorIF visitor) throws Exception { 25 | return visitor.visitDelete(this); 26 | } 27 | 28 | public void traverse(VisitorIF visitor) throws Exception { 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Dict.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Dict extends exprType { 8 | public java.util.List keys; 9 | public java.util.List values; 10 | 11 | public Dict(java.util.List keys, java.util.List 12 | values,int lineno, int col_offset) { 13 | this.keys = keys; 14 | this.values = values; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "Dict"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitDict(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/DictComp.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class DictComp extends exprType { 8 | public exprType key; 9 | public exprType value; 10 | public java.util.List generators; 11 | 12 | public DictComp(exprType key, exprType value, 13 | java.util.List generators,int lineno, int 14 | col_offset) { 15 | this.key = key; 16 | this.value = value; 17 | this.generators = generators; 18 | this.lineno = lineno; 19 | this.col_offset = col_offset; 20 | } 21 | 22 | public String toString() { 23 | return "DictComp"; 24 | } 25 | 26 | public void pickle(DataOutputStream ostream) throws IOException { 27 | } 28 | 29 | public Object accept(VisitorIF visitor) throws Exception { 30 | return visitor.visitDictComp(this); 31 | } 32 | 33 | public void traverse(VisitorIF visitor) throws Exception { 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Ellipsis.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Ellipsis extends exprType { 8 | 9 | public Ellipsis(int lineno, int col_offset) { 10 | this.lineno = lineno; 11 | this.col_offset = col_offset; 12 | } 13 | 14 | public String toString() { 15 | return "Ellipsis"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitEllipsis(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/ExceptHandler.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class ExceptHandler extends excepthandlerType { 8 | public exprType type; 9 | public String name; 10 | public java.util.List body; 11 | 12 | public ExceptHandler(exprType type, String name, 13 | java.util.List body,int lineno, int col_offset) { 14 | this.type = type; 15 | this.name = name; 16 | this.body = body; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "ExceptHandler"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitExceptHandler(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Expr.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Expr extends stmtType { 8 | public exprType value; 9 | 10 | public Expr(exprType value,int lineno, int col_offset) { 11 | this.value = value; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Expr"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitExpr(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Expression.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Expression extends modType { 8 | public exprType body; 9 | 10 | public Expression(exprType body) { 11 | this.body = body; 12 | } 13 | 14 | public String toString() { 15 | return "Expression"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitExpression(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/ExtSlice.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class ExtSlice extends sliceType { 8 | public java.util.List dims; 9 | 10 | public ExtSlice(java.util.List dims) { 11 | this.dims = dims; 12 | } 13 | 14 | public String toString() { 15 | return "ExtSlice"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitExtSlice(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/For.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class For extends stmtType { 8 | public exprType target; 9 | public exprType iter; 10 | public java.util.List body; 11 | public java.util.List orelse; 12 | 13 | public For(exprType target, exprType iter, java.util.List 14 | body, java.util.List orelse,int lineno, int col_offset) { 15 | this.target = target; 16 | this.iter = iter; 17 | this.body = body; 18 | this.orelse = orelse; 19 | this.lineno = lineno; 20 | this.col_offset = col_offset; 21 | } 22 | 23 | public String toString() { 24 | return "For"; 25 | } 26 | 27 | public void pickle(DataOutputStream ostream) throws IOException { 28 | } 29 | 30 | public Object accept(VisitorIF visitor) throws Exception { 31 | return visitor.visitFor(this); 32 | } 33 | 34 | public void traverse(VisitorIF visitor) throws Exception { 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/FunctionDef.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class FunctionDef extends stmtType { 8 | public String name; 9 | public argumentsType args; 10 | public java.util.List body; 11 | public java.util.List decorator_list; 12 | public exprType returns; 13 | 14 | public FunctionDef(String name, argumentsType args, 15 | java.util.List body, java.util.List decorator_list, 16 | exprType returns,int lineno, int col_offset) { 17 | this.name = name; 18 | this.args = args; 19 | this.body = body; 20 | this.decorator_list = decorator_list; 21 | this.returns = returns; 22 | this.lineno = lineno; 23 | this.col_offset = col_offset; 24 | } 25 | 26 | public String toString() { 27 | return "FunctionDef"; 28 | } 29 | 30 | public void pickle(DataOutputStream ostream) throws IOException { 31 | } 32 | 33 | public Object accept(VisitorIF visitor) throws Exception { 34 | return visitor.visitFunctionDef(this); 35 | } 36 | 37 | public void traverse(VisitorIF visitor) throws Exception { 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/GeneratorExp.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class GeneratorExp extends exprType { 8 | public exprType elt; 9 | public java.util.List generators; 10 | 11 | public GeneratorExp(exprType elt, java.util.List 12 | generators,int lineno, int col_offset) { 13 | this.elt = elt; 14 | this.generators = generators; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "GeneratorExp"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitGeneratorExp(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Global.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Global extends stmtType { 8 | public java.util.List names; 9 | 10 | public Global(java.util.List names,int lineno, int col_offset) { 11 | this.names = names; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Global"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitGlobal(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/If.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class If extends stmtType { 8 | public exprType test; 9 | public java.util.List body; 10 | public java.util.List orelse; 11 | 12 | public If(exprType test, java.util.List body, 13 | java.util.List orelse,int lineno, int col_offset) { 14 | this.test = test; 15 | this.body = body; 16 | this.orelse = orelse; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "If"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitIf(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/IfExp.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class IfExp extends exprType { 8 | public exprType test; 9 | public exprType body; 10 | public exprType orelse; 11 | 12 | public IfExp(exprType test, exprType body, exprType orelse,int lineno, 13 | int col_offset) { 14 | this.test = test; 15 | this.body = body; 16 | this.orelse = orelse; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "IfExp"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitIfExp(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Import.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Import extends stmtType { 8 | public java.util.List names; 9 | 10 | public Import(java.util.List names,int lineno, int 11 | col_offset) { 12 | this.names = names; 13 | this.lineno = lineno; 14 | this.col_offset = col_offset; 15 | } 16 | 17 | public String toString() { 18 | return "Import"; 19 | } 20 | 21 | public void pickle(DataOutputStream ostream) throws IOException { 22 | } 23 | 24 | public Object accept(VisitorIF visitor) throws Exception { 25 | return visitor.visitImport(this); 26 | } 27 | 28 | public void traverse(VisitorIF visitor) throws Exception { 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/ImportFrom.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class ImportFrom extends stmtType { 8 | public String module; 9 | public java.util.List names; 10 | public int level; 11 | 12 | public ImportFrom(String module, java.util.List names, int 13 | level,int lineno, int col_offset) { 14 | this.module = module; 15 | this.names = names; 16 | this.level = level; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "ImportFrom"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitImportFrom(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Index.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Index extends sliceType { 8 | public exprType value; 9 | 10 | public Index(exprType value) { 11 | this.value = value; 12 | } 13 | 14 | public String toString() { 15 | return "Index"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitIndex(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Interactive.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Interactive extends modType { 8 | public java.util.List body; 9 | 10 | public Interactive(java.util.List body) { 11 | this.body = body; 12 | } 13 | 14 | public String toString() { 15 | return "Interactive"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitInteractive(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Lambda.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Lambda extends exprType { 8 | public argumentsType args; 9 | public exprType body; 10 | 11 | public Lambda(argumentsType args, exprType body,int lineno, int 12 | col_offset) { 13 | this.args = args; 14 | this.body = body; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "Lambda"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitLambda(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/List.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class List extends exprType { 8 | public java.util.List elts; 9 | public expr_contextType ctx; 10 | 11 | public List(java.util.List elts, expr_contextType ctx,int 12 | lineno, int col_offset) { 13 | this.elts = elts; 14 | this.ctx = ctx; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "List"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitList(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/ListComp.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class ListComp extends exprType { 8 | public exprType elt; 9 | public java.util.List generators; 10 | 11 | public ListComp(exprType elt, java.util.List 12 | generators,int lineno, int col_offset) { 13 | this.elt = elt; 14 | this.generators = generators; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "ListComp"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitListComp(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Module.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Module extends modType { 8 | public java.util.List body; 9 | 10 | public Module(java.util.List body) { 11 | this.body = body; 12 | } 13 | 14 | public String toString() { 15 | return "Module"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitModule(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Name.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Name extends exprType { 8 | public String id; 9 | public expr_contextType ctx; 10 | 11 | public Name(String id, expr_contextType ctx,int lineno, int col_offset) 12 | { 13 | this.id = id; 14 | this.ctx = ctx; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "Name"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitName(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/NameConstant.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class NameConstant extends exprType { 8 | public PyObject value; 9 | 10 | public NameConstant(PyObject value,int lineno, int col_offset) { 11 | this.value = value; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "NameConstant"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitNameConstant(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Nonlocal.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Nonlocal extends stmtType { 8 | public java.util.List names; 9 | 10 | public Nonlocal(java.util.List names,int lineno, int 11 | col_offset) { 12 | this.names = names; 13 | this.lineno = lineno; 14 | this.col_offset = col_offset; 15 | } 16 | 17 | public String toString() { 18 | return "Nonlocal"; 19 | } 20 | 21 | public void pickle(DataOutputStream ostream) throws IOException { 22 | } 23 | 24 | public Object accept(VisitorIF visitor) throws Exception { 25 | return visitor.visitNonlocal(this); 26 | } 27 | 28 | public void traverse(VisitorIF visitor) throws Exception { 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Num.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Num extends exprType { 8 | public PyObject n; 9 | 10 | public Num(PyObject n,int lineno, int col_offset) { 11 | this.n = n; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Num"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitNum(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Pass.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Pass extends stmtType { 8 | 9 | public Pass(int lineno, int col_offset) { 10 | this.lineno = lineno; 11 | this.col_offset = col_offset; 12 | } 13 | 14 | public String toString() { 15 | return "Pass"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitPass(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Raise.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Raise extends stmtType { 8 | public exprType exc; 9 | public exprType cause; 10 | 11 | public Raise(exprType exc, exprType cause,int lineno, int col_offset) { 12 | this.exc = exc; 13 | this.cause = cause; 14 | this.lineno = lineno; 15 | this.col_offset = col_offset; 16 | } 17 | 18 | public String toString() { 19 | return "Raise"; 20 | } 21 | 22 | public void pickle(DataOutputStream ostream) throws IOException { 23 | } 24 | 25 | public Object accept(VisitorIF visitor) throws Exception { 26 | return visitor.visitRaise(this); 27 | } 28 | 29 | public void traverse(VisitorIF visitor) throws Exception { 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Return.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Return extends stmtType { 8 | public exprType value; 9 | 10 | public Return(exprType value,int lineno, int col_offset) { 11 | this.value = value; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Return"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitReturn(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Set.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Set extends exprType { 8 | public java.util.List elts; 9 | 10 | public Set(java.util.List elts,int lineno, int col_offset) { 11 | this.elts = elts; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Set"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitSet(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/SetComp.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class SetComp extends exprType { 8 | public exprType elt; 9 | public java.util.List generators; 10 | 11 | public SetComp(exprType elt, java.util.List 12 | generators,int lineno, int col_offset) { 13 | this.elt = elt; 14 | this.generators = generators; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "SetComp"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitSetComp(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Slice.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Slice extends sliceType { 8 | public exprType lower; 9 | public exprType upper; 10 | public exprType step; 11 | 12 | public Slice(exprType lower, exprType upper, exprType step) { 13 | this.lower = lower; 14 | this.upper = upper; 15 | this.step = step; 16 | } 17 | 18 | public String toString() { 19 | return "Slice"; 20 | } 21 | 22 | public void pickle(DataOutputStream ostream) throws IOException { 23 | } 24 | 25 | public Object accept(VisitorIF visitor) throws Exception { 26 | return visitor.visitSlice(this); 27 | } 28 | 29 | public void traverse(VisitorIF visitor) throws Exception { 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Starred.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Starred extends exprType { 8 | public exprType value; 9 | public expr_contextType ctx; 10 | 11 | public Starred(exprType value, expr_contextType ctx,int lineno, int 12 | col_offset) { 13 | this.value = value; 14 | this.ctx = ctx; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "Starred"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitStarred(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Str.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Str extends exprType { 8 | public PyObject s; 9 | 10 | public Str(PyObject s,int lineno, int col_offset) { 11 | this.s = s; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Str"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitStr(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Subscript.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Subscript extends exprType { 8 | public exprType value; 9 | public sliceType slice; 10 | public expr_contextType ctx; 11 | 12 | public Subscript(exprType value, sliceType slice, expr_contextType 13 | ctx,int lineno, int col_offset) { 14 | this.value = value; 15 | this.slice = slice; 16 | this.ctx = ctx; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "Subscript"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitSubscript(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Suite.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Suite extends modType { 8 | public java.util.List body; 9 | 10 | public Suite(java.util.List body) { 11 | this.body = body; 12 | } 13 | 14 | public String toString() { 15 | return "Suite"; 16 | } 17 | 18 | public void pickle(DataOutputStream ostream) throws IOException { 19 | } 20 | 21 | public Object accept(VisitorIF visitor) throws Exception { 22 | return visitor.visitSuite(this); 23 | } 24 | 25 | public void traverse(VisitorIF visitor) throws Exception { 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Try.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Try extends stmtType { 8 | public java.util.List body; 9 | public java.util.List handlers; 10 | public java.util.List orelse; 11 | public java.util.List finalbody; 12 | 13 | public Try(java.util.List body, 14 | java.util.List handlers, java.util.List 15 | orelse, java.util.List finalbody,int lineno, int col_offset) { 16 | this.body = body; 17 | this.handlers = handlers; 18 | this.orelse = orelse; 19 | this.finalbody = finalbody; 20 | this.lineno = lineno; 21 | this.col_offset = col_offset; 22 | } 23 | 24 | public String toString() { 25 | return "Try"; 26 | } 27 | 28 | public void pickle(DataOutputStream ostream) throws IOException { 29 | } 30 | 31 | public Object accept(VisitorIF visitor) throws Exception { 32 | return visitor.visitTry(this); 33 | } 34 | 35 | public void traverse(VisitorIF visitor) throws Exception { 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Tuple.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Tuple extends exprType { 8 | public java.util.List elts; 9 | public expr_contextType ctx; 10 | 11 | public Tuple(java.util.List elts, expr_contextType ctx,int 12 | lineno, int col_offset) { 13 | this.elts = elts; 14 | this.ctx = ctx; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "Tuple"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitTuple(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/UnaryOp.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class UnaryOp extends exprType { 8 | public unaryopType op; 9 | public exprType operand; 10 | 11 | public UnaryOp(unaryopType op, exprType operand,int lineno, int 12 | col_offset) { 13 | this.op = op; 14 | this.operand = operand; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "UnaryOp"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitUnaryOp(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/VisitorIF.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public interface VisitorIF { 6 | public Object visitModule(Module node) throws Exception; 7 | public Object visitInteractive(Interactive node) throws Exception; 8 | public Object visitExpression(Expression node) throws Exception; 9 | public Object visitSuite(Suite node) throws Exception; 10 | public Object visitFunctionDef(FunctionDef node) throws Exception; 11 | public Object visitAsyncFunctionDef(AsyncFunctionDef node) throws 12 | Exception; 13 | public Object visitClassDef(ClassDef node) throws Exception; 14 | public Object visitReturn(Return node) throws Exception; 15 | public Object visitDelete(Delete node) throws Exception; 16 | public Object visitAssign(Assign node) throws Exception; 17 | public Object visitAugAssign(AugAssign node) throws Exception; 18 | public Object visitFor(For node) throws Exception; 19 | public Object visitAsyncFor(AsyncFor node) throws Exception; 20 | public Object visitWhile(While node) throws Exception; 21 | public Object visitIf(If node) throws Exception; 22 | public Object visitWith(With node) throws Exception; 23 | public Object visitAsyncWith(AsyncWith node) throws Exception; 24 | public Object visitRaise(Raise node) throws Exception; 25 | public Object visitTry(Try node) throws Exception; 26 | public Object visitAssert(Assert node) throws Exception; 27 | public Object visitImport(Import node) throws Exception; 28 | public Object visitImportFrom(ImportFrom node) throws Exception; 29 | public Object visitGlobal(Global node) throws Exception; 30 | public Object visitNonlocal(Nonlocal node) throws Exception; 31 | public Object visitExpr(Expr node) throws Exception; 32 | public Object visitPass(Pass node) throws Exception; 33 | public Object visitBreak(Break node) throws Exception; 34 | public Object visitContinue(Continue node) throws Exception; 35 | public Object visitBoolOp(BoolOp node) throws Exception; 36 | public Object visitBinOp(BinOp node) throws Exception; 37 | public Object visitUnaryOp(UnaryOp node) throws Exception; 38 | public Object visitLambda(Lambda node) throws Exception; 39 | public Object visitIfExp(IfExp node) throws Exception; 40 | public Object visitDict(Dict node) throws Exception; 41 | public Object visitSet(Set node) throws Exception; 42 | public Object visitListComp(ListComp node) throws Exception; 43 | public Object visitSetComp(SetComp node) throws Exception; 44 | public Object visitDictComp(DictComp node) throws Exception; 45 | public Object visitGeneratorExp(GeneratorExp node) throws Exception; 46 | public Object visitAwait(Await node) throws Exception; 47 | public Object visitYield(Yield node) throws Exception; 48 | public Object visitYieldFrom(YieldFrom node) throws Exception; 49 | public Object visitCompare(Compare node) throws Exception; 50 | public Object visitCall(Call node) throws Exception; 51 | public Object visitNum(Num node) throws Exception; 52 | public Object visitStr(Str node) throws Exception; 53 | public Object visitBytes(Bytes node) throws Exception; 54 | public Object visitNameConstant(NameConstant node) throws Exception; 55 | public Object visitEllipsis(Ellipsis node) throws Exception; 56 | public Object visitAttribute(Attribute node) throws Exception; 57 | public Object visitSubscript(Subscript node) throws Exception; 58 | public Object visitStarred(Starred node) throws Exception; 59 | public Object visitName(Name node) throws Exception; 60 | public Object visitList(List node) throws Exception; 61 | public Object visitTuple(Tuple node) throws Exception; 62 | public Object visitSlice(Slice node) throws Exception; 63 | public Object visitExtSlice(ExtSlice node) throws Exception; 64 | public Object visitIndex(Index node) throws Exception; 65 | public Object visitExceptHandler(ExceptHandler node) throws Exception; 66 | } 67 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/While.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class While extends stmtType { 8 | public exprType test; 9 | public java.util.List body; 10 | public java.util.List orelse; 11 | 12 | public While(exprType test, java.util.List body, 13 | java.util.List orelse,int lineno, int col_offset) { 14 | this.test = test; 15 | this.body = body; 16 | this.orelse = orelse; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "While"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | return visitor.visitWhile(this); 30 | } 31 | 32 | public void traverse(VisitorIF visitor) throws Exception { 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/With.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class With extends stmtType { 8 | public java.util.List items; 9 | public java.util.List body; 10 | 11 | public With(java.util.List items, 12 | java.util.List body,int lineno, int col_offset) { 13 | this.items = items; 14 | this.body = body; 15 | this.lineno = lineno; 16 | this.col_offset = col_offset; 17 | } 18 | 19 | public String toString() { 20 | return "With"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | return visitor.visitWith(this); 28 | } 29 | 30 | public void traverse(VisitorIF visitor) throws Exception { 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/Yield.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class Yield extends exprType { 8 | public exprType value; 9 | 10 | public Yield(exprType value,int lineno, int col_offset) { 11 | this.value = value; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "Yield"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitYield(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/YieldFrom.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class YieldFrom extends exprType { 8 | public exprType value; 9 | 10 | public YieldFrom(exprType value,int lineno, int col_offset) { 11 | this.value = value; 12 | this.lineno = lineno; 13 | this.col_offset = col_offset; 14 | } 15 | 16 | public String toString() { 17 | return "YieldFrom"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | return visitor.visitYieldFrom(this); 25 | } 26 | 27 | public void traverse(VisitorIF visitor) throws Exception { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/aliasType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class aliasType{ 8 | public String name; 9 | public String asname; 10 | 11 | public aliasType(String name, String asname) { 12 | this.name = name; 13 | this.asname = asname; 14 | } 15 | 16 | public String toString() { 17 | return "aliasType"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | traverse(visitor); 25 | return null; 26 | } 27 | 28 | public void traverse(VisitorIF visitor) throws Exception { 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/argType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class argType{ 8 | public String arg; 9 | public exprType annotation; 10 | public int lineno; 11 | public int col_offset; 12 | 13 | public argType(String arg, exprType annotation,int lineno, int 14 | col_offset) { 15 | this.arg = arg; 16 | this.annotation = annotation; 17 | this.lineno = lineno; 18 | this.col_offset = col_offset; 19 | } 20 | 21 | public String toString() { 22 | return "argType"; 23 | } 24 | 25 | public void pickle(DataOutputStream ostream) throws IOException { 26 | } 27 | 28 | public Object accept(VisitorIF visitor) throws Exception { 29 | traverse(visitor); 30 | return null; 31 | } 32 | 33 | public void traverse(VisitorIF visitor) throws Exception { 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/argumentsType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class argumentsType{ 8 | public java.util.List args; 9 | public argType vararg; 10 | public java.util.List kwonlyargs; 11 | public java.util.List kw_defaults; 12 | public argType kwarg; 13 | public java.util.List defaults; 14 | 15 | public argumentsType(java.util.List args, argType vararg, 16 | java.util.List kwonlyargs, java.util.List 17 | kw_defaults, argType kwarg, java.util.List defaults) { 18 | this.args = args; 19 | this.vararg = vararg; 20 | this.kwonlyargs = kwonlyargs; 21 | this.kw_defaults = kw_defaults; 22 | this.kwarg = kwarg; 23 | this.defaults = defaults; 24 | } 25 | 26 | public String toString() { 27 | return "argumentsType"; 28 | } 29 | 30 | public void pickle(DataOutputStream ostream) throws IOException { 31 | } 32 | 33 | public Object accept(VisitorIF visitor) throws Exception { 34 | traverse(visitor); 35 | return null; 36 | } 37 | 38 | public void traverse(VisitorIF visitor) throws Exception { 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/boolopType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public enum boolopType { 6 | UNDEFINED, 7 | And, 8 | Or, 9 | } 10 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/cmpopType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public enum cmpopType { 6 | UNDEFINED, 7 | Eq, 8 | NotEq, 9 | Lt, 10 | LtE, 11 | Gt, 12 | GtE, 13 | Is, 14 | IsNot, 15 | In, 16 | NotIn, 17 | } 18 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/comprehensionType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class comprehensionType{ 8 | public exprType target; 9 | public exprType iter; 10 | public java.util.List ifs; 11 | 12 | public comprehensionType(exprType target, exprType iter, 13 | java.util.List ifs) { 14 | this.target = target; 15 | this.iter = iter; 16 | this.ifs = ifs; 17 | } 18 | 19 | public String toString() { 20 | return "comprehensionType"; 21 | } 22 | 23 | public void pickle(DataOutputStream ostream) throws IOException { 24 | } 25 | 26 | public Object accept(VisitorIF visitor) throws Exception { 27 | traverse(visitor); 28 | return null; 29 | } 30 | 31 | public void traverse(VisitorIF visitor) throws Exception { 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/excepthandlerType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public abstract class excepthandlerType{ 6 | public int lineno; 7 | public int col_offset; 8 | } 9 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/exprType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public abstract class exprType{ 6 | public int lineno; 7 | public int col_offset; 8 | } 9 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/expr_contextType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public enum expr_contextType { 6 | UNDEFINED, 7 | Load, 8 | Store, 9 | Del, 10 | AugLoad, 11 | AugStore, 12 | Param, 13 | } 14 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/keywordType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class keywordType{ 8 | public String arg; 9 | public exprType value; 10 | 11 | public keywordType(String arg, exprType value) { 12 | this.arg = arg; 13 | this.value = value; 14 | } 15 | 16 | public String toString() { 17 | return "keywordType"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | traverse(visitor); 25 | return null; 26 | } 27 | 28 | public void traverse(VisitorIF visitor) throws Exception { 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/modType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public abstract class modType{ 6 | } 7 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/operatorType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public enum operatorType { 6 | UNDEFINED, 7 | Add, 8 | Sub, 9 | Mult, 10 | MatMult, 11 | Div, 12 | Mod, 13 | Pow, 14 | LShift, 15 | RShift, 16 | BitOr, 17 | BitXor, 18 | BitAnd, 19 | FloorDiv, 20 | } 21 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/sliceType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public abstract class sliceType{ 6 | } 7 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/stmtType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public abstract class stmtType{ 6 | public int lineno; 7 | public int col_offset; 8 | } 9 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/unaryopType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | 5 | public enum unaryopType { 6 | UNDEFINED, 7 | Invert, 8 | Not, 9 | UAdd, 10 | USub, 11 | } 12 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/ast/withitemType.java: -------------------------------------------------------------------------------- 1 | // Autogenerated AST node 2 | package pers.xia.jpython.ast; 3 | import pers.xia.jpython.object.PyObject; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | 7 | public class withitemType{ 8 | public exprType context_expr; 9 | public exprType optional_vars; 10 | 11 | public withitemType(exprType context_expr, exprType optional_vars) { 12 | this.context_expr = context_expr; 13 | this.optional_vars = optional_vars; 14 | } 15 | 16 | public String toString() { 17 | return "withitemType"; 18 | } 19 | 20 | public void pickle(DataOutputStream ostream) throws IOException { 21 | } 22 | 23 | public Object accept(VisitorIF visitor) throws Exception { 24 | traverse(visitor); 25 | return null; 26 | } 27 | 28 | public void traverse(VisitorIF visitor) throws Exception { 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/compiler/BlockType.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.compiler; 2 | 3 | public enum BlockType 4 | { 5 | FunctionBlock, ClassBlock, ModuleBlock 6 | } 7 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/compiler/Compiler.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.compiler; 2 | 3 | import pers.xia.jpython.ast.modType; 4 | import pers.xia.jpython.object.PyCodeObject; 5 | import pers.xia.jpython.object.PyList; 6 | import pers.xia.jpython.object.PyObject; 7 | import pers.xia.jpython.object.PyUnicode; 8 | 9 | public class Compiler 10 | { 11 | public PyObject fileName; 12 | public Symtable st; 13 | 14 | public int optimize; // optimization level 15 | public boolean interactive; // is or not interactive mode 16 | public int nestLevel; 17 | 18 | public CompilerUnit u; 19 | public PyList stack; 20 | 21 | private static PyObject __doc__; 22 | 23 | public Compiler() 24 | { 25 | this.stack = new PyList(0); 26 | } 27 | 28 | public Compiler(PyObject fileName, int optimize, Symtable st) 29 | { 30 | this.fileName = fileName; 31 | this.optimize = optimize; 32 | this.nestLevel = 0; 33 | this.st = st; 34 | } 35 | 36 | public static PyObject _PyMangLe(String privateStr, String ident) 37 | { 38 | if(privateStr == null || ident.charAt(0) == '_' || ident.charAt(0) == '_') 39 | { 40 | return PyUnicode.internFromString(ident); 41 | } 42 | 43 | int nlen = ident.length(); 44 | int plen = privateStr.length(); 45 | 46 | if(ident.charAt(nlen - 1) == '_' && ident.charAt(nlen - 2) == '_' || 47 | ident.indexOf('.') < 0) 48 | { 49 | return PyUnicode.internFromString(ident); 50 | } 51 | 52 | int ipriv = 0; 53 | while(ipriv < plen && privateStr.charAt(ipriv) == '_') ipriv++; 54 | if(ipriv == plen) 55 | { 56 | return PyUnicode.internFromString(ident); 57 | } 58 | 59 | return PyUnicode.internFromString(privateStr.substring(ipriv) + ident); 60 | } 61 | 62 | public PyCodeObject mod(modType mod) 63 | { 64 | return null; 65 | } 66 | 67 | public static PyCodeObject compile(modType mod, String fileName) 68 | { 69 | PyObject fileNameObj = PyUnicode.internFromString(fileName); 70 | return compileObject(mod, fileNameObj, -1); 71 | } 72 | 73 | public static PyCodeObject compileObject(modType mod, PyObject fileName, 74 | int optmize) 75 | { 76 | if(__doc__ == null) 77 | { 78 | __doc__ = PyUnicode.internFromString("__doc__"); 79 | } 80 | 81 | Symtable st = Symtable.buildObject(mod, fileName); 82 | Compiler c = new Compiler(fileName, optmize, st); 83 | PyCodeObject co = c.mod(mod); 84 | return co; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/compiler/CompilerUnit.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.compiler; 2 | 3 | public class CompilerUnit 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/compiler/PySTEntryObject.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.compiler; 2 | 3 | import pers.xia.jpython.object.Py; 4 | import pers.xia.jpython.object.PyDict; 5 | import pers.xia.jpython.object.PyList; 6 | import pers.xia.jpython.object.PyLong; 7 | import pers.xia.jpython.object.PyObject; 8 | import pers.xia.jpython.object.PySet; 9 | 10 | class PySTEntryObject extends PyObject 11 | { 12 | PyLong steId; /* int: key in ste_table->st_blocks */ 13 | PyDict steSymbols; /* dict: variable names to flags */ 14 | String steName; /* string: name of current block */ 15 | PyList steVarnames; /* list of function parameters */ 16 | PyList steChildren; /* list of child blocks */ 17 | PyList steDirecitives; /* locations of global and nonlocal statements */ 18 | BlockType steType; /* module, class, or function */ 19 | boolean steNested; /* true if block is nested */ 20 | boolean steFree; /* true if block has free variables */ 21 | boolean steChildFree; /* true if a child block has free vars, 22 | including free refs to globals */ 23 | boolean steGenerator; /* true if namespace is a generator */ 24 | boolean steVarargs; /* true if block has varargs */ 25 | boolean steVarkeywords; /* true if block has varkeywords */ 26 | boolean steReturnsValue; /* true if a child block has free vars, 27 | including free refs to globals */ 28 | 29 | boolean steNeedsClassClosure;/* for class scopes, true if a 30 | closure over __class__ 31 | should be created */ 32 | int steLineno; /* first line of block */ 33 | int steColOffset; /* offset of first line of block */ 34 | int steOptLineno; /* lineno of last exec or import * */ 35 | int steOptColOffset; /* offset of last exec or import * */ 36 | int steTmpname; /* counter for listcomp temp vars */ 37 | Symtable steTable; 38 | 39 | // TODO coding 40 | public PySTEntryObject(Symtable st, String name, BlockType block, 41 | Object key, int lineno, int colOffet) 42 | { 43 | PyLong k = null; 44 | k = Py.newInteger(key.hashCode()); 45 | this.steTable = st; 46 | this.steId = k; 47 | this.steName = name; 48 | 49 | this.steSymbols = null; 50 | this.steVarnames = null; 51 | this.steChildren = null; 52 | 53 | this.steDirecitives = null; 54 | 55 | this.steType = block; 56 | this.steNested = false; 57 | this.steFree = false; 58 | this.steVarargs = false; 59 | this.steVarkeywords = false; 60 | this.steOptLineno = 0; 61 | this.steOptColOffset = 0; 62 | this.steTmpname = 0; 63 | this.steLineno = lineno; 64 | this.steColOffset = colOffet; 65 | 66 | if(st.stCur != null && (st.stCur.steNested 67 | || st.stCur.steType == BlockType.FunctionBlock)) 68 | { 69 | this.steNested = true; 70 | } 71 | this.steGenerator = false; 72 | this.steGenerator = false; 73 | this.steReturnsValue = false; 74 | this.steNeedsClassClosure = false; 75 | 76 | this.steSymbols = new PyDict(); 77 | this.steVarnames = new PyList(0); 78 | this.steChildren = new PyList(0); 79 | 80 | st.stBlocks.setItem(this.steId, this); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/config/Config.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.config; 2 | 3 | public class Config 4 | { 5 | public static final int MAXINDENT = 100; 6 | public static final int TABSIZE = 8; 7 | public static final int BUFSIZ = 8192; 8 | public static final int NSMALLPOSINTS = 105; 9 | public static final int NSMALLNEGINTS = -5; 10 | } 11 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/Arc.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar; 2 | 3 | public class Arc 4 | { 5 | public int label; 6 | public int nextState; 7 | 8 | public Arc(int label, int nextState) 9 | { 10 | this.label = label; 11 | this.nextState = nextState; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/DFA.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar; 2 | 3 | import pers.xia.jpython.object.PyExceptions; 4 | 5 | public class DFA 6 | { 7 | public DFAType name; //DFA name 8 | public int initial; //Initial state, 9 | public int nstates; //the number of state 10 | public State[] states; //states for DFA 11 | public int[] acceptLabel; //保存跳转到当前DFA 12 | 13 | 14 | public DFA(DFAType name, int initial, int nstates, State[] states, int[] acceptLabel) 15 | { 16 | this.name = name; 17 | this.initial = initial; 18 | this.nstates = nstates; 19 | this.states = states; 20 | this.acceptLabel = acceptLabel; 21 | } 22 | 23 | public State getState(int index) 24 | { 25 | if(index >= nstates || index < 0) 26 | { 27 | throw new PyExceptions("DFA: Out of Index"); 28 | } 29 | return this.states[index]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/DFAType.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar; 2 | 3 | public enum DFAType 4 | { 5 | single_input, 6 | file_input, 7 | eval_input, 8 | decorator, 9 | decorators, 10 | decorated, 11 | async_funcdef, 12 | funcdef, 13 | parameters, 14 | typedargslist, 15 | tfpdef, 16 | varargslist, 17 | vfpdef, 18 | stmt, 19 | simple_stmt, 20 | small_stmt, 21 | expr_stmt, 22 | testlist_star_expr, 23 | augassign, 24 | del_stmt, 25 | pass_stmt, 26 | flow_stmt, 27 | break_stmt, 28 | continue_stmt, 29 | return_stmt, 30 | yield_stmt, 31 | raise_stmt, 32 | import_stmt, 33 | import_name, 34 | import_from, 35 | import_as_name, 36 | dotted_as_name, 37 | import_as_names, 38 | dotted_as_names, 39 | dotted_name, 40 | global_stmt, 41 | nonlocal_stmt, 42 | assert_stmt, 43 | compound_stmt, 44 | async_stmt, 45 | if_stmt, 46 | while_stmt, 47 | for_stmt, 48 | try_stmt, 49 | with_stmt, 50 | with_item, 51 | except_clause, 52 | suite, 53 | test, 54 | test_nocond, 55 | lambdef, 56 | lambdef_nocond, 57 | or_test, 58 | and_test, 59 | not_test, 60 | comparison, 61 | comp_op, 62 | star_expr, 63 | expr, 64 | xor_expr, 65 | and_expr, 66 | shift_expr, 67 | arith_expr, 68 | term, 69 | factor, 70 | power, 71 | atom_expr, 72 | atom, 73 | testlist_comp, 74 | trailer, 75 | subscriptlist, 76 | subscript, 77 | sliceop, 78 | exprlist, 79 | testlist, 80 | dictorsetmaker, 81 | classdef, 82 | arglist, 83 | argument, 84 | comp_iter, 85 | comp_for, 86 | comp_if, 87 | encoding_decl, 88 | yield_expr, 89 | yield_arg, 90 | 91 | /* The follow dfa type are copyed from TokState.java */ 92 | /* The follow type a used to make analyze easier */ 93 | 94 | ENDMARKER, 95 | NAME, 96 | NUMBER, 97 | STRING, 98 | NEWLINE, 99 | INDENT, 100 | DEDENT, 101 | LPAR, // ( 102 | RPAR, // ) 103 | LSQB, // [ 104 | RSQB, // ] 105 | COLON, // : 106 | COMMA, // , 107 | SEMI, // ; 108 | PLUS, // + 109 | MINUS, // - 110 | STAR, // * 111 | SLASH, // / 112 | VBAR, // | 113 | AMPER, // & 114 | LESS, // < 115 | GREATER, // > 116 | EQUAL, // = 117 | DOT, // . 118 | PERCENT, // % 119 | BACKQUOTE, 120 | LBRACE, // { 121 | RBRACE, // } 122 | EQEQUAL, // == 123 | NOTEQUAL, // != 124 | LESSEQUAL, // <= 125 | GREATEREQUAL, // >= 126 | TILDE, // ~ 127 | CIRCUMFLEX, // ^ 128 | LEFTSHIFT, // << 129 | RIGHTSHIFT, // >> 130 | DOUBLESTAR, // ** 131 | PLUSEQUAL, // += 132 | MINEQUAL, // -= 133 | STAREQUAL, // *= 134 | SLASHEQUAL, // /= 135 | PERCENTEQUAL, // %= 136 | AMPEREQUAL, // &= 137 | VBAREQUAL, // |= 138 | CIRCUMFLEXEQUAL, // ^= 139 | LEFTSHIFTEQUAL, // <<= 140 | RIGHTSHIFTEQUAL, // >>= 141 | DOUBLESTAREQUAL, // **= 142 | DOUBLESLASH, // \\ 143 | DOUBLESLASHEQUAL, // \\= 144 | AT, // @ 145 | ATEQUAL, // @= 146 | RARROW, // -> 147 | ELLIPSIS, // ... 148 | OP, 149 | AWAIT, // await 150 | ASYNC, // async 151 | ERRORTOKEN, 152 | N_TOKENS, 153 | } -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/Grammar.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar; 2 | 3 | import pers.xia.jpython.object.PyExceptions; 4 | 5 | public class Grammar 6 | { 7 | public int ndfas; // dfa总数 8 | public DFA[] dfas; // 保存dfa的数组 9 | 10 | public int nlabels; // label总数 11 | public Label[] labels; // 保存label的数组 12 | 13 | public int start; //起始dfa 14 | 15 | public boolean accel; // 加速器 set if accelerators present 16 | 17 | public Grammar(int ndfas, DFA[] dfas, int nlabels, Label[] labels, int start) 18 | { 19 | this.ndfas = ndfas; 20 | this.dfas = dfas; 21 | this.nlabels = nlabels; 22 | this.labels = labels; 23 | this.start = start; 24 | } 25 | 26 | private void fixState(State state) 27 | { 28 | int accel[] = new int[this.nlabels]; 29 | for(int i = 0; i < this.nlabels; i++) 30 | { 31 | accel[i] = -1; 32 | } 33 | 34 | Arc a[] = state.arcs; 35 | 36 | for(int i = 0; i < state.narcs; i++) 37 | { 38 | int lbl = a[i].label; 39 | Label l = this.getLabel(lbl); 40 | 41 | if(l.isTerminal) 42 | { 43 | accel[lbl] = a[i].nextState; 44 | } 45 | else if(l.nextDfa == -1) 46 | { 47 | state.accept = true; 48 | } 49 | else 50 | { 51 | DFA dfa = this.getDFA(l.nextDfa); 52 | 53 | for(int j = 0; j < dfa.acceptLabel.length; j++) 54 | { 55 | accel[dfa.acceptLabel[j]] = l.nextDfa << 8 | 1 << 7 | 56 | a[i].nextState; 57 | } 58 | } 59 | } 60 | 61 | int lower; //标记最小的label下标 62 | int upper; //标记最大的label下标 63 | 64 | //获取最小坐标和最大坐标 65 | for(lower = 0; lower < this.nlabels && accel[lower] == -1; lower++); 66 | for(upper = this.nlabels; upper >= 0 && upper > lower && accel[upper-1] == -1; upper--); 67 | 68 | //不存在有效边 69 | if(upper == lower) 70 | { 71 | return; 72 | } 73 | 74 | state.lower = lower; 75 | state.upper = upper; 76 | 77 | //将生成的数组放到state里面 78 | state.accel = new int[state.upper - state.lower]; 79 | for(int i = 0; i < upper - lower; i++) 80 | { 81 | state.accel[i] = accel[i + lower]; 82 | } 83 | } 84 | 85 | private void fixDFA(DFA dfa) 86 | { 87 | for(int i = 0; i < dfa.nstates; i++) 88 | { 89 | fixState(dfa.states[i]); 90 | } 91 | } 92 | 93 | public void addAccelerators() 94 | { 95 | for(int i = 0; i < this.ndfas; i++) 96 | { 97 | fixDFA(this.dfas[i]); 98 | } 99 | this.accel = true; 100 | } 101 | 102 | public DFA getDFA(int index) 103 | { 104 | if(index < 0 || index >= this.ndfas) 105 | { 106 | throw new PyExceptions("Grammar: Out of Index while get DFA"); 107 | } 108 | return this.dfas[index]; 109 | } 110 | 111 | public Label getLabel(int index) 112 | { 113 | if(index < 0 || index >= this.nlabels) 114 | { 115 | throw new PyExceptions("Grammar: Out of Index while get DFA"); 116 | } 117 | return this.labels[index]; 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/Label.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar; 2 | 3 | import pers.xia.jpython.tokenizer.TokState; 4 | 5 | public class Label 6 | { 7 | //DFAName dfaName; //DFA的名字 8 | public int nextDfa; //下一个DFA 9 | 10 | public TokState tokState; 11 | public String str; 12 | 13 | public boolean isTerminal; 14 | 15 | public Label(int nextDfa) 16 | { 17 | //this.dfaName = dfaName; 18 | this.nextDfa = nextDfa; 19 | this.isTerminal = false; 20 | } 21 | 22 | public Label(TokState tokState, String str) 23 | { 24 | this.tokState = tokState; 25 | this.str = str; 26 | this.isTerminal = true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/State.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar; 2 | 3 | public class State 4 | { 5 | public int narcs; 6 | public Arc[] arcs; 7 | 8 | /* Optional accelerators */ 9 | public int lower; /* Lowest label index */ 10 | public int upper; /* Highest label index */ 11 | int accel[]; /* Accelerators */ 12 | public boolean accept; /* Accepting state */ 13 | 14 | public State(int narcs, Arc[] arcs) 15 | { 16 | this.narcs = narcs; 17 | this.arcs = arcs; 18 | this.accept = false; 19 | } 20 | 21 | public int next(int index) 22 | { 23 | return accel[index]; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/pgen/DoubleS.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar.pgen; 2 | 3 | // 表示 O->O 这种格式的FA单元 4 | class DoubleS 5 | { 6 | _State start; 7 | _State end; 8 | public DoubleS() 9 | { 10 | this.start = new _State(); 11 | this.end = new _State(); 12 | } 13 | 14 | public DoubleS(_State start, _State end) 15 | { 16 | this.start = start; 17 | this.end = end; 18 | } 19 | 20 | public DoubleS(_Arc arc) 21 | { 22 | start = new _State(); 23 | start.addArc(arc); 24 | end = arc.nextState; 25 | } 26 | 27 | public DoubleS(_Label label) 28 | { 29 | start = new _State(); 30 | end = new _State(); 31 | _Arc arc = new _Arc(label, end); 32 | start.addArc(arc); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/pgen/_Arc.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar.pgen; 2 | 3 | /* 4 | * 用于Pgen生成数据 5 | * 由于Label类被替代,故Arc类也必须有对应的替代。 6 | */ 7 | class _Arc 8 | { 9 | public _Label label; 10 | public _State nextState; 11 | 12 | public _Arc(_Label label, _State nextState) 13 | { 14 | this.label = label; 15 | this.nextState = nextState; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/pgen/_DFA.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar.pgen; 2 | 3 | import java.util.HashSet; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Set; 8 | import java.util.Stack; 9 | 10 | import pers.xia.jpython.object.PyExceptions; 11 | 12 | class _DFA 13 | { 14 | 15 | String name; //DFA name 16 | _State initial; //Initial state, 17 | int nstates; //the number of state 18 | // 这里其实用List操作起来会方便很多。。。一开始没设计好后面也不好修改,还好这里对最终代码没影响 19 | _State[] states; //states for DFA 20 | Map<_Label, _DFA> jumpedDFAs; //当跳转到本DFA时根据lebel判断实际需要跳转的DFA 21 | 22 | static final int MAXSIZE = 256; 23 | 24 | public _DFA(String name) 25 | { 26 | if(name == null) 27 | { 28 | throw new PyExceptions("_DFA must have name"); 29 | } 30 | this.name = name; 31 | this.initial = null; 32 | this.nstates = 0; 33 | this.states = new _State[_DFA.MAXSIZE]; 34 | this.jumpedDFAs = null; 35 | } 36 | 37 | public boolean addState(_State state) 38 | { 39 | if(this.nstates >= this.states.length) 40 | { 41 | throw new PyExceptions("Over the max size of DFA"); 42 | } 43 | this.states[this.nstates++] = state; 44 | return true; 45 | } 46 | 47 | public _DFA(String name, _State initial, int nstates, _State[] states) 48 | { 49 | this.name = name; 50 | this.initial = initial; 51 | this.nstates = nstates; 52 | this.states = states; 53 | } 54 | 55 | public boolean cmp(_DFA dfa) 56 | { 57 | if(this.name.equals(dfa.name)) 58 | { 59 | return true; 60 | } 61 | return false; 62 | } 63 | 64 | public List<_Label> getAllStartLabel() 65 | { 66 | List<_Label> labels = new LinkedList<_Label>(); 67 | for(int i = 0; i < this.initial.narcs; i++) 68 | { 69 | labels.add(this.initial.arcs[i].label); 70 | } 71 | return labels; 72 | } 73 | 74 | public void setStates() 75 | { 76 | Set<_State> states = new HashSet<_State>(); 77 | Stack<_State> stateStack = new Stack<_State>(); 78 | 79 | stateStack.push(this.initial); 80 | _State state = null; 81 | while(!stateStack.empty()) 82 | { 83 | state = stateStack.pop(); 84 | if(states.contains(state)) 85 | { 86 | continue; 87 | } 88 | states.add(state); 89 | 90 | for(int i = 0; i < state.narcs; i++) 91 | { 92 | if(state.arcs[i].nextState != null) 93 | stateStack.push(state.arcs[i].nextState); 94 | } 95 | } 96 | 97 | int i = 0; 98 | for(_State s : states) 99 | { 100 | this.states[i++] = s; 101 | } 102 | this.nstates = i; 103 | 104 | //将起始结点放到0号位 105 | for(i = 0; i < this.nstates; i++) 106 | { 107 | if(this.states[i] == this.initial) 108 | { 109 | _State s = this.states[0]; 110 | this.states[0] = this.states[i]; 111 | this.states[i] = s; 112 | } 113 | } 114 | 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/pgen/_Grammar.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar.pgen; 2 | 3 | import pers.xia.jpython.object.PyExceptions; 4 | 5 | class _Grammar 6 | { 7 | static final int DFAMAXSIZE = 256; 8 | static final int LABELMAXSIZE = 512; 9 | 10 | int ndfas; 11 | _DFA[] dfas; 12 | 13 | int nlabels; 14 | _Label[] labels; 15 | 16 | _DFA start; 17 | 18 | public _Grammar() 19 | { 20 | this.ndfas = 0; 21 | this.dfas = new _DFA[_Grammar.DFAMAXSIZE]; 22 | this.nlabels = 0; 23 | this.labels = new _Label[_Grammar.LABELMAXSIZE]; 24 | } 25 | 26 | public _Grammar(int ndfas, _DFA[] dfas, int nlabels, _Label[] labels, _DFA start) 27 | { 28 | this.ndfas = ndfas; 29 | this.dfas = dfas; 30 | this.nlabels = nlabels; 31 | this.labels = labels; 32 | this.start = start; 33 | } 34 | 35 | private boolean addLabel(_Label label) 36 | { 37 | if(nlabels >= this.labels.length) 38 | { 39 | throw new PyExceptions("Over the max size of label"); 40 | } 41 | this.labels[this.nlabels++] = label; 42 | return true; 43 | } 44 | 45 | public _Label setLabel(_Label label) 46 | { 47 | int i = 0; 48 | for(; i < this.nlabels; i++) 49 | { 50 | if(this.labels[i].cmp(label)) 51 | { 52 | break; 53 | } 54 | } 55 | 56 | if(i >= this.nlabels) 57 | { 58 | this.addLabel(label); 59 | return label; 60 | } 61 | else 62 | { 63 | return this.labels[i]; 64 | } 65 | } 66 | 67 | private boolean addDFA(_DFA dfa) 68 | { 69 | if(this.ndfas >= this.dfas.length) 70 | { 71 | throw new PyExceptions("Over the max size of dfa"); 72 | } 73 | this.dfas[this.ndfas++] = dfa; 74 | return true; 75 | } 76 | 77 | public _DFA setDFA(_DFA dfa) 78 | { 79 | int i = 0; 80 | for(; i < this.ndfas; i++) 81 | { 82 | if(this.dfas[i].cmp(dfa)) 83 | { 84 | break; 85 | } 86 | } 87 | 88 | if(i >= this.ndfas) 89 | { 90 | this.addDFA(dfa); 91 | return dfa; 92 | } 93 | else 94 | { 95 | return this.dfas[i]; 96 | } 97 | } 98 | 99 | public _DFA getDFA(String DFAName) 100 | { 101 | for(int i = 0; i < this.ndfas; i++) 102 | { 103 | if(this.dfas[i].name.equals(DFAName)) 104 | { 105 | return this.dfas[i]; 106 | } 107 | } 108 | return null; 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/pgen/_Label.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar.pgen; 2 | 3 | import pers.xia.jpython.tokenizer.TokState; 4 | 5 | /* 6 | * 用于Pgen生成数据 7 | * Label类的替代。由于DFAName和DFA一开始没有定义,所以无法直接使用 8 | * 原来的Lable,故后面的操作会用这个_Label来代替。 9 | */ 10 | class _Label 11 | { 12 | String nextDfa; //下一个DFA对象 13 | 14 | TokState tokState; 15 | String str; 16 | 17 | boolean isTerminal; 18 | 19 | public _Label(String nextDfa) 20 | { 21 | this.nextDfa = nextDfa; 22 | this.isTerminal = false; 23 | } 24 | 25 | public _Label(TokState tokState, String str) 26 | { 27 | this.tokState = tokState; 28 | this.str = str; 29 | this.isTerminal = true; 30 | } 31 | 32 | public boolean cmp(_Label label) 33 | { 34 | if(this.isTerminal != label.isTerminal) return false; 35 | if(this.isTerminal) 36 | { 37 | if(this.tokState == label.tokState) 38 | { 39 | //判断字符串是否相等,如果两个字符串都为null的话也应当被视作相等 40 | if(this.str == null && label.str == null) return true; 41 | if((this.str == null && label.str != null) || 42 | this.str != null & this.str == null) 43 | return false; 44 | if(this.str.equals(label.str)) return true; 45 | } 46 | } 47 | else 48 | { 49 | if(this.nextDfa.equals(label.nextDfa)) return true; 50 | else return false; 51 | } 52 | return false; // Make Compiler happy 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/pgen/_Node.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar.pgen; 2 | 3 | import java.util.LinkedList; 4 | 5 | /* 6 | * for Pgen 7 | */ 8 | class _Node 9 | { 10 | public _NodeType type; 11 | public String value; 12 | public LinkedList<_Node> childs; 13 | public _Node() 14 | { 15 | this.type = null; 16 | this.value = null; 17 | this.childs = new LinkedList<_Node>(); 18 | } 19 | 20 | public boolean addChild(_Node node) 21 | { 22 | this.childs.add(node); 23 | return true; 24 | } 25 | 26 | public boolean addFirstChild(_Node node) 27 | { 28 | this.childs.addFirst(node); 29 | return true; 30 | } 31 | 32 | public String toString() 33 | { 34 | return type.toString() + ": " + this.value; 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/pgen/_NodeType.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar.pgen; 2 | 3 | 4 | enum _NodeType 5 | { 6 | NAME, 7 | STRING, 8 | DFANAME, 9 | 10 | LPAR, 11 | RPAR, 12 | LSQB, 13 | RSQB, 14 | PLUS, 15 | STAR, 16 | VBAR, 17 | 18 | AND, 19 | OR, 20 | } 21 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/grammar/pgen/_State.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.grammar.pgen; 2 | 3 | import pers.xia.jpython.object.PyExceptions; 4 | 5 | /* 6 | * 用于Pgen生成数据 7 | */ 8 | class _State 9 | { 10 | static final int MAXSIZE = 100; 11 | int narcs; 12 | _Arc[] arcs; 13 | 14 | public _State() 15 | { 16 | this.narcs = 0; 17 | this.arcs = new _Arc[_State.MAXSIZE]; 18 | } 19 | 20 | public boolean addArc(_Arc arc) 21 | { 22 | if(this.narcs >= this.arcs.length) 23 | { 24 | throw new PyExceptions("Over the max size of arcs"); 25 | } 26 | this.arcs[this.narcs++] = arc; 27 | return true; 28 | } 29 | 30 | public int findArc(_Label label) 31 | { 32 | for(int i = 0; i < this.narcs; i++) 33 | { 34 | if(this.arcs[i].label == label) 35 | { 36 | return i; 37 | } 38 | } 39 | return -1; 40 | } 41 | 42 | public int findArc(_State nextState) 43 | { 44 | for(int i = 0; i < this.narcs; i++) 45 | { 46 | if(this.arcs[i].nextState == nextState) 47 | { 48 | return i; 49 | } 50 | } 51 | return -1; 52 | } 53 | 54 | public void removeAllArc(_Label label) 55 | { 56 | for(int i = 0; i < this.narcs; i++) 57 | { 58 | if(this.arcs[i].label == label) 59 | { 60 | this.arcs[i] = this.arcs[this.narcs - 1]; 61 | this.narcs--; 62 | i--; 63 | } 64 | } 65 | } 66 | 67 | public boolean removeArc(_Arc arc) 68 | { 69 | int i = 0; 70 | for(; i < this.narcs; i++) 71 | { 72 | if(this.arcs[i] == arc) 73 | { 74 | break; 75 | } 76 | } 77 | 78 | if(i >= this.narcs) 79 | { 80 | return false; 81 | } 82 | 83 | //把最后一个arc放到i位置上,然后总数减1,这样可以不使用前移数组这个操作 84 | this.arcs[i] = this.arcs[--this.narcs]; 85 | 86 | return true; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/main/Run.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.main; 2 | 3 | import java.io.File; 4 | 5 | import pers.xia.jpython.grammar.GramInit; 6 | import pers.xia.jpython.object.PyExceptions; 7 | import pers.xia.jpython.parser.Ast; 8 | import pers.xia.jpython.parser.Node; 9 | import pers.xia.jpython.parser.ParseToken; 10 | import pers.xia.jpython.parser.Parser; 11 | import pers.xia.jpython.parser.Parser.ReturnCode; 12 | import pers.xia.jpython.tokenizer.TokState; 13 | import pers.xia.jpython.tokenizer.Token; 14 | import pers.xia.jpython.tokenizer.Tokenizer; 15 | 16 | public class Run 17 | { 18 | 19 | public static void parse(String fileName) 20 | { 21 | File file = new File(fileName); 22 | if(file.isDirectory()) 23 | return; 24 | try 25 | { 26 | Node node = ParseToken.parseFile(file, GramInit.grammar, 1); 27 | 28 | Ast ast = new Ast(); 29 | ast.fromNode(node); 30 | } 31 | catch (PyExceptions e) 32 | { 33 | System.out.println(fileName); 34 | e.printStackTrace(); 35 | throw e; 36 | } 37 | } 38 | 39 | public static void main(String[] args) 40 | { 41 | File file = new File("./test"); 42 | if(file.isDirectory()) 43 | { 44 | String[] fileList = file.list(); 45 | for (String fileName : fileList) 46 | { 47 | if(fileName.charAt(fileName.length() - 1) == 'y') 48 | parse("./test/" + fileName); 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/Py.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | import pers.xia.jpython.config.Config; 4 | 5 | public final class Py 6 | { 7 | public final static PyNone None = new PyNone(); 8 | public final static PyBoolean True = new PyBoolean(true); 9 | public final static PyBoolean False = new PyBoolean(false); 10 | 11 | public static String encoding = "utf-8"; 12 | 13 | private final static PyLong[] integerCache = new PyLong[Config.NSMALLPOSINTS 14 | - Config.NSMALLNEGINTS]; 15 | 16 | static 17 | { 18 | for (int i = 0; i < Config.NSMALLPOSINTS - Config.NSMALLNEGINTS; i++) 19 | { 20 | integerCache[i] = new PyLong(Config.NSMALLNEGINTS + i); 21 | } 22 | } 23 | 24 | public static final PyLong newInteger(int i) 25 | { 26 | if(i < Config.NSMALLPOSINTS && i > Config.NSMALLNEGINTS) 27 | { 28 | return integerCache[(int)i - Config.NSMALLNEGINTS]; 29 | } 30 | return new PyLong(i); 31 | } 32 | 33 | public static final PyLong newInteger(long i) 34 | { 35 | if(i < Integer.MIN_VALUE || i > Integer.MAX_VALUE) 36 | { 37 | return newInteger((int)i); 38 | } 39 | else 40 | { 41 | return new PyLong(i); 42 | } 43 | } 44 | 45 | public static PyObject PyNumber_InPlaceOr(PyObject v, PyObject w) 46 | { 47 | return null; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyBoolean.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyBoolean extends PyObject 4 | { 5 | public PyBoolean(boolean b) 6 | { 7 | 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyBytes.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyBytes extends PyObject 4 | { 5 | public byte[] bytes; 6 | 7 | public PyBytes(byte[] bytes) 8 | { 9 | this.bytes = bytes; 10 | } 11 | 12 | public PyBytes(String str) 13 | { 14 | this.bytes = str.getBytes(); 15 | } 16 | 17 | public PyBytes concat(PyBytes pyBytes) 18 | { 19 | byte[] concatedBytes = pyBytes.bytes; 20 | byte[] newBytes = new byte[bytes.length + concatedBytes.length]; 21 | System.arraycopy(bytes, 0, newBytes, 0, bytes.length); 22 | System.arraycopy(concatedBytes, 0, newBytes, bytes.length, 23 | concatedBytes.length); 24 | return new PyBytes(newBytes); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyCodeObject.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyCodeObject extends PyObject 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyComplex.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyComplex extends PyObject 4 | { 5 | double real; 6 | double imag; 7 | 8 | public PyComplex(double real, double imag) 9 | { 10 | this.real = real; 11 | this.imag = imag; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyDict.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyDict extends PyObject 4 | { 5 | public PyDict() 6 | { 7 | 8 | } 9 | 10 | public boolean setItem(PyObject key, PyObject value) 11 | { 12 | return true; 13 | } 14 | 15 | public PyObject getItem(PyObject key) 16 | { 17 | return null; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyDictComm.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyDictComm extends PyObject 4 | { 5 | int hash; 6 | PyObject key; 7 | PyObject value; 8 | } 9 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyExceptions.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | import java.util.Stack; 4 | 5 | import pers.xia.jpython.parser.Node; 6 | import pers.xia.jpython.tokenizer.Token; 7 | 8 | public class PyExceptions extends RuntimeException 9 | { 10 | private static final long serialVersionUID = -5566345378602605958L; 11 | 12 | public static enum ErrorType 13 | { 14 | AST_ERROR, 15 | PARSER_ERROR, 16 | SYSTEM_ERROR, 17 | SYMTABLE_ERROR, 18 | SYNTAX_ERROR 19 | } 20 | 21 | public Stack map = new Stack(); 22 | public Token tok; 23 | 24 | public PyExceptions() 25 | { 26 | super(); 27 | } 28 | 29 | public PyExceptions(String msg) 30 | { 31 | super(msg); 32 | map.push(msg); 33 | } 34 | 35 | public PyExceptions(String msg, Throwable cause) 36 | { 37 | super(msg, cause); 38 | } 39 | 40 | public PyExceptions(ErrorType type, String msg) 41 | { 42 | this(type, msg, 0, 0); 43 | } 44 | 45 | public PyExceptions(ErrorType type, String msg, int lineno, int colOffset) 46 | { 47 | super(msg); 48 | msg += "\nlineno: " + lineno + ", colOffset: " + colOffset; 49 | map.push(msg); 50 | } 51 | 52 | public PyExceptions(String msg, Node n) 53 | { 54 | this(null, msg, n); 55 | } 56 | 57 | public PyExceptions(ErrorType type, String msg, Node n) 58 | { 59 | super(msg); 60 | msg = msg + "\n" + "line: " + n.lineNo + " colOffset: " + n.colOffset 61 | + "\n" + n.dfaType + " " + n.str; 62 | map.push(msg); 63 | } 64 | 65 | public PyExceptions(String msg, Token tok) 66 | { 67 | super(msg); 68 | msg = "line: " + tok.lineNo + "\n" 69 | + tok.buf.substring(tok.lineStart, tok.lineEnd) + "\n" + msg; 70 | map.push(msg); 71 | this.tok = tok; 72 | } 73 | 74 | public PyExceptions(ErrorType type, String msg, PyObject obj) 75 | { 76 | super(msg); 77 | map.push(msg); 78 | } 79 | 80 | public String toString() 81 | { 82 | String msg = ""; 83 | while (!map.empty()) 84 | { 85 | msg += map.pop() + "\n"; 86 | } 87 | return msg; 88 | } 89 | 90 | public void printStackTrace() 91 | { 92 | String msg = ""; 93 | while (!map.empty()) 94 | { 95 | msg += map.pop() + "\n"; 96 | } 97 | if(msg.length() > 0) 98 | { 99 | System.err.println(msg); 100 | } 101 | else 102 | { 103 | super.printStackTrace(); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyFloat.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyFloat extends PyObject 4 | { 5 | double num; 6 | 7 | public PyFloat(double num) 8 | { 9 | this.num = num; 10 | } 11 | 12 | public PyFloat(String str) 13 | { 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyList.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class PyList extends PySequence 7 | { 8 | private ArrayList list; 9 | 10 | public PyList(int n) 11 | { 12 | this.list = new ArrayList(n); 13 | } 14 | 15 | public PyList(ArrayList list) 16 | { 17 | this.list = list; 18 | } 19 | 20 | public boolean append(PyObject newitem) 21 | { 22 | // TODO Auto-generated method stub 23 | list.add(newitem); 24 | return true; 25 | } 26 | 27 | public boolean extend(PyObject newitems) 28 | { 29 | PyList items = (PyList)newitems; 30 | for(int i = 0; i < items.getSize(); i++) 31 | { 32 | this.append(items.getItem(i)); 33 | } 34 | return true; 35 | } 36 | 37 | public int getSize() 38 | { 39 | // TODO Auto-generated method stub 40 | return list.size(); 41 | } 42 | 43 | public void setSlice(int ilow, int ihigh, PyObject v) 44 | { 45 | // F complete follow 46 | if(v == null) 47 | { 48 | ArrayList l = new ArrayList(this.list.subList(0, ilow)); 49 | ArrayList l2 = new ArrayList(this.list.subList(ihigh, this.getSize())); 50 | this.list = l; 51 | this.list.addAll(l2); 52 | } 53 | else 54 | { 55 | PyList l = (PyList)v; 56 | PyList l1 = new PyList(new ArrayList(this.list.subList(ihigh, this.getSize()))); 57 | this.list = (ArrayList) this.list.subList(0, ilow); 58 | this.extend(l); 59 | this.extend(l1);; 60 | } 61 | } 62 | 63 | public PyList getSlice(int ilow, int ihigh) 64 | { 65 | ArrayList newList = new ArrayList(this.list.subList(ilow, ihigh)); 66 | return new PyList(newList); 67 | } 68 | 69 | public PyObject getItem(int l) 70 | { 71 | // TODO Auto-generated method stub 72 | return list.get(l); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyLong.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyLong extends PyObject 4 | { 5 | private long num; 6 | 7 | public PyLong(long num) 8 | { 9 | this.num = num; 10 | } 11 | 12 | public static PyObject newLong(String str) 13 | { 14 | // TODO check the str is too much longer 15 | if (str.length() <= 1) 16 | { 17 | return new PyLong(Long.parseLong((str))); 18 | } 19 | try 20 | { 21 | if (str.charAt(1) == 'x' || str.charAt(1) == 'X') 22 | { 23 | return new PyLong(Long.parseLong(str.substring(2), 16)); 24 | } 25 | else if (str.charAt(1) == 'b' || str.charAt(1) == 'B') 26 | { 27 | return new PyLong(Long.parseLong(str.substring(2), 2)); 28 | } 29 | else if (str.charAt(1) == 'o' || str.charAt(1) == 'O') 30 | { 31 | return new PyLong(Long.parseLong(str.substring(2), 8)); 32 | } 33 | else 34 | { 35 | return new PyLong(Long.parseLong((str))); 36 | } 37 | } 38 | catch (NumberFormatException err) 39 | { 40 | //System.out.println(str); 41 | return new PyNumber(str); 42 | } 43 | } 44 | 45 | public long asLong() 46 | { 47 | return num; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyNone.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyNone extends PyObject 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyNumber.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | import java.math.BigInteger; 4 | 5 | public class PyNumber extends PyObject 6 | { 7 | BigInteger num; 8 | 9 | public PyNumber(String s) 10 | { 11 | // TODO 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyObject.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PyObject 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PySequence.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public abstract class PySequence extends PyObject 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PySet.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | public class PySet extends PyObject{ 4 | 5 | public PySet(PyObject iterable) 6 | { 7 | 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/object/PyUnicode.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.object; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | 5 | import pers.xia.jpython.object.PyExceptions.ErrorType; 6 | 7 | public class PyUnicode extends PyObject 8 | { 9 | String str; 10 | 11 | public PyUnicode(byte[] b, String encode) 12 | { 13 | try 14 | { 15 | this.str = new String(b, encode); 16 | } 17 | catch (UnsupportedEncodingException e) 18 | { 19 | // TODO Auto-generated catch block 20 | e.printStackTrace(); 21 | throw new PyExceptions(ErrorType.SYSTEM_ERROR, 22 | "Can't convert bytes to str implicitly"); 23 | } 24 | } 25 | 26 | public static PyUnicode internFromString(String str) 27 | { 28 | return internFromString(str, false); 29 | } 30 | 31 | public static PyUnicode internFromString(String str, boolean rawmode) 32 | { 33 | return new PyUnicode(str, rawmode); 34 | } 35 | 36 | private PyUnicode(String str, boolean rawmode) 37 | { 38 | this.str = str; 39 | if(!rawmode) 40 | { 41 | // FIXME should use faster and more complete method 42 | this.str.replace("\\n", "\n"); 43 | this.str.replace("\\t", "\t"); 44 | this.str.replace("\\\\", "\\"); 45 | } 46 | } 47 | 48 | private PyUnicode(PyObject obj) 49 | { 50 | // TODO 51 | } 52 | 53 | public static PyUnicode concat(PyObject left, PyObject right) 54 | { 55 | if(!(left instanceof PyUnicode)) 56 | { 57 | throw new PyExceptions(ErrorType.SYSTEM_ERROR, "transform error", 58 | left); 59 | } 60 | 61 | return ((PyUnicode) left).concat(right); 62 | } 63 | 64 | public PyUnicode concat(PyObject obj) 65 | { 66 | if(!(obj instanceof PyUnicode)) 67 | { 68 | throw new PyExceptions(ErrorType.SYSTEM_ERROR, "transform error", 69 | obj); 70 | } 71 | 72 | String newStr = this.str + ((PyUnicode) obj).str; 73 | return internFromString(newStr); 74 | 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/parser/Node.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.parser; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.Stack; 7 | 8 | import pers.xia.jpython.grammar.DFAType; 9 | import pers.xia.jpython.object.PyExceptions; 10 | import pers.xia.jpython.tokenizer.TokState; 11 | 12 | public class Node 13 | { 14 | public boolean isDFAType; 15 | public DFAType dfaType; 16 | 17 | public String str; 18 | public int lineNo; 19 | public int colOffset; 20 | 21 | List childs; 22 | 23 | public Node() 24 | { 25 | // empty method 26 | } 27 | 28 | public Node(DFAType dfaType) 29 | { 30 | this.childs = new ArrayList(); 31 | this.dfaType = dfaType; 32 | this.isDFAType = true; 33 | } 34 | 35 | public Node(TokState tokType) 36 | { 37 | this.childs = new ArrayList(); 38 | this.dfaType = DFAType.valueOf(tokType.toString()); // modify TokState 39 | // to DFAType 40 | this.isDFAType = false; 41 | } 42 | 43 | // 创建一个node的拷贝 44 | /* 45 | * private Node(Node node) { this.isDFAType = node.isDFAType; this.dfaType = 46 | * node.dfaType; this.tokType = node.tokType; this.str = node.str; 47 | * this.lineNo = node.lineNo; this.colOffset = node.colOffset; } 48 | */ 49 | 50 | public void addChild(DFAType dfaName, int lineNo, int colOffset) 51 | { 52 | Node node = new Node(dfaName); 53 | 54 | node.lineNo = lineNo; 55 | node.colOffset = colOffset; 56 | 57 | this.childs.add(node); 58 | } 59 | 60 | public void addChild(TokState tokState, String str, int lineNo, 61 | int colOffset) 62 | { 63 | Node node = new Node(tokState); 64 | 65 | node.lineNo = lineNo; 66 | node.colOffset = colOffset; 67 | node.str = str; 68 | 69 | this.childs.add(node); 70 | } 71 | 72 | public Node getChild(int n) 73 | { 74 | if(n < 0) 75 | { 76 | n = this.childs.size() + n; 77 | } 78 | if(n < 0 || n > this.childs.size()) 79 | { 80 | throw new PyExceptions("Out of range by Node child's list", this); 81 | } 82 | return this.childs.get(n); 83 | } 84 | 85 | public int nChild() 86 | { 87 | return this.childs.size(); 88 | } 89 | 90 | public void show() 91 | { 92 | //save the node and there index 93 | class NodeAndIndex 94 | { 95 | Node node; 96 | int index; 97 | 98 | NodeAndIndex(Node node) 99 | { 100 | this.node = node; 101 | this.index = 0; 102 | } 103 | } 104 | 105 | Stack stack = new Stack(); 106 | LinkedList nodeNames = new LinkedList(); 107 | NodeAndIndex ni = new NodeAndIndex(this); 108 | 109 | nodeNames.add(ni.node.dfaType.toString()); 110 | stack.add(ni); 111 | 112 | while (!stack.empty()) 113 | { 114 | ni = stack.peek(); 115 | if(ni.index >= ni.node.childs.size()) 116 | { 117 | stack.pop(); 118 | nodeNames.removeLast(); 119 | continue; 120 | } 121 | 122 | Node node = ni.node.getChild(ni.index++); 123 | if(node.isDFAType) 124 | { 125 | NodeAndIndex ni2 = new NodeAndIndex(node); 126 | stack.push(ni2); 127 | nodeNames.add(ni2.node.dfaType.toString()); 128 | continue; 129 | } 130 | 131 | for (String s : nodeNames) 132 | { 133 | System.out.print(s + " "); 134 | } 135 | if(node.dfaType == DFAType.NAME) 136 | { 137 | System.out.println(node.dfaType.toString() + " " + node.str); 138 | } 139 | else 140 | { 141 | System.out.println(node.dfaType.toString()); 142 | } 143 | } 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/parser/ParseToken.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.parser; 2 | 3 | import java.io.File; 4 | 5 | import pers.xia.jpython.grammar.GramInit; 6 | import pers.xia.jpython.grammar.Grammar; 7 | import pers.xia.jpython.object.PyExceptions; 8 | import pers.xia.jpython.parser.Parser.ReturnCode; 9 | import pers.xia.jpython.tokenizer.TokState; 10 | import pers.xia.jpython.tokenizer.Token; 11 | import pers.xia.jpython.tokenizer.Tokenizer; 12 | 13 | public class ParseToken 14 | { 15 | public static Node parseFile(File file, Grammar grammar, int start) 16 | { 17 | 18 | try 19 | { 20 | Parser parser = new Parser(grammar, start); 21 | Tokenizer tokenizer = new Tokenizer(file); 22 | Token tok = tokenizer.nextToken(); 23 | int colOffset = 1; 24 | int lineNo = 1; 25 | while (parser.addToken(tok, colOffset) != ReturnCode.ACCEPT) 26 | { 27 | 28 | tok = tokenizer.nextToken(); 29 | if(tok.state != TokState.NEWLINE && tok.state != TokState.INDENT 30 | && tok.state != TokState.DEDENT) 31 | { 32 | if(tok.lineNo != lineNo) 33 | { 34 | colOffset = 1; 35 | lineNo = tok.lineNo; 36 | } 37 | else 38 | { 39 | colOffset++; 40 | } 41 | } 42 | } 43 | return parser.tree; 44 | } 45 | catch (PyExceptions e) 46 | { 47 | e.printStackTrace(); 48 | throw e; 49 | } 50 | } 51 | 52 | public static void main(String[] args) 53 | { 54 | File file = new File("test/translator.py"); 55 | Node n = ParseToken.parseFile(file, GramInit.grammar, 1); 56 | n.show(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/parser/Parser.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.parser; 2 | 3 | import java.io.File; 4 | import java.util.LinkedList; 5 | import java.util.Stack; 6 | 7 | import pers.xia.jpython.grammar.DFA; 8 | import pers.xia.jpython.grammar.DFAType; 9 | import pers.xia.jpython.grammar.GramInit; 10 | import pers.xia.jpython.grammar.Grammar; 11 | import pers.xia.jpython.grammar.State; 12 | import pers.xia.jpython.object.PyExceptions; 13 | import pers.xia.jpython.tokenizer.TokState; 14 | import pers.xia.jpython.tokenizer.Token; 15 | import pers.xia.jpython.tokenizer.Tokenizer; 16 | 17 | public class Parser 18 | { 19 | public static enum ReturnCode 20 | { 21 | OK, ACCEPT 22 | }; 23 | 24 | class StackEntry //栈中的元素 25 | { 26 | DFA dfa; //所属的DFA 27 | int curState; //当前的state 28 | Node parentNode = null; //当前的父结点,用于连接下面的结点 29 | } 30 | 31 | Stack stack; // DFA的状态栈 32 | Grammar grammar; //使用的grammar 33 | public Node tree; //CST树 34 | 35 | public Parser(Grammar grammar) 36 | { 37 | this(grammar, -1); 38 | } 39 | 40 | public Parser(Grammar grammar, int start) 41 | { 42 | if(!grammar.accel) 43 | { 44 | grammar.addAccelerators(); 45 | } 46 | //this.log = Logger.getLogger(Parser.class); 47 | 48 | StackEntry stackEntry = new StackEntry(); 49 | 50 | if(start < 0) 51 | start = grammar.start; 52 | 53 | stackEntry.dfa = grammar.getDFA(start); 54 | stackEntry.curState = grammar.getDFA(start).initial; 55 | stackEntry.parentNode = new Node(grammar.getDFA(start).name); 56 | 57 | this.stack = new Stack(); 58 | this.stack.push(stackEntry); 59 | this.grammar = grammar; 60 | this.tree = stackEntry.parentNode; 61 | } 62 | 63 | //根据Token确定相应的label 64 | private int classify(Token token) 65 | { 66 | /* 67 | * 如果token的state是NAME的话需要考虑为关键字还是普通的NAME, 68 | * 做法是如果遇到label(NAME, null)的话把他提取出来,没找到目标Label的 69 | * 情况下就返回这个label 70 | */ 71 | if(token.state == TokState.NAME) 72 | { 73 | int label = -1; // 保存(NAME, null)这个label 74 | 75 | for (int i = 0; i < this.grammar.nlabels; i++) 76 | { 77 | if(this.grammar.labels[i].tokState == TokState.NAME) 78 | { 79 | if(this.grammar.labels[i].str == null) 80 | { 81 | label = i; 82 | continue; 83 | } 84 | if(this.grammar.labels[i].str.equals(token.str)) 85 | { 86 | //log.info(token.str + " is a key word"); 87 | return i; 88 | } 89 | } 90 | } 91 | if(label == -1) 92 | throw new PyExceptions("Illegal token", token); 93 | //log.info("\"" + token.str + "\" is a token we know"); 94 | return label; 95 | } 96 | 97 | /* 98 | * 对于普通的label只需要匹配到label的tokState即可 99 | */ 100 | for (int i = 0; i < this.grammar.nlabels; i++) 101 | { 102 | if(this.grammar.labels[i].tokState == token.state) 103 | { 104 | //log.info(token.state + " is a key word"); 105 | return i; 106 | } 107 | } 108 | throw new PyExceptions("Illegal token", token); 109 | } 110 | 111 | //设置下一个结点 112 | private void shift(TokState tokState, int nextState, String str, int lineNo, 113 | int colOffset) 114 | { 115 | StackEntry se = this.stack.peek(); 116 | se.parentNode.addChild(tokState, str, lineNo, colOffset); 117 | se.curState = nextState; 118 | } 119 | 120 | //添加一个新的stackEntry 121 | private void push(DFA nextDFA, int nextState, int lineNo, int colOffset) 122 | { 123 | StackEntry se = this.stack.peek(); 124 | 125 | se.parentNode.addChild(nextDFA.name, lineNo, colOffset); 126 | se.curState = nextState; 127 | 128 | Node node = se.parentNode.getChild(-1); 129 | 130 | StackEntry se1 = new StackEntry(); 131 | se1.dfa = nextDFA; 132 | se1.curState = nextDFA.initial; 133 | se1.parentNode = node; 134 | 135 | this.stack.push(se1); 136 | } 137 | 138 | public ReturnCode addToken(Token token, int colOffset) 139 | { 140 | int ilabel = this.classify(token); 141 | 142 | for (;;) 143 | { 144 | StackEntry se = this.stack.peek(); 145 | DFA dfa = se.dfa; 146 | State state = dfa.getState(se.curState); 147 | 148 | //log.debug("DFA: " + dfa.name); 149 | 150 | if(ilabel >= state.lower && ilabel < state.upper) 151 | { 152 | int x = state.next(ilabel - state.lower); 153 | 154 | if(x > -1) 155 | { 156 | if((x & (1 << 7)) > 0) 157 | { 158 | //log.debug("push..."); 159 | DFA dfa1 = grammar.getDFA(x >> 8); 160 | int nextState = x & ((1 << 7) - 1); 161 | this.push(dfa1, nextState, token.lineNo, colOffset); 162 | continue; 163 | } 164 | 165 | //log.debug("shift..."); 166 | this.shift(token.state, x, token.str, token.lineNo, 167 | colOffset); 168 | 169 | /* 170 | * Pop while we are in an accept-only state 171 | */ 172 | 173 | state = dfa.getState(this.stack.peek().curState); 174 | while (state.accept && state.narcs == 1) 175 | { 176 | //log.debug("Pop while singal accept-only state..."); 177 | //log.debug("DFA before Pop: " 178 | // + this.stack.peek().dfa.name); 179 | this.stack.pop(); 180 | if(this.stack.empty()) 181 | { 182 | //log.debug("accept"); 183 | return ReturnCode.ACCEPT; 184 | } 185 | se = this.stack.peek(); 186 | dfa = se.dfa; 187 | state = dfa.getState(se.curState); 188 | } 189 | return ReturnCode.OK; 190 | } 191 | } 192 | 193 | if(state.accept) 194 | { 195 | //log.debug("Pop while accept..."); 196 | //log.debug("DFA before Pop: " + this.stack.peek().dfa.name); 197 | this.stack.pop(); 198 | if(this.stack.empty()) 199 | { 200 | new PyExceptions(" Error: bottom of stack.\n"); 201 | } 202 | continue; 203 | } 204 | 205 | //log.error("token: " + token.state + " " + token.str + " ilabel: " 206 | // + ilabel + " curState: " + se.curState + " lower: " 207 | // + state.lower + " lineNo: " + token.lineNo); 208 | throw new PyExceptions("Illigal token: ", token); 209 | } 210 | } 211 | 212 | public static void main(String[] args) 213 | { 214 | File file = new File("test/test.py"); 215 | 216 | try 217 | { 218 | Parser parser = new Parser(GramInit.grammar, 1); 219 | Tokenizer tokenizer = new Tokenizer(file); 220 | Token tok = tokenizer.nextToken(); 221 | int colOffset = 1; 222 | int lineNo = 1; 223 | while (parser.addToken(tok, colOffset) != ReturnCode.ACCEPT) 224 | { 225 | 226 | tok = tokenizer.nextToken(); 227 | if(tok.lineNo != lineNo) 228 | { 229 | colOffset = 1; 230 | lineNo = tok.lineNo; 231 | } 232 | else 233 | { 234 | colOffset++; 235 | } 236 | //System.out.println(tok + " " + colOffset); 237 | } 238 | parser.tree.show(); 239 | } 240 | catch (PyExceptions e) 241 | { 242 | e.printStackTrace(); 243 | throw e; 244 | } 245 | } 246 | } 247 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/tokenizer/ErrorCode.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.tokenizer; 2 | // Necessary? Maybe this file while be removed while Exception is completed 3 | public enum ErrorCode 4 | { 5 | E_OK, 6 | E_EOF, 7 | E_INTR, 8 | E_TOKEN, 9 | E_SYNTAX, 10 | E_NOMEM, 11 | E_DONE, 12 | E_ERROR, 13 | E_TABSPACE, 14 | E_OVERFLOW, 15 | E_TOODEEP, 16 | E_DEDENT, 17 | E_DECODE, 18 | E_EOFS, 19 | E_EOLS, 20 | E_LINECONT, 21 | E_IDENTIFIER, 22 | E_BADSINGLE, 23 | } 24 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/tokenizer/TokState.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.tokenizer; 2 | 3 | public enum TokState 4 | { 5 | ENDMARKER, 6 | NAME, 7 | NUMBER, 8 | STRING, 9 | NEWLINE, 10 | INDENT, 11 | DEDENT, 12 | LPAR, // ( 13 | RPAR, // ) 14 | LSQB, // [ 15 | RSQB, // ] 16 | COLON, // : 17 | COMMA, // , 18 | SEMI, // ; 19 | PLUS, // + 20 | MINUS, // - 21 | STAR, // * 22 | SLASH, // / 23 | VBAR, // | 24 | AMPER, // & 25 | LESS, // < 26 | GREATER, // > 27 | EQUAL, // = 28 | DOT, // . 29 | PERCENT, // % 30 | BACKQUOTE, 31 | LBRACE, // { 32 | RBRACE, // } 33 | EQEQUAL, // == 34 | NOTEQUAL, // != 35 | LESSEQUAL, // <= 36 | GREATEREQUAL, // >= 37 | TILDE, // ~ 38 | CIRCUMFLEX, // ^ 39 | LEFTSHIFT, // << 40 | RIGHTSHIFT, // >> 41 | DOUBLESTAR, // ** 42 | PLUSEQUAL, // += 43 | MINEQUAL, // -= 44 | STAREQUAL, // *= 45 | SLASHEQUAL, // /= 46 | PERCENTEQUAL, // %= 47 | AMPEREQUAL, // &= 48 | VBAREQUAL, // |= 49 | CIRCUMFLEXEQUAL, // ^= 50 | LEFTSHIFTEQUAL, // <<= 51 | RIGHTSHIFTEQUAL, // >>= 52 | DOUBLESTAREQUAL, // **= 53 | DOUBLESLASH, // \\ 54 | DOUBLESLASHEQUAL, // \\= 55 | AT, // @ 56 | ATEQUAL, // @= 57 | RARROW, // -> 58 | ELLIPSIS, // ... 59 | OP, 60 | AWAIT, // await 61 | ASYNC, // async 62 | ERRORTOKEN, 63 | N_TOKENS, 64 | } 65 | -------------------------------------------------------------------------------- /src/pers/xia/jpython/tokenizer/Tokenizer.java: -------------------------------------------------------------------------------- 1 | package pers.xia.jpython.tokenizer; 2 | 3 | import java.io.File; 4 | 5 | import pers.xia.jpython.object.PyExceptions; 6 | 7 | public class Tokenizer 8 | { 9 | private Token tok; 10 | 11 | public Tokenizer() 12 | { 13 | } 14 | 15 | public Tokenizer(File file) 16 | { 17 | this(); 18 | this.tok = new Token(file); 19 | } 20 | 21 | public Token nextToken() 22 | { 23 | this.tok.get(); 24 | this.tok.str = this.tok.buf.substring(this.tok.start, this.tok.end); 25 | return tok; 26 | } 27 | 28 | public static void main(String[] args) 29 | { 30 | File file = new File("./test/test.py"); 31 | try 32 | { 33 | Tokenizer tokenizer = new Tokenizer(file); 34 | Token tok = tokenizer.nextToken(); 35 | while (tok.state != TokState.ENDMARKER) 36 | { 37 | System.out.println(tok); 38 | tok = tokenizer.nextToken(); 39 | } 40 | } 41 | catch (PyExceptions e) 42 | { 43 | e.printStackTrace(); 44 | } 45 | } 46 | } 47 | --------------------------------------------------------------------------------