Created
October 12, 2011 13:37
-
-
Save mstefaniuk/1281239 to your computer and use it in GitHub Desktop.
Prividing expectations in PEG.js generated parser (last lines)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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