Skip to content

Commit 20fd6c5

Browse files
committed
fix context helper
1 parent 17edc84 commit 20fd6c5

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

lib/json_mend/parser.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def parse_string
246246
rstring_delimiter = '”'
247247
when /[\p{L}0-9]/
248248
# Could be a boolean/null, but not if it's an object key.
249-
if ["t", "f", "n"].include?(char.downcase) && current_context != :object_key
249+
if ["t", "f", "n"].include?(char.downcase) && !current_context?(:object_key)
250250
# parse_literal is non-destructive if it fails to match.
251251
value = parse_literal
252252
return value if value != ''
@@ -262,9 +262,9 @@ def parse_string
262262
next_value = peek_char(1)
263263

264264
if (
265-
current_context == :object_key && next_value == ':'
265+
current_context?(:object_key) && next_value == ':'
266266
) || (
267-
current_context == :object_value && [",", "}"].include?(next_value)
267+
current_context?(:object_value) && [",", "}"].include?(next_value)
268268
)
269269
@scanner.getch
270270
return ''
@@ -305,11 +305,11 @@ def parse_string
305305
# --- Main Parsing Loop ---
306306
while !@scanner.eos? && char != rstring_delimiter
307307
if missing_quotes
308-
break if current_context == :object_key && (char == ':' || char.match?(/\s/))
309-
break if current_context == :array && [']', ','].include?(char)
308+
break if current_context?(:object_key) && (char == ':' || char.match?(/\s/))
309+
break if current_context?(:array) && [']', ','].include?(char)
310310
end
311311

312-
if current_context == :object_value && [',', '}'].include?(char) && (string_acc.empty? || string_acc[-1] != rstring_delimiter)
312+
if current_context?(:object_value) && [',', '}'].include?(char) && (string_acc.empty? || string_acc[-1] != rstring_delimiter)
313313
rstring_delimiter_missing = true
314314
# check if this is a case in which the closing comma is NOT missing instead
315315
skip_whitespaces
@@ -427,7 +427,7 @@ def parse_string
427427
end
428428
end
429429
# If we are in object key context and we find a colon, it could be a missing right quote
430-
if (char == ':' && !missing_quotes && current_context == :object_key)
430+
if (char == ':' && !missing_quotes && current_context?(:object_key))
431431
i = skip_to_character(lstring_delimiter, start_idx: 1)
432432
next_c = peek_char(i)
433433
break unless next_c
@@ -450,7 +450,7 @@ def parse_string
450450
if char == rstring_delimiter && string_acc[-1] != '\\'
451451
if doubled_quotes && peek_char(1) == rstring_delimiter
452452
@scanner.getch
453-
elsif missing_quotes && current_context == :object_value
453+
elsif missing_quotes && current_context?(:object_value)
454454
i = 1
455455
next_c = peek_char(i)
456456
while next_c && ![rstring_delimiter, lstring_delimiter].include?(next_c)
@@ -489,7 +489,7 @@ def parse_string
489489
(context_contain?(:array) && [']', ','].include?(next_c)) ||
490490
(
491491
check_comma_in_object_value &&
492-
current_context == :object_value &&
492+
current_context?(:object_value) &&
493493
next_c == ','
494494
)
495495
break
@@ -500,7 +500,7 @@ def parse_string
500500
end
501501

502502
# If we stopped for a comma in object_value context, let's check if find a "} at the end of the string
503-
if next_c == ',' && current_context == :object_value
503+
if next_c == ',' && current_context?(:object_value)
504504
i += 1
505505
i = skip_to_character(rstring_delimiter, start_idx: i)
506506
next_c = peek_char(i)
@@ -518,7 +518,7 @@ def parse_string
518518
# Check if self.index:self.index+i is only whitespaces, break if that's the case
519519
break if (1..i).all? { |j| peek_char(j).to_s.match(/\s/) }
520520

521-
if current_context == :object_value
521+
if current_context?(:object_value)
522522
i = skip_whitespaces_at(start_idx: i + 1)
523523
if peek_char(i) == ','
524524
# So we found a comma, this could be a case of a single quote like "va"lue",
@@ -560,7 +560,7 @@ def parse_string
560560
@scanner.getch # Consume the character
561561
char = peek_char
562562
end
563-
elsif current_context == :array
563+
elsif current_context?(:array)
564564
# So here we can have a few valid cases:
565565
# ["bla bla bla "puppy" bla bla bla "kitty" bla bla"]
566566
# ["value1" value2", "value3"]
@@ -584,7 +584,7 @@ def parse_string
584584
string_acc << char.to_s
585585
@scanner.getch # Consume the character
586586
char = peek_char
587-
elsif current_context == :object_key
587+
elsif current_context?(:object_key)
588588
string_acc << char.to_s
589589
@scanner.getch # Consume the character
590590
char = peek_char
@@ -595,7 +595,7 @@ def parse_string
595595
end
596596
end
597597

598-
if !@scanner.eos? && missing_quotes && current_context == :object_key && char.match(/\s/)
598+
if !@scanner.eos? && missing_quotes && current_context?(:object_key) && char.match(/\s/)
599599
skip_whitespaces
600600
return '' unless [':', ','].include?(peek_char)
601601
end
@@ -619,7 +619,7 @@ def parse_string
619619
# inputs that might be generated by LLMs
620620
def parse_number
621621
allowed_chars = NUMBER_CHARS.dup
622-
allowed_chars.delete(',') if current_context == :array
622+
allowed_chars.delete(',') if current_context?(:array)
623623
regex = /[#{Regexp.escape(allowed_chars.to_a.join)}]+/
624624

625625
scanned_str = @scanner.scan(regex)
@@ -641,7 +641,7 @@ def parse_number
641641
# Use rescue to handle conversion errors gracefully, returning the original string.
642642
begin
643643
if scanned_str.include?(',')
644-
return scanned_str.to_s
644+
scanned_str.to_s
645645
elsif scanned_str.match?(/[\.eE]/)
646646
Float(scanned_str)
647647
else
@@ -698,7 +698,7 @@ def parse_comment
698698
else
699699
# The character at the current position (likely '/') is not the start of a
700700
# valid comment. To prevent an infinite loop in the calling parser, we must
701-
# consume this single stray character before exiting.
701+
# consume this single stray character before exiting
702702
@scanner.getch
703703
end
704704

@@ -796,8 +796,8 @@ def peek_char(offset = 0)
796796
rest_of_string.chars[offset]
797797
end
798798

799-
def current_context
800-
@context&.last
799+
def current_context?(value)
800+
@context&.last == value
801801
end
802802

803803
def context_contain?(value)

0 commit comments

Comments
 (0)