Sha256: 07dbd9fea28622020cb6ab37e9d70b1797d3698ebd29d23bc45ff15760d87ab4

Contents?: true

Size: 992 Bytes

Versions: 7

Compression:

Stored size: 992 Bytes

Contents

require 'strscan'

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(/\*/)
        [: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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
andyjeffries-journey-1.0.0.20111022124133 lib/journey/scanner.rb
andyjeffries-journey-1.0.0 lib/journey/scanner.rb
journey-1.0.0 lib/journey/scanner.rb
journey-1.0.0.rc4 lib/journey/scanner.rb
journey-1.0.0.rc3 lib/journey/scanner.rb
journey-1.0.0.rc2 lib/journey/scanner.rb
journey-1.0.0.rc1 lib/journey/scanner.rb