jiffy 1.0.4 → 1.1.0

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.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +13 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +27 -28
  5. data/Rakefile +17 -0
  6. data/bin/jiffy +28 -3
  7. data/jiffy.gemspec +15 -66
  8. data/lib/jiffy.rb +27 -27
  9. data/lib/jiffy/array_mimicking_io.rb +1 -1
  10. data/lib/jiffy/outputter.rb +85 -0
  11. data/lib/jiffy/outputters/json.rb +35 -0
  12. data/lib/jiffy/outputters/ruby.rb +35 -0
  13. data/lib/jiffy/parser.rb +17 -0
  14. data/lib/jiffy/parsers/json.rb +18 -42
  15. data/lib/jiffy/parsers/json.rl +10 -22
  16. data/lib/jiffy/parsers/json_array.rb +22 -29
  17. data/lib/jiffy/parsers/json_array.rl +11 -12
  18. data/lib/jiffy/parsers/json_float.rb +39 -21
  19. data/lib/jiffy/parsers/json_float.rl +15 -7
  20. data/lib/jiffy/parsers/json_object.rb +30 -49
  21. data/lib/jiffy/parsers/json_object.rl +13 -20
  22. data/lib/jiffy/parsers/json_string.rb +70 -27
  23. data/lib/jiffy/parsers/json_string.rl +21 -6
  24. data/lib/jiffy/parsers/json_value.rb +33 -52
  25. data/lib/jiffy/parsers/json_value.rl +15 -34
  26. data/lib/jiffy/version.rb +1 -1
  27. data/test/jiffy_test.rb +261 -34
  28. data/test/outputter_test.rb +155 -0
  29. data/test/outputters/json_test.rb +22 -0
  30. metadata +9 -60
  31. data/lib/jiffy/json_outputter.rb +0 -62
  32. data/test/negative-examples/hexadecimal.json +0 -3
  33. data/test/negative-examples/infinity-value.json +0 -3
  34. data/test/negative-examples/leading-comma.json +0 -3
  35. data/test/negative-examples/leading-zero.json +0 -3
  36. data/test/negative-examples/line-break.json +0 -4
  37. data/test/negative-examples/missing-colon.json +0 -3
  38. data/test/negative-examples/nan-value,json +0 -3
  39. data/test/negative-examples/positive-float.json +0 -3
  40. data/test/negative-examples/positive-integer.json +0 -3
  41. data/test/negative-examples/single-quote.json +0 -3
  42. data/test/negative-examples/string-as-root.json +0 -1
  43. data/test/negative-examples/tab-character.json +0 -3
  44. data/test/negative-examples/trailing-array-seperator.json +0 -3
  45. data/test/negative-examples/trailing-object-seperator.json +0 -3
  46. data/test/negative-examples/true-as-root.json +0 -1
  47. data/test/negative-examples/unclosed-array.json +0 -2
  48. data/test/negative-examples/unclosed-object.json +0 -2
  49. data/test/positive-examples/array-as-root.json +0 -3
  50. data/test/positive-examples/array-nested-inside-array.json +0 -5
  51. data/test/positive-examples/array-nested-inside-object.json +0 -5
  52. data/test/positive-examples/false-value.json +0 -3
  53. data/test/positive-examples/null-value.json +0 -3
  54. data/test/positive-examples/number-1.json +0 -3
  55. data/test/positive-examples/number-10.json +0 -3
  56. data/test/positive-examples/number-11.json +0 -3
  57. data/test/positive-examples/number-12.json +0 -3
  58. data/test/positive-examples/number-13.json +0 -3
  59. data/test/positive-examples/number-14.json +0 -3
  60. data/test/positive-examples/number-15.json +0 -3
  61. data/test/positive-examples/number-16.json +0 -3
  62. data/test/positive-examples/number-17.json +0 -3
  63. data/test/positive-examples/number-18.json +0 -3
  64. data/test/positive-examples/number-19.json +0 -3
  65. data/test/positive-examples/number-2.json +0 -3
  66. data/test/positive-examples/number-20.json +0 -3
  67. data/test/positive-examples/number-3.json +0 -3
  68. data/test/positive-examples/number-4.json +0 -3
  69. data/test/positive-examples/number-5.json +0 -3
  70. data/test/positive-examples/number-6.json +0 -3
  71. data/test/positive-examples/number-7.json +0 -3
  72. data/test/positive-examples/number-8.json +0 -3
  73. data/test/positive-examples/number-9.json +0 -3
  74. data/test/positive-examples/object-as-root.json +0 -3
  75. data/test/positive-examples/object-nested-inside-array.json +0 -5
  76. data/test/positive-examples/object-nested-inside-object.json +0 -5
  77. data/test/positive-examples/seperated-array-values.json +0 -4
  78. data/test/positive-examples/seperated-object-properties.json +0 -4
  79. data/test/positive-examples/string-backspace.json +0 -3
  80. data/test/positive-examples/string-carriage-return.json +0 -3
  81. data/test/positive-examples/string-formfeed.json +0 -3
  82. data/test/positive-examples/string-horizontal-tab.json +0 -3
  83. data/test/positive-examples/string-newline.json +0 -3
  84. data/test/positive-examples/string-quotation.json +0 -3
  85. data/test/positive-examples/string-reverse-solidus.json +0 -3
  86. data/test/positive-examples/string-solidus.json +0 -3
  87. data/test/positive-examples/string-trivial.json +0 -3
  88. data/test/positive-examples/string-unicode.json +0 -3
  89. data/test/positive-examples/true-value.json +0 -3
