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
Andrew White 56fee39c39 Integrate Journey into Action Dispatch
Move the Journey code underneath the ActionDispatch namespace so
that we don't pollute the global namespace with names that may
be used for models.

Fixes rails/journey#49.
2012-12-19 22:13:08 +00:00

60 lines
1.1 KiB
Ruby

require 'strscan'
module ActionDispatch
module Journey
class Scanner
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
def scan
case
# /
when text = @ss.scan(/\//)
[:SLASH, text]
when text = @ss.scan(/\*\w+/)
[:STAR, text]
when text = @ss.scan(/\(/)
[:LPAREN, text]
when text = @ss.scan(/\)/)
[:RPAREN, text]
when text = @ss.scan(/\|/)
[:OR, text]
when text = @ss.scan(/\./)
[:DOT, text]
when text = @ss.scan(/:\w+/)
[:SYMBOL, text]
when text = @ss.scan(/[\w%\-~]+/)
[:LITERAL, text]
# any char
when text = @ss.scan(/./)
[:LITERAL, text]
end
end
end
end
end