-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast2scope.js
282 lines (227 loc) · 8.82 KB
/
ast2scope.js
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
/*global global exports require acorn need$ load*/
var global, exports;
//#BEGIN_BUILD_STRIP
// With Node.js
if (typeof require === 'function')
{
acorn = require( './acorn.25.03.2014/acorn' );
acorn.walk = require( './acorn.25.03.2014/util/walk' );
}
//#END_BUILD_STRIP
// Without Node.js (browser, or V8 alone)
// Support both use cases: browser development (example: jsm_dev) and
// command-line transformation (example: jsm_dev -> jsm_out).
if (typeof acorn.walk === 'undefined')
(typeof need$ !== 'undefined' ? need$ : load)( "acorn.25.03.2014/util/walk.js" );
(function (global) {
// ---------- Public API
global.ast2scope = ast2scope;
// ---------- Public API implementation
function ast2scope( /*object*/topnode )
// Returns an object with various method to access scope information.
//
// Guillaume Lathoud
{
var key2scope
, funinfoarr
, funkey2info
, idinfoarr
, idstart2info
, key2node = {}
, GLOBAL = '<global>'
;
ensure_ready();
return {
topnode : function () { return topnode; }
, id2scope : function ( idnode ) { ensure_ready(); return key2scope [ node2key( idnode ) ]; }
, idinfoarr : function () { ensure_ready(); return idinfoarr.slice(); }
, idstart2info : function () { ensure_ready(); return Object.create( idstart2info ); }
, fun2info : function ( funnode ) { ensure_ready(); return funkey2info[ node2key( funnode ) ]; }
, funinfoarr : function () { ensure_ready(); return funinfoarr.slice(); }
};
function ensure_ready()
{
if (!key2scope)
create_key2scope();
}
function node2key( node )
{
if (!node)
return GLOBAL;
node.start.toPrecision.call.a;
node.end .toPrecision.call.a;
(node.type || 0).substring.call.a;
var key = node.start + '.' + node.end + '.' + node.type;
// Sanity check: key unicity
if (key2node.hasOwnProperty( key ))
{
if (node !== key2node[ key ])
throw new Error( 'insanity!' );
}
else
{
key2node[ key ] = node;
}
return key;
}
function create_key2scope()
{
key2scope = {};
funinfoarr = [];
funkey2info = {};
idinfoarr = [];
idstart2info = {};
// First walk to list the name (if any), params and
// declarations of each function.
var latest_info = create_funinfo( null, null )
, base = Object.create( acorn.walk.base )
, base_Function = base.Function
;
base.Function = new_base_Function;
function new_base_Function( node /* , ... */ )
{
first_base_Function2( node );
return base_Function.apply( base, arguments );
}
acorn.walk.simple(
topnode
, { VariableDeclaration : first_meet_VariableDeclaration }
, base
);
// Second walk to define the scope of each identifier
// usage.
var second_latest_info = funkey2info[ node2key( null ) ]
, second_base = Object.create( acorn.walk.base )
, second_base_Function = second_base.Function
;
second_base.Function = new_second_base_Function;
function new_second_base_Function( node /*, ... */ )
{
second_base_Function2( node );
return second_base_Function.apply( base, arguments );
}
acorn.walk.simple(
topnode
, { Identifier : second_meet_Identifier }
, second_base
);
// Sort for convenience
idinfoarr.sort( idinfo_compare );
// --- Details
function idinfo_compare( a, b )
{
return a.node.start - b.node.start;
}
function first_base_Function2( node )
{
if (/declaration/i.test( node.type ))
first_meet_DeclarationIdentifier( node.id );
latest_info = create_funinfo( node, latest_info );
node.params.forEach( first_meet_DeclarationIdentifier );
}
function first_meet_VariableDeclaration( node )
{
node.declarations.forEach( function ( d ) { first_meet_DeclarationIdentifier( d.id ); } );
}
function first_meet_DeclarationIdentifier( node )
{
if ('Identifier' !== node.type)
throw new Error('bug#first_meet_DeclarationIdentifier');
var key = node2key( node );
if (!key2scope.hasOwnProperty( key ))
{
latest_info = get_parent_info( node, latest_info );
latest_info.declarr.push( node );
var scope = latest_info.funnode;
save_idinfo( node, key, scope );
}
}
function second_base_Function2( node )
{
second_latest_info = funkey2info[ node2key( node ) ];
second_latest_info.declarr.slice.call.a; // Check
}
function second_meet_Identifier( node )
{
var key = node2key( node );
if (!key2scope.hasOwnProperty( key ))
{
second_latest_info = get_parent_info( node, second_latest_info );
var scope = find_scope( node, second_latest_info );
save_idinfo( node, key, scope );
}
}
}
// --- More details
function save_idinfo( node, key, scope )
{
key2scope[ key ] = scope;
var idinfo = {
node : node
, scope : scope
};
idinfoarr.push( idinfo );
var idstart = node.start;
idstart.toPrecision.call.a;
if (idstart in idstart2info)
throw new Error( 'bug#save_idinfo' );
idstart2info[ idstart ] = idinfo;
}
function create_funinfo( node, latest_info )
{
var funkey = node2key( node );
if (funkey2info.hasOwnProperty( funkey ))
throw new Error('bug#create_funinfo');
var parent = node && get_parent( node, latest_info )
, p_info = node && funkey2info[ node2key( parent ) ]
;
if (p_info)
p_info.kidfunarr.push( node );
var ret = funkey2info[ funkey ] = {
funnode : node || null
, parent : parent
, declarr : []
, kidfunarr : []
};
funinfoarr.push( ret );
return ret;
}
function get_parent_info( node, latest_info )
{
var p = get_parent( node, latest_info );
return funkey2info[ node2key( p ) ];
}
function get_parent( node, latest_info )
{
var p_info = latest_info // latest_info: sibling or parent -> if a sibling, then find the parent
, a_start = node.start
, a_end = node.end
;
while (p_info)
{
var pfa = p_info.funnode;
if (!pfa)
return null; // global scope
if (pfa.start < a_start && a_end < pfa.end)
return pfa;
p_info = funkey2info[ node2key( p_info.parent ) ];
}
return null; // global scope
}
function find_scope( node, info )
{
if (node.type !== 'Identifier')
throw new Error( 'bug#find_scope' );
var node_name = node.name
, declarr = info.declarr
;
for (var i = declarr.length; i--;) // Could be smarter: bisection on .start
{
if (declarr[ i ].name === node_name)
return info.funnode;
}
return info.parent ? find_scope( node, funkey2info[ node2key( info.parent ) ] ) : null;
}
}
})( global || exports || this );