-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathserializer.rb
410 lines (352 loc) · 10.2 KB
/
serializer.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
module Ruby2JS
class Token < String
attr_accessor :loc
attr_accessor :ast
def initialize(string, ast)
super(string.to_s)
@ast = ast
@loc = ast.location if ast
end
end
class Line < Array
attr_accessor :indent
def initialize(*args)
super(args)
@indent = 0
end
def comment?
first = find {|token| !token.empty?}
first and first.start_with? '//'
end
def empty?
all? {|line| line.empty?}
end
def to_s
if empty?
''
elsif ['case ', 'default:'].include? self[0]
' ' * ([0,indent-2].max) + join
elsif indent > 0
' ' * indent + join
else
join
end
end
end
class Serializer
attr_reader :timestamps
attr_accessor :file_name
def initialize
@sep = '; '
@nl = ''
@ws = ' '
@width = 80
@indent = 0
@lines = [Line.new]
@line = @lines.last
@timestamps = {}
@ast = nil
@file_name = ''
end
def timestamp(file)
if file
@timestamps[file] = File.mtime(file) if File.exist?(file)
end
end
def uptodate?
return false if @timestamps.empty?
return @timestamps.all? {|file, mtime| File.mtime(file) == mtime}
end
def mtime
return Time.now if @timestamps.empty?
return @timestamps.values.max
end
def enable_vertical_whitespace
@sep = ";\n"
@nl = "\n"
@ws = @nl
@indent = 2
end
# indent multi-line parameter lists, array constants, blocks
def reindent(lines)
indent = 0
lines.each do |line|
first = line.find {|token| !token.empty?}
if first
last = line[line.rindex {|token| !token.empty?}]
if (first.start_with? '<' and line.include? '>') or
(last.end_with? '>' and line.include? '<')
then
node = line.join[/.*?(<.*)/, 1]
indent -= @indent if node.start_with? '</'
line.indent = indent
node = line.join[/.*(<.*)/, 1]
indent += @indent unless node.include? '</' or node.include? '/>'
else
indent -= @indent if ')}]'.include? first[0] and indent >= @indent
line.indent = indent
indent += @indent if '({['.include? last[-1]
end
else
line.indent = indent
end
end
end
# add horizontal (indentation) and vertical (blank lines) whitespace
def respace
return if @indent == 0
reindent @lines
(@lines.length-3).downto(0) do |i|
if \
@lines[i].length == 0
then
@lines.delete i
elsif \
@lines[i+1].comment? and not @lines[i].comment? and
@lines[i].indent == @lines[i+1].indent
then
# before a comment
@lines.insert i+1, Line.new
elsif \
@lines[i].indent == @lines[i+1].indent and
@lines[i+1].indent < @lines[i+2].indent and
not @lines[i].comment?
then
# start of indented block
@lines.insert i+1, Line.new
elsif \
@lines[i].indent > @lines[i+1].indent and
@lines[i+1].indent == @lines[i+2].indent and
not @lines[i+2].empty?
then
# end of indented block
@lines.insert i+2, Line.new
end
end
end
# add a single token to the current line
def put(string)
unless String === string and string.include? "\n"
@line << Token.new(string, @ast)
else
parts = string.split("\n")
first = parts.shift
@line << Token.new(first, @ast) if first
@lines += parts.map {|part| Line.new(Token.new(part, @ast))}
@lines << Line.new if string.end_with?("\n")
@line = @lines.last
end
end
# add a single token to the current line without checking for newline
def put!(string)
@line << Token.new(string.gsub("\r", "\n"), @ast)
end
# add a single token to the current line and then advance to next line
def puts(string)
unless String === string and string.include? "\n"
@line << Token.new(string, @ast)
else
put string
end
@line = Line.new
@lines << @line
end
# advance to next line and then add a single token to the current line
def sput(string)
unless String === string and string.include? "\n"
@line = Line.new(Token.new(string, @ast))
@lines << @line
else
@line = Line.new
@lines << @line
put string
end
end
# current location: [line number, token number]
def output_location
[@lines.length-1, @line.length]
end
# insert a line into the output
def insert(mark, line)
if mark.last == 0
@lines.insert(mark.first, Line.new(Token.new(line.chomp, @ast)))
else
@lines[mark.first].insert(mark.last, Token.new(line, @ast))
end
end
# capture (and remove) tokens from the output stream
def capture(&block)
mark = output_location
block.call
lines = @lines.slice!(mark.first+1..-1)
@line = @lines.last
if lines.empty?
lines = [@line.slice!(mark.last..-1)]
elsif @line.length != mark.last
lines.unshift @line.slice!(mark.last..-1)
end
lines.map(&:join).join(@ws)
end
# wrap long statements in curly braces
def wrap(open = '{', close = '}')
puts open
mark = output_location
yield
if \
@lines.length > mark.first+1 or
@lines[mark.first-1].join.length + @line.join.length >= @width
then
sput close
else
@line = @lines[mark.first-1]
@line[-1..-1] = @lines.pop
end
end
# compact small expressions into a single line
def compact
mark = output_location
yield
return unless @lines.length - mark.first > 1
return if @indent == 0
# survey what we have to work with, keeping track of a possible
# split of the last argument or value
work = []
len = 0
trail = split = nil
slice = @lines[mark.first..-1]
reindent(slice)
slice.each_with_index do |line, index|
line << "" if line.empty?
if line.first.start_with? '//'
len += @width # comments are a deal breaker
else
(work.push ' '; len += 1) if trail == line.indent and @indent > 0
len += line.map(&:length).inject(&:+)
work += line
if trail == @indent and line.indent == @indent
split = [len, work.length, index]
break if len >= @width - 10
end
trail = line.indent
end
end
if len < @width - 10
# full collapse
@lines[mark.first..-1] = [Line.new(*work)]
@line = @lines.last
elsif split and split[0] < @width-10
if slice[split[2]].indent < slice[split[2]+1].indent
# collapse all but the last argument (typically a hash or function)
close = slice.pop
slice[-1].push(*close)
@lines[mark.first] = Line.new(*work[0..split[1]-1])
@lines[mark.first+1..-1] = slice[split[2]+1..-1]
@line = @lines.last
end
end
end
# return the output as a string
def to_s
return @str if (@str ||= nil)
respace
@lines.map(&:to_s).join(@nl)
end
def to_str
@str ||= to_s
end
def +(value)
to_s+value
end
BASE64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
# https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
# http://sokra.github.io/source-map-visualization/
def vlq(*mark)
if !@mark
diffs = mark
@mark = [0, 0, 0, 0, 0, 0]
else
if @mark[0] == mark[0]
return if @mark[4] == mark[4] and @mark[3] == mark[3]
@mappings += ',' unless @mappings == ''
end
diffs = mark.zip(@mark).map {|a,b| a-b}
end
while @mark[0] < mark[0]
@mappings += ';'
@mark[0] += 1
diffs[1] = mark[1]
end
@mark[0...mark.length] = mark
diffs[1..-1].each do |diff|
if diff < 0
data = (-diff << 1) + 1
else
data = diff << 1
end
if data <= 0b11111
# workaround https://github.com/opal/opal/issues/575
encoded = BASE64[data]
else
encoded = ''
begin
digit = data & 0b11111
data >>= 5
digit |= 0b100000 if data > 0
encoded += BASE64[digit]
end while data > 0
end
@mappings += encoded
end
end
def sourcemap
respace
@mappings = ''
sources = []
names = []
@mark = nil
@lines.each_with_index do |line, row|
col = line.indent
line.each do |token|
if token.respond_to? :loc and token.loc
pos = token.loc.expression.begin_pos
buffer = token.loc.expression.source_buffer
source_index = sources.index(buffer)
if not source_index
source_index = sources.length
timestamp buffer.name
sources << buffer
end
line = buffer.line_for_position(pos) - 1
column = buffer.column_for_position(pos)
name = nil
if %i{lvasgn lvar}.include? token.ast.type
name = token.ast.children.first
elsif %i{casgn const}.include? token.ast.type
if token.ast.children.first == nil
name = token.ast.children[1]
end
end
if name
index = names.find_index(name)
unless index
index = names.length
names << name
end
vlq row, col, source_index, line, column, index
else
vlq row, col, source_index, line, column
end
end
col += token.length
end
end
@sourcemap = {
version: 3,
file: @file_name,
sources: sources.map(&:name),
names: names.map(&:to_s),
mappings: @mappings
}
end
end
end