-
Notifications
You must be signed in to change notification settings - Fork 2
/
scripts.rb
81 lines (71 loc) · 1.97 KB
/
scripts.rb
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Script
def initialize(code)
@code = code
do_conversions
end
def Script.tabs(line)
line.match(%r{(\s*)})[1]
end
def to_s
@code.collect(&:to_s)
end
end
class MethodCall
REGEX = %r{.+\(.*\)}
def initialize(line)
start = line.index("(")
i = start + 1
p = 1
while p > 0 and i < line.length
if line[i] == "("
p += 1
elsif line[i] == ")"
p -= 1
end
i += 1
end
stop = i
j = start
while j > 0 and not line[j].match(/\s/)
j -= 1
end
precall = line[j + 1...start].split(".")
if precall.length > 1
@caller = precall[0...precall.size - 1].join(".")
@method = precall.last
else
@method = precall.last
end
@args = line[start + 1...stop - 1].split(%r{\s*,\s*})
reduce_args
@tabs = line[0..j]
@remainder = line[stop...line.length]
if @remainder.match(REGEX)
@remainder = MethodCall.new(@remainder)
end
do_conversions
end
def reduce_args
@args.each.with_index do |arg, i|
if arg.include?("(") and not arg.include?(")")
j = i + 1
999.times do
if @args[j].include?(")")
break
else
j += 1
end
end
@args[i] = @args[i..j].join(", ")
(j - i).times do
@args.delete_at(i + 1)
end
reduce_args
return
end
end
end
def to_s
@override or @tabs + (@caller ? "#{@caller}.#{@method}" : @method.to_s) + "(" + @args&.join(", ").to_s + ")" + @remainder.to_s
end
end