Skip to content

Instantly share code, notes, and snippets.

@mstefaniuk
Created October 12, 2011 13:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mstefaniuk/1281239 to your computer and use it in GitHub Desktop.
Save mstefaniuk/1281239 to your computer and use it in GitHub Desktop.
Prividing expectations in PEG.js generated parser (last lines)
/*
* The parser is now in one of the following three states:
*
* 1. The parser successfully parsed the whole input.
*
* - |result !== null|
* - |pos === input.length|
* - |rightmostMatchFailuresExpected| may or may not contain something
*
* 2. The parser successfully parsed only a part of the input.
*
* - |result !== null|
* - |pos < input.length|
* - |rightmostMatchFailuresExpected| may or may not contain something
*
* 3. The parser did not successfully parse any part of the input.
*
* - |result === null|
* - |pos === 0|
* - |rightmostMatchFailuresExpected| contains at least one failure
*
* All code following this comment (including called functions) must
* handle these states.
*/
if (result === null || pos !== input.length) {
var errorPosition = computeErrorPosition();
throw new this.SyntaxError(
buildErrorMessage(),
errorPosition.line,
errorPosition.column,
rightmostMatchFailuresPos,
rightmostMatchFailuresExpected
);
}
return result;
},
/* Returns the parser source code. */
toSource: function() { return this._source; }
};
/* Thrown when a parser encounters a syntax error. */
result.SyntaxError = function(message, line, column, position, expectations) {
this.name = 'SyntaxError';
this.message = message;
this.line = line;
this.column = column;
this.position = position;
this.expectations = expectations;
};
result.SyntaxError.prototype = Error.prototype;
return result;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment