Python.asdl 4.24 KB
Newer Older
1
-- ASDL's four builtin types are identifier, int, string, object
Jeremy Hylton's avatar
Jeremy Hylton committed
2

3
module Python version "$Revision$"
Jeremy Hylton's avatar
Jeremy Hylton committed
4 5 6 7 8 9 10 11 12
{
	mod = Module(stmt* body)
	    | Interactive(stmt* body)
	    | Expression(expr body)

	    -- not really an actual node but useful in Jython's typesystem.
	    | Suite(stmt* body)

	stmt = FunctionDef(identifier name, arguments args, 
13
                           stmt* body, expr* decorator_list, expr? returns)
14 15 16 17 18
	      | ClassDef(identifier name, 
			 expr* bases,
			 keyword* keywords,
			 expr? starargs,
			 expr? kwargs,
19
			 stmt* body,
20
			 expr* decorator_list)
Jeremy Hylton's avatar
Jeremy Hylton committed
21 22 23 24 25 26 27 28 29 30
	      | Return(expr? value)

	      | Delete(expr* targets)
	      | Assign(expr* targets, expr value)
	      | AugAssign(expr target, operator op, expr value)

	      -- use 'orelse' because else is a keyword in target languages
	      | For(expr target, expr iter, stmt* body, stmt* orelse)
	      | While(expr test, stmt* body, stmt* orelse)
	      | If(expr test, stmt* body, stmt* orelse)
31
	      | With(expr context_expr, expr? optional_vars, stmt* body)
Jeremy Hylton's avatar
Jeremy Hylton committed
32

33
	      | Raise(expr? exc, expr? cause)
Jeremy Hylton's avatar
Jeremy Hylton committed
34 35 36 37 38
	      | TryExcept(stmt* body, excepthandler* handlers, stmt* orelse)
	      | TryFinally(stmt* body, stmt* finalbody)
	      | Assert(expr test, expr? msg)

	      | Import(alias* names)
Benjamin Peterson's avatar
Benjamin Peterson committed
39
	      | ImportFrom(identifier? module, alias* names, int? level)
Jeremy Hylton's avatar
Jeremy Hylton committed
40 41

	      | Global(identifier* names)
42
	      | Nonlocal(identifier* names)
Jeremy Hylton's avatar
Jeremy Hylton committed
43 44 45 46
	      | Expr(expr value)
	      | Pass | Break | Continue

	      -- XXX Jython will be different
47 48
	      -- col_offset is the byte offset in the utf8 string the parser uses
	      attributes (int lineno, int col_offset)
Jeremy Hylton's avatar
Jeremy Hylton committed
49 50 51 52 53 54

	      -- BoolOp() can use left & right?
	expr = BoolOp(boolop op, expr* values)
	     | BinOp(expr left, operator op, expr right)
	     | UnaryOp(unaryop op, expr operand)
	     | Lambda(arguments args, expr body)
55
	     | IfExp(expr test, expr body, expr orelse)
Jeremy Hylton's avatar
Jeremy Hylton committed
56
	     | Dict(expr* keys, expr* values)
57
	     | Set(expr* elts)
Jeremy Hylton's avatar
Jeremy Hylton committed
58
	     | ListComp(expr elt, comprehension* generators)
59 60
	     | SetComp(expr elt, comprehension* generators)
	     | DictComp(expr key, expr value, comprehension* generators)
Jeremy Hylton's avatar
Jeremy Hylton committed
61
	     | GeneratorExp(expr elt, comprehension* generators)
62
	     -- the grammar constrains where yield expressions can occur
Jeremy Hylton's avatar
Jeremy Hylton committed
63 64 65 66 67 68 69 70
	     | Yield(expr? value)
	     -- need sequences for compare to distinguish between
	     -- x < 4 < 3 and (x < 4) < 3
	     | Compare(expr left, cmpop* ops, expr* comparators)
	     | Call(expr func, expr* args, keyword* keywords,
			 expr? starargs, expr? kwargs)
	     | Num(object n) -- a number as a PyObject.
	     | Str(string s) -- need to specify raw, unicode, etc?
Thomas Wouters's avatar
Thomas Wouters committed
71
	     | Bytes(string s)
72
	     | Ellipsis
Jeremy Hylton's avatar
Jeremy Hylton committed
73 74 75 76 77
	     -- other literals? bools?

	     -- the following expression can appear in assignment context
	     | Attribute(expr value, identifier attr, expr_context ctx)
	     | Subscript(expr value, slice slice, expr_context ctx)
78
	     | Starred(expr value, expr_context ctx)
Jeremy Hylton's avatar
Jeremy Hylton committed
79 80
	     | Name(identifier id, expr_context ctx)
	     | List(expr* elts, expr_context ctx) 
81
	     | Tuple(expr* elts, expr_context ctx)
Jeremy Hylton's avatar
Jeremy Hylton committed
82

83 84
	      -- col_offset is the byte offset in the utf8 string the parser uses
	      attributes (int lineno, int col_offset)
Jeremy Hylton's avatar
Jeremy Hylton committed
85 86 87

	expr_context = Load | Store | Del | AugLoad | AugStore | Param

88
	slice = Slice(expr? lower, expr? upper, expr? step) 
Jeremy Hylton's avatar
Jeremy Hylton committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	      | ExtSlice(slice* dims) 
	      | Index(expr value) 

	boolop = And | Or 

	operator = Add | Sub | Mult | Div | Mod | Pow | LShift 
                 | RShift | BitOr | BitXor | BitAnd | FloorDiv

	unaryop = Invert | Not | UAdd | USub

	cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn

	comprehension = (expr target, expr iter, expr* ifs)

	-- not sure what to call the first argument for raise and except
104 105
	excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)
	                attributes (int lineno, int col_offset)
Jeremy Hylton's avatar
Jeremy Hylton committed
106

107 108 109 110
	arguments = (arg* args, identifier? vararg, expr? varargannotation,
                     arg* kwonlyargs, identifier? kwarg,
                     expr? kwargannotation, expr* defaults,
                     expr* kw_defaults)
111
	arg = (identifier arg, expr? annotation)
Jeremy Hylton's avatar
Jeremy Hylton committed
112 113 114 115 116 117 118

        -- keyword arguments supplied to call
        keyword = (identifier arg, expr value)

        -- import name with optional 'as' alias.
        alias = (identifier name, identifier? asname)
}
119