esquery.js 130 KB

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