-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathnode_table_loader.rb
More file actions
150 lines (146 loc) · 3.84 KB
/
node_table_loader.rb
File metadata and controls
150 lines (146 loc) · 3.84 KB
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
# Loads Spinel's text AST format into a node table object.
#
# The table object supplies the parallel-array storage operations used by
# Compiler: alloc_node, set_root_id, set_node_type, set_node_content, and
# set_*_field.
class NodeTableLoader
def initialize(table)
@table = table
end
def read_text_ast(data)
lines = data.split(10.chr)
# Pass 1: find max node ID
max_id = 0
i = 0
while i < lines.length
line = lines[i]
if line.length > 0
parts = line.split(" ")
if parts.length >= 2
if parts.first == "ROOT"
@table.set_root_id(parts[1].to_i)
end
# Issue #878: SOURCE_FILE <escaped-path> appears once near the
# top of the AST. Loader stashes the unescaped path on the
# table so __dir__ et al. can recover it without scanning
# SourceFileNode entries (which only exist if the source
# references __FILE__).
if parts.first == "SOURCE_FILE"
if parts.length >= 2
@table.set_source_file_path(unescape_str(parts[1]))
end
end
# Debug multi-file map (FILE <id> <escaped-path>): records which
# original file each node's `node_file` id refers to, so codegen can
# emit `#line N "file"` across require_relative-inlined sources.
if parts.first == "FILE"
if parts.length >= 3
@table.set_file_entry(parts[1].to_i, unescape_str(parts[2]))
end
end
if parts.first == "N"
nid = parts[1].to_i
if nid > max_id
max_id = nid
end
end
end
end
i = i + 1
end
# Allocate all node slots in one bulk presize pass instead of
# max_id+1 separate alloc_node calls. The bulk path avoids ~46 * N
# Array#push operations (and their amortized realloc events).
@table.alloc_nodes(max_id + 1)
# Pass 2: populate fields
i = 0
while i < lines.length
line = lines[i]
if line.length > 0
ast_parse_line(line)
end
i = i + 1
end
end
def ast_parse_line(line)
parts = line.split(" ")
if parts.length < 3
return
end
tag = parts.first
nid = parts[1].to_i
if tag == "N"
@table.set_node_type(nid, parts[2])
elsif tag == "S"
field = parts[2]
val = ""
if parts.length >= 4
val = unescape_str(parts[3])
end
@table.set_string_field(nid, field, val)
elsif tag == "I"
field = parts[2]
ival = 0
if parts.length >= 4
ival = parts[3].to_i
end
@table.set_int_field(nid, field, ival)
elsif tag == "F"
if parts.length >= 4
@table.set_node_content(nid, parts[3])
end
elsif tag == "R"
field = parts[2]
ref_id = -1
if parts.length >= 4
ref_id = parts[3].to_i
end
@table.set_ref_field(nid, field, ref_id)
elsif tag == "A"
field = parts[2]
ids_str = ""
if parts.length >= 4
ids_str = parts[3]
end
@table.set_array_field(nid, field, ids_str)
end
0
end
def unescape_str(s)
result = ""
i = 0
while i < s.length
ch = s[i]
if ch == "%"
if i + 2 < s.length
hex = s[i + 1] + s[i + 2]
case hex
when "0A"
result = result + 10.chr
when "0D"
result = result + 13.chr
when "09"
result = result + 9.chr
when "20"
result = result + " "
when "25"
result = result + "%"
when "00"
# Issue #722: NUL byte (literal embedded in a Ruby string).
result = result + 0.chr
else
result = result + "%" + hex
end
i = i + 3
else
result = result + ch
i = i + 1
end
else
result = result + ch
i = i + 1
end
end
result
end
end