parser.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. module.exports = (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. "segmentTail": parse_segmentTail,
  40. "segment": parse_segment,
  41. "string": parse_string,
  42. "chars": parse_chars,
  43. "char": parse_char,
  44. "hexDigit": parse_hexDigit,
  45. "identifier": parse_identifier,
  46. "number": parse_number,
  47. "size": parse_size,
  48. "specifierList": parse_specifierList,
  49. "specifierTail": parse_specifierTail,
  50. "specifier": parse_specifier,
  51. "unit": parse_unit,
  52. "ws": parse_ws
  53. };
  54. if (startRule !== undefined) {
  55. if (parseFunctions[startRule] === undefined) {
  56. throw new Error("Invalid rule name: " + quote(startRule) + ".");
  57. }
  58. } else {
  59. startRule = "start";
  60. }
  61. var pos = 0;
  62. var reportFailures = 0;
  63. var rightmostFailuresPos = 0;
  64. var rightmostFailuresExpected = [];
  65. function padLeft(input, padding, length) {
  66. var result = input;
  67. var padLength = length - input.length;
  68. for (var i = 0; i < padLength; i++) {
  69. result = padding + result;
  70. }
  71. return result;
  72. }
  73. function escape(ch) {
  74. var charCode = ch.charCodeAt(0);
  75. var escapeChar;
  76. var length;
  77. if (charCode <= 0xFF) {
  78. escapeChar = 'x';
  79. length = 2;
  80. } else {
  81. escapeChar = 'u';
  82. length = 4;
  83. }
  84. return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);
  85. }
  86. function matchFailed(failure) {
  87. if (pos < rightmostFailuresPos) {
  88. return;
  89. }
  90. if (pos > rightmostFailuresPos) {
  91. rightmostFailuresPos = pos;
  92. rightmostFailuresExpected = [];
  93. }
  94. rightmostFailuresExpected.push(failure);
  95. }
  96. function parse_start() {
  97. var result0, result1, result2, result3;
  98. var pos0, pos1;
  99. pos0 = pos;
  100. pos1 = pos;
  101. result0 = parse_ws();
  102. if (result0 !== null) {
  103. result1 = parse_segment();
  104. if (result1 !== null) {
  105. result2 = [];
  106. result3 = parse_segmentTail();
  107. while (result3 !== null) {
  108. result2.push(result3);
  109. result3 = parse_segmentTail();
  110. }
  111. if (result2 !== null) {
  112. result0 = [result0, result1, result2];
  113. } else {
  114. result0 = null;
  115. pos = pos1;
  116. }
  117. } else {
  118. result0 = null;
  119. pos = pos1;
  120. }
  121. } else {
  122. result0 = null;
  123. pos = pos1;
  124. }
  125. if (result0 !== null) {
  126. result0 = (function(offset, head, tail) { tail.unshift(head); return tail; })(pos0, result0[1], result0[2]);
  127. }
  128. if (result0 === null) {
  129. pos = pos0;
  130. }
  131. return result0;
  132. }
  133. function parse_segmentTail() {
  134. var result0, result1, result2, result3;
  135. var pos0, pos1;
  136. pos0 = pos;
  137. pos1 = pos;
  138. result0 = parse_ws();
  139. if (result0 !== null) {
  140. if (input.charCodeAt(pos) === 44) {
  141. result1 = ",";
  142. pos++;
  143. } else {
  144. result1 = null;
  145. if (reportFailures === 0) {
  146. matchFailed("\",\"");
  147. }
  148. }
  149. if (result1 !== null) {
  150. result2 = parse_ws();
  151. if (result2 !== null) {
  152. result3 = parse_segment();
  153. if (result3 !== null) {
  154. result0 = [result0, result1, result2, result3];
  155. } else {
  156. result0 = null;
  157. pos = pos1;
  158. }
  159. } else {
  160. result0 = null;
  161. pos = pos1;
  162. }
  163. } else {
  164. result0 = null;
  165. pos = pos1;
  166. }
  167. } else {
  168. result0 = null;
  169. pos = pos1;
  170. }
  171. if (result0 !== null) {
  172. result0 = (function(offset, seg) { return seg; })(pos0, result0[3]);
  173. }
  174. if (result0 === null) {
  175. pos = pos0;
  176. }
  177. return result0;
  178. }
  179. function parse_segment() {
  180. var result0, result1, result2;
  181. var pos0, pos1;
  182. pos0 = pos;
  183. result0 = parse_string();
  184. if (result0 !== null) {
  185. result0 = (function(offset, str) { return {string: str}; })(pos0, result0);
  186. }
  187. if (result0 === null) {
  188. pos = pos0;
  189. }
  190. if (result0 === null) {
  191. pos0 = pos;
  192. pos1 = pos;
  193. result0 = parse_identifier();
  194. if (result0 !== null) {
  195. result1 = parse_size();
  196. result1 = result1 !== null ? result1 : "";
  197. if (result1 !== null) {
  198. result2 = parse_specifierList();
  199. result2 = result2 !== null ? result2 : "";
  200. if (result2 !== null) {
  201. result0 = [result0, result1, result2];
  202. } else {
  203. result0 = null;
  204. pos = pos1;
  205. }
  206. } else {
  207. result0 = null;
  208. pos = pos1;
  209. }
  210. } else {
  211. result0 = null;
  212. pos = pos1;
  213. }
  214. if (result0 !== null) {
  215. result0 = (function(offset, v, size, specs) { return {name: v, size: size, specifiers: specs}; })(pos0, result0[0], result0[1], result0[2]);
  216. }
  217. if (result0 === null) {
  218. pos = pos0;
  219. }
  220. if (result0 === null) {
  221. pos0 = pos;
  222. pos1 = pos;
  223. result0 = parse_number();
  224. if (result0 !== null) {
  225. result1 = parse_size();
  226. result1 = result1 !== null ? result1 : "";
  227. if (result1 !== null) {
  228. result2 = parse_specifierList();
  229. result2 = result2 !== null ? result2 : "";
  230. if (result2 !== null) {
  231. result0 = [result0, result1, result2];
  232. } else {
  233. result0 = null;
  234. pos = pos1;
  235. }
  236. } else {
  237. result0 = null;
  238. pos = pos1;
  239. }
  240. } else {
  241. result0 = null;
  242. pos = pos1;
  243. }
  244. if (result0 !== null) {
  245. result0 = (function(offset, v, size, specs) { return {value: v, size: size, specifiers: specs}; })(pos0, result0[0], result0[1], result0[2]);
  246. }
  247. if (result0 === null) {
  248. pos = pos0;
  249. }
  250. }
  251. }
  252. return result0;
  253. }
  254. function parse_string() {
  255. var result0, result1, result2;
  256. var pos0, pos1;
  257. pos0 = pos;
  258. pos1 = pos;
  259. if (input.charCodeAt(pos) === 34) {
  260. result0 = "\"";
  261. pos++;
  262. } else {
  263. result0 = null;
  264. if (reportFailures === 0) {
  265. matchFailed("\"\\\"\"");
  266. }
  267. }
  268. if (result0 !== null) {
  269. if (input.charCodeAt(pos) === 34) {
  270. result1 = "\"";
  271. pos++;
  272. } else {
  273. result1 = null;
  274. if (reportFailures === 0) {
  275. matchFailed("\"\\\"\"");
  276. }
  277. }
  278. if (result1 !== null) {
  279. result0 = [result0, result1];
  280. } else {
  281. result0 = null;
  282. pos = pos1;
  283. }
  284. } else {
  285. result0 = null;
  286. pos = pos1;
  287. }
  288. if (result0 !== null) {
  289. result0 = (function(offset) { return ""; })(pos0);
  290. }
  291. if (result0 === null) {
  292. pos = pos0;
  293. }
  294. if (result0 === null) {
  295. pos0 = pos;
  296. pos1 = pos;
  297. if (input.charCodeAt(pos) === 34) {
  298. result0 = "\"";
  299. pos++;
  300. } else {
  301. result0 = null;
  302. if (reportFailures === 0) {
  303. matchFailed("\"\\\"\"");
  304. }
  305. }
  306. if (result0 !== null) {
  307. result1 = parse_chars();
  308. if (result1 !== null) {
  309. if (input.charCodeAt(pos) === 34) {
  310. result2 = "\"";
  311. pos++;
  312. } else {
  313. result2 = null;
  314. if (reportFailures === 0) {
  315. matchFailed("\"\\\"\"");
  316. }
  317. }
  318. if (result2 !== null) {
  319. result0 = [result0, result1, result2];
  320. } else {
  321. result0 = null;
  322. pos = pos1;
  323. }
  324. } else {
  325. result0 = null;
  326. pos = pos1;
  327. }
  328. } else {
  329. result0 = null;
  330. pos = pos1;
  331. }
  332. if (result0 !== null) {
  333. result0 = (function(offset, chars) { return chars; })(pos0, result0[1]);
  334. }
  335. if (result0 === null) {
  336. pos = pos0;
  337. }
  338. }
  339. return result0;
  340. }
  341. function parse_chars() {
  342. var result0, result1;
  343. var pos0;
  344. pos0 = pos;
  345. result1 = parse_char();
  346. if (result1 !== null) {
  347. result0 = [];
  348. while (result1 !== null) {
  349. result0.push(result1);
  350. result1 = parse_char();
  351. }
  352. } else {
  353. result0 = null;
  354. }
  355. if (result0 !== null) {
  356. result0 = (function(offset, chars) { return chars.join(""); })(pos0, result0);
  357. }
  358. if (result0 === null) {
  359. pos = pos0;
  360. }
  361. return result0;
  362. }
  363. function parse_char() {
  364. var result0, result1, result2, result3, result4;
  365. var pos0, pos1;
  366. if (/^[^"\\\0-\x1F]/.test(input.charAt(pos))) {
  367. result0 = input.charAt(pos);
  368. pos++;
  369. } else {
  370. result0 = null;
  371. if (reportFailures === 0) {
  372. matchFailed("[^\"\\\\\\0-\\x1F]");
  373. }
  374. }
  375. if (result0 === null) {
  376. pos0 = pos;
  377. if (input.substr(pos, 2) === "\\\"") {
  378. result0 = "\\\"";
  379. pos += 2;
  380. } else {
  381. result0 = null;
  382. if (reportFailures === 0) {
  383. matchFailed("\"\\\\\\\"\"");
  384. }
  385. }
  386. if (result0 !== null) {
  387. result0 = (function(offset) { return '"'; })(pos0);
  388. }
  389. if (result0 === null) {
  390. pos = pos0;
  391. }
  392. if (result0 === null) {
  393. pos0 = pos;
  394. if (input.substr(pos, 2) === "\\\\") {
  395. result0 = "\\\\";
  396. pos += 2;
  397. } else {
  398. result0 = null;
  399. if (reportFailures === 0) {
  400. matchFailed("\"\\\\\\\\\"");
  401. }
  402. }
  403. if (result0 !== null) {
  404. result0 = (function(offset) { return "\\"; })(pos0);
  405. }
  406. if (result0 === null) {
  407. pos = pos0;
  408. }
  409. if (result0 === null) {
  410. pos0 = pos;
  411. if (input.substr(pos, 2) === "\\/") {
  412. result0 = "\\/";
  413. pos += 2;
  414. } else {
  415. result0 = null;
  416. if (reportFailures === 0) {
  417. matchFailed("\"\\\\/\"");
  418. }
  419. }
  420. if (result0 !== null) {
  421. result0 = (function(offset) { return "/"; })(pos0);
  422. }
  423. if (result0 === null) {
  424. pos = pos0;
  425. }
  426. if (result0 === null) {
  427. pos0 = pos;
  428. if (input.substr(pos, 2) === "\\b") {
  429. result0 = "\\b";
  430. pos += 2;
  431. } else {
  432. result0 = null;
  433. if (reportFailures === 0) {
  434. matchFailed("\"\\\\b\"");
  435. }
  436. }
  437. if (result0 !== null) {
  438. result0 = (function(offset) { return "\b"; })(pos0);
  439. }
  440. if (result0 === null) {
  441. pos = pos0;
  442. }
  443. if (result0 === null) {
  444. pos0 = pos;
  445. if (input.substr(pos, 2) === "\\f") {
  446. result0 = "\\f";
  447. pos += 2;
  448. } else {
  449. result0 = null;
  450. if (reportFailures === 0) {
  451. matchFailed("\"\\\\f\"");
  452. }
  453. }
  454. if (result0 !== null) {
  455. result0 = (function(offset) { return "\f"; })(pos0);
  456. }
  457. if (result0 === null) {
  458. pos = pos0;
  459. }
  460. if (result0 === null) {
  461. pos0 = pos;
  462. if (input.substr(pos, 2) === "\\n") {
  463. result0 = "\\n";
  464. pos += 2;
  465. } else {
  466. result0 = null;
  467. if (reportFailures === 0) {
  468. matchFailed("\"\\\\n\"");
  469. }
  470. }
  471. if (result0 !== null) {
  472. result0 = (function(offset) { return "\n"; })(pos0);
  473. }
  474. if (result0 === null) {
  475. pos = pos0;
  476. }
  477. if (result0 === null) {
  478. pos0 = pos;
  479. if (input.substr(pos, 2) === "\\r") {
  480. result0 = "\\r";
  481. pos += 2;
  482. } else {
  483. result0 = null;
  484. if (reportFailures === 0) {
  485. matchFailed("\"\\\\r\"");
  486. }
  487. }
  488. if (result0 !== null) {
  489. result0 = (function(offset) { return "\r"; })(pos0);
  490. }
  491. if (result0 === null) {
  492. pos = pos0;
  493. }
  494. if (result0 === null) {
  495. pos0 = pos;
  496. if (input.substr(pos, 2) === "\\t") {
  497. result0 = "\\t";
  498. pos += 2;
  499. } else {
  500. result0 = null;
  501. if (reportFailures === 0) {
  502. matchFailed("\"\\\\t\"");
  503. }
  504. }
  505. if (result0 !== null) {
  506. result0 = (function(offset) { return "\t"; })(pos0);
  507. }
  508. if (result0 === null) {
  509. pos = pos0;
  510. }
  511. if (result0 === null) {
  512. pos0 = pos;
  513. pos1 = pos;
  514. if (input.substr(pos, 2) === "\\u") {
  515. result0 = "\\u";
  516. pos += 2;
  517. } else {
  518. result0 = null;
  519. if (reportFailures === 0) {
  520. matchFailed("\"\\\\u\"");
  521. }
  522. }
  523. if (result0 !== null) {
  524. result1 = parse_hexDigit();
  525. if (result1 !== null) {
  526. result2 = parse_hexDigit();
  527. if (result2 !== null) {
  528. result3 = parse_hexDigit();
  529. if (result3 !== null) {
  530. result4 = parse_hexDigit();
  531. if (result4 !== null) {
  532. result0 = [result0, result1, result2, result3, result4];
  533. } else {
  534. result0 = null;
  535. pos = pos1;
  536. }
  537. } else {
  538. result0 = null;
  539. pos = pos1;
  540. }
  541. } else {
  542. result0 = null;
  543. pos = pos1;
  544. }
  545. } else {
  546. result0 = null;
  547. pos = pos1;
  548. }
  549. } else {
  550. result0 = null;
  551. pos = pos1;
  552. }
  553. if (result0 !== null) {
  554. result0 = (function(offset, h1, h2, h3, h4) {
  555. return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4));
  556. })(pos0, result0[1], result0[2], result0[3], result0[4]);
  557. }
  558. if (result0 === null) {
  559. pos = pos0;
  560. }
  561. }
  562. }
  563. }
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. return result0;
  571. }
  572. function parse_hexDigit() {
  573. var result0;
  574. if (/^[0-9a-fA-F]/.test(input.charAt(pos))) {
  575. result0 = input.charAt(pos);
  576. pos++;
  577. } else {
  578. result0 = null;
  579. if (reportFailures === 0) {
  580. matchFailed("[0-9a-fA-F]");
  581. }
  582. }
  583. return result0;
  584. }
  585. function parse_identifier() {
  586. var result0, result1, result2;
  587. var pos0, pos1;
  588. pos0 = pos;
  589. pos1 = pos;
  590. if (/^[_a-zA-Z]/.test(input.charAt(pos))) {
  591. result0 = input.charAt(pos);
  592. pos++;
  593. } else {
  594. result0 = null;
  595. if (reportFailures === 0) {
  596. matchFailed("[_a-zA-Z]");
  597. }
  598. }
  599. if (result0 !== null) {
  600. result1 = [];
  601. if (/^[_a-zA-Z0-9]/.test(input.charAt(pos))) {
  602. result2 = input.charAt(pos);
  603. pos++;
  604. } else {
  605. result2 = null;
  606. if (reportFailures === 0) {
  607. matchFailed("[_a-zA-Z0-9]");
  608. }
  609. }
  610. while (result2 !== null) {
  611. result1.push(result2);
  612. if (/^[_a-zA-Z0-9]/.test(input.charAt(pos))) {
  613. result2 = input.charAt(pos);
  614. pos++;
  615. } else {
  616. result2 = null;
  617. if (reportFailures === 0) {
  618. matchFailed("[_a-zA-Z0-9]");
  619. }
  620. }
  621. }
  622. if (result1 !== null) {
  623. result0 = [result0, result1];
  624. } else {
  625. result0 = null;
  626. pos = pos1;
  627. }
  628. } else {
  629. result0 = null;
  630. pos = pos1;
  631. }
  632. if (result0 !== null) {
  633. result0 = (function(offset, head, tail) { return head + tail.join(''); })(pos0, result0[0], result0[1]);
  634. }
  635. if (result0 === null) {
  636. pos = pos0;
  637. }
  638. return result0;
  639. }
  640. function parse_number() {
  641. var result0, result1, result2;
  642. var pos0, pos1;
  643. pos0 = pos;
  644. if (input.charCodeAt(pos) === 48) {
  645. result0 = "0";
  646. pos++;
  647. } else {
  648. result0 = null;
  649. if (reportFailures === 0) {
  650. matchFailed("\"0\"");
  651. }
  652. }
  653. if (result0 !== null) {
  654. result0 = (function(offset) { return 0; })(pos0);
  655. }
  656. if (result0 === null) {
  657. pos = pos0;
  658. }
  659. if (result0 === null) {
  660. pos0 = pos;
  661. pos1 = pos;
  662. if (/^[1-9]/.test(input.charAt(pos))) {
  663. result0 = input.charAt(pos);
  664. pos++;
  665. } else {
  666. result0 = null;
  667. if (reportFailures === 0) {
  668. matchFailed("[1-9]");
  669. }
  670. }
  671. if (result0 !== null) {
  672. result1 = [];
  673. if (/^[0-9]/.test(input.charAt(pos))) {
  674. result2 = input.charAt(pos);
  675. pos++;
  676. } else {
  677. result2 = null;
  678. if (reportFailures === 0) {
  679. matchFailed("[0-9]");
  680. }
  681. }
  682. while (result2 !== null) {
  683. result1.push(result2);
  684. if (/^[0-9]/.test(input.charAt(pos))) {
  685. result2 = input.charAt(pos);
  686. pos++;
  687. } else {
  688. result2 = null;
  689. if (reportFailures === 0) {
  690. matchFailed("[0-9]");
  691. }
  692. }
  693. }
  694. if (result1 !== null) {
  695. result0 = [result0, result1];
  696. } else {
  697. result0 = null;
  698. pos = pos1;
  699. }
  700. } else {
  701. result0 = null;
  702. pos = pos1;
  703. }
  704. if (result0 !== null) {
  705. result0 = (function(offset, head, tail) { return parseInt(head + tail.join('')); })(pos0, result0[0], result0[1]);
  706. }
  707. if (result0 === null) {
  708. pos = pos0;
  709. }
  710. }
  711. return result0;
  712. }
  713. function parse_size() {
  714. var result0, result1;
  715. var pos0, pos1;
  716. pos0 = pos;
  717. pos1 = pos;
  718. if (input.charCodeAt(pos) === 58) {
  719. result0 = ":";
  720. pos++;
  721. } else {
  722. result0 = null;
  723. if (reportFailures === 0) {
  724. matchFailed("\":\"");
  725. }
  726. }
  727. if (result0 !== null) {
  728. result1 = parse_number();
  729. if (result1 !== null) {
  730. result0 = [result0, result1];
  731. } else {
  732. result0 = null;
  733. pos = pos1;
  734. }
  735. } else {
  736. result0 = null;
  737. pos = pos1;
  738. }
  739. if (result0 !== null) {
  740. result0 = (function(offset, num) { return num; })(pos0, result0[1]);
  741. }
  742. if (result0 === null) {
  743. pos = pos0;
  744. }
  745. if (result0 === null) {
  746. pos0 = pos;
  747. pos1 = pos;
  748. if (input.charCodeAt(pos) === 58) {
  749. result0 = ":";
  750. pos++;
  751. } else {
  752. result0 = null;
  753. if (reportFailures === 0) {
  754. matchFailed("\":\"");
  755. }
  756. }
  757. if (result0 !== null) {
  758. result1 = parse_identifier();
  759. if (result1 !== null) {
  760. result0 = [result0, result1];
  761. } else {
  762. result0 = null;
  763. pos = pos1;
  764. }
  765. } else {
  766. result0 = null;
  767. pos = pos1;
  768. }
  769. if (result0 !== null) {
  770. result0 = (function(offset, id) { return id; })(pos0, result0[1]);
  771. }
  772. if (result0 === null) {
  773. pos = pos0;
  774. }
  775. }
  776. return result0;
  777. }
  778. function parse_specifierList() {
  779. var result0, result1, result2, result3;
  780. var pos0, pos1;
  781. pos0 = pos;
  782. pos1 = pos;
  783. if (input.charCodeAt(pos) === 47) {
  784. result0 = "/";
  785. pos++;
  786. } else {
  787. result0 = null;
  788. if (reportFailures === 0) {
  789. matchFailed("\"/\"");
  790. }
  791. }
  792. if (result0 !== null) {
  793. result1 = parse_specifier();
  794. if (result1 !== null) {
  795. result2 = [];
  796. result3 = parse_specifierTail();
  797. while (result3 !== null) {
  798. result2.push(result3);
  799. result3 = parse_specifierTail();
  800. }
  801. if (result2 !== null) {
  802. result0 = [result0, result1, result2];
  803. } else {
  804. result0 = null;
  805. pos = pos1;
  806. }
  807. } else {
  808. result0 = null;
  809. pos = pos1;
  810. }
  811. } else {
  812. result0 = null;
  813. pos = pos1;
  814. }
  815. if (result0 !== null) {
  816. result0 = (function(offset, head, tail) { tail.unshift(head); return tail; })(pos0, result0[1], result0[2]);
  817. }
  818. if (result0 === null) {
  819. pos = pos0;
  820. }
  821. return result0;
  822. }
  823. function parse_specifierTail() {
  824. var result0, result1;
  825. var pos0, pos1;
  826. pos0 = pos;
  827. pos1 = pos;
  828. if (input.charCodeAt(pos) === 45) {
  829. result0 = "-";
  830. pos++;
  831. } else {
  832. result0 = null;
  833. if (reportFailures === 0) {
  834. matchFailed("\"-\"");
  835. }
  836. }
  837. if (result0 !== null) {
  838. result1 = parse_specifier();
  839. if (result1 !== null) {
  840. result0 = [result0, result1];
  841. } else {
  842. result0 = null;
  843. pos = pos1;
  844. }
  845. } else {
  846. result0 = null;
  847. pos = pos1;
  848. }
  849. if (result0 !== null) {
  850. result0 = (function(offset, spec) { return spec; })(pos0, result0[1]);
  851. }
  852. if (result0 === null) {
  853. pos = pos0;
  854. }
  855. return result0;
  856. }
  857. function parse_specifier() {
  858. var result0;
  859. if (input.substr(pos, 6) === "little") {
  860. result0 = "little";
  861. pos += 6;
  862. } else {
  863. result0 = null;
  864. if (reportFailures === 0) {
  865. matchFailed("\"little\"");
  866. }
  867. }
  868. if (result0 === null) {
  869. if (input.substr(pos, 3) === "big") {
  870. result0 = "big";
  871. pos += 3;
  872. } else {
  873. result0 = null;
  874. if (reportFailures === 0) {
  875. matchFailed("\"big\"");
  876. }
  877. }
  878. if (result0 === null) {
  879. if (input.substr(pos, 6) === "signed") {
  880. result0 = "signed";
  881. pos += 6;
  882. } else {
  883. result0 = null;
  884. if (reportFailures === 0) {
  885. matchFailed("\"signed\"");
  886. }
  887. }
  888. if (result0 === null) {
  889. if (input.substr(pos, 8) === "unsigned") {
  890. result0 = "unsigned";
  891. pos += 8;
  892. } else {
  893. result0 = null;
  894. if (reportFailures === 0) {
  895. matchFailed("\"unsigned\"");
  896. }
  897. }
  898. if (result0 === null) {
  899. if (input.substr(pos, 7) === "integer") {
  900. result0 = "integer";
  901. pos += 7;
  902. } else {
  903. result0 = null;
  904. if (reportFailures === 0) {
  905. matchFailed("\"integer\"");
  906. }
  907. }
  908. if (result0 === null) {
  909. if (input.substr(pos, 6) === "binary") {
  910. result0 = "binary";
  911. pos += 6;
  912. } else {
  913. result0 = null;
  914. if (reportFailures === 0) {
  915. matchFailed("\"binary\"");
  916. }
  917. }
  918. if (result0 === null) {
  919. if (input.substr(pos, 5) === "float") {
  920. result0 = "float";
  921. pos += 5;
  922. } else {
  923. result0 = null;
  924. if (reportFailures === 0) {
  925. matchFailed("\"float\"");
  926. }
  927. }
  928. if (result0 === null) {
  929. result0 = parse_unit();
  930. }
  931. }
  932. }
  933. }
  934. }
  935. }
  936. }
  937. return result0;
  938. }
  939. function parse_unit() {
  940. var result0, result1;
  941. var pos0, pos1;
  942. pos0 = pos;
  943. pos1 = pos;
  944. if (input.substr(pos, 5) === "unit:") {
  945. result0 = "unit:";
  946. pos += 5;
  947. } else {
  948. result0 = null;
  949. if (reportFailures === 0) {
  950. matchFailed("\"unit:\"");
  951. }
  952. }
  953. if (result0 !== null) {
  954. result1 = parse_number();
  955. if (result1 !== null) {
  956. result0 = [result0, result1];
  957. } else {
  958. result0 = null;
  959. pos = pos1;
  960. }
  961. } else {
  962. result0 = null;
  963. pos = pos1;
  964. }
  965. if (result0 !== null) {
  966. result0 = (function(offset, num) { return 'unit:' + num; })(pos0, result0[1]);
  967. }
  968. if (result0 === null) {
  969. pos = pos0;
  970. }
  971. return result0;
  972. }
  973. function parse_ws() {
  974. var result0, result1;
  975. result0 = [];
  976. if (/^[ \t\n]/.test(input.charAt(pos))) {
  977. result1 = input.charAt(pos);
  978. pos++;
  979. } else {
  980. result1 = null;
  981. if (reportFailures === 0) {
  982. matchFailed("[ \\t\\n]");
  983. }
  984. }
  985. while (result1 !== null) {
  986. result0.push(result1);
  987. if (/^[ \t\n]/.test(input.charAt(pos))) {
  988. result1 = input.charAt(pos);
  989. pos++;
  990. } else {
  991. result1 = null;
  992. if (reportFailures === 0) {
  993. matchFailed("[ \\t\\n]");
  994. }
  995. }
  996. }
  997. return result0;
  998. }
  999. function cleanupExpected(expected) {
  1000. expected.sort();
  1001. var lastExpected = null;
  1002. var cleanExpected = [];
  1003. for (var i = 0; i < expected.length; i++) {
  1004. if (expected[i] !== lastExpected) {
  1005. cleanExpected.push(expected[i]);
  1006. lastExpected = expected[i];
  1007. }
  1008. }
  1009. return cleanExpected;
  1010. }
  1011. function computeErrorPosition() {
  1012. /*
  1013. * The first idea was to use |String.split| to break the input up to the
  1014. * error position along newlines and derive the line and column from
  1015. * there. However IE's |split| implementation is so broken that it was
  1016. * enough to prevent it.
  1017. */
  1018. var line = 1;
  1019. var column = 1;
  1020. var seenCR = false;
  1021. for (var i = 0; i < Math.max(pos, rightmostFailuresPos); i++) {
  1022. var ch = input.charAt(i);
  1023. if (ch === "\n") {
  1024. if (!seenCR) { line++; }
  1025. column = 1;
  1026. seenCR = false;
  1027. } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") {
  1028. line++;
  1029. column = 1;
  1030. seenCR = true;
  1031. } else {
  1032. column++;
  1033. seenCR = false;
  1034. }
  1035. }
  1036. return { line: line, column: column };
  1037. }
  1038. var result = parseFunctions[startRule]();
  1039. /*
  1040. * The parser is now in one of the following three states:
  1041. *
  1042. * 1. The parser successfully parsed the whole input.
  1043. *
  1044. * - |result !== null|
  1045. * - |pos === input.length|
  1046. * - |rightmostFailuresExpected| may or may not contain something
  1047. *
  1048. * 2. The parser successfully parsed only a part of the input.
  1049. *
  1050. * - |result !== null|
  1051. * - |pos < input.length|
  1052. * - |rightmostFailuresExpected| may or may not contain something
  1053. *
  1054. * 3. The parser did not successfully parse any part of the input.
  1055. *
  1056. * - |result === null|
  1057. * - |pos === 0|
  1058. * - |rightmostFailuresExpected| contains at least one failure
  1059. *
  1060. * All code following this comment (including called functions) must
  1061. * handle these states.
  1062. */
  1063. if (result === null || pos !== input.length) {
  1064. var offset = Math.max(pos, rightmostFailuresPos);
  1065. var found = offset < input.length ? input.charAt(offset) : null;
  1066. var errorPosition = computeErrorPosition();
  1067. throw new this.SyntaxError(
  1068. cleanupExpected(rightmostFailuresExpected),
  1069. found,
  1070. offset,
  1071. errorPosition.line,
  1072. errorPosition.column
  1073. );
  1074. }
  1075. return result;
  1076. },
  1077. /* Returns the parser source code. */
  1078. toSource: function() { return this._source; }
  1079. };
  1080. /* Thrown when a parser encounters a syntax error. */
  1081. result.SyntaxError = function(expected, found, offset, line, column) {
  1082. function buildMessage(expected, found) {
  1083. var expectedHumanized, foundHumanized;
  1084. switch (expected.length) {
  1085. case 0:
  1086. expectedHumanized = "end of input";
  1087. break;
  1088. case 1:
  1089. expectedHumanized = expected[0];
  1090. break;
  1091. default:
  1092. expectedHumanized = expected.slice(0, expected.length - 1).join(", ")
  1093. + " or "
  1094. + expected[expected.length - 1];
  1095. }
  1096. foundHumanized = found ? quote(found) : "end of input";
  1097. return "Expected " + expectedHumanized + " but " + foundHumanized + " found.";
  1098. }
  1099. this.name = "SyntaxError";
  1100. this.expected = expected;
  1101. this.found = found;
  1102. this.message = buildMessage(expected, found);
  1103. this.offset = offset;
  1104. this.line = line;
  1105. this.column = column;
  1106. };
  1107. result.SyntaxError.prototype = Error.prototype;
  1108. return result;
  1109. })();