Debian Codegraph @

esniff/index.js

raw
1 "use strict";
2
3 var ensureString = require("type/string/ensure")
4 , ensurePlainFunction = require("type/plain-function/ensure")
5 , from = require("es5-ext/array/from")
6 , primitiveSet = require("es5-ext/object/primitive-set")
7 , eventEmitter = require("event-emitter")
8 , allOff = require("event-emitter/all-off")
9 , d = require("d")
10 , eolSet = require("./lib/ws-eol")
11 , wsSet = require("./lib/ws")
12 , identStart = require("./lib/ident-start-pattern")
13 , identNext = require("./lib/ident-next-pattern");
14
15 var objHasOwnProperty = Object.prototype.hasOwnProperty
16 , preRegExpSet = primitiveSet.apply(null, from(";{=([,<>+-*/%&|^!~?:}"))
17 , nonNameSet = primitiveSet.apply(null, from(";{=([,<>+-*/%&|^!~?:})].`"))
18 , reIdentStart = new RegExp(identStart)
19 , reIdentNext = new RegExp(identNext);
20
21 var code, index, char, state, columnIndex, line, quote, scopeDepth, templateContext, previousToken
22 , followsWhitespace, results, followsSkip, collectedScopeDatum, collectedScopeData
23 , collectedScopeDepth, commentDatum, shouldCollectComments;
24
25 var handleEol = function () {
26 if (char === "\r" && code[index + 1] === "\n") char = code[++index];
27 columnIndex = index + 1;
28 ++line;
29 };
30
31 var emitter = eventEmitter();
32 var accessor = Object.create(null, {
33 skipCodePart: d(function (codePart) {
34 var codePartLength = codePart.length;
35 for (var i = 0; i < codePartLength; ++i) {
36 if (code[index + i] !== codePart[i]) return false;
37 }
38 index += codePartLength;
39 char = code[index];
40 previousToken = code[index - 1];
41 followsWhitespace = false;
42 followsSkip = true;
43 return true;
44 }),
45 skipIdentifier: d(function () {
46 if (!reIdentStart.test(char)) return null;
47 var startIndex = index;
48 var identifier = char;
49 while ((char = code[++index]) && reIdentNext.test(char)) identifier += char;
50 followsWhitespace = false;
51 followsSkip = true;
52 previousToken = code[index - 1];
53 return { name: identifier, start: startIndex, end: index };
54 }),
55 skipWhitespace: d(function () {
56 while (char) {
57 if (objHasOwnProperty.call(wsSet, char)) {
58 if (objHasOwnProperty.call(eolSet, char)) handleEol();
59 } else if (char === "/") {
60 if (code[index + 1] === "/") {
61 // Single line comment
62 if (shouldCollectComments) {
63 commentDatum = {
64 type: "comment",
65 point: index,
66 line: line,
67 column: index - columnIndex
68 };
69 }
70 index += 2;
71 char = code[index];
72 while (char) {
73 if (objHasOwnProperty.call(eolSet, char)) {
74 if (commentDatum) {
75 commentDatum.endPoint = index;
76 results.push(commentDatum);
77 commentDatum = null;
78 }
79 handleEol();
80 break;
81 }
82 char = code[++index];
83 }
84 } else if (code[index + 1] === "*") {
85 if (shouldCollectComments) {
86 commentDatum = {
87 type: "comment",
88 point: index,
89 line: line,
90 column: index - columnIndex
91 };
92 }
93 index += 2;
94 char = code[index];
95 while (char) {
96 if (objHasOwnProperty.call(eolSet, char)) handleEol();
97 if (char === "*" && code[index + 1] === "/") {
98 if (commentDatum) {
99 commentDatum.endPoint = index + 2;
100 results.push(commentDatum);
101 commentDatum = null;
102 }
103 char = code[++index];
104 break;
105 }
106 char = code[++index];
107 }
108 } else {
109 break;
110 }
111 } else {
112 break;
113 }
114 followsWhitespace = true;
115 followsSkip = true;
116 char = code[++index];
117 }
118 }),
119 collectScope: d(function () {
120 if (char !== "(") return;
121 previousToken = char;
122 char = code[++index];
123 followsSkip = true;
124 if (collectedScopeDatum) collectedScopeData.push(collectedScopeDepth, collectedScopeDatum);
125 collectedScopeDepth = ++scopeDepth;
126 collectedScopeDatum = {
127 type: "scope",
128 point: index + 1,
129 line: line,
130 column: index - columnIndex + 1
131 };
132 }),
133 stop: d(function () { state = null; }),
134 index: d.gs(function () { return index; }),
135 previousToken: d.gs(function () { return previousToken; }),
136 scopeDepth: d.gs(function () { return scopeDepth; }),
137 shouldCollectComments: d.gs(
138 function () { return shouldCollectComments; },
139 function (value) { shouldCollectComments = Boolean(value); }
140 )
141 });
142
143 module.exports = function (userCode, executor) {
144 code = ensureString(userCode);
145 executor = ensurePlainFunction(executor);
146 allOff(emitter);
147 executor(emitter, accessor);
148 index = -1;
149 state = "out";
150 columnIndex = 0;
151 line = 1;
152 scopeDepth = 0;
153 templateContext = [];
154 previousToken = null;
155 followsWhitespace = true;
156 results = [];
157 followsSkip = false;
158 collectedScopeDatum = null;
159 collectedScopeData = [];
160 collectedScopeDepth = null;
161
162 stateLoop: while (state) {
163 if (followsSkip) followsSkip = false;
164 else char = code[++index];
165 if (!char) break;
166
167 switch (state) {
168 case "out":
169 if (objHasOwnProperty.call(wsSet, char)) {
170 if (objHasOwnProperty.call(eolSet, char)) {
171 handleEol();
172 }
173 followsWhitespace = true;
174 continue stateLoop;
175 }
176 if (char === "/") {
177 if (previousToken && objHasOwnProperty.call(preRegExpSet, previousToken)) {
178 state = "slashOrRegexp";
179 } else {
180 state = "slash";
181 }
182 } else if (char === "'" || char === "\"") {
183 state = "string";
184 quote = char;
185 } else if (char === "`") {
186 state = "template";
187 } else if (char === "(" || char === "{" || char === "[") {
188 ++scopeDepth;
189 } else if (char === ")" || char === "}" || char === "]") {
190 if (scopeDepth === collectedScopeDepth) {
191 collectedScopeDatum.raw = code.slice(collectedScopeDatum.point - 1, index);
192 results.push(collectedScopeDatum);
193 collectedScopeDatum = collectedScopeData.pop();
194 collectedScopeDepth = collectedScopeData.pop();
195 }
196 --scopeDepth;
197 if (char === "}") {
198 if (templateContext[templateContext.length - 1] === scopeDepth + 1) {
199 templateContext.pop();
200 state = "template";
201 }
202 }
203 }
204 if (
205 !previousToken ||
206 followsWhitespace ||
207 objHasOwnProperty.call(nonNameSet, previousToken) ||
208 objHasOwnProperty.call(nonNameSet, char)
209 ) {
210 emitter.emit("trigger:" + char, accessor);
211 if (followsSkip) continue stateLoop;
212 }
213 previousToken = char;
214 followsWhitespace = false;
215 continue stateLoop;
216 case "slashOrRegexp":
217 case "slash":
218 if (char === "/") {
219 if (shouldCollectComments) {
220 commentDatum = {
221 type: "comment",
222 point: index - 1,
223 line: line,
224 column: index - columnIndex - 1
225 };
226 }
227 state = "singleLineComment";
228 } else if (char === "*") {
229 if (shouldCollectComments) {
230 commentDatum = {
231 type: "comment",
232 point: index - 1,
233 line: line,
234 column: index - columnIndex - 1
235 };
236 }
237 state = "multiLineComment";
238 } else if (objHasOwnProperty.call(eolSet, char)) {
239 handleEol();
240 followsWhitespace = true;
241 state = "out";
242 continue stateLoop;
243 } else if (state === "slashOrRegexp") {
244 state = "regexp";
245 } else {
246 state = "out";
247 continue stateLoop;
248 }
249 break;
250 case "singleLineComment":
251 if (objHasOwnProperty.call(eolSet, char)) {
252 if (commentDatum) {
253 commentDatum.endPoint = index;
254 results.push(commentDatum);
255 commentDatum = null;
256 }
257 handleEol();
258 followsWhitespace = true;
259 state = "out";
260 }
261 continue stateLoop;
262 case "multiLineComment":
263 if (char === "*") state = "multiLineCommentStar";
264 else if (objHasOwnProperty.call(eolSet, char)) handleEol();
265 continue stateLoop;
266 case "multiLineCommentStar":
267 if (char === "/") {
268 followsWhitespace = true;
269 state = "out";
270 if (commentDatum) {
271 commentDatum.endPoint = index + 1;
272 results.push(commentDatum);
273 commentDatum = null;
274 }
275 } else if (char !== "*") {
276 if (objHasOwnProperty.call(eolSet, char)) handleEol();
277 state = "multiLineComment";
278 }
279 continue stateLoop;
280 case "string":
281 if (char === "\\") state = "stringEscape";
282 else if (char === quote) state = "out";
283 break;
284 case "stringEscape":
285 if (objHasOwnProperty.call(eolSet, char)) handleEol();
286 state = "string";
287 break;
288 case "template":
289 if (char === "$") state = "templateDollar";
290 else if (char === "\\") state = "templateEscape";
291 else if (char === "`") state = "out";
292 else if (objHasOwnProperty.call(eolSet, char)) handleEol();
293 break;
294 case "templateEscape":
295 if (objHasOwnProperty.call(eolSet, char)) handleEol();
296 state = "template";
297 break;
298 case "templateDollar":
299 if (char === "{") {
300 templateContext.push(++scopeDepth);
301 state = "out";
302 } else if (char !== "$") {
303 if (objHasOwnProperty.call(eolSet, char)) handleEol();
304 state = "template";
305 }
306 break;
307 case "regexp":
308 if (char === "\\") state = "regexpEscape";
309 else if (char === "/") state = "out";
310 break;
311 case "regexpEscape":
312 state = "regexp";
313 break;
314 /* istanbul ignore next */
315 default:
316 throw new Error("Unexpected state " + state);
317 }
318 previousToken = null;
319 followsWhitespace = false;
320 }
321
322 return results;
323 };