idl-parser 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 1.1
2
+ Name: idl_parser
3
+ Version: 0.0.1
4
+ Summary: OMG IDL Parser
5
+ Home-page: http://www.sugarsweetrobotics.com/
6
+ Author: ysuga
7
+ Author-email: ysuga@ysuga.net
8
+ License: GPLv3
9
+ Download-URL: https://github.com/sugarsweetrobotics/idl_parser
10
+ Description: UNKNOWN
11
+ Platform: UNKNOWN
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
16
+ Classifier: Natural Language :: English
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python
19
+ Classifier: Topic :: Scientific/Engineering
@@ -0,0 +1,7 @@
1
+ #from parser import IDLParser
2
+ __all__=['parser']
3
+
4
+
5
+
6
+
7
+
@@ -0,0 +1,50 @@
1
+ import os, sys, traceback
2
+
3
+ from . import node
4
+ sep = '::'
5
+
6
+
7
+ class IDLConst(node.IDLNode):
8
+
9
+ def __init__(self, name, typename, value, parent, filepath=None):
10
+ super(IDLConst, self).__init__('IDLConst', name, parent)
11
+ self._typename = typename
12
+ self._verbose = True
13
+ self._value = value
14
+ self._filepath= filepath
15
+
16
+ def to_simple_dic(self, quiet=False, full_path=False, recursive=False, member_only=False):
17
+ name = self.full_path if full_path else self.name
18
+ if quiet:
19
+ return 'const %s %s = %s' % (self.typename, name, self.value)
20
+ dic = { 'const %s' % name : { 'type' : self.typename,
21
+ 'value' : self.value } }
22
+ return dic
23
+
24
+ def to_dic(self):
25
+ dic = { 'name' : self.name,
26
+ 'filepath' : self.filepath,
27
+ 'classname' : self.classname,
28
+ 'typename' : self.typename,
29
+ 'value' : self.value }
30
+ return dic
31
+
32
+ @property
33
+ def typename(self):
34
+ return self._typename
35
+
36
+ @property
37
+ def type(self):
38
+ return self.root_node.find_types(self.typename)[0]
39
+ @property
40
+ def value(self):
41
+ return self._value
42
+
43
+ @property
44
+ def value_string(self):
45
+ return self._value
46
+
47
+ @property
48
+ def full_path(self):
49
+ return self.parent.full_path + sep + self.name
50
+
@@ -0,0 +1,114 @@
1
+ import os, sys, traceback
2
+
3
+ from . import node
4
+ sep = '::'
5
+
6
+ class IDLEnumValue(node.IDLNode):
7
+ def __init__(self, value, parent):
8
+ super(IDLEnumValue, self).__init__('IDLEnumValue', '', parent)
9
+ self._verbose = True
10
+ self._value = value
11
+
12
+ def parse_blocks(self, blocks, filepath=None):
13
+ self._filepath = filepath
14
+ if len(blocks) == 1:
15
+ self._name = blocks[0]
16
+ else:
17
+ sys.stdout.write('Unkown Enum format %s\n' % blocks)
18
+ #name, type = self._name_and_type(blocks)
19
+ #self._name = name
20
+ #self._type = type
21
+
22
+ @property
23
+ def full_path(self):
24
+ return self.full_path + '.' + self.name
25
+
26
+ def to_simple_dic(self):
27
+ dic = {self.name : self.value }
28
+ return dic
29
+
30
+ def to_dic(self):
31
+ dic = { 'name' : self.name,
32
+ 'filepath' : self.filepath,
33
+ 'classname' : self.classname,
34
+ 'value' : self.value }
35
+ return dic
36
+ @property
37
+ def value(self):
38
+ return self._value
39
+
40
+
41
+
42
+
43
+ class IDLEnum(node.IDLNode):
44
+
45
+ def __init__(self, name, parent):
46
+ super(IDLEnum, self).__init__('IDLEnum', name, parent)
47
+ self._verbose = True
48
+ self._values = []
49
+
50
+ def to_simple_dic(self, quiet=False, full_path=False, recursive=False, member_only=False):
51
+ name = self.full_path if full_path else self.name
52
+ if quiet:
53
+ return 'enum %s' % name
54
+ dic = { 'enum %s' % name : [v.to_simple_dic() for v in self.values] }
55
+ return dic
56
+
57
+
58
+ def to_dic(self):
59
+ dic = { 'name' : self.name,
60
+ 'classname' : self.classname,
61
+ 'values' : [v.to_dic() for v in self.values] }
62
+ return dic
63
+
64
+ @property
65
+ def full_path(self):
66
+ return self.parent.full_path + sep + self.name
67
+
68
+ def parse_tokens(self, token_buf, filepath=None):
69
+ self._filepath = filepath
70
+ self._counter = 0
71
+ kakko = token_buf.pop()
72
+ if not kakko == '{':
73
+ if self._verbose: sys.stdout.write('# Error. No kakko "{".\n')
74
+ raise InvalidIDLSyntaxError()
75
+
76
+ block_tokens = []
77
+ while True:
78
+ token = token_buf.pop()
79
+ if token == None:
80
+ if self._verbose: sys.stdout.write('# Error. No kokka "}".\n')
81
+ raise InvalidIDLSyntaxError()
82
+
83
+ elif token == '}':
84
+ token = token_buf.pop()
85
+ if not token == ';':
86
+ if self._verbose: sys.stdout.write('# Error. No semi-colon after "}".\n')
87
+ raise InvalidIDLSyntaxError()
88
+
89
+ if len(block_tokens) > 0:
90
+ self._parse_block(block_tokens)
91
+ break
92
+
93
+ if token == ',':
94
+ self._parse_block(block_tokens)
95
+ block_tokens = []
96
+ continue
97
+ block_tokens.append(token)
98
+
99
+ def _parse_block(self, blocks):
100
+ v = IDLEnumValue(self._counter, self)
101
+ self._counter = self._counter+ 1
102
+ v.parse_blocks(blocks, self.filepath)
103
+ self._values.append(v)
104
+
105
+ @property
106
+ def values(self):
107
+ return self._values
108
+
109
+
110
+ def value_by_name(self, name):
111
+ for m in self.values:
112
+ if m.name == name:
113
+ return m
114
+ return None
@@ -0,0 +1,207 @@
1
+ from . import node
2
+
3
+ from . import type as idl_type
4
+
5
+ sep = '::'
6
+
7
+ class IDLArgument(node.IDLNode):
8
+ def __init__(self, parent):
9
+ super(IDLArgument, self).__init__('IDLArgument', '', parent)
10
+ self._verbose = True
11
+ self._dir = 'in'
12
+ self._type = None
13
+
14
+ def parse_blocks(self, blocks, filepath=None):
15
+ self._filepath= filepath
16
+ directions = ['in', 'out', 'inout']
17
+ self._dir = 'in'
18
+ if blocks[0] in directions:
19
+ self._dir = blocks[0]
20
+ blocks.pop(0)
21
+ pass
22
+ argument_name, argument_type = self._name_and_type(blocks)
23
+ self._name = argument_name
24
+ self._type = idl_type.IDLType(argument_type, self)
25
+
26
+ def to_simple_dic(self):
27
+ dic = '%s %s %s' % (self.direction, self.type, self.name)
28
+ return dic
29
+
30
+ def to_dic(self):
31
+ dic = { 'name' : self.name,
32
+ 'classname' : self.classname,
33
+ 'type' : str(self.type),
34
+ 'direction' : self.direction,
35
+ 'filepath' : self.filepath }
36
+ return dic
37
+
38
+ @property
39
+ def direction(self):
40
+ return self._dir
41
+
42
+ @property
43
+ def type(self):
44
+ return self._type
45
+
46
+ def post_process(self):
47
+ #self._type = self.refine_typename(self.type)
48
+ pass
49
+
50
+
51
+ class IDLMethod(node.IDLNode):
52
+ def __init__(self, parent):
53
+ super(IDLMethod, self).__init__('IDLValue', '', parent)
54
+ self._verbose = True
55
+ self._returns = None
56
+ self._arguments = []
57
+
58
+ def parse_blocks(self, blocks, filepath=None):
59
+ self._filepath=filepath
60
+
61
+ if blocks[0] == 'oneway':
62
+ self._oneway = True
63
+ blocks.pop(0)
64
+ else:
65
+ self._oneway = False
66
+
67
+ self._returns = idl_type.IDLType(blocks[0], self)
68
+ self._name = blocks[1]
69
+ self._arguments = []
70
+
71
+ if not blocks[2] == '(':
72
+ print(' -- Invalid Interface Token (%s)' % interface_name)
73
+ print( blocks)
74
+
75
+ index = 3
76
+ argument_blocks = []
77
+ while True:
78
+ if index == len(blocks):
79
+ break
80
+ token = blocks[index]
81
+ if token == ',' or token == ')':
82
+ if len(argument_blocks) == 0:
83
+ break
84
+
85
+ a = IDLArgument(self)
86
+ self._arguments.append(a)
87
+ a.parse_blocks(argument_blocks, self.filepath)
88
+
89
+ argument_blocks = []
90
+ else:
91
+ argument_blocks.append(token)
92
+ index = index + 1
93
+
94
+ def to_simple_dic(self):
95
+ return {self.name : {
96
+ 'returns' : str(self.returns),
97
+ 'params' : [a.to_simple_dic() for a in self.arguments]}}
98
+
99
+ def to_dic(self):
100
+ dic = { 'name' : self.name,
101
+ 'filepath' : self.filepath,
102
+ 'classname' : self.classname,
103
+ 'returns' : str(self._returns),
104
+ 'arguments' : [a.to_dic() for a in self.arguments]}
105
+ return dic
106
+
107
+ @property
108
+ def returns(self):
109
+ return self._returns
110
+
111
+ @property
112
+ def arguments(self):
113
+ return self._arguments
114
+
115
+ def argument_by_name(self, name):
116
+ for a in self.arguments:
117
+ if a.name == name:
118
+ return a
119
+
120
+ return None
121
+
122
+
123
+ def forEachArgument(self, func):
124
+ for a in self.arguments:
125
+ func(a)
126
+
127
+ def post_process(self):
128
+ #self._returns = self.refine_typename(self.returns)
129
+ #self.forEachArgument(lambda a : a.post_process())
130
+ pass
131
+
132
+ class IDLInterface(node.IDLNode):
133
+
134
+ def __init__(self, name, parent):
135
+ super(IDLInterface, self).__init__('IDLInterface', name, parent)
136
+ self._verbose = True
137
+ self._methods = []
138
+
139
+ @property
140
+ def full_path(self):
141
+ return self.parent.full_path + sep + self.name
142
+
143
+ def to_simple_dic(self, quiet=False, full_path=False, recursive=False, member_only=False):
144
+ if quiet:
145
+ return 'interface %s' % self.name
146
+ dic = { 'interface ' + self.name : [m.to_simple_dic() for m in self.methods] }
147
+ return dic
148
+
149
+ def to_dic(self):
150
+ dic = { 'name' : self.name,
151
+ 'filepath' : self.filepath,
152
+ 'classname' : self.classname,
153
+ 'methods' : [m.to_dic() for m in self.methods] }
154
+ return dic
155
+
156
+ def parse_tokens(self, token_buf, filepath=None):
157
+ self._filepath=filepath
158
+ kakko = token_buf.pop()
159
+ if not kakko == '{':
160
+ if self._verbose: sys.stdout.write('# Error. No kakko "{".\n')
161
+ raise InvalidIDLSyntaxError()
162
+
163
+ block_tokens = []
164
+ while True:
165
+
166
+ token = token_buf.pop()
167
+ if token == None:
168
+ if self._verbose: sys.stdout.write('# Error. No kokka "}".\n')
169
+ raise InvalidIDLSyntaxError()
170
+
171
+ elif token == '}':
172
+ token = token_buf.pop()
173
+ if not token == ';':
174
+ if self._verbose: sys.stdout.write('# Error. No semi-colon after "}".\n')
175
+ raise InvalidIDLSyntaxError()
176
+ break
177
+
178
+ if token == ';':
179
+ self._parse_block(block_tokens)
180
+ block_tokens = []
181
+ continue
182
+ block_tokens.append(token)
183
+ self._post_process()
184
+
185
+ def _post_process(self):
186
+ self.forEachMethod(lambda m : m.post_process())
187
+
188
+ def _parse_block(self, blocks):
189
+ v = IDLMethod(self)
190
+ v.parse_blocks(blocks, self.filepath)
191
+ self._methods.append(v)
192
+
193
+ @property
194
+ def methods(self):
195
+ return self._methods
196
+
197
+ def method_by_name(self, name):
198
+ for m in self.methods:
199
+ if name == m.name:
200
+ return m
201
+ return None
202
+
203
+ def forEachMethod(self, func):
204
+ for m in self.methods:
205
+ func(m)
206
+
207
+
@@ -0,0 +1,259 @@
1
+ import os, sys, traceback
2
+
3
+ from . import node, type
4
+ from . import struct, typedef, interface, enum, const
5
+ global_namespace = '__global__'
6
+ sep = '::'
7
+
8
+ class IDLModule(node.IDLNode):
9
+
10
+ def __init__(self, name=None, parent = None):
11
+ super(IDLModule, self).__init__('IDLModule', name, parent)
12
+ self._verbose = False
13
+ if name is None:
14
+ self._name = global_namespace
15
+
16
+ self._interfaces = []
17
+ self._typedefs = []
18
+ self._structs = []
19
+ self._enums = []
20
+ self._consts = []
21
+ self._modules = []
22
+
23
+ @property
24
+ def is_global(self):
25
+ return self.name == global_namespace
26
+
27
+ @property
28
+ def full_path(self):
29
+ if self.parent is None:
30
+ return '' # self.name
31
+ else:
32
+ if len(self.parent.full_path) == 0:
33
+ return self.name
34
+ return self.parent.full_path + sep + self.name
35
+
36
+ def to_simple_dic(self, quiet=False):
37
+ dic = {'module %s' % self.name : [s.to_simple_dic(quiet) for s in self.structs] +
38
+ [i.to_simple_dic(quiet) for i in self.interfaces] +
39
+ [m.to_simple_dic(quiet) for m in self.modules] +
40
+ [e.to_simple_dic(quiet) for e in self.enums] +
41
+ [t.to_simple_dic(quiet) for t in self.typedefs] +
42
+ [t.to_simple_dic(quiet) for t in self.consts]}
43
+ return dic
44
+
45
+ def to_dic(self):
46
+ dic = { 'name' : self.name,
47
+ 'filepath' : self.filepath,
48
+ 'classname' : self.classname,
49
+ 'interfaces' : [i.to_dic() for i in self.interfaces],
50
+ 'typedefs' : [t.to_dic() for t in self.typedefs],
51
+ 'structs' : [s.to_dic() for s in self.structs],
52
+ 'enums' : [e.to_dic() for e in self.enums],
53
+ 'modules' : [m.to_dic() for m in self.modules],
54
+ 'consts' : [c.to_dic() for c in self.consts] }
55
+ return dic
56
+
57
+
58
+ def parse_tokens(self, token_buf, filepath=None):
59
+ self._filepath = filepath
60
+ if not self.name == global_namespace:
61
+ kakko = token_buf.pop()
62
+ if not kakko == '{':
63
+ if self._verbose: sys.stdout.write('# Error. No kakko "{".\n')
64
+ raise InvalidIDLSyntaxError()
65
+
66
+ while True:
67
+ token = token_buf.pop()
68
+ if token == None:
69
+ if self.name == global_namespace:
70
+ break
71
+ if self._verbose: sys.stdout.write('# Error. No kokka "}".\n')
72
+ raise InvalidIDLSyntaxError()
73
+ elif token == 'module':
74
+ name_ = token_buf.pop()
75
+ m = self.module_by_name(name_)
76
+ if m == None:
77
+ m = IDLModule(name_, self)
78
+ self._modules.append(m)
79
+ m.parse_tokens(token_buf, filepath=filepath)
80
+ elif token == 'typedef':
81
+ blocks = []
82
+ while True:
83
+ t = token_buf.pop()
84
+ if t == None:
85
+ raise InvalidIDLSyntaxError()
86
+ elif t == ';':
87
+ break
88
+ else:
89
+ blocks.append(t)
90
+ t = typedef.IDLTypedef(self)
91
+ self._typedefs.append(t)
92
+ t.parse_blocks(blocks, filepath=filepath)
93
+ elif token == 'struct':
94
+ name_ = token_buf.pop()
95
+ s_ = self.struct_by_name(name_)
96
+ s = struct.IDLStruct(name_, self)
97
+ s.parse_tokens(token_buf, filepath=filepath)
98
+ if s_:
99
+ if self._verbose: sys.stdout.write('# Error. Same Struct Defined (%s)\n' % name_)
100
+ # raise InvalidIDLSyntaxError
101
+ else:
102
+ self._structs.append(s)
103
+
104
+ elif token == 'interface':
105
+ name_ = token_buf.pop()
106
+ s = interface.IDLInterface(name_, self)
107
+ s.parse_tokens(token_buf, filepath=filepath)
108
+
109
+ s_ = self.interface_by_name(name_)
110
+ if s_:
111
+ if self._verbose: sys.stdout.write('# Error. Same Interface Defined (%s)\n' % name_)
112
+ # raise InvalidIDLSyntaxError
113
+ else:
114
+ self._interfaces.append(s)
115
+
116
+ elif token == 'enum':
117
+ name_ = token_buf.pop()
118
+ s = enum.IDLEnum(name_, self)
119
+ s.parse_tokens(token_buf, filepath)
120
+ s_ = self.enum_by_name(name_)
121
+ if s_:
122
+ if self._verbose: sys.stdout.write('# Error. Same Enum Defined (%s)\n' % name_)
123
+ # raise InvalidIDLSyntaxError
124
+ else:
125
+ self._enums.append(s)
126
+
127
+ pass
128
+
129
+ elif token == 'const':
130
+ values = []
131
+ while True:
132
+ t = token_buf.pop()
133
+ if t == ';':
134
+ break
135
+ values.append(t)
136
+
137
+ value_ = values[-1]
138
+ name_ = values[-3]
139
+ typename = ''
140
+ for t in values[:-3]:
141
+ typename = typename + ' ' + t
142
+ typename = typename.strip()
143
+ s = const.IDLConst(name_, typename, value_, self, filepath=filepath)
144
+ s_ = self.const_by_name(name_)
145
+ if s_:
146
+ if self._verbose: sys.stdout.write('# Error. Same Const Defined (%s)\n' % name_)
147
+ else:
148
+ self._consts.append(s)
149
+
150
+ elif token == '}':
151
+ break
152
+
153
+ return True
154
+
155
+
156
+
157
+ @property
158
+ def modules(self):
159
+ return self._modules
160
+
161
+ def module_by_name(self, name):
162
+ for m in self.modules:
163
+ if m.name == name:
164
+ return m
165
+ return None
166
+
167
+ def forEachModule(self, func):
168
+ for m in self.modules:
169
+ func(m)
170
+
171
+ @property
172
+ def interfaces(self):
173
+ return self._interfaces
174
+
175
+ def interface_by_name(self, name):
176
+ for i in self.interfaces:
177
+ if i.name == name:
178
+ return i
179
+ return None
180
+
181
+ def forEachInterface(self, func):
182
+ for m in self.interfaces:
183
+ func(m)
184
+
185
+ @property
186
+ def structs(self):
187
+ return self._structs
188
+
189
+ def struct_by_name(self, name):
190
+ for s in self.structs:
191
+ if s.name == name:
192
+ return s
193
+ return None
194
+
195
+ def forEachStruct(self, func):
196
+ for m in self.structs:
197
+ func(m)
198
+
199
+ @property
200
+ def enums(self):
201
+ return self._enums
202
+
203
+ def enum_by_name(self, name):
204
+ for e in self.enums:
205
+ if e.name == name:
206
+ return e
207
+ return None
208
+
209
+ def forEachEnum(self, func):
210
+ for m in self.enums:
211
+ func(m)
212
+
213
+ @property
214
+ def consts(self):
215
+ return self._consts
216
+
217
+ def const_by_name(self, name):
218
+ for c in self.consts:
219
+ if c.name == name:
220
+ return c
221
+ return None
222
+
223
+ def forEachConst(self, func):
224
+ for m in self.consts:
225
+ func(m)
226
+
227
+ @property
228
+ def typedefs(self):
229
+ return self._typedefs
230
+
231
+ def typedef_by_name(self, name):
232
+ for t in self.typedefs:
233
+ if t.name == name:
234
+ return t
235
+ return None
236
+
237
+ def forEachTypedef(self, func):
238
+ for m in self.typedefs:
239
+ func(m)
240
+
241
+ def find_types(self, full_typename):
242
+ if type.is_primitive(full_typename):
243
+ return [type.IDLType(full_typename, self)]
244
+ typenode = []
245
+
246
+ def parse_node(s, name=str(full_typename)):
247
+ if s.name == name.strip() or s.full_path == name.strip():
248
+ typenode.append(s)
249
+
250
+ def parse_module(m):
251
+ m.forEachModule(parse_module)
252
+ m.forEachStruct(parse_node)
253
+ m.forEachTypedef(parse_node)
254
+ m.forEachEnum(parse_node)
255
+ m.forEachInterface(parse_node)
256
+
257
+ parse_module(self)
258
+
259
+ return typenode