Skip to content

Commit 6c602c9

Browse files
authored
readd some of the documentation that helps to understand how RestrictedPython works internally (#177)
* readd some of the documentation that helps to understand how RestrictedPython works internally, and the AST Grammars for comparision * formating and diff for ast files
1 parent 9c94d26 commit 6c602c9

21 files changed

+1958
-1
lines changed

docs/contributing/ast/python2_6.ast

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
-- Python 2.6 AST
2+
-- ASDL's five builtin types are identifier, int, string, object, bool
3+
4+
module Python version "2.6"
5+
{
6+
mod = Module(stmt* body)
7+
| Interactive(stmt* body)
8+
| Expression(expr body)
9+
10+
-- not really an actual node but useful in Jython's typesystem.
11+
| Suite(stmt* body)
12+
13+
stmt = FunctionDef(identifier name,
14+
arguments args,
15+
stmt* body,
16+
expr* decorator_list)
17+
| ClassDef(identifier name, expr* bases, stmt* body, expr *decorator_list)
18+
| Return(expr? value)
19+
20+
| Delete(expr* targets)
21+
| Assign(expr* targets, expr value)
22+
| AugAssign(expr target, operator op, expr value)
23+
24+
-- not sure if bool is allowed, can always use int
25+
| Print(expr? dest, expr* values, bool nl)
26+
27+
-- use 'orelse' because else is a keyword in target languages
28+
| For(expr target, expr iter, stmt* body, stmt* orelse)
29+
| While(expr test, stmt* body, stmt* orelse)
30+
| If(expr test, stmt* body, stmt* orelse)
31+
| With(expr context_expr, expr? optional_vars, stmt* body)
32+
33+
-- 'type' is a bad name
34+
| Raise(expr? type, expr? inst, expr? tback)
35+
| TryExcept(stmt* body, excepthandler* handlers, stmt* orelse)
36+
| TryFinally(stmt* body, stmt* finalbody)
37+
| Assert(expr test, expr? msg)
38+
39+
| Import(alias* names)
40+
| ImportFrom(identifier module, alias* names, int? level)
41+
42+
-- Doesn't capture requirement that locals must be
43+
-- defined if globals is
44+
-- still supports use as a function!
45+
| Exec(expr body, expr? globals, expr? locals)
46+
47+
| Global(identifier* names)
48+
| Expr(expr value)
49+
| Pass | Break | Continue
50+
51+
-- XXX Jython will be different
52+
-- col_offset is the byte offset in the utf8 string the parser uses
53+
attributes (int lineno, int col_offset)
54+
55+
-- BoolOp() can use left & right?
56+
expr = BoolOp(boolop op, expr* values)
57+
| BinOp(expr left, operator op, expr right)
58+
| UnaryOp(unaryop op, expr operand)
59+
| Lambda(arguments args, expr body)
60+
| IfExp(expr test, expr body, expr orelse)
61+
| Dict(expr* keys, expr* values)
62+
| ListComp(expr elt, comprehension* generators)
63+
| GeneratorExp(expr elt, comprehension* generators)
64+
-- the grammar constrains where yield expressions can occur
65+
| Yield(expr? value)
66+
-- need sequences for compare to distinguish between
67+
-- x < 4 < 3 and (x < 4) < 3
68+
| Compare(expr left, cmpop* ops, expr* comparators)
69+
| Call(expr func, expr* args, keyword* keywords, expr? starargs, expr? kwargs)
70+
| Repr(expr value)
71+
| Num(object n) -- a number as a PyObject.
72+
| Str(string s) -- need to specify raw, unicode, etc?
73+
-- other literals? bools?
74+
75+
-- the following expression can appear in assignment context
76+
| Attribute(expr value, identifier attr, expr_context ctx)
77+
| Subscript(expr value, slice slice, expr_context ctx)
78+
| Name(identifier id, expr_context ctx)
79+
| List(expr* elts, expr_context ctx)
80+
| Tuple(expr* elts, expr_context ctx)
81+
82+
-- col_offset is the byte offset in the utf8 string the parser uses
83+
attributes (int lineno, int col_offset)
84+
85+
expr_context = Load
86+
| Store
87+
| Del
88+
| AugLoad
89+
| AugStore
90+
| Param
91+
92+
slice = Ellipsis
93+
| Slice(expr? lower, expr? upper, expr? step)
94+
| ExtSlice(slice* dims)
95+
| Index(expr value)
96+
97+
boolop = And
98+
| Or
99+
100+
operator = Add
101+
| Sub
102+
| Mult
103+
| Div
104+
| Mod
105+
| Pow
106+
| LShift
107+
| RShift
108+
| BitOr
109+
| BitXor
110+
| BitAnd
111+
| FloorDiv
112+
113+
unaryop = Invert
114+
| Not
115+
| UAdd
116+
| USub
117+
118+
cmpop = Eq
119+
| NotEq
120+
| Lt
121+
| LtE
122+
| Gt
123+
| GtE
124+
| Is
125+
| IsNot
126+
| In
127+
| NotIn
128+
129+
comprehension = (expr target, expr iter, expr* ifs)
130+
131+
-- not sure what to call the first argument for raise and except
132+
excepthandler = ExceptHandler(expr? type, expr? name, stmt* body)
133+
attributes (int lineno, int col_offset)
134+
135+
arguments = (expr* args, identifier? vararg,
136+
identifier? kwarg, expr* defaults)
137+
138+
-- keyword arguments supplied to call
139+
keyword = (identifier arg, expr value)
140+
141+
-- import name with optional 'as' alias.
142+
alias = (identifier name, identifier? asname)
143+
}

docs/contributing/ast/python2_7.ast

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
-- Python 2.7 AST
2+
-- ASDL's five builtin types are identifier, int, string, object, bool
3+
4+
module Python version "2.7"
5+
{
6+
mod = Module(stmt* body)
7+
| Interactive(stmt* body)
8+
| Expression(expr body)
9+
10+
-- not really an actual node but useful in Jython's typesystem.
11+
| Suite(stmt* body)
12+
13+
stmt = FunctionDef(identifier name,
14+
arguments args,
15+
stmt* body,
16+
expr* decorator_list)
17+
| ClassDef(identifier name, expr* bases, stmt* body, expr* decorator_list)
18+
| Return(expr? value)
19+
20+
| Delete(expr* targets)
21+
| Assign(expr* targets, expr value)
22+
| AugAssign(expr target, operator op, expr value)
23+
24+
-- not sure if bool is allowed, can always use int
25+
| Print(expr? dest, expr* values, bool nl)
26+
27+
-- use 'orelse' because else is a keyword in target languages
28+
| For(expr target, expr iter, stmt* body, stmt* orelse)
29+
| While(expr test, stmt* body, stmt* orelse)
30+
| If(expr test, stmt* body, stmt* orelse)
31+
| With(expr context_expr, expr? optional_vars, stmt* body)
32+
33+
-- 'type' is a bad name
34+
| Raise(expr? type, expr? inst, expr? tback)
35+
| TryExcept(stmt* body, excepthandler* handlers, stmt* orelse)
36+
| TryFinally(stmt* body, stmt* finalbody)
37+
| Assert(expr test, expr? msg)
38+
39+
| Import(alias* names)
40+
| ImportFrom(identifier? module, alias* names, int? level)
41+
42+
-- Doesn't capture requirement that locals must be
43+
-- defined if globals is
44+
-- still supports use as a function!
45+
| Exec(expr body, expr? globals, expr? locals)
46+
47+
| Global(identifier* names)
48+
| Expr(expr value)
49+
| Pass | Break | Continue
50+
51+
-- XXX Jython will be different
52+
-- col_offset is the byte offset in the utf8 string the parser uses
53+
attributes (int lineno, int col_offset)
54+
55+
-- BoolOp() can use left & right?
56+
expr = BoolOp(boolop op, expr* values)
57+
| BinOp(expr left, operator op, expr right)
58+
| UnaryOp(unaryop op, expr operand)
59+
| Lambda(arguments args, expr body)
60+
| IfExp(expr test, expr body, expr orelse)
61+
| Dict(expr* keys, expr* values)
62+
| Set(expr* elts)
63+
| ListComp(expr elt, comprehension* generators)
64+
| SetComp(expr elt, comprehension* generators)
65+
| DictComp(expr key, expr value, comprehension* generators)
66+
| GeneratorExp(expr elt, comprehension* generators)
67+
-- the grammar constrains where yield expressions can occur
68+
| Yield(expr? value)
69+
-- need sequences for compare to distinguish between
70+
-- x < 4 < 3 and (x < 4) < 3
71+
| Compare(expr left, cmpop* ops, expr* comparators)
72+
| Call(expr func, expr* args, keyword* keywords, expr? starargs, expr? kwargs)
73+
| Repr(expr value)
74+
| Num(object n) -- a number as a PyObject.
75+
| Str(string s) -- need to specify raw, unicode, etc?
76+
-- other literals? bools?
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+
| Name(identifier id, expr_context ctx)
82+
| List(expr* elts, expr_context ctx)
83+
| Tuple(expr* elts, expr_context ctx)
84+
85+
-- col_offset is the byte offset in the utf8 string the parser uses
86+
attributes (int lineno, int col_offset)
87+
88+
expr_context = Load
89+
| Store
90+
| Del
91+
| AugLoad
92+
| AugStore
93+
| Param
94+
95+
slice = Ellipsis
96+
| Slice(expr? lower, expr? upper, expr? step)
97+
| ExtSlice(slice* dims)
98+
| Index(expr value)
99+
100+
boolop = And
101+
| Or
102+
103+
operator = Add
104+
| Sub
105+
| Mult
106+
| Div
107+
| Mod
108+
| Pow
109+
| LShift
110+
| RShift
111+
| BitOr
112+
| BitXor
113+
| BitAnd
114+
| FloorDiv
115+
116+
unaryop = Invert
117+
| Not
118+
| UAdd
119+
| USub
120+
121+
cmpop = Eq
122+
| NotEq
123+
| Lt
124+
| LtE
125+
| Gt
126+
| GtE
127+
| Is
128+
| IsNot
129+
| In
130+
| NotIn
131+
132+
comprehension = (expr target, expr iter, expr* ifs)
133+
134+
-- not sure what to call the first argument for raise and except
135+
excepthandler = ExceptHandler(expr? type, expr? name, stmt* body)
136+
attributes (int lineno, int col_offset)
137+
138+
arguments = (expr* args, identifier? vararg,
139+
identifier? kwarg, expr* defaults)
140+
141+
-- keyword arguments supplied to call
142+
keyword = (identifier arg, expr value)
143+
144+
-- import name with optional 'as' alias.
145+
alias = (identifier name, identifier? asname)
146+
}

0 commit comments

Comments
 (0)