msgpack-idl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,403 @@
1
+ #
2
+ # MessagePack IDL Processor
3
+ #
4
+ # Copyright (C) 2011 FURUHASHI Sadayuki
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ module MessagePack
19
+ module IDL
20
+
21
+
22
+ module IR
23
+ class Spec
24
+ def initialize(namespace, types, services, applications)
25
+ @namespace = namespace
26
+ @types = types
27
+ @services = services
28
+ @applications = applications
29
+ end
30
+
31
+ attr_reader :namespace
32
+ attr_reader :types
33
+ attr_reader :services
34
+ attr_reader :applications
35
+
36
+ def messages
37
+ @types.select {|t| t.is_a?(Message) }
38
+ end
39
+
40
+ def enums
41
+ @types.select {|t| t.is_a?(Enum) }
42
+ end
43
+ end
44
+
45
+
46
+ class ServerSpec
47
+ end
48
+
49
+ class ClientSpec
50
+ end
51
+
52
+ class TypeSpec
53
+ end
54
+
55
+
56
+ class Namespace < Array
57
+ end
58
+
59
+ class Type
60
+ def parameterized_type?
61
+ false
62
+ end
63
+
64
+ def nullable_type?
65
+ false
66
+ end
67
+
68
+ def real_type
69
+ self
70
+ end
71
+
72
+ def list_type?
73
+ false
74
+ end
75
+
76
+ def map_type?
77
+ false
78
+ end
79
+
80
+ def void_type?
81
+ false
82
+ end
83
+ end
84
+
85
+ class PrimitiveType < Type
86
+ def initialize(name)
87
+ @name = name
88
+ end
89
+ attr_reader :name
90
+
91
+ def void_type?
92
+ self == Primitive.void
93
+ end
94
+ end
95
+
96
+ class ParameterizedType < Type
97
+ def initialize(type_params, generic_type)
98
+ @generic_type = generic_type
99
+ @type_params = type_params
100
+ end
101
+ attr_reader :type_params, :generic_type
102
+
103
+ def name
104
+ @generic_type.name
105
+ end
106
+
107
+ def parameterized_type?
108
+ true
109
+ end
110
+
111
+ def list_type?
112
+ @generic_type == Primitive.list
113
+ end
114
+
115
+ def map_type?
116
+ @generic_type == Primitive.map
117
+ end
118
+ end
119
+
120
+ class TypeParameterSymbol
121
+ def initialize(name)
122
+ @name = name
123
+ end
124
+ attr_reader :name
125
+ end
126
+
127
+ class GenericType
128
+ def initialize(name, type_params)
129
+ @name = name
130
+ @type_params = type_params
131
+ end
132
+ attr_reader :name, :type_params
133
+
134
+ def list_type?
135
+ false
136
+ end
137
+
138
+ def map_type?
139
+ false
140
+ end
141
+ end
142
+
143
+ class PrimitiveGenericType < GenericType
144
+ end
145
+
146
+ class NullableType < ParameterizedType
147
+ def initialize(type)
148
+ super([type], Primitive.nullable)
149
+ end
150
+
151
+ def nullable_type?
152
+ true
153
+ end
154
+
155
+ def real_type
156
+ @type_params[0].real_type
157
+ end
158
+ end
159
+
160
+ module Primitive
161
+ def self.define(name, value)
162
+ (class << self; self; end).module_eval {
163
+ define_method(name) { value }
164
+ }
165
+ end
166
+
167
+ define :byte, PrimitiveType.new('byte')
168
+ define :short, PrimitiveType.new('short')
169
+ define :int, PrimitiveType.new('int')
170
+ define :long, PrimitiveType.new('long')
171
+ define :ubyte, PrimitiveType.new('ubyte')
172
+ define :ushort, PrimitiveType.new('ushort')
173
+ define :uint, PrimitiveType.new('uint')
174
+ define :ulong, PrimitiveType.new('ulong')
175
+ define :float, PrimitiveType.new('float')
176
+ define :double, PrimitiveType.new('double')
177
+ define :bool, PrimitiveType.new('bool')
178
+ define :raw, PrimitiveType.new('raw')
179
+ define :string, PrimitiveType.new('string')
180
+ define :list, PrimitiveGenericType.new('list', [
181
+ TypeParameterSymbol.new('E')])
182
+ define :map, PrimitiveGenericType.new('map', [
183
+ TypeParameterSymbol.new('K'),
184
+ TypeParameterSymbol.new('V')])
185
+ define :nullable, PrimitiveGenericType.new('nullable', [
186
+ TypeParameterSymbol.new('T')])
187
+ define :void, PrimitiveType.new('void')
188
+
189
+ INT_TYPES = [byte, short, int, long, ubyte, ushort, uint, ulong]
190
+ end
191
+
192
+ class Value
193
+ end
194
+
195
+ class NilValue < Value
196
+ n = NilValue.new
197
+
198
+ (class << self; self; end).module_eval {
199
+ define_method(:nil) { n }
200
+ }
201
+ end
202
+
203
+ class BoolValue < Value
204
+ def initialize(bool)
205
+ @bool = bool
206
+ end
207
+ attr_reader :bool
208
+
209
+ t = BoolValue.new(true)
210
+ f = BoolValue.new(false)
211
+
212
+ (class << self; self; end).module_eval {
213
+ define_method(:true) { t }
214
+ define_method(:false) { f }
215
+ }
216
+ end
217
+
218
+ class IntValue < Value
219
+ def initialize(int)
220
+ @int = int
221
+ end
222
+ attr_reader :int
223
+
224
+ def ==(o)
225
+ o.class == IntValue && @int == o.int
226
+ end
227
+ end
228
+
229
+ class EnumValue < Value
230
+ def initialize(enum, field)
231
+ @enum = enum
232
+ @field = field
233
+ end
234
+ attr_reader :enum, :field
235
+
236
+ def ==(o)
237
+ # TODO
238
+ o.class == EnumValue && @enum.name == o.enum.name && @field.id == o.field.id
239
+ end
240
+ end
241
+
242
+ class EmptyValue < Value
243
+ def ==(o)
244
+ # TODO
245
+ o.class == EmptyValue
246
+ end
247
+ end
248
+
249
+ class Message < Type
250
+ def initialize(name, super_class, new_fields)
251
+ @name = name
252
+ @super_class = super_class
253
+ @new_fields = new_fields
254
+
255
+ if super_class
256
+ @all_fields = (super_class.all_fields + new_fields).sort_by {|f| f.id }
257
+ else
258
+ @all_fields = new_fields
259
+ end
260
+ @max_id = @all_fields.map {|f| f.id }.max || 0
261
+ @max_required_id = @all_fields.select {|f| f.required? }.map {|f| f.id }.max || 0
262
+ end
263
+
264
+ attr_reader :name, :super_class, :new_fields
265
+ attr_reader :all_fields, :max_id, :max_required_id
266
+
267
+ def [](id)
268
+ @all_fields.find {|f| f.id == id }
269
+ end
270
+ end
271
+
272
+ class Exception < Message
273
+ end
274
+
275
+ class Field
276
+ def initialize(id, type, name, option, value)
277
+ @id = id
278
+ @type = type
279
+ @name = name
280
+ @option = option
281
+ @value = value
282
+ end
283
+
284
+ attr_reader :id, :type, :name, :option, :value
285
+
286
+ def required?
287
+ @option == FIELD_REQUIRED
288
+ end
289
+
290
+ def optional?
291
+ @option == FIELD_OPTIONAL
292
+ end
293
+
294
+ def ==(o)
295
+ o.class == self.class && @id == o.id && @type == o.type &&
296
+ @name == o.name && @option == o.option && @value == o.value
297
+ end
298
+ end
299
+
300
+ FIELD_OPTIONAL = :optional
301
+ FIELD_REQUIRED = :required
302
+
303
+ class Enum < Type
304
+ def initialize(name, fields)
305
+ @name = name
306
+ @fields = fields
307
+ end
308
+ attr_reader :name, :fields
309
+ end
310
+
311
+ class EnumField
312
+ def initialize(id, name)
313
+ @id = id
314
+ @name = name
315
+ end
316
+ attr_reader :id, :name
317
+ end
318
+
319
+ class Service
320
+ def initialize(name, versions)
321
+ @name = name
322
+ @versions = versions
323
+ end
324
+ attr_reader :name, :versions
325
+ attr_writer :versions
326
+
327
+ def [](version)
328
+ @versions.find {|sv| sv.version == version }
329
+ end
330
+
331
+ def versions_upto(version)
332
+ @versions.each {|sv|
333
+ break if sv.version > version
334
+ yield sv
335
+ }
336
+ end
337
+ end
338
+
339
+ class ServiceVersion
340
+ def initialize(version, funcs)
341
+ @version = version
342
+ @functions = funcs
343
+ end
344
+ attr_reader :version, :functions
345
+ end
346
+
347
+ class Function
348
+ def initialize(name, return_type, args, super_version, super_func)
349
+ @name = name
350
+ @return_type = return_type
351
+ @args = args
352
+ @super_version = super_version
353
+ @super_func = super_func
354
+ @max_id = @args.map {|a| a.id }.max || 0
355
+ @max_required_id = @args.select {|a| a.required? }.map {|a| a.id }.max || 0
356
+ end
357
+ attr_reader :name, :return_type, :args
358
+ attr_reader :super_version, :super_func
359
+ attr_reader :max_id, :max_required_id
360
+ attr_writer :super_version, :super_func
361
+
362
+ def super_class; nil; end
363
+ alias new_fields args
364
+ alias all_fields args
365
+
366
+ def [](id)
367
+ @args.find {|a| a.id == id }
368
+ end
369
+ end
370
+
371
+ class Argument < Field
372
+ end
373
+
374
+ class Application
375
+ def initialize(name, scopes)
376
+ @name = name
377
+ @scopes = scopes
378
+ end
379
+ attr_reader :name, :scopes
380
+
381
+ def default_scope
382
+ @scopes.find {|c| c.default_scope? }
383
+ end
384
+ end
385
+
386
+ class Scope
387
+ def initialize(name, service, version, default)
388
+ @name = name
389
+ @service = service
390
+ @version = version
391
+ @default = default
392
+ end
393
+ attr_reader :name, :service, :version
394
+
395
+ def default_scope?
396
+ @default
397
+ end
398
+ end
399
+ end
400
+
401
+
402
+ end
403
+ end
@@ -0,0 +1,45 @@
1
+ #
2
+ # MessagePack IDL Processor
3
+ #
4
+ # Copyright (C) 2011 FURUHASHI Sadayuki
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ module MessagePack
19
+ module IDL
20
+
21
+
22
+ module ProcessorModule
23
+ attr_writer :log
24
+
25
+ def log
26
+ @log ||= STDOUT
27
+ end
28
+
29
+ def log_warn(msg)
30
+ log.puts msg
31
+ end
32
+
33
+ def log_error(msg)
34
+ log.puts msg
35
+ end
36
+
37
+ def log_trace(msg)
38
+ log.puts msg
39
+ end
40
+ end
41
+
42
+
43
+ end
44
+ end
45
+