Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add PEG.js Lisp parser based on our example grammar; runs at similar …

…speed to our parser.
  • Loading branch information...
commit 6a67b43dc047e5bd550f8909c504c625f8dcd6a7 1 parent e02cf5b
James Coglan authored

Showing 2 changed files with 861 additions and 0 deletions. Show diff stats Hide diff stats

  1. +5 0 examples/lisp.js
  2. +856 0 examples/pegjs/lisp.js
5 examples/lisp.js
... ... @@ -1,6 +1,7 @@
1 1 load('vendor/js.class/build/min/core.js');
2 2 load('build/stake-min.js');
3 3 load('examples/benchmark.js');
  4 +load('examples/pegjs/lisp.js');
4 5
5 6 grammar = 'grammar CompiledLisp \
6 7 \
@@ -29,3 +30,7 @@ benchmark('Combinator parser', 20, function() {
29 30 CombinatorLispParser.parse(program);
30 31 });
31 32
  33 +benchmark('PEG.js parser', 20, function() {
  34 + LispParser.parse(program);
  35 +});
  36 +
856 examples/pegjs/lisp.js
... ... @@ -0,0 +1,856 @@
  1 +LispParser = (function(){
  2 + /* Generated by PEG.js (http://pegjs.majda.cz/). */
  3 +
  4 + var result = {
  5 + _startRule: "start",
  6 +
  7 + _quoteString: function(s) {
  8 + /*
  9 + * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
  10 + * literal except for the closing quote character, backslash, carriage
  11 + * return, line separator, paragraph separator, and line feed. Any character
  12 + * may appear in the form of an escape sequence.
  13 + */
  14 + return '"' + s
  15 + .replace(/\\/g, '\\\\') // backslash
  16 + .replace(/"/g, '\\"') // closing quote character
  17 + .replace(/\r/g, '\\r') // carriage return
  18 + .replace(/\u2028/g, '\\u2028') // line separator
  19 + .replace(/\u2029/g, '\\u2029') // paragraph separator
  20 + .replace(/\n/g, '\\n') // line feed
  21 + + '"';
  22 + },
  23 +
  24 + _arrayContains: function(array, value) {
  25 + /*
  26 + * Stupid IE does not have Array.prototype.indexOf, otherwise this function
  27 + * would be a one-liner.
  28 + */
  29 + var length = array.length;
  30 + for (var i = 0; i < length; i++) {
  31 + if (array[i] === value) {
  32 + return true;
  33 + }
  34 + }
  35 + return false;
  36 + },
  37 +
  38 + _matchFailed: function(failure) {
  39 + if (this._pos > this._rightmostMatchFailuresPos) {
  40 + this._rightmostMatchFailuresPos = this._pos;
  41 + this._rightmostMatchFailuresExpected = [];
  42 + }
  43 +
  44 + if (!this._arrayContains(this._rightmostMatchFailuresExpected, failure)) {
  45 + this._rightmostMatchFailuresExpected.push(failure);
  46 + }
  47 + },
  48 +
  49 + _parse_start: function(context) {
  50 + var cacheKey = "start" + '@' + this._pos;
  51 + var cachedResult = this._cache[cacheKey];
  52 + if (cachedResult !== undefined) {
  53 + this._pos = cachedResult.nextPos;
  54 + return cachedResult.result;
  55 + }
  56 +
  57 + var pos = this._pos;
  58 +
  59 +
  60 + var result1 = this._parse_cell(context);
  61 + if (result1 !== null) {
  62 + var result0 = [];
  63 + while (result1 !== null) {
  64 + result0.push(result1);
  65 + var result1 = this._parse_cell(context);
  66 + }
  67 + } else {
  68 + var result0 = null;
  69 + }
  70 +
  71 +
  72 +
  73 + this._cache[cacheKey] = {
  74 + nextPos: this._pos,
  75 + result: result0
  76 + };
  77 + return result0;
  78 + },
  79 +
  80 + _parse_cell: function(context) {
  81 + var cacheKey = "cell" + '@' + this._pos;
  82 + var cachedResult = this._cache[cacheKey];
  83 + if (cachedResult !== undefined) {
  84 + this._pos = cachedResult.nextPos;
  85 + return cachedResult.result;
  86 + }
  87 +
  88 + var pos = this._pos;
  89 +
  90 +
  91 + var savedPos0 = this._pos;
  92 + var result3 = [];
  93 + var result9 = this._parse_space(context);
  94 + while (result9 !== null) {
  95 + result3.push(result9);
  96 + var result9 = this._parse_space(context);
  97 + }
  98 + if (result3 !== null) {
  99 + var result8 = this._parse_list(context);
  100 + if (result8 !== null) {
  101 + var result4 = result8;
  102 + } else {
  103 + var result7 = this._parse_atom(context);
  104 + if (result7 !== null) {
  105 + var result4 = result7;
  106 + } else {
  107 + var result4 = null;;
  108 + };
  109 + }
  110 + if (result4 !== null) {
  111 + var result5 = [];
  112 + var result6 = this._parse_space(context);
  113 + while (result6 !== null) {
  114 + result5.push(result6);
  115 + var result6 = this._parse_space(context);
  116 + }
  117 + if (result5 !== null) {
  118 + var result2 = [result3, result4, result5];
  119 + } else {
  120 + var result2 = null;
  121 + this._pos = savedPos0;
  122 + }
  123 + } else {
  124 + var result2 = null;
  125 + this._pos = savedPos0;
  126 + }
  127 + } else {
  128 + var result2 = null;
  129 + this._pos = savedPos0;
  130 + }
  131 +
  132 +
  133 +
  134 + this._cache[cacheKey] = {
  135 + nextPos: this._pos,
  136 + result: result2
  137 + };
  138 + return result2;
  139 + },
  140 +
  141 + _parse_list: function(context) {
  142 + var cacheKey = "list" + '@' + this._pos;
  143 + var cachedResult = this._cache[cacheKey];
  144 + if (cachedResult !== undefined) {
  145 + this._pos = cachedResult.nextPos;
  146 + return cachedResult.result;
  147 + }
  148 +
  149 + var pos = this._pos;
  150 +
  151 +
  152 + var savedPos1 = this._pos;
  153 + if (this._input.substr(this._pos, 1) === "(") {
  154 + var result11 = "(";
  155 + this._pos += 1;
  156 + } else {
  157 + var result11 = null;
  158 + if (context.reportMatchFailures) {
  159 + this._matchFailed(this._quoteString("("));
  160 + }
  161 + }
  162 + if (result11 !== null) {
  163 + var result14 = this._parse_cell(context);
  164 + if (result14 !== null) {
  165 + var result12 = [];
  166 + while (result14 !== null) {
  167 + result12.push(result14);
  168 + var result14 = this._parse_cell(context);
  169 + }
  170 + } else {
  171 + var result12 = null;
  172 + }
  173 + if (result12 !== null) {
  174 + if (this._input.substr(this._pos, 1) === ")") {
  175 + var result13 = ")";
  176 + this._pos += 1;
  177 + } else {
  178 + var result13 = null;
  179 + if (context.reportMatchFailures) {
  180 + this._matchFailed(this._quoteString(")"));
  181 + }
  182 + }
  183 + if (result13 !== null) {
  184 + var result10 = [result11, result12, result13];
  185 + } else {
  186 + var result10 = null;
  187 + this._pos = savedPos1;
  188 + }
  189 + } else {
  190 + var result10 = null;
  191 + this._pos = savedPos1;
  192 + }
  193 + } else {
  194 + var result10 = null;
  195 + this._pos = savedPos1;
  196 + }
  197 +
  198 +
  199 +
  200 + this._cache[cacheKey] = {
  201 + nextPos: this._pos,
  202 + result: result10
  203 + };
  204 + return result10;
  205 + },
  206 +
  207 + _parse_atom: function(context) {
  208 + var cacheKey = "atom" + '@' + this._pos;
  209 + var cachedResult = this._cache[cacheKey];
  210 + if (cachedResult !== undefined) {
  211 + this._pos = cachedResult.nextPos;
  212 + return cachedResult.result;
  213 + }
  214 +
  215 + var pos = this._pos;
  216 +
  217 +
  218 + var result19 = this._parse_boolean(context);
  219 + if (result19 !== null) {
  220 + var result15 = result19;
  221 + } else {
  222 + var result18 = this._parse_integer(context);
  223 + if (result18 !== null) {
  224 + var result15 = result18;
  225 + } else {
  226 + var result17 = this._parse_string(context);
  227 + if (result17 !== null) {
  228 + var result15 = result17;
  229 + } else {
  230 + var result16 = this._parse_symbol(context);
  231 + if (result16 !== null) {
  232 + var result15 = result16;
  233 + } else {
  234 + var result15 = null;;
  235 + };
  236 + };
  237 + };
  238 + }
  239 +
  240 +
  241 +
  242 + this._cache[cacheKey] = {
  243 + nextPos: this._pos,
  244 + result: result15
  245 + };
  246 + return result15;
  247 + },
  248 +
  249 + _parse_boolean: function(context) {
  250 + var cacheKey = "boolean" + '@' + this._pos;
  251 + var cachedResult = this._cache[cacheKey];
  252 + if (cachedResult !== undefined) {
  253 + this._pos = cachedResult.nextPos;
  254 + return cachedResult.result;
  255 + }
  256 +
  257 + var pos = this._pos;
  258 +
  259 +
  260 + if (this._input.substr(this._pos, 2) === "#t") {
  261 + var result22 = "#t";
  262 + this._pos += 2;
  263 + } else {
  264 + var result22 = null;
  265 + if (context.reportMatchFailures) {
  266 + this._matchFailed(this._quoteString("#t"));
  267 + }
  268 + }
  269 + if (result22 !== null) {
  270 + var result20 = result22;
  271 + } else {
  272 + if (this._input.substr(this._pos, 2) === "#f") {
  273 + var result21 = "#f";
  274 + this._pos += 2;
  275 + } else {
  276 + var result21 = null;
  277 + if (context.reportMatchFailures) {
  278 + this._matchFailed(this._quoteString("#f"));
  279 + }
  280 + }
  281 + if (result21 !== null) {
  282 + var result20 = result21;
  283 + } else {
  284 + var result20 = null;;
  285 + };
  286 + }
  287 +
  288 +
  289 +
  290 + this._cache[cacheKey] = {
  291 + nextPos: this._pos,
  292 + result: result20
  293 + };
  294 + return result20;
  295 + },
  296 +
  297 + _parse_integer: function(context) {
  298 + var cacheKey = "integer" + '@' + this._pos;
  299 + var cachedResult = this._cache[cacheKey];
  300 + if (cachedResult !== undefined) {
  301 + this._pos = cachedResult.nextPos;
  302 + return cachedResult.result;
  303 + }
  304 +
  305 + var pos = this._pos;
  306 +
  307 +
  308 + var savedPos2 = this._pos;
  309 + if (this._input.substr(this._pos).match(/^[1-9]/) !== null) {
  310 + var result24 = this._input.charAt(this._pos);
  311 + this._pos++;
  312 + } else {
  313 + var result24 = null;
  314 + if (context.reportMatchFailures) {
  315 + this._matchFailed('[' + "1-9" + ']');
  316 + }
  317 + }
  318 + if (result24 !== null) {
  319 + var result25 = [];
  320 + if (this._input.substr(this._pos).match(/^[0-9]/) !== null) {
  321 + var result26 = this._input.charAt(this._pos);
  322 + this._pos++;
  323 + } else {
  324 + var result26 = null;
  325 + if (context.reportMatchFailures) {
  326 + this._matchFailed('[' + "0-9" + ']');
  327 + }
  328 + }
  329 + while (result26 !== null) {
  330 + result25.push(result26);
  331 + if (this._input.substr(this._pos).match(/^[0-9]/) !== null) {
  332 + var result26 = this._input.charAt(this._pos);
  333 + this._pos++;
  334 + } else {
  335 + var result26 = null;
  336 + if (context.reportMatchFailures) {
  337 + this._matchFailed('[' + "0-9" + ']');
  338 + }
  339 + }
  340 + }
  341 + if (result25 !== null) {
  342 + var result23 = [result24, result25];
  343 + } else {
  344 + var result23 = null;
  345 + this._pos = savedPos2;
  346 + }
  347 + } else {
  348 + var result23 = null;
  349 + this._pos = savedPos2;
  350 + }
  351 +
  352 +
  353 +
  354 + this._cache[cacheKey] = {
  355 + nextPos: this._pos,
  356 + result: result23
  357 + };
  358 + return result23;
  359 + },
  360 +
  361 + _parse_string: function(context) {
  362 + var cacheKey = "string" + '@' + this._pos;
  363 + var cachedResult = this._cache[cacheKey];
  364 + if (cachedResult !== undefined) {
  365 + this._pos = cachedResult.nextPos;
  366 + return cachedResult.result;
  367 + }
  368 +
  369 + var pos = this._pos;
  370 +
  371 +
  372 + var savedPos3 = this._pos;
  373 + if (this._input.substr(this._pos, 1) === "\"") {
  374 + var result28 = "\"";
  375 + this._pos += 1;
  376 + } else {
  377 + var result28 = null;
  378 + if (context.reportMatchFailures) {
  379 + this._matchFailed(this._quoteString("\""));
  380 + }
  381 + }
  382 + if (result28 !== null) {
  383 + var result29 = [];
  384 + var savedPos4 = this._pos;
  385 + if (this._input.substr(this._pos, 1) === "\\") {
  386 + var result34 = "\\";
  387 + this._pos += 1;
  388 + } else {
  389 + var result34 = null;
  390 + if (context.reportMatchFailures) {
  391 + this._matchFailed(this._quoteString("\\"));
  392 + }
  393 + }
  394 + if (result34 !== null) {
  395 + if (this._input.length > this._pos) {
  396 + var result35 = this._input.charAt(this._pos);
  397 + this._pos++;
  398 + } else {
  399 + var result35 = null;
  400 + if (context.reportMatchFailures) {
  401 + this._matchFailed('any character');
  402 + }
  403 + }
  404 + if (result35 !== null) {
  405 + var result33 = [result34, result35];
  406 + } else {
  407 + var result33 = null;
  408 + this._pos = savedPos4;
  409 + }
  410 + } else {
  411 + var result33 = null;
  412 + this._pos = savedPos4;
  413 + }
  414 + if (result33 !== null) {
  415 + var result31 = result33;
  416 + } else {
  417 + if (this._input.substr(this._pos).match(/^[^"]/) !== null) {
  418 + var result32 = this._input.charAt(this._pos);
  419 + this._pos++;
  420 + } else {
  421 + var result32 = null;
  422 + if (context.reportMatchFailures) {
  423 + this._matchFailed('[' + "^\"" + ']');
  424 + }
  425 + }
  426 + if (result32 !== null) {
  427 + var result31 = result32;
  428 + } else {
  429 + var result31 = null;;
  430 + };
  431 + }
  432 + while (result31 !== null) {
  433 + result29.push(result31);
  434 + var savedPos4 = this._pos;
  435 + if (this._input.substr(this._pos, 1) === "\\") {
  436 + var result34 = "\\";
  437 + this._pos += 1;
  438 + } else {
  439 + var result34 = null;
  440 + if (context.reportMatchFailures) {
  441 + this._matchFailed(this._quoteString("\\"));
  442 + }
  443 + }
  444 + if (result34 !== null) {
  445 + if (this._input.length > this._pos) {
  446 + var result35 = this._input.charAt(this._pos);
  447 + this._pos++;
  448 + } else {
  449 + var result35 = null;
  450 + if (context.reportMatchFailures) {
  451 + this._matchFailed('any character');
  452 + }
  453 + }
  454 + if (result35 !== null) {
  455 + var result33 = [result34, result35];
  456 + } else {
  457 + var result33 = null;
  458 + this._pos = savedPos4;
  459 + }
  460 + } else {
  461 + var result33 = null;
  462 + this._pos = savedPos4;
  463 + }
  464 + if (result33 !== null) {
  465 + var result31 = result33;
  466 + } else {
  467 + if (this._input.substr(this._pos).match(/^[^"]/) !== null) {
  468 + var result32 = this._input.charAt(this._pos);
  469 + this._pos++;
  470 + } else {
  471 + var result32 = null;
  472 + if (context.reportMatchFailures) {
  473 + this._matchFailed('[' + "^\"" + ']');
  474 + }
  475 + }
  476 + if (result32 !== null) {
  477 + var result31 = result32;
  478 + } else {
  479 + var result31 = null;;
  480 + };
  481 + }
  482 + }
  483 + if (result29 !== null) {
  484 + if (this._input.substr(this._pos, 1) === "\"") {
  485 + var result30 = "\"";
  486 + this._pos += 1;
  487 + } else {
  488 + var result30 = null;
  489 + if (context.reportMatchFailures) {
  490 + this._matchFailed(this._quoteString("\""));
  491 + }
  492 + }
  493 + if (result30 !== null) {
  494 + var result27 = [result28, result29, result30];
  495 + } else {
  496 + var result27 = null;
  497 + this._pos = savedPos3;
  498 + }
  499 + } else {
  500 + var result27 = null;
  501 + this._pos = savedPos3;
  502 + }
  503 + } else {
  504 + var result27 = null;
  505 + this._pos = savedPos3;
  506 + }
  507 +
  508 +
  509 +
  510 + this._cache[cacheKey] = {
  511 + nextPos: this._pos,
  512 + result: result27
  513 + };
  514 + return result27;
  515 + },
  516 +
  517 + _parse_symbol: function(context) {
  518 + var cacheKey = "symbol" + '@' + this._pos;
  519 + var cachedResult = this._cache[cacheKey];
  520 + if (cachedResult !== undefined) {
  521 + this._pos = cachedResult.nextPos;
  522 + return cachedResult.result;
  523 + }
  524 +
  525 + var pos = this._pos;
  526 +
  527 +
  528 + var savedPos5 = this._pos;
  529 + var savedPos6 = this._pos;
  530 + var savedReportMatchFailuresVar0 = context.reportMatchFailures;
  531 + context.reportMatchFailures = false;
  532 + var result40 = this._parse_delimiter(context);
  533 + context.reportMatchFailures = savedReportMatchFailuresVar0;
  534 + if (result40 === null) {
  535 + var result38 = '';
  536 + } else {
  537 + var result38 = null;
  538 + this._pos = savedPos6;
  539 + }
  540 + if (result38 !== null) {
  541 + if (this._input.length > this._pos) {
  542 + var result39 = this._input.charAt(this._pos);
  543 + this._pos++;
  544 + } else {
  545 + var result39 = null;
  546 + if (context.reportMatchFailures) {
  547 + this._matchFailed('any character');
  548 + }
  549 + }
  550 + if (result39 !== null) {
  551 + var result37 = [result38, result39];
  552 + } else {
  553 + var result37 = null;
  554 + this._pos = savedPos5;
  555 + }
  556 + } else {
  557 + var result37 = null;
  558 + this._pos = savedPos5;
  559 + }
  560 + if (result37 !== null) {
  561 + var result36 = [];
  562 + while (result37 !== null) {
  563 + result36.push(result37);
  564 + var savedPos5 = this._pos;
  565 + var savedPos6 = this._pos;
  566 + var savedReportMatchFailuresVar0 = context.reportMatchFailures;
  567 + context.reportMatchFailures = false;
  568 + var result40 = this._parse_delimiter(context);
  569 + context.reportMatchFailures = savedReportMatchFailuresVar0;
  570 + if (result40 === null) {
  571 + var result38 = '';
  572 + } else {
  573 + var result38 = null;
  574 + this._pos = savedPos6;
  575 + }
  576 + if (result38 !== null) {
  577 + if (this._input.length > this._pos) {
  578 + var result39 = this._input.charAt(this._pos);
  579 + this._pos++;
  580 + } else {
  581 + var result39 = null;
  582 + if (context.reportMatchFailures) {
  583 + this._matchFailed('any character');
  584 + }
  585 + }
  586 + if (result39 !== null) {
  587 + var result37 = [result38, result39];
  588 + } else {
  589 + var result37 = null;
  590 + this._pos = savedPos5;
  591 + }
  592 + } else {
  593 + var result37 = null;
  594 + this._pos = savedPos5;
  595 + }
  596 + }
  597 + } else {
  598 + var result36 = null;
  599 + }
  600 +
  601 +
  602 +
  603 + this._cache[cacheKey] = {
  604 + nextPos: this._pos,
  605 + result: result36
  606 + };
  607 + return result36;
  608 + },
  609 +
  610 + _parse_space: function(context) {
  611 + var cacheKey = "space" + '@' + this._pos;
  612 + var cachedResult = this._cache[cacheKey];
  613 + if (cachedResult !== undefined) {
  614 + this._pos = cachedResult.nextPos;
  615 + return cachedResult.result;
  616 + }
  617 +
  618 + var pos = this._pos;
  619 +
  620 +
  621 + if (this._input.substr(this._pos).match(/^[\s\n\r\t]/) !== null) {
  622 + var result41 = this._input.charAt(this._pos);
  623 + this._pos++;
  624 + } else {
  625 + var result41 = null;
  626 + if (context.reportMatchFailures) {
  627 + this._matchFailed('[' + "\\s\\n\\r\\t" + ']');
  628 + }
  629 + }
  630 +
  631 +
  632 +
  633 + this._cache[cacheKey] = {
  634 + nextPos: this._pos,
  635 + result: result41
  636 + };
  637 + return result41;
  638 + },
  639 +
  640 + _parse_paren: function(context) {
  641 + var cacheKey = "paren" + '@' + this._pos;
  642 + var cachedResult = this._cache[cacheKey];
  643 + if (cachedResult !== undefined) {
  644 + this._pos = cachedResult.nextPos;
  645 + return cachedResult.result;
  646 + }
  647 +
  648 + var pos = this._pos;
  649 +
  650 +
  651 + if (this._input.substr(this._pos, 1) === "(") {
  652 + var result44 = "(";
  653 + this._pos += 1;
  654 + } else {
  655 + var result44 = null;
  656 + if (context.reportMatchFailures) {
  657 + this._matchFailed(this._quoteString("("));
  658 + }
  659 + }
  660 + if (result44 !== null) {
  661 + var result42 = result44;
  662 + } else {
  663 + if (this._input.substr(this._pos, 1) === ")") {
  664 + var result43 = ")";
  665 + this._pos += 1;
  666 + } else {
  667 + var result43 = null;
  668 + if (context.reportMatchFailures) {
  669 + this._matchFailed(this._quoteString(")"));
  670 + }
  671 + }
  672 + if (result43 !== null) {
  673 + var result42 = result43;
  674 + } else {
  675 + var result42 = null;;
  676 + };
  677 + }
  678 +
  679 +
  680 +
  681 + this._cache[cacheKey] = {
  682 + nextPos: this._pos,
  683 + result: result42
  684 + };
  685 + return result42;
  686 + },
  687 +
  688 + _parse_delimiter: function(context) {
  689 + var cacheKey = "delimiter" + '@' + this._pos;
  690 + var cachedResult = this._cache[cacheKey];
  691 + if (cachedResult !== undefined) {
  692 + this._pos = cachedResult.nextPos;
  693 + return cachedResult.result;
  694 + }
  695 +
  696 + var pos = this._pos;
  697 +
  698 +
  699 + var result47 = this._parse_paren(context);
  700 + if (result47 !== null) {
  701 + var result45 = result47;
  702 + } else {
  703 + var result46 = this._parse_space(context);
  704 + if (result46 !== null) {
  705 + var result45 = result46;
  706 + } else {
  707 + var result45 = null;;
  708 + };
  709 + }
  710 +
  711 +
  712 +
  713 + this._cache[cacheKey] = {
  714 + nextPos: this._pos,
  715 + result: result45
  716 + };
  717 + return result45;
  718 + },
  719 +
  720 + /*
  721 + * Parses the input with a generated parser. If the parsing is successfull,
  722 + * returns a value explicitly or implicitly specified by the grammar from
  723 + * which the parser was generated (see |PEG.buildParser|). If the parsing is
  724 + * unsuccessful, throws |PEG.grammarParser.SyntaxError| describing the error.
  725 + */
  726 + parse: function(input) {
  727 + var that = this;
  728 +
  729 + function initialize() {
  730 + that._input = input;
  731 + that._pos = 0;
  732 + that._rightmostMatchFailuresPos = 0;
  733 + that._rightmostMatchFailuresExpected = [];
  734 + that._cache = {};
  735 + }
  736 +
  737 + function buildErrorMessage() {
  738 + function buildExpected(failuresExpected) {
  739 + switch (failuresExpected.length) {
  740 + case 0:
  741 + return 'end of input';
  742 + case 1:
  743 + return failuresExpected[0];
  744 + default:
  745 + failuresExpected.sort();
  746 + return failuresExpected.slice(0, failuresExpected.length - 1).join(', ')
  747 + + ' or '
  748 + + failuresExpected[failuresExpected.length - 1];
  749 + }
  750 + }
  751 +
  752 + var expected = buildExpected(that._rightmostMatchFailuresExpected);
  753 + var pos = Math.max(that._pos, that._rightmostMatchFailuresPos);
  754 + var actual = pos < that._input.length
  755 + ? that._quoteString(that._input.charAt(pos))
  756 + : 'end of input';
  757 +
  758 + return 'Expected ' + expected + ' but ' + actual + ' found.';
  759 + }
  760 +
  761 + function computeErrorPosition() {
  762 + /*
  763 + * The first idea was to use |String.split| to break the input up to the
  764 + * error position along newlines and derive the line and column from
  765 + * there. However IE's |split| implementation is so broken that it was
  766 + * enough to prevent it.
  767 + */
  768 +
  769 + var input = that._input;
  770 + var pos = that._rightmostMatchFailuresPos;
  771 + var line = 1;
  772 + var column = 1;
  773 + var seenCR = false;
  774 +
  775 + for (var i = 0; i < pos; i++) {
  776 + var ch = input.charAt(i);
  777 + if (ch === '\n') {
  778 + if (!seenCR) { line++; }
  779 + column = 1;
  780 + seenCR = false;
  781 + } else if (ch === '\r' | ch === '\u2028' || ch === '\u2029') {
  782 + line++;
  783 + column = 1;
  784 + seenCR = true;
  785 + } else {
  786 + column++;
  787 + seenCR = false;
  788 + }
  789 + }
  790 +
  791 + return { line: line, column: column };
  792 + }
  793 +
  794 + initialize();
  795 +
  796 + var initialContext = {
  797 + reportMatchFailures: true
  798 + };
  799 +
  800 + var result = this['_parse_' + this._startRule](initialContext);
  801 +
  802 + /*
  803 + * The parser is now in one of the following three states:
  804 + *
  805 + * 1. The parser successfully parsed the whole input.
  806 + *
  807 + * - |result !== null|
  808 + * - |that._pos === input.length|
  809 + * - |that._rightmostMatchFailuresExpected.length| may or may not contain
  810 + * something
  811 + *
  812 + * 2. The parser successfully parsed only a part of the input.
  813 + *
  814 + * - |result !== null|
  815 + * - |that._pos < input.length|
  816 + * - |that._rightmostMatchFailuresExpected.length| may or may not contain
  817 + * something
  818 + *
  819 + * 3. The parser did not successfully parse any part of the input.
  820 + *
  821 + * - |result === null|
  822 + * - |that._pos === 0|
  823 + * - |that._rightmostMatchFailuresExpected.length| contains at least one failure
  824 + *
  825 + * All code following this comment (including called functions) must
  826 + * handle these states.
  827 + */
  828 + if (result === null || this._pos !== input.length) {
  829 + var errorPosition = computeErrorPosition();
  830 + throw new this.SyntaxError(
  831 + buildErrorMessage(),
  832 + errorPosition.line,
  833 + errorPosition.column
  834 + );
  835 + }
  836 +
  837 + return result;
  838 + },
  839 +
  840 + /* Returns the parser source code. */
  841 + toSource: function() { return this._source; }
  842 + };
  843 +
  844 + /* Thrown when a parser encounters a syntax error. */
  845 +
  846 + result.SyntaxError = function(message, line, column) {
  847 + this.name = 'SyntaxError';
  848 + this.message = message;
  849 + this.line = line;
  850 + this.column = column;
  851 + };
  852 +
  853 + result.SyntaxError.prototype = Error.prototype;
  854 +
  855 + return result;
  856 +})();

0 comments on commit 6a67b43

Please sign in to comment.
Something went wrong with that request. Please try again.