@@ -0,0 +1,35 @@
1
+ class Jiffy
2
+ module Outputters
3
+ class Json < Outputter
4
+ rule :begin_object, :payload => "{", :indent => 1, :break_after => true
5
+ rule :end_object, :payload => "}", :indent => -1, :break_before => true
6
+ rule :begin_array, :payload => "[", :indent => 1, :break_after => true
7
+ rule :end_array, :payload => "]", :indent => -1, :break_before => true
8
+ rule :value_separator, :payload => ",", :break_after => true
9
+ rule :name_separator, :payload => ": "
10
+ rule :begin_name, :payload => "\"", :color => :purple
11
+ rule :end_name, :payload => "\"", :color => :purple
12
+ rule :begin_string, :payload => "\"", :color => :purple
13
+ rule :end_string, :payload => "\"", :color => :purple
14
+ rule :null, :payload => "null", :color => :gray
15
+ rule :true, :payload => "true", :color => :blue
16
+ rule :false, :payload => "false", :color => :blue
17
+ rule :char, :color => :purple
18
+ rule :number, :color => :cyan
19
+ rule :exponent, :payload => "e", :color => :cyan
20
+ rule :minus, :payload => "-", :color => :cyan
21
+ rule :plus, :payload => "+", :color => :cyan
22
+ rule :escaped_quotation_mark, :payload => "\""
23
+ rule :escaped_reverse_solidus, :payload => "\\"
24
+ rule :escaped_solidus, :payload => "/"
25
+ rule :escaped_backspace, :payload => "\\b"
26
+ rule :escaped_formfeed, :payload => "\\f"
27
+ rule :escaped_newline, :payload => "\\n"
28
+ rule :escaped_carriage_return, :payload => "\\r"
29
+ rule :escaped_horizontal_tab, :payload => "\\t"
30
+ rule :escaped_unicode do |sequence|
31
+ "\\u#{sequence}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ class Jiffy
2
+ module Outputters
3
+ class Ruby < Outputter
4
+ rule :begin_object, :payload => "{", :indent => 1, :break_after => true
5
+ rule :end_object, :payload => "}", :indent => -1, :break_before => true
6
+ rule :begin_array, :payload => "[", :indent => 1, :break_after => true
7
+ rule :end_array, :payload => "]", :indent => -1, :break_before => true
8
+ rule :value_separator, :payload => ",", :break_after => true
9
+ rule :name_separator, :payload => " => "
10
+ rule :begin_name, :payload => "\"", :color => :light_purple
11
+ rule :end_name, :payload => "\"", :color => :light_purple
12
+ rule :begin_string, :payload => "\"", :color => :light_purple
13
+ rule :end_string, :payload => "\"", :color => :light_purple
14
+ rule :null, :payload => "nil", :color => :purple
15
+ rule :true, :payload => "true", :color => :purple
16
+ rule :false, :payload => "false", :color => :purple
17
+ rule :char, :color => :purple
18
+ rule :number, :color => :purple
19
+ rule :exponent, :payload => "e", :color => :purple
20
+ rule :minus, :payload => "-", :color => :purple
21
+ rule :plus, :payload => "+", :color => :purple
22
+ rule :escaped_quotation_mark, :payload => "\""
23
+ rule :escaped_reverse_solidus, :payload => "\\"
24
+ rule :escaped_solidus, :payload => "/"
25
+ rule :escaped_backspace, :payload => "\\b"
26
+ rule :escaped_formfeed, :payload => "\\f"
27
+ rule :escaped_newline, :payload => "\\n"
28
+ rule :escaped_carriage_return, :payload => "\\r"
29
+ rule :escaped_horizontal_tab, :payload => "\\t"
30
+ rule :escaped_unicode do |sequence|
31
+ "\\u#{sequence}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ class Jiffy
2
+ class Parser
3
+ attr_accessor :p, :data, :yielder
4
+
5
+ alias_method :y, :yielder
6
+
7
+ def initialize(options = {})
8
+ self.p = options[:p]
9
+ self.data = options[:data]
10
+ self.yielder = options[:yielder]
11
+ end
12
+
13
+ def raise_unparseable(p)
14
+ raise UnparseableError, "Unexpected token at position #{p}"
15
+ end
16
+ end
17
+ end
@@ -1,12 +1,12 @@
1
1
 
