forked from BentleyJOakes/PCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_xml.py
207 lines (138 loc) · 8.5 KB
/
to_xml.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import xml.etree.cElementTree as ET
def write_xml(program, name):
root = ET.Element("root")
root.set("kind", "XML")
write_node(program, root)
tree = ET.ElementTree(root)
tree.write(name + ".xml")
def write_node(node, parent):
node_xml = ET.SubElement(parent, str(node['kind']))
used_tokens = []
for child in node['children']:
used_tokens += write_node(child, node_xml)
#get unique set of tokens
tokens = node['tokens']
#for t in tokens:
# print("==\t\t" + str(t))
for t in used_tokens:
if t == None:
continue
tokens.remove(t)
for attrib in node:
if attrib == "children" or attrib == "tokens":
continue
if attrib == "spelling" and node[attrib] == None:
continue
if attrib == "location" and node[attrib] == None:
continue
node_xml.set(attrib, str(node[attrib]))
node_kind = str(node['kind'])
#print(node_kind)
tokens_to_use = []
if "LITERAL" in node_kind:
if node_kind == "CursorKind.CXX_BOOL_LITERAL_EXPR":
#needs to find true/false
#note that this is C++
tokens_to_use.append(remove_token_kind("TokenKind.KEYWORD", tokens))
else:
tokens_to_use.append(remove_token_kind("TokenKind.LITERAL", tokens))
elif node_kind == "CursorKind.DECL_REF_EXPR":
tokens_to_use.append(remove_token_kind("TokenKind.IDENTIFIER", tokens))
elif node_kind == "CursorKind.BINARY_OPERATOR" or node_kind == "CursorKind.UNARY_OPERATOR":
tokens_to_use.append(remove_token_kind("TokenKind.PUNCTUATION", tokens))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, required = False, spelling = ';'))
elif node_kind == "CursorKind.DECL_STMT":
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '='))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ';', required = False))
elif node_kind == "CursorKind.VAR_DECL":
tokens_to_use.append(remove_token_kind("TokenKind.KEYWORD", tokens))
#could be 'long long'
tokens_to_use.append(remove_token_kind("TokenKind.KEYWORD", tokens, required = False))
tokens_to_use.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, required = False, spelling = '*'))
tokens_to_use.append(remove_token_kind("TokenKind.IDENTIFIER", tokens))
elif node_kind == "CursorKind.COMPOUND_ASSIGNMENT_OPERATOR":
#examples: '/=', '+='
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ';'))
elif node_kind == "CursorKind.COMPOUND_STMT":
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '{'))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '}'))
while True:
t = remove_token_kind("TokenKind.COMMENT", tokens, required = False)
if t == None:
break
tokens_to_use.append(t)
elif node_kind == "CursorKind.PAREN_EXPR":
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '('))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ')'))
elif node_kind == "CursorKind.IF_STMT":
#remove 'if' and '(' and ')' but don't output to XML
used_tokens.append(remove_token_kind("TokenKind.KEYWORD", tokens, spelling = 'if'))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '('))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ')'))
used_tokens.append(remove_token_kind("TokenKind.KEYWORD", tokens, spelling = 'else', required = False))
elif node_kind == "CursorKind.FOR_STMT":
used_tokens.append(remove_token_kind("TokenKind.KEYWORD", tokens, spelling = 'for'))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '('))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ';', required = False))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ';', required = False))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ')'))
elif node_kind == "CursorKind.WHILE_STMT":
used_tokens.append(remove_token_kind("TokenKind.KEYWORD", tokens, spelling = 'while'))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '('))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ')'))
elif node_kind == "CursorKind.CALL_EXPR":
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '('))
#remove all commas from the call expression
while True:
t = remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ',', required = False)
if t == None:
break
used_tokens.append(t)
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ')'))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ';', required = False))
elif node_kind == "CursorKind.PARM_DECL":
tokens_to_use.append(remove_token_kind("TokenKind.KEYWORD", tokens))
tokens_to_use.append(remove_token_kind("TokenKind.IDENTIFIER", tokens))
#keep pointer and array info
tokens_to_use.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, required = False, spelling = '&'))
tokens_to_use.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, required = False, spelling = '*'))
tokens_to_use.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, required = False, spelling = '['))
tokens_to_use.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, required = False, spelling = ']'))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, required = False, spelling = ','))
elif node_kind == "CursorKind.BREAK_STMT":
tokens_to_use.append(remove_token_kind("TokenKind.KEYWORD", tokens, spelling = 'break'))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ';'))
elif node_kind == "CursorKind.RETURN_STMT":
tokens_to_use.append(remove_token_kind("TokenKind.KEYWORD", tokens, spelling = 'return'))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ';'))
elif node_kind == "CursorKind.FUNCTION_DECL":
tokens_to_use.append(remove_token_kind("TokenKind.KEYWORD", tokens))
tokens_to_use.append(remove_token_kind("TokenKind.IDENTIFIER", tokens))
#from params
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '(', required = False))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = ')', required = False))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '{', required = False))
used_tokens.append(remove_token_kind("TokenKind.PUNCTUATION", tokens, spelling = '}', required = False))
#mark the token as consumed and output it to XML
#keep ordering of tokens
for t in tokens_to_use:
if t == None:
continue
node_xml.set(t.kind, t.spelling)
used_tokens.append(t)
#for t in tokens:
# print("\t\t" + str(t))
return used_tokens
def remove_token_kind(kind, tokens, reverse = False, required = True, spelling = None):
if reverse:
tokens = reversed(tokens)
for t in tokens:
#make sure kind matches, and potentially do a spelling check (for punctuation)
if t.kind == kind and (spelling == None or t.spelling == spelling):
tokens.remove(t)
#print("\tFound: " + str(t))
return t
if required:
raise ValueError('Kind: ' + kind + ' was not found in the token list. Spelling: ' + str(spelling))
return None