Skip to content

Commit 26e3d7f

Browse files
Merge pull request RustPython#1146 from RustPython/ast-module
Implement more nodes in the ast.
2 parents 8516ad2 + e06de30 commit 26e3d7f

File tree

4 files changed

+222
-106
lines changed

4 files changed

+222
-106
lines changed

crawl_sourcecode.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import ast
3+
import sys
4+
5+
filename = sys.argv[1]
6+
print('Crawling file:', filename)
7+
8+
9+
with open(filename, 'r') as f:
10+
source = f.read()
11+
12+
t = ast.parse(source)
13+
print(t)
14+
15+
shift = 3
16+
def print_node(node, indent=0):
17+
if isinstance(node, ast.AST):
18+
print(' '*indent, "NODE", node.__class__.__name__)
19+
for field in node._fields:
20+
print(' '*indent,'-', field)
21+
f = getattr(node, field)
22+
if isinstance(f, list):
23+
for f2 in f:
24+
print_node(f2, indent=indent+shift)
25+
else:
26+
print_node(f, indent=indent+shift)
27+
else:
28+
print(' '*indent, 'OBJ', node)
29+
30+
print_node(t)
31+
32+
# print(ast.dump(t))
33+

parser/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub enum Statement {
5555
Break,
5656
Continue,
5757
Return {
58-
value: Option<Box<Expression>>,
58+
value: Option<Expression>,
5959
},
6060
Import {
6161
import_parts: Vec<SingleImport>,

parser/src/python.lalrpop

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ FlowStatement: ast::LocatedStatement = {
168168
node: ast::Statement::Continue,
169169
}
170170
},
171-
<loc:@L> "return" <t:TestList?> => {
171+
<loc:@L> "return" <value:TestList?> => {
172172
ast::LocatedStatement {
173173
location: loc,
174-
node: ast::Statement::Return { value: t.map(Box::new) },
174+
node: ast::Statement::Return { value },
175175
}
176176
},
177177
<loc:@L> <y:YieldExpr> => {

0 commit comments

Comments
 (0)