Workflowy selector parser

A parser for a DSL used to select specific items in a Workflowy hierarchy.

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greatest.deepsurf.us/scripts/22247/141751/Workflowy%20selector%20parser.js

  1. window.Parser = (function() {
  2. "use strict";
  3.  
  4. /*
  5. * Generated by PEG.js 0.9.0.
  6. *
  7. * http://pegjs.org/
  8. */
  9.  
  10. function peg$subclass(child, parent) {
  11. function ctor() { this.constructor = child; }
  12. ctor.prototype = parent.prototype;
  13. child.prototype = new ctor();
  14. }
  15.  
  16. function peg$SyntaxError(message, expected, found, location) {
  17. this.message = message;
  18. this.expected = expected;
  19. this.found = found;
  20. this.location = location;
  21. this.name = "SyntaxError";
  22.  
  23. if (typeof Error.captureStackTrace === "function") {
  24. Error.captureStackTrace(this, peg$SyntaxError);
  25. }
  26. }
  27.  
  28. peg$subclass(peg$SyntaxError, Error);
  29.  
  30. function peg$parse(input) {
  31. var options = arguments.length > 1 ? arguments[1] : {},
  32. parser = this,
  33.  
  34. peg$FAILED = {},
  35.  
  36. peg$startRuleFunctions = { expr: peg$parseexpr },
  37. peg$startRuleFunction = peg$parseexpr,
  38.  
  39. peg$c0 = function(s, ch) {
  40. return [[null, s]].concat(ch.map(s => [s[0], s[2]]))
  41. },
  42. peg$c1 = "#",
  43. peg$c2 = { type: "literal", value: "#", description: "\"#\"" },
  44. peg$c3 = /^[a-z0-9\-]/i,
  45. peg$c4 = { type: "class", value: "[a-z0-9-]i", description: "[a-z0-9-]i" },
  46. peg$c5 = function(id) { return {type: 'id', val: id[1].join('')} },
  47. peg$c6 = "/",
  48. peg$c7 = { type: "literal", value: "/", description: "\"/\"" },
  49. peg$c8 = /^[^\/]/,
  50. peg$c9 = { type: "class", value: "[^/]", description: "[^/]" },
  51. peg$c10 = function(re) { return {type: 'regex', val: new RegExp(re[1].join(''))} },
  52. peg$c11 = "'",
  53. peg$c12 = { type: "literal", value: "'", description: "\"'\"" },
  54. peg$c13 = /^[^']/i,
  55. peg$c14 = { type: "class", value: "[^']i", description: "[^']i" },
  56. peg$c15 = function(name) { return {type: 'name', val: name[1].join('')} },
  57. peg$c16 = "*",
  58. peg$c17 = { type: "literal", value: "*", description: "\"*\"" },
  59. peg$c18 = function(any) { return {type: 'any'} },
  60. peg$c19 = ">",
  61. peg$c20 = { type: "literal", value: ">", description: "\">\"" },
  62. peg$c21 = function() { return 'directchild' },
  63. peg$c22 = " ",
  64. peg$c23 = { type: "literal", value: " ", description: "\" \"" },
  65.  
  66. peg$currPos = 0,
  67. peg$savedPos = 0,
  68. peg$posDetailsCache = [{ line: 1, column: 1, seenCR: false }],
  69. peg$maxFailPos = 0,
  70. peg$maxFailExpected = [],
  71. peg$silentFails = 0,
  72.  
  73. peg$result;
  74.  
  75. if ("startRule" in options) {
  76. if (!(options.startRule in peg$startRuleFunctions)) {
  77. throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
  78. }
  79.  
  80. peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
  81. }
  82.  
  83. function text() {
  84. return input.substring(peg$savedPos, peg$currPos);
  85. }
  86.  
  87. function location() {
  88. return peg$computeLocation(peg$savedPos, peg$currPos);
  89. }
  90.  
  91. function expected(description) {
  92. throw peg$buildException(
  93. null,
  94. [{ type: "other", description: description }],
  95. input.substring(peg$savedPos, peg$currPos),
  96. peg$computeLocation(peg$savedPos, peg$currPos)
  97. );
  98. }
  99.  
  100. function error(message) {
  101. throw peg$buildException(
  102. message,
  103. null,
  104. input.substring(peg$savedPos, peg$currPos),
  105. peg$computeLocation(peg$savedPos, peg$currPos)
  106. );
  107. }
  108.  
  109. function peg$computePosDetails(pos) {
  110. var details = peg$posDetailsCache[pos],
  111. p, ch;
  112.  
  113. if (details) {
  114. return details;
  115. } else {
  116. p = pos - 1;
  117. while (!peg$posDetailsCache[p]) {
  118. p--;
  119. }
  120.  
  121. details = peg$posDetailsCache[p];
  122. details = {
  123. line: details.line,
  124. column: details.column,
  125. seenCR: details.seenCR
  126. };
  127.  
  128. while (p < pos) {
  129. ch = input.charAt(p);
  130. if (ch === "\n") {
  131. if (!details.seenCR) { details.line++; }
  132. details.column = 1;
  133. details.seenCR = false;
  134. } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") {
  135. details.line++;
  136. details.column = 1;
  137. details.seenCR = true;
  138. } else {
  139. details.column++;
  140. details.seenCR = false;
  141. }
  142.  
  143. p++;
  144. }
  145.  
  146. peg$posDetailsCache[pos] = details;
  147. return details;
  148. }
  149. }
  150.  
  151. function peg$computeLocation(startPos, endPos) {
  152. var startPosDetails = peg$computePosDetails(startPos),
  153. endPosDetails = peg$computePosDetails(endPos);
  154.  
  155. return {
  156. start: {
  157. offset: startPos,
  158. line: startPosDetails.line,
  159. column: startPosDetails.column
  160. },
  161. end: {
  162. offset: endPos,
  163. line: endPosDetails.line,
  164. column: endPosDetails.column
  165. }
  166. };
  167. }
  168.  
  169. function peg$fail(expected) {
  170. if (peg$currPos < peg$maxFailPos) { return; }
  171.  
  172. if (peg$currPos > peg$maxFailPos) {
  173. peg$maxFailPos = peg$currPos;
  174. peg$maxFailExpected = [];
  175. }
  176.  
  177. peg$maxFailExpected.push(expected);
  178. }
  179.  
  180. function peg$buildException(message, expected, found, location) {
  181. function cleanupExpected(expected) {
  182. var i = 1;
  183.  
  184. expected.sort(function(a, b) {
  185. if (a.description < b.description) {
  186. return -1;
  187. } else if (a.description > b.description) {
  188. return 1;
  189. } else {
  190. return 0;
  191. }
  192. });
  193.  
  194. while (i < expected.length) {
  195. if (expected[i - 1] === expected[i]) {
  196. expected.splice(i, 1);
  197. } else {
  198. i++;
  199. }
  200. }
  201. }
  202.  
  203. function buildMessage(expected, found) {
  204. function stringEscape(s) {
  205. function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); }
  206.  
  207. return s
  208. .replace(/\\/g, '\\\\')
  209. .replace(/"/g, '\\"')
  210. .replace(/\x08/g, '\\b')
  211. .replace(/\t/g, '\\t')
  212. .replace(/\n/g, '\\n')
  213. .replace(/\f/g, '\\f')
  214. .replace(/\r/g, '\\r')
  215. .replace(/[\x00-\x07\x0B\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
  216. .replace(/[\x10-\x1F\x80-\xFF]/g, function(ch) { return '\\x' + hex(ch); })
  217. .replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); })
  218. .replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); });
  219. }
  220.  
  221. var expectedDescs = new Array(expected.length),
  222. expectedDesc, foundDesc, i;
  223.  
  224. for (i = 0; i < expected.length; i++) {
  225. expectedDescs[i] = expected[i].description;
  226. }
  227.  
  228. expectedDesc = expected.length > 1
  229. ? expectedDescs.slice(0, -1).join(", ")
  230. + " or "
  231. + expectedDescs[expected.length - 1]
  232. : expectedDescs[0];
  233.  
  234. foundDesc = found ? "\"" + stringEscape(found) + "\"" : "end of input";
  235.  
  236. return "Expected " + expectedDesc + " but " + foundDesc + " found.";
  237. }
  238.  
  239. if (expected !== null) {
  240. cleanupExpected(expected);
  241. }
  242.  
  243. return new peg$SyntaxError(
  244. message !== null ? message : buildMessage(expected, found),
  245. expected,
  246. found,
  247. location
  248. );
  249. }
  250.  
  251. function peg$parseexpr() {
  252. var s0, s1, s2, s3, s4, s5, s6, s7, s8;
  253.  
  254. s0 = peg$currPos;
  255. s1 = peg$parseselector();
  256. if (s1 !== peg$FAILED) {
  257. s2 = peg$parse_();
  258. if (s2 !== peg$FAILED) {
  259. s3 = [];
  260. s4 = peg$currPos;
  261. s5 = peg$parseconnector();
  262. if (s5 === peg$FAILED) {
  263. s5 = null;
  264. }
  265. if (s5 !== peg$FAILED) {
  266. s6 = peg$parse_();
  267. if (s6 !== peg$FAILED) {
  268. s7 = peg$parseselector();
  269. if (s7 !== peg$FAILED) {
  270. s8 = peg$parse_();
  271. if (s8 !== peg$FAILED) {
  272. s5 = [s5, s6, s7, s8];
  273. s4 = s5;
  274. } else {
  275. peg$currPos = s4;
  276. s4 = peg$FAILED;
  277. }
  278. } else {
  279. peg$currPos = s4;
  280. s4 = peg$FAILED;
  281. }
  282. } else {
  283. peg$currPos = s4;
  284. s4 = peg$FAILED;
  285. }
  286. } else {
  287. peg$currPos = s4;
  288. s4 = peg$FAILED;
  289. }
  290. while (s4 !== peg$FAILED) {
  291. s3.push(s4);
  292. s4 = peg$currPos;
  293. s5 = peg$parseconnector();
  294. if (s5 === peg$FAILED) {
  295. s5 = null;
  296. }
  297. if (s5 !== peg$FAILED) {
  298. s6 = peg$parse_();
  299. if (s6 !== peg$FAILED) {
  300. s7 = peg$parseselector();
  301. if (s7 !== peg$FAILED) {
  302. s8 = peg$parse_();
  303. if (s8 !== peg$FAILED) {
  304. s5 = [s5, s6, s7, s8];
  305. s4 = s5;
  306. } else {
  307. peg$currPos = s4;
  308. s4 = peg$FAILED;
  309. }
  310. } else {
  311. peg$currPos = s4;
  312. s4 = peg$FAILED;
  313. }
  314. } else {
  315. peg$currPos = s4;
  316. s4 = peg$FAILED;
  317. }
  318. } else {
  319. peg$currPos = s4;
  320. s4 = peg$FAILED;
  321. }
  322. }
  323. if (s3 !== peg$FAILED) {
  324. peg$savedPos = s0;
  325. s1 = peg$c0(s1, s3);
  326. s0 = s1;
  327. } else {
  328. peg$currPos = s0;
  329. s0 = peg$FAILED;
  330. }
  331. } else {
  332. peg$currPos = s0;
  333. s0 = peg$FAILED;
  334. }
  335. } else {
  336. peg$currPos = s0;
  337. s0 = peg$FAILED;
  338. }
  339.  
  340. return s0;
  341. }
  342.  
  343. function peg$parseselector() {
  344. var s0, s1, s2, s3, s4;
  345.  
  346. s0 = peg$currPos;
  347. s1 = peg$currPos;
  348. if (input.charCodeAt(peg$currPos) === 35) {
  349. s2 = peg$c1;
  350. peg$currPos++;
  351. } else {
  352. s2 = peg$FAILED;
  353. if (peg$silentFails === 0) { peg$fail(peg$c2); }
  354. }
  355. if (s2 !== peg$FAILED) {
  356. s3 = [];
  357. if (peg$c3.test(input.charAt(peg$currPos))) {
  358. s4 = input.charAt(peg$currPos);
  359. peg$currPos++;
  360. } else {
  361. s4 = peg$FAILED;
  362. if (peg$silentFails === 0) { peg$fail(peg$c4); }
  363. }
  364. if (s4 !== peg$FAILED) {
  365. while (s4 !== peg$FAILED) {
  366. s3.push(s4);
  367. if (peg$c3.test(input.charAt(peg$currPos))) {
  368. s4 = input.charAt(peg$currPos);
  369. peg$currPos++;
  370. } else {
  371. s4 = peg$FAILED;
  372. if (peg$silentFails === 0) { peg$fail(peg$c4); }
  373. }
  374. }
  375. } else {
  376. s3 = peg$FAILED;
  377. }
  378. if (s3 !== peg$FAILED) {
  379. s2 = [s2, s3];
  380. s1 = s2;
  381. } else {
  382. peg$currPos = s1;
  383. s1 = peg$FAILED;
  384. }
  385. } else {
  386. peg$currPos = s1;
  387. s1 = peg$FAILED;
  388. }
  389. if (s1 !== peg$FAILED) {
  390. peg$savedPos = s0;
  391. s1 = peg$c5(s1);
  392. }
  393. s0 = s1;
  394. if (s0 === peg$FAILED) {
  395. s0 = peg$currPos;
  396. s1 = peg$currPos;
  397. if (input.charCodeAt(peg$currPos) === 47) {
  398. s2 = peg$c6;
  399. peg$currPos++;
  400. } else {
  401. s2 = peg$FAILED;
  402. if (peg$silentFails === 0) { peg$fail(peg$c7); }
  403. }
  404. if (s2 !== peg$FAILED) {
  405. s3 = [];
  406. if (peg$c8.test(input.charAt(peg$currPos))) {
  407. s4 = input.charAt(peg$currPos);
  408. peg$currPos++;
  409. } else {
  410. s4 = peg$FAILED;
  411. if (peg$silentFails === 0) { peg$fail(peg$c9); }
  412. }
  413. if (s4 !== peg$FAILED) {
  414. while (s4 !== peg$FAILED) {
  415. s3.push(s4);
  416. if (peg$c8.test(input.charAt(peg$currPos))) {
  417. s4 = input.charAt(peg$currPos);
  418. peg$currPos++;
  419. } else {
  420. s4 = peg$FAILED;
  421. if (peg$silentFails === 0) { peg$fail(peg$c9); }
  422. }
  423. }
  424. } else {
  425. s3 = peg$FAILED;
  426. }
  427. if (s3 !== peg$FAILED) {
  428. if (input.charCodeAt(peg$currPos) === 47) {
  429. s4 = peg$c6;
  430. peg$currPos++;
  431. } else {
  432. s4 = peg$FAILED;
  433. if (peg$silentFails === 0) { peg$fail(peg$c7); }
  434. }
  435. if (s4 !== peg$FAILED) {
  436. s2 = [s2, s3, s4];
  437. s1 = s2;
  438. } else {
  439. peg$currPos = s1;
  440. s1 = peg$FAILED;
  441. }
  442. } else {
  443. peg$currPos = s1;
  444. s1 = peg$FAILED;
  445. }
  446. } else {
  447. peg$currPos = s1;
  448. s1 = peg$FAILED;
  449. }
  450. if (s1 !== peg$FAILED) {
  451. peg$savedPos = s0;
  452. s1 = peg$c10(s1);
  453. }
  454. s0 = s1;
  455. if (s0 === peg$FAILED) {
  456. s0 = peg$currPos;
  457. s1 = peg$currPos;
  458. if (input.charCodeAt(peg$currPos) === 39) {
  459. s2 = peg$c11;
  460. peg$currPos++;
  461. } else {
  462. s2 = peg$FAILED;
  463. if (peg$silentFails === 0) { peg$fail(peg$c12); }
  464. }
  465. if (s2 !== peg$FAILED) {
  466. s3 = [];
  467. if (peg$c13.test(input.charAt(peg$currPos))) {
  468. s4 = input.charAt(peg$currPos);
  469. peg$currPos++;
  470. } else {
  471. s4 = peg$FAILED;
  472. if (peg$silentFails === 0) { peg$fail(peg$c14); }
  473. }
  474. if (s4 !== peg$FAILED) {
  475. while (s4 !== peg$FAILED) {
  476. s3.push(s4);
  477. if (peg$c13.test(input.charAt(peg$currPos))) {
  478. s4 = input.charAt(peg$currPos);
  479. peg$currPos++;
  480. } else {
  481. s4 = peg$FAILED;
  482. if (peg$silentFails === 0) { peg$fail(peg$c14); }
  483. }
  484. }
  485. } else {
  486. s3 = peg$FAILED;
  487. }
  488. if (s3 !== peg$FAILED) {
  489. if (input.charCodeAt(peg$currPos) === 39) {
  490. s4 = peg$c11;
  491. peg$currPos++;
  492. } else {
  493. s4 = peg$FAILED;
  494. if (peg$silentFails === 0) { peg$fail(peg$c12); }
  495. }
  496. if (s4 !== peg$FAILED) {
  497. s2 = [s2, s3, s4];
  498. s1 = s2;
  499. } else {
  500. peg$currPos = s1;
  501. s1 = peg$FAILED;
  502. }
  503. } else {
  504. peg$currPos = s1;
  505. s1 = peg$FAILED;
  506. }
  507. } else {
  508. peg$currPos = s1;
  509. s1 = peg$FAILED;
  510. }
  511. if (s1 !== peg$FAILED) {
  512. peg$savedPos = s0;
  513. s1 = peg$c15(s1);
  514. }
  515. s0 = s1;
  516. if (s0 === peg$FAILED) {
  517. s0 = peg$currPos;
  518. if (input.charCodeAt(peg$currPos) === 42) {
  519. s1 = peg$c16;
  520. peg$currPos++;
  521. } else {
  522. s1 = peg$FAILED;
  523. if (peg$silentFails === 0) { peg$fail(peg$c17); }
  524. }
  525. if (s1 !== peg$FAILED) {
  526. peg$savedPos = s0;
  527. s1 = peg$c18(s1);
  528. }
  529. s0 = s1;
  530. }
  531. }
  532. }
  533.  
  534. return s0;
  535. }
  536.  
  537. function peg$parseconnector() {
  538. var s0, s1;
  539.  
  540. s0 = peg$currPos;
  541. if (input.charCodeAt(peg$currPos) === 62) {
  542. s1 = peg$c19;
  543. peg$currPos++;
  544. } else {
  545. s1 = peg$FAILED;
  546. if (peg$silentFails === 0) { peg$fail(peg$c20); }
  547. }
  548. if (s1 !== peg$FAILED) {
  549. peg$savedPos = s0;
  550. s1 = peg$c21();
  551. }
  552. s0 = s1;
  553.  
  554. return s0;
  555. }
  556.  
  557. function peg$parse_() {
  558. var s0, s1;
  559.  
  560. s0 = [];
  561. if (input.charCodeAt(peg$currPos) === 32) {
  562. s1 = peg$c22;
  563. peg$currPos++;
  564. } else {
  565. s1 = peg$FAILED;
  566. if (peg$silentFails === 0) { peg$fail(peg$c23); }
  567. }
  568. while (s1 !== peg$FAILED) {
  569. s0.push(s1);
  570. if (input.charCodeAt(peg$currPos) === 32) {
  571. s1 = peg$c22;
  572. peg$currPos++;
  573. } else {
  574. s1 = peg$FAILED;
  575. if (peg$silentFails === 0) { peg$fail(peg$c23); }
  576. }
  577. }
  578.  
  579. return s0;
  580. }
  581.  
  582. peg$result = peg$startRuleFunction();
  583.  
  584. if (peg$result !== peg$FAILED && peg$currPos === input.length) {
  585. return peg$result;
  586. } else {
  587. if (peg$result !== peg$FAILED && peg$currPos < input.length) {
  588. peg$fail({ type: "end", description: "end of input" });
  589. }
  590.  
  591. throw peg$buildException(
  592. null,
  593. peg$maxFailExpected,
  594. peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null,
  595. peg$maxFailPos < input.length
  596. ? peg$computeLocation(peg$maxFailPos, peg$maxFailPos + 1)
  597. : peg$computeLocation(peg$maxFailPos, peg$maxFailPos)
  598. );
  599. }
  600. }
  601.  
  602. return {
  603. SyntaxError: peg$SyntaxError,
  604. parse: peg$parse
  605. };
  606. })();