-
Notifications
You must be signed in to change notification settings - Fork 341
/
parser.js
136 lines (110 loc) · 3.41 KB
/
parser.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
var assert = require("assert");
var types = require("./types");
var n = types.namedTypes;
var b = types.builders;
var Patcher = require("./patcher").Patcher;
var Visitor = require("./visitor").Visitor;
var normalizeOptions = require("./options").normalize;
exports.parse = function(source, options) {
options = normalizeOptions(options);
var lines = require("./lines").fromString(source, options);
var pure = options.esprima.parse(lines.toString({
tabWidth: options.tabWidth,
reuseWhitespace: false,
useTabs: false
}), {
loc: true,
range: options.range,
comment: true,
tolerant: options.tolerant
});
new LocationFixer(lines).visit(pure);
require("./comments").add(pure, lines);
// In order to ensure we reprint leading and trailing program
// comments, wrap the original Program node with a File node.
pure = b.file(pure);
pure.loc = {
lines: lines,
start: lines.firstPos(),
end: lines.lastPos()
};
// Return a copy of the original AST so that any changes made may be
// compared to the original.
return copyAst(pure);
};
var LocationFixer = Visitor.extend({
init: function(lines) {
this.lines = lines;
},
genericVisit: function(node) {
this._super(node);
var loc = node && node.loc,
start = loc && loc.start,
end = loc && loc.end;
if (loc) {
Object.defineProperty(loc, "lines", {
value: this.lines
});
}
if (start) {
start.line = Math.max(start.line, 1);
}
if (end) {
end.line = Math.max(end.line, 1);
var lines = loc.lines, pos = {
line: end.line,
column: end.column
};
// Negative columns might indicate an Esprima bug?
// For now, treat them as reverse indices, a la Python.
if (pos.column < 0)
pos.column += lines.getLineLength(pos.line);
while (lines.prevPos(pos)) {
if (/\S/.test(lines.charAt(pos))) {
assert.ok(lines.nextPos(pos));
end.line = pos.line;
end.column = pos.column;
break;
}
}
}
}
});
function copyAst(node, parent) {
if (typeof node === "object" &&
node !== null)
{
if (node instanceof RegExp)
return node;
if (node instanceof Array) {
return node.map(function(child) {
return copyAst(child, parent);
});
}
var copy = {},
key,
val;
for (key in node) {
if (node.hasOwnProperty(key)) {
val = copyAst(node[key], node);
if (typeof val !== "function")
copy[key] = val;
}
}
if (parent && n.Node.check(node)) {
// Allow upwards traversal of the original AST.
Object.defineProperty(node, "parentNode", {
value: parent
});
}
// Provide a link from the copy to the original.
Object.defineProperty(copy, "original", {
value: node,
configurable: false,
enumerable: false,
writable: true,
});
return copy;
}
return node;
}