2
2
  # line 1 "json.rl"
3
3
 
4
- # line 27 "json.rl"
4
+ # line 17 "json.rl"
5
5
 
6
6
 
7
7
  class Jiffy
8
8
  module Parsers
9
- module Json
9
+ class Json < Parser
10
10
  def initialize(*args)
11
11
 
12
12
  # line 13 "json.rb"
@@ -114,26 +114,27 @@ end
114
114
  self.json_en_main = 1;
115
115
 
116
116
 
117
- # line 34 "json.rl"
117
+ # line 24 "json.rl"
118
+
118
119
  super
119
120
  end
120
121
 
121
- def parse_json
122
+ def parse
122
123
  pe = :ignored
123
124
  eof = :ignored
125
+ p = self.p
124
126
 
125
- begin
126
-
127
- # line 128 "json.rb"
127
+
128
+ # line 129 "json.rb"
128
129
  begin
129
130
  p ||= 0
130
131
  pe ||= data.length
131
132
  cs = json_start
132
133
  end
133
134
 
134
- # line 43 "json.rl"
135
-
136
- # line 137 "json.rb"
135
+ # line 34 "json.rl"
136
+
137
+ # line 138 "json.rb"
137
138
  begin
138
139
  _klen, _trans, _keys, _acts, _nacts = nil
139
140
  _goto_level = 0
@@ -218,39 +219,17 @@ when 0 then
218
219
  # line 5 "json.rl"
219
220
  begin
220
221
 
221
- if np = parse_json_object(p, pe)
222
- begin p = (( np))-1; end
222
+ begin p = (( JsonObject.new(p: p, data: data, yielder: yielder).parse))-1; end
223
223
 
224
- else
225
- p = p - 1;
226
- begin
227
- p += 1
228
- _trigger_goto = true
229
- _goto_level = _out
230
- break
231
- end
232
-
233
- end
234
224
  end
235
225
  when 1 then
236
- # line 14 "json.rl"
226
+ # line 9 "json.rl"
237
227
  begin
238
228
 
239
- if np = parse_json_array(p, pe)
240
- begin p = (( np))-1; end
229
+ begin p = (( JsonArray.new(p: p, data: data, yielder: yielder).parse))-1; end
241
230
 
242
- else
243
- p = p - 1;
244
- begin
245
- p += 1
246
- _trigger_goto = true
247
- _goto_level = _out
248
- break
249
- end
250
-
251
- end
252
231
  end
