slow_blink 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,448 @@
1
+ # Copyright (c) 2016 Cameron Harper
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ # this software and associated documentation files (the "Software"), to deal in
5
+ # the Software without restriction, including without limitation the rights to
6
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ # the Software, and to permit persons to whom the Software is furnished to do so,
8
+ # subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+
20
+ module SlowBlink
21
+
22
+ class Type
23
+
24
+ extend CompactEncoder
25
+ include Annotatable
26
+
27
+ # @param [String]
28
+ attr_reader :location
29
+
30
+ # @private
31
+ #
32
+ # @param location [String]
33
+ def initialize(location)
34
+ @schema = nil
35
+ @annotes = {}
36
+ @location = location
37
+ end
38
+
39
+ # @private
40
+ #
41
+ # @macro common_link
42
+ def link(schema, stack=[])
43
+ @schema = schema
44
+ end
45
+
46
+ def ===(other)
47
+ self.class == other.class
48
+ end
49
+
50
+ end
51
+
52
+ # Blink Specification 3.2
53
+ class STRING < Type
54
+
55
+ # @return [Integer] maximum size
56
+ # @return [nil] no maximum size
57
+ attr_reader :size
58
+
59
+ # @private
60
+ #
61
+ # @param size [Integer] maximum size
62
+ # @param location [String]
63
+ def initialize(size, location)
64
+ @size = size
65
+ super(location)
66
+ end
67
+
68
+ # @private
69
+ def validate(value)
70
+ if @schema and value.kind_of? String and value.size < @size
71
+ true
72
+ else
73
+ raise
74
+ end
75
+ end
76
+
77
+ # @private
78
+ def encode_compact(value)
79
+ putVLC(value.size) + value
80
+ end
81
+
82
+ end
83
+
84
+ # Blink Specification 3.4
85
+ class FIXED < STRING
86
+ # @private
87
+ def validate(value)
88
+ if @schema and value.kind_of? String and value.size == @size
89
+ true
90
+ else
91
+ raise
92
+ end
93
+ end
94
+
95
+ # @private
96
+ def encode_compact(value)
97
+ value
98
+ end
99
+ end
100
+
101
+ # Blink Specification 3.3
102
+ class BINARY < STRING
103
+ end
104
+
105
+ class INTEGER < Type
106
+
107
+ # @private
108
+ def validate(value)
109
+ if @schema and value.kind_of? Integer and self.class::RANGE.include? value
110
+ true
111
+ else
112
+ raise
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ # Blink Specification 3.1
119
+ class I8 < INTEGER
120
+
121
+ RANGE = Range.new(-128, 127)
122
+
123
+ # @private
124
+ def encode_compact(value)
125
+ putVLC(value, signed: true)
126
+ end
127
+ end
128
+
129
+ # Blink Specification 3.1
130
+ class I16 < INTEGER
131
+
132
+ RANGE = Range.new(-32768, 32767)
133
+
134
+ # @private
135
+ def encode_compact(value)
136
+ putVLC(value, signed: true)
137
+ end
138
+ end
139
+
140
+ # Blink Specification 3.1
141
+ class I32 < INTEGER
142
+
143
+ RANGE = Range.new(-2147483648, 2147483647)
144
+
145
+ # @private
146
+ def encode_compact(value)
147
+ putVLC(value, signed: true)
148
+ end
149
+ end
150
+
151
+ # Blink Specification 3.1
152
+ class I64 < INTEGER
153
+
154
+ RANGE = Range.new(-9223372036854775808, 9223372036854775807)
155
+
156
+ # @private
157
+ def encode_compact(value)
158
+ putVLC(value, signed: true)
159
+ end
160
+ end
161
+
162
+ # Blink Specification 3.1
163
+ class U8 < INTEGER
164
+
165
+ RANGE = Range.new(0, 0xff)
166
+
167
+ # @private
168
+ def encode_compact(value)
169
+ putVLC(value)
170
+ end
171
+ end
172
+
173
+ # Blink Specification 3.1
174
+ class U16 < INTEGER
175
+
176
+ RANGE = Range.new(0, 0xffff)
177
+
178
+ # @private
179
+ def encode_compact(value)
180
+ putVLC(value)
181
+ end
182
+ end
183
+
184
+ # Blink Specification 3.1
185
+ class U32 < INTEGER
186
+
187
+ RANGE = Range.new(0, 0xffffffff)
188
+
189
+ # @private
190
+ def encode_compact(value)
191
+ putVLC(value)
192
+ end
193
+ end
194
+
195
+ # Blink Specification 3.1
196
+ class U64 < INTEGER
197
+
198
+ RANGE = Range.new(0, 0xffffffffffffffff)
199
+
200
+ # @private
201
+ def encode_compact(value)
202
+ putVLC(value)
203
+ end
204
+ end
205
+
206
+ # Blink Specification 3.1
207
+ class F64 < INTEGER
208
+
209
+ # @private
210
+ def encode_compact(value)
211
+ putF64(value)
212
+ end
213
+ end
214
+
215
+ # Blink Specification 3.6
216
+ class BOOLEAN < Type
217
+
218
+ # @private
219
+ def encode_compact(value)
220
+ putBool(value)
221
+ end
222
+ end
223
+
224
+ # Blink Specification 3.7
225
+ class DECIMAL < Type
226
+ end
227
+
228
+ # Blink Specification 3.10
229
+ class DATE < Type
230
+ end
231
+
232
+ # Blink Specification 3.9
233
+ class NANO_TIME < Type
234
+
235
+ # @private
236
+ def validate(value)
237
+ if value.kind_of? Time or value.kind_of? Integer
238
+ true
239
+ else
240
+ raise
241
+ end
242
+ end
243
+
244
+ end
245
+
246
+ # Blink Specification 3.9
247
+ class MILLI_TIME < Type
248
+
249
+ # @private
250
+ def validate(value)
251
+ if value.kind_of? Time or value.kind_of? Integer
252
+ true
253
+ else
254
+ raise
255
+ end
256
+ end
257
+
258
+ end
259
+
260
+ # Blink Specification 3.11
261
+ class TIME_OF_DAY_NANO < Type
262
+
263
+ # @private
264
+ def validate(value)
265
+ if value.kind_of? Time
266
+ true
267
+ elsif value.kind_of? Integer and value <= 86400000000000
268
+ true
269
+ else
270
+ raise
271
+ end
272
+ end
273
+
274
+ end
275
+
276
+ # Blink Specification 3.11
277
+ class TIME_OF_DAY_MILLI < Type
278
+
279
+ # @private
280
+ def validate(value)
281
+ if value.kind_of? Time
282
+ true
283
+ elsif value.kind_of? Integer and value <= 86400000
284
+ true
285
+ else
286
+ raise
287
+ end
288
+ end
289
+
290
+ end
291
+
292
+ # Blink Specification 3.12
293
+ class SEQUENCE < Type
294
+
295
+ # @return [Type]
296
+ attr_reader :type
297
+
298
+ # @private
299
+ #
300
+ # @param type [Type] repeating type
301
+ # @param location [String]
302
+ def initialize(type, location)
303
+ @type = nil
304
+ @rawType = type
305
+ super(location)
306
+ end
307
+
308
+ # @private
309
+ #
310
+ # @macro common_link
311
+ def link(schema, stack=[])
312
+ if @schema != schema
313
+ @schema = nil
314
+ case @rawType.class
315
+ when REF
316
+ schema.symbol(@rawType)
317
+ when SEQUENCE
318
+ puts "error: sequence of sequence is not permitted"
319
+ else
320
+ @type = @rawType
321
+ @schema = schema
322
+ end
323
+ end
324
+ @schema
325
+ end
326
+
327
+ # @private
328
+ def validate(value)
329
+ if @schema and value.kind_of? Array
330
+ value.each do |v|
331
+ @type.validate(v)
332
+ end
333
+ else
334
+ raise
335
+ end
336
+ end
337
+
338
+ # @private
339
+ def encode_compact(value)
340
+ out = putVLC(value.size)
341
+ value.each do |v|
342
+ out << @type.encode_compact(v)
343
+ end
344
+ out
345
+ end
346
+
347
+ end
348
+
349
+ class REF < Type
350
+
351
+ # @return [true] dynamic reference
352
+ # @return [false] static reference
353
+ def dynamic?
354
+ @dynamic
355
+ end
356
+
357
+ # @private
358
+ #
359
+ # @param ref [String]
360
+ # @param dynamic [true,false]
361
+ # @param location [String]
362
+ def initialize(ref, dynamic, location)
363
+ @ref = ref
364
+ @dynamic = dynamic
365
+ @object = nil
366
+ super(location)
367
+ end
368
+
369
+ def value
370
+ if @schema
371
+ @object
372
+ else
373
+ raise "must be linked"
374
+ end
375
+ end
376
+
377
+ # @private
378
+ #
379
+ # @macro common_link
380
+ def link(schema, stack=[])
381
+ if @schema != schema
382
+ @schema = nil
383
+ ref = @ref
384
+ object = schema.symbol(ref)
385
+ if object and object.link(schema, stack << self)
386
+ # walk through all references until object
387
+ # refers to an actual type
388
+ loop do
389
+ if object.is_a? REF
390
+ object = object.object
391
+ else
392
+ break
393
+ end
394
+ end
395
+ if @dynamic and @object.class != Group
396
+ puts "#{@location}: error: '#{@ref} *' must resolve to a Group"
397
+ else
398
+ @object = object
399
+ @schema = schema
400
+ end
401
+ else
402
+ puts "#{@location}: error: '#{@ref}' is not defined in schema"
403
+ end
404
+ end
405
+ @schema
406
+ end
407
+
408
+ # @private
409
+ def validate(value)
410
+ @object.validate(input)
411
+ end
412
+
413
+ # @private
414
+ def encode_compact(value)
415
+ @object.encode_compact(value, dynamic: @dynamic)
416
+ end
417
+ end
418
+
419
+ # any group
420
+ #
421
+ # Blink Specification 3.9
422
+ class OBJECT < Type
423
+
424
+ # @private
425
+ def validate(value)
426
+ if value.kind_of? Hash and value["$type"]
427
+
428
+ group = @schema.group(value["$type"])
429
+ if group
430
+ group.validate(value)
431
+ else
432
+ raise
433
+ end
434
+
435
+ else
436
+ raise
437
+ end
438
+ end
439
+
440
+ # @private
441
+ def encode_compact(value, **opts)
442
+ @object.encode_compact(value, dynamic: true)
443
+ end
444
+ end
445
+
446
+ end
447
+
448
+
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2016 Cameron Harper
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ # this software and associated documentation files (the "Software"), to deal in
5
+ # the Software without restriction, including without limitation the rights to
6
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ # the Software, and to permit persons to whom the Software is furnished to do so,
8
+ # subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+
20
+ module SlowBlink
21
+ VERSION = '0.0.2'.freeze
22
+ end
data/lib/slow_blink.rb ADDED
@@ -0,0 +1,20 @@
1
+ # Copyright (c) 2016 Cameron Harper
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ # this software and associated documentation files (the "Software"), to deal in
5
+ # the Software without restriction, including without limitation the rights to
6
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ # the Software, and to permit persons to whom the Software is furnished to do so,
8
+ # subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+
20
+ require 'slow_blink/schema'
data/rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rake/testtask'
2
+ require 'rake/extensiontask'
3
+
4
+ DIR_SRC = "ext/slow_blink/ext_schema_parser"
5
+
6
+ Rake::ExtensionTask.new do |ext|
7
+ ext.name = "ext_schema_parser"
8
+ ext.ext_dir = DIR_SRC
9
+ ext.lib_dir = "lib/slow_blink"
10
+ end
11
+
12
+ desc "parser tests"
13
+ Rake::TestTask.new do |t|
14
+ t.name = :test
15
+ t.libs << "lib"
16
+ t.test_files = FileList["test/integration/tc_*.rb"]
17
+ end
18
+
19
+ desc "run flex-bison"
20
+ task :flexbison do
21
+ system "flex --outfile=#{DIR_SRC}/lexer.c --header-file=#{DIR_SRC}/lexer.h #{DIR_SRC}/parser.l"
22
+ system "bison -d #{DIR_SRC}/parser.y --output=#{DIR_SRC}/parser.c --report=all --report-file=#{DIR_SRC}/report.txt"
23
+ end
24
+
25
+ task :local => [:flexbison, :compile, :test] do
26
+ end
27
+
28
+ task :default => :test
29
+
@@ -0,0 +1,20 @@
1
+ require 'stringio'
2
+
3
+ module Kernel
4
+
5
+ def capture_stderr
6
+
7
+ err = StringIO.new
8
+ $stderr = err
9
+
10
+ yield
11
+
12
+ return err
13
+
14
+ ensure
15
+
16
+ $stderr = STDERR
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,57 @@
1
+ require_relative "capture_stderr"
2
+ require "test/unit"
3
+ require "slow_blink"
4
+
5
+ inputs = {}
6
+ root = "test/input"
7
+
8
+ testClass = Class.new(Test::Unit::TestCase) do
9
+
10
+ class << self
11
+ attr_accessor :inputs
12
+ end
13
+
14
+ Dir.foreach(root) do |filename|
15
+
16
+ next if filename == ".." or filename == "."
17
+
18
+ test_name = "test_#{filename.sub(".blink", "")}"
19
+ inputs[test_name.to_sym] = {:fileName => filename, :buffer => File.new("#{root}/#{filename}", "r").read}
20
+
21
+ define_method( test_name ) do
22
+
23
+ output = nil
24
+
25
+ # run and intercept stderr output
26
+ #err = capture_stderr do
27
+
28
+ output = SlowBlink::Schema.parse(inputs[__method__][:buffer], filename: inputs[__method__][:fileName])
29
+ puts output
30
+
31
+
32
+ #end
33
+
34
+ # there should have been no messages to stderr
35
+ #assert_equal("", err.string, "unexpected error messages")
36
+
37
+ # if there were messages, forward them to stderr
38
+ #if err.string != ""
39
+
40
+ #STDERR.puts err.string
41
+
42
+ #end
43
+
44
+ #puts output.to_s
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ # set inputs as a class variable
53
+ testClass.inputs = inputs
54
+
55
+ # name the dynamic test class
56
+ Object.const_set("TestInputs", testClass)
57
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slow_blink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Harper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-16 00:00:00.000000000 Z
11
+ date: 2016-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -55,9 +55,38 @@ dependencies:
55
55
  description:
