forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_exception_factory.py
More file actions
185 lines (143 loc) · 5.38 KB
/
generate_exception_factory.py
File metadata and controls
185 lines (143 loc) · 5.38 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
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
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
# See the LICENSE file in the project root for more information.
import sys
import generate
class ExceptionItem:
def __init__(self, type, ID, text, param_num):
self.type = type
self.ID = ID
self.text = text
self.param_num = param_num
class StringItem:
def __init__(self, ID, text, param_num):
self.ID = ID
self.text = text
self.param_num = param_num
def make_string_item(line1):
splitted = line1.split('=', 1)
ID = splitted[0]
text = splitted[1]
num = text.count('{')
string_item = StringItem(ID, text ,num)
return string_item
def make_exception_item(line1, line2):
type = line1.rsplit('=', 1)[1]
splitted = line2.split('=', 1)
ID = splitted[0]
text = splitted[1]
num = text.count('{')
exception_item = ExceptionItem(type, ID, text ,num)
return exception_item
def collect_exceptions(filename):
thefile = open(filename)
text = thefile.read()
lines = text.splitlines()
items = []
for i in range(len(lines)):
line1 = lines[i]
if line1.startswith("## "):
line2 = lines[i+1]
items.append(make_exception_item(line1, line2))
return items
def collect_strings(filename):
thefile = open(filename)
text = thefile.read()
lines = text.splitlines()
items = []
for i in range(len(lines)):
line1 = lines[i]
if (not (line1.startswith("#") or line1.startswith(";")) and line1.find("=") > 0 ):
items.append(make_string_item(line1))
return items
def make_signature(cnt):
if cnt == 0:
return "()"
sig = "("
for i in range(cnt - 1):
sig += "object p" + str(i) + ", "
sig += "object p" + str(cnt - 1) + ")"
return sig
def make_args(cnt):
if cnt == 0:
return "()"
sig = "("
for i in range(cnt - 1):
sig += "p" + str(i) + ", "
sig += "p" + str(cnt - 1) + ")"
return sig
def make_format(text, cnt):
if cnt == 0:
return '"' + text + '"'
format = 'FormatString("' + text + '", '
for i in range(cnt - 1):
format += "p" + str(i) + ", "
format += "p" + str((cnt - 1)) + ")"
return format
def escape(s):
s = s.replace('"', '\\"')
return s
def escape_xml(s):
s = s.replace("<", "<")
s = s.replace(">", ">")
return s
def gen_expr_factory(cw, source):
strings = collect_strings(source)
exceptions = collect_exceptions(source)
result = ""
result += "/// <summary>" + "\n"
result += "/// Strongly-typed and parameterized string factory." + "\n"
result += "/// </summary>" + "\n"
cw.write(result)
cw.enter_block("internal static partial class Strings")
for ex in strings:
result = ""
result += "/// <summary>" + "\n"
result += '/// A string like "' + escape_xml(ex.text) + '"\n'
result += "/// </summary>" + "\n"
if (ex.param_num > 0):
result += "internal static string " + str(ex.ID) + make_signature(ex.param_num) + " {" + "\n"
result += " return " + make_format(escape(ex.text), ex.param_num) + ";" + "\n"
result += "}" + "\n"
else:
result += "internal static string " + str(ex.ID) + " {" + "\n"
result += " get {" + "\n"
result += ' return "' + escape(ex.text) + '";' + '\n'
result += " }" + "\n"
result += "}" + "\n"
cw.write(result)
cw.exit_block()
result = "/// <summary>" + "\n"
result += "/// Strongly-typed and parameterized exception factory." + "\n"
result += "/// </summary>" + "\n"
cw.write(result)
cw.enter_block("internal static partial class Error")
for ex in exceptions:
result = ""
result += "/// <summary>" + "\n"
result += "/// " + ex.type + ' with message like "' + escape_xml(ex.text) + '"\n'
result += "/// </summary>" + "\n"
if (ex.type == "System.Runtime.InteropServices.COMException"):
result += '[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]' + '\n'
result += "internal static Exception " + str(ex.ID) + make_signature(ex.param_num) + " {" + "\n"
if (ex.param_num == 0):
result += " return new " + ex.type + "(Strings." + ex.ID + ");" + "\n"
else:
result += " return new " + ex.type + "(Strings." + ex.ID + make_args(ex.param_num) + ");" + "\n"
result += "}" + "\n"
cw.write(result)
cw.exit_block()
def gen_expr_factory_core(cw):
gen_expr_factory(cw, generate.root_dir + "\\..\\..\\ndp\\fx\\src\\Core\\System\\Linq\\Expressions\\System.Linq.Expressions.txt")
def gen_expr_factory_com(cw):
gen_expr_factory(cw, generate.root_dir + "\\..\\..\\ndp\\fx\\src\\Dynamic\\System\\Dynamic\\System.Dynamic.txt")
def gen_expr_factory_scripting(cw):
gen_expr_factory(cw, generate.root_dir + "\\src\\dlr\\src\\Microsoft.Dynamic\\Microsoft.Scripting.txt")
def main():
return generate.generate(
# ("Exception Factory", gen_expr_factory_core),
# ("Com Exception Factory", gen_expr_factory_com),
("Microsoft.Scripting Exception Factory", gen_expr_factory_scripting),
)
if __name__ == "__main__":
main()