jdogo 3.0.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.
@@ -0,0 +1,710 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strscan"
4
+
5
+ module JDogo
6
+ # The document reader. It works on byte offsets: every structural character is ASCII, so tokens
7
+ # always start and end on character boundaries.
8
+ #
9
+ # @api private
10
+ class Decoder
11
+ ABSENT = Shared::ABSENT
12
+
13
+ COMMA = 44
14
+ NL = 10
15
+ CR = 13
16
+ SP = 32
17
+ TAB = 9
18
+ QUOTE = 34
19
+ BACKSLASH = 92
20
+ LBRACKET = 91
21
+ RBRACKET = 93
22
+ LBRACE = 123
23
+ RBRACE = 125
24
+ LPAREN = 40
25
+ RPAREN = 41
26
+ COLON = 58
27
+ SEMI = 59
28
+ TILDE = 126
29
+ CARET = 94
30
+ AT = 64
31
+ EQ = 61
32
+ QMARK = 63
33
+ PLUS = 43
34
+ DOLLAR = 36
35
+
36
+ TOKEN = /[^,\n\r\[\]{}:;]*/
37
+ TOKEN_WRAP = /[^,\n\r\[\]{}:;()]*/
38
+ VALUE_TOKEN = /[^,\n\r\]};]*/
39
+ VALUE_TOKEN_WRAP = /[^,\n\r\]};)]*/
40
+ SPEC_NAME = /[^{\[?=@+^$,}\n\r]*/
41
+ PREFIX_TEXT = /[^+,}\n\r;$]*/
42
+ SUFFIX_TEXT = /[^+,}\n\r;^]*/
43
+ KEY_TEXT = /[^:]*/
44
+ QUOTE_STOP = /["\\]/
45
+ HEX_BYTES = "0123456789abcdefABCDEF".bytes.freeze
46
+ DIGITS = /\A[0-9]+\z/
47
+ ESCAPES = { QUOTE => '"', BACKSLASH => "\\", 47 => "/", 98 => "\b", 102 => "\f", 110 => "\n",
48
+ 114 => "\r", 116 => "\t" }.freeze
49
+
50
+ # decode reads a document (any JSON text is one). Empty or whitespace-only text reads as nil.
51
+ def self.decode(text, max_depth, max_collection)
52
+ text = Text.valid_text(text)
53
+ text = text.byteslice(3, text.bytesize - 3) if text.start_with?("\uFEFF") # not part of the document
54
+ s = text.end_with?("\n", "\r") ? text.sub(/[\r\n]+\z/, "") : text
55
+ return nil if s.match?(/\A[ \t\r\n]*\z/)
56
+
57
+ new(s, max_depth, max_collection).document
58
+ end
59
+
60
+ def initialize(s, max_depth, max_collection)
61
+ @s = s
62
+ @n = s.bytesize
63
+ @p = 0
64
+ @ss = StringScanner.new(s)
65
+ @depth = 0
66
+ @max_depth = max_depth
67
+ @max_collection = max_collection
68
+ end
69
+
70
+ # ---------------------------------------------------------------- cursor
71
+
72
+ def eof? = @p >= @n
73
+ def peek = @s.getbyte(@p)
74
+ def at?(ch) = @s.getbyte(@p) == ch
75
+
76
+ def fail!(msg)
77
+ raise ParseError.new(@p, msg)
78
+ end
79
+
80
+ def expect(ch)
81
+ @p += 1 if ch == NL && @s.getbyte(@p) == CR && @s.getbyte(@p + 1) == NL # Windows line endings
82
+ fail!("expected '#{ch.chr}'") unless at?(ch)
83
+ @p += 1
84
+ end
85
+
86
+ def enter
87
+ @depth += 1
88
+ return if @depth <= @max_depth
89
+
90
+ raise LimitError, "Maximum deserialization depth exceeded: #{@depth} > #{@max_depth}"
91
+ end
92
+
93
+ def leave
94
+ @depth -= 1
95
+ end
96
+
97
+ def check_size(n)
98
+ raise LimitError, "Maximum collection size exceeded" if n >= @max_collection
99
+ end
100
+
101
+ # skip_ws skips spaces and tabs, and line breaks too when newlines (inside brackets), as JSON does.
102
+ def skip_ws(newlines)
103
+ while @p < @n
104
+ ch = @s.getbyte(@p)
105
+ break unless ch == SP || ch == TAB || (newlines && (ch == NL || ch == CR))
106
+
107
+ @p += 1
108
+ end
109
+ end
110
+
111
+ # scan_span moves past the longest run matching re (a negated class) and returns its start.
112
+ def scan_span(re)
113
+ start = @p
114
+ @ss.pos = @p
115
+ @p += @ss.skip(re)
116
+ start
117
+ end
118
+
119
+ # trimmed returns the text from start to the cursor without trailing spaces and tabs.
120
+ def trimmed(start)
121
+ stop = @p
122
+ stop -= 1 while stop > start && ((c = @s.getbyte(stop - 1)) == SP || c == TAB)
123
+ @s.byteslice(start, stop - start)
124
+ end
125
+
126
+ # ---------------------------------------------------------------- tokens
127
+
128
+ def parse_quoted
129
+ expect(QUOTE)
130
+ out = +""
131
+ loop do
132
+ @ss.pos = @p
133
+ len = @ss.skip_until(QUOTE_STOP)
134
+ unless len
135
+ @p = @n
136
+ fail!("unterminated quoted string")
137
+ end
138
+ out << @s.byteslice(@p, len - 1) if len > 1 # raw control characters are allowed, as in Java
139
+ @p += len
140
+ return out if @s.getbyte(@p - 1) == QUOTE
141
+
142
+ fail!("unterminated string escape") if eof?
143
+ esc = @s.getbyte(@p)
144
+ @p += 1
145
+ if esc == 117 # 'u'
146
+ out << unicode_escape
147
+ elsif (r = ESCAPES[esc])
148
+ out << r
149
+ else
150
+ fail!("invalid escape \\#{esc < 128 ? esc.chr : format('x%02x', esc)}") # only JSON escapes
151
+ end
152
+ end
153
+ end
154
+
155
+ # unicode_escape reads the 4 hex digits after '\u', joining a surrogate pair. A lone surrogate,
156
+ # which valid UTF-8 cannot hold, becomes U+FFFD.
157
+ def unicode_escape
158
+ r = hex4
159
+ if r >= 0xD800 && r < 0xDC00 && @s.getbyte(@p) == BACKSLASH && @s.getbyte(@p + 1) == 117
160
+ save = @p
161
+ @p += 2
162
+ lo = hex4
163
+ return (0x10000 + ((r - 0xD800) << 10) + (lo - 0xDC00)).chr(Text::UTF8) if lo >= 0xDC00 && lo <= 0xDFFF
164
+
165
+ @p = save
166
+ end
167
+ return Text::REPLACEMENT if r >= 0xD800 && r <= 0xDFFF
168
+
169
+ r.chr(Text::UTF8)
170
+ end
171
+
172
+ def hex4
173
+ fail!("invalid unicode escape") unless @p + 4 <= @n && (0..3).all? { |i| HEX_BYTES.include?(@s.getbyte(@p + i)) }
174
+ @p += 4
175
+ @s.byteslice(@p - 4, 4).to_i(16)
176
+ end
177
+
178
+ # parse_token reads up to a structural character (a dictionary index or difference cell).
179
+ def parse_token(in_wrap) = trimmed(scan_span(in_wrap ? TOKEN_WRAP : TOKEN))
180
+
181
+ # parse_value_token reads a bare value: it runs to the next ',', ']', '}', ';' or line end (and ')'
182
+ # inside a wrap), as in Java; ':' and brackets inside it are text (timestamps, URLs).
183
+ def parse_value_token(in_wrap) = trimmed(scan_span(in_wrap ? VALUE_TOKEN_WRAP : VALUE_TOKEN))
184
+
185
+ def ends_value?(ch, in_wrap)
186
+ ch == COMMA || ch == NL || ch == CR || ch == RBRACKET || ch == RBRACE || ch == SEMI ||
187
+ (in_wrap && ch == RPAREN)
188
+ end
189
+
190
+ # ---------------------------------------------------------------- inline values
191
+
192
+ # parse_value reads one value with the whitespace around it skipped (line breaks too when newlines).
193
+ def parse_value(in_wrap, newlines)
194
+ skip_ws(newlines)
195
+ v = parse_value_raw(in_wrap)
196
+ skip_ws(newlines) unless ABSENT.equal?(v)
197
+ v
198
+ end
199
+
200
+ def parse_value_raw(in_wrap)
201
+ return ABSENT if eof? || ends_value?(peek, in_wrap)
202
+
203
+ case peek
204
+ when QUOTE then parse_quoted
205
+ when LBRACKET then parse_list(in_wrap)
206
+ when LBRACE then parse_object(in_wrap)
207
+ else scalar(parse_value_token(in_wrap))
208
+ end
209
+ end
210
+
211
+ def parse_list(in_wrap)
212
+ enter
213
+ @p += 1
214
+ list = []
215
+ skip_ws(true)
216
+ while !eof? && peek != RBRACKET
217
+ check_size(list.size)
218
+ v = parse_value_raw(in_wrap)
219
+ fail!("empty array element") if ABSENT.equal?(v) # as Java: never invent a null
220
+ list << v
221
+ skip_ws(true)
222
+ break unless at?(SEMI) || at?(COMMA)
223
+
224
+ @p += 1
225
+ skip_ws(true)
226
+ fail!("empty array element") if at?(RBRACKET) # a trailing comma, as Java rejects it
227
+ end
228
+ expect(RBRACKET)
229
+ leave
230
+ list
231
+ end
232
+
233
+ def parse_object(in_wrap)
234
+ enter
235
+ @p += 1
236
+ obj = {}
237
+ skip_ws(true)
238
+ while !eof? && peek != RBRACE
239
+ if peek == QUOTE
240
+ key = parse_quoted
241
+ skip_ws(true)
242
+ else
243
+ start = scan_span(KEY_TEXT) # as Java: an unquoted key runs to the ':'
244
+ stop = @p
245
+ stop -= 1 while stop > start && [SP, TAB, CR, NL].include?(@s.getbyte(stop - 1))
246
+ key = @s.byteslice(start, stop - start)
247
+ end
248
+ expect(COLON)
249
+ skip_ws(true)
250
+ v = parse_value_raw(in_wrap)
251
+ fail!("empty object value") if ABSENT.equal?(v) # as Java: never invent a null
252
+ obj[key] = v
253
+ skip_ws(true)
254
+ break unless at?(COMMA)
255
+
256
+ @p += 1
257
+ skip_ws(true)
258
+ fail!("trailing comma in object") if at?(RBRACE)
259
+ end
260
+ expect(RBRACE)
261
+ leave
262
+ obj
263
+ end
264
+
265
+ # scalar reads a bare token: '~' and 'null' are null, 'true' and 'false' booleans, a number (spec
266
+ # 2.1 rule 5) an Integer or Float, and anything else text.
267
+ def scalar(tok)
268
+ case tok
269
+ when "~", "null" then nil
270
+ when "true" then true
271
+ when "false" then false
272
+ else Text.looks_numeric?(tok) ? number(tok) : tok
273
+ end
274
+ end
275
+
276
+ def number(tok)
277
+ return Integer(tok, 10) if Num::INTEGER_TOKEN.match?(tok)
278
+
279
+ # too large for a 64-bit float: an error, not Infinity (spec 2.1 rule 5.5)
280
+ Num.parse_float(tok) || fail!("number out of range: #{tok[0, 40]}")
281
+ rescue ArgumentError
282
+ fail!("invalid number: #{tok[0, 40]}")
283
+ end
284
+
285
+ # ---------------------------------------------------------------- the header
286
+
287
+ def parse_specs
288
+ enter
289
+ fields = []
290
+ loop do
291
+ fields << parse_spec
292
+ break unless at?(COMMA)
293
+
294
+ @p += 1
295
+ end
296
+ leave
297
+ fields
298
+ end
299
+
300
+ def parse_spec
301
+ skip_ws(false)
302
+ if at?(QUOTE)
303
+ name = parse_quoted
304
+ skip_ws(false)
305
+ else
306
+ name = trimmed(scan_span(SPEC_NAME))
307
+ fail!("expected a field name") if name.empty? # an empty name is written as ""
308
+ end
309
+ f = Field.new(name)
310
+ return f if eof?
311
+
312
+ case peek
313
+ when LBRACE
314
+ @p += 1
315
+ f.kind = :obj
316
+ f.sub = parse_specs
317
+ expect(RBRACE)
318
+ when QMARK
319
+ @p += 1
320
+ expect(LBRACE)
321
+ f.kind = :obj
322
+ f.wrapped = true
323
+ f.sub = parse_specs
324
+ expect(RBRACE)
325
+ when LBRACKET
326
+ @p += 1
327
+ expect(RBRACKET)
328
+ if at?(QMARK)
329
+ @p += 1
330
+ f.wrapped = true
331
+ end
332
+ expect(LBRACE)
333
+ f.kind = :arr
334
+ f.sub = parse_specs
335
+ expect(RBRACE)
336
+ when EQ
337
+ @p += 1
338
+ v = parse_value(false, false)
339
+ fail!("empty constant") if ABSENT.equal?(v)
340
+ f.kind = :const
341
+ f.const_val = v
342
+ when AT
343
+ @p += 1
344
+ f.kind = :dict
345
+ f.dict = []
346
+ loop do
347
+ # Read like Java's readRaw: up to , ] } ; or the line end; ':' and brackets are text.
348
+ f.dict << (at?(QUOTE) ? parse_quoted : parse_value_token(false))
349
+ break unless at?(SEMI)
350
+
351
+ @p += 1
352
+ end
353
+ when CARET, DOLLAR, PLUS
354
+ parse_modifiers(f)
355
+ end
356
+ f
357
+ end
358
+
359
+ def modifier?(ch) = ch == CARET || ch == DOLLAR || ch == PLUS
360
+
361
+ # parse_modifiers reads ^prefix, $suffix and + (differences) in any order, or '+t' / '+t3' alone.
362
+ def parse_modifiers(f)
363
+ while !eof? && modifier?(peek)
364
+ mod = peek
365
+ @p += 1
366
+ if mod == PLUS
367
+ if at?(116) # 't'
368
+ fail!("'+t' cannot follow a prefix or suffix") if f.kind == :affix
369
+ @p += 1
370
+ f.time_unit = TimeColumn::SECONDS
371
+ if at?(51) # '3'
372
+ @p += 1
373
+ f.time_unit = TimeColumn::MILLIS
374
+ end
375
+ f.kind = :time
376
+ f.delta = true
377
+ fail!("'+t' takes no other modifier") if !eof? && modifier?(peek)
378
+ return
379
+ end
380
+ f.kind = :delta unless f.kind == :affix
381
+ f.delta = true
382
+ next
383
+ end
384
+ f.kind = :affix
385
+ stop = mod == CARET ? PREFIX_TEXT : SUFFIX_TEXT
386
+ val = at?(QUOTE) ? parse_quoted : trimmed(scan_span(stop))
387
+ if mod == CARET
388
+ f.prefix = val
389
+ else
390
+ f.suffix = val
391
+ end
392
+ end
393
+ end
394
+
395
+ # ---------------------------------------------------------------- rows
396
+
397
+ def at_cell_end?
398
+ ch = @s.getbyte(@p)
399
+ ch.nil? || ch == COMMA || ch == NL || ch == CR
400
+ end
401
+
402
+ # parse_fields reads one row's cells into target; started says whether a cell came before on this
403
+ # line. It returns the new started.
404
+ def parse_fields(fields, target, started, in_wrap)
405
+ fields.each do |f|
406
+ if f.kind == :const
407
+ target[f.name] = copy_value(f.const_val) # each row gets its own copy of a list or object
408
+ next
409
+ end
410
+ if f.kind == :obj && !f.wrapped
411
+ # An unwrapped object writes no separator of its own: its fields are cells of this row.
412
+ sub = {}
413
+ started = parse_fields(f.sub, sub, started, in_wrap)
414
+ target[f.name] = sub
415
+ next
416
+ end
417
+ if started
418
+ skip_ws(false)
419
+ expect(COMMA) # every cell after the first needs its ',', so a cut-off row is an error
420
+ end
421
+ started = true
422
+ skip_ws(false)
423
+ case f.kind
424
+ when :obj
425
+ next if at_cell_end?
426
+
427
+ if peek == TILDE
428
+ @p += 1
429
+ target[f.name] = nil
430
+ elsif peek == LPAREN
431
+ @p += 1
432
+ sub = {}
433
+ parse_fields(f.sub, sub, false, true)
434
+ expect(RPAREN)
435
+ target[f.name] = sub
436
+ end
437
+ when :arr
438
+ next if at_cell_end?
439
+
440
+ if peek == TILDE
441
+ @p += 1
442
+ target[f.name] = nil
443
+ elsif peek == LBRACKET
444
+ @p += 1
445
+ target[f.name] = parse_items(f, in_wrap)
446
+ end
447
+ else
448
+ # An empty cell (field absent) ends at ',' or the line, or at ';' / ']' inside a list item and
449
+ # ')' inside a wrap.
450
+ next if at_cell_end? || peek == SEMI || peek == RBRACKET || (in_wrap && peek == RPAREN)
451
+
452
+ v = parse_leaf(f, in_wrap)
453
+ target[f.name] = v unless ABSENT.equal?(v)
454
+ end
455
+ end
456
+ skip_ws(false)
457
+ started
458
+ end
459
+
460
+ # parse_items reads a list-of-objects cell after its '[': '[]' is empty; otherwise one item per ';'.
461
+ # A wrapped item is '(...)' or '~'; an unwrapped item may be empty.
462
+ def parse_items(f, in_wrap)
463
+ list = []
464
+ if at?(RBRACKET)
465
+ @p += 1
466
+ return list
467
+ end
468
+ loop do
469
+ check_size(list.size)
470
+ if f.wrapped && at?(TILDE)
471
+ @p += 1
472
+ list << nil
473
+ elsif f.wrapped
474
+ expect(LPAREN)
475
+ sub = {}
476
+ parse_fields(f.sub, sub, false, true)
477
+ expect(RPAREN)
478
+ list << sub
479
+ else
480
+ sub = {}
481
+ parse_fields(f.sub, sub, false, in_wrap)
482
+ list << sub
483
+ end
484
+ if at?(SEMI)
485
+ @p += 1
486
+ next
487
+ end
488
+ expect(RBRACKET)
489
+ return list
490
+ end
491
+ end
492
+
493
+ DITTO_END = [COMMA, RBRACKET, RBRACE, SEMI, NL, CR].freeze
494
+
495
+ def parse_leaf(f, in_wrap)
496
+ case f.kind
497
+ when :dict
498
+ tok = parse_token(in_wrap)
499
+ idx = DIGITS.match?(tok) ? tok.to_i : nil
500
+ fail!("dictionary index out of range: #{tok.inspect}") if idx.nil? || idx >= f.dict.size # an error, as in Java
501
+ return f.dict[idx]
502
+ when :affix
503
+ if at?(TILDE)
504
+ @p += 1
505
+ return nil
506
+ end
507
+ return "#{f.prefix}#{running_value(f, in_wrap)}#{f.suffix}" if f.delta
508
+
509
+ mid = at?(QUOTE) ? parse_quoted : parse_value_token(in_wrap)
510
+ return f.prefix + mid + f.suffix
511
+ when :delta
512
+ return running_value(f, in_wrap)
513
+ when :time
514
+ cur = delta_sum(f, in_wrap)
515
+ text = TimeColumn.format(f.time_unit, cur) || fail!("timestamp out of range")
516
+ f.prev = cur
517
+ f.has_prev = true
518
+ return text
519
+ end
520
+ if !in_wrap && peek == CARET
521
+ q = @p + 1
522
+ q += 1 while q < @n && ((c = @s.getbyte(q)) == SP || c == TAB)
523
+ if q >= @n || DITTO_END.include?(@s.getbyte(q))
524
+ @p += 1
525
+ return copy_value(f.prev_val)
526
+ end
527
+ end
528
+ v = parse_value_raw(in_wrap) # parse_fields has skipped the spaces before the cell
529
+ unless ABSENT.equal?(v)
530
+ f.prev_val = v
531
+ skip_ws(false)
532
+ end
533
+ v
534
+ end
535
+
536
+ # running_value reads a difference cell and returns the new running value.
537
+ def running_value(f, in_wrap)
538
+ cur = delta_sum(f, in_wrap)
539
+ f.prev = cur
540
+ f.has_prev = true
541
+ cur
542
+ end
543
+
544
+ # delta_sum reads a difference ('-'? and ASCII digits, as Java reads it) and adds it to the running
545
+ # value; outside 64 bits is an error.
546
+ def delta_sum(f, in_wrap)
547
+ tok = parse_token(in_wrap)
548
+ fail!("expected integer, got #{tok.inspect}") unless Num::INTEGER_TOKEN.match?(tok)
549
+ diff = tok.to_i
550
+ fail!("integer out of range") unless Num.int64?(diff)
551
+ return diff unless f.has_prev
552
+
553
+ cur = f.prev + diff
554
+ fail!("integer out of range") unless Num.int64?(cur)
555
+ cur
556
+ end
557
+
558
+ # copy_value copies lists and objects, so rows never share a constant or ditto value.
559
+ def copy_value(v)
560
+ case v
561
+ when Array then v.map { |e| copy_value(e) }
562
+ when Hash then v.transform_values { |e| copy_value(e) }
563
+ else v
564
+ end
565
+ end
566
+
567
+ # ---------------------------------------------------------------- record lines ('@:')
568
+
569
+ def read_hybrid_scalar(dense_cell)
570
+ fail!("missing value") if eof?
571
+ ch = peek
572
+ return parse_quoted if ch == QUOTE
573
+ # spec 2.3.1 rule 7.1: the space after the bracket separates the next field, so it is not skipped
574
+ return parse_value_raw(false) if !dense_cell && (ch == LBRACE || ch == LBRACKET)
575
+
576
+ start = @p
577
+ while @p < @n
578
+ x = @s.getbyte(@p)
579
+ break if x == SP || x == NL || (dense_cell && x == COMMA)
580
+
581
+ @p += 1
582
+ end
583
+ tok = @s.byteslice(start, @p - start)
584
+ case tok
585
+ when "" then fail!("empty value")
586
+ when "~" then nil
587
+ when "true" then true
588
+ when "false" then false
589
+ else ReadableEncoder::NUMBER_TOKEN.match?(tok) ? number(tok) : tok
590
+ end
591
+ end
592
+
593
+ def decode_hybrid
594
+ @p = 2
595
+ dense = []
596
+ while !eof? && peek != NL
597
+ start = @p
598
+ @p += 1 while @p < @n && (c = @s.getbyte(@p)) != COMMA && c != NL
599
+ dense << @s.byteslice(start, @p - start)
600
+ @p += 1 if at?(COMMA)
601
+ end
602
+ rows = []
603
+ until eof?
604
+ expect(NL)
605
+ check_size(rows.size)
606
+ row = {}
607
+ dense.each_with_index do |name, i|
608
+ expect(COMMA) if i.positive?
609
+ row[name] = read_hybrid_scalar(true)
610
+ end
611
+ first = dense.empty?
612
+ while !eof? && peek != NL
613
+ expect(SP) unless first
614
+ first = false
615
+ start = @p
616
+ @p += 1 while @p < @n && (c = @s.getbyte(@p)) != EQ && c != SP && c != NL
617
+ key = @s.byteslice(start, @p - start)
618
+ fail!("empty field name") if key.empty?
619
+ expect(EQ)
620
+ row[key] = read_hybrid_scalar(false)
621
+ end
622
+ rows << row
623
+ end
624
+ rows
625
+ end
626
+
627
+ # ---------------------------------------------------------------- documents
628
+
629
+ def document
630
+ unless header?
631
+ v = parse_value(false, true)
632
+ fail!("empty value") if ABSENT.equal?(v)
633
+ fail!("unexpected trailing data") unless eof?
634
+ return v
635
+ end
636
+ @p = 1
637
+ if @s.byteslice(1, 1) == ":"
638
+ decode_hybrid
639
+ elsif @s.byteslice(1, 2) == "[]"
640
+ decode_matrix
641
+ elsif @s.byteslice(1, 1) == "="
642
+ @p = 2
643
+ fields = parse_specs
644
+ fail!("a single-object document ('@=') needs its row") if eof? # '{}' is written inline
645
+ expect(NL)
646
+ obj = {}
647
+ parse_fields(fields, obj, false, false)
648
+ fail!("unexpected trailing data") unless eof? # nothing may follow the one row
649
+ obj
650
+ elsif @s.byteslice(1, 2) == "{}"
651
+ @p = 3
652
+ fields = parse_specs
653
+ out = {}
654
+ until eof?
655
+ expect(NL)
656
+ check_size(out.size)
657
+ skip_ws(false)
658
+ key = at?(QUOTE) ? parse_quoted : parse_value_token(false) # like Java readRaw: ':' is text
659
+ row = {}
660
+ parse_fields(fields, row, true, false)
661
+ out[key] = row
662
+ end
663
+ out
664
+ else
665
+ fields = parse_specs
666
+ rows = []
667
+ until eof?
668
+ expect(NL)
669
+ check_size(rows.size)
670
+ row = {}
671
+ parse_fields(fields, row, false, false)
672
+ rows << row
673
+ end
674
+ rows
675
+ end
676
+ end
677
+
678
+ def decode_matrix
679
+ @p = 3
680
+ rows = []
681
+ until eof?
682
+ expect(NL)
683
+ check_size(rows.size)
684
+ row = []
685
+ loop do
686
+ v = parse_value(false, false)
687
+ fail!("empty matrix cell") if ABSENT.equal?(v)
688
+ row << v
689
+ break unless at?(COMMA)
690
+
691
+ @p += 1
692
+ end
693
+ rows << row
694
+ end
695
+ rows
696
+ end
697
+
698
+ # header?: whether the first character that is not whitespace is '@'.
699
+ def header?
700
+ i = 0
701
+ while i < @n
702
+ c = @s.getbyte(i)
703
+ return c == AT unless c == SP || c == TAB || c == NL || c == CR
704
+
705
+ i += 1
706
+ end
707
+ false
708
+ end
709
+ end
710
+ end