This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
bindings.js
106 lines (91 loc) · 3.33 KB
/
bindings.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
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
/* Name bindings for lexical variables. */
if(typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require, exports) {
var astutil = require('./astutil'),
symtab = require('./symtab');
function addBindings(ast) {
var global_scope = new symtab.Symtab();
global_scope.global = true;
var scope = global_scope;
var decl_scope = scope;
astutil.visit(ast,
function enter(nd, visit) {
switch(nd.type) {
case 'FunctionDeclaration':
decl_scope.set(nd.id.name, nd.id);
visit(nd.id);
// FALL THROUGH
case 'FunctionExpression':
var old_decl_scope = decl_scope;
scope = decl_scope = new symtab.Symtab(scope);
scope.global = false;
nd.attr.scope = scope;
if(nd.type === 'FunctionExpression' && nd.id) {
decl_scope.set(nd.id.name, nd.id);
visit(nd.id);
}
decl_scope.set('this', { type: 'Identifier',
name: 'this',
loc: nd.loc,
range: nd.range,
attr: { enclosingFile: nd.attr.enclosingFile,
scope: decl_scope } });
for(var i=0;i<nd.params.length;++i) {
decl_scope.set(nd.params[i].name, nd.params[i]);
visit(nd.params[i]);
}
visit(nd.body);
// restore previous scope
if(!decl_scope.hasOwn('arguments'))
decl_scope.set('arguments', { type: 'Identifier',
name: 'arguments',
loc: nd.loc,
range: nd.range,
attr: { enclosingFile: nd.attr.enclosingFile,
scope: decl_scope } });
scope = scope.outer;
decl_scope = old_decl_scope;
return false;
case 'CatchClause':
scope = new symtab.Symtab(scope);
scope.global = false;
scope.set(nd.param.name, nd.param);
visit(nd.param);
visit(nd.body);
scope = scope.outer;
return false;
case 'Identifier':
case 'ThisExpression':
nd.attr.scope = decl_scope;
break;
case 'MemberExpression':
visit(nd.object);
if(nd.computed)
visit(nd.property);
return false;
case 'VariableDeclarator':
if(!decl_scope.hasOwn(nd.id.name))
decl_scope.set(nd.id.name, nd.id);
break;
case 'Property':
// don't visit nd.key
visit(nd.value);
return false;
}
});
}
exports.addBindings = addBindings;
return exports;
});