-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bsc
executable file
·174 lines (157 loc) · 3.21 KB
/
bsc
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
#!/usr/bin/ruby
# TODO test Ruby < 1.9
$input = ARGF.read.freeze
$i = 0
######################################################################## utils
def putc
Kernel.putc(c)
ensure
$i += 1
end
def prev
j = $i - 1;
$input[j..j]
end
def peek
j = $i + 1;
$input[j..j]
end
def whitespace_reverse
begin
$i -= 1
end while c =~ /\s/
end
def whitespace_until_EOL #TODO is for loop more concise?
j = $i + 1
while j < $input.length
return true if $input[j..j] == "\n"
return false unless $input[j..j] =~ /\s/
j += 1
end
return true # no more file left counts as EOL too!
end
def c
exit if $i >= $input.length
$input[$i..$i]
end
def seek s
s.upcase!
while $i < $input.length - s.length
return true if $input[$i..$i+s.length-1].upcase == s
$i += 1
end
$i = $input.length - 1
false
end
def seek_bounded_html
j = $i
n = 1
begin $i +=1 end while c =~ /[a-zA-Z]/
tag = $input[j+1..$i-1].upcase
if tag == 'H' && c =~ /[0-9]/
tag = tag + c
$i +=1
end
loop do
found = case tag
when 'IMG'
seek '>'
break
else
seek tag
end
break unless found
if $input[$i-2..$i+tag.length].upcase == "</#{tag}>"
n -= 1
if n == 0
$i += tag.length
break
end
elsif $input[$i-1..$i+tag.length].upcase =~ /<#{tag}[ >]/
n += 1
end
$i += 1
end
end
def seek_html
return unless peek =~ /[a-zA-Z]/
loop do
seek_bounded_html
j = $i
begin
j += 1
end while $input[j..j] =~ /\s/
return true unless $input[j..j+1] =~ /<[a-zA-Z]/
$i = j
end
end
################################################################### core logic
def tokenize
while $i < $input.length
case c
when '/'
putc
while putc != "\n"; end if putc == '/'
while putc != '*' and peek != '/'; end if putc == '*'
next
when "'"
putc
while putc != "'" and prev != '\\'; end
next
when '"'
j = $i
$i += 1
if c == '"' and peek == '"'
$i += 2
begin seek('"""') end while prev == '\\'
$i += 2
yield :multi, $input[j..$i]
else
$i += 1 until c == '"' and prev != '\\'
yield :string, $input[j..$i]
end
when '<'
j = $i
if seek_html
yield :html, $input[j..$i]
else
print c
end
else
print c
end
$i += 1
end
end
class String
def bsubst sep = "'"
split(/(#[nNxX]?\{.*?\})/).map do |part|
if part =~ /#([nNxX]?)\{(.*?)\}/
subst = if $1 and $1.length > 0
"bs.#$1(#$2)"
else
"(#$2)"
end
%Q{#{sep} + #{subst} + #{sep}}
else
part.gsub(sep, %{\\\\#{sep}})
end
end.join
end
end
#################################################################### main loop
tokenize do |type, string, opts|
case type
when :string
print %{"#{string[1..-2].bsubst('"')}"}
when :multi
string = string[3..-4] # remove surrounding triple quotes
string = string.bsubst
string = string.gsub(/\s*\n(\s*)/m, %Q{\\n'+\n\\1'})
print %Q{'#{string}'}
when :html
string = string.bsubst
string.gsub!(/\s*\n(\s*)/m, %Q{ '+\n\\1'})
print "$('#{string}')"
end
end