Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/samualtnorman/decurse-transform. transforming recursive functions to non-recursive functions
Something went wrong. Try again.
decurse-transform json-parser.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219// from https://gist.github.com/samualtnorman/af7c9d247a44de91f4ac52b09c721d4bimport { 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<void, MyGenerator | undefined, never>
let generator: MyGenerator | undefined = parseValue(undefined) let result
while (result = generator!.next(), generator = result.done ? result.value : parseValue(generator));
return tokens
function* parseValue<T>(value: T): Generator<void, T, never> { 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<Token>): string { let json = ``
for (const token of tokens) json += source.slice(token.index, token.index + token.size)
return json}