Class: AutoBind

Inherits:
Object show all
Defined in:
lib/goat/autobind.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inp, out) ⇒ AutoBind

Returns a new instance of AutoBind.



4
5
6
7
# File 'lib/goat/autobind.rb', line 4

def initialize(inp, out)
  @inp = inp
  @out = out
end

Class Method Details

.process(str) ⇒ Object



9
10
11
12
13
# File 'lib/goat/autobind.rb', line 9

def self.process(str)
  out = StringIO.new
  new(StringIO.new(str), out).process
  out.string
end

Instance Method Details

#expand(c) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/goat/autobind.rb', line 46

def expand(c)
  @out.write("(closure(this, function(")
  
  if c == ?(
    while (c2 = @inp.getc) != ?)
      @out.putc c2
    end
    while @inp.getc != ?{
      nil
    end
  end
  
  @out.write("){")
  process
  @out.write("))")
end

#processObject



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
# File 'lib/goat/autobind.rb', line 15

def process
  brace_count = 0
  while char = @inp.getc
    case char
    when ?^
      char2 = @inp.getc
      if char2 == ?{ || char2 == ?(
        expand(char2)
      else
        brace_count += 1
        @out.putc char
        @out.putc char2 if char2
      end
    when ?{
      brace_count += 1
      @out.putc char
    when ?}
      brace_count -= 1
      @out.putc char
      if brace_count < 0
        return
      end
    when ?", ?'
      @out.putc char
      string(char)
    else
      @out.putc char
    end
  end
end

#string(delim) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/goat/autobind.rb', line 63

def string(delim)
  while char = @inp.getc
    case char
    when ?\\
      char2 = @inp.getc
      @out.putc char
      @out.putc char2 if char2
    when delim
      @out.putc char
      return
    else
      @out.putc char
    end
  end
end