jsonast
Version:
a json to ast parser which allows error recovery
88 lines • 2.53 kB
JavaScript
"use strict";
var CharacterStream = (function () {
function CharacterStream(text) {
this.text = text;
this._offset = 0;
this._line = 1;
this._column = 1;
}
Object.defineProperty(CharacterStream.prototype, "ch", {
get: function () {
return this.text.charAt(this._offset);
},
enumerable: true,
configurable: true
});
Object.defineProperty(CharacterStream.prototype, "eoi", {
get: function () {
return this.text.length === this._offset;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CharacterStream.prototype, "line", {
get: function () {
return this._line;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CharacterStream.prototype, "column", {
get: function () {
return this._column;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CharacterStream.prototype, "offset", {
get: function () {
return this._offset;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CharacterStream.prototype, "pos", {
get: function () {
return {
line: this.line,
column: this.column,
char: this.offset
};
},
enumerable: true,
configurable: true
});
CharacterStream.prototype.expect = function (text) {
if (this.ch !== text) {
var ch = this.ch
.replace('\n', '\\n')
.replace('\r', '\\r')
.replace('\t', '\\t');
throw new Error("Unexpected character '" + ch + "' at " + this.line + ":" + this.column + ". Expected '" + text + "'");
}
};
CharacterStream.prototype.next = function () {
if (this.ch === '\n') {
this._column = 1;
this._line++;
}
else {
this._column++;
}
this._offset++;
};
CharacterStream.prototype.accept = function (text) {
this.expect(text);
this.next();
};
CharacterStream.prototype.skip = function (text) {
if (this.ch === text) {
this.next();
return true;
}
return false;
};
return CharacterStream;
}());
exports.CharacterStream = CharacterStream;
//# sourceMappingURL=character-stream.js.map