56
56
  email: contact@cjh.id.au
57
57
  executables: []
58
- extensions: []
58
+ extensions:
59
+ - ext/slow_blink/ext_schema_parser/extconf.rb
59
60
  extra_rdoc_files: []
60
- files: []
61
+ files:
62
+ - ext/slow_blink/ext_schema_parser/common.h
63
+ - ext/slow_blink/ext_schema_parser/extconf.rb
64
+ - ext/slow_blink/ext_schema_parser/lexer.c
65
+ - ext/slow_blink/ext_schema_parser/lexer.h
66
+ - ext/slow_blink/ext_schema_parser/parser.c
67
+ - ext/slow_blink/ext_schema_parser/parser.h
68
+ - ext/slow_blink/ext_schema_parser/parser.l
69
+ - ext/slow_blink/ext_schema_parser/parser.y
70
+ - lib/slow_blink.rb
71
+ - lib/slow_blink/annotatable.rb
72
+ - lib/slow_blink/annotation.rb
73
+ - lib/slow_blink/compact_encoder.rb
74
+ - lib/slow_blink/component_reference.rb
75
+ - lib/slow_blink/definition.rb
76
+ - lib/slow_blink/enumeration.rb
77
+ - lib/slow_blink/error.rb
78
+ - lib/slow_blink/field.rb
79
+ - lib/slow_blink/group.rb
80
+ - lib/slow_blink/incremental_annotation.rb
81
+ - lib/slow_blink/message.rb
82
+ - lib/slow_blink/name_with_id.rb
83
+ - lib/slow_blink/schema.rb
84
+ - lib/slow_blink/sym.rb
85
+ - lib/slow_blink/type.rb
86
+ - lib/slow_blink/version.rb
87
+ - rakefile
88
+ - test/integration/capture_stderr.rb
89
+ - test/integration/tc_inputs.rb
61
90
  homepage: https://github.com/cjhdev/slow_blink
62
91
  licenses:
63
92
  - MIT
@@ -82,5 +111,7 @@ rubygems_version: 2.5.1
82
111
  signing_key:
83
112
  specification_version: 4
84
113
  summary: Blink Protocol in Ruby (placeholder)
85
- test_files: []
114
+ test_files:
115
+ - test/integration/tc_inputs.rb
116
+ - test/integration/capture_stderr.rb
86
117
  has_rdoc: yard