vidarh / writing-a-compiler-in-ruby

Code from my series on writing a Ruby compiler in Ruby

This URL has Read+Write access

writing-a-compiler-in-ruby / scanner.rb
100644 65 lines (56 sloc) 1.332 kb
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
59
60
61
62
63
64
65
# The purpose of the Scanner is to present a narrow interface to read characters from, with support for lookahead / unget.
# Why not StringScanner? Well, it's a Ruby C-extension, and I want to get the compiler self-hosted as soon as possible,
# so I'm sticking to something simple. The code below is sufficient to write recursive descent parsers in a pretty
# concise style in Ruby
class Scanner
  def initialize io
    @io = io
    @buf = ""
  end
 
  def fill
    if @buf.empty?
      c = @io.getc
      c = c.chr if c
      @buf = c ? c.to_s : ""
    end
  end
 
  def peek
    fill
    return @buf[-1]
  end
    
  def get
    fill
    return @buf.slice!(-1,1)
  end
 
  def unget(c)
    c = c.reverse if c.is_a?(String)
    @buf += c
  end
 
  def expect(str)
    return true if str == ""
    return str.expect(self) if str.respond_to?(:expect)
    buf = ""
    str.each_byte do |s|
      c = peek
      if !c || c.to_i != s
        unget(buf)
        return false
      end
      buf += get
    end
    return true
  end
 
  # ws ::= ([\t\b\r ] | '#' [~\n]* '\n')*
  def ws
    while (c = peek) && [9,10,13,32,?#].member?(c) do
      get
      if c == ?#
        while (c = get) && c != "\n" do end
      end
    end
  end
 
  # nolfws ::= [\t\r ]*
  def nolfws
    while (c = peek) && [9,13,32].member?(c) do get; end
  end
end