File: Synopsis/Parsers/IDL/idlutil.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"""Utility functions for IDL compilers
73
74escapifyString() -- return a string with non-printing characters escaped.
75escapifyWString() -- return a wstring with non-printing characters escaped.
76reprFloat() -- return a string represenation of an IDL float type.
77slashName() -- format a scoped name with '/' separating components.
78dotName() -- format a scoped name with '.' separating components.
79ccolonName() -- format a scoped name with '::' separating components.
80pruneScope() -- remove common prefix from a scoped name.
81relativeScope() -- give a minimal name for one scope relative to another."""
82
83import string
84
85def slashName(scopedName, our_scope=[]):
86 """slashName(list, [list]) -> string
87
88Return a scoped name given as a list of strings as a single string
89with the components separated by '/' characters. If a second list is
90given, remove a common prefix using pruneScope()."""
91
92 pscope = pruneScope(scopedName, our_scope)
93 return string.join(pscope, "/")
94
95def dotName(scopedName, our_scope=[]):
96 """dotName(list, [list]) -> string
97
98Return a scoped name given as a list of strings as a single string
99with the components separated by '.' characters. If a second list is
100given, remove a common prefix using pruneScope()."""
101
102 pscope = pruneScope(scopedName, our_scope)
103 return string.join(pscope, ".")
104
105def ccolonName(scopedName, our_scope=[]):
106 """ccolonName(list, [list]) -> string
107
108Return a scoped name given as a list of strings as a single string
109with the components separated by '::' strings. If a second list is
110given, remove a common prefix using pruneScope()."""
111
112 pscope = pruneScope(scopedName, our_scope)
113 return string.join(pscope, "::")
114
115def pruneScope(target_scope, our_scope):
116 """pruneScope(list A, list B) -> list
117
118Given two lists of strings (scoped names), return a copy of list A
119with any prefix it shares with B removed.
120
121 e.g. pruneScope(['A', 'B', 'C', 'D'], ['A', 'B', 'D']) -> ['C', 'D']"""
122
123 tscope = target_scope[:]
124 i = 0
125 while len(tscope) > 0 andi < len(our_scope) andtscope[0] == our_scope[i]:
126 del tscope[0]
127 i = i + 1
128 return tscope
129
130_valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"" 0123456789_!$%^&*()-=+[]{};#:@~,./<>?|`"
131
132_valid_unichars = map(ord, list(_valid_chars))
133
134def escapifyString(s):
135 """escapifyString(string) -> string
136
137Return the given string with any non-printing characters escaped."""
138
139 global _valid_chars
140 l = list(s)
141 for i in range(len(l)):
142 if l[i] not in _valid_chars:
143 l[i] = "\\%03o" % ord(l[i])
144 return string.join(l, "")
145
146
147def escapifyWString(l, escchar="u"):
148 """escapifyWString(int list) -> string
149
150Take a list of integers representing Unicode characters and return an
151ASCII string with all characters outside that range replaced with \\u
152escapes."""
153
154 global _valid_unichars
155 m = [None] * len(l)
156 for i in range(len(l)):
157 assert(l[i] <= 0xffff)
158 if l[i] in _valid_unichars:
159 m[i] = chr(l[i])
160 else:
161 m[i] = "\\%s%04x" % (escchar, l[i])
162 return string.join(m, "")
163
164
165def reprFloat(f):
166 """reprFloat(float) -> string
167
168Return the string representation of an IDL float type (float, double,
169long double), with enough precision to completely reconstruct the bit
170pattern."""
171
172
173 s = "%.17g" % f
174 if string.find(s, ".") == -1:
175 s = s + ".0"
176 return s
177
178
179def relativeScope(fromScope, destScope):
180 """relativeScope(fromScope, destScope) -> list
181
182Given two globally-scoped names, return a minimal scoped name list
183which identifies the destination scope, without clashing with another
184identifier. For example, given IDL:
185
186 module M {
187 typedef short A;
188 typedef long B;
189
190 module N {
191 typedef string B;
192 interface I {
193 void op(in ::M::A x, in ::M::B y);
194 };
195 };
196 };
197
198relativeScope(["M", "N", "I"], ["M", "A"]) -> ["A"]
199relativeScope(["M", "N", "I"], ["M", "B"]) -> ["M", "B"]
200
201If the only valid result is a globally-scoped name, the result list is
202prefixed with None:
203
204 module O {
205 typedef short C;
206 };
207 module P {
208 module O {
209 interface J {
210 void op(in ::O::C z);
211 };
212 };
213 };
214
215relativeScope(["P", "O", "J"], ["O", "C"]) -> [None, "O", "C"]
216
217If either scoped name does not exist, returns None."""
218
219 import _omniidl
220 return _omniidl.relativeScopedName(fromScope, destScope)
221
Generated on Thu Apr 16 16:27:16 2009 by
synopsis (version devel)