UNPKG

133 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global = global || self, global.esquery = factory());
5}(this, (function () { 'use strict';
6
7 function _iterableToArrayLimit(arr, i) {
8 var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
9 if (null != _i) {
10 var _s,
11 _e,
12 _x,
13 _r,
14 _arr = [],
15 _n = !0,
16 _d = !1;
17 try {
18 if (_x = (_i = _i.call(arr)).next, 0 === i) {
19 if (Object(_i) !== _i) return;
20 _n = !1;
21 } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
22 } catch (err) {
23 _d = !0, _e = err;
24 } finally {
25 try {
26 if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
27 } finally {
28 if (_d) throw _e;
29 }
30 }
31 return _arr;
32 }
33 }
34 function _typeof(obj) {
35 "@babel/helpers - typeof";
36
37 return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
38 return typeof obj;
39 } : function (obj) {
40 return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
41 }, _typeof(obj);
42 }
43 function _slicedToArray(arr, i) {
44 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
45 }
46 function _toConsumableArray(arr) {
47 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
48 }
49 function _arrayWithoutHoles(arr) {
50 if (Array.isArray(arr)) return _arrayLikeToArray(arr);
51 }
52 function _arrayWithHoles(arr) {
53 if (Array.isArray(arr)) return arr;
54 }
55 function _iterableToArray(iter) {
56 if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
57 }
58 function _unsupportedIterableToArray(o, minLen) {
59 if (!o) return;
60 if (typeof o === "string") return _arrayLikeToArray(o, minLen);
61 var n = Object.prototype.toString.call(o).slice(8, -1);
62 if (n === "Object" && o.constructor) n = o.constructor.name;
63 if (n === "Map" || n === "Set") return Array.from(o);
64 if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
65 }
66 function _arrayLikeToArray(arr, len) {
67 if (len == null || len > arr.length) len = arr.length;
68 for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
69 return arr2;
70 }
71 function _nonIterableSpread() {
72 throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
73 }
74 function _nonIterableRest() {
75 throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
76 }
77
78 var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
79
80 function createCommonjsModule(fn, module) {
81 return module = { exports: {} }, fn(module, module.exports), module.exports;
82 }
83
84 var estraverse = createCommonjsModule(function (module, exports) {
85 /*
86 Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
87 Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
88
89 Redistribution and use in source and binary forms, with or without
90 modification, are permitted provided that the following conditions are met:
91
92 * Redistributions of source code must retain the above copyright
93 notice, this list of conditions and the following disclaimer.
94 * Redistributions in binary form must reproduce the above copyright
95 notice, this list of conditions and the following disclaimer in the
96 documentation and/or other materials provided with the distribution.
97
98 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
99 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
102 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
103 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
104 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
105 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
106 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
107 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
108 */
109 /*jslint vars:false, bitwise:true*/
110 /*jshint indent:4*/
111 /*global exports:true*/
112 (function clone(exports) {
113
114 var Syntax, VisitorOption, VisitorKeys, BREAK, SKIP, REMOVE;
115 function deepCopy(obj) {
116 var ret = {},
117 key,
118 val;
119 for (key in obj) {
120 if (obj.hasOwnProperty(key)) {
121 val = obj[key];
122 if (typeof val === 'object' && val !== null) {
123 ret[key] = deepCopy(val);
124 } else {
125 ret[key] = val;
126 }
127 }
128 }
129 return ret;
130 }
131
132 // based on LLVM libc++ upper_bound / lower_bound
133 // MIT License
134
135 function upperBound(array, func) {
136 var diff, len, i, current;
137 len = array.length;
138 i = 0;
139 while (len) {
140 diff = len >>> 1;
141 current = i + diff;
142 if (func(array[current])) {
143 len = diff;
144 } else {
145 i = current + 1;
146 len -= diff + 1;
147 }
148 }
149 return i;
150 }
151 Syntax = {
152 AssignmentExpression: 'AssignmentExpression',
153 AssignmentPattern: 'AssignmentPattern',
154 ArrayExpression: 'ArrayExpression',
155 ArrayPattern: 'ArrayPattern',
156 ArrowFunctionExpression: 'ArrowFunctionExpression',
157 AwaitExpression: 'AwaitExpression',
158 // CAUTION: It's deferred to ES7.
159 BlockStatement: 'BlockStatement',
160 BinaryExpression: 'BinaryExpression',
161 BreakStatement: 'BreakStatement',
162 CallExpression: 'CallExpression',
163 CatchClause: 'CatchClause',
164 ChainExpression: 'ChainExpression',
165 ClassBody: 'ClassBody',
166 ClassDeclaration: 'ClassDeclaration',
167 ClassExpression: 'ClassExpression',
168 ComprehensionBlock: 'ComprehensionBlock',
169 // CAUTION: It's deferred to ES7.
170 ComprehensionExpression: 'ComprehensionExpression',
171 // CAUTION: It's deferred to ES7.
172 ConditionalExpression: 'ConditionalExpression',
173 ContinueStatement: 'ContinueStatement',
174 DebuggerStatement: 'DebuggerStatement',
175 DirectiveStatement: 'DirectiveStatement',
176 DoWhileStatement: 'DoWhileStatement',
177 EmptyStatement: 'EmptyStatement',
178 ExportAllDeclaration: 'ExportAllDeclaration',
179 ExportDefaultDeclaration: 'ExportDefaultDeclaration',
180 ExportNamedDeclaration: 'ExportNamedDeclaration',
181 ExportSpecifier: 'ExportSpecifier',
182 ExpressionStatement: 'ExpressionStatement',
183 ForStatement: 'ForStatement',
184 ForInStatement: 'ForInStatement',
185 ForOfStatement: 'ForOfStatement',
186 FunctionDeclaration: 'FunctionDeclaration',
187 FunctionExpression: 'FunctionExpression',
188 GeneratorExpression: 'GeneratorExpression',
189 // CAUTION: It's deferred to ES7.
190 Identifier: 'Identifier',
191 IfStatement: 'IfStatement',
192 ImportExpression: 'ImportExpression',
193 ImportDeclaration: 'ImportDeclaration',
194 ImportDefaultSpecifier: 'ImportDefaultSpecifier',
195 ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
196 ImportSpecifier: 'ImportSpecifier',
197 Literal: 'Literal',
198 LabeledStatement: 'LabeledStatement',
199 LogicalExpression: 'LogicalExpression',
200 MemberExpression: 'MemberExpression',
201 MetaProperty: 'MetaProperty',
202 MethodDefinition: 'MethodDefinition',
203 ModuleSpecifier: 'ModuleSpecifier',
204 NewExpression: 'NewExpression',
205 ObjectExpression: 'ObjectExpression',
206 ObjectPattern: 'ObjectPattern',
207 PrivateIdentifier: 'PrivateIdentifier',
208 Program: 'Program',
209 Property: 'Property',
210 PropertyDefinition: 'PropertyDefinition',
211 RestElement: 'RestElement',
212 ReturnStatement: 'ReturnStatement',
213 SequenceExpression: 'SequenceExpression',
214 SpreadElement: 'SpreadElement',
215 Super: 'Super',
216 SwitchStatement: 'SwitchStatement',
217 SwitchCase: 'SwitchCase',
218 TaggedTemplateExpression: 'TaggedTemplateExpression',
219 TemplateElement: 'TemplateElement',
220 TemplateLiteral: 'TemplateLiteral',
221 ThisExpression: 'ThisExpression',
222 ThrowStatement: 'ThrowStatement',
223 TryStatement: 'TryStatement',
224 UnaryExpression: 'UnaryExpression',
225 UpdateExpression: 'UpdateExpression',
226 VariableDeclaration: 'VariableDeclaration',
227 VariableDeclarator: 'VariableDeclarator',
228 WhileStatement: 'WhileStatement',
229 WithStatement: 'WithStatement',
230 YieldExpression: 'YieldExpression'
231 };
232 VisitorKeys = {
233 AssignmentExpression: ['left', 'right'],
234 AssignmentPattern: ['left', 'right'],
235 ArrayExpression: ['elements'],
236 ArrayPattern: ['elements'],
237 ArrowFunctionExpression: ['params', 'body'],
238 AwaitExpression: ['argument'],
239 // CAUTION: It's deferred to ES7.
240 BlockStatement: ['body'],
241 BinaryExpression: ['left', 'right'],
242 BreakStatement: ['label'],
243 CallExpression: ['callee', 'arguments'],
244 CatchClause: ['param', 'body'],
245 ChainExpression: ['expression'],
246 ClassBody: ['body'],
247 ClassDeclaration: ['id', 'superClass', 'body'],
248 ClassExpression: ['id', 'superClass', 'body'],
249 ComprehensionBlock: ['left', 'right'],
250 // CAUTION: It's deferred to ES7.
251 ComprehensionExpression: ['blocks', 'filter', 'body'],
252 // CAUTION: It's deferred to ES7.
253 ConditionalExpression: ['test', 'consequent', 'alternate'],
254 ContinueStatement: ['label'],
255 DebuggerStatement: [],
256 DirectiveStatement: [],
257 DoWhileStatement: ['body', 'test'],
258 EmptyStatement: [],
259 ExportAllDeclaration: ['source'],
260 ExportDefaultDeclaration: ['declaration'],
261 ExportNamedDeclaration: ['declaration', 'specifiers', 'source'],
262 ExportSpecifier: ['exported', 'local'],
263 ExpressionStatement: ['expression'],
264 ForStatement: ['init', 'test', 'update', 'body'],
265 ForInStatement: ['left', 'right', 'body'],
266 ForOfStatement: ['left', 'right', 'body'],
267 FunctionDeclaration: ['id', 'params', 'body'],
268 FunctionExpression: ['id', 'params', 'body'],
269 GeneratorExpression: ['blocks', 'filter', 'body'],
270 // CAUTION: It's deferred to ES7.
271 Identifier: [],
272 IfStatement: ['test', 'consequent', 'alternate'],
273 ImportExpression: ['source'],
274 ImportDeclaration: ['specifiers', 'source'],
275 ImportDefaultSpecifier: ['local'],
276 ImportNamespaceSpecifier: ['local'],
277 ImportSpecifier: ['imported', 'local'],
278 Literal: [],
279 LabeledStatement: ['label', 'body'],
280 LogicalExpression: ['left', 'right'],
281 MemberExpression: ['object', 'property'],
282 MetaProperty: ['meta', 'property'],
283 MethodDefinition: ['key', 'value'],
284 ModuleSpecifier: [],
285 NewExpression: ['callee', 'arguments'],
286 ObjectExpression: ['properties'],
287 ObjectPattern: ['properties'],
288 PrivateIdentifier: [],
289 Program: ['body'],
290 Property: ['key', 'value'],
291 PropertyDefinition: ['key', 'value'],
292 RestElement: ['argument'],
293 ReturnStatement: ['argument'],
294 SequenceExpression: ['expressions'],
295 SpreadElement: ['argument'],
296 Super: [],
297 SwitchStatement: ['discriminant', 'cases'],
298 SwitchCase: ['test', 'consequent'],
299 TaggedTemplateExpression: ['tag', 'quasi'],
300 TemplateElement: [],
301 TemplateLiteral: ['quasis', 'expressions'],
302 ThisExpression: [],
303 ThrowStatement: ['argument'],
304 TryStatement: ['block', 'handler', 'finalizer'],
305 UnaryExpression: ['argument'],
306 UpdateExpression: ['argument'],
307 VariableDeclaration: ['declarations'],
308 VariableDeclarator: ['id', 'init'],
309 WhileStatement: ['test', 'body'],
310 WithStatement: ['object', 'body'],
311 YieldExpression: ['argument']
312 };
313
314 // unique id
315 BREAK = {};
316 SKIP = {};
317 REMOVE = {};
318 VisitorOption = {
319 Break: BREAK,
320 Skip: SKIP,
321 Remove: REMOVE
322 };
323 function Reference(parent, key) {
324 this.parent = parent;
325 this.key = key;
326 }
327 Reference.prototype.replace = function replace(node) {
328 this.parent[this.key] = node;
329 };
330 Reference.prototype.remove = function remove() {
331 if (Array.isArray(this.parent)) {
332 this.parent.splice(this.key, 1);
333 return true;
334 } else {
335 this.replace(null);
336 return false;
337 }
338 };
339 function Element(node, path, wrap, ref) {
340 this.node = node;
341 this.path = path;
342 this.wrap = wrap;
343 this.ref = ref;
344 }
345 function Controller() {}
346
347 // API:
348 // return property path array from root to current node
349 Controller.prototype.path = function path() {
350 var i, iz, j, jz, result, element;
351 function addToPath(result, path) {
352 if (Array.isArray(path)) {
353 for (j = 0, jz = path.length; j < jz; ++j) {
354 result.push(path[j]);
355 }
356 } else {
357 result.push(path);
358 }
359 }
360
361 // root node
362 if (!this.__current.path) {
363 return null;
364 }
365
366 // first node is sentinel, second node is root element
367 result = [];
368 for (i = 2, iz = this.__leavelist.length; i < iz; ++i) {
369 element = this.__leavelist[i];
370 addToPath(result, element.path);
371 }
372 addToPath(result, this.__current.path);
373 return result;
374 };
375
376 // API:
377 // return type of current node
378 Controller.prototype.type = function () {
379 var node = this.current();
380 return node.type || this.__current.wrap;
381 };
382
383 // API:
384 // return array of parent elements
385 Controller.prototype.parents = function parents() {
386 var i, iz, result;
387
388 // first node is sentinel
389 result = [];
390 for (i = 1, iz = this.__leavelist.length; i < iz; ++i) {
391 result.push(this.__leavelist[i].node);
392 }
393 return result;
394 };
395
396 // API:
397 // return current node
398 Controller.prototype.current = function current() {
399 return this.__current.node;
400 };
401 Controller.prototype.__execute = function __execute(callback, element) {
402 var previous, result;
403 result = undefined;
404 previous = this.__current;
405 this.__current = element;
406 this.__state = null;
407 if (callback) {
408 result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node);
409 }
410 this.__current = previous;
411 return result;
412 };
413
414 // API:
415 // notify control skip / break
416 Controller.prototype.notify = function notify(flag) {
417 this.__state = flag;
418 };
419
420 // API:
421 // skip child nodes of current node
422 Controller.prototype.skip = function () {
423 this.notify(SKIP);
424 };
425
426 // API:
427 // break traversals
428 Controller.prototype['break'] = function () {
429 this.notify(BREAK);
430 };
431
432 // API:
433 // remove node
434 Controller.prototype.remove = function () {
435 this.notify(REMOVE);
436 };
437 Controller.prototype.__initialize = function (root, visitor) {
438 this.visitor = visitor;
439 this.root = root;
440 this.__worklist = [];
441 this.__leavelist = [];
442 this.__current = null;
443 this.__state = null;
444 this.__fallback = null;
445 if (visitor.fallback === 'iteration') {
446 this.__fallback = Object.keys;
447 } else if (typeof visitor.fallback === 'function') {
448 this.__fallback = visitor.fallback;
449 }
450 this.__keys = VisitorKeys;
451 if (visitor.keys) {
452 this.__keys = Object.assign(Object.create(this.__keys), visitor.keys);
453 }
454 };
455 function isNode(node) {
456 if (node == null) {
457 return false;
458 }
459 return typeof node === 'object' && typeof node.type === 'string';
460 }
461 function isProperty(nodeType, key) {
462 return (nodeType === Syntax.ObjectExpression || nodeType === Syntax.ObjectPattern) && 'properties' === key;
463 }
464 function candidateExistsInLeaveList(leavelist, candidate) {
465 for (var i = leavelist.length - 1; i >= 0; --i) {
466 if (leavelist[i].node === candidate) {
467 return true;
468 }
469 }
470 return false;
471 }
472 Controller.prototype.traverse = function traverse(root, visitor) {
473 var worklist, leavelist, element, node, nodeType, ret, key, current, current2, candidates, candidate, sentinel;
474 this.__initialize(root, visitor);
475 sentinel = {};
476
477 // reference
478 worklist = this.__worklist;
479 leavelist = this.__leavelist;
480
481 // initialize
482 worklist.push(new Element(root, null, null, null));
483 leavelist.push(new Element(null, null, null, null));
484 while (worklist.length) {
485 element = worklist.pop();
486 if (element === sentinel) {
487 element = leavelist.pop();
488 ret = this.__execute(visitor.leave, element);
489 if (this.__state === BREAK || ret === BREAK) {
490 return;
491 }
492 continue;
493 }
494 if (element.node) {
495 ret = this.__execute(visitor.enter, element);
496 if (this.__state === BREAK || ret === BREAK) {
497 return;
498 }
499 worklist.push(sentinel);
500 leavelist.push(element);
501 if (this.__state === SKIP || ret === SKIP) {
502 continue;
503 }
504 node = element.node;
505 nodeType = node.type || element.wrap;
506 candidates = this.__keys[nodeType];
507 if (!candidates) {
508 if (this.__fallback) {
509 candidates = this.__fallback(node);
510 } else {
511 throw new Error('Unknown node type ' + nodeType + '.');
512 }
513 }
514 current = candidates.length;
515 while ((current -= 1) >= 0) {
516 key = candidates[current];
517 candidate = node[key];
518 if (!candidate) {
519 continue;
520 }
521 if (Array.isArray(candidate)) {
522 current2 = candidate.length;
523 while ((current2 -= 1) >= 0) {
524 if (!candidate[current2]) {
525 continue;
526 }
527 if (candidateExistsInLeaveList(leavelist, candidate[current2])) {
528 continue;
529 }
530 if (isProperty(nodeType, candidates[current])) {
531 element = new Element(candidate[current2], [key, current2], 'Property', null);
532 } else if (isNode(candidate[current2])) {
533 element = new Element(candidate[current2], [key, current2], null, null);
534 } else {
535 continue;
536 }
537 worklist.push(element);
538 }
539 } else if (isNode(candidate)) {
540 if (candidateExistsInLeaveList(leavelist, candidate)) {
541 continue;
542 }
543 worklist.push(new Element(candidate, key, null, null));
544 }
545 }
546 }
547 }
548 };
549 Controller.prototype.replace = function replace(root, visitor) {
550 var worklist, leavelist, node, nodeType, target, element, current, current2, candidates, candidate, sentinel, outer, key;
551 function removeElem(element) {
552 var i, key, nextElem, parent;
553 if (element.ref.remove()) {
554 // When the reference is an element of an array.
555 key = element.ref.key;
556 parent = element.ref.parent;
557
558 // If removed from array, then decrease following items' keys.
559 i = worklist.length;
560 while (i--) {
561 nextElem = worklist[i];
562 if (nextElem.ref && nextElem.ref.parent === parent) {
563 if (nextElem.ref.key < key) {
564 break;
565 }
566 --nextElem.ref.key;
567 }
568 }
569 }
570 }
571 this.__initialize(root, visitor);
572 sentinel = {};
573
574 // reference
575 worklist = this.__worklist;
576 leavelist = this.__leavelist;
577
578 // initialize
579 outer = {
580 root: root
581 };
582 element = new Element(root, null, null, new Reference(outer, 'root'));
583 worklist.push(element);
584 leavelist.push(element);
585 while (worklist.length) {
586 element = worklist.pop();
587 if (element === sentinel) {
588 element = leavelist.pop();
589 target = this.__execute(visitor.leave, element);
590
591 // node may be replaced with null,
592 // so distinguish between undefined and null in this place
593 if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
594 // replace
595 element.ref.replace(target);
596 }
597 if (this.__state === REMOVE || target === REMOVE) {
598 removeElem(element);
599 }
600 if (this.__state === BREAK || target === BREAK) {
601 return outer.root;
602 }
603 continue;
604 }
605 target = this.__execute(visitor.enter, element);
606
607 // node may be replaced with null,
608 // so distinguish between undefined and null in this place
609 if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
610 // replace
611 element.ref.replace(target);
612 element.node = target;
613 }
614 if (this.__state === REMOVE || target === REMOVE) {
615 removeElem(element);
616 element.node = null;
617 }
618 if (this.__state === BREAK || target === BREAK) {
619 return outer.root;
620 }
621
622 // node may be null
623 node = element.node;
624 if (!node) {
625 continue;
626 }
627 worklist.push(sentinel);
628 leavelist.push(element);
629 if (this.__state === SKIP || target === SKIP) {
630 continue;
631 }
632 nodeType = node.type || element.wrap;
633 candidates = this.__keys[nodeType];
634 if (!candidates) {
635 if (this.__fallback) {
636 candidates = this.__fallback(node);
637 } else {
638 throw new Error('Unknown node type ' + nodeType + '.');
639 }
640 }
641 current = candidates.length;
642 while ((current -= 1) >= 0) {
643 key = candidates[current];
644 candidate = node[key];
645 if (!candidate) {
646 continue;
647 }
648 if (Array.isArray(candidate)) {
649 current2 = candidate.length;
650 while ((current2 -= 1) >= 0) {
651 if (!candidate[current2]) {
652 continue;
653 }
654 if (isProperty(nodeType, candidates[current])) {
655 element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2));
656 } else if (isNode(candidate[current2])) {
657 element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2));
658 } else {
659 continue;
660 }
661 worklist.push(element);
662 }
663 } else if (isNode(candidate)) {
664 worklist.push(new Element(candidate, key, null, new Reference(node, key)));
665 }
666 }
667 }
668 return outer.root;
669 };
670 function traverse(root, visitor) {
671 var controller = new Controller();
672 return controller.traverse(root, visitor);
673 }
674 function replace(root, visitor) {
675 var controller = new Controller();
676 return controller.replace(root, visitor);
677 }
678 function extendCommentRange(comment, tokens) {
679 var target;
680 target = upperBound(tokens, function search(token) {
681 return token.range[0] > comment.range[0];
682 });
683 comment.extendedRange = [comment.range[0], comment.range[1]];
684 if (target !== tokens.length) {
685 comment.extendedRange[1] = tokens[target].range[0];
686 }
687 target -= 1;
688 if (target >= 0) {
689 comment.extendedRange[0] = tokens[target].range[1];
690 }
691 return comment;
692 }
693 function attachComments(tree, providedComments, tokens) {
694 // At first, we should calculate extended comment ranges.
695 var comments = [],
696 comment,
697 len,
698 i,
699 cursor;
700 if (!tree.range) {
701 throw new Error('attachComments needs range information');
702 }
703
704 // tokens array is empty, we attach comments to tree as 'leadingComments'
705 if (!tokens.length) {
706 if (providedComments.length) {
707 for (i = 0, len = providedComments.length; i < len; i += 1) {
708 comment = deepCopy(providedComments[i]);
709 comment.extendedRange = [0, tree.range[0]];
710 comments.push(comment);
711 }
712 tree.leadingComments = comments;
713 }
714 return tree;
715 }
716 for (i = 0, len = providedComments.length; i < len; i += 1) {
717 comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));
718 }
719
720 // This is based on John Freeman's implementation.
721 cursor = 0;
722 traverse(tree, {
723 enter: function (node) {
724 var comment;
725 while (cursor < comments.length) {
726 comment = comments[cursor];
727 if (comment.extendedRange[1] > node.range[0]) {
728 break;
729 }
730 if (comment.extendedRange[1] === node.range[0]) {
731 if (!node.leadingComments) {
732 node.leadingComments = [];
733 }
734 node.leadingComments.push(comment);
735 comments.splice(cursor, 1);
736 } else {
737 cursor += 1;
738 }
739 }
740
741 // already out of owned node
742 if (cursor === comments.length) {
743 return VisitorOption.Break;
744 }
745 if (comments[cursor].extendedRange[0] > node.range[1]) {
746 return VisitorOption.Skip;
747 }
748 }
749 });
750 cursor = 0;
751 traverse(tree, {
752 leave: function (node) {
753 var comment;
754 while (cursor < comments.length) {
755 comment = comments[cursor];
756 if (node.range[1] < comment.extendedRange[0]) {
757 break;
758 }
759 if (node.range[1] === comment.extendedRange[0]) {
760 if (!node.trailingComments) {
761 node.trailingComments = [];
762 }
763 node.trailingComments.push(comment);
764 comments.splice(cursor, 1);
765 } else {
766 cursor += 1;
767 }
768 }
769
770 // already out of owned node
771 if (cursor === comments.length) {
772 return VisitorOption.Break;
773 }
774 if (comments[cursor].extendedRange[0] > node.range[1]) {
775 return VisitorOption.Skip;
776 }
777 }
778 });
779 return tree;
780 }
781 exports.Syntax = Syntax;
782 exports.traverse = traverse;
783 exports.replace = replace;
784 exports.attachComments = attachComments;
785 exports.VisitorKeys = VisitorKeys;
786 exports.VisitorOption = VisitorOption;
787 exports.Controller = Controller;
788 exports.cloneEnvironment = function () {
789 return clone({});
790 };
791 return exports;
792 })(exports);
793 /* vim: set sw=4 ts=4 et tw=80 : */
794 });
795
796 var parser = createCommonjsModule(function (module) {
797 /*
798 * Generated by PEG.js 0.10.0.
799 *
800 * http://pegjs.org/
801 */
802 (function (root, factory) {
803 if ( module.exports) {
804 module.exports = factory();
805 }
806 })(commonjsGlobal, function () {
807
808 function peg$subclass(child, parent) {
809 function ctor() {
810 this.constructor = child;
811 }
812 ctor.prototype = parent.prototype;
813 child.prototype = new ctor();
814 }
815 function peg$SyntaxError(message, expected, found, location) {
816 this.message = message;
817 this.expected = expected;
818 this.found = found;
819 this.location = location;
820 this.name = "SyntaxError";
821 if (typeof Error.captureStackTrace === "function") {
822 Error.captureStackTrace(this, peg$SyntaxError);
823 }
824 }
825 peg$subclass(peg$SyntaxError, Error);
826 peg$SyntaxError.buildMessage = function (expected, found) {
827 var DESCRIBE_EXPECTATION_FNS = {
828 literal: function literal(expectation) {
829 return "\"" + literalEscape(expectation.text) + "\"";
830 },
831 "class": function _class(expectation) {
832 var escapedParts = "",
833 i;
834 for (i = 0; i < expectation.parts.length; i++) {
835 escapedParts += expectation.parts[i] instanceof Array ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) : classEscape(expectation.parts[i]);
836 }
837 return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
838 },
839 any: function any(expectation) {
840 return "any character";
841 },
842 end: function end(expectation) {
843 return "end of input";
844 },
845 other: function other(expectation) {
846 return expectation.description;
847 }
848 };
849 function hex(ch) {
850 return ch.charCodeAt(0).toString(16).toUpperCase();
851 }
852 function literalEscape(s) {
853 return s.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\0/g, '\\0').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/[\x00-\x0F]/g, function (ch) {
854 return '\\x0' + hex(ch);
855 }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
856 return '\\x' + hex(ch);
857 });
858 }
859 function classEscape(s) {
860 return s.replace(/\\/g, '\\\\').replace(/\]/g, '\\]').replace(/\^/g, '\\^').replace(/-/g, '\\-').replace(/\0/g, '\\0').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/[\x00-\x0F]/g, function (ch) {
861 return '\\x0' + hex(ch);
862 }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
863 return '\\x' + hex(ch);
864 });
865 }
866 function describeExpectation(expectation) {
867 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
868 }
869 function describeExpected(expected) {
870 var descriptions = new Array(expected.length),
871 i,
872 j;
873 for (i = 0; i < expected.length; i++) {
874 descriptions[i] = describeExpectation(expected[i]);
875 }
876 descriptions.sort();
877 if (descriptions.length > 0) {
878 for (i = 1, j = 1; i < descriptions.length; i++) {
879 if (descriptions[i - 1] !== descriptions[i]) {
880 descriptions[j] = descriptions[i];
881 j++;
882 }
883 }
884 descriptions.length = j;
885 }
886 switch (descriptions.length) {
887 case 1:
888 return descriptions[0];
889 case 2:
890 return descriptions[0] + " or " + descriptions[1];
891 default:
892 return descriptions.slice(0, -1).join(", ") + ", or " + descriptions[descriptions.length - 1];
893 }
894 }
895 function describeFound(found) {
896 return found ? "\"" + literalEscape(found) + "\"" : "end of input";
897 }
898 return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
899 };
900 function peg$parse(input, options) {
901 options = options !== void 0 ? options : {};
902 var peg$FAILED = {},
903 peg$startRuleFunctions = {
904 start: peg$parsestart
905 },
906 peg$startRuleFunction = peg$parsestart,
907 peg$c0 = function peg$c0(ss) {
908 return ss.length === 1 ? ss[0] : {
909 type: 'matches',
910 selectors: ss
911 };
912 },
913 peg$c1 = function peg$c1() {
914 return void 0;
915 },
916 peg$c2 = " ",
917 peg$c3 = peg$literalExpectation(" ", false),
918 peg$c4 = /^[^ [\],():#!=><~+.]/,
919 peg$c5 = peg$classExpectation([" ", "[", "]", ",", "(", ")", ":", "#", "!", "=", ">", "<", "~", "+", "."], true, false),
920 peg$c6 = function peg$c6(i) {
921 return i.join('');
922 },
923 peg$c7 = ">",
924 peg$c8 = peg$literalExpectation(">", false),
925 peg$c9 = function peg$c9() {
926 return 'child';
927 },
928 peg$c10 = "~",
929 peg$c11 = peg$literalExpectation("~", false),
930 peg$c12 = function peg$c12() {
931 return 'sibling';
932 },
933 peg$c13 = "+",
934 peg$c14 = peg$literalExpectation("+", false),
935 peg$c15 = function peg$c15() {
936 return 'adjacent';
937 },
938 peg$c16 = function peg$c16() {
939 return 'descendant';
940 },
941 peg$c17 = ",",
942 peg$c18 = peg$literalExpectation(",", false),
943 peg$c19 = function peg$c19(s, ss) {
944 return [s].concat(ss.map(function (s) {
945 return s[3];
946 }));
947 },
948 peg$c20 = function peg$c20(op, s) {
949 if (!op) return s;
950 return {
951 type: op,
952 left: {
953 type: 'exactNode'
954 },
955 right: s
956 };
957 },
958 peg$c21 = function peg$c21(a, ops) {
959 return ops.reduce(function (memo, rhs) {
960 return {
961 type: rhs[0],
962 left: memo,
963 right: rhs[1]
964 };
965 }, a);
966 },
967 peg$c22 = "!",
968 peg$c23 = peg$literalExpectation("!", false),
969 peg$c24 = function peg$c24(subject, as) {
970 var b = as.length === 1 ? as[0] : {
971 type: 'compound',
972 selectors: as
973 };
974 if (subject) b.subject = true;
975 return b;
976 },
977 peg$c25 = "*",
978 peg$c26 = peg$literalExpectation("*", false),
979 peg$c27 = function peg$c27(a) {
980 return {
981 type: 'wildcard',
982 value: a
983 };
984 },
985 peg$c28 = "#",
986 peg$c29 = peg$literalExpectation("#", false),
987 peg$c30 = function peg$c30(i) {
988 return {
989 type: 'identifier',
990 value: i
991 };
992 },
993 peg$c31 = "[",
994 peg$c32 = peg$literalExpectation("[", false),
995 peg$c33 = "]",
996 peg$c34 = peg$literalExpectation("]", false),
997 peg$c35 = function peg$c35(v) {
998 return v;
999 },
1000 peg$c36 = /^[><!]/,
1001 peg$c37 = peg$classExpectation([">", "<", "!"], false, false),
1002 peg$c38 = "=",
1003 peg$c39 = peg$literalExpectation("=", false),
1004 peg$c40 = function peg$c40(a) {
1005 return (a || '') + '=';
1006 },
1007 peg$c41 = /^[><]/,
1008 peg$c42 = peg$classExpectation([">", "<"], false, false),
1009 peg$c43 = ".",
1010 peg$c44 = peg$literalExpectation(".", false),
1011 peg$c45 = function peg$c45(a, as) {
1012 return [].concat.apply([a], as).join('');
1013 },
1014 peg$c46 = function peg$c46(name, op, value) {
1015 return {
1016 type: 'attribute',
1017 name: name,
1018 operator: op,
1019 value: value
1020 };
1021 },
1022 peg$c47 = function peg$c47(name) {
1023 return {
1024 type: 'attribute',
1025 name: name
1026 };
1027 },
1028 peg$c48 = "\"",
1029 peg$c49 = peg$literalExpectation("\"", false),
1030 peg$c50 = /^[^\\"]/,
1031 peg$c51 = peg$classExpectation(["\\", "\""], true, false),
1032 peg$c52 = "\\",
1033 peg$c53 = peg$literalExpectation("\\", false),
1034 peg$c54 = peg$anyExpectation(),
1035 peg$c55 = function peg$c55(a, b) {
1036 return a + b;
1037 },
1038 peg$c56 = function peg$c56(d) {
1039 return {
1040 type: 'literal',
1041 value: strUnescape(d.join(''))
1042 };
1043 },
1044 peg$c57 = "'",
1045 peg$c58 = peg$literalExpectation("'", false),
1046 peg$c59 = /^[^\\']/,
1047 peg$c60 = peg$classExpectation(["\\", "'"], true, false),
1048 peg$c61 = /^[0-9]/,
1049 peg$c62 = peg$classExpectation([["0", "9"]], false, false),
1050 peg$c63 = function peg$c63(a, b) {
1051 // Can use `a.flat().join('')` once supported
1052 var leadingDecimals = a ? [].concat.apply([], a).join('') : '';
1053 return {
1054 type: 'literal',
1055 value: parseFloat(leadingDecimals + b.join(''))
1056 };
1057 },
1058 peg$c64 = function peg$c64(i) {
1059 return {
1060 type: 'literal',
1061 value: i
1062 };
1063 },
1064 peg$c65 = "type(",
1065 peg$c66 = peg$literalExpectation("type(", false),
1066 peg$c67 = /^[^ )]/,
1067 peg$c68 = peg$classExpectation([" ", ")"], true, false),
1068 peg$c69 = ")",
1069 peg$c70 = peg$literalExpectation(")", false),
1070 peg$c71 = function peg$c71(t) {
1071 return {
1072 type: 'type',
1073 value: t.join('')
1074 };
1075 },
1076 peg$c72 = /^[imsu]/,
1077 peg$c73 = peg$classExpectation(["i", "m", "s", "u"], false, false),
1078 peg$c74 = "/",
1079 peg$c75 = peg$literalExpectation("/", false),
1080 peg$c76 = /^[^\/]/,
1081 peg$c77 = peg$classExpectation(["/"], true, false),
1082 peg$c78 = function peg$c78(d, flgs) {
1083 return {
1084 type: 'regexp',
1085 value: new RegExp(d.join(''), flgs ? flgs.join('') : '')
1086 };
1087 },
1088 peg$c79 = function peg$c79(i, is) {
1089 return {
1090 type: 'field',
1091 name: is.reduce(function (memo, p) {
1092 return memo + p[0] + p[1];
1093 }, i)
1094 };
1095 },
1096 peg$c80 = ":not(",
1097 peg$c81 = peg$literalExpectation(":not(", false),
1098 peg$c82 = function peg$c82(ss) {
1099 return {
1100 type: 'not',
1101 selectors: ss
1102 };
1103 },
1104 peg$c83 = ":matches(",
1105 peg$c84 = peg$literalExpectation(":matches(", false),
1106 peg$c85 = function peg$c85(ss) {
1107 return {
1108 type: 'matches',
1109 selectors: ss
1110 };
1111 },
1112 peg$c86 = ":has(",
1113 peg$c87 = peg$literalExpectation(":has(", false),
1114 peg$c88 = function peg$c88(ss) {
1115 return {
1116 type: 'has',
1117 selectors: ss
1118 };
1119 },
1120 peg$c89 = ":first-child",
1121 peg$c90 = peg$literalExpectation(":first-child", false),
1122 peg$c91 = function peg$c91() {
1123 return nth(1);
1124 },
1125 peg$c92 = ":last-child",
1126 peg$c93 = peg$literalExpectation(":last-child", false),
1127 peg$c94 = function peg$c94() {
1128 return nthLast(1);
1129 },
1130 peg$c95 = ":nth-child(",
1131 peg$c96 = peg$literalExpectation(":nth-child(", false),
1132 peg$c97 = function peg$c97(n) {
1133 return nth(parseInt(n.join(''), 10));
1134 },
1135 peg$c98 = ":nth-last-child(",
1136 peg$c99 = peg$literalExpectation(":nth-last-child(", false),
1137 peg$c100 = function peg$c100(n) {
1138 return nthLast(parseInt(n.join(''), 10));
1139 },
1140 peg$c101 = ":",
1141 peg$c102 = peg$literalExpectation(":", false),
1142 peg$c103 = function peg$c103(c) {
1143 return {
1144 type: 'class',
1145 name: c
1146 };
1147 },
1148 peg$currPos = 0,
1149 peg$posDetailsCache = [{
1150 line: 1,
1151 column: 1
1152 }],
1153 peg$maxFailPos = 0,
1154 peg$maxFailExpected = [],
1155 peg$resultsCache = {},
1156 peg$result;
1157 if ("startRule" in options) {
1158 if (!(options.startRule in peg$startRuleFunctions)) {
1159 throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
1160 }
1161 peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
1162 }
1163 function peg$literalExpectation(text, ignoreCase) {
1164 return {
1165 type: "literal",
1166 text: text,
1167 ignoreCase: ignoreCase
1168 };
1169 }
1170 function peg$classExpectation(parts, inverted, ignoreCase) {
1171 return {
1172 type: "class",
1173 parts: parts,
1174 inverted: inverted,
1175 ignoreCase: ignoreCase
1176 };
1177 }
1178 function peg$anyExpectation() {
1179 return {
1180 type: "any"
1181 };
1182 }
1183 function peg$endExpectation() {
1184 return {
1185 type: "end"
1186 };
1187 }
1188 function peg$computePosDetails(pos) {
1189 var details = peg$posDetailsCache[pos],
1190 p;
1191 if (details) {
1192 return details;
1193 } else {
1194 p = pos - 1;
1195 while (!peg$posDetailsCache[p]) {
1196 p--;
1197 }
1198 details = peg$posDetailsCache[p];
1199 details = {
1200 line: details.line,
1201 column: details.column
1202 };
1203 while (p < pos) {
1204 if (input.charCodeAt(p) === 10) {
1205 details.line++;
1206 details.column = 1;
1207 } else {
1208 details.column++;
1209 }
1210 p++;
1211 }
1212 peg$posDetailsCache[pos] = details;
1213 return details;
1214 }
1215 }
1216 function peg$computeLocation(startPos, endPos) {
1217 var startPosDetails = peg$computePosDetails(startPos),
1218 endPosDetails = peg$computePosDetails(endPos);
1219 return {
1220 start: {
1221 offset: startPos,
1222 line: startPosDetails.line,
1223 column: startPosDetails.column
1224 },
1225 end: {
1226 offset: endPos,
1227 line: endPosDetails.line,
1228 column: endPosDetails.column
1229 }
1230 };
1231 }
1232 function peg$fail(expected) {
1233 if (peg$currPos < peg$maxFailPos) {
1234 return;
1235 }
1236 if (peg$currPos > peg$maxFailPos) {
1237 peg$maxFailPos = peg$currPos;
1238 peg$maxFailExpected = [];
1239 }
1240 peg$maxFailExpected.push(expected);
1241 }
1242 function peg$buildStructuredError(expected, found, location) {
1243 return new peg$SyntaxError(peg$SyntaxError.buildMessage(expected, found), expected, found, location);
1244 }
1245 function peg$parsestart() {
1246 var s0, s1, s2, s3;
1247 var key = peg$currPos * 32 + 0,
1248 cached = peg$resultsCache[key];
1249 if (cached) {
1250 peg$currPos = cached.nextPos;
1251 return cached.result;
1252 }
1253 s0 = peg$currPos;
1254 s1 = peg$parse_();
1255 if (s1 !== peg$FAILED) {
1256 s2 = peg$parseselectors();
1257 if (s2 !== peg$FAILED) {
1258 s3 = peg$parse_();
1259 if (s3 !== peg$FAILED) {
1260 s1 = peg$c0(s2);
1261 s0 = s1;
1262 } else {
1263 peg$currPos = s0;
1264 s0 = peg$FAILED;
1265 }
1266 } else {
1267 peg$currPos = s0;
1268 s0 = peg$FAILED;
1269 }
1270 } else {
1271 peg$currPos = s0;
1272 s0 = peg$FAILED;
1273 }
1274 if (s0 === peg$FAILED) {
1275 s0 = peg$currPos;
1276 s1 = peg$parse_();
1277 if (s1 !== peg$FAILED) {
1278 s1 = peg$c1();
1279 }
1280 s0 = s1;
1281 }
1282 peg$resultsCache[key] = {
1283 nextPos: peg$currPos,
1284 result: s0
1285 };
1286 return s0;
1287 }
1288 function peg$parse_() {
1289 var s0, s1;
1290 var key = peg$currPos * 32 + 1,
1291 cached = peg$resultsCache[key];
1292 if (cached) {
1293 peg$currPos = cached.nextPos;
1294 return cached.result;
1295 }
1296 s0 = [];
1297 if (input.charCodeAt(peg$currPos) === 32) {
1298 s1 = peg$c2;
1299 peg$currPos++;
1300 } else {
1301 s1 = peg$FAILED;
1302 {
1303 peg$fail(peg$c3);
1304 }
1305 }
1306 while (s1 !== peg$FAILED) {
1307 s0.push(s1);
1308 if (input.charCodeAt(peg$currPos) === 32) {
1309 s1 = peg$c2;
1310 peg$currPos++;
1311 } else {
1312 s1 = peg$FAILED;
1313 {
1314 peg$fail(peg$c3);
1315 }
1316 }
1317 }
1318 peg$resultsCache[key] = {
1319 nextPos: peg$currPos,
1320 result: s0
1321 };
1322 return s0;
1323 }
1324 function peg$parseidentifierName() {
1325 var s0, s1, s2;
1326 var key = peg$currPos * 32 + 2,
1327 cached = peg$resultsCache[key];
1328 if (cached) {
1329 peg$currPos = cached.nextPos;
1330 return cached.result;
1331 }
1332 s0 = peg$currPos;
1333 s1 = [];
1334 if (peg$c4.test(input.charAt(peg$currPos))) {
1335 s2 = input.charAt(peg$currPos);
1336 peg$currPos++;
1337 } else {
1338 s2 = peg$FAILED;
1339 {
1340 peg$fail(peg$c5);
1341 }
1342 }
1343 if (s2 !== peg$FAILED) {
1344 while (s2 !== peg$FAILED) {
1345 s1.push(s2);
1346 if (peg$c4.test(input.charAt(peg$currPos))) {
1347 s2 = input.charAt(peg$currPos);
1348 peg$currPos++;
1349 } else {
1350 s2 = peg$FAILED;
1351 {
1352 peg$fail(peg$c5);
1353 }
1354 }
1355 }
1356 } else {
1357 s1 = peg$FAILED;
1358 }
1359 if (s1 !== peg$FAILED) {
1360 s1 = peg$c6(s1);
1361 }
1362 s0 = s1;
1363 peg$resultsCache[key] = {
1364 nextPos: peg$currPos,
1365 result: s0
1366 };
1367 return s0;
1368 }
1369 function peg$parsebinaryOp() {
1370 var s0, s1, s2, s3;
1371 var key = peg$currPos * 32 + 3,
1372 cached = peg$resultsCache[key];
1373 if (cached) {
1374 peg$currPos = cached.nextPos;
1375 return cached.result;
1376 }
1377 s0 = peg$currPos;
1378 s1 = peg$parse_();
1379 if (s1 !== peg$FAILED) {
1380 if (input.charCodeAt(peg$currPos) === 62) {
1381 s2 = peg$c7;
1382 peg$currPos++;
1383 } else {
1384 s2 = peg$FAILED;
1385 {
1386 peg$fail(peg$c8);
1387 }
1388 }
1389 if (s2 !== peg$FAILED) {
1390 s3 = peg$parse_();
1391 if (s3 !== peg$FAILED) {
1392 s1 = peg$c9();
1393 s0 = s1;
1394 } else {
1395 peg$currPos = s0;
1396 s0 = peg$FAILED;
1397 }
1398 } else {
1399 peg$currPos = s0;
1400 s0 = peg$FAILED;
1401 }
1402 } else {
1403 peg$currPos = s0;
1404 s0 = peg$FAILED;
1405 }
1406 if (s0 === peg$FAILED) {
1407 s0 = peg$currPos;
1408 s1 = peg$parse_();
1409 if (s1 !== peg$FAILED) {
1410 if (input.charCodeAt(peg$currPos) === 126) {
1411 s2 = peg$c10;
1412 peg$currPos++;
1413 } else {
1414 s2 = peg$FAILED;
1415 {
1416 peg$fail(peg$c11);
1417 }
1418 }
1419 if (s2 !== peg$FAILED) {
1420 s3 = peg$parse_();
1421 if (s3 !== peg$FAILED) {
1422 s1 = peg$c12();
1423 s0 = s1;
1424 } else {
1425 peg$currPos = s0;
1426 s0 = peg$FAILED;
1427 }
1428 } else {
1429 peg$currPos = s0;
1430 s0 = peg$FAILED;
1431 }
1432 } else {
1433 peg$currPos = s0;
1434 s0 = peg$FAILED;
1435 }
1436 if (s0 === peg$FAILED) {
1437 s0 = peg$currPos;
1438 s1 = peg$parse_();
1439 if (s1 !== peg$FAILED) {
1440 if (input.charCodeAt(peg$currPos) === 43) {
1441 s2 = peg$c13;
1442 peg$currPos++;
1443 } else {
1444 s2 = peg$FAILED;
1445 {
1446 peg$fail(peg$c14);
1447 }
1448 }
1449 if (s2 !== peg$FAILED) {
1450 s3 = peg$parse_();
1451 if (s3 !== peg$FAILED) {
1452 s1 = peg$c15();
1453 s0 = s1;
1454 } else {
1455 peg$currPos = s0;
1456 s0 = peg$FAILED;
1457 }
1458 } else {
1459 peg$currPos = s0;
1460 s0 = peg$FAILED;
1461 }
1462 } else {
1463 peg$currPos = s0;
1464 s0 = peg$FAILED;
1465 }
1466 if (s0 === peg$FAILED) {
1467 s0 = peg$currPos;
1468 if (input.charCodeAt(peg$currPos) === 32) {
1469 s1 = peg$c2;
1470 peg$currPos++;
1471 } else {
1472 s1 = peg$FAILED;
1473 {
1474 peg$fail(peg$c3);
1475 }
1476 }
1477 if (s1 !== peg$FAILED) {
1478 s2 = peg$parse_();
1479 if (s2 !== peg$FAILED) {
1480 s1 = peg$c16();
1481 s0 = s1;
1482 } else {
1483 peg$currPos = s0;
1484 s0 = peg$FAILED;
1485 }
1486 } else {
1487 peg$currPos = s0;
1488 s0 = peg$FAILED;
1489 }
1490 }
1491 }
1492 }
1493 peg$resultsCache[key] = {
1494 nextPos: peg$currPos,
1495 result: s0
1496 };
1497 return s0;
1498 }
1499 function peg$parsehasSelectors() {
1500 var s0, s1, s2, s3, s4, s5, s6, s7;
1501 var key = peg$currPos * 32 + 4,
1502 cached = peg$resultsCache[key];
1503 if (cached) {
1504 peg$currPos = cached.nextPos;
1505 return cached.result;
1506 }
1507 s0 = peg$currPos;
1508 s1 = peg$parsehasSelector();
1509 if (s1 !== peg$FAILED) {
1510 s2 = [];
1511 s3 = peg$currPos;
1512 s4 = peg$parse_();
1513 if (s4 !== peg$FAILED) {
1514 if (input.charCodeAt(peg$currPos) === 44) {
1515 s5 = peg$c17;
1516 peg$currPos++;
1517 } else {
1518 s5 = peg$FAILED;
1519 {
1520 peg$fail(peg$c18);
1521 }
1522 }
1523 if (s5 !== peg$FAILED) {
1524 s6 = peg$parse_();
1525 if (s6 !== peg$FAILED) {
1526 s7 = peg$parsehasSelector();
1527 if (s7 !== peg$FAILED) {
1528 s4 = [s4, s5, s6, s7];
1529 s3 = s4;
1530 } else {
1531 peg$currPos = s3;
1532 s3 = peg$FAILED;
1533 }
1534 } else {
1535 peg$currPos = s3;
1536 s3 = peg$FAILED;
1537 }
1538 } else {
1539 peg$currPos = s3;
1540 s3 = peg$FAILED;
1541 }
1542 } else {
1543 peg$currPos = s3;
1544 s3 = peg$FAILED;
1545 }
1546 while (s3 !== peg$FAILED) {
1547 s2.push(s3);
1548 s3 = peg$currPos;
1549 s4 = peg$parse_();
1550 if (s4 !== peg$FAILED) {
1551 if (input.charCodeAt(peg$currPos) === 44) {
1552 s5 = peg$c17;
1553 peg$currPos++;
1554 } else {
1555 s5 = peg$FAILED;
1556 {
1557 peg$fail(peg$c18);
1558 }
1559 }
1560 if (s5 !== peg$FAILED) {
1561 s6 = peg$parse_();
1562 if (s6 !== peg$FAILED) {
1563 s7 = peg$parsehasSelector();
1564 if (s7 !== peg$FAILED) {
1565 s4 = [s4, s5, s6, s7];
1566 s3 = s4;
1567 } else {
1568 peg$currPos = s3;
1569 s3 = peg$FAILED;
1570 }
1571 } else {
1572 peg$currPos = s3;
1573 s3 = peg$FAILED;
1574 }
1575 } else {
1576 peg$currPos = s3;
1577 s3 = peg$FAILED;
1578 }
1579 } else {
1580 peg$currPos = s3;
1581 s3 = peg$FAILED;
1582 }
1583 }
1584 if (s2 !== peg$FAILED) {
1585 s1 = peg$c19(s1, s2);
1586 s0 = s1;
1587 } else {
1588 peg$currPos = s0;
1589 s0 = peg$FAILED;
1590 }
1591 } else {
1592 peg$currPos = s0;
1593 s0 = peg$FAILED;
1594 }
1595 peg$resultsCache[key] = {
1596 nextPos: peg$currPos,
1597 result: s0
1598 };
1599 return s0;
1600 }
1601 function peg$parseselectors() {
1602 var s0, s1, s2, s3, s4, s5, s6, s7;
1603 var key = peg$currPos * 32 + 5,
1604 cached = peg$resultsCache[key];
1605 if (cached) {
1606 peg$currPos = cached.nextPos;
1607 return cached.result;
1608 }
1609 s0 = peg$currPos;
1610 s1 = peg$parseselector();
1611 if (s1 !== peg$FAILED) {
1612 s2 = [];
1613 s3 = peg$currPos;
1614 s4 = peg$parse_();
1615 if (s4 !== peg$FAILED) {
1616 if (input.charCodeAt(peg$currPos) === 44) {
1617 s5 = peg$c17;
1618 peg$currPos++;
1619 } else {
1620 s5 = peg$FAILED;
1621 {
1622 peg$fail(peg$c18);
1623 }
1624 }
1625 if (s5 !== peg$FAILED) {
1626 s6 = peg$parse_();
1627 if (s6 !== peg$FAILED) {
1628 s7 = peg$parseselector();
1629 if (s7 !== peg$FAILED) {
1630 s4 = [s4, s5, s6, s7];
1631 s3 = s4;
1632 } else {
1633 peg$currPos = s3;
1634 s3 = peg$FAILED;
1635 }
1636 } else {
1637 peg$currPos = s3;
1638 s3 = peg$FAILED;
1639 }
1640 } else {
1641 peg$currPos = s3;
1642 s3 = peg$FAILED;
1643 }
1644 } else {
1645 peg$currPos = s3;
1646 s3 = peg$FAILED;
1647 }
1648 while (s3 !== peg$FAILED) {
1649 s2.push(s3);
1650 s3 = peg$currPos;
1651 s4 = peg$parse_();
1652 if (s4 !== peg$FAILED) {
1653 if (input.charCodeAt(peg$currPos) === 44) {
1654 s5 = peg$c17;
1655 peg$currPos++;
1656 } else {
1657 s5 = peg$FAILED;
1658 {
1659 peg$fail(peg$c18);
1660 }
1661 }
1662 if (s5 !== peg$FAILED) {
1663 s6 = peg$parse_();
1664 if (s6 !== peg$FAILED) {
1665 s7 = peg$parseselector();
1666 if (s7 !== peg$FAILED) {
1667 s4 = [s4, s5, s6, s7];
1668 s3 = s4;
1669 } else {
1670 peg$currPos = s3;
1671 s3 = peg$FAILED;
1672 }
1673 } else {
1674 peg$currPos = s3;
1675 s3 = peg$FAILED;
1676 }
1677 } else {
1678 peg$currPos = s3;
1679 s3 = peg$FAILED;
1680 }
1681 } else {
1682 peg$currPos = s3;
1683 s3 = peg$FAILED;
1684 }
1685 }
1686 if (s2 !== peg$FAILED) {
1687 s1 = peg$c19(s1, s2);
1688 s0 = s1;
1689 } else {
1690 peg$currPos = s0;
1691 s0 = peg$FAILED;
1692 }
1693 } else {
1694 peg$currPos = s0;
1695 s0 = peg$FAILED;
1696 }
1697 peg$resultsCache[key] = {
1698 nextPos: peg$currPos,
1699 result: s0
1700 };
1701 return s0;
1702 }
1703 function peg$parsehasSelector() {
1704 var s0, s1, s2;
1705 var key = peg$currPos * 32 + 6,
1706 cached = peg$resultsCache[key];
1707 if (cached) {
1708 peg$currPos = cached.nextPos;
1709 return cached.result;
1710 }
1711 s0 = peg$currPos;
1712 s1 = peg$parsebinaryOp();
1713 if (s1 === peg$FAILED) {
1714 s1 = null;
1715 }
1716 if (s1 !== peg$FAILED) {
1717 s2 = peg$parseselector();
1718 if (s2 !== peg$FAILED) {
1719 s1 = peg$c20(s1, s2);
1720 s0 = s1;
1721 } else {
1722 peg$currPos = s0;
1723 s0 = peg$FAILED;
1724 }
1725 } else {
1726 peg$currPos = s0;
1727 s0 = peg$FAILED;
1728 }
1729 peg$resultsCache[key] = {
1730 nextPos: peg$currPos,
1731 result: s0
1732 };
1733 return s0;
1734 }
1735 function peg$parseselector() {
1736 var s0, s1, s2, s3, s4, s5;
1737 var key = peg$currPos * 32 + 7,
1738 cached = peg$resultsCache[key];
1739 if (cached) {
1740 peg$currPos = cached.nextPos;
1741 return cached.result;
1742 }
1743 s0 = peg$currPos;
1744 s1 = peg$parsesequence();
1745 if (s1 !== peg$FAILED) {
1746 s2 = [];
1747 s3 = peg$currPos;
1748 s4 = peg$parsebinaryOp();
1749 if (s4 !== peg$FAILED) {
1750 s5 = peg$parsesequence();
1751 if (s5 !== peg$FAILED) {
1752 s4 = [s4, s5];
1753 s3 = s4;
1754 } else {
1755 peg$currPos = s3;
1756 s3 = peg$FAILED;
1757 }
1758 } else {
1759 peg$currPos = s3;
1760 s3 = peg$FAILED;
1761 }
1762 while (s3 !== peg$FAILED) {
1763 s2.push(s3);
1764 s3 = peg$currPos;
1765 s4 = peg$parsebinaryOp();
1766 if (s4 !== peg$FAILED) {
1767 s5 = peg$parsesequence();
1768 if (s5 !== peg$FAILED) {
1769 s4 = [s4, s5];
1770 s3 = s4;
1771 } else {
1772 peg$currPos = s3;
1773 s3 = peg$FAILED;
1774 }
1775 } else {
1776 peg$currPos = s3;
1777 s3 = peg$FAILED;
1778 }
1779 }
1780 if (s2 !== peg$FAILED) {
1781 s1 = peg$c21(s1, s2);
1782 s0 = s1;
1783 } else {
1784 peg$currPos = s0;
1785 s0 = peg$FAILED;
1786 }
1787 } else {
1788 peg$currPos = s0;
1789 s0 = peg$FAILED;
1790 }
1791 peg$resultsCache[key] = {
1792 nextPos: peg$currPos,
1793 result: s0
1794 };
1795 return s0;
1796 }
1797 function peg$parsesequence() {
1798 var s0, s1, s2, s3;
1799 var key = peg$currPos * 32 + 8,
1800 cached = peg$resultsCache[key];
1801 if (cached) {
1802 peg$currPos = cached.nextPos;
1803 return cached.result;
1804 }
1805 s0 = peg$currPos;
1806 if (input.charCodeAt(peg$currPos) === 33) {
1807 s1 = peg$c22;
1808 peg$currPos++;
1809 } else {
1810 s1 = peg$FAILED;
1811 {
1812 peg$fail(peg$c23);
1813 }
1814 }
1815 if (s1 === peg$FAILED) {
1816 s1 = null;
1817 }
1818 if (s1 !== peg$FAILED) {
1819 s2 = [];
1820 s3 = peg$parseatom();
1821 if (s3 !== peg$FAILED) {
1822 while (s3 !== peg$FAILED) {
1823 s2.push(s3);
1824 s3 = peg$parseatom();
1825 }
1826 } else {
1827 s2 = peg$FAILED;
1828 }
1829 if (s2 !== peg$FAILED) {
1830 s1 = peg$c24(s1, s2);
1831 s0 = s1;
1832 } else {
1833 peg$currPos = s0;
1834 s0 = peg$FAILED;
1835 }
1836 } else {
1837 peg$currPos = s0;
1838 s0 = peg$FAILED;
1839 }
1840 peg$resultsCache[key] = {
1841 nextPos: peg$currPos,
1842 result: s0
1843 };
1844 return s0;
1845 }
1846 function peg$parseatom() {
1847 var s0;
1848 var key = peg$currPos * 32 + 9,
1849 cached = peg$resultsCache[key];
1850 if (cached) {
1851 peg$currPos = cached.nextPos;
1852 return cached.result;
1853 }
1854 s0 = peg$parsewildcard();
1855 if (s0 === peg$FAILED) {
1856 s0 = peg$parseidentifier();
1857 if (s0 === peg$FAILED) {
1858 s0 = peg$parseattr();
1859 if (s0 === peg$FAILED) {
1860 s0 = peg$parsefield();
1861 if (s0 === peg$FAILED) {
1862 s0 = peg$parsenegation();
1863 if (s0 === peg$FAILED) {
1864 s0 = peg$parsematches();
1865 if (s0 === peg$FAILED) {
1866 s0 = peg$parsehas();
1867 if (s0 === peg$FAILED) {
1868 s0 = peg$parsefirstChild();
1869 if (s0 === peg$FAILED) {
1870 s0 = peg$parselastChild();
1871 if (s0 === peg$FAILED) {
1872 s0 = peg$parsenthChild();
1873 if (s0 === peg$FAILED) {
1874 s0 = peg$parsenthLastChild();
1875 if (s0 === peg$FAILED) {
1876 s0 = peg$parseclass();
1877 }
1878 }
1879 }
1880 }
1881 }
1882 }
1883 }
1884 }
1885 }
1886 }
1887 }
1888 peg$resultsCache[key] = {
1889 nextPos: peg$currPos,
1890 result: s0
1891 };
1892 return s0;
1893 }
1894 function peg$parsewildcard() {
1895 var s0, s1;
1896 var key = peg$currPos * 32 + 10,
1897 cached = peg$resultsCache[key];
1898 if (cached) {
1899 peg$currPos = cached.nextPos;
1900 return cached.result;
1901 }
1902 s0 = peg$currPos;
1903 if (input.charCodeAt(peg$currPos) === 42) {
1904 s1 = peg$c25;
1905 peg$currPos++;
1906 } else {
1907 s1 = peg$FAILED;
1908 {
1909 peg$fail(peg$c26);
1910 }
1911 }
1912 if (s1 !== peg$FAILED) {
1913 s1 = peg$c27(s1);
1914 }
1915 s0 = s1;
1916 peg$resultsCache[key] = {
1917 nextPos: peg$currPos,
1918 result: s0
1919 };
1920 return s0;
1921 }
1922 function peg$parseidentifier() {
1923 var s0, s1, s2;
1924 var key = peg$currPos * 32 + 11,
1925 cached = peg$resultsCache[key];
1926 if (cached) {
1927 peg$currPos = cached.nextPos;
1928 return cached.result;
1929 }
1930 s0 = peg$currPos;
1931 if (input.charCodeAt(peg$currPos) === 35) {
1932 s1 = peg$c28;
1933 peg$currPos++;
1934 } else {
1935 s1 = peg$FAILED;
1936 {
1937 peg$fail(peg$c29);
1938 }
1939 }
1940 if (s1 === peg$FAILED) {
1941 s1 = null;
1942 }
1943 if (s1 !== peg$FAILED) {
1944 s2 = peg$parseidentifierName();
1945 if (s2 !== peg$FAILED) {
1946 s1 = peg$c30(s2);
1947 s0 = s1;
1948 } else {
1949 peg$currPos = s0;
1950 s0 = peg$FAILED;
1951 }
1952 } else {
1953 peg$currPos = s0;
1954 s0 = peg$FAILED;
1955 }
1956 peg$resultsCache[key] = {
1957 nextPos: peg$currPos,
1958 result: s0
1959 };
1960 return s0;
1961 }
1962 function peg$parseattr() {
1963 var s0, s1, s2, s3, s4, s5;
1964 var key = peg$currPos * 32 + 12,
1965 cached = peg$resultsCache[key];
1966 if (cached) {
1967 peg$currPos = cached.nextPos;
1968 return cached.result;
1969 }
1970 s0 = peg$currPos;
1971 if (input.charCodeAt(peg$currPos) === 91) {
1972 s1 = peg$c31;
1973 peg$currPos++;
1974 } else {
1975 s1 = peg$FAILED;
1976 {
1977 peg$fail(peg$c32);
1978 }
1979 }
1980 if (s1 !== peg$FAILED) {
1981 s2 = peg$parse_();
1982 if (s2 !== peg$FAILED) {
1983 s3 = peg$parseattrValue();
1984 if (s3 !== peg$FAILED) {
1985 s4 = peg$parse_();
1986 if (s4 !== peg$FAILED) {
1987 if (input.charCodeAt(peg$currPos) === 93) {
1988 s5 = peg$c33;
1989 peg$currPos++;
1990 } else {
1991 s5 = peg$FAILED;
1992 {
1993 peg$fail(peg$c34);
1994 }
1995 }
1996 if (s5 !== peg$FAILED) {
1997 s1 = peg$c35(s3);
1998 s0 = s1;
1999 } else {
2000 peg$currPos = s0;
2001 s0 = peg$FAILED;
2002 }
2003 } else {
2004 peg$currPos = s0;
2005 s0 = peg$FAILED;
2006 }
2007 } else {
2008 peg$currPos = s0;
2009 s0 = peg$FAILED;
2010 }
2011 } else {
2012 peg$currPos = s0;
2013 s0 = peg$FAILED;
2014 }
2015 } else {
2016 peg$currPos = s0;
2017 s0 = peg$FAILED;
2018 }
2019 peg$resultsCache[key] = {
2020 nextPos: peg$currPos,
2021 result: s0
2022 };
2023 return s0;
2024 }
2025 function peg$parseattrOps() {
2026 var s0, s1, s2;
2027 var key = peg$currPos * 32 + 13,
2028 cached = peg$resultsCache[key];
2029 if (cached) {
2030 peg$currPos = cached.nextPos;
2031 return cached.result;
2032 }
2033 s0 = peg$currPos;
2034 if (peg$c36.test(input.charAt(peg$currPos))) {
2035 s1 = input.charAt(peg$currPos);
2036 peg$currPos++;
2037 } else {
2038 s1 = peg$FAILED;
2039 {
2040 peg$fail(peg$c37);
2041 }
2042 }
2043 if (s1 === peg$FAILED) {
2044 s1 = null;
2045 }
2046 if (s1 !== peg$FAILED) {
2047 if (input.charCodeAt(peg$currPos) === 61) {
2048 s2 = peg$c38;
2049 peg$currPos++;
2050 } else {
2051 s2 = peg$FAILED;
2052 {
2053 peg$fail(peg$c39);
2054 }
2055 }
2056 if (s2 !== peg$FAILED) {
2057 s1 = peg$c40(s1);
2058 s0 = s1;
2059 } else {
2060 peg$currPos = s0;
2061 s0 = peg$FAILED;
2062 }
2063 } else {
2064 peg$currPos = s0;
2065 s0 = peg$FAILED;
2066 }
2067 if (s0 === peg$FAILED) {
2068 if (peg$c41.test(input.charAt(peg$currPos))) {
2069 s0 = input.charAt(peg$currPos);
2070 peg$currPos++;
2071 } else {
2072 s0 = peg$FAILED;
2073 {
2074 peg$fail(peg$c42);
2075 }
2076 }
2077 }
2078 peg$resultsCache[key] = {
2079 nextPos: peg$currPos,
2080 result: s0
2081 };
2082 return s0;
2083 }
2084 function peg$parseattrEqOps() {
2085 var s0, s1, s2;
2086 var key = peg$currPos * 32 + 14,
2087 cached = peg$resultsCache[key];
2088 if (cached) {
2089 peg$currPos = cached.nextPos;
2090 return cached.result;
2091 }
2092 s0 = peg$currPos;
2093 if (input.charCodeAt(peg$currPos) === 33) {
2094 s1 = peg$c22;
2095 peg$currPos++;
2096 } else {
2097 s1 = peg$FAILED;
2098 {
2099 peg$fail(peg$c23);
2100 }
2101 }
2102 if (s1 === peg$FAILED) {
2103 s1 = null;
2104 }
2105 if (s1 !== peg$FAILED) {
2106 if (input.charCodeAt(peg$currPos) === 61) {
2107 s2 = peg$c38;
2108 peg$currPos++;
2109 } else {
2110 s2 = peg$FAILED;
2111 {
2112 peg$fail(peg$c39);
2113 }
2114 }
2115 if (s2 !== peg$FAILED) {
2116 s1 = peg$c40(s1);
2117 s0 = s1;
2118 } else {
2119 peg$currPos = s0;
2120 s0 = peg$FAILED;
2121 }
2122 } else {
2123 peg$currPos = s0;
2124 s0 = peg$FAILED;
2125 }
2126 peg$resultsCache[key] = {
2127 nextPos: peg$currPos,
2128 result: s0
2129 };
2130 return s0;
2131 }
2132 function peg$parseattrName() {
2133 var s0, s1, s2, s3, s4, s5;
2134 var key = peg$currPos * 32 + 15,
2135 cached = peg$resultsCache[key];
2136 if (cached) {
2137 peg$currPos = cached.nextPos;
2138 return cached.result;
2139 }
2140 s0 = peg$currPos;
2141 s1 = peg$parseidentifierName();
2142 if (s1 !== peg$FAILED) {
2143 s2 = [];
2144 s3 = peg$currPos;
2145 if (input.charCodeAt(peg$currPos) === 46) {
2146 s4 = peg$c43;
2147 peg$currPos++;
2148 } else {
2149 s4 = peg$FAILED;
2150 {
2151 peg$fail(peg$c44);
2152 }
2153 }
2154 if (s4 !== peg$FAILED) {
2155 s5 = peg$parseidentifierName();
2156 if (s5 !== peg$FAILED) {
2157 s4 = [s4, s5];
2158 s3 = s4;
2159 } else {
2160 peg$currPos = s3;
2161 s3 = peg$FAILED;
2162 }
2163 } else {
2164 peg$currPos = s3;
2165 s3 = peg$FAILED;
2166 }
2167 while (s3 !== peg$FAILED) {
2168 s2.push(s3);
2169 s3 = peg$currPos;
2170 if (input.charCodeAt(peg$currPos) === 46) {
2171 s4 = peg$c43;
2172 peg$currPos++;
2173 } else {
2174 s4 = peg$FAILED;
2175 {
2176 peg$fail(peg$c44);
2177 }
2178 }
2179 if (s4 !== peg$FAILED) {
2180 s5 = peg$parseidentifierName();
2181 if (s5 !== peg$FAILED) {
2182 s4 = [s4, s5];
2183 s3 = s4;
2184 } else {
2185 peg$currPos = s3;
2186 s3 = peg$FAILED;
2187 }
2188 } else {
2189 peg$currPos = s3;
2190 s3 = peg$FAILED;
2191 }
2192 }
2193 if (s2 !== peg$FAILED) {
2194 s1 = peg$c45(s1, s2);
2195 s0 = s1;
2196 } else {
2197 peg$currPos = s0;
2198 s0 = peg$FAILED;
2199 }
2200 } else {
2201 peg$currPos = s0;
2202 s0 = peg$FAILED;
2203 }
2204 peg$resultsCache[key] = {
2205 nextPos: peg$currPos,
2206 result: s0
2207 };
2208 return s0;
2209 }
2210 function peg$parseattrValue() {
2211 var s0, s1, s2, s3, s4, s5;
2212 var key = peg$currPos * 32 + 16,
2213 cached = peg$resultsCache[key];
2214 if (cached) {
2215 peg$currPos = cached.nextPos;
2216 return cached.result;
2217 }
2218 s0 = peg$currPos;
2219 s1 = peg$parseattrName();
2220 if (s1 !== peg$FAILED) {
2221 s2 = peg$parse_();
2222 if (s2 !== peg$FAILED) {
2223 s3 = peg$parseattrEqOps();
2224 if (s3 !== peg$FAILED) {
2225 s4 = peg$parse_();
2226 if (s4 !== peg$FAILED) {
2227 s5 = peg$parsetype();
2228 if (s5 === peg$FAILED) {
2229 s5 = peg$parseregex();
2230 }
2231 if (s5 !== peg$FAILED) {
2232 s1 = peg$c46(s1, s3, s5);
2233 s0 = s1;
2234 } else {
2235 peg$currPos = s0;
2236 s0 = peg$FAILED;
2237 }
2238 } else {
2239 peg$currPos = s0;
2240 s0 = peg$FAILED;
2241 }
2242 } else {
2243 peg$currPos = s0;
2244 s0 = peg$FAILED;
2245 }
2246 } else {
2247 peg$currPos = s0;
2248 s0 = peg$FAILED;
2249 }
2250 } else {
2251 peg$currPos = s0;
2252 s0 = peg$FAILED;
2253 }
2254 if (s0 === peg$FAILED) {
2255 s0 = peg$currPos;
2256 s1 = peg$parseattrName();
2257 if (s1 !== peg$FAILED) {
2258 s2 = peg$parse_();
2259 if (s2 !== peg$FAILED) {
2260 s3 = peg$parseattrOps();
2261 if (s3 !== peg$FAILED) {
2262 s4 = peg$parse_();
2263 if (s4 !== peg$FAILED) {
2264 s5 = peg$parsestring();
2265 if (s5 === peg$FAILED) {
2266 s5 = peg$parsenumber();
2267 if (s5 === peg$FAILED) {
2268 s5 = peg$parsepath();
2269 }
2270 }
2271 if (s5 !== peg$FAILED) {
2272 s1 = peg$c46(s1, s3, s5);
2273 s0 = s1;
2274 } else {
2275 peg$currPos = s0;
2276 s0 = peg$FAILED;
2277 }
2278 } else {
2279 peg$currPos = s0;
2280 s0 = peg$FAILED;
2281 }
2282 } else {
2283 peg$currPos = s0;
2284 s0 = peg$FAILED;
2285 }
2286 } else {
2287 peg$currPos = s0;
2288 s0 = peg$FAILED;
2289 }
2290 } else {
2291 peg$currPos = s0;
2292 s0 = peg$FAILED;
2293 }
2294 if (s0 === peg$FAILED) {
2295 s0 = peg$currPos;
2296 s1 = peg$parseattrName();
2297 if (s1 !== peg$FAILED) {
2298 s1 = peg$c47(s1);
2299 }
2300 s0 = s1;
2301 }
2302 }
2303 peg$resultsCache[key] = {
2304 nextPos: peg$currPos,
2305 result: s0
2306 };
2307 return s0;
2308 }
2309 function peg$parsestring() {
2310 var s0, s1, s2, s3, s4, s5;
2311 var key = peg$currPos * 32 + 17,
2312 cached = peg$resultsCache[key];
2313 if (cached) {
2314 peg$currPos = cached.nextPos;
2315 return cached.result;
2316 }
2317 s0 = peg$currPos;
2318 if (input.charCodeAt(peg$currPos) === 34) {
2319 s1 = peg$c48;
2320 peg$currPos++;
2321 } else {
2322 s1 = peg$FAILED;
2323 {
2324 peg$fail(peg$c49);
2325 }
2326 }
2327 if (s1 !== peg$FAILED) {
2328 s2 = [];
2329 if (peg$c50.test(input.charAt(peg$currPos))) {
2330 s3 = input.charAt(peg$currPos);
2331 peg$currPos++;
2332 } else {
2333 s3 = peg$FAILED;
2334 {
2335 peg$fail(peg$c51);
2336 }
2337 }
2338 if (s3 === peg$FAILED) {
2339 s3 = peg$currPos;
2340 if (input.charCodeAt(peg$currPos) === 92) {
2341 s4 = peg$c52;
2342 peg$currPos++;
2343 } else {
2344 s4 = peg$FAILED;
2345 {
2346 peg$fail(peg$c53);
2347 }
2348 }
2349 if (s4 !== peg$FAILED) {
2350 if (input.length > peg$currPos) {
2351 s5 = input.charAt(peg$currPos);
2352 peg$currPos++;
2353 } else {
2354 s5 = peg$FAILED;
2355 {
2356 peg$fail(peg$c54);
2357 }
2358 }
2359 if (s5 !== peg$FAILED) {
2360 s4 = peg$c55(s4, s5);
2361 s3 = s4;
2362 } else {
2363 peg$currPos = s3;
2364 s3 = peg$FAILED;
2365 }
2366 } else {
2367 peg$currPos = s3;
2368 s3 = peg$FAILED;
2369 }
2370 }
2371 while (s3 !== peg$FAILED) {
2372 s2.push(s3);
2373 if (peg$c50.test(input.charAt(peg$currPos))) {
2374 s3 = input.charAt(peg$currPos);
2375 peg$currPos++;
2376 } else {
2377 s3 = peg$FAILED;
2378 {
2379 peg$fail(peg$c51);
2380 }
2381 }
2382 if (s3 === peg$FAILED) {
2383 s3 = peg$currPos;
2384 if (input.charCodeAt(peg$currPos) === 92) {
2385 s4 = peg$c52;
2386 peg$currPos++;
2387 } else {
2388 s4 = peg$FAILED;
2389 {
2390 peg$fail(peg$c53);
2391 }
2392 }
2393 if (s4 !== peg$FAILED) {
2394 if (input.length > peg$currPos) {
2395 s5 = input.charAt(peg$currPos);
2396 peg$currPos++;
2397 } else {
2398 s5 = peg$FAILED;
2399 {
2400 peg$fail(peg$c54);
2401 }
2402 }
2403 if (s5 !== peg$FAILED) {
2404 s4 = peg$c55(s4, s5);
2405 s3 = s4;
2406 } else {
2407 peg$currPos = s3;
2408 s3 = peg$FAILED;
2409 }
2410 } else {
2411 peg$currPos = s3;
2412 s3 = peg$FAILED;
2413 }
2414 }
2415 }
2416 if (s2 !== peg$FAILED) {
2417 if (input.charCodeAt(peg$currPos) === 34) {
2418 s3 = peg$c48;
2419 peg$currPos++;
2420 } else {
2421 s3 = peg$FAILED;
2422 {
2423 peg$fail(peg$c49);
2424 }
2425 }
2426 if (s3 !== peg$FAILED) {
2427 s1 = peg$c56(s2);
2428 s0 = s1;
2429 } else {
2430 peg$currPos = s0;
2431 s0 = peg$FAILED;
2432 }
2433 } else {
2434 peg$currPos = s0;
2435 s0 = peg$FAILED;
2436 }
2437 } else {
2438 peg$currPos = s0;
2439 s0 = peg$FAILED;
2440 }
2441 if (s0 === peg$FAILED) {
2442 s0 = peg$currPos;
2443 if (input.charCodeAt(peg$currPos) === 39) {
2444 s1 = peg$c57;
2445 peg$currPos++;
2446 } else {
2447 s1 = peg$FAILED;
2448 {
2449 peg$fail(peg$c58);
2450 }
2451 }
2452 if (s1 !== peg$FAILED) {
2453 s2 = [];
2454 if (peg$c59.test(input.charAt(peg$currPos))) {
2455 s3 = input.charAt(peg$currPos);
2456 peg$currPos++;
2457 } else {
2458 s3 = peg$FAILED;
2459 {
2460 peg$fail(peg$c60);
2461 }
2462 }
2463 if (s3 === peg$FAILED) {
2464 s3 = peg$currPos;
2465 if (input.charCodeAt(peg$currPos) === 92) {
2466 s4 = peg$c52;
2467 peg$currPos++;
2468 } else {
2469 s4 = peg$FAILED;
2470 {
2471 peg$fail(peg$c53);
2472 }
2473 }
2474 if (s4 !== peg$FAILED) {
2475 if (input.length > peg$currPos) {
2476 s5 = input.charAt(peg$currPos);
2477 peg$currPos++;
2478 } else {
2479 s5 = peg$FAILED;
2480 {
2481 peg$fail(peg$c54);
2482 }
2483 }
2484 if (s5 !== peg$FAILED) {
2485 s4 = peg$c55(s4, s5);
2486 s3 = s4;
2487 } else {
2488 peg$currPos = s3;
2489 s3 = peg$FAILED;
2490 }
2491 } else {
2492 peg$currPos = s3;
2493 s3 = peg$FAILED;
2494 }
2495 }
2496 while (s3 !== peg$FAILED) {
2497 s2.push(s3);
2498 if (peg$c59.test(input.charAt(peg$currPos))) {
2499 s3 = input.charAt(peg$currPos);
2500 peg$currPos++;
2501 } else {
2502 s3 = peg$FAILED;
2503 {
2504 peg$fail(peg$c60);
2505 }
2506 }
2507 if (s3 === peg$FAILED) {
2508 s3 = peg$currPos;
2509 if (input.charCodeAt(peg$currPos) === 92) {
2510 s4 = peg$c52;
2511 peg$currPos++;
2512 } else {
2513 s4 = peg$FAILED;
2514 {
2515 peg$fail(peg$c53);
2516 }
2517 }
2518 if (s4 !== peg$FAILED) {
2519 if (input.length > peg$currPos) {
2520 s5 = input.charAt(peg$currPos);
2521 peg$currPos++;
2522 } else {
2523 s5 = peg$FAILED;
2524 {
2525 peg$fail(peg$c54);
2526 }
2527 }
2528 if (s5 !== peg$FAILED) {
2529 s4 = peg$c55(s4, s5);
2530 s3 = s4;
2531 } else {
2532 peg$currPos = s3;
2533 s3 = peg$FAILED;
2534 }
2535 } else {
2536 peg$currPos = s3;
2537 s3 = peg$FAILED;
2538 }
2539 }
2540 }
2541 if (s2 !== peg$FAILED) {
2542 if (input.charCodeAt(peg$currPos) === 39) {
2543 s3 = peg$c57;
2544 peg$currPos++;
2545 } else {
2546 s3 = peg$FAILED;
2547 {
2548 peg$fail(peg$c58);
2549 }
2550 }
2551 if (s3 !== peg$FAILED) {
2552 s1 = peg$c56(s2);
2553 s0 = s1;
2554 } else {
2555 peg$currPos = s0;
2556 s0 = peg$FAILED;
2557 }
2558 } else {
2559 peg$currPos = s0;
2560 s0 = peg$FAILED;
2561 }
2562 } else {
2563 peg$currPos = s0;
2564 s0 = peg$FAILED;
2565 }
2566 }
2567 peg$resultsCache[key] = {
2568 nextPos: peg$currPos,
2569 result: s0
2570 };
2571 return s0;
2572 }
2573 function peg$parsenumber() {
2574 var s0, s1, s2, s3;
2575 var key = peg$currPos * 32 + 18,
2576 cached = peg$resultsCache[key];
2577 if (cached) {
2578 peg$currPos = cached.nextPos;
2579 return cached.result;
2580 }
2581 s0 = peg$currPos;
2582 s1 = peg$currPos;
2583 s2 = [];
2584 if (peg$c61.test(input.charAt(peg$currPos))) {
2585 s3 = input.charAt(peg$currPos);
2586 peg$currPos++;
2587 } else {
2588 s3 = peg$FAILED;
2589 {
2590 peg$fail(peg$c62);
2591 }
2592 }
2593 while (s3 !== peg$FAILED) {
2594 s2.push(s3);
2595 if (peg$c61.test(input.charAt(peg$currPos))) {
2596 s3 = input.charAt(peg$currPos);
2597 peg$currPos++;
2598 } else {
2599 s3 = peg$FAILED;
2600 {
2601 peg$fail(peg$c62);
2602 }
2603 }
2604 }
2605 if (s2 !== peg$FAILED) {
2606 if (input.charCodeAt(peg$currPos) === 46) {
2607 s3 = peg$c43;
2608 peg$currPos++;
2609 } else {
2610 s3 = peg$FAILED;
2611 {
2612 peg$fail(peg$c44);
2613 }
2614 }
2615 if (s3 !== peg$FAILED) {
2616 s2 = [s2, s3];
2617 s1 = s2;
2618 } else {
2619 peg$currPos = s1;
2620 s1 = peg$FAILED;
2621 }
2622 } else {
2623 peg$currPos = s1;
2624 s1 = peg$FAILED;
2625 }
2626 if (s1 === peg$FAILED) {
2627 s1 = null;
2628 }
2629 if (s1 !== peg$FAILED) {
2630 s2 = [];
2631 if (peg$c61.test(input.charAt(peg$currPos))) {
2632 s3 = input.charAt(peg$currPos);
2633 peg$currPos++;
2634 } else {
2635 s3 = peg$FAILED;
2636 {
2637 peg$fail(peg$c62);
2638 }
2639 }
2640 if (s3 !== peg$FAILED) {
2641 while (s3 !== peg$FAILED) {
2642 s2.push(s3);
2643 if (peg$c61.test(input.charAt(peg$currPos))) {
2644 s3 = input.charAt(peg$currPos);
2645 peg$currPos++;
2646 } else {
2647 s3 = peg$FAILED;
2648 {
2649 peg$fail(peg$c62);
2650 }
2651 }
2652 }
2653 } else {
2654 s2 = peg$FAILED;
2655 }
2656 if (s2 !== peg$FAILED) {
2657 s1 = peg$c63(s1, s2);
2658 s0 = s1;
2659 } else {
2660 peg$currPos = s0;
2661 s0 = peg$FAILED;
2662 }
2663 } else {
2664 peg$currPos = s0;
2665 s0 = peg$FAILED;
2666 }
2667 peg$resultsCache[key] = {
2668 nextPos: peg$currPos,
2669 result: s0
2670 };
2671 return s0;
2672 }
2673 function peg$parsepath() {
2674 var s0, s1;
2675 var key = peg$currPos * 32 + 19,
2676 cached = peg$resultsCache[key];
2677 if (cached) {
2678 peg$currPos = cached.nextPos;
2679 return cached.result;
2680 }
2681 s0 = peg$currPos;
2682 s1 = peg$parseidentifierName();
2683 if (s1 !== peg$FAILED) {
2684 s1 = peg$c64(s1);
2685 }
2686 s0 = s1;
2687 peg$resultsCache[key] = {
2688 nextPos: peg$currPos,
2689 result: s0
2690 };
2691 return s0;
2692 }
2693 function peg$parsetype() {
2694 var s0, s1, s2, s3, s4, s5;
2695 var key = peg$currPos * 32 + 20,
2696 cached = peg$resultsCache[key];
2697 if (cached) {
2698 peg$currPos = cached.nextPos;
2699 return cached.result;
2700 }
2701 s0 = peg$currPos;
2702 if (input.substr(peg$currPos, 5) === peg$c65) {
2703 s1 = peg$c65;
2704 peg$currPos += 5;
2705 } else {
2706 s1 = peg$FAILED;
2707 {
2708 peg$fail(peg$c66);
2709 }
2710 }
2711 if (s1 !== peg$FAILED) {
2712 s2 = peg$parse_();
2713 if (s2 !== peg$FAILED) {
2714 s3 = [];
2715 if (peg$c67.test(input.charAt(peg$currPos))) {
2716 s4 = input.charAt(peg$currPos);
2717 peg$currPos++;
2718 } else {
2719 s4 = peg$FAILED;
2720 {
2721 peg$fail(peg$c68);
2722 }
2723 }
2724 if (s4 !== peg$FAILED) {
2725 while (s4 !== peg$FAILED) {
2726 s3.push(s4);
2727 if (peg$c67.test(input.charAt(peg$currPos))) {
2728 s4 = input.charAt(peg$currPos);
2729 peg$currPos++;
2730 } else {
2731 s4 = peg$FAILED;
2732 {
2733 peg$fail(peg$c68);
2734 }
2735 }
2736 }
2737 } else {
2738 s3 = peg$FAILED;
2739 }
2740 if (s3 !== peg$FAILED) {
2741 s4 = peg$parse_();
2742 if (s4 !== peg$FAILED) {
2743 if (input.charCodeAt(peg$currPos) === 41) {
2744 s5 = peg$c69;
2745 peg$currPos++;
2746 } else {
2747 s5 = peg$FAILED;
2748 {
2749 peg$fail(peg$c70);
2750 }
2751 }
2752 if (s5 !== peg$FAILED) {
2753 s1 = peg$c71(s3);
2754 s0 = s1;
2755 } else {
2756 peg$currPos = s0;
2757 s0 = peg$FAILED;
2758 }
2759 } else {
2760 peg$currPos = s0;
2761 s0 = peg$FAILED;
2762 }
2763 } else {
2764 peg$currPos = s0;
2765 s0 = peg$FAILED;
2766 }
2767 } else {
2768 peg$currPos = s0;
2769 s0 = peg$FAILED;
2770 }
2771 } else {
2772 peg$currPos = s0;
2773 s0 = peg$FAILED;
2774 }
2775 peg$resultsCache[key] = {
2776 nextPos: peg$currPos,
2777 result: s0
2778 };
2779 return s0;
2780 }
2781 function peg$parseflags() {
2782 var s0, s1;
2783 var key = peg$currPos * 32 + 21,
2784 cached = peg$resultsCache[key];
2785 if (cached) {
2786 peg$currPos = cached.nextPos;
2787 return cached.result;
2788 }
2789 s0 = [];
2790 if (peg$c72.test(input.charAt(peg$currPos))) {
2791 s1 = input.charAt(peg$currPos);
2792 peg$currPos++;
2793 } else {
2794 s1 = peg$FAILED;
2795 {
2796 peg$fail(peg$c73);
2797 }
2798 }
2799 if (s1 !== peg$FAILED) {
2800 while (s1 !== peg$FAILED) {
2801 s0.push(s1);
2802 if (peg$c72.test(input.charAt(peg$currPos))) {
2803 s1 = input.charAt(peg$currPos);
2804 peg$currPos++;
2805 } else {
2806 s1 = peg$FAILED;
2807 {
2808 peg$fail(peg$c73);
2809 }
2810 }
2811 }
2812 } else {
2813 s0 = peg$FAILED;
2814 }
2815 peg$resultsCache[key] = {
2816 nextPos: peg$currPos,
2817 result: s0
2818 };
2819 return s0;
2820 }
2821 function peg$parseregex() {
2822 var s0, s1, s2, s3, s4;
2823 var key = peg$currPos * 32 + 22,
2824 cached = peg$resultsCache[key];
2825 if (cached) {
2826 peg$currPos = cached.nextPos;
2827 return cached.result;
2828 }
2829 s0 = peg$currPos;
2830 if (input.charCodeAt(peg$currPos) === 47) {
2831 s1 = peg$c74;
2832 peg$currPos++;
2833 } else {
2834 s1 = peg$FAILED;
2835 {
2836 peg$fail(peg$c75);
2837 }
2838 }
2839 if (s1 !== peg$FAILED) {
2840 s2 = [];
2841 if (peg$c76.test(input.charAt(peg$currPos))) {
2842 s3 = input.charAt(peg$currPos);
2843 peg$currPos++;
2844 } else {
2845 s3 = peg$FAILED;
2846 {
2847 peg$fail(peg$c77);
2848 }
2849 }
2850 if (s3 !== peg$FAILED) {
2851 while (s3 !== peg$FAILED) {
2852 s2.push(s3);
2853 if (peg$c76.test(input.charAt(peg$currPos))) {
2854 s3 = input.charAt(peg$currPos);
2855 peg$currPos++;
2856 } else {
2857 s3 = peg$FAILED;
2858 {
2859 peg$fail(peg$c77);
2860 }
2861 }
2862 }
2863 } else {
2864 s2 = peg$FAILED;
2865 }
2866 if (s2 !== peg$FAILED) {
2867 if (input.charCodeAt(peg$currPos) === 47) {
2868 s3 = peg$c74;
2869 peg$currPos++;
2870 } else {
2871 s3 = peg$FAILED;
2872 {
2873 peg$fail(peg$c75);
2874 }
2875 }
2876 if (s3 !== peg$FAILED) {
2877 s4 = peg$parseflags();
2878 if (s4 === peg$FAILED) {
2879 s4 = null;
2880 }
2881 if (s4 !== peg$FAILED) {
2882 s1 = peg$c78(s2, s4);
2883 s0 = s1;
2884 } else {
2885 peg$currPos = s0;
2886 s0 = peg$FAILED;
2887 }
2888 } else {
2889 peg$currPos = s0;
2890 s0 = peg$FAILED;
2891 }
2892 } else {
2893 peg$currPos = s0;
2894 s0 = peg$FAILED;
2895 }
2896 } else {
2897 peg$currPos = s0;
2898 s0 = peg$FAILED;
2899 }
2900 peg$resultsCache[key] = {
2901 nextPos: peg$currPos,
2902 result: s0
2903 };
2904 return s0;
2905 }
2906 function peg$parsefield() {
2907 var s0, s1, s2, s3, s4, s5, s6;
2908 var key = peg$currPos * 32 + 23,
2909 cached = peg$resultsCache[key];
2910 if (cached) {
2911 peg$currPos = cached.nextPos;
2912 return cached.result;
2913 }
2914 s0 = peg$currPos;
2915 if (input.charCodeAt(peg$currPos) === 46) {
2916 s1 = peg$c43;
2917 peg$currPos++;
2918 } else {
2919 s1 = peg$FAILED;
2920 {
2921 peg$fail(peg$c44);
2922 }
2923 }
2924 if (s1 !== peg$FAILED) {
2925 s2 = peg$parseidentifierName();
2926 if (s2 !== peg$FAILED) {
2927 s3 = [];
2928 s4 = peg$currPos;
2929 if (input.charCodeAt(peg$currPos) === 46) {
2930 s5 = peg$c43;
2931 peg$currPos++;
2932 } else {
2933 s5 = peg$FAILED;
2934 {
2935 peg$fail(peg$c44);
2936 }
2937 }
2938 if (s5 !== peg$FAILED) {
2939 s6 = peg$parseidentifierName();
2940 if (s6 !== peg$FAILED) {
2941 s5 = [s5, s6];
2942 s4 = s5;
2943 } else {
2944 peg$currPos = s4;
2945 s4 = peg$FAILED;
2946 }
2947 } else {
2948 peg$currPos = s4;
2949 s4 = peg$FAILED;
2950 }
2951 while (s4 !== peg$FAILED) {
2952 s3.push(s4);
2953 s4 = peg$currPos;
2954 if (input.charCodeAt(peg$currPos) === 46) {
2955 s5 = peg$c43;
2956 peg$currPos++;
2957 } else {
2958 s5 = peg$FAILED;
2959 {
2960 peg$fail(peg$c44);
2961 }
2962 }
2963 if (s5 !== peg$FAILED) {
2964 s6 = peg$parseidentifierName();
2965 if (s6 !== peg$FAILED) {
2966 s5 = [s5, s6];
2967 s4 = s5;
2968 } else {
2969 peg$currPos = s4;
2970 s4 = peg$FAILED;
2971 }
2972 } else {
2973 peg$currPos = s4;
2974 s4 = peg$FAILED;
2975 }
2976 }
2977 if (s3 !== peg$FAILED) {
2978 s1 = peg$c79(s2, s3);
2979 s0 = s1;
2980 } else {
2981 peg$currPos = s0;
2982 s0 = peg$FAILED;
2983 }
2984 } else {
2985 peg$currPos = s0;
2986 s0 = peg$FAILED;
2987 }
2988 } else {
2989 peg$currPos = s0;
2990 s0 = peg$FAILED;
2991 }
2992 peg$resultsCache[key] = {
2993 nextPos: peg$currPos,
2994 result: s0
2995 };
2996 return s0;
2997 }
2998 function peg$parsenegation() {
2999 var s0, s1, s2, s3, s4, s5;
3000 var key = peg$currPos * 32 + 24,
3001 cached = peg$resultsCache[key];
3002 if (cached) {
3003 peg$currPos = cached.nextPos;
3004 return cached.result;
3005 }
3006 s0 = peg$currPos;
3007 if (input.substr(peg$currPos, 5) === peg$c80) {
3008 s1 = peg$c80;
3009 peg$currPos += 5;
3010 } else {
3011 s1 = peg$FAILED;
3012 {
3013 peg$fail(peg$c81);
3014 }
3015 }
3016 if (s1 !== peg$FAILED) {
3017 s2 = peg$parse_();
3018 if (s2 !== peg$FAILED) {
3019 s3 = peg$parseselectors();
3020 if (s3 !== peg$FAILED) {
3021 s4 = peg$parse_();
3022 if (s4 !== peg$FAILED) {
3023 if (input.charCodeAt(peg$currPos) === 41) {
3024 s5 = peg$c69;
3025 peg$currPos++;
3026 } else {
3027 s5 = peg$FAILED;
3028 {
3029 peg$fail(peg$c70);
3030 }
3031 }
3032 if (s5 !== peg$FAILED) {
3033 s1 = peg$c82(s3);
3034 s0 = s1;
3035 } else {
3036 peg$currPos = s0;
3037 s0 = peg$FAILED;
3038 }
3039 } else {
3040 peg$currPos = s0;
3041 s0 = peg$FAILED;
3042 }
3043 } else {
3044 peg$currPos = s0;
3045 s0 = peg$FAILED;
3046 }
3047 } else {
3048 peg$currPos = s0;
3049 s0 = peg$FAILED;
3050 }
3051 } else {
3052 peg$currPos = s0;
3053 s0 = peg$FAILED;
3054 }
3055 peg$resultsCache[key] = {
3056 nextPos: peg$currPos,
3057 result: s0
3058 };
3059 return s0;
3060 }
3061 function peg$parsematches() {
3062 var s0, s1, s2, s3, s4, s5;
3063 var key = peg$currPos * 32 + 25,
3064 cached = peg$resultsCache[key];
3065 if (cached) {
3066 peg$currPos = cached.nextPos;
3067 return cached.result;
3068 }
3069 s0 = peg$currPos;
3070 if (input.substr(peg$currPos, 9) === peg$c83) {
3071 s1 = peg$c83;
3072 peg$currPos += 9;
3073 } else {
3074 s1 = peg$FAILED;
3075 {
3076 peg$fail(peg$c84);
3077 }
3078 }
3079 if (s1 !== peg$FAILED) {
3080 s2 = peg$parse_();
3081 if (s2 !== peg$FAILED) {
3082 s3 = peg$parseselectors();
3083 if (s3 !== peg$FAILED) {
3084 s4 = peg$parse_();
3085 if (s4 !== peg$FAILED) {
3086 if (input.charCodeAt(peg$currPos) === 41) {
3087 s5 = peg$c69;
3088 peg$currPos++;
3089 } else {
3090 s5 = peg$FAILED;
3091 {
3092 peg$fail(peg$c70);
3093 }
3094 }
3095 if (s5 !== peg$FAILED) {
3096 s1 = peg$c85(s3);
3097 s0 = s1;
3098 } else {
3099 peg$currPos = s0;
3100 s0 = peg$FAILED;
3101 }
3102 } else {
3103 peg$currPos = s0;
3104 s0 = peg$FAILED;
3105 }
3106 } else {
3107 peg$currPos = s0;
3108 s0 = peg$FAILED;
3109 }
3110 } else {
3111 peg$currPos = s0;
3112 s0 = peg$FAILED;
3113 }
3114 } else {
3115 peg$currPos = s0;
3116 s0 = peg$FAILED;
3117 }
3118 peg$resultsCache[key] = {
3119 nextPos: peg$currPos,
3120 result: s0
3121 };
3122 return s0;
3123 }
3124 function peg$parsehas() {
3125 var s0, s1, s2, s3, s4, s5;
3126 var key = peg$currPos * 32 + 26,
3127 cached = peg$resultsCache[key];
3128 if (cached) {
3129 peg$currPos = cached.nextPos;
3130 return cached.result;
3131 }
3132 s0 = peg$currPos;
3133 if (input.substr(peg$currPos, 5) === peg$c86) {
3134 s1 = peg$c86;
3135 peg$currPos += 5;
3136 } else {
3137 s1 = peg$FAILED;
3138 {
3139 peg$fail(peg$c87);
3140 }
3141 }
3142 if (s1 !== peg$FAILED) {
3143 s2 = peg$parse_();
3144 if (s2 !== peg$FAILED) {
3145 s3 = peg$parsehasSelectors();
3146 if (s3 !== peg$FAILED) {
3147 s4 = peg$parse_();
3148 if (s4 !== peg$FAILED) {
3149 if (input.charCodeAt(peg$currPos) === 41) {
3150 s5 = peg$c69;
3151 peg$currPos++;
3152 } else {
3153 s5 = peg$FAILED;
3154 {
3155 peg$fail(peg$c70);
3156 }
3157 }
3158 if (s5 !== peg$FAILED) {
3159 s1 = peg$c88(s3);
3160 s0 = s1;
3161 } else {
3162 peg$currPos = s0;
3163 s0 = peg$FAILED;
3164 }
3165 } else {
3166 peg$currPos = s0;
3167 s0 = peg$FAILED;
3168 }
3169 } else {
3170 peg$currPos = s0;
3171 s0 = peg$FAILED;
3172 }
3173 } else {
3174 peg$currPos = s0;
3175 s0 = peg$FAILED;
3176 }
3177 } else {
3178 peg$currPos = s0;
3179 s0 = peg$FAILED;
3180 }
3181 peg$resultsCache[key] = {
3182 nextPos: peg$currPos,
3183 result: s0
3184 };
3185 return s0;
3186 }
3187 function peg$parsefirstChild() {
3188 var s0, s1;
3189 var key = peg$currPos * 32 + 27,
3190 cached = peg$resultsCache[key];
3191 if (cached) {
3192 peg$currPos = cached.nextPos;
3193 return cached.result;
3194 }
3195 s0 = peg$currPos;
3196 if (input.substr(peg$currPos, 12) === peg$c89) {
3197 s1 = peg$c89;
3198 peg$currPos += 12;
3199 } else {
3200 s1 = peg$FAILED;
3201 {
3202 peg$fail(peg$c90);
3203 }
3204 }
3205 if (s1 !== peg$FAILED) {
3206 s1 = peg$c91();
3207 }
3208 s0 = s1;
3209 peg$resultsCache[key] = {
3210 nextPos: peg$currPos,
3211 result: s0
3212 };
3213 return s0;
3214 }
3215 function peg$parselastChild() {
3216 var s0, s1;
3217 var key = peg$currPos * 32 + 28,
3218 cached = peg$resultsCache[key];
3219 if (cached) {
3220 peg$currPos = cached.nextPos;
3221 return cached.result;
3222 }
3223 s0 = peg$currPos;
3224 if (input.substr(peg$currPos, 11) === peg$c92) {
3225 s1 = peg$c92;
3226 peg$currPos += 11;
3227 } else {
3228 s1 = peg$FAILED;
3229 {
3230 peg$fail(peg$c93);
3231 }
3232 }
3233 if (s1 !== peg$FAILED) {
3234 s1 = peg$c94();
3235 }
3236 s0 = s1;
3237 peg$resultsCache[key] = {
3238 nextPos: peg$currPos,
3239 result: s0
3240 };
3241 return s0;
3242 }
3243 function peg$parsenthChild() {
3244 var s0, s1, s2, s3, s4, s5;
3245 var key = peg$currPos * 32 + 29,
3246 cached = peg$resultsCache[key];
3247 if (cached) {
3248 peg$currPos = cached.nextPos;
3249 return cached.result;
3250 }
3251 s0 = peg$currPos;
3252 if (input.substr(peg$currPos, 11) === peg$c95) {
3253 s1 = peg$c95;
3254 peg$currPos += 11;
3255 } else {
3256 s1 = peg$FAILED;
3257 {
3258 peg$fail(peg$c96);
3259 }
3260 }
3261 if (s1 !== peg$FAILED) {
3262 s2 = peg$parse_();
3263 if (s2 !== peg$FAILED) {
3264 s3 = [];
3265 if (peg$c61.test(input.charAt(peg$currPos))) {
3266 s4 = input.charAt(peg$currPos);
3267 peg$currPos++;
3268 } else {
3269 s4 = peg$FAILED;
3270 {
3271 peg$fail(peg$c62);
3272 }
3273 }
3274 if (s4 !== peg$FAILED) {
3275 while (s4 !== peg$FAILED) {
3276 s3.push(s4);
3277 if (peg$c61.test(input.charAt(peg$currPos))) {
3278 s4 = input.charAt(peg$currPos);
3279 peg$currPos++;
3280 } else {
3281 s4 = peg$FAILED;
3282 {
3283 peg$fail(peg$c62);
3284 }
3285 }
3286 }
3287 } else {
3288 s3 = peg$FAILED;
3289 }
3290 if (s3 !== peg$FAILED) {
3291 s4 = peg$parse_();
3292 if (s4 !== peg$FAILED) {
3293 if (input.charCodeAt(peg$currPos) === 41) {
3294 s5 = peg$c69;
3295 peg$currPos++;
3296 } else {
3297 s5 = peg$FAILED;
3298 {
3299 peg$fail(peg$c70);
3300 }
3301 }
3302 if (s5 !== peg$FAILED) {
3303 s1 = peg$c97(s3);
3304 s0 = s1;
3305 } else {
3306 peg$currPos = s0;
3307 s0 = peg$FAILED;
3308 }
3309 } else {
3310 peg$currPos = s0;
3311 s0 = peg$FAILED;
3312 }
3313 } else {
3314 peg$currPos = s0;
3315 s0 = peg$FAILED;
3316 }
3317 } else {
3318 peg$currPos = s0;
3319 s0 = peg$FAILED;
3320 }
3321 } else {
3322 peg$currPos = s0;
3323 s0 = peg$FAILED;
3324 }
3325 peg$resultsCache[key] = {
3326 nextPos: peg$currPos,
3327 result: s0
3328 };
3329 return s0;
3330 }
3331 function peg$parsenthLastChild() {
3332 var s0, s1, s2, s3, s4, s5;
3333 var key = peg$currPos * 32 + 30,
3334 cached = peg$resultsCache[key];
3335 if (cached) {
3336 peg$currPos = cached.nextPos;
3337 return cached.result;
3338 }
3339 s0 = peg$currPos;
3340 if (input.substr(peg$currPos, 16) === peg$c98) {
3341 s1 = peg$c98;
3342 peg$currPos += 16;
3343 } else {
3344 s1 = peg$FAILED;
3345 {
3346 peg$fail(peg$c99);
3347 }
3348 }
3349 if (s1 !== peg$FAILED) {
3350 s2 = peg$parse_();
3351 if (s2 !== peg$FAILED) {
3352 s3 = [];
3353 if (peg$c61.test(input.charAt(peg$currPos))) {
3354 s4 = input.charAt(peg$currPos);
3355 peg$currPos++;
3356 } else {
3357 s4 = peg$FAILED;
3358 {
3359 peg$fail(peg$c62);
3360 }
3361 }
3362 if (s4 !== peg$FAILED) {
3363 while (s4 !== peg$FAILED) {
3364 s3.push(s4);
3365 if (peg$c61.test(input.charAt(peg$currPos))) {
3366 s4 = input.charAt(peg$currPos);
3367 peg$currPos++;
3368 } else {
3369 s4 = peg$FAILED;
3370 {
3371 peg$fail(peg$c62);
3372 }
3373 }
3374 }
3375 } else {
3376 s3 = peg$FAILED;
3377 }
3378 if (s3 !== peg$FAILED) {
3379 s4 = peg$parse_();
3380 if (s4 !== peg$FAILED) {
3381 if (input.charCodeAt(peg$currPos) === 41) {
3382 s5 = peg$c69;
3383 peg$currPos++;
3384 } else {
3385 s5 = peg$FAILED;
3386 {
3387 peg$fail(peg$c70);
3388 }
3389 }
3390 if (s5 !== peg$FAILED) {
3391 s1 = peg$c100(s3);
3392 s0 = s1;
3393 } else {
3394 peg$currPos = s0;
3395 s0 = peg$FAILED;
3396 }
3397 } else {
3398 peg$currPos = s0;
3399 s0 = peg$FAILED;
3400 }
3401 } else {
3402 peg$currPos = s0;
3403 s0 = peg$FAILED;
3404 }
3405 } else {
3406 peg$currPos = s0;
3407 s0 = peg$FAILED;
3408 }
3409 } else {
3410 peg$currPos = s0;
3411 s0 = peg$FAILED;
3412 }
3413 peg$resultsCache[key] = {
3414 nextPos: peg$currPos,
3415 result: s0
3416 };
3417 return s0;
3418 }
3419 function peg$parseclass() {
3420 var s0, s1, s2;
3421 var key = peg$currPos * 32 + 31,
3422 cached = peg$resultsCache[key];
3423 if (cached) {
3424 peg$currPos = cached.nextPos;
3425 return cached.result;
3426 }
3427 s0 = peg$currPos;
3428 if (input.charCodeAt(peg$currPos) === 58) {
3429 s1 = peg$c101;
3430 peg$currPos++;
3431 } else {
3432 s1 = peg$FAILED;
3433 {
3434 peg$fail(peg$c102);
3435 }
3436 }
3437 if (s1 !== peg$FAILED) {
3438 s2 = peg$parseidentifierName();
3439 if (s2 !== peg$FAILED) {
3440 s1 = peg$c103(s2);
3441 s0 = s1;
3442 } else {
3443 peg$currPos = s0;
3444 s0 = peg$FAILED;
3445 }
3446 } else {
3447 peg$currPos = s0;
3448 s0 = peg$FAILED;
3449 }
3450 peg$resultsCache[key] = {
3451 nextPos: peg$currPos,
3452 result: s0
3453 };
3454 return s0;
3455 }
3456 function nth(n) {
3457 return {
3458 type: 'nth-child',
3459 index: {
3460 type: 'literal',
3461 value: n
3462 }
3463 };
3464 }
3465 function nthLast(n) {
3466 return {
3467 type: 'nth-last-child',
3468 index: {
3469 type: 'literal',
3470 value: n
3471 }
3472 };
3473 }
3474 function strUnescape(s) {
3475 return s.replace(/\\(.)/g, function (match, ch) {
3476 switch (ch) {
3477 case 'b':
3478 return '\b';
3479 case 'f':
3480 return '\f';
3481 case 'n':
3482 return '\n';
3483 case 'r':
3484 return '\r';
3485 case 't':
3486 return '\t';
3487 case 'v':
3488 return '\v';
3489 default:
3490 return ch;
3491 }
3492 });
3493 }
3494 peg$result = peg$startRuleFunction();
3495 if (peg$result !== peg$FAILED && peg$currPos === input.length) {
3496 return peg$result;
3497 } else {
3498 if (peg$result !== peg$FAILED && peg$currPos < input.length) {
3499 peg$fail(peg$endExpectation());
3500 }
3501 throw peg$buildStructuredError(peg$maxFailExpected, peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null, peg$maxFailPos < input.length ? peg$computeLocation(peg$maxFailPos, peg$maxFailPos + 1) : peg$computeLocation(peg$maxFailPos, peg$maxFailPos));
3502 }
3503 }
3504 return {
3505 SyntaxError: peg$SyntaxError,
3506 parse: peg$parse
3507 };
3508 });
3509 });
3510
3511 /**
3512 * @typedef {"LEFT_SIDE"|"RIGHT_SIDE"} Side
3513 */
3514
3515 var LEFT_SIDE = 'LEFT_SIDE';
3516 var RIGHT_SIDE = 'RIGHT_SIDE';
3517
3518 /**
3519 * @external AST
3520 * @see https://esprima.readthedocs.io/en/latest/syntax-tree-format.html
3521 */
3522
3523 /**
3524 * One of the rules of `grammar.pegjs`
3525 * @typedef {PlainObject} SelectorAST
3526 * @see grammar.pegjs
3527 */
3528
3529 /**
3530 * The `sequence` production of `grammar.pegjs`
3531 * @typedef {PlainObject} SelectorSequenceAST
3532 */
3533
3534 /**
3535 * Get the value of a property which may be multiple levels down
3536 * in the object.
3537 * @param {?PlainObject} obj
3538 * @param {string[]} keys
3539 * @returns {undefined|boolean|string|number|external:AST}
3540 */
3541 function getPath(obj, keys) {
3542 for (var i = 0; i < keys.length; ++i) {
3543 if (obj == null) {
3544 return obj;
3545 }
3546 obj = obj[keys[i]];
3547 }
3548 return obj;
3549 }
3550
3551 /**
3552 * Determine whether `node` can be reached by following `path`,
3553 * starting at `ancestor`.
3554 * @param {?external:AST} node
3555 * @param {?external:AST} ancestor
3556 * @param {string[]} path
3557 * @param {Integer} fromPathIndex
3558 * @returns {boolean}
3559 */
3560 function inPath(node, ancestor, path, fromPathIndex) {
3561 var current = ancestor;
3562 for (var i = fromPathIndex; i < path.length; ++i) {
3563 if (current == null) {
3564 return false;
3565 }
3566 var field = current[path[i]];
3567 if (Array.isArray(field)) {
3568 for (var k = 0; k < field.length; ++k) {
3569 if (inPath(node, field[k], path, i + 1)) {
3570 return true;
3571 }
3572 }
3573 return false;
3574 }
3575 current = field;
3576 }
3577 return node === current;
3578 }
3579
3580 /**
3581 * A generated matcher function for a selector.
3582 * @callback SelectorMatcher
3583 * @param {?SelectorAST} selector
3584 * @param {external:AST[]} [ancestry=[]]
3585 * @param {ESQueryOptions} [options]
3586 * @returns {void}
3587 */
3588
3589 /**
3590 * A WeakMap for holding cached matcher functions for selectors.
3591 * @type {WeakMap<SelectorAST, SelectorMatcher>}
3592 */
3593 var MATCHER_CACHE = typeof WeakMap === 'function' ? new WeakMap() : null;
3594
3595 /**
3596 * Look up a matcher function for `selector` in the cache.
3597 * If it does not exist, generate it with `generateMatcher` and add it to the cache.
3598 * In engines without WeakMap, the caching is skipped and matchers are generated with every call.
3599 * @param {?SelectorAST} selector
3600 * @returns {SelectorMatcher}
3601 */
3602 function getMatcher(selector) {
3603 if (selector == null) {
3604 return function () {
3605 return true;
3606 };
3607 }
3608 if (MATCHER_CACHE != null) {
3609 var matcher = MATCHER_CACHE.get(selector);
3610 if (matcher != null) {
3611 return matcher;
3612 }
3613 matcher = generateMatcher(selector);
3614 MATCHER_CACHE.set(selector, matcher);
3615 return matcher;
3616 }
3617 return generateMatcher(selector);
3618 }
3619
3620 /**
3621 * Create a matcher function for `selector`,
3622 * @param {?SelectorAST} selector
3623 * @returns {SelectorMatcher}
3624 */
3625 function generateMatcher(selector) {
3626 switch (selector.type) {
3627 case 'wildcard':
3628 return function () {
3629 return true;
3630 };
3631 case 'identifier':
3632 {
3633 var value = selector.value.toLowerCase();
3634 return function (node, ancestry, options) {
3635 var nodeTypeKey = options && options.nodeTypeKey || 'type';
3636 return value === node[nodeTypeKey].toLowerCase();
3637 };
3638 }
3639 case 'exactNode':
3640 return function (node, ancestry) {
3641 return ancestry.length === 0;
3642 };
3643 case 'field':
3644 {
3645 var path = selector.name.split('.');
3646 return function (node, ancestry) {
3647 var ancestor = ancestry[path.length - 1];
3648 return inPath(node, ancestor, path, 0);
3649 };
3650 }
3651 case 'matches':
3652 {
3653 var matchers = selector.selectors.map(getMatcher);
3654 return function (node, ancestry, options) {
3655 for (var i = 0; i < matchers.length; ++i) {
3656 if (matchers[i](node, ancestry, options)) {
3657 return true;
3658 }
3659 }
3660 return false;
3661 };
3662 }
3663 case 'compound':
3664 {
3665 var _matchers = selector.selectors.map(getMatcher);
3666 return function (node, ancestry, options) {
3667 for (var i = 0; i < _matchers.length; ++i) {
3668 if (!_matchers[i](node, ancestry, options)) {
3669 return false;
3670 }
3671 }
3672 return true;
3673 };
3674 }
3675 case 'not':
3676 {
3677 var _matchers2 = selector.selectors.map(getMatcher);
3678 return function (node, ancestry, options) {
3679 for (var i = 0; i < _matchers2.length; ++i) {
3680 if (_matchers2[i](node, ancestry, options)) {
3681 return false;
3682 }
3683 }
3684 return true;
3685 };
3686 }
3687 case 'has':
3688 {
3689 var _matchers3 = selector.selectors.map(getMatcher);
3690 return function (node, ancestry, options) {
3691 var result = false;
3692 var a = [];
3693 estraverse.traverse(node, {
3694 enter: function enter(node, parent) {
3695 if (parent != null) {
3696 a.unshift(parent);
3697 }
3698 for (var i = 0; i < _matchers3.length; ++i) {
3699 if (_matchers3[i](node, a, options)) {
3700 result = true;
3701 this["break"]();
3702 return;
3703 }
3704 }
3705 },
3706 leave: function leave() {
3707 a.shift();
3708 },
3709 keys: options && options.visitorKeys,
3710 fallback: options && options.fallback || 'iteration'
3711 });
3712 return result;
3713 };
3714 }
3715 case 'child':
3716 {
3717 var left = getMatcher(selector.left);
3718 var right = getMatcher(selector.right);
3719 return function (node, ancestry, options) {
3720 if (ancestry.length > 0 && right(node, ancestry, options)) {
3721 return left(ancestry[0], ancestry.slice(1), options);
3722 }
3723 return false;
3724 };
3725 }
3726 case 'descendant':
3727 {
3728 var _left = getMatcher(selector.left);
3729 var _right = getMatcher(selector.right);
3730 return function (node, ancestry, options) {
3731 if (_right(node, ancestry, options)) {
3732 for (var i = 0, l = ancestry.length; i < l; ++i) {
3733 if (_left(ancestry[i], ancestry.slice(i + 1), options)) {
3734 return true;
3735 }
3736 }
3737 }
3738 return false;
3739 };
3740 }
3741 case 'attribute':
3742 {
3743 var _path = selector.name.split('.');
3744 switch (selector.operator) {
3745 case void 0:
3746 return function (node) {
3747 return getPath(node, _path) != null;
3748 };
3749 case '=':
3750 switch (selector.value.type) {
3751 case 'regexp':
3752 return function (node) {
3753 var p = getPath(node, _path);
3754 return typeof p === 'string' && selector.value.value.test(p);
3755 };
3756 case 'literal':
3757 {
3758 var literal = "".concat(selector.value.value);
3759 return function (node) {
3760 return literal === "".concat(getPath(node, _path));
3761 };
3762 }
3763 case 'type':
3764 return function (node) {
3765 return selector.value.value === _typeof(getPath(node, _path));
3766 };
3767 }
3768 throw new Error("Unknown selector value type: ".concat(selector.value.type));
3769 case '!=':
3770 switch (selector.value.type) {
3771 case 'regexp':
3772 return function (node) {
3773 return !selector.value.value.test(getPath(node, _path));
3774 };
3775 case 'literal':
3776 {
3777 var _literal = "".concat(selector.value.value);
3778 return function (node) {
3779 return _literal !== "".concat(getPath(node, _path));
3780 };
3781 }
3782 case 'type':
3783 return function (node) {
3784 return selector.value.value !== _typeof(getPath(node, _path));
3785 };
3786 }
3787 throw new Error("Unknown selector value type: ".concat(selector.value.type));
3788 case '<=':
3789 return function (node) {
3790 return getPath(node, _path) <= selector.value.value;
3791 };
3792 case '<':
3793 return function (node) {
3794 return getPath(node, _path) < selector.value.value;
3795 };
3796 case '>':
3797 return function (node) {
3798 return getPath(node, _path) > selector.value.value;
3799 };
3800 case '>=':
3801 return function (node) {
3802 return getPath(node, _path) >= selector.value.value;
3803 };
3804 }
3805 throw new Error("Unknown operator: ".concat(selector.operator));
3806 }
3807 case 'sibling':
3808 {
3809 var _left2 = getMatcher(selector.left);
3810 var _right2 = getMatcher(selector.right);
3811 return function (node, ancestry, options) {
3812 return _right2(node, ancestry, options) && sibling(node, _left2, ancestry, LEFT_SIDE, options) || selector.left.subject && _left2(node, ancestry, options) && sibling(node, _right2, ancestry, RIGHT_SIDE, options);
3813 };
3814 }
3815 case 'adjacent':
3816 {
3817 var _left3 = getMatcher(selector.left);
3818 var _right3 = getMatcher(selector.right);
3819 return function (node, ancestry, options) {
3820 return _right3(node, ancestry, options) && adjacent(node, _left3, ancestry, LEFT_SIDE, options) || selector.right.subject && _left3(node, ancestry, options) && adjacent(node, _right3, ancestry, RIGHT_SIDE, options);
3821 };
3822 }
3823 case 'nth-child':
3824 {
3825 var nth = selector.index.value;
3826 var _right4 = getMatcher(selector.right);
3827 return function (node, ancestry, options) {
3828 return _right4(node, ancestry, options) && nthChild(node, ancestry, nth, options);
3829 };
3830 }
3831 case 'nth-last-child':
3832 {
3833 var _nth = -selector.index.value;
3834 var _right5 = getMatcher(selector.right);
3835 return function (node, ancestry, options) {
3836 return _right5(node, ancestry, options) && nthChild(node, ancestry, _nth, options);
3837 };
3838 }
3839 case 'class':
3840 {
3841 var name = selector.name.toLowerCase();
3842 return function (node, ancestry, options) {
3843 if (options && options.matchClass) {
3844 return options.matchClass(selector.name, node, ancestry);
3845 }
3846 if (options && options.nodeTypeKey) return false;
3847 switch (name) {
3848 case 'statement':
3849 if (node.type.slice(-9) === 'Statement') return true;
3850 // fallthrough: interface Declaration <: Statement { }
3851 case 'declaration':
3852 return node.type.slice(-11) === 'Declaration';
3853 case 'pattern':
3854 if (node.type.slice(-7) === 'Pattern') return true;
3855 // fallthrough: interface Expression <: Node, Pattern { }
3856 case 'expression':
3857 return node.type.slice(-10) === 'Expression' || node.type.slice(-7) === 'Literal' || node.type === 'Identifier' && (ancestry.length === 0 || ancestry[0].type !== 'MetaProperty') || node.type === 'MetaProperty';
3858 case 'function':
3859 return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
3860 }
3861 throw new Error("Unknown class name: ".concat(selector.name));
3862 };
3863 }
3864 }
3865 throw new Error("Unknown selector type: ".concat(selector.type));
3866 }
3867
3868 /**
3869 * @callback TraverseOptionFallback
3870 * @param {external:AST} node The given node.
3871 * @returns {string[]} An array of visitor keys for the given node.
3872 */
3873
3874 /**
3875 * @callback ClassMatcher
3876 * @param {string} className The name of the class to match.
3877 * @param {external:AST} node The node to match against.
3878 * @param {Array<external:AST>} ancestry The ancestry of the node.
3879 * @returns {boolean} True if the node matches the class, false if not.
3880 */
3881
3882 /**
3883 * @typedef {object} ESQueryOptions
3884 * @property {string} [nodeTypeKey="type"] By passing `nodeTypeKey`, we can allow other ASTs to use ESQuery.
3885 * @property { { [nodeType: string]: string[] } } [visitorKeys] By passing `visitorKeys` mapping, we can extend the properties of the nodes that traverse the node.
3886 * @property {TraverseOptionFallback} [fallback] By passing `fallback` option, we can control the properties of traversing nodes when encountering unknown nodes.
3887 * @property {ClassMatcher} [matchClass] By passing `matchClass` option, we can customize the interpretation of classes.
3888 */
3889
3890 /**
3891 * Given a `node` and its ancestors, determine if `node` is matched
3892 * by `selector`.
3893 * @param {?external:AST} node
3894 * @param {?SelectorAST} selector
3895 * @param {external:AST[]} [ancestry=[]]
3896 * @param {ESQueryOptions} [options]
3897 * @throws {Error} Unknowns (operator, class name, selector type, or
3898 * selector value type)
3899 * @returns {boolean}
3900 */
3901 function matches(node, selector, ancestry, options) {
3902 if (!selector) {
3903 return true;
3904 }
3905 if (!node) {
3906 return false;
3907 }
3908 if (!ancestry) {
3909 ancestry = [];
3910 }
3911 return getMatcher(selector)(node, ancestry, options);
3912 }
3913
3914 /**
3915 * Get visitor keys of a given node.
3916 * @param {external:AST} node The AST node to get keys.
3917 * @param {ESQueryOptions|undefined} options
3918 * @returns {string[]} Visitor keys of the node.
3919 */
3920 function getVisitorKeys(node, options) {
3921 var nodeTypeKey = options && options.nodeTypeKey || 'type';
3922 var nodeType = node[nodeTypeKey];
3923 if (options && options.visitorKeys && options.visitorKeys[nodeType]) {
3924 return options.visitorKeys[nodeType];
3925 }
3926 if (estraverse.VisitorKeys[nodeType]) {
3927 return estraverse.VisitorKeys[nodeType];
3928 }
3929 if (options && typeof options.fallback === 'function') {
3930 return options.fallback(node);
3931 }
3932 // 'iteration' fallback
3933 return Object.keys(node).filter(function (key) {
3934 return key !== nodeTypeKey;
3935 });
3936 }
3937
3938 /**
3939 * Check whether the given value is an ASTNode or not.
3940 * @param {any} node The value to check.
3941 * @param {ESQueryOptions|undefined} options The options to use.
3942 * @returns {boolean} `true` if the value is an ASTNode.
3943 */
3944 function isNode(node, options) {
3945 var nodeTypeKey = options && options.nodeTypeKey || 'type';
3946 return node !== null && _typeof(node) === 'object' && typeof node[nodeTypeKey] === 'string';
3947 }
3948
3949 /**
3950 * Determines if the given node has a sibling that matches the
3951 * given selector matcher.
3952 * @param {external:AST} node
3953 * @param {SelectorMatcher} matcher
3954 * @param {external:AST[]} ancestry
3955 * @param {Side} side
3956 * @param {ESQueryOptions|undefined} options
3957 * @returns {boolean}
3958 */
3959 function sibling(node, matcher, ancestry, side, options) {
3960 var _ancestry = _slicedToArray(ancestry, 1),
3961 parent = _ancestry[0];
3962 if (!parent) {
3963 return false;
3964 }
3965 var keys = getVisitorKeys(parent, options);
3966 for (var i = 0; i < keys.length; ++i) {
3967 var listProp = parent[keys[i]];
3968 if (Array.isArray(listProp)) {
3969 var startIndex = listProp.indexOf(node);
3970 if (startIndex < 0) {
3971 continue;
3972 }
3973 var lowerBound = void 0,
3974 upperBound = void 0;
3975 if (side === LEFT_SIDE) {
3976 lowerBound = 0;
3977 upperBound = startIndex;
3978 } else {
3979 lowerBound = startIndex + 1;
3980 upperBound = listProp.length;
3981 }
3982 for (var k = lowerBound; k < upperBound; ++k) {
3983 if (isNode(listProp[k], options) && matcher(listProp[k], ancestry, options)) {
3984 return true;
3985 }
3986 }
3987 }
3988 }
3989 return false;
3990 }
3991
3992 /**
3993 * Determines if the given node has an adjacent sibling that matches
3994 * the given selector matcher.
3995 * @param {external:AST} node
3996 * @param {SelectorMatcher} matcher
3997 * @param {external:AST[]} ancestry
3998 * @param {Side} side
3999 * @param {ESQueryOptions|undefined} options
4000 * @returns {boolean}
4001 */
4002 function adjacent(node, matcher, ancestry, side, options) {
4003 var _ancestry2 = _slicedToArray(ancestry, 1),
4004 parent = _ancestry2[0];
4005 if (!parent) {
4006 return false;
4007 }
4008 var keys = getVisitorKeys(parent, options);
4009 for (var i = 0; i < keys.length; ++i) {
4010 var listProp = parent[keys[i]];
4011 if (Array.isArray(listProp)) {
4012 var idx = listProp.indexOf(node);
4013 if (idx < 0) {
4014 continue;
4015 }
4016 if (side === LEFT_SIDE && idx > 0 && isNode(listProp[idx - 1], options) && matcher(listProp[idx - 1], ancestry, options)) {
4017 return true;
4018 }
4019 if (side === RIGHT_SIDE && idx < listProp.length - 1 && isNode(listProp[idx + 1], options) && matcher(listProp[idx + 1], ancestry, options)) {
4020 return true;
4021 }
4022 }
4023 }
4024 return false;
4025 }
4026
4027 /**
4028 * Determines if the given node is the `nth` child.
4029 * If `nth` is negative then the position is counted
4030 * from the end of the list of children.
4031 * @param {external:AST} node
4032 * @param {external:AST[]} ancestry
4033 * @param {Integer} nth
4034 * @param {ESQueryOptions|undefined} options
4035 * @returns {boolean}
4036 */
4037 function nthChild(node, ancestry, nth, options) {
4038 if (nth === 0) {
4039 return false;
4040 }
4041 var _ancestry3 = _slicedToArray(ancestry, 1),
4042 parent = _ancestry3[0];
4043 if (!parent) {
4044 return false;
4045 }
4046 var keys = getVisitorKeys(parent, options);
4047 for (var i = 0; i < keys.length; ++i) {
4048 var listProp = parent[keys[i]];
4049 if (Array.isArray(listProp)) {
4050 var idx = nth < 0 ? listProp.length + nth : nth - 1;
4051 if (idx >= 0 && idx < listProp.length && listProp[idx] === node) {
4052 return true;
4053 }
4054 }
4055 }
4056 return false;
4057 }
4058
4059 /**
4060 * For each selector node marked as a subject, find the portion of the
4061 * selector that the subject must match.
4062 * @param {SelectorAST} selector
4063 * @param {SelectorAST} [ancestor] Defaults to `selector`
4064 * @returns {SelectorAST[]}
4065 */
4066 function subjects(selector, ancestor) {
4067 if (selector == null || _typeof(selector) != 'object') {
4068 return [];
4069 }
4070 if (ancestor == null) {
4071 ancestor = selector;
4072 }
4073 var results = selector.subject ? [ancestor] : [];
4074 var keys = Object.keys(selector);
4075 for (var i = 0; i < keys.length; ++i) {
4076 var p = keys[i];
4077 var sel = selector[p];
4078 results.push.apply(results, _toConsumableArray(subjects(sel, p === 'left' ? sel : ancestor)));
4079 }
4080 return results;
4081 }
4082
4083 /**
4084 * @callback TraverseVisitor
4085 * @param {?external:AST} node
4086 * @param {?external:AST} parent
4087 * @param {external:AST[]} ancestry
4088 */
4089
4090 /**
4091 * From a JS AST and a selector AST, collect all JS AST nodes that
4092 * match the selector.
4093 * @param {external:AST} ast
4094 * @param {?SelectorAST} selector
4095 * @param {TraverseVisitor} visitor
4096 * @param {ESQueryOptions} [options]
4097 * @returns {external:AST[]}
4098 */
4099 function traverse(ast, selector, visitor, options) {
4100 if (!selector) {
4101 return;
4102 }
4103 var ancestry = [];
4104 var matcher = getMatcher(selector);
4105 var altSubjects = subjects(selector).map(getMatcher);
4106 estraverse.traverse(ast, {
4107 enter: function enter(node, parent) {
4108 if (parent != null) {
4109 ancestry.unshift(parent);
4110 }
4111 if (matcher(node, ancestry, options)) {
4112 if (altSubjects.length) {
4113 for (var i = 0, l = altSubjects.length; i < l; ++i) {
4114 if (altSubjects[i](node, ancestry, options)) {
4115 visitor(node, parent, ancestry);
4116 }
4117 for (var k = 0, m = ancestry.length; k < m; ++k) {
4118 var succeedingAncestry = ancestry.slice(k + 1);
4119 if (altSubjects[i](ancestry[k], succeedingAncestry, options)) {
4120 visitor(ancestry[k], parent, succeedingAncestry);
4121 }
4122 }
4123 }
4124 } else {
4125 visitor(node, parent, ancestry);
4126 }
4127 }
4128 },
4129 leave: function leave() {
4130 ancestry.shift();
4131 },
4132 keys: options && options.visitorKeys,
4133 fallback: options && options.fallback || 'iteration'
4134 });
4135 }
4136
4137 /**
4138 * From a JS AST and a selector AST, collect all JS AST nodes that
4139 * match the selector.
4140 * @param {external:AST} ast
4141 * @param {?SelectorAST} selector
4142 * @param {ESQueryOptions} [options]
4143 * @returns {external:AST[]}
4144 */
4145 function match(ast, selector, options) {
4146 var results = [];
4147 traverse(ast, selector, function (node) {
4148 results.push(node);
4149 }, options);
4150 return results;
4151 }
4152
4153 /**
4154 * Parse a selector string and return its AST.
4155 * @param {string} selector
4156 * @returns {SelectorAST}
4157 */
4158 function parse(selector) {
4159 return parser.parse(selector);
4160 }
4161
4162 /**
4163 * Query the code AST using the selector string.
4164 * @param {external:AST} ast
4165 * @param {string} selector
4166 * @param {ESQueryOptions} [options]
4167 * @returns {external:AST[]}
4168 */
4169 function query(ast, selector, options) {
4170 return match(ast, parse(selector), options);
4171 }
4172 query.parse = parse;
4173 query.match = match;
4174 query.traverse = traverse;
4175 query.matches = matches;
4176 query.query = query;
4177
4178 return query;
4179
4180})));