json-colorizer
Version:
A library to format JSON with colors for display in the console
41 lines (40 loc) • 1.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.tokenize = void 0;
const tokenTypes = [
{ regex: /^\s+/, tokenType: 'Whitespace' },
{ regex: /^[{}]/, tokenType: 'Brace' },
{ regex: /^[[\]]/, tokenType: 'Bracket' },
{ regex: /^:/, tokenType: 'Colon' },
{ regex: /^,/, tokenType: 'Comma' },
{ regex: /^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?/i, tokenType: 'NumberLiteral' },
{ regex: /^"(?:\\.|[^"\\])*"(?=\s*:)/, tokenType: 'StringKey' },
{ regex: /^"(?:\\.|[^"\\])*"/, tokenType: 'StringLiteral' },
{ regex: /^true|^false/, tokenType: 'BooleanLiteral' },
{ regex: /^null/, tokenType: 'NullLiteral' }
];
function tokenize(input) {
const tokens = [];
let cursor = 0;
while (cursor < input.length) {
let matched = false;
for (const tokenType of tokenTypes) {
const match = input.slice(cursor).match(tokenType.regex);
if (match) {
tokens.push({
type: tokenType.tokenType,
value: match[0]
});
cursor += match[0].length;
matched = true;
break;
}
}
if (!matched) {
throw new Error(`Unexpected character at position ${cursor}`);
}
}
return tokens;
}
exports.tokenize = tokenize;
;
;