253
- # line 254 "json.rb"
232
+ # line 233 "json.rb"
254
233
  end # action switch
255
234
  end
256
235
  end
@@ -277,14 +256,11 @@ when 1 then
277
256
  end
278
257
  end
279
258
 
280
- # line 44 "json.rl"
281
- rescue EOFError
282
- if p < data.bytes_read || data.bytes_read == 0
283
- raise UnexpectedEndError, 'Unexpected end of input'
284
- end
285
- end
259
+ # line 35 "json.rl"
286
260
 
287
261
  raise_unparseable p unless p == data.bytes_read
262
+ ensure
263
+ self.p = p
288
264
  end
289
265
  end
290
266
  end
@@ -3,21 +3,11 @@
3
3
  include json_common "json_common.rl";
4
4
 
5
5
  action parse_object {
6
- if np = parse_json_object(fpc, pe)
7
- fexec np;
8
- else
9
- fhold;
10
- fbreak;
11
- end
6
+ fexec JsonObject.new(p: p, data: data, yielder: yielder).parse;
12
7
  }
13
8
 
14
9
  action parse_array {
15
- if np = parse_json_array(fpc, pe)
16
- fexec np;
17
- else
18
- fhold;
19
- fbreak;
20
- end
10
+ fexec JsonArray.new(p: p, data: data, yielder: yielder).parse;
21
11
  }
22
12
 
