@@ -226,7 +226,7 @@ def state
226226 end
227227
228228 # Tokens where state should be ignored
229- # used for :on_comment, :on_heredoc_end, :on_embexpr_end
229+ # used for :on_sp, : on_comment, :on_heredoc_end, :on_embexpr_end
230230 class IgnoreStateToken < Token
231231 def ==( other ) # :nodoc:
232232 self [ 0 ...-1 ] == other [ 0 ...-1 ]
@@ -611,10 +611,10 @@ def self.build(opening)
611611 BOM_FLUSHED = RUBY_VERSION >= "3.3.0"
612612 private_constant :BOM_FLUSHED
613613
614- attr_reader :source , : options
614+ attr_reader :options
615615
616- def initialize ( source , **options )
617- @source = source
616+ def initialize ( code , **options )
617+ @code = code
618618 @options = options
619619 end
620620
@@ -624,12 +624,13 @@ def result
624624 state = :default
625625 heredoc_stack = [ [ ] ] #: Array[Array[Heredoc::PlainHeredoc | Heredoc::DashHeredoc | Heredoc::DedentingHeredoc]]
626626
627- result = Prism . lex ( source , **options )
627+ result = Prism . lex ( @code , **options )
628+ @source = result . source
628629 result_value = result . value
629630 previous_state = nil #: State?
630631 last_heredoc_end = nil #: Integer?
631632
632- bom = source . byteslice ( 0 ..2 ) == "\xEF \xBB \xBF "
633+ bom = @ source. slice ( 0 ..2 ) == "\xEF \xBB \xBF "
633634
634635 result_value . each_with_index do |( token , lex_state ) , index |
635636 lineno = token . location . start_line
@@ -763,7 +764,7 @@ def result
763764 end_offset += 3
764765 end
765766
766- tokens << Token . new ( [ [ lineno , 0 ] , :on_nl , source . byteslice ( start_offset ...end_offset ) , lex_state ] )
767+ tokens << Token . new ( [ [ lineno , 0 ] , :on_nl , @ source. slice ( start_offset ...end_offset ) , lex_state ] )
767768 end
768769 end
769770
@@ -857,7 +858,39 @@ def result
857858 # We sort by location to compare against Ripper's output
858859 tokens . sort_by! ( &:location )
859860
860- Result . new ( tokens , result . comments , result . magic_comments , result . data_loc , result . errors , result . warnings , Source . for ( source ) )
861+ # Add :on_sp tokens
862+ tokens = add_on_sp_tokens ( tokens )
863+
864+ Result . new ( tokens , result . comments , result . magic_comments , result . data_loc , result . errors , result . warnings , @source )
865+ end
866+
867+ def add_on_sp_tokens ( tokens )
868+ new_tokens = [ ]
869+
870+ prev_token_state = Translation ::Ripper ::Lexer ::State . new ( Translation ::Ripper ::EXPR_BEG )
871+ prev_token_end = 0
872+
873+ tokens . each do |token |
874+ line , column = token . location
875+ start_offset = @source . line_to_byte_offset ( line ) + column
876+ if start_offset > prev_token_end
877+ new_tokens << IgnoreStateToken . new ( [
878+ [
879+ @source . line ( prev_token_end ) ,
880+ @source . column ( prev_token_end ) ,
881+ ] ,
882+ :on_sp ,
883+ @source . slice ( prev_token_end , start_offset - prev_token_end ) ,
884+ prev_token_state
885+ ] )
886+ end
887+ new_tokens << token
888+
889+ prev_token_state = token . state
890+ prev_token_end = start_offset + token . value . bytesize
891+ end
892+
893+ new_tokens
861894 end
862895 end
863896
0 commit comments