forked from zhangj111/astnn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
85 lines (71 loc) · 2.51 KB
/
utils.py
File metadata and controls
85 lines (71 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pandas as pd
import javalang
from javalang.ast import Node
from tree import ASTNode, BlockNode
import sys
import os
sys.setrecursionlimit(10000)
def get_token(node):
token = ''
if isinstance(node, str):
token = node
elif isinstance(node, set):
token = 'Modifier'#node.pop()
elif isinstance(node, Node):
token = node.__class__.__name__
return token
def get_children(root):
if isinstance(root, Node):
children = root.children
elif isinstance(root, set):
children = list(root)
else:
children = []
def expand(nested_list):
for item in nested_list:
if isinstance(item, list):
for sub_item in expand(item):
yield sub_item
elif item:
yield item
return list(expand(children))
def get_sequence(node, sequence):
token, children = get_token(node), get_children(node)
sequence.append(token)
for child in children:
get_sequence(child, sequence)
if token in ['ForStatement', 'WhileStatement', 'DoStatement','SwitchStatement', 'IfStatement']:
sequence.append('End')
def get_blocks_v1(node, block_seq):
name, children = get_token(node), get_children(node)
logic = ['SwitchStatement','IfStatement', 'ForStatement', 'WhileStatement', 'DoStatement']
if name in ['MethodDeclaration', 'ConstructorDeclaration']:
block_seq.append(BlockNode(node))
body = node.body
for child in body:
if get_token(child) not in logic and not hasattr(child, 'block'):
block_seq.append(BlockNode(child))
else:
get_blocks_v1(child, block_seq)
elif name in logic:
block_seq.append(BlockNode(node))
for child in children[1:]:
token = get_token(child)
if not hasattr(node, 'block') and token not in logic+['BlockStatement']:
block_seq.append(BlockNode(child))
else:
get_blocks_v1(child, block_seq)
block_seq.append(BlockNode('End'))
elif name is 'BlockStatement' or hasattr(node, 'block'):
block_seq.append(BlockNode(name))
for child in children:
if get_token(child)not in logic:
block_seq.append(BlockNode(child))
else:
get_blocks_v1(child, block_seq)
else:
for child in children:
get_blocks_v1(child, block_seq)
def check_or_create(path):
if not os.path.exists(path):
os.mkdir(path)