-
Notifications
You must be signed in to change notification settings - Fork 1
/
cs_ast.py
151 lines (113 loc) · 3.38 KB
/
cs_ast.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
import sys, traceback
from cs_global import glob
def tab(tlvl, tstr=" "):
s = ""
for i in range(tlvl):
s += tstr
return s
class Node (object):
def __init__(self):
self.children = []
self.parent = None
self.line = glob.g_line
self.file = glob.g_file
self.lexpos = glob.g_lexpos
def __getitem__(self, item):
return self.children[item]
def __setitem__(self, item, val):
self.children[item] = val
def __len__(self):
return len(self.children)
def index(self, i):
return self.children.index(i)
def replace(self, oldnode, newnode):
i = 0
for c in self.children:
if c == oldnode:
break
i += 1
self.children[i] = newnode
newnode.parent = self
def _default_node(self, node):
if type(node) == str:
node = StrNode(node)
return node
def pop(self, i):
self.children.pop(i);
def add(self, node):
node = self._default_node(node)
self.children.append(node)
node.parent = self
def remove(self, node):
self.children.remove(node)
def insert(self, i, node):
node = self._default_node(node)
self.children.insert(i, node);
node.parent = self
def prepend(self, node):
node = self._default_node(node)
self.children.insert(0, node)
node.parent = self
def extra_str(self):
return ""
def copy_basic(self, n2):
n2.line = self.line
n2.file = self.file
n2.lexpos = self.lexpos
n2.final_type = self.final_type
if hasattr(self, "template"):
if self.template != None:
n2.template = n2.template.copy()
def copy(self):
raise RuntimeError("Unimplemented copy function in type %s!"%str(type(self)))
def copy_children(self, n2):
n2.children[:] = []
for c in self:
n2.add(c.copy())
def gen_js(self, tlevel):
raise RuntimeError("Unimplemented gen_js function in type %s!"%str(type(self)))
def get_line_str(self):
name = str(type(self)).replace("js_ast.", "").replace("<class", "").replace(">", "").replace(" ", "").replace("'", "")
c = self.extra_str()
if len(c.strip()) > 0: c = " " + c
return name + c
def get_ntype_name(self):
return str(type(self)).replace("js_ast.", "").replace("<class", "").replace(">", "").replace(" ", "").replace("'", "")
def __str__(self, tlevel=0):
t = tab(tlevel, "-")
name = ""
name += str(type(self)).replace("cs_ast.", "").replace("<class", "").replace(">", "").replace(" ", "").replace("'", "")
if len(self.children) == 0:
return t + name + " " + self.extra_str()
else:
s = t + name + " " + self.extra_str() + " {\n"
for c in self.children:
cs = c.__str__(tlevel+1)
if not (cs.endswith("\n")):
cs += "\n"
s += cs
s += t + "}\n"
return s
def __repr__(self):
return str(self)
class StatementList(Node):
pass
class StrNode(Node):
def __init__(self, val):
Node.__init__(self)
self.val = val
def extra_str(self):
if self.val.replace(" ", "").replace("\t", "")[-1] != "\n":
return "\n" + self.val
else:
return "\n" + self.val
class CodeNode(StrNode):
pass
class BindingNode(StrNode):
def __init__(self, val, type):
StrNode.__init__(self, val)
self.type = type
class HtmlNode(StrNode):
pass
class IncludeNode(StrNode):
pass