23
13
  main := ignore* (
@@ -28,26 +18,24 @@
28
18
 
29
19
  class Jiffy
30
20
  module Parsers
31
- module Json
21
+ class Json < Parser
32
22
  def initialize(*args)
33
23
  %% write data;
24
+
34
25
  super
35
26
  end
36
27
 
37
- def parse_json
28
+ def parse
38
29
  pe = :ignored
39
30
  eof = :ignored
31
+ p = self.p
40
32
 
41
- begin
42
- %% write init;
43
- %% write exec;
44
- rescue EOFError
45
- if p < data.bytes_read || data.bytes_read == 0
46
- raise UnexpectedEndError, 'Unexpected end of input'
47
- end
48
- end
33
+ %% write init;
34
+ %% write exec;
49
35
 
50
36
  raise_unparseable p unless p == data.bytes_read
37
+ ensure
38
+ self.p = p
51
39
  end
52
40
  end
53
41
  end
@@ -1,12 +1,12 @@
1
1
 
2
2
  # line 1 "json_array.rl"
3
3
 
4
- # line 23 "json_array.rl"
4
+ # line 17 "json_array.rl"
5
5
 
6
6
 
7
7
  class Jiffy
8
8
  module Parsers
9
- module JsonArray
9
+ class JsonArray < Parser
10
10
  def initialize(*args)
11
11
 
12
12
  # line 13 "json_array.rb"
@@ -129,22 +129,27 @@ end
129
129
  self.json_array_en_main = 1;
130
130
 
131
131
 
132
- # line 30 "json_array.rl"
132
+ # line 24 "json_array.rl"
133
+
133
134
  super
134
135
  end
135
136
 
136
- def parse_json_array(p, pe)
137
+ def parse
138
+ pe = :ignored
139
+ eof = :ignored
140
+ p = self.p
141
+
137
142
 
138
- # line 139 "json_array.rb"
143
+ # line 144 "json_array.rb"
139
144
  begin
140
145
  p ||= 0
141
146
  pe ||= data.length
142
147
  cs = json_array_start
143
148
  end
144
149
 
145
- # line 35 "json_array.rl"
150
+ # line 34 "json_array.rl"
146
151
 
147
- # line 148 "json_array.rb"
152
+ # line 153 "json_array.rb"
148
153
  begin
149
154
  _klen, _trans, _keys, _acts, _nacts = nil
150
155
  _goto_level = 0
@@ -229,23 +234,11 @@ when 0 then
229
234
  # line 5 "json_array.rl"
230
235
  begin
231
236
 
232
- np = parse_json_value(p, pe)
233
-
234
- if np
235
- begin p = (( np))-1; end
236
-
237
- else
238
- p = p - 1; begin
239
- p += 1
240
- _trigger_goto = true
241
- _goto_level = _out
242
- break
243
- end
237
+ begin p = (( JsonValue.new(p: p, data: data, yielder: yielder).parse))-1; end
244
238
 
245
- end
246
239
  end
247
240
  when 1 then
248
- # line 15 "json_array.rl"
241
+ # line 9 "json_array.rl"
249
242
  begin
250
243
  p = p - 1; begin
251
244
  p += 1
@@ -255,18 +248,18 @@ when 1 then
255
248
  end
256
249
  end
257
250
  when 2 then
258
- # line 17 "json_array.rl"
251
+ # line 11 "json_array.rl"
259
252
  begin
260
- o.t :value_separator end
253
+ y << :value_separator end
261
254
  when 3 then
262
- # line 19 "json_array.rl"
255
+ # line 13 "json_array.rl"
263
256
  begin
264
- o.t :begin_array end
257
+ y << :begin_array end
265
258
  when 4 then
266
- # line 22 "json_array.rl"
259
+ # line 16 "json_array.rl"
267
260
  begin
268
- o.t :end_array end
269
- # line 270 "json_array.rb"
261
+ y << :end_array end
262
+ # line 263 "json_array.rb"
270
263
  end # action switch
271
264
  end
272
265
  end
@@ -293,7 +286,7 @@ when 4 then
293
286
  end
294
287
  end
295
288
 
296
- # line 36 "json_array.rl"
289
+ # line 35 "json_array.rl"
297
290
 
298
291
  if cs >= json_array_first_final
299
292
  p + 1
@@ -3,34 +3,33 @@
3
3
  include json_common "json_common.rl";
4
4
 
5
5
  action parse_value {
6
- np = parse_json_value(fpc, pe)
7
-
8
- if np
9
- fexec np;
10
- else
11
- fhold; fbreak;
12
- end
6
+ fexec JsonValue.new(p: p, data: data, yielder: yielder).parse;
13
7
  }
14
8
 
15
9
  action exit { fhold; fbreak; }
16
10
 
17
- next_element = value_separator >{ o.t :value_separator } ignore* begin_value >parse_value;
11
+ next_element = value_separator >{ y << :value_separator } ignore* begin_value >parse_value;
18
12
 
19
- main := begin_array >{ o.t :begin_array } ignore*
13
+ main := begin_array >{ y << :begin_array } ignore*
20
14
  ((begin_value >parse_value ignore*)
21
15
  (ignore* next_element ignore*)*)?
22
- end_array >{ o.t :end_array } @exit;
16
+ end_array >{ y << :end_array } @exit;
23
17
  }%%
24
18
 
25
19
  class Jiffy
26
20
  module Parsers
27
- module JsonArray
21
+ class JsonArray < Parser
28
22
  def initialize(*args)
29
23
  %% write data;
24
+
30
25
  super
31
26
  end
32
27
 
33
- def parse_json_array(p, pe)
28
+ def parse
29
+ pe = :ignored
30
+ eof = :ignored
31
+ p = self.p
32
+
34
33
  %% write init;
35
34
  %% write exec;
36
35
 
@@ -1,12 +1,12 @@
1
1
 
2
2
  # line 1 "json_float.rl"
3
3
 
4
- # line 15 "json_float.rl"
4
+ # line 18 "json_float.rl"
5
5
 
6
6
 
7
7
  class Jiffy
8
8
  module Parsers
9
- module JsonFloat
9
+ class JsonFloat < Parser
10
10
  def initialize(*args)
11
11
 
12
12
  # line 13 "json_float.rb"
@@ -15,7 +15,8 @@ class << self
15
15
  private :_json_float_actions, :_json_float_actions=
16
16
  end
17
17
  self._json_float_actions = [
18
- 0, 1, 0, 1, 1, 1, 2
18
+ 0, 1, 0, 1, 1, 1, 2, 1,
19
+ 3, 1, 4, 1, 5
19
20
  ]
20
21
 
21
22
  class << self
@@ -72,10 +73,10 @@ class << self
72
73
  end
73
74
  self._json_float_indicies = [
74
75
  0, 2, 3, 1, 2, 3, 1, 4,
75
- 1, 5, 5, 6, 1, 6, 1, 1,
76
- 8, 9, 9, 1, 7, 1, 9, 9,
77
- 1, 4, 7, 1, 1, 1, 6, 7,
78
- 1, 8, 9, 9, 10, 7, 0
76
+ 1, 5, 6, 7, 1, 7, 1, 1,
77
+ 9, 10, 10, 1, 8, 1, 10, 10,
78
+ 1, 4, 8, 1, 1, 1, 7, 8,
79
+ 1, 9, 10, 10, 3, 8, 0
79
80
  ]
80
81
 
81
82
  class << self
@@ -83,8 +84,8 @@ class << self
83
84
  private :_json_float_trans_targs, :_json_float_trans_targs=
84
85
  end
85
86
  self._json_float_trans_targs = [
86
- 2, 0, 6, 10, 8, 5, 9, 7,
87
- 3, 4, 10
87
+ 2, 0, 6, 10, 8, 5, 5, 9,
88
+ 7, 3, 4
88
89
  ]
89
90
 
90
91
  class << self
@@ -92,8 +93,8 @@ class << self
92
93
  private :_json_float_trans_actions, :_json_float_trans_actions=
93
94
  end
94
95
  self._json_float_trans_actions = [
95
- 3, 0, 5, 3, 5, 3, 5, 1,
96
- 3, 3, 5
96
+ 9, 0, 5, 5, 5, 11, 9, 5,
97
+ 1, 3, 7
97
98
  ]
98
99
 
99
100
  class << self
@@ -115,24 +116,27 @@ end
115
116
  self.json_float_en_main = 1;
116
117
 
117
118
 
118
- # line 22 "json_float.rl"
119
+ # line 25 "json_float.rl"
120
+
119
121
  super
120
122
  end
121
123
 
122
- def parse_json_float(p, pe)
123
- t_p = p
124
+ def parse
125
+ pe = :ignored
126
+ eof = :ignored
127
+ p = self.p
124
128
 
125
129
 
126
- # line 127 "json_float.rb"
130
+ # line 131 "json_float.rb"
127
131
  begin
128
132
  p ||= 0
129
133
  pe ||= data.length
130
134
  cs = json_float_start
131
135
  end
132
136
 
133
- # line 29 "json_float.rl"
137
+ # line 35 "json_float.rl"
134
138
 
135
- # line 136 "json_float.rb"
139
+ # line 140 "json_float.rb"
136
140
  begin
137
141
  _klen, _trans, _keys, _acts, _nacts = nil
138
142
  _goto_level = 0
@@ -226,12 +230,24 @@ when 0 then
226
230
  when 1 then
227
231
  # line 7 "json_float.rl"
228
232
  begin
229
- o.t :char, [data[p]].pack("c*") end
233
+ y << [:char, [data[p]].pack("c*")] end
230
234
  when 2 then
231
235
  # line 8 "json_float.rl"
232
236
  begin
233
- o.t :number, [data[p]].pack("c*").to_i end
234
- # line 235 "json_float.rb"
237
+ y << [:number, [data[p]].pack("c*").to_i] end
238
+ when 3 then
239
+ # line 9 "json_float.rl"
240
+ begin
241
+ y << :exponent end
242
+ when 4 then
243
+ # line 10 "json_float.rl"
244
+ begin
245
+ y << :minus end
246
+ when 5 then
247
+ # line 11 "json_float.rl"
248
+ begin
249
+ y << :plus end
250
+ # line 251 "json_float.rb"
235
251
  end # action switch
236
252
  end
237
253
  end
@@ -258,10 +274,12 @@ when 2 then
258
274
  end
259
275
  end
260
276
 
261
- # line 30 "json_float.rl"
277
+ # line 36 "json_float.rl"
262
278
 
263
279
  if cs >= json_float_first_final
264
280
  p
281
+ else
282
+ raise_unparseable p
265
283
  end
266
284
  end
267
285
  end