Class: DSL

Inherits:
Object
  • Object
show all
Defined in:
tools/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, options) ⇒ DSL

Returns a new instance of DSL.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'tools/dsl.rb', line 14

def initialize(code, options)
  @events = {}
  @error = options.include?("error")
  @brace = options.include?("brace")
  if options.include?("final")
    @final = "p->result"
  else
    @final = (options.grep(/\A\$(?:\$|\d+)\z/)[0] || "$$")
  end
  @vars = 0

  # create $1 == "$1", $2 == "$2", ...
  s = (1..20).map {|n| "$#{n}"}
  re = Array.new(s.size, "([^\0]+)")
  /#{re.join("\0")}/ =~ s.join("\0")

  # struct parser_params *p
  p = p = "p"

  @code = ""
  @last_value = eval(code)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(event, *args) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'tools/dsl.rb', line 74

def method_missing(event, *args)
  if event.to_s =~ /!\z/
    add_event(event, args)
  elsif args.empty? and /\Aid[A-Z_]/ =~ event.to_s
    event
  else
    "#{ event }(#{ args.join(", ") })"
  end
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



37
38
39
# File 'tools/dsl.rb', line 37

def events
  @events
end

Class Method Details

.const_missing(name) ⇒ Object



84
85
86
# File 'tools/dsl.rb', line 84

def self.const_missing(name)
  name
end

Instance Method Details

#add_event(event, args, qundef_check = false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'tools/dsl.rb', line 59

def add_event(event, args, qundef_check = false)
  event = event.to_s.sub(/!\z/, "")
  @events[event] = args.size
  vars = []
  args.each do |arg|
    vars << v = new_var
    @code << "#{ v }=#{ arg };"
  end
  v = new_var
  d = "dispatch#{ args.size }(#{ [event, *vars].join(",") })"
  d = "#{ vars.last }==Qundef ? #{ vars.first } : #{ d }" if qundef_check
  @code << "#{ v }=#{ d };"
  v
end

#generateObject



43
44
45
46
47
48
49
# File 'tools/dsl.rb', line 43

def generate
  s = "#@code#@final=#@last_value;"
  s = "{VALUE #{ (1..@vars).map {|v| "v#{ v }" }.join(",") };#{ s }}" if @vars > 0
  s << "ripper_error(p);" if @error
  s = "{#{ s }}" if @brace
  "\t\t\t#{s}"
end

#new_varObject



51
52
53
# File 'tools/dsl.rb', line 51

def new_var
  "v#{ @vars += 1 }"
end

#opt_event(event, default, addend) ⇒ Object



55
56
57
# File 'tools/dsl.rb', line 55

def opt_event(event, default, addend)
  add_event(event, [default, addend], true)
end