Skip to main content
Module

x/ohm_js/src/Failure.js

A library and language for building parsers, interpreters, compilers, etc.
Go to Latest
File
'use strict';
// --------------------------------------------------------------------// Private stuff// --------------------------------------------------------------------
/* `Failure`s represent expressions that weren't matched while parsing. They are used to generate error messages automatically. The interface of `Failure`s includes the collowing methods:
- getText() : String - getType() : String (one of {"description", "string", "code"}) - isDescription() : bool - isStringTerminal() : bool - isCode() : bool - isFluffy() : bool - makeFluffy() : void - subsumes(Failure) : bool*/
function isValidType(type) { return type === 'description' || type === 'string' || type === 'code';}
function Failure(pexpr, text, type) { if (!isValidType(type)) { throw new Error('invalid Failure type: ' + type); } this.pexpr = pexpr; this.text = text; this.type = type; this.fluffy = false;}
Failure.prototype.getPExpr = function() { return this.pexpr;};
Failure.prototype.getText = function() { return this.text;};
Failure.prototype.getType = function() { return this.type;};
Failure.prototype.isDescription = function() { return this.type === 'description';};
Failure.prototype.isStringTerminal = function() { return this.type === 'string';};
Failure.prototype.isCode = function() { return this.type === 'code';};
Failure.prototype.isFluffy = function() { return this.fluffy;};
Failure.prototype.makeFluffy = function() { this.fluffy = true;};
Failure.prototype.clearFluffy = function() { this.fluffy = false;};
Failure.prototype.subsumes = function(that) { return ( this.getText() === that.getText() && this.type === that.type && (!this.isFluffy() || (this.isFluffy() && that.isFluffy())) );};
Failure.prototype.toString = function() { return this.type === 'string' ? JSON.stringify(this.getText()) : this.getText();};
Failure.prototype.clone = function() { const failure = new Failure(this.pexpr, this.text, this.type); if (this.isFluffy()) { failure.makeFluffy(); } return failure;};
Failure.prototype.toKey = function() { return this.toString() + '#' + this.type;};
// --------------------------------------------------------------------// Exports// --------------------------------------------------------------------
module.exports = Failure;