You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4553 lines
120 KiB

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