-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.py
128 lines (99 loc) · 3.63 KB
/
Utility.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
__author__ = 'alessandro'
from xml.dom import minidom
class Tokenizen_file:
"""it represents the xml file that contains all the information about the related cpp file and the tokens"""
def __init__(self, file_name):
self.line_no = 0
self.file_name = file_name
self.file_tree = minidom.parse(file_name)
self.item_list = self.file_tree.getElementsByTagName('line')
self.size = len(self.item_list)
def token_at(self, i):
if i >= self.size or i + self.line_no >= self.size:
return Token("TOK_DONT_CARE", 0, "")
token = self.item_list[self.line_no + i].attributes['token'].value
line = self.item_list[self.line_no + i].attributes['number'].value
text = self.item_list[self.line_no + i].attributes['text'].value
return Token(token, line, text)
def next(self):
self.line_no += 1
def size(self):
return self.size
class Token:
def __init__(self, token, line, text):
self.token = token
self.text = text
self.line = line
class ClassCollection:
def __init__(self):
self.classes = []
def add_class(self, class_to_insert):
class_present = False
for c in self.classes:
if c.name == class_to_insert.name:
class_present = True
if not class_present:
self.classes.append(class_to_insert)
def index_of(self, class_obj):
return self.classes.index(class_obj)
def class_from_name(self, name):
"""creates or return the class with that name"""
for c in self.classes:
if c.name == name:
return c
new_class = Class(name)
self.classes.append(new_class)
return new_class
class Class:
"""object to keep information about the current class"""
def __init__(self, file_name):
self.name = self.class_name_from_file_name(file_name)
self.file = ''
self.external = True
self.line = 0
self.size = 0
self.last_line = 0
self.superclasses = []
self.instances = []
self.class_parentheses = 0
self.class_scope = False
self.is_superclass = False
def increase_size(self, token):
if self.size == 0:
self.last_line = token.line
self.size += 1
return
if self.last_line != token.line:
self.last_line = token.line
self.size += 1
def set_file_name(self, file_name):
self.file = file_name
self.external = False
def set_line(self, line):
self.line = line
def set_superclass(self):
self.is_superclass = True
def class_name_from_file_name(self, file_name):
file_name_split = file_name.split('/')
file_name_w_extension = file_name_split.pop()
file_name_w_extension_split = file_name_w_extension.split('.')
return file_name_w_extension_split[0]
def change_class(self, name):
self.class_parentheses = 0
self.class_scope = True
self.name = name
def class_parentheses_increase(self):
if self.class_parentheses == 0:
self.class_scope = True
self.class_parentheses += 1
def class_parentheses_decrease(self):
self.class_parentheses -= 1
if self.class_parentheses == 0:
self.class_scope = False
class Instance:
"""information about the call"""
def __init__(self, class_instantiated, call_type, code_line, file_name):
self.call_type = call_type
self.class_instantiated = class_instantiated
self.code_line = code_line
self.file_name = file_name