1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/action_dispatch/journey/scanner.rb
Ryuta Kamizono c81af6ae72 Enable Layout/EmptyLinesAroundAccessModifier cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00

70 lines
1.4 KiB
Ruby

# 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