fracturedjsonjs
Version:
JSON formatter that produces highly readable but fairly compact output
60 lines (59 loc) • 2.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScannerState = void 0;
const FracturedJsonError_1 = require("./FracturedJsonError");
/**
* Class for keeping track of info while scanning text into JSON tokens.
*/
class ScannerState {
constructor(originalText) {
this.CurrentPosition = { Index: 0, Row: 0, Column: 0 };
this.TokenPosition = { Index: 0, Row: 0, Column: 0 };
this.NonWhitespaceSinceLastNewline = false;
this._originalText = originalText;
}
Advance(isWhitespace) {
if (this.CurrentPosition.Index >= MaxDocSize)
throw new Error("Maximum document length exceeded");
this.CurrentPosition.Index += 1;
this.CurrentPosition.Column += 1;
this.NonWhitespaceSinceLastNewline || (this.NonWhitespaceSinceLastNewline = !isWhitespace);
}
NewLine() {
if (this.CurrentPosition.Index >= MaxDocSize)
throw new Error("Maximum document length exceeded");
this.CurrentPosition.Index += 1;
this.CurrentPosition.Row += 1;
this.CurrentPosition.Column = 0;
this.NonWhitespaceSinceLastNewline = false;
}
SetTokenStart() {
this.TokenPosition = Object.assign({}, this.CurrentPosition);
}
MakeTokenFromBuffer(type, trimEnd = false) {
const substring = this._originalText.substring(this.TokenPosition.Index, this.CurrentPosition.Index);
return {
Type: type,
Text: (trimEnd) ? substring.trimEnd() : substring,
InputPosition: Object.assign({}, this.TokenPosition),
};
}
MakeToken(type, text) {
return {
Type: type,
Text: text,
InputPosition: Object.assign({}, this.TokenPosition),
};
}
Current() {
return (this.AtEnd()) ? NaN : this._originalText.charCodeAt(this.CurrentPosition.Index);
}
AtEnd() {
return this.CurrentPosition.Index >= this._originalText.length;
}
Throw(message) {
throw new FracturedJsonError_1.FracturedJsonError(message, this.CurrentPosition);
}
}
exports.ScannerState = ScannerState;
const MaxDocSize = 2000000000;