Sha256: b68a788aaa6ce20397467e61ca1d9dc3ae0a02e6b33f23b78cdad2c32ce01ca7

Contents?: true

Size: 1.44 KB

Versions: 25

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require "strscan"

module ActionDispatch
  module Journey # :nodoc:
    class Scanner # :nodoc:
      def initialize
        @ss = nil
      end

      def scan_setup(str)
        @ss = StringScanner.new(str)
      end

      def eos?
        @ss.eos?
      end

      def pos
        @ss.pos
      end

      def pre_match
        @ss.pre_match
      end

      def next_token
        return if @ss.eos?

        until token = scan || @ss.eos?; end
        token
      end

      private

        # takes advantage of String @- deduping capabilities in Ruby 2.5 upwards
        # see: https://bugs.ruby-lang.org/issues/13077
        def dedup_scan(regex)
          r = @ss.scan(regex)
          r ? -r : nil
        end

        def scan
          case
            # /
          when @ss.skip(/\//)
            [:SLASH, "/"]
          when @ss.skip(/\(/)
            [:LPAREN, "("]
          when @ss.skip(/\)/)
            [:RPAREN, ")"]
          when @ss.skip(/\|/)
            [:OR, "|"]
          when @ss.skip(/\./)
            [:DOT, "."]
          when text = dedup_scan(/:\w+/)
            [:SYMBOL, text]
          when text = dedup_scan(/\*\w+/)
            [:STAR, text]
          when text = @ss.scan(/(?:[\w%\-~!$&'*+,;=@]|\\[:()])+/)
            text.tr! "\\", ""
            [:LITERAL, -text]
            # any char
          when text = dedup_scan(/./)
            [:LITERAL, text]
          end
        end
    end
  end
end

Version data entries

25 entries across 23 versions & 5 rubygems

Version Path
actionpack-6.0.2.2 lib/action_dispatch/journey/scanner.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.2.1/lib/action_dispatch/journey/scanner.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.2.1/lib/action_dispatch/journey/scanner.rb
actionpack-6.0.2.1 lib/action_dispatch/journey/scanner.rb
actionpack-6.0.2 lib/action_dispatch/journey/scanner.rb
actionpack-6.0.2.rc2 lib/action_dispatch/journey/scanner.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-6.0.1/lib/action_dispatch/journey/scanner.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/scanner.rb
actionpack-6.0.2.rc1 lib/action_dispatch/journey/scanner.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/scanner.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-6.0.1/lib/action_dispatch/journey/scanner.rb
actionpack-6.0.1 lib/action_dispatch/journey/scanner.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/scanner.rb
actionpack-6.0.1.rc1 lib/action_dispatch/journey/scanner.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/scanner.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/scanner.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/scanner.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/scanner.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/scanner.rb
actionpack-6.0.0 lib/action_dispatch/journey/scanner.rb