parser.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674
  1. var result = (function(){
  2. /*
  3. * Generated by PEG.js 0.7.0.
  4. *
  5. * http://pegjs.majda.cz/
  6. */
  7. function quote(s) {
  8. /*
  9. * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a
  10. * string literal except for the closing quote character, backslash,
  11. * carriage return, line separator, paragraph separator, and line feed.
  12. * Any character may appear in the form of an escape sequence.
  13. *
  14. * For portability, we also escape escape all control and non-ASCII
  15. * characters. Note that "\0" and "\v" escape sequences are not used
  16. * because JSHint does not like the first and IE the second.
  17. */
  18. return '"' + s
  19. .replace(/\\/g, '\\\\') // backslash
  20. .replace(/"/g, '\\"') // closing quote character
  21. .replace(/\x08/g, '\\b') // backspace
  22. .replace(/\t/g, '\\t') // horizontal tab
  23. .replace(/\n/g, '\\n') // line feed
  24. .replace(/\f/g, '\\f') // form feed
  25. .replace(/\r/g, '\\r') // carriage return
  26. .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape)
  27. + '"';
  28. }
  29. var result = {
  30. /*
  31. * Parses the input with a generated parser. If the parsing is successfull,
  32. * returns a value explicitly or implicitly specified by the grammar from
  33. * which the parser was generated (see |PEG.buildParser|). If the parsing is
  34. * unsuccessful, throws |PEG.parser.SyntaxError| describing the error.
  35. */
  36. parse: function(input, startRule) {
  37. var parseFunctions = {
  38. "start": parse_start,
  39. "_": parse__,
  40. "identifierName": parse_identifierName,
  41. "binaryOp": parse_binaryOp,
  42. "selectors": parse_selectors,
  43. "selector": parse_selector,
  44. "sequence": parse_sequence,
  45. "atom": parse_atom,
  46. "wildcard": parse_wildcard,
  47. "identifier": parse_identifier,
  48. "attr": parse_attr,
  49. "attrOps": parse_attrOps,
  50. "attrEqOps": parse_attrEqOps,
  51. "attrName": parse_attrName,
  52. "attrValue": parse_attrValue,
  53. "string": parse_string,
  54. "number": parse_number,
  55. "path": parse_path,
  56. "type": parse_type,
  57. "regex": parse_regex,
  58. "field": parse_field,
  59. "negation": parse_negation,
  60. "matches": parse_matches,
  61. "has": parse_has,
  62. "firstChild": parse_firstChild,
  63. "lastChild": parse_lastChild,
  64. "nthChild": parse_nthChild,
  65. "nthLastChild": parse_nthLastChild,
  66. "class": parse_class
  67. };
  68. if (startRule !== undefined) {
  69. if (parseFunctions[startRule] === undefined) {
  70. throw new Error("Invalid rule name: " + quote(startRule) + ".");
  71. }
  72. } else {
  73. startRule = "start";
  74. }
  75. var pos = 0;
  76. var reportFailures = 0;
  77. var rightmostFailuresPos = 0;
  78. var rightmostFailuresExpected = [];
  79. var cache = {};
  80. function padLeft(input, padding, length) {
  81. var result = input;
  82. var padLength = length - input.length;
  83. for (var i = 0; i < padLength; i++) {
  84. result = padding + result;
  85. }
  86. return result;
  87. }
  88. function escape(ch) {
  89. var charCode = ch.charCodeAt(0);
  90. var escapeChar;
  91. var length;
  92. if (charCode <= 0xFF) {
  93. escapeChar = 'x';
  94. length = 2;
  95. } else {
  96. escapeChar = 'u';
  97. length = 4;
  98. }
  99. return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);
  100. }
  101. function matchFailed(failure) {
  102. if (pos < rightmostFailuresPos) {
  103. return;
  104. }
  105. if (pos > rightmostFailuresPos) {
  106. rightmostFailuresPos = pos;
  107. rightmostFailuresExpected = [];
  108. }
  109. rightmostFailuresExpected.push(failure);
  110. }
  111. function parse_start() {
  112. var cacheKey = "start@" + pos;
  113. var cachedResult = cache[cacheKey];
  114. if (cachedResult) {
  115. pos = cachedResult.nextPos;
  116. return cachedResult.result;
  117. }
  118. var result0, result1, result2;
  119. var pos0, pos1;
  120. pos0 = pos;
  121. pos1 = pos;
  122. result0 = parse__();
  123. if (result0 !== null) {
  124. result1 = parse_selectors();
  125. if (result1 !== null) {
  126. result2 = parse__();
  127. if (result2 !== null) {
  128. result0 = [result0, result1, result2];
  129. } else {
  130. result0 = null;
  131. pos = pos1;
  132. }
  133. } else {
  134. result0 = null;
  135. pos = pos1;
  136. }
  137. } else {
  138. result0 = null;
  139. pos = pos1;
  140. }
  141. if (result0 !== null) {
  142. result0 = (function(offset, ss) { return ss.length === 1 ? ss[0] : { type: 'matches', selectors: ss }; })(pos0, result0[1]);
  143. }
  144. if (result0 === null) {
  145. pos = pos0;
  146. }
  147. if (result0 === null) {
  148. pos0 = pos;
  149. result0 = parse__();
  150. if (result0 !== null) {
  151. result0 = (function(offset) { return void 0; })(pos0);
  152. }
  153. if (result0 === null) {
  154. pos = pos0;
  155. }
  156. }
  157. cache[cacheKey] = {
  158. nextPos: pos,
  159. result: result0
  160. };
  161. return result0;
  162. }
  163. function parse__() {
  164. var cacheKey = "_@" + pos;
  165. var cachedResult = cache[cacheKey];
  166. if (cachedResult) {
  167. pos = cachedResult.nextPos;
  168. return cachedResult.result;
  169. }
  170. var result0, result1;
  171. result0 = [];
  172. if (input.charCodeAt(pos) === 32) {
  173. result1 = " ";
  174. pos++;
  175. } else {
  176. result1 = null;
  177. if (reportFailures === 0) {
  178. matchFailed("\" \"");
  179. }
  180. }
  181. while (result1 !== null) {
  182. result0.push(result1);
  183. if (input.charCodeAt(pos) === 32) {
  184. result1 = " ";
  185. pos++;
  186. } else {
  187. result1 = null;
  188. if (reportFailures === 0) {
  189. matchFailed("\" \"");
  190. }
  191. }
  192. }
  193. cache[cacheKey] = {
  194. nextPos: pos,
  195. result: result0
  196. };
  197. return result0;
  198. }
  199. function parse_identifierName() {
  200. var cacheKey = "identifierName@" + pos;
  201. var cachedResult = cache[cacheKey];
  202. if (cachedResult) {
  203. pos = cachedResult.nextPos;
  204. return cachedResult.result;
  205. }
  206. var result0, result1;
  207. var pos0;
  208. pos0 = pos;
  209. if (/^[^ [\],():#!=><~+.]/.test(input.charAt(pos))) {
  210. result1 = input.charAt(pos);
  211. pos++;
  212. } else {
  213. result1 = null;
  214. if (reportFailures === 0) {
  215. matchFailed("[^ [\\],():#!=><~+.]");
  216. }
  217. }
  218. if (result1 !== null) {
  219. result0 = [];
  220. while (result1 !== null) {
  221. result0.push(result1);
  222. if (/^[^ [\],():#!=><~+.]/.test(input.charAt(pos))) {
  223. result1 = input.charAt(pos);
  224. pos++;
  225. } else {
  226. result1 = null;
  227. if (reportFailures === 0) {
  228. matchFailed("[^ [\\],():#!=><~+.]");
  229. }
  230. }
  231. }
  232. } else {
  233. result0 = null;
  234. }
  235. if (result0 !== null) {
  236. result0 = (function(offset, i) { return i.join(''); })(pos0, result0);
  237. }
  238. if (result0 === null) {
  239. pos = pos0;
  240. }
  241. cache[cacheKey] = {
  242. nextPos: pos,
  243. result: result0
  244. };
  245. return result0;
  246. }
  247. function parse_binaryOp() {
  248. var cacheKey = "binaryOp@" + pos;
  249. var cachedResult = cache[cacheKey];
  250. if (cachedResult) {
  251. pos = cachedResult.nextPos;
  252. return cachedResult.result;
  253. }
  254. var result0, result1, result2;
  255. var pos0, pos1;
  256. pos0 = pos;
  257. pos1 = pos;
  258. result0 = parse__();
  259. if (result0 !== null) {
  260. if (input.charCodeAt(pos) === 62) {
  261. result1 = ">";
  262. pos++;
  263. } else {
  264. result1 = null;
  265. if (reportFailures === 0) {
  266. matchFailed("\">\"");
  267. }
  268. }
  269. if (result1 !== null) {
  270. result2 = parse__();
  271. if (result2 !== null) {
  272. result0 = [result0, result1, result2];
  273. } else {
  274. result0 = null;
  275. pos = pos1;
  276. }
  277. } else {
  278. result0 = null;
  279. pos = pos1;
  280. }
  281. } else {
  282. result0 = null;
  283. pos = pos1;
  284. }
  285. if (result0 !== null) {
  286. result0 = (function(offset) { return 'child'; })(pos0);
  287. }
  288. if (result0 === null) {
  289. pos = pos0;
  290. }
  291. if (result0 === null) {
  292. pos0 = pos;
  293. pos1 = pos;
  294. result0 = parse__();
  295. if (result0 !== null) {
  296. if (input.charCodeAt(pos) === 126) {
  297. result1 = "~";
  298. pos++;
  299. } else {
  300. result1 = null;
  301. if (reportFailures === 0) {
  302. matchFailed("\"~\"");
  303. }
  304. }
  305. if (result1 !== null) {
  306. result2 = parse__();
  307. if (result2 !== null) {
  308. result0 = [result0, result1, result2];
  309. } else {
  310. result0 = null;
  311. pos = pos1;
  312. }
  313. } else {
  314. result0 = null;
  315. pos = pos1;
  316. }
  317. } else {
  318. result0 = null;
  319. pos = pos1;
  320. }
  321. if (result0 !== null) {
  322. result0 = (function(offset) { return 'sibling'; })(pos0);
  323. }
  324. if (result0 === null) {
  325. pos = pos0;
  326. }
  327. if (result0 === null) {
  328. pos0 = pos;
  329. pos1 = pos;
  330. result0 = parse__();
  331. if (result0 !== null) {
  332. if (input.charCodeAt(pos) === 43) {
  333. result1 = "+";
  334. pos++;
  335. } else {
  336. result1 = null;
  337. if (reportFailures === 0) {
  338. matchFailed("\"+\"");
  339. }
  340. }
  341. if (result1 !== null) {
  342. result2 = parse__();
  343. if (result2 !== null) {
  344. result0 = [result0, result1, result2];
  345. } else {
  346. result0 = null;
  347. pos = pos1;
  348. }
  349. } else {
  350. result0 = null;
  351. pos = pos1;
  352. }
  353. } else {
  354. result0 = null;
  355. pos = pos1;
  356. }
  357. if (result0 !== null) {
  358. result0 = (function(offset) { return 'adjacent'; })(pos0);
  359. }
  360. if (result0 === null) {
  361. pos = pos0;
  362. }
  363. if (result0 === null) {
  364. pos0 = pos;
  365. pos1 = pos;
  366. if (input.charCodeAt(pos) === 32) {
  367. result0 = " ";
  368. pos++;
  369. } else {
  370. result0 = null;
  371. if (reportFailures === 0) {
  372. matchFailed("\" \"");
  373. }
  374. }
  375. if (result0 !== null) {
  376. result1 = parse__();
  377. if (result1 !== null) {
  378. result0 = [result0, result1];
  379. } else {
  380. result0 = null;
  381. pos = pos1;
  382. }
  383. } else {
  384. result0 = null;
  385. pos = pos1;
  386. }
  387. if (result0 !== null) {
  388. result0 = (function(offset) { return 'descendant'; })(pos0);
  389. }
  390. if (result0 === null) {
  391. pos = pos0;
  392. }
  393. }
  394. }
  395. }
  396. cache[cacheKey] = {
  397. nextPos: pos,
  398. result: result0
  399. };
  400. return result0;
  401. }
  402. function parse_selectors() {
  403. var cacheKey = "selectors@" + pos;
  404. var cachedResult = cache[cacheKey];
  405. if (cachedResult) {
  406. pos = cachedResult.nextPos;
  407. return cachedResult.result;
  408. }
  409. var result0, result1, result2, result3, result4, result5;
  410. var pos0, pos1, pos2;
  411. pos0 = pos;
  412. pos1 = pos;
  413. result0 = parse_selector();
  414. if (result0 !== null) {
  415. result1 = [];
  416. pos2 = pos;
  417. result2 = parse__();
  418. if (result2 !== null) {
  419. if (input.charCodeAt(pos) === 44) {
  420. result3 = ",";
  421. pos++;
  422. } else {
  423. result3 = null;
  424. if (reportFailures === 0) {
  425. matchFailed("\",\"");
  426. }
  427. }
  428. if (result3 !== null) {
  429. result4 = parse__();
  430. if (result4 !== null) {
  431. result5 = parse_selector();
  432. if (result5 !== null) {
  433. result2 = [result2, result3, result4, result5];
  434. } else {
  435. result2 = null;
  436. pos = pos2;
  437. }
  438. } else {
  439. result2 = null;
  440. pos = pos2;
  441. }
  442. } else {
  443. result2 = null;
  444. pos = pos2;
  445. }
  446. } else {
  447. result2 = null;
  448. pos = pos2;
  449. }
  450. while (result2 !== null) {
  451. result1.push(result2);
  452. pos2 = pos;
  453. result2 = parse__();
  454. if (result2 !== null) {
  455. if (input.charCodeAt(pos) === 44) {
  456. result3 = ",";
  457. pos++;
  458. } else {
  459. result3 = null;
  460. if (reportFailures === 0) {
  461. matchFailed("\",\"");
  462. }
  463. }
  464. if (result3 !== null) {
  465. result4 = parse__();
  466. if (result4 !== null) {
  467. result5 = parse_selector();
  468. if (result5 !== null) {
  469. result2 = [result2, result3, result4, result5];
  470. } else {
  471. result2 = null;
  472. pos = pos2;
  473. }
  474. } else {
  475. result2 = null;
  476. pos = pos2;
  477. }
  478. } else {
  479. result2 = null;
  480. pos = pos2;
  481. }
  482. } else {
  483. result2 = null;
  484. pos = pos2;
  485. }
  486. }
  487. if (result1 !== null) {
  488. result0 = [result0, result1];
  489. } else {
  490. result0 = null;
  491. pos = pos1;
  492. }
  493. } else {
  494. result0 = null;
  495. pos = pos1;
  496. }
  497. if (result0 !== null) {
  498. result0 = (function(offset, s, ss) {
  499. return [s].concat(ss.map(function (s) { return s[3]; }));
  500. })(pos0, result0[0], result0[1]);
  501. }
  502. if (result0 === null) {
  503. pos = pos0;
  504. }
  505. cache[cacheKey] = {
  506. nextPos: pos,
  507. result: result0
  508. };
  509. return result0;
  510. }
  511. function parse_selector() {
  512. var cacheKey = "selector@" + pos;
  513. var cachedResult = cache[cacheKey];
  514. if (cachedResult) {
  515. pos = cachedResult.nextPos;
  516. return cachedResult.result;
  517. }
  518. var result0, result1, result2, result3;
  519. var pos0, pos1, pos2;
  520. pos0 = pos;
  521. pos1 = pos;
  522. result0 = parse_sequence();
  523. if (result0 !== null) {
  524. result1 = [];
  525. pos2 = pos;
  526. result2 = parse_binaryOp();
  527. if (result2 !== null) {
  528. result3 = parse_sequence();
  529. if (result3 !== null) {
  530. result2 = [result2, result3];
  531. } else {
  532. result2 = null;
  533. pos = pos2;
  534. }
  535. } else {
  536. result2 = null;
  537. pos = pos2;
  538. }
  539. while (result2 !== null) {
  540. result1.push(result2);
  541. pos2 = pos;
  542. result2 = parse_binaryOp();
  543. if (result2 !== null) {
  544. result3 = parse_sequence();
  545. if (result3 !== null) {
  546. result2 = [result2, result3];
  547. } else {
  548. result2 = null;
  549. pos = pos2;
  550. }
  551. } else {
  552. result2 = null;
  553. pos = pos2;
  554. }
  555. }
  556. if (result1 !== null) {
  557. result0 = [result0, result1];
  558. } else {
  559. result0 = null;
  560. pos = pos1;
  561. }
  562. } else {
  563. result0 = null;
  564. pos = pos1;
  565. }
  566. if (result0 !== null) {
  567. result0 = (function(offset, a, ops) {
  568. return ops.reduce(function (memo, rhs) {
  569. return { type: rhs[0], left: memo, right: rhs[1] };
  570. }, a);
  571. })(pos0, result0[0], result0[1]);
  572. }
  573. if (result0 === null) {
  574. pos = pos0;
  575. }
  576. cache[cacheKey] = {
  577. nextPos: pos,
  578. result: result0
  579. };
  580. return result0;
  581. }
  582. function parse_sequence() {
  583. var cacheKey = "sequence@" + pos;
  584. var cachedResult = cache[cacheKey];
  585. if (cachedResult) {
  586. pos = cachedResult.nextPos;
  587. return cachedResult.result;
  588. }
  589. var result0, result1, result2;
  590. var pos0, pos1;
  591. pos0 = pos;
  592. pos1 = pos;
  593. if (input.charCodeAt(pos) === 33) {
  594. result0 = "!";
  595. pos++;
  596. } else {
  597. result0 = null;
  598. if (reportFailures === 0) {
  599. matchFailed("\"!\"");
  600. }
  601. }
  602. result0 = result0 !== null ? result0 : "";
  603. if (result0 !== null) {
  604. result2 = parse_atom();
  605. if (result2 !== null) {
  606. result1 = [];
  607. while (result2 !== null) {
  608. result1.push(result2);
  609. result2 = parse_atom();
  610. }
  611. } else {
  612. result1 = null;
  613. }
  614. if (result1 !== null) {
  615. result0 = [result0, result1];
  616. } else {
  617. result0 = null;
  618. pos = pos1;
  619. }
  620. } else {
  621. result0 = null;
  622. pos = pos1;
  623. }
  624. if (result0 !== null) {
  625. result0 = (function(offset, subject, as) {
  626. var b = as.length === 1 ? as[0] : { type: 'compound', selectors: as };
  627. if(subject) b.subject = true;
  628. return b;
  629. })(pos0, result0[0], result0[1]);
  630. }
  631. if (result0 === null) {
  632. pos = pos0;
  633. }
  634. cache[cacheKey] = {
  635. nextPos: pos,
  636. result: result0
  637. };
  638. return result0;
  639. }
  640. function parse_atom() {
  641. var cacheKey = "atom@" + pos;
  642. var cachedResult = cache[cacheKey];
  643. if (cachedResult) {
  644. pos = cachedResult.nextPos;
  645. return cachedResult.result;
  646. }
  647. var result0;
  648. result0 = parse_wildcard();
  649. if (result0 === null) {
  650. result0 = parse_identifier();
  651. if (result0 === null) {
  652. result0 = parse_attr();
  653. if (result0 === null) {
  654. result0 = parse_field();
  655. if (result0 === null) {
  656. result0 = parse_negation();
  657. if (result0 === null) {
  658. result0 = parse_matches();
  659. if (result0 === null) {
  660. result0 = parse_has();
  661. if (result0 === null) {
  662. result0 = parse_firstChild();
  663. if (result0 === null) {
  664. result0 = parse_lastChild();
  665. if (result0 === null) {
  666. result0 = parse_nthChild();
  667. if (result0 === null) {
  668. result0 = parse_nthLastChild();
  669. if (result0 === null) {
  670. result0 = parse_class();
  671. }
  672. }
  673. }
  674. }
  675. }
  676. }
  677. }
  678. }
  679. }
  680. }
  681. }
  682. cache[cacheKey] = {
  683. nextPos: pos,
  684. result: result0
  685. };
  686. return result0;
  687. }
  688. function parse_wildcard() {
  689. var cacheKey = "wildcard@" + pos;
  690. var cachedResult = cache[cacheKey];
  691. if (cachedResult) {
  692. pos = cachedResult.nextPos;
  693. return cachedResult.result;
  694. }
  695. var result0;
  696. var pos0;
  697. pos0 = pos;
  698. if (input.charCodeAt(pos) === 42) {
  699. result0 = "*";
  700. pos++;
  701. } else {
  702. result0 = null;
  703. if (reportFailures === 0) {
  704. matchFailed("\"*\"");
  705. }
  706. }
  707. if (result0 !== null) {
  708. result0 = (function(offset, a) { return { type: 'wildcard', value: a }; })(pos0, result0);
  709. }
  710. if (result0 === null) {
  711. pos = pos0;
  712. }
  713. cache[cacheKey] = {
  714. nextPos: pos,
  715. result: result0
  716. };
  717. return result0;
  718. }
  719. function parse_identifier() {
  720. var cacheKey = "identifier@" + pos;
  721. var cachedResult = cache[cacheKey];
  722. if (cachedResult) {
  723. pos = cachedResult.nextPos;
  724. return cachedResult.result;
  725. }
  726. var result0, result1;
  727. var pos0, pos1;
  728. pos0 = pos;
  729. pos1 = pos;
  730. if (input.charCodeAt(pos) === 35) {
  731. result0 = "#";
  732. pos++;
  733. } else {
  734. result0 = null;
  735. if (reportFailures === 0) {
  736. matchFailed("\"#\"");
  737. }
  738. }
  739. result0 = result0 !== null ? result0 : "";
  740. if (result0 !== null) {
  741. result1 = parse_identifierName();
  742. if (result1 !== null) {
  743. result0 = [result0, result1];
  744. } else {
  745. result0 = null;
  746. pos = pos1;
  747. }
  748. } else {
  749. result0 = null;
  750. pos = pos1;
  751. }
  752. if (result0 !== null) {
  753. result0 = (function(offset, i) { return { type: 'identifier', value: i }; })(pos0, result0[1]);
  754. }
  755. if (result0 === null) {
  756. pos = pos0;
  757. }
  758. cache[cacheKey] = {
  759. nextPos: pos,
  760. result: result0
  761. };
  762. return result0;
  763. }
  764. function parse_attr() {
  765. var cacheKey = "attr@" + pos;
  766. var cachedResult = cache[cacheKey];
  767. if (cachedResult) {
  768. pos = cachedResult.nextPos;
  769. return cachedResult.result;
  770. }
  771. var result0, result1, result2, result3, result4;
  772. var pos0, pos1;
  773. pos0 = pos;
  774. pos1 = pos;
  775. if (input.charCodeAt(pos) === 91) {
  776. result0 = "[";
  777. pos++;
  778. } else {
  779. result0 = null;
  780. if (reportFailures === 0) {
  781. matchFailed("\"[\"");
  782. }
  783. }
  784. if (result0 !== null) {
  785. result1 = parse__();
  786. if (result1 !== null) {
  787. result2 = parse_attrValue();
  788. if (result2 !== null) {
  789. result3 = parse__();
  790. if (result3 !== null) {
  791. if (input.charCodeAt(pos) === 93) {
  792. result4 = "]";
  793. pos++;
  794. } else {
  795. result4 = null;
  796. if (reportFailures === 0) {
  797. matchFailed("\"]\"");
  798. }
  799. }
  800. if (result4 !== null) {
  801. result0 = [result0, result1, result2, result3, result4];
  802. } else {
  803. result0 = null;
  804. pos = pos1;
  805. }
  806. } else {
  807. result0 = null;
  808. pos = pos1;
  809. }
  810. } else {
  811. result0 = null;
  812. pos = pos1;
  813. }
  814. } else {
  815. result0 = null;
  816. pos = pos1;
  817. }
  818. } else {
  819. result0 = null;
  820. pos = pos1;
  821. }
  822. if (result0 !== null) {
  823. result0 = (function(offset, v) { return v; })(pos0, result0[2]);
  824. }
  825. if (result0 === null) {
  826. pos = pos0;
  827. }
  828. cache[cacheKey] = {
  829. nextPos: pos,
  830. result: result0
  831. };
  832. return result0;
  833. }
  834. function parse_attrOps() {
  835. var cacheKey = "attrOps@" + pos;
  836. var cachedResult = cache[cacheKey];
  837. if (cachedResult) {
  838. pos = cachedResult.nextPos;
  839. return cachedResult.result;
  840. }
  841. var result0, result1;
  842. var pos0, pos1;
  843. pos0 = pos;
  844. pos1 = pos;
  845. if (/^[><!]/.test(input.charAt(pos))) {
  846. result0 = input.charAt(pos);
  847. pos++;
  848. } else {
  849. result0 = null;
  850. if (reportFailures === 0) {
  851. matchFailed("[><!]");
  852. }
  853. }
  854. result0 = result0 !== null ? result0 : "";
  855. if (result0 !== null) {
  856. if (input.charCodeAt(pos) === 61) {
  857. result1 = "=";
  858. pos++;
  859. } else {
  860. result1 = null;
  861. if (reportFailures === 0) {
  862. matchFailed("\"=\"");
  863. }
  864. }
  865. if (result1 !== null) {
  866. result0 = [result0, result1];
  867. } else {
  868. result0 = null;
  869. pos = pos1;
  870. }
  871. } else {
  872. result0 = null;
  873. pos = pos1;
  874. }
  875. if (result0 !== null) {
  876. result0 = (function(offset, a) { return a + '='; })(pos0, result0[0]);
  877. }
  878. if (result0 === null) {
  879. pos = pos0;
  880. }
  881. if (result0 === null) {
  882. if (/^[><]/.test(input.charAt(pos))) {
  883. result0 = input.charAt(pos);
  884. pos++;
  885. } else {
  886. result0 = null;
  887. if (reportFailures === 0) {
  888. matchFailed("[><]");
  889. }
  890. }
  891. }
  892. cache[cacheKey] = {
  893. nextPos: pos,
  894. result: result0
  895. };
  896. return result0;
  897. }
  898. function parse_attrEqOps() {
  899. var cacheKey = "attrEqOps@" + pos;
  900. var cachedResult = cache[cacheKey];
  901. if (cachedResult) {
  902. pos = cachedResult.nextPos;
  903. return cachedResult.result;
  904. }
  905. var result0, result1;
  906. var pos0, pos1;
  907. pos0 = pos;
  908. pos1 = pos;
  909. if (input.charCodeAt(pos) === 33) {
  910. result0 = "!";
  911. pos++;
  912. } else {
  913. result0 = null;
  914. if (reportFailures === 0) {
  915. matchFailed("\"!\"");
  916. }
  917. }
  918. result0 = result0 !== null ? result0 : "";
  919. if (result0 !== null) {
  920. if (input.charCodeAt(pos) === 61) {
  921. result1 = "=";
  922. pos++;
  923. } else {
  924. result1 = null;
  925. if (reportFailures === 0) {
  926. matchFailed("\"=\"");
  927. }
  928. }
  929. if (result1 !== null) {
  930. result0 = [result0, result1];
  931. } else {
  932. result0 = null;
  933. pos = pos1;
  934. }
  935. } else {
  936. result0 = null;
  937. pos = pos1;
  938. }
  939. if (result0 !== null) {
  940. result0 = (function(offset, a) { return a + '='; })(pos0, result0[0]);
  941. }
  942. if (result0 === null) {
  943. pos = pos0;
  944. }
  945. cache[cacheKey] = {
  946. nextPos: pos,
  947. result: result0
  948. };
  949. return result0;
  950. }
  951. function parse_attrName() {
  952. var cacheKey = "attrName@" + pos;
  953. var cachedResult = cache[cacheKey];
  954. if (cachedResult) {
  955. pos = cachedResult.nextPos;
  956. return cachedResult.result;
  957. }
  958. var result0, result1;
  959. var pos0;
  960. pos0 = pos;
  961. result1 = parse_identifierName();
  962. if (result1 === null) {
  963. if (input.charCodeAt(pos) === 46) {
  964. result1 = ".";
  965. pos++;
  966. } else {
  967. result1 = null;
  968. if (reportFailures === 0) {
  969. matchFailed("\".\"");
  970. }
  971. }
  972. }
  973. if (result1 !== null) {
  974. result0 = [];
  975. while (result1 !== null) {
  976. result0.push(result1);
  977. result1 = parse_identifierName();
  978. if (result1 === null) {
  979. if (input.charCodeAt(pos) === 46) {
  980. result1 = ".";
  981. pos++;
  982. } else {
  983. result1 = null;
  984. if (reportFailures === 0) {
  985. matchFailed("\".\"");
  986. }
  987. }
  988. }
  989. }
  990. } else {
  991. result0 = null;
  992. }
  993. if (result0 !== null) {
  994. result0 = (function(offset, i) { return i.join(''); })(pos0, result0);
  995. }
  996. if (result0 === null) {
  997. pos = pos0;
  998. }
  999. cache[cacheKey] = {
  1000. nextPos: pos,
  1001. result: result0
  1002. };
  1003. return result0;
  1004. }
  1005. function parse_attrValue() {
  1006. var cacheKey = "attrValue@" + pos;
  1007. var cachedResult = cache[cacheKey];
  1008. if (cachedResult) {
  1009. pos = cachedResult.nextPos;
  1010. return cachedResult.result;
  1011. }
  1012. var result0, result1, result2, result3, result4;
  1013. var pos0, pos1;
  1014. pos0 = pos;
  1015. pos1 = pos;
  1016. result0 = parse_attrName();
  1017. if (result0 !== null) {
  1018. result1 = parse__();
  1019. if (result1 !== null) {
  1020. result2 = parse_attrEqOps();
  1021. if (result2 !== null) {
  1022. result3 = parse__();
  1023. if (result3 !== null) {
  1024. result4 = parse_type();
  1025. if (result4 === null) {
  1026. result4 = parse_regex();
  1027. }
  1028. if (result4 !== null) {
  1029. result0 = [result0, result1, result2, result3, result4];
  1030. } else {
  1031. result0 = null;
  1032. pos = pos1;
  1033. }
  1034. } else {
  1035. result0 = null;
  1036. pos = pos1;
  1037. }
  1038. } else {
  1039. result0 = null;
  1040. pos = pos1;
  1041. }
  1042. } else {
  1043. result0 = null;
  1044. pos = pos1;
  1045. }
  1046. } else {
  1047. result0 = null;
  1048. pos = pos1;
  1049. }
  1050. if (result0 !== null) {
  1051. result0 = (function(offset, name, op, value) {
  1052. return { type: 'attribute', name: name, operator: op, value: value };
  1053. })(pos0, result0[0], result0[2], result0[4]);
  1054. }
  1055. if (result0 === null) {
  1056. pos = pos0;
  1057. }
  1058. if (result0 === null) {
  1059. pos0 = pos;
  1060. pos1 = pos;
  1061. result0 = parse_attrName();
  1062. if (result0 !== null) {
  1063. result1 = parse__();
  1064. if (result1 !== null) {
  1065. result2 = parse_attrOps();
  1066. if (result2 !== null) {
  1067. result3 = parse__();
  1068. if (result3 !== null) {
  1069. result4 = parse_string();
  1070. if (result4 === null) {
  1071. result4 = parse_number();
  1072. if (result4 === null) {
  1073. result4 = parse_path();
  1074. }
  1075. }
  1076. if (result4 !== null) {
  1077. result0 = [result0, result1, result2, result3, result4];
  1078. } else {
  1079. result0 = null;
  1080. pos = pos1;
  1081. }
  1082. } else {
  1083. result0 = null;
  1084. pos = pos1;
  1085. }
  1086. } else {
  1087. result0 = null;
  1088. pos = pos1;
  1089. }
  1090. } else {
  1091. result0 = null;
  1092. pos = pos1;
  1093. }
  1094. } else {
  1095. result0 = null;
  1096. pos = pos1;
  1097. }
  1098. if (result0 !== null) {
  1099. result0 = (function(offset, name, op, value) {
  1100. return { type: 'attribute', name: name, operator: op, value: value };
  1101. })(pos0, result0[0], result0[2], result0[4]);
  1102. }
  1103. if (result0 === null) {
  1104. pos = pos0;
  1105. }
  1106. if (result0 === null) {
  1107. pos0 = pos;
  1108. result0 = parse_attrName();
  1109. if (result0 !== null) {
  1110. result0 = (function(offset, name) { return { type: 'attribute', name: name }; })(pos0, result0);
  1111. }
  1112. if (result0 === null) {
  1113. pos = pos0;
  1114. }
  1115. }
  1116. }
  1117. cache[cacheKey] = {
  1118. nextPos: pos,
  1119. result: result0
  1120. };
  1121. return result0;
  1122. }
  1123. function parse_string() {
  1124. var cacheKey = "string@" + pos;
  1125. var cachedResult = cache[cacheKey];
  1126. if (cachedResult) {
  1127. pos = cachedResult.nextPos;
  1128. return cachedResult.result;
  1129. }
  1130. var result0, result1, result2, result3;
  1131. var pos0, pos1, pos2, pos3;
  1132. pos0 = pos;
  1133. pos1 = pos;
  1134. if (input.charCodeAt(pos) === 34) {
  1135. result0 = "\"";
  1136. pos++;
  1137. } else {
  1138. result0 = null;
  1139. if (reportFailures === 0) {
  1140. matchFailed("\"\\\"\"");
  1141. }
  1142. }
  1143. if (result0 !== null) {
  1144. result1 = [];
  1145. if (/^[^\\"]/.test(input.charAt(pos))) {
  1146. result2 = input.charAt(pos);
  1147. pos++;
  1148. } else {
  1149. result2 = null;
  1150. if (reportFailures === 0) {
  1151. matchFailed("[^\\\\\"]");
  1152. }
  1153. }
  1154. if (result2 === null) {
  1155. pos2 = pos;
  1156. pos3 = pos;
  1157. if (input.charCodeAt(pos) === 92) {
  1158. result2 = "\\";
  1159. pos++;
  1160. } else {
  1161. result2 = null;
  1162. if (reportFailures === 0) {
  1163. matchFailed("\"\\\\\"");
  1164. }
  1165. }
  1166. if (result2 !== null) {
  1167. if (input.length > pos) {
  1168. result3 = input.charAt(pos);
  1169. pos++;
  1170. } else {
  1171. result3 = null;
  1172. if (reportFailures === 0) {
  1173. matchFailed("any character");
  1174. }
  1175. }
  1176. if (result3 !== null) {
  1177. result2 = [result2, result3];
  1178. } else {
  1179. result2 = null;
  1180. pos = pos3;
  1181. }
  1182. } else {
  1183. result2 = null;
  1184. pos = pos3;
  1185. }
  1186. if (result2 !== null) {
  1187. result2 = (function(offset, a, b) { return a + b; })(pos2, result2[0], result2[1]);
  1188. }
  1189. if (result2 === null) {
  1190. pos = pos2;
  1191. }
  1192. }
  1193. while (result2 !== null) {
  1194. result1.push(result2);
  1195. if (/^[^\\"]/.test(input.charAt(pos))) {
  1196. result2 = input.charAt(pos);
  1197. pos++;
  1198. } else {
  1199. result2 = null;
  1200. if (reportFailures === 0) {
  1201. matchFailed("[^\\\\\"]");
  1202. }
  1203. }
  1204. if (result2 === null) {
  1205. pos2 = pos;
  1206. pos3 = pos;
  1207. if (input.charCodeAt(pos) === 92) {
  1208. result2 = "\\";
  1209. pos++;
  1210. } else {
  1211. result2 = null;
  1212. if (reportFailures === 0) {
  1213. matchFailed("\"\\\\\"");
  1214. }
  1215. }
  1216. if (result2 !== null) {
  1217. if (input.length > pos) {
  1218. result3 = input.charAt(pos);
  1219. pos++;
  1220. } else {
  1221. result3 = null;
  1222. if (reportFailures === 0) {
  1223. matchFailed("any character");
  1224. }
  1225. }
  1226. if (result3 !== null) {
  1227. result2 = [result2, result3];
  1228. } else {
  1229. result2 = null;
  1230. pos = pos3;
  1231. }
  1232. } else {
  1233. result2 = null;
  1234. pos = pos3;
  1235. }
  1236. if (result2 !== null) {
  1237. result2 = (function(offset, a, b) { return a + b; })(pos2, result2[0], result2[1]);
  1238. }
  1239. if (result2 === null) {
  1240. pos = pos2;
  1241. }
  1242. }
  1243. }
  1244. if (result1 !== null) {
  1245. if (input.charCodeAt(pos) === 34) {
  1246. result2 = "\"";
  1247. pos++;
  1248. } else {
  1249. result2 = null;
  1250. if (reportFailures === 0) {
  1251. matchFailed("\"\\\"\"");
  1252. }
  1253. }
  1254. if (result2 !== null) {
  1255. result0 = [result0, result1, result2];
  1256. } else {
  1257. result0 = null;
  1258. pos = pos1;
  1259. }
  1260. } else {
  1261. result0 = null;
  1262. pos = pos1;
  1263. }
  1264. } else {
  1265. result0 = null;
  1266. pos = pos1;
  1267. }
  1268. if (result0 !== null) {
  1269. result0 = (function(offset, d) {
  1270. return { type: 'literal', value: strUnescape(d.join('')) };
  1271. })(pos0, result0[1]);
  1272. }
  1273. if (result0 === null) {
  1274. pos = pos0;
  1275. }
  1276. if (result0 === null) {
  1277. pos0 = pos;
  1278. pos1 = pos;
  1279. if (input.charCodeAt(pos) === 39) {
  1280. result0 = "'";
  1281. pos++;
  1282. } else {
  1283. result0 = null;
  1284. if (reportFailures === 0) {
  1285. matchFailed("\"'\"");
  1286. }
  1287. }
  1288. if (result0 !== null) {
  1289. result1 = [];
  1290. if (/^[^\\']/.test(input.charAt(pos))) {
  1291. result2 = input.charAt(pos);
  1292. pos++;
  1293. } else {
  1294. result2 = null;
  1295. if (reportFailures === 0) {
  1296. matchFailed("[^\\\\']");
  1297. }
  1298. }
  1299. if (result2 === null) {
  1300. pos2 = pos;
  1301. pos3 = pos;
  1302. if (input.charCodeAt(pos) === 92) {
  1303. result2 = "\\";
  1304. pos++;
  1305. } else {
  1306. result2 = null;
  1307. if (reportFailures === 0) {
  1308. matchFailed("\"\\\\\"");
  1309. }
  1310. }
  1311. if (result2 !== null) {
  1312. if (input.length > pos) {
  1313. result3 = input.charAt(pos);
  1314. pos++;
  1315. } else {
  1316. result3 = null;
  1317. if (reportFailures === 0) {
  1318. matchFailed("any character");
  1319. }
  1320. }
  1321. if (result3 !== null) {
  1322. result2 = [result2, result3];
  1323. } else {
  1324. result2 = null;
  1325. pos = pos3;
  1326. }
  1327. } else {
  1328. result2 = null;
  1329. pos = pos3;
  1330. }
  1331. if (result2 !== null) {
  1332. result2 = (function(offset, a, b) { return a + b; })(pos2, result2[0], result2[1]);
  1333. }
  1334. if (result2 === null) {
  1335. pos = pos2;
  1336. }
  1337. }
  1338. while (result2 !== null) {
  1339. result1.push(result2);
  1340. if (/^[^\\']/.test(input.charAt(pos))) {
  1341. result2 = input.charAt(pos);
  1342. pos++;
  1343. } else {
  1344. result2 = null;
  1345. if (reportFailures === 0) {
  1346. matchFailed("[^\\\\']");
  1347. }
  1348. }
  1349. if (result2 === null) {
  1350. pos2 = pos;
  1351. pos3 = pos;
  1352. if (input.charCodeAt(pos) === 92) {
  1353. result2 = "\\";
  1354. pos++;
  1355. } else {
  1356. result2 = null;
  1357. if (reportFailures === 0) {
  1358. matchFailed("\"\\\\\"");
  1359. }
  1360. }
  1361. if (result2 !== null) {
  1362. if (input.length > pos) {
  1363. result3 = input.charAt(pos);
  1364. pos++;
  1365. } else {
  1366. result3 = null;
  1367. if (reportFailures === 0) {
  1368. matchFailed("any character");
  1369. }
  1370. }
  1371. if (result3 !== null) {
  1372. result2 = [result2, result3];
  1373. } else {
  1374. result2 = null;
  1375. pos = pos3;
  1376. }
  1377. } else {
  1378. result2 = null;
  1379. pos = pos3;
  1380. }
  1381. if (result2 !== null) {
  1382. result2 = (function(offset, a, b) { return a + b; })(pos2, result2[0], result2[1]);
  1383. }
  1384. if (result2 === null) {
  1385. pos = pos2;
  1386. }
  1387. }
  1388. }
  1389. if (result1 !== null) {
  1390. if (input.charCodeAt(pos) === 39) {
  1391. result2 = "'";
  1392. pos++;
  1393. } else {
  1394. result2 = null;
  1395. if (reportFailures === 0) {
  1396. matchFailed("\"'\"");
  1397. }
  1398. }
  1399. if (result2 !== null) {
  1400. result0 = [result0, result1, result2];
  1401. } else {
  1402. result0 = null;
  1403. pos = pos1;
  1404. }
  1405. } else {
  1406. result0 = null;
  1407. pos = pos1;
  1408. }
  1409. } else {
  1410. result0 = null;
  1411. pos = pos1;
  1412. }
  1413. if (result0 !== null) {
  1414. result0 = (function(offset, d) {
  1415. return { type: 'literal', value: strUnescape(d.join('')) };
  1416. })(pos0, result0[1]);
  1417. }
  1418. if (result0 === null) {
  1419. pos = pos0;
  1420. }
  1421. }
  1422. cache[cacheKey] = {
  1423. nextPos: pos,
  1424. result: result0
  1425. };
  1426. return result0;
  1427. }
  1428. function parse_number() {
  1429. var cacheKey = "number@" + pos;
  1430. var cachedResult = cache[cacheKey];
  1431. if (cachedResult) {
  1432. pos = cachedResult.nextPos;
  1433. return cachedResult.result;
  1434. }
  1435. var result0, result1, result2;
  1436. var pos0, pos1, pos2;
  1437. pos0 = pos;
  1438. pos1 = pos;
  1439. pos2 = pos;
  1440. result0 = [];
  1441. if (/^[0-9]/.test(input.charAt(pos))) {
  1442. result1 = input.charAt(pos);
  1443. pos++;
  1444. } else {
  1445. result1 = null;
  1446. if (reportFailures === 0) {
  1447. matchFailed("[0-9]");
  1448. }
  1449. }
  1450. while (result1 !== null) {
  1451. result0.push(result1);
  1452. if (/^[0-9]/.test(input.charAt(pos))) {
  1453. result1 = input.charAt(pos);
  1454. pos++;
  1455. } else {
  1456. result1 = null;
  1457. if (reportFailures === 0) {
  1458. matchFailed("[0-9]");
  1459. }
  1460. }
  1461. }
  1462. if (result0 !== null) {
  1463. if (input.charCodeAt(pos) === 46) {
  1464. result1 = ".";
  1465. pos++;
  1466. } else {
  1467. result1 = null;
  1468. if (reportFailures === 0) {
  1469. matchFailed("\".\"");
  1470. }
  1471. }
  1472. if (result1 !== null) {
  1473. result0 = [result0, result1];
  1474. } else {
  1475. result0 = null;
  1476. pos = pos2;
  1477. }
  1478. } else {
  1479. result0 = null;
  1480. pos = pos2;
  1481. }
  1482. result0 = result0 !== null ? result0 : "";
  1483. if (result0 !== null) {
  1484. if (/^[0-9]/.test(input.charAt(pos))) {
  1485. result2 = input.charAt(pos);
  1486. pos++;
  1487. } else {
  1488. result2 = null;
  1489. if (reportFailures === 0) {
  1490. matchFailed("[0-9]");
  1491. }
  1492. }
  1493. if (result2 !== null) {
  1494. result1 = [];
  1495. while (result2 !== null) {
  1496. result1.push(result2);
  1497. if (/^[0-9]/.test(input.charAt(pos))) {
  1498. result2 = input.charAt(pos);
  1499. pos++;
  1500. } else {
  1501. result2 = null;
  1502. if (reportFailures === 0) {
  1503. matchFailed("[0-9]");
  1504. }
  1505. }
  1506. }
  1507. } else {
  1508. result1 = null;
  1509. }
  1510. if (result1 !== null) {
  1511. result0 = [result0, result1];
  1512. } else {
  1513. result0 = null;
  1514. pos = pos1;
  1515. }
  1516. } else {
  1517. result0 = null;
  1518. pos = pos1;
  1519. }
  1520. if (result0 !== null) {
  1521. result0 = (function(offset, a, b) {
  1522. return { type: 'literal', value: parseFloat((a ? a.join('') : '') + b.join('')) };
  1523. })(pos0, result0[0], result0[1]);
  1524. }
  1525. if (result0 === null) {
  1526. pos = pos0;
  1527. }
  1528. cache[cacheKey] = {
  1529. nextPos: pos,
  1530. result: result0
  1531. };
  1532. return result0;
  1533. }
  1534. function parse_path() {
  1535. var cacheKey = "path@" + pos;
  1536. var cachedResult = cache[cacheKey];
  1537. if (cachedResult) {
  1538. pos = cachedResult.nextPos;
  1539. return cachedResult.result;
  1540. }
  1541. var result0;
  1542. var pos0;
  1543. pos0 = pos;
  1544. result0 = parse_identifierName();
  1545. if (result0 !== null) {
  1546. result0 = (function(offset, i) { return { type: 'literal', value: i }; })(pos0, result0);
  1547. }
  1548. if (result0 === null) {
  1549. pos = pos0;
  1550. }
  1551. cache[cacheKey] = {
  1552. nextPos: pos,
  1553. result: result0
  1554. };
  1555. return result0;
  1556. }
  1557. function parse_type() {
  1558. var cacheKey = "type@" + pos;
  1559. var cachedResult = cache[cacheKey];
  1560. if (cachedResult) {
  1561. pos = cachedResult.nextPos;
  1562. return cachedResult.result;
  1563. }
  1564. var result0, result1, result2, result3, result4;
  1565. var pos0, pos1;
  1566. pos0 = pos;
  1567. pos1 = pos;
  1568. if (input.substr(pos, 5) === "type(") {
  1569. result0 = "type(";
  1570. pos += 5;
  1571. } else {
  1572. result0 = null;
  1573. if (reportFailures === 0) {
  1574. matchFailed("\"type(\"");
  1575. }
  1576. }
  1577. if (result0 !== null) {
  1578. result1 = parse__();
  1579. if (result1 !== null) {
  1580. if (/^[^ )]/.test(input.charAt(pos))) {
  1581. result3 = input.charAt(pos);
  1582. pos++;
  1583. } else {
  1584. result3 = null;
  1585. if (reportFailures === 0) {
  1586. matchFailed("[^ )]");
  1587. }
  1588. }
  1589. if (result3 !== null) {
  1590. result2 = [];
  1591. while (result3 !== null) {
  1592. result2.push(result3);
  1593. if (/^[^ )]/.test(input.charAt(pos))) {
  1594. result3 = input.charAt(pos);
  1595. pos++;
  1596. } else {
  1597. result3 = null;
  1598. if (reportFailures === 0) {
  1599. matchFailed("[^ )]");
  1600. }
  1601. }
  1602. }
  1603. } else {
  1604. result2 = null;
  1605. }
  1606. if (result2 !== null) {
  1607. result3 = parse__();
  1608. if (result3 !== null) {
  1609. if (input.charCodeAt(pos) === 41) {
  1610. result4 = ")";
  1611. pos++;
  1612. } else {
  1613. result4 = null;
  1614. if (reportFailures === 0) {
  1615. matchFailed("\")\"");
  1616. }
  1617. }
  1618. if (result4 !== null) {
  1619. result0 = [result0, result1, result2, result3, result4];
  1620. } else {
  1621. result0 = null;
  1622. pos = pos1;
  1623. }
  1624. } else {
  1625. result0 = null;
  1626. pos = pos1;
  1627. }
  1628. } else {
  1629. result0 = null;
  1630. pos = pos1;
  1631. }
  1632. } else {
  1633. result0 = null;
  1634. pos = pos1;
  1635. }
  1636. } else {
  1637. result0 = null;
  1638. pos = pos1;
  1639. }
  1640. if (result0 !== null) {
  1641. result0 = (function(offset, t) { return { type: 'type', value: t.join('') }; })(pos0, result0[2]);
  1642. }
  1643. if (result0 === null) {
  1644. pos = pos0;
  1645. }
  1646. cache[cacheKey] = {
  1647. nextPos: pos,
  1648. result: result0
  1649. };
  1650. return result0;
  1651. }
  1652. function parse_regex() {
  1653. var cacheKey = "regex@" + pos;
  1654. var cachedResult = cache[cacheKey];
  1655. if (cachedResult) {
  1656. pos = cachedResult.nextPos;
  1657. return cachedResult.result;
  1658. }
  1659. var result0, result1, result2;
  1660. var pos0, pos1;
  1661. pos0 = pos;
  1662. pos1 = pos;
  1663. if (input.charCodeAt(pos) === 47) {
  1664. result0 = "/";
  1665. pos++;
  1666. } else {
  1667. result0 = null;
  1668. if (reportFailures === 0) {
  1669. matchFailed("\"/\"");
  1670. }
  1671. }
  1672. if (result0 !== null) {
  1673. if (/^[^\/]/.test(input.charAt(pos))) {
  1674. result2 = input.charAt(pos);
  1675. pos++;
  1676. } else {
  1677. result2 = null;
  1678. if (reportFailures === 0) {
  1679. matchFailed("[^\\/]");
  1680. }
  1681. }
  1682. if (result2 !== null) {
  1683. result1 = [];
  1684. while (result2 !== null) {
  1685. result1.push(result2);
  1686. if (/^[^\/]/.test(input.charAt(pos))) {
  1687. result2 = input.charAt(pos);
  1688. pos++;
  1689. } else {
  1690. result2 = null;
  1691. if (reportFailures === 0) {
  1692. matchFailed("[^\\/]");
  1693. }
  1694. }
  1695. }
  1696. } else {
  1697. result1 = null;
  1698. }
  1699. if (result1 !== null) {
  1700. if (input.charCodeAt(pos) === 47) {
  1701. result2 = "/";
  1702. pos++;
  1703. } else {
  1704. result2 = null;
  1705. if (reportFailures === 0) {
  1706. matchFailed("\"/\"");
  1707. }
  1708. }
  1709. if (result2 !== null) {
  1710. result0 = [result0, result1, result2];
  1711. } else {
  1712. result0 = null;
  1713. pos = pos1;
  1714. }
  1715. } else {
  1716. result0 = null;
  1717. pos = pos1;
  1718. }
  1719. } else {
  1720. result0 = null;
  1721. pos = pos1;
  1722. }
  1723. if (result0 !== null) {
  1724. result0 = (function(offset, d) { return { type: 'regexp', value: new RegExp(d.join('')) }; })(pos0, result0[1]);
  1725. }
  1726. if (result0 === null) {
  1727. pos = pos0;
  1728. }
  1729. cache[cacheKey] = {
  1730. nextPos: pos,
  1731. result: result0
  1732. };
  1733. return result0;
  1734. }
  1735. function parse_field() {
  1736. var cacheKey = "field@" + pos;
  1737. var cachedResult = cache[cacheKey];
  1738. if (cachedResult) {
  1739. pos = cachedResult.nextPos;
  1740. return cachedResult.result;
  1741. }
  1742. var result0, result1, result2, result3, result4;
  1743. var pos0, pos1, pos2;
  1744. pos0 = pos;
  1745. pos1 = pos;
  1746. if (input.charCodeAt(pos) === 46) {
  1747. result0 = ".";
  1748. pos++;
  1749. } else {
  1750. result0 = null;
  1751. if (reportFailures === 0) {
  1752. matchFailed("\".\"");
  1753. }
  1754. }
  1755. if (result0 !== null) {
  1756. result1 = parse_identifierName();
  1757. if (result1 !== null) {
  1758. result2 = [];
  1759. pos2 = pos;
  1760. if (input.charCodeAt(pos) === 46) {
  1761. result3 = ".";
  1762. pos++;
  1763. } else {
  1764. result3 = null;
  1765. if (reportFailures === 0) {
  1766. matchFailed("\".\"");
  1767. }
  1768. }
  1769. if (result3 !== null) {
  1770. result4 = parse_identifierName();
  1771. if (result4 !== null) {
  1772. result3 = [result3, result4];
  1773. } else {
  1774. result3 = null;
  1775. pos = pos2;
  1776. }
  1777. } else {
  1778. result3 = null;
  1779. pos = pos2;
  1780. }
  1781. while (result3 !== null) {
  1782. result2.push(result3);
  1783. pos2 = pos;
  1784. if (input.charCodeAt(pos) === 46) {
  1785. result3 = ".";
  1786. pos++;
  1787. } else {
  1788. result3 = null;
  1789. if (reportFailures === 0) {
  1790. matchFailed("\".\"");
  1791. }
  1792. }
  1793. if (result3 !== null) {
  1794. result4 = parse_identifierName();
  1795. if (result4 !== null) {
  1796. result3 = [result3, result4];
  1797. } else {
  1798. result3 = null;
  1799. pos = pos2;
  1800. }
  1801. } else {
  1802. result3 = null;
  1803. pos = pos2;
  1804. }
  1805. }
  1806. if (result2 !== null) {
  1807. result0 = [result0, result1, result2];
  1808. } else {
  1809. result0 = null;
  1810. pos = pos1;
  1811. }
  1812. } else {
  1813. result0 = null;
  1814. pos = pos1;
  1815. }
  1816. } else {
  1817. result0 = null;
  1818. pos = pos1;
  1819. }
  1820. if (result0 !== null) {
  1821. result0 = (function(offset, i, is) {
  1822. return { type: 'field', name: is.reduce(function(memo, p){ return memo + p[0] + p[1]; }, i)};
  1823. })(pos0, result0[1], result0[2]);
  1824. }
  1825. if (result0 === null) {
  1826. pos = pos0;
  1827. }
  1828. cache[cacheKey] = {
  1829. nextPos: pos,
  1830. result: result0
  1831. };
  1832. return result0;
  1833. }
  1834. function parse_negation() {
  1835. var cacheKey = "negation@" + pos;
  1836. var cachedResult = cache[cacheKey];
  1837. if (cachedResult) {
  1838. pos = cachedResult.nextPos;
  1839. return cachedResult.result;
  1840. }
  1841. var result0, result1, result2, result3, result4;
  1842. var pos0, pos1;
  1843. pos0 = pos;
  1844. pos1 = pos;
  1845. if (input.substr(pos, 5) === ":not(") {
  1846. result0 = ":not(";
  1847. pos += 5;
  1848. } else {
  1849. result0 = null;
  1850. if (reportFailures === 0) {
  1851. matchFailed("\":not(\"");
  1852. }
  1853. }
  1854. if (result0 !== null) {
  1855. result1 = parse__();
  1856. if (result1 !== null) {
  1857. result2 = parse_selectors();
  1858. if (result2 !== null) {
  1859. result3 = parse__();
  1860. if (result3 !== null) {
  1861. if (input.charCodeAt(pos) === 41) {
  1862. result4 = ")";
  1863. pos++;
  1864. } else {
  1865. result4 = null;
  1866. if (reportFailures === 0) {
  1867. matchFailed("\")\"");
  1868. }
  1869. }
  1870. if (result4 !== null) {
  1871. result0 = [result0, result1, result2, result3, result4];
  1872. } else {
  1873. result0 = null;
  1874. pos = pos1;
  1875. }
  1876. } else {
  1877. result0 = null;
  1878. pos = pos1;
  1879. }
  1880. } else {
  1881. result0 = null;
  1882. pos = pos1;
  1883. }
  1884. } else {
  1885. result0 = null;
  1886. pos = pos1;
  1887. }
  1888. } else {
  1889. result0 = null;
  1890. pos = pos1;
  1891. }
  1892. if (result0 !== null) {
  1893. result0 = (function(offset, ss) { return { type: 'not', selectors: ss }; })(pos0, result0[2]);
  1894. }
  1895. if (result0 === null) {
  1896. pos = pos0;
  1897. }
  1898. cache[cacheKey] = {
  1899. nextPos: pos,
  1900. result: result0
  1901. };
  1902. return result0;
  1903. }
  1904. function parse_matches() {
  1905. var cacheKey = "matches@" + pos;
  1906. var cachedResult = cache[cacheKey];
  1907. if (cachedResult) {
  1908. pos = cachedResult.nextPos;
  1909. return cachedResult.result;
  1910. }
  1911. var result0, result1, result2, result3, result4;
  1912. var pos0, pos1;
  1913. pos0 = pos;
  1914. pos1 = pos;
  1915. if (input.substr(pos, 9) === ":matches(") {
  1916. result0 = ":matches(";
  1917. pos += 9;
  1918. } else {
  1919. result0 = null;
  1920. if (reportFailures === 0) {
  1921. matchFailed("\":matches(\"");
  1922. }
  1923. }
  1924. if (result0 !== null) {
  1925. result1 = parse__();
  1926. if (result1 !== null) {
  1927. result2 = parse_selectors();
  1928. if (result2 !== null) {
  1929. result3 = parse__();
  1930. if (result3 !== null) {
  1931. if (input.charCodeAt(pos) === 41) {
  1932. result4 = ")";
  1933. pos++;
  1934. } else {
  1935. result4 = null;
  1936. if (reportFailures === 0) {
  1937. matchFailed("\")\"");
  1938. }
  1939. }
  1940. if (result4 !== null) {
  1941. result0 = [result0, result1, result2, result3, result4];
  1942. } else {
  1943. result0 = null;
  1944. pos = pos1;
  1945. }
  1946. } else {
  1947. result0 = null;
  1948. pos = pos1;
  1949. }
  1950. } else {
  1951. result0 = null;
  1952. pos = pos1;
  1953. }
  1954. } else {
  1955. result0 = null;
  1956. pos = pos1;
  1957. }
  1958. } else {
  1959. result0 = null;
  1960. pos = pos1;
  1961. }
  1962. if (result0 !== null) {
  1963. result0 = (function(offset, ss) { return { type: 'matches', selectors: ss }; })(pos0, result0[2]);
  1964. }
  1965. if (result0 === null) {
  1966. pos = pos0;
  1967. }
  1968. cache[cacheKey] = {
  1969. nextPos: pos,
  1970. result: result0
  1971. };
  1972. return result0;
  1973. }
  1974. function parse_has() {
  1975. var cacheKey = "has@" + pos;
  1976. var cachedResult = cache[cacheKey];
  1977. if (cachedResult) {
  1978. pos = cachedResult.nextPos;
  1979. return cachedResult.result;
  1980. }
  1981. var result0, result1, result2, result3, result4;
  1982. var pos0, pos1;
  1983. pos0 = pos;
  1984. pos1 = pos;
  1985. if (input.substr(pos, 5) === ":has(") {
  1986. result0 = ":has(";
  1987. pos += 5;
  1988. } else {
  1989. result0 = null;
  1990. if (reportFailures === 0) {
  1991. matchFailed("\":has(\"");
  1992. }
  1993. }
  1994. if (result0 !== null) {
  1995. result1 = parse__();
  1996. if (result1 !== null) {
  1997. result2 = parse_selectors();
  1998. if (result2 !== null) {
  1999. result3 = parse__();
  2000. if (result3 !== null) {
  2001. if (input.charCodeAt(pos) === 41) {
  2002. result4 = ")";
  2003. pos++;
  2004. } else {
  2005. result4 = null;
  2006. if (reportFailures === 0) {
  2007. matchFailed("\")\"");
  2008. }
  2009. }
  2010. if (result4 !== null) {
  2011. result0 = [result0, result1, result2, result3, result4];
  2012. } else {
  2013. result0 = null;
  2014. pos = pos1;
  2015. }
  2016. } else {
  2017. result0 = null;
  2018. pos = pos1;
  2019. }
  2020. } else {
  2021. result0 = null;
  2022. pos = pos1;
  2023. }
  2024. } else {
  2025. result0 = null;
  2026. pos = pos1;
  2027. }
  2028. } else {
  2029. result0 = null;
  2030. pos = pos1;
  2031. }
  2032. if (result0 !== null) {
  2033. result0 = (function(offset, ss) { return { type: 'has', selectors: ss }; })(pos0, result0[2]);
  2034. }
  2035. if (result0 === null) {
  2036. pos = pos0;
  2037. }
  2038. cache[cacheKey] = {
  2039. nextPos: pos,
  2040. result: result0
  2041. };
  2042. return result0;
  2043. }
  2044. function parse_firstChild() {
  2045. var cacheKey = "firstChild@" + pos;
  2046. var cachedResult = cache[cacheKey];
  2047. if (cachedResult) {
  2048. pos = cachedResult.nextPos;
  2049. return cachedResult.result;
  2050. }
  2051. var result0;
  2052. var pos0;
  2053. pos0 = pos;
  2054. if (input.substr(pos, 12) === ":first-child") {
  2055. result0 = ":first-child";
  2056. pos += 12;
  2057. } else {
  2058. result0 = null;
  2059. if (reportFailures === 0) {
  2060. matchFailed("\":first-child\"");
  2061. }
  2062. }
  2063. if (result0 !== null) {
  2064. result0 = (function(offset) { return nth(1); })(pos0);
  2065. }
  2066. if (result0 === null) {
  2067. pos = pos0;
  2068. }
  2069. cache[cacheKey] = {
  2070. nextPos: pos,
  2071. result: result0
  2072. };
  2073. return result0;
  2074. }
  2075. function parse_lastChild() {
  2076. var cacheKey = "lastChild@" + pos;
  2077. var cachedResult = cache[cacheKey];
  2078. if (cachedResult) {
  2079. pos = cachedResult.nextPos;
  2080. return cachedResult.result;
  2081. }
  2082. var result0;
  2083. var pos0;
  2084. pos0 = pos;
  2085. if (input.substr(pos, 11) === ":last-child") {
  2086. result0 = ":last-child";
  2087. pos += 11;
  2088. } else {
  2089. result0 = null;
  2090. if (reportFailures === 0) {
  2091. matchFailed("\":last-child\"");
  2092. }
  2093. }
  2094. if (result0 !== null) {
  2095. result0 = (function(offset) { return nthLast(1); })(pos0);
  2096. }
  2097. if (result0 === null) {
  2098. pos = pos0;
  2099. }
  2100. cache[cacheKey] = {
  2101. nextPos: pos,
  2102. result: result0
  2103. };
  2104. return result0;
  2105. }
  2106. function parse_nthChild() {
  2107. var cacheKey = "nthChild@" + pos;
  2108. var cachedResult = cache[cacheKey];
  2109. if (cachedResult) {
  2110. pos = cachedResult.nextPos;
  2111. return cachedResult.result;
  2112. }
  2113. var result0, result1, result2, result3, result4;
  2114. var pos0, pos1;
  2115. pos0 = pos;
  2116. pos1 = pos;
  2117. if (input.substr(pos, 11) === ":nth-child(") {
  2118. result0 = ":nth-child(";
  2119. pos += 11;
  2120. } else {
  2121. result0 = null;
  2122. if (reportFailures === 0) {
  2123. matchFailed("\":nth-child(\"");
  2124. }
  2125. }
  2126. if (result0 !== null) {
  2127. result1 = parse__();
  2128. if (result1 !== null) {
  2129. if (/^[0-9]/.test(input.charAt(pos))) {
  2130. result3 = input.charAt(pos);
  2131. pos++;
  2132. } else {
  2133. result3 = null;
  2134. if (reportFailures === 0) {
  2135. matchFailed("[0-9]");
  2136. }
  2137. }
  2138. if (result3 !== null) {
  2139. result2 = [];
  2140. while (result3 !== null) {
  2141. result2.push(result3);
  2142. if (/^[0-9]/.test(input.charAt(pos))) {
  2143. result3 = input.charAt(pos);
  2144. pos++;
  2145. } else {
  2146. result3 = null;
  2147. if (reportFailures === 0) {
  2148. matchFailed("[0-9]");
  2149. }
  2150. }
  2151. }
  2152. } else {
  2153. result2 = null;
  2154. }
  2155. if (result2 !== null) {
  2156. result3 = parse__();
  2157. if (result3 !== null) {
  2158. if (input.charCodeAt(pos) === 41) {
  2159. result4 = ")";
  2160. pos++;
  2161. } else {
  2162. result4 = null;
  2163. if (reportFailures === 0) {
  2164. matchFailed("\")\"");
  2165. }
  2166. }
  2167. if (result4 !== null) {
  2168. result0 = [result0, result1, result2, result3, result4];
  2169. } else {
  2170. result0 = null;
  2171. pos = pos1;
  2172. }
  2173. } else {
  2174. result0 = null;
  2175. pos = pos1;
  2176. }
  2177. } else {
  2178. result0 = null;
  2179. pos = pos1;
  2180. }
  2181. } else {
  2182. result0 = null;
  2183. pos = pos1;
  2184. }
  2185. } else {
  2186. result0 = null;
  2187. pos = pos1;
  2188. }
  2189. if (result0 !== null) {
  2190. result0 = (function(offset, n) { return nth(parseInt(n.join(''), 10)); })(pos0, result0[2]);
  2191. }
  2192. if (result0 === null) {
  2193. pos = pos0;
  2194. }
  2195. cache[cacheKey] = {
  2196. nextPos: pos,
  2197. result: result0
  2198. };
  2199. return result0;
  2200. }
  2201. function parse_nthLastChild() {
  2202. var cacheKey = "nthLastChild@" + pos;
  2203. var cachedResult = cache[cacheKey];
  2204. if (cachedResult) {
  2205. pos = cachedResult.nextPos;
  2206. return cachedResult.result;
  2207. }
  2208. var result0, result1, result2, result3, result4;
  2209. var pos0, pos1;
  2210. pos0 = pos;
  2211. pos1 = pos;
  2212. if (input.substr(pos, 16) === ":nth-last-child(") {
  2213. result0 = ":nth-last-child(";
  2214. pos += 16;
  2215. } else {
  2216. result0 = null;
  2217. if (reportFailures === 0) {
  2218. matchFailed("\":nth-last-child(\"");
  2219. }
  2220. }
  2221. if (result0 !== null) {
  2222. result1 = parse__();
  2223. if (result1 !== null) {
  2224. if (/^[0-9]/.test(input.charAt(pos))) {
  2225. result3 = input.charAt(pos);
  2226. pos++;
  2227. } else {
  2228. result3 = null;
  2229. if (reportFailures === 0) {
  2230. matchFailed("[0-9]");
  2231. }
  2232. }
  2233. if (result3 !== null) {
  2234. result2 = [];
  2235. while (result3 !== null) {
  2236. result2.push(result3);
  2237. if (/^[0-9]/.test(input.charAt(pos))) {
  2238. result3 = input.charAt(pos);
  2239. pos++;
  2240. } else {
  2241. result3 = null;
  2242. if (reportFailures === 0) {
  2243. matchFailed("[0-9]");
  2244. }
  2245. }
  2246. }
  2247. } else {
  2248. result2 = null;
  2249. }
  2250. if (result2 !== null) {
  2251. result3 = parse__();
  2252. if (result3 !== null) {
  2253. if (input.charCodeAt(pos) === 41) {
  2254. result4 = ")";
  2255. pos++;
  2256. } else {
  2257. result4 = null;
  2258. if (reportFailures === 0) {
  2259. matchFailed("\")\"");
  2260. }
  2261. }
  2262. if (result4 !== null) {
  2263. result0 = [result0, result1, result2, result3, result4];
  2264. } else {
  2265. result0 = null;
  2266. pos = pos1;
  2267. }
  2268. } else {
  2269. result0 = null;
  2270. pos = pos1;
  2271. }
  2272. } else {
  2273. result0 = null;
  2274. pos = pos1;
  2275. }
  2276. } else {
  2277. result0 = null;
  2278. pos = pos1;
  2279. }
  2280. } else {
  2281. result0 = null;
  2282. pos = pos1;
  2283. }
  2284. if (result0 !== null) {
  2285. result0 = (function(offset, n) { return nthLast(parseInt(n.join(''), 10)); })(pos0, result0[2]);
  2286. }
  2287. if (result0 === null) {
  2288. pos = pos0;
  2289. }
  2290. cache[cacheKey] = {
  2291. nextPos: pos,
  2292. result: result0
  2293. };
  2294. return result0;
  2295. }
  2296. function parse_class() {
  2297. var cacheKey = "class@" + pos;
  2298. var cachedResult = cache[cacheKey];
  2299. if (cachedResult) {
  2300. pos = cachedResult.nextPos;
  2301. return cachedResult.result;
  2302. }
  2303. var result0, result1;
  2304. var pos0, pos1;
  2305. pos0 = pos;
  2306. pos1 = pos;
  2307. if (input.charCodeAt(pos) === 58) {
  2308. result0 = ":";
  2309. pos++;
  2310. } else {
  2311. result0 = null;
  2312. if (reportFailures === 0) {
  2313. matchFailed("\":\"");
  2314. }
  2315. }
  2316. if (result0 !== null) {
  2317. if (input.substr(pos, 9).toLowerCase() === "statement") {
  2318. result1 = input.substr(pos, 9);
  2319. pos += 9;
  2320. } else {
  2321. result1 = null;
  2322. if (reportFailures === 0) {
  2323. matchFailed("\"statement\"");
  2324. }
  2325. }
  2326. if (result1 === null) {
  2327. if (input.substr(pos, 10).toLowerCase() === "expression") {
  2328. result1 = input.substr(pos, 10);
  2329. pos += 10;
  2330. } else {
  2331. result1 = null;
  2332. if (reportFailures === 0) {
  2333. matchFailed("\"expression\"");
  2334. }
  2335. }
  2336. if (result1 === null) {
  2337. if (input.substr(pos, 11).toLowerCase() === "declaration") {
  2338. result1 = input.substr(pos, 11);
  2339. pos += 11;
  2340. } else {
  2341. result1 = null;
  2342. if (reportFailures === 0) {
  2343. matchFailed("\"declaration\"");
  2344. }
  2345. }
  2346. if (result1 === null) {
  2347. if (input.substr(pos, 8).toLowerCase() === "function") {
  2348. result1 = input.substr(pos, 8);
  2349. pos += 8;
  2350. } else {
  2351. result1 = null;
  2352. if (reportFailures === 0) {
  2353. matchFailed("\"function\"");
  2354. }
  2355. }
  2356. if (result1 === null) {
  2357. if (input.substr(pos, 7).toLowerCase() === "pattern") {
  2358. result1 = input.substr(pos, 7);
  2359. pos += 7;
  2360. } else {
  2361. result1 = null;
  2362. if (reportFailures === 0) {
  2363. matchFailed("\"pattern\"");
  2364. }
  2365. }
  2366. }
  2367. }
  2368. }
  2369. }
  2370. if (result1 !== null) {
  2371. result0 = [result0, result1];
  2372. } else {
  2373. result0 = null;
  2374. pos = pos1;
  2375. }
  2376. } else {
  2377. result0 = null;
  2378. pos = pos1;
  2379. }
  2380. if (result0 !== null) {
  2381. result0 = (function(offset, c) {
  2382. return { type: 'class', name: c };
  2383. })(pos0, result0[1]);
  2384. }
  2385. if (result0 === null) {
  2386. pos = pos0;
  2387. }
  2388. cache[cacheKey] = {
  2389. nextPos: pos,
  2390. result: result0
  2391. };
  2392. return result0;
  2393. }
  2394. function cleanupExpected(expected) {
  2395. expected.sort();
  2396. var lastExpected = null;
  2397. var cleanExpected = [];
  2398. for (var i = 0; i < expected.length; i++) {
  2399. if (expected[i] !== lastExpected) {
  2400. cleanExpected.push(expected[i]);
  2401. lastExpected = expected[i];
  2402. }
  2403. }
  2404. return cleanExpected;
  2405. }
  2406. function computeErrorPosition() {
  2407. /*
  2408. * The first idea was to use |String.split| to break the input up to the
  2409. * error position along newlines and derive the line and column from
  2410. * there. However IE's |split| implementation is so broken that it was
  2411. * enough to prevent it.
  2412. */
  2413. var line = 1;
  2414. var column = 1;
  2415. var seenCR = false;
  2416. for (var i = 0; i < Math.max(pos, rightmostFailuresPos); i++) {
  2417. var ch = input.charAt(i);
  2418. if (ch === "\n") {
  2419. if (!seenCR) { line++; }
  2420. column = 1;
  2421. seenCR = false;
  2422. } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") {
  2423. line++;
  2424. column = 1;
  2425. seenCR = true;
  2426. } else {
  2427. column++;
  2428. seenCR = false;
  2429. }
  2430. }
  2431. return { line: line, column: column };
  2432. }
  2433. function nth(n) { return { type: 'nth-child', index: { type: 'literal', value: n } }; }
  2434. function nthLast(n) { return { type: 'nth-last-child', index: { type: 'literal', value: n } }; }
  2435. function strUnescape(s) {
  2436. return s.replace(/\\(.)/g, function(match, ch) {
  2437. switch(ch) {
  2438. case 'a': return '\a';
  2439. case 'b': return '\b';
  2440. case 'f': return '\f';
  2441. case 'n': return '\n';
  2442. case 'r': return '\r';
  2443. case 't': return '\t';
  2444. case 'v': return '\v';
  2445. default: return ch;
  2446. }
  2447. });
  2448. }
  2449. var result = parseFunctions[startRule]();
  2450. /*
  2451. * The parser is now in one of the following three states:
  2452. *
  2453. * 1. The parser successfully parsed the whole input.
  2454. *
  2455. * - |result !== null|
  2456. * - |pos === input.length|
  2457. * - |rightmostFailuresExpected| may or may not contain something
  2458. *
  2459. * 2. The parser successfully parsed only a part of the input.
  2460. *
  2461. * - |result !== null|
  2462. * - |pos < input.length|
  2463. * - |rightmostFailuresExpected| may or may not contain something
  2464. *
  2465. * 3. The parser did not successfully parse any part of the input.
  2466. *
  2467. * - |result === null|
  2468. * - |pos === 0|
  2469. * - |rightmostFailuresExpected| contains at least one failure
  2470. *
  2471. * All code following this comment (including called functions) must
  2472. * handle these states.
  2473. */
  2474. if (result === null || pos !== input.length) {
  2475. var offset = Math.max(pos, rightmostFailuresPos);
  2476. var found = offset < input.length ? input.charAt(offset) : null;
  2477. var errorPosition = computeErrorPosition();
  2478. throw new this.SyntaxError(
  2479. cleanupExpected(rightmostFailuresExpected),
  2480. found,
  2481. offset,
  2482. errorPosition.line,
  2483. errorPosition.column
  2484. );
  2485. }
  2486. return result;
  2487. },
  2488. /* Returns the parser source code. */
  2489. toSource: function() { return this._source; }
  2490. };
  2491. /* Thrown when a parser encounters a syntax error. */
  2492. result.SyntaxError = function(expected, found, offset, line, column) {
  2493. function buildMessage(expected, found) {
  2494. var expectedHumanized, foundHumanized;
  2495. switch (expected.length) {
  2496. case 0:
  2497. expectedHumanized = "end of input";
  2498. break;
  2499. case 1:
  2500. expectedHumanized = expected[0];
  2501. break;
  2502. default:
  2503. expectedHumanized = expected.slice(0, expected.length - 1).join(", ")
  2504. + " or "
  2505. + expected[expected.length - 1];
  2506. }
  2507. foundHumanized = found ? quote(found) : "end of input";
  2508. return "Expected " + expectedHumanized + " but " + foundHumanized + " found.";
  2509. }
  2510. this.name = "SyntaxError";
  2511. this.expected = expected;
  2512. this.found = found;
  2513. this.message = buildMessage(expected, found);
  2514. this.offset = offset;
  2515. this.line = line;
  2516. this.column = column;
  2517. };
  2518. result.SyntaxError.prototype = Error.prototype;
  2519. return result;
  2520. })();
  2521. if (typeof define === "function" && define.amd) { define(function(){ return result; }); } else if (typeof module !== "undefined" && module.exports) { module.exports = result; } else { this.esquery = result; }