-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgen_shared.rb
More file actions
136 lines (121 loc) · 4.46 KB
/
gen_shared.rb
File metadata and controls
136 lines (121 loc) · 4.46 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class GenShared
class << self
def export_to_function!(haxe)
haxe.gsub!(/^export function/, "public function")
haxe.gsub!(/^function/, "private function")
end
def export_type_to_typedef!(haxe)
# Test this...
# haxe.gsub!(/export type (.*?) = {.*?\n};/m) { |block|
# block.gsub!(/(\w+)\?:(\s+\?)?/, "?\\1 /* opt */ :")
# block.sub!(/^export type/, "typedef /* export type */")
# block
# }
haxe.gsub!(/export type/, "typedef /* export type */")
haxe.gsub!(/(\w+)\?:(\s+\?)?/, "?\\1 /* opt */ :")
end
def backticks!(haxe)
# Backticks to single ticks
haxe.gsub!(/\`.*?\`/) { |match|
cont = match[1,match.length-2];
cont.gsub!('\'', '\\\\'+"'");
cont.gsub!('\\\\$', '$'); # recover actual intentional ${ }
## cont.gsub!('$', '$$');
## cont.gsub!('\\\\$$', '$'); # recover actual intentional ${ }
"'#{ cont }'"
}
#haxe.gsub!(/\`.*?\`/) { |match| cont = match[1,match.length-2]; cont.gsub!("'", "\'"); cont.gsub!('$', '$$'); "'#{ cont }'" }
end
def case_fall_throughs! haxe
# Collapse case fall-throughs
while haxe.match(/(case \w+\.\w+( \|.*?)?):\s*\n\s*case /m)
haxe.gsub!(/(case \w+\.\w+( \|.*?)?):\s*\n\s*case /m, "\\1 | /*CFT*/ ")
end
while haxe.match(/(case ['"]\w+['"]( \|.*?)?):\s*\n\s*case /m)
haxe.gsub!(/(case ['"]\w+['"]( \|.*?)?):\s*\n\s*case /m, "\\1 | /*CFT*/ ")
end
while haxe.match(/(case \d+( \|.*?)?):\s*\n\s*case /m)
haxe.gsub!(/(case \d+( \|.*?)?):\s*\n\s*case /m, "\\1 | /*CFT*/ ")
end
# remove break; statements just before new case statements
haxe.gsub!(/break;(.+?)(case|default)/m) { |match|
obet = $1.dup
between = $1
stmt = $2
if (!between.include?('}')) then
# ignore comments in between
between.gsub!(/\/\/.*?$/, "")
between.gsub!(/\/\*.*?\*\//m, "")
if (between.match(/^\s+$/)) then
match = "/* break; */#{ obet }#{ stmt }"
end
end
match
}
end
def basic_types_and_junk!(haxe)
# Basic types
haxe.gsub!(/:\s*boolean\b/, ":Bool")
haxe.gsub!(/:\s*any\b([^(])/, ":Dynamic\\1")
haxe.gsub!(/:\s*string\b/, ":String")
haxe.gsub!(/:\s*number\b/, ":Int /* number */")
# const let ===
haxe.gsub!(/\bconst\s+/, "var ")
haxe.gsub!(/\blet\s+/, "var ")
haxe.gsub!(/===/, "==")
haxe.gsub!(/!==/, "!=")
end
def func_args_trailing_comma!(haxe)
# Remove newline after last function parameter -- also keeps {
# on the next line, but that's just stylistic
haxe.gsub!(/function (\w+\(.*?),\s+\):(.*?){/m, "function \\1):\\2\n{")
end
def func_calls_trailing_comma!(haxe)
# Haxe doesn't like comma, newline, close paren
haxe.gsub!(/,\s*\n\s*\)\s*/m, ")")
end
def ES_implied_object_keys(haxe)
replace_esiok = lambda { |code|
# ESIOK... ES implied object key name... GRR!
code.gsub!(/{(.*?)}/m) { |block|
if (block[1,block.length-1].include?('{')) then
# inmost { } blocks, please
#puts "================"
#puts "================"
#puts "================ THERE:"
#puts block[1,block.length-1]
#puts "================ NOW:"
block ='{' + replace_esiok.call(block[1,block.length-1])
#puts block
else
looks_like_a_struct = true
#puts "================"
#puts "================"
#puts "================ HERE:"
#puts block
block[1,block.length-2].split("\n").each { |line|
if (line.match(/^\s*$/)) then
elsif (!line.match(/^(\s+)(\w+)\s*.*?,/)) then
looks_like_a_struct = false if !line.match(/^\s*[?:]/)
#puts "LLAS failed on: #{ line }"
end
}
#puts "===== LLAS=#{ looks_like_a_struct }"
if (looks_like_a_struct) then
block = block.split("\n").map { |line|
line.sub(/^(\s+)(\w+)\s*,\s*$/, "\\1\\2:\\2, /* ESIOK: stupid inferred key name */")
}.join("\n")
end
#puts "MOD:"
#puts block
#puts "================"
#puts "================"
end
block
}
code
}
return replace_esiok.call(haxe)
end
end
end