Module: SmarterJSON::Recovery
- Includes:
- Bytes
- Defined in:
- lib/smarter_json/parser.rb
Constant Summary
Constants included
from Bytes
Bytes::BACKSLASH, Bytes::COLON, Bytes::COMMA, Bytes::CR, Bytes::DOLLAR, Bytes::DOT, Bytes::DQUOTE, Bytes::HASH, Bytes::LBRACE, Bytes::LBRACKET, Bytes::LF, Bytes::LOWER_E, Bytes::LOWER_F, Bytes::LOWER_N, Bytes::LOWER_T, Bytes::LOWER_U, Bytes::LOWER_X, Bytes::MINUS, Bytes::NINE, Bytes::PLUS, Bytes::RBRACE, Bytes::RBRACKET, Bytes::SLASH, Bytes::SPACE, Bytes::SQUOTE, Bytes::STAR, Bytes::TAB, Bytes::UNDERSCORE, Bytes::UPPER_E, Bytes::UPPER_F, Bytes::UPPER_I, Bytes::UPPER_N, Bytes::UPPER_T, Bytes::UPPER_X, Bytes::ZERO
Class Method Summary
collapse
-
.candidate_ranges(input) ⇒ Object
-
.emit_wrapper_warnings(payloads, handler) ⇒ Object
-
.extract_payloads(input, options) ⇒ Object
-
.leading_label?(input) ⇒ Boolean
Whether the input opens with a bare “JSON:” / “Final answer:” label (which would otherwise parse, wrongly, as an implicit-root object keyed by the label).
-
.line_col_for(input, offset) ⇒ Object
-
.non_payload_text(input, ranges) ⇒ Object
-
.process_string(input, options, &block) ⇒ Object
-
.replay_payloads(payloads, options, &block) ⇒ Object
-
.substantive_text?(text) ⇒ Boolean
-
.warn(handler, type, message, line, col) ⇒ Object
-
.wrapper_meta(input, ranges) ⇒ Object
Class Method Details
.candidate_ranges(input) ⇒ Object
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
# File 'lib/smarter_json/parser.rb', line 570
def candidate_ranges(input)
ranges = []
stack = []
start_pos = nil
i = 0
mode = nil
while i < input.bytesize
b = input.getbyte(i)
if mode == :double
if b == BACKSLASH
i += 2
next
elsif b == DQUOTE
mode = nil
end
i += 1
next
elsif mode == :single
if b == BACKSLASH
i += 2
next
elsif b == SQUOTE
mode = nil
end
i += 1
next
elsif mode == :triple
if input.byteslice(i, 3) == "'''"
mode = nil
i += 3
else
i += 1
end
next
elsif mode == :line_comment
if [LF, CR].include?(b)
mode = nil
else
i += 1
next
end
elsif mode == :block_comment
if input.byteslice(i, 2) == "*/"
mode = nil
i += 2
else
i += 1
end
next
else
if input.byteslice(i, 2) == "//"
mode = :line_comment
i += 2
next
elsif input.byteslice(i, 2) == "/*"
mode = :block_comment
i += 2
next
elsif b == HASH
mode = :line_comment
i += 1
next
elsif b == DQUOTE
mode = :double
i += 1
next
elsif input.byteslice(i, 3) == "'''"
mode = :triple
i += 3
next
elsif b == SQUOTE
mode = :single
i += 1
next
elsif [LBRACE, LBRACKET].include?(b)
start_pos = i if stack.empty?
stack << b
elsif b == RBRACE
stack.pop if stack.last == LBRACE
if stack.empty? && start_pos
ranges << (start_pos...(i + 1))
start_pos = nil
end
elsif b == RBRACKET
stack.pop if stack.last == LBRACKET
if stack.empty? && start_pos
ranges << (start_pos...(i + 1))
start_pos = nil
end
end
end
i += 1
end
ranges
end
|
.emit_wrapper_warnings(payloads, handler) ⇒ Object
475
476
477
478
479
480
481
482
483
|
# File 'lib/smarter_json/parser.rb', line 475
def emit_wrapper_warnings(payloads, handler)
return unless handler
meta = payloads.first[:meta]
warn(handler, :prefix_text_ignored, "ignored non-JSON text before the payload", *meta[:first_pos]) if meta[:prefix]
warn(handler, :code_fence_stripped, "stripped markdown code fences around the payload", *meta[:first_pos]) if meta[:fence]
warn(handler, :wrapper_tag_stripped, "stripped wrapper tags around the payload", *meta[:first_pos]) if meta[:wrapper]
warn(handler, :suffix_text_ignored, "ignored non-JSON text after the payload", *meta[:last_pos]) if meta[:suffix]
end
|
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
# File 'lib/smarter_json/parser.rb', line 485
def (input, options)
payloads = candidate_ranges(input).filter_map do |range|
slice = input.byteslice(range.begin, range.end - range.begin)
begin
SmarterJSON.send(:process_content, slice, options.merge(on_warning: nil))
{ slice: slice, range: range }
rescue ParseError
nil
end
end
meta = wrapper_meta(input, payloads.map { |p| p[:range] })
payloads.each { |payload| payload[:meta] = meta }
payloads
end
|
.leading_label?(input) ⇒ Boolean
Whether the input opens with a bare “JSON:” / “Final answer:” label (which would otherwise parse, wrongly, as an implicit-root object keyed by the label). We use String#start_with? with a Regexp rather than match?(/A…/): start_with? checks only the beginning, whereas a A-anchored match? still retries at every byte position and so scans the WHOLE input (≈0.3s on a 200 MB document) on every parse. (Caller has already established the input is valid_encoding?.)
448
449
450
|
# File 'lib/smarter_json/parser.rb', line 448
def leading_label?(input)
input.start_with?(/[[:space:]]*(?:JSON|Final answer)[[:space:]]*:/i)
end
|
.line_col_for(input, offset) ⇒ Object
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
# File 'lib/smarter_json/parser.rb', line 532
def line_col_for(input, offset)
line = 1
col = 1
i = 0
while i < offset
b = input.getbyte(i)
break if b.nil?
if b == LF
line += 1
col = 1
i += 1
elsif b == CR
line += 1
col = 1
i += 1
i += 1 if i < offset && input.getbyte(i) == LF
else
col += 1
i += 1
end
end
[line, col]
end
|
.non_payload_text(input, ranges) ⇒ Object
521
522
523
524
525
526
527
528
529
530
|
# File 'lib/smarter_json/parser.rb', line 521
def non_payload_text(input, ranges)
out = +""
pos = 0
ranges.each do |range|
out << input.byteslice(pos, range.begin - pos) if range.begin > pos
pos = range.end
end
out << input.byteslice(pos, input.bytesize - pos) if pos < input.bytesize
out
end
|
.process_string(input, options, &block) ⇒ Object
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# File 'lib/smarter_json/parser.rb', line 413
def process_string(input, options, &block)
return SmarterJSON.send(:process_content, input, options, &block) unless input.valid_encoding?
if leading_label?(input)
payloads = (input, options)
return replay_payloads(payloads, options, &block) unless payloads.empty?
end
SmarterJSON.send(:process_content, input, options, &block)
rescue ParseError => e
raise if e.is_a?(EncodingError)
payloads = (input, options)
return replay_payloads(payloads, options, &block) unless payloads.empty?
raise
end
|
.replay_payloads(payloads, options, &block) ⇒ Object
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
# File 'lib/smarter_json/parser.rb', line 452
def replay_payloads(payloads, options, &block)
handler = options[:on_warning]
emit_wrapper_warnings(payloads, handler)
if block_given?
count = 0
payloads.each do |payload|
SmarterJSON.send(:process_content, payload[:slice], options) do |doc|
block.call(doc)
count += 1
end
end
return count
end
payloads.flat_map do |payload|
SmarterJSON.send(:process_content, payload[:slice], options)
end
end
|
.substantive_text?(text) ⇒ Boolean
557
558
559
560
561
562
563
564
|
# File 'lib/smarter_json/parser.rb', line 557
def substantive_text?(text)
return false if text.nil? || text.empty?
stripped = text.dup
stripped.gsub!(%r{/\*.*?\*/}m, "")
stripped.gsub!(/^\s*(?:#|\/\/).*$/, "")
!stripped.strip.empty? && !stripped.strip.match?(/\A(?:```[a-zA-Z0-9_-]*)?\z/) && !stripped.strip.match?(/\A(?:<\/?json>|BEGIN_JSON|END_JSON)\z/i)
end
|
.warn(handler, type, message, line, col) ⇒ Object
566
567
568
|
# File 'lib/smarter_json/parser.rb', line 566
def warn(handler, type, message, line, col)
handler.call(Warning.new(type, message, line, col))
end
|
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
# File 'lib/smarter_json/parser.rb', line 500
def wrapper_meta(input, ranges)
return { prefix: false, suffix: false, fence: false, wrapper: false } if ranges.empty?
first = ranges.first
last = ranges.last
prefix = input.byteslice(0, first.begin)
suffix = input.byteslice(last.end, input.bytesize - last.end)
outside = non_payload_text(input, ranges)
{
prefix: substantive_text?(prefix),
suffix: substantive_text?(suffix),
fence: outside.include?("```"),
wrapper: outside.match?(/<json\b|BEGIN_JSON\b/i),
first_pos: line_col_for(input, first.begin),
last_pos: line_col_for(input, last.begin)
}
end
|