File: scanner.rb

package info (click to toggle)
ruby-journey 1.0.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 288 kB
  • sloc: ruby: 2,830; javascript: 113; yacc: 42; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 995 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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