-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess.rb
50 lines (43 loc) · 1.33 KB
/
preprocess.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
def getTab line
return line.chars.take_while { |c| c == ' ' or c == "\t" }.join
end
def isEmpty line
return (line.strip.start_with?('#', '//') or line.chars.all? { |c| c == ' ' or c == "\t" or c == "\n"})
end
File.open(ARGV[0], "r") do |file|
tabStack = [""]
emptyLines = []
while not file.eof? do
line = file.readline
line.slice! /\ufeff+/
while line.end_with? "\\\n" do
line = line[0..-3] + file.readline
end
if isEmpty line then
emptyLines << line
else
currentTab = getTab line
if currentTab.length > tabStack.last.length then
puts tabStack.last + '{'
tabStack << currentTab
elsif currentTab.length < tabStack.last.length then
while tabStack.last.length > currentTab.length do
tabStack.pop
puts tabStack.last + '}'
end
end
emptyLines.each do |line| puts line end
emptyLines = []
puts line
end
end
currentTab = ""
if tabStack.length > 0 then
while tabStack.last.length > currentTab.length do
tabStack.pop
puts tabStack.last + '}'
end
end
emptyLines.each do |line| puts line end
emptyLines = 0
end