@@ -597,33 +597,49 @@ def parse_literal
597597 # After skipping the comment, it either continues parsing if at the top level
598598 # or returns an empty string to be ignored by the caller.
599599 def parse_comment
600+ char = @scanner . peek ( 1 )
601+
600602 # Determine valid line comment termination characters based on the current context.
601603 termination_chars = [ "\n " , "\r " ]
602604 termination_chars << ']' if @context . include? ( :array )
603605 termination_chars << '}' if @context . include? ( :object_value )
604606 termination_chars << ':' if @context . include? ( :object_key )
605- line_comment_matcher = Regexp . new ( "[#{ Regexp . escape ( termination_chars . join ) } ]" )
606607
607608 # Line comment starting with #
608- if @scanner . skip ( /#/ )
609- # Skip until the next terminator or the end of the string.
610- found_terminator = @scanner . skip_until ( line_comment_matcher )
611- @scanner . terminate unless found_terminator
609+ if char == "#"
610+ comment = +""
611+ while !@scanner . eos? && !termination_chars . include? ( char )
612+ comment << char
613+ @scanner . pos += 1
614+ char = @scanner . peek ( 1 )
615+ end
612616 # Comments starting with /
613- elsif @scanner . check ( %r{/} )
617+ elsif char == "/"
618+ next_char = @scanner . string [ @scanner . pos + 1 ]
614619 # Handle line comment starting with //
615- if @scanner . skip ( %r{//} )
616- found_terminator = @scanner . skip_until ( line_comment_matcher )
617- @scanner . terminate unless found_terminator
618-
620+ if next_char == "/"
621+ comment = +"//"
622+ @scanner . pos += 2
623+ char = @scanner . peek ( 1 )
624+ while !@scanner . eos? && !termination_chars . include? ( char )
625+ comment << char
626+ @scanner . pos += 1
627+ char = @scanner . peek ( 1 )
628+ end
619629 # Handle block comment starting with /*
620- elsif @scanner . skip ( %r{/\* } )
621- found_terminator = @scanner . skip_until ( %r{\* /} )
622- @scanner . terminate unless found_terminator
630+ elsif next_char == "*"
631+ comment = "/*"
632+ @scanner . pos += 2
633+ loop do
634+ break if @scanner . eos?
623635
624- # Handle standalone '/' characters that are not part of a comment.
636+ char = @scanner . peek ( 1 )
637+ comment += char
638+ @scanner . pos += 1
639+ break if comment . end_with? ( "*/" )
640+ end
625641 else
626- @scanner . pos += 1 # Skip it to avoid an infinite loop.
642+ @scanner . pos += 1
627643 end
628644 end
629645
0 commit comments