22# :markup: markdown
33
44require "delegate"
5- require "ripper"
65
76module Prism
87 # This class is responsible for lexing the source using prism and then
@@ -249,8 +248,8 @@ def ==(other) # :nodoc:
249248 class IdentToken < Token
250249 def ==( other ) # :nodoc:
251250 ( self [ 0 ...-1 ] == other [ 0 ...-1 ] ) && (
252- ( other [ 3 ] == Ripper ::EXPR_LABEL | Ripper ::EXPR_END ) ||
253- ( other [ 3 ] & Ripper ::EXPR_ARG_ANY != 0 )
251+ ( other [ 3 ] == Translation :: Ripper ::EXPR_LABEL | Translation :: Ripper ::EXPR_END ) ||
252+ ( other [ 3 ] & ( Translation :: Ripper ::EXPR_ARG | Translation :: Ripper :: EXPR_CMDARG ) != 0 )
254253 )
255254 end
256255 end
@@ -261,8 +260,8 @@ class IgnoredNewlineToken < Token
261260 def ==( other ) # :nodoc:
262261 return false unless self [ 0 ...-1 ] == other [ 0 ...-1 ]
263262
264- if self [ 3 ] == Ripper ::EXPR_ARG | Ripper ::EXPR_LABELED
265- other [ 3 ] & Ripper ::EXPR_ARG | Ripper ::EXPR_LABELED != 0
263+ if self [ 3 ] == Translation :: Ripper ::EXPR_ARG | Translation :: Ripper ::EXPR_LABELED
264+ other [ 3 ] & Translation :: Ripper ::EXPR_ARG | Translation :: Ripper ::EXPR_LABELED != 0
266265 else
267266 self [ 3 ] == other [ 3 ]
268267 end
@@ -280,8 +279,8 @@ def ==(other) # :nodoc:
280279 class ParamToken < Token
281280 def ==( other ) # :nodoc:
282281 ( self [ 0 ...-1 ] == other [ 0 ...-1 ] ) && (
283- ( other [ 3 ] == Ripper ::EXPR_END ) ||
284- ( other [ 3 ] == Ripper ::EXPR_END | Ripper ::EXPR_LABEL )
282+ ( other [ 3 ] == Translation :: Ripper ::EXPR_END ) ||
283+ ( other [ 3 ] == Translation :: Ripper ::EXPR_END | Translation :: Ripper ::EXPR_LABEL )
285284 )
286285 end
287286 end
@@ -615,6 +614,11 @@ def self.build(opening)
615614
616615 private_constant :Heredoc
617616
617+ # In previous versions of Ruby, Ripper wouldn't flush the bom before the
618+ # first token, so we had to have a hack in place to account for that.
619+ BOM_FLUSHED = RUBY_VERSION >= "3.3.0"
620+ private_constant :BOM_FLUSHED
621+
618622 attr_reader :source , :options
619623
620624 def initialize ( source , **options )
@@ -630,13 +634,9 @@ def result
630634
631635 result = Prism . lex ( source , **options )
632636 result_value = result . value
633- previous_state = nil #: Ripper::Lexer:: State?
637+ previous_state = nil #: State?
634638 last_heredoc_end = nil #: Integer?
635639
636- # In previous versions of Ruby, Ripper wouldn't flush the bom before the
637- # first token, so we had to have a hack in place to account for that. This
638- # checks for that behavior.
639- bom_flushed = Ripper . lex ( "\xEF \xBB \xBF # test" ) [ 0 ] [ 0 ] [ 1 ] == 0
640640 bom = source . byteslice ( 0 ..2 ) == "\xEF \xBB \xBF "
641641
642642 result_value . each_with_index do |( token , lex_state ) , index |
@@ -651,7 +651,7 @@ def result
651651 if bom && lineno == 1
652652 column -= 3
653653
654- if index == 0 && column == 0 && !bom_flushed
654+ if index == 0 && column == 0 && !BOM_FLUSHED
655655 flushed =
656656 case token . type
657657 when :BACK_REFERENCE , :INSTANCE_VARIABLE , :CLASS_VARIABLE ,
@@ -675,7 +675,7 @@ def result
675675
676676 event = RIPPER . fetch ( token . type )
677677 value = token . value
678- lex_state = Ripper ::Lexer ::State . new ( lex_state )
678+ lex_state = Translation :: Ripper ::Lexer ::State . new ( lex_state )
679679
680680 token =
681681 case event
@@ -689,7 +689,7 @@ def result
689689 last_heredoc_end = token . location . end_offset
690690 IgnoreStateToken . new ( [ [ lineno , column ] , event , value , lex_state ] )
691691 when :on_ident
692- if lex_state == Ripper ::EXPR_END
692+ if lex_state == Translation :: Ripper ::EXPR_END
693693 # If we have an identifier that follows a method name like:
694694 #
695695 # def foo bar
@@ -699,7 +699,7 @@ def result
699699 # yet. We do this more accurately, so we need to allow comparing
700700 # against both END and END|LABEL.
701701 ParamToken . new ( [ [ lineno , column ] , event , value , lex_state ] )
702- elsif lex_state == Ripper ::EXPR_END | Ripper ::EXPR_LABEL
702+ elsif lex_state == Translation :: Ripper ::EXPR_END | Translation :: Ripper ::EXPR_LABEL
703703 # In the event that we're comparing identifiers, we're going to
704704 # allow a little divergence. Ripper doesn't account for local
705705 # variables introduced through named captures in regexes, and we
@@ -739,7 +739,7 @@ def result
739739 counter += { on_embexpr_beg : -1 , on_embexpr_end : 1 } [ current_event ] || 0
740740 end
741741
742- Ripper ::Lexer ::State . new ( result_value [ current_index ] [ 1 ] )
742+ Translation :: Ripper ::Lexer ::State . new ( result_value [ current_index ] [ 1 ] )
743743 else
744744 previous_state
745745 end
@@ -867,62 +867,4 @@ def result
867867 end
868868
869869 private_constant :LexCompat
870-
871- # This is a class that wraps the Ripper lexer to produce almost exactly the
872- # same tokens.
873- class LexRipper # :nodoc:
874- attr_reader :source
875-
876- def initialize ( source )
877- @source = source
878- end
879-
880- def result
881- previous = [ ] #: [[Integer, Integer], Symbol, String, untyped] | []
882- results = [ ] #: Array[[[Integer, Integer], Symbol, String, untyped]]
883-
884- lex ( source ) . each do |token |
885- case token [ 1 ]
886- when :on_sp
887- # skip
888- when :on_tstring_content
889- if previous [ 1 ] == :on_tstring_content && ( token [ 2 ] . start_with? ( "\# $" ) || token [ 2 ] . start_with? ( "\# @" ) )
890- previous [ 2 ] << token [ 2 ]
891- else
892- results << token
893- previous = token
894- end
895- when :on_words_sep
896- if previous [ 1 ] == :on_words_sep
897- previous [ 2 ] << token [ 2 ]
898- else
899- results << token
900- previous = token
901- end
902- else
903- results << token
904- previous = token
905- end
906- end
907-
908- results
909- end
910-
911- private
912-
913- if Ripper . method ( :lex ) . parameters . assoc ( :keyrest )
914- def lex ( source )
915- Ripper . lex ( source , raise_errors : true )
916- end
917- else
918- def lex ( source )
919- ripper = Ripper ::Lexer . new ( source )
920- ripper . lex . tap do |result |
921- raise SyntaxError , ripper . errors . map ( &:message ) . join ( ' ;' ) if ripper . errors . any?
922- end
923- end
924- end
925- end
926-
927- private_constant :LexRipper
928870end
0 commit comments