-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathperl2js
executable file
·197 lines (136 loc) · 3.6 KB
/
perl2js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/perl
## rules of thumb
# no regex should contain a .
# most should be non-greedy
undef $/;
$source = join ('', <>);
## swap over functions with params
$source =~ s/
(sub\s+) # 1 sub
(\w+?) # 2 function
(\s*{\s*) # 3 {
(my\s*) # 4 my
(\() # 5 (
([\$\w\s\,]*?) # 6 $args Can be $ letters or ,
(\)) # 7 )
(\s*=\s*\@_;) # 8 = @_;
/function $2($6)$3/gmsx;
## swap over functions with no params
$source =~ s/
(sub\s) # 1 sub
(\w+?) # 2 function
(\s*{\s*) # 3 {
/function $2()$3/gmsx;
## swap my for var
$source =~ s/
(my) # 1 my
(\s+?) # 2
/var$2/gmsx;
## swap my for var
$source =~ s/
(\n\s*) # 1
(print) # 2 print
(.+?) # 3 something
(;) # 4;
/$1document.write($3 )$4/gmsx;
# swap out some escaped chars with unicode equivalents to keep it safe and easy for the other regex's
$source =~ s/\\\"/\\u0022/gmsx;
$source =~ s/\\\$/\\u0024/gmsx;
## swap variables inside " strings
sub replace_vars {
($in) = @_;
$in =~ s/
(\$) # 1 $
(\w+) # 2 var
(\W|$) # 3 stuff or end of line
/\"+$1$2+\"$3/gmsx;
return "\"$in\"";
}
$source =~ s/
(") # 1 "
([^"]*) # 2 stuff that ain't a "
(") # 3 "
/replace_vars($2)/egmsx;
## Get rid of empty string literals
$source =~ s/
(\+) # 1 +
(") # 2 "
(") # 3 "
//egmsx;
$source =~ s/
(") # 1 "
(") # 2 "
(\+) # 3 +
//egmsx;
## replace static join function with String join method
$source =~ s/
(join) # 1 join
(\s+) # 2
( # 3 the delimiter
('[^']*') # 4 a ' string
| # or
("[^"]*") # 5 a " string
| # or
(\$\w+) # 6 variable
)
(,) # 7 ,
(\s+) # 8 whitespace
( # 9 an array expression
(@\w+) # an array variable
)
# (;) # 10 ;,
/$9.join($3)/gmsx;
## convert @array literals
$source =~ s/
(=) # 1 =
(\s*) # 2
(\() # 3 (
(.*?) # 4 values
(\)) # 5 )
/$1$2\[$4\]/gmsx;
## convert foreach loop
$source =~ s/
(foreach) # 1 foreach
(\s+) # 2 whitespace
(var) # 3 var
(\s+) # 4 whitespace
(\$\w+) # 5 var
(\s*) # 6 whitespace
(\() # 7 (
(.*?) # 8 array expression
(\)\s*\{) # 9 ) {
/for $7$3$4_$5 in $8$9var $5 = $8\[_$5\];/gmsx;
## Template regex
#$source =~ s/
# />>>>>>>>>>>>>>>> 1:$1 | 2:$2 | 3:$3 | 4:$4 | 5:$5 | 6:$6 <<<<</gmsx;
#print $source;
#exit;
## get rid of $ on the front of vars
$source =~ s/
(\$) # 1 $
(\w+) # 2 var
(\W|$) # 3 stuff or end of line
/$2$3/gmsx;
## get rid of @ on the front of arrays
$source =~ s/
(\@) # 1 @
(\w+) # 2 var
(\W|$) # 3 stuff or end of line
/$2$3/gmsx;
## replace comments
$source =~ s/
(\n) # 1 new line
([^\n]*) # 2 some optional code
(\#) # 3 #
([^\n]*) # 4 the comment
(?!\n) # 5 end of line or file (zero width assertion)
/$1$2\/\/$4$5/gmsx;
# swap join
# swap substring
# swap regex?
# swap out some escaped chars with unicode equivalents to keep it safe and easy for the other regex's
$source =~ s/\\u0022/\\"/gmsx;
$source =~ s/\\u0024/\\\$/gmsx;
# remove trailing 1;
$source =~ s/1;$//g;
print $source;