// from https://gist.github.com/samualtnorman/af7c9d247a44de91f4ac52b09c721d4b import { TokenTag } from "./TokenTag.ts" export type Token = { tag: TokenTag, index: number, size: number } export function tokenise(source: string): Token[] { const tokens: Token[] = [] const maybeEatDigit = () => maybeEat("0") || maybeEat("1") || maybeEat("2") || maybeEat("3") || maybeEat("4") || maybeEat("5") || maybeEat("6") || maybeEat("7") || maybeEat("8") || maybeEat("9") let index = 0 function unexpectedCharacter(): never { throw SyntaxError(`Unexpected character ${JSON.stringify(source[index])} at index ${index}`) } type MyGenerator = Generator let generator: MyGenerator | undefined = parseValue(undefined) let result while (result = generator!.next(), generator = result.done ? result.value : parseValue(generator)); return tokens function* parseValue(value: T): Generator { skipWhitespace() const tokenIndex = index if (maybeEat("{")) { tokens.push({ tag: TokenTag.OpenSquiglyBracket, index: tokenIndex, size: 1 }) skipWhitespace() const closeSquiglyBracketTokenIndex = index if (maybeEat(`}`)) tokens.push({ tag: TokenTag.CloseSquiglyBracket, index: closeSquiglyBracketTokenIndex, size: 1 }) else { while (index < source.length) { const stringToken = maybeParseString() if (!stringToken) unexpectedCharacter() stringToken.tag = TokenTag.Key tokens.push(stringToken) skipWhitespace() const colonIndex = index if (!maybeEat(":")) unexpectedCharacter() tokens.push({ tag: TokenTag.Colon, index: colonIndex, size: 1 }) yield const commaTokenIndex = index if (!maybeEat(",")) break tokens.push({ tag: TokenTag.Comma, index: commaTokenIndex, size: 1 }) skipWhitespace() } const closeSquiglyBracketTokenIndex = index if (!maybeEat("}")) unexpectedCharacter() tokens.push({ tag: TokenTag.CloseSquiglyBracket, index: closeSquiglyBracketTokenIndex, size: 1 }) } } else if (maybeEat("[")) { tokens.push({ tag: TokenTag.OpenSquareBracket, index: tokenIndex, size: 1 }) skipWhitespace() const closeSquareBracketTokenIndex = index if (maybeEat("]")) tokens.push({ tag: TokenTag.CloseSquareBracket, index: closeSquareBracketTokenIndex, size: 1 }) else { while (index < source.length) { yield const commaTokenIndex = index if (!maybeEat(",")) break tokens.push({ tag: TokenTag.Comma, index: commaTokenIndex, size: 1 }) } const closeSquareBracketTokenIndex = index if (!maybeEat("]")) unexpectedCharacter() tokens.push({ tag: TokenTag.CloseSquareBracket, index: closeSquareBracketTokenIndex, size: 1 }) } } else tokens.push(maybeParseString() || maybeParseKeyword() || maybeParseNumber()) skipWhitespace() return value } function maybeParseNumber(): Token { const tokenIndex = index maybeEat("-") if (!maybeEat("0")) { if (!(maybeEat("1") || maybeEat("2") || maybeEat("3") || maybeEat("4") || maybeEat("5") || maybeEat("6") || maybeEat("7") || maybeEat("8") || maybeEat("9") )) unexpectedCharacter() while (index < source.length && maybeEatDigit()); } if (maybeEat(".")) { if (!maybeEatDigit()) unexpectedCharacter() while (index < source.length && maybeEatDigit()); } if (maybeEat("e") || maybeEat("E")) { maybeEat("-") || maybeEat("+") if (!maybeEatDigit()) unexpectedCharacter() while (index < source.length && maybeEatDigit()); } return { tag: TokenTag.Number, index: tokenIndex, size: index - tokenIndex } } function maybeParseString(): Token | undefined { const tokenIndex = index if (maybeEat(`"`)) { while (index < source.length) { if (source[index]! < "\x20") unexpectedCharacter() if (maybeEat(`"`)) return { tag: TokenTag.String, index: tokenIndex, size: index - tokenIndex } if (maybeEat("\\")) { if (maybeEat("u")) { for (let count = 4; count--;) { if (maybeEat("0") || maybeEat("1") || maybeEat("2") || maybeEat("3") || maybeEat("4") || maybeEat("5") || maybeEat("6") || maybeEat("7") || maybeEat("8") || maybeEat("9") || maybeEat("a") || maybeEat("b") || maybeEat("c") || maybeEat("d") || maybeEat("e") || maybeEat("f") || maybeEat("A") || maybeEat("B") || maybeEat("C") || maybeEat("D") || maybeEat("E") || maybeEat("F") ) continue unexpectedCharacter() } continue } else if (maybeEat(`"`) || maybeEat("\\") || maybeEat("/") || maybeEat("b") || maybeEat("f") || maybeEat("n") || maybeEat("r") || maybeEat("t") ) continue unexpectedCharacter() } index++ } } } function maybeParseKeyword(): Token | undefined { const tokenIndex = index if (maybeEat("true")) return { tag: TokenTag.True, index: tokenIndex, size: index - tokenIndex } if (maybeEat("false")) return { tag: TokenTag.False, index: tokenIndex, size: index - tokenIndex } if (maybeEat("null")) return { tag: TokenTag.Null, index: tokenIndex, size: index - tokenIndex } } function skipWhitespace() { while (index < source.length && (maybeEat(" ") || maybeEat("\n") || maybeEat("\r") || maybeEat("\t"))); } function maybeEat(search: string): boolean { if (source.startsWith(search, index)) { index += search.length return true } return false } } export function tokensToJson(source: string, tokens: Iterable): string { let json = `` for (const token of tokens) json += source.slice(token.index, token.index + token.size) return json }