Class: DSL
- Inherits:
-
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.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'tools/dsl.rb', line 11
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
s = (1..20).map {|n| "$#{n}"}
re = Array.new(s.size, "([^\0]+)")
/#{re.join("\0")}/ =~ s.join("\0")
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
71
72
73
74
75
76
77
78
79
|
# File 'tools/dsl.rb', line 71
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
#events ⇒ Object
Returns the value of attribute events
34
35
36
|
# File 'tools/dsl.rb', line 34
def events
@events
end
|
Class Method Details
.const_missing(name) ⇒ Object
81
82
83
|
# File 'tools/dsl.rb', line 81
def self.const_missing(name)
name
end
|
Instance Method Details
#add_event(event, args, qundef_check = false) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'tools/dsl.rb', line 56
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
|
#generate ⇒ Object
40
41
42
43
44
45
46
|
# File 'tools/dsl.rb', line 40
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_var ⇒ Object
48
49
50
|
# File 'tools/dsl.rb', line 48
def new_var
"v#{ @vars += 1 }"
end
|
#opt_event(event, default, addend) ⇒ Object
52
53
54
|
# File 'tools/dsl.rb', line 52
def opt_event(event, default, addend)
add_event(event, [default, addend], true)
end
|