brotorift 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/runtime.rb ADDED
@@ -0,0 +1,345 @@
1
+
2
+ class Def
3
+ attr_reader :ast
4
+
5
+ def initialize ast
6
+ @ast = ast
7
+ end
8
+ end
9
+
10
+
11
+ class TypeDef < Def
12
+ attr_reader :name
13
+
14
+ def initialize ast, name
15
+ super ast
16
+ @name = name
17
+ end
18
+ end
19
+
20
+
21
+ class BuiltinTypeDef < TypeDef
22
+ def initialize name
23
+ super nil, name
24
+ end
25
+ end
26
+
27
+
28
+ class EnumElementDef < Def
29
+ attr_reader :name, :value, :doc
30
+
31
+ def initialize ast
32
+ super ast
33
+ @name = ast.name
34
+ @value = ast.value
35
+ @doc = ast.doc
36
+ end
37
+ end
38
+
39
+
40
+ class EnumTypeDef < TypeDef
41
+ attr_reader :elements, :doc
42
+
43
+ def initialize ast
44
+ super ast, ast.name
45
+ @doc = ast.doc
46
+ @elements = {}
47
+ end
48
+
49
+ def add_element element_def
50
+ @elements[element_def.name] = element_def
51
+ end
52
+ end
53
+
54
+
55
+ class NodeDef < Def
56
+ attr_reader :name, :language, :nickname, :namespace, :doc
57
+
58
+ def initialize ast
59
+ super ast
60
+ @name = ast.name
61
+ @language = ast.language
62
+ @nickname = ast.nickname
63
+ @namespace = ast.namespace
64
+ @doc = ast.doc
65
+ end
66
+ end
67
+
68
+
69
+ class StructTypeDef < TypeDef
70
+ attr_reader :members, :doc
71
+
72
+ def initialize ast
73
+ super ast, ast.name
74
+ @members = []
75
+ @doc = ast.doc
76
+ end
77
+
78
+ def add_member member_def
79
+ @members.push member_def
80
+ end
81
+
82
+ def get_member name
83
+ @members.find { |m| m.name == name }
84
+ end
85
+ end
86
+
87
+
88
+ class TypeInstanceDef < Def
89
+ attr_reader :type, :params, :runtime
90
+
91
+ def initialize ast, type, params, runtime
92
+ super ast
93
+ @type = type
94
+ @params = params
95
+ @runtime = runtime
96
+ end
97
+ end
98
+
99
+
100
+ class MemberDef < Def
101
+ attr_reader :name, :type, :doc
102
+
103
+ def initialize ast, type
104
+ super ast
105
+ @name = ast.name
106
+ @type = type
107
+ @doc = ast.doc
108
+ end
109
+ end
110
+
111
+
112
+ class DirectionDef < Def
113
+ attr_reader :client, :direction, :server, :messages, :doc
114
+
115
+ def initialize ast, client, server
116
+ super ast
117
+ @client = client
118
+ @direction = ast.direction
119
+ @server = server
120
+ @messages = {}
121
+ @doc = ast.doc
122
+ end
123
+
124
+ def name
125
+ case @direction
126
+ when :left
127
+ return "#{@client.name} <- #{@server.name}"
128
+ when :right
129
+ return "#{@client.name} -> #{@server.name}"
130
+ end
131
+ end
132
+
133
+ def add_message message_def
134
+ @messages[message_def.name] = message_def
135
+ end
136
+ end
137
+
138
+
139
+ class MessageDef < Def
140
+ attr_reader :name, :id, :members, :doc
141
+
142
+ def initialize ast, id
143
+ super ast
144
+ @name = ast.name
145
+ @id = id
146
+ @members = []
147
+ @doc = ast.doc
148
+ end
149
+
150
+ def add_member member_def
151
+ @members.push member_def
152
+ end
153
+
154
+ def get_member name
155
+ @members.find { |m| m.name == name }
156
+ end
157
+ end
158
+
159
+
160
+ class SequenceDef < Def
161
+ attr_reader :name, :steps, :doc
162
+
163
+ def initialize ast
164
+ super ast
165
+ @name = ast.name
166
+ @steps = []
167
+ @doc = ast.doc
168
+ end
169
+
170
+ def add_step step_def
171
+ @steps.push step_def
172
+ end
173
+ end
174
+
175
+
176
+ class StepDef < Def
177
+ attr_reader :direction, :message, :doc
178
+
179
+ def initialize ast, direction, message
180
+ super ast
181
+ @direction = direction
182
+ @message = message
183
+ @doc = ast.doc
184
+ end
185
+ end
186
+
187
+
188
+ class Runtime
189
+ attr_reader :filename
190
+ attr_reader :builtins
191
+ attr_reader :nodes
192
+ attr_reader :enums
193
+ attr_reader :structs
194
+ attr_reader :directions
195
+ attr_reader :sequences
196
+ attr_reader :includes
197
+
198
+ def initialize filename
199
+ @filename = filename
200
+ @builtins = {}
201
+ @enums = {}
202
+ @nodes = {}
203
+ @structs = {}
204
+ @directions = []
205
+ @sequences = {}
206
+ @includes = []
207
+
208
+ self.init_builtins
209
+ end
210
+
211
+ def init_builtins
212
+ type_names = [
213
+ 'Bool',
214
+ 'Byte',
215
+ 'Short',
216
+ 'Int',
217
+ 'Long',
218
+ 'UShort',
219
+ 'UInt',
220
+ 'ULong',
221
+ 'Float',
222
+ 'Double',
223
+ 'String',
224
+ 'DateTime',
225
+ 'ByteBuffer',
226
+ 'List',
227
+ 'Set',
228
+ 'Map',
229
+ 'Vector2',
230
+ 'Vector3',
231
+ 'Color',
232
+ ]
233
+ type_names.each { |t| @builtins[t] = BuiltinTypeDef.new t }
234
+ end
235
+
236
+ def add_enum enum_def
237
+ @enums[enum_def.name] = enum_def
238
+ end
239
+
240
+ def add_node node_def
241
+ @nodes[node_def.name] = node_def
242
+ @nodes[node_def.nickname] = node_def if node_def.name != node_def.nickname
243
+ end
244
+
245
+ def add_struct struct_def
246
+ @structs[struct_def.name] = struct_def
247
+ end
248
+
249
+ def get_type name
250
+ type_def = @builtins[name]
251
+ return type_def, self if type_def != nil
252
+
253
+ type_def = @enums[name]
254
+ return type_def, self if type_def != nil
255
+
256
+ type_def = @structs[name]
257
+ return type_def, self if type_def != nil
258
+
259
+ @includes.each do |i|
260
+ type_def = i.get_type name
261
+ return type_def, i if type_def != nil
262
+ end
263
+
264
+ return nil, self
265
+ end
266
+
267
+ def add_direction direction_def
268
+ @directions.push direction_def
269
+ end
270
+
271
+ def get_direction client, direction, server
272
+ dir = @directions.find { |d| d.client == client and d.server == server and d.direction == direction }
273
+ return dir if dir != nil
274
+
275
+ @includes.each do |i|
276
+ dir = i.get_direction client, direction, server
277
+ return dir if dir != nil
278
+ end
279
+
280
+ return nil
281
+ end
282
+
283
+ def add_sequence sequence_def
284
+ @sequences[sequence_def.name] = sequence_def
285
+ end
286
+
287
+ def get_sequence name
288
+ sequence = @sequences[name]
289
+ return sequence if sequence != nil
290
+
291
+ @includes.each do |i|
292
+ sequence = i.get_sequence name
293
+ return sequence if sequence != nil
294
+ end
295
+
296
+ return nil
297
+ end
298
+
299
+ def add_include runtime
300
+ @includes.push runtime
301
+ end
302
+
303
+ def get_node_directions node, side
304
+ node_directions = {}
305
+ @directions.each do |direction|
306
+ next if side == :client and direction.client != node
307
+ next if side == :server and direction.server != node
308
+
309
+ opposite_node = nil
310
+ case side
311
+ when :client
312
+ opposite_node = direction.server
313
+ when :server
314
+ opposite_node = direction.client
315
+ end
316
+
317
+ node_direction = node_directions[opposite_node]
318
+ if node_direction == nil
319
+ node_direction = NodeDirection.new node, opposite_node
320
+ node_directions[opposite_node] = node_direction
321
+ end
322
+
323
+ if side == :client and direction.direction == :left
324
+ node_direction.in_direction = direction
325
+ elsif side == :client and direction.direction == :right
326
+ node_direction.out_direction = direction
327
+ elsif side == :server and direction.direction == :left
328
+ node_direction.out_direction = direction
329
+ elsif side == :server and direction.direction == :right
330
+ node_direction.in_direction = direction
331
+ end
332
+ end
333
+ node_directions.values
334
+ end
335
+ end
336
+
337
+
338
+ class NodeDirection
339
+ attr_accessor :local, :remote, :in_direction, :out_direction
340
+
341
+ def initialize local, remote
342
+ @local = local
343
+ @remote = remote
344
+ end
345
+ end
@@ -0,0 +1,47 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'open-uri'
4
+
5
+
6
+ class SequenceDiagramGenerator
7
+ def self.generate runtime
8
+ index = 0
9
+ total = runtime.sequences.length
10
+ runtime.sequences.values.each do |sequence|
11
+ index += 1
12
+ puts "Generating sequence diagram [#{index}/#{total}]: #{sequence.name}.png"
13
+ generate_sequence sequence
14
+ end
15
+ end
16
+
17
+ def self.generate_sequence sequence
18
+ file = "docs/diagrams/#{sequence.name}.png"
19
+ text = make_code sequence
20
+ response = Net::HTTP.post_form(URI.parse('http://www.websequencediagrams.com/index.php'), 'style' => 'omegapple', 'message' => text)
21
+ if response.body =~ /img: "(.+)"/
22
+ url = "http://www.websequencediagrams.com/#{ $1 }"
23
+ url.match(/(png)=(.+)$/)
24
+ open url do |content|
25
+ File.open file, 'wb' do |file|
26
+ file.puts content.read
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def self.make_code sequence
33
+ str = "title #{sequence.name} #{sequence.doc}\n"
34
+ sequence.steps.each do |step|
35
+ direction = step.direction
36
+ head = ''
37
+ case direction.direction
38
+ when :left
39
+ head = direction.server.name + ' -> ' + direction.client.name
40
+ when :right
41
+ head = direction.client.name + ' -> ' + direction.server.name
42
+ end
43
+ str += head + ': ' + step.message.name + ' ' + step.doc + "\n"
44
+ end
45
+ str
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brotorift
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Ren
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rltk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Brotorift generator
42
+ email: lcdcdr2004@gmail.com
43
+ executables:
44
+ - brotorift
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - bin/brotorift
49
+ - lib/ast.rb
50
+ - lib/brotorift.rb
51
+ - lib/case_helper.rb
52
+ - lib/compiler.rb
53
+ - lib/compiler_error.rb
54
+ - lib/generators/elixir_server_generator.ex.erb
55
+ - lib/generators/elixir_server_generator.rb
56
+ - lib/generators/scala_server_generator.rb
57
+ - lib/generators/scala_server_generator.scala.erb
58
+ - lib/generators/unity_client_generator.cs.erb
59
+ - lib/generators/unity_client_generator.rb
60
+ - lib/lexer.rb
61
+ - lib/parser.rb
62
+ - lib/runtime.rb
63
+ - lib/sequence_diagram_generator.rb
64
+ homepage: http://rubygems.org/gems/brotorift
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.7.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Brotorift
88
+ test_files: []