summaryrefslogtreecommitdiff
path: root/ext/ripper/tools/dsl.rb
blob: 6b4742f17c75282aa851ff16225b17af852b0c43 (plain)
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
# Simple DSL implementation for Ripper code generation
#
# input: /*% ripper: stmts_add(stmts_new, void_stmt) %*/
# output: $$ = dispatch2(stmts_add, dispatch0(stmts_new), dispatch0(void_stmt))

class DSL
  def initialize(code, options)
    @events = {}
    @error = options.include?("error")
    @brace = options.include?("brace")
    @final = options.include?("final")

    # create $1 == "$1", $2 == "$2", ...
    re, s = "", ""
    1.upto(9) do |n|
      re << "(..)"
      s << "$#{ n }"
    end
    /#{ re }/ =~ s

    @code = eval(code)
  end

  attr_reader :events

  undef lambda
  undef hash
  undef class

  def generate
    s = "$$"
    s = "p->result" if @final
    s = "\t\t\t#{ s } = #@code;"
    s << "ripper_error(p);" if @error
    s = "{#{ s }}" if @brace
    s
  end

  def method_missing(event, *args)
    if event.to_s =~ /!\z/
      event = $`
      @events[event] = args.size
      "dispatch#{ args.size }(#{ [event, *args].join(", ") })"
    else
      "#{ event }(#{ args.join(", ") })"
    end
  end
end