forked from w7cook/AoPL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
includes.rb
99 lines (89 loc) · 2.25 KB
/
includes.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
# this program computes the start/end line of latex includes for
# references to any files
# usage:
# Example 1 - Read File and close
def scan(fileName, content, location)
file = File.new(fileName, "r")
$stderr << "Scanning '#{fileName}'\n"
active = {}
file.each do |line|
hasBeginOrEnd = false
line.scan(/BEGIN\:([a-zA-Z][a-zA-Z0-9]*)/).flatten.each do |name|
if content[name] || active[name]
$stderr << "Symbol '#{name}' multiply defined\n"
else
# $stderr << " Found '#{name}'\n"
end
active[name] = []
location[name] = fileName
hasBeginOrEnd = true
end
line.scan(/END\:([a-zA-Z][a-zA-Z0-9]*)/).flatten.each do |name|
content[name] = active[name]
active.delete(name)
hasBeginOrEnd = true
end
if !hasBeginOrEnd
active.values.each do |list|
list << line
end
end
end
active.keys.each do |name|
$stderr << "Missing close for #{name}\n"
end
file.close
end
def process(input, output, marker, content, location)
included = false
name = ""
unused = content.keys
sectionFiles = []
section = ""
input.each do |line|
if line =~ /^ *\#+ /
if sectionFiles.size > 0
# $stderr << "#{section}: #{sectionFiles}\n"
sectionFiles = []
end
section = line.strip
end
if line =~ /INCLUDE\:([a-zA-Z][a-zA-Z0-9]*)/
name = $1
output.write(line)
included = true
if content[name]
sectionFiles << location[name] if !sectionFiles.include? location[name]
unused.delete(name)
content[name].each do |out|
output.write("#{marker} #{out}")
end
included = true
else
$stderr << "NO CONTENT for #{name}\n"
end
elsif included && line =~ /^#{marker}/
# skip this line!
else
if included
output.write("#{marker} -- %#{name}\n")
included = false
end
output.write(line)
end
end
unused.each do |name|
$stderr << "UNUSED #{name}\n"
end
end
content = {}
location = {}
marker = ARGV.pop
ARGV.each do |spec|
Dir[spec].each do |file|
scan(file, content, location)
end
end
process($stdin, $stdout, marker, content, location)
# to run:
# ruby "src/*.hs" "output/*.out" ">"