Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/samualtnorman/solid-syntax-highlight-text-editor. Syntax highlighting text editor in Solid.js samualtnorman.github.io/solid-syntax-highlight-text-editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210// from https://gist.github.com/samualtnorman/af7c9d247a44de91f4ac52b09c721d4bexport enum TokenTag { CloseSquareBracket, CloseSquiglyBracket, Colon, Comma, False, Null, Number, OpenSquareBracket, OpenSquiglyBracket, String, True, Key}
export type Token = { tag: TokenTag, index: number, size: number }
export function* tokenise(source: string): Generator<Token, void, void> { 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
yield* parseValue()
function* parseValue(): Generator<Token, void, void> { skipWhitespace()
const tokenIndex = index
if (maybeEat("{")) { yield { tag: TokenTag.OpenSquiglyBracket, index: tokenIndex, size: 1 } skipWhitespace()
const closeSquiglyBracketTokenIndex = index
if (maybeEat(`}`)) yield { tag: TokenTag.CloseSquiglyBracket, index: closeSquiglyBracketTokenIndex, size: 1 } else { while (index < source.length) { const stringToken = maybeParseString()
if (!stringToken) throw SyntaxError(`Unexpected character "${source[index]}"`)
stringToken.tag = TokenTag.Key yield stringToken skipWhitespace()
const colonIndex = index if (!maybeEat(":")) throw SyntaxError(`Unexpected character "${source[index]}"`) yield { tag: TokenTag.Colon, index: colonIndex, size: 1 } yield* parseValue()
const commaTokenIndex = index
if (!maybeEat(",")) break
yield { tag: TokenTag.Comma, index: commaTokenIndex, size: 1 }
skipWhitespace() }
const closeSquiglyBracketTokenIndex = index if (!maybeEat("}")) throw SyntaxError(`Unexpected character "${source[index]}"`)
yield { tag: TokenTag.CloseSquiglyBracket, index: closeSquiglyBracketTokenIndex, size: 1 } } } else if (maybeEat("[")) { yield { tag: TokenTag.OpenSquareBracket, index: tokenIndex, size: 1 } skipWhitespace()
const closeSquareBracketTokenIndex = index
if (maybeEat("]")) yield { tag: TokenTag.CloseSquareBracket, index: closeSquareBracketTokenIndex, size: 1 } else { while (index < source.length) { yield* parseValue()
const commaTokenIndex = index
if (!maybeEat(",")) break
yield { tag: TokenTag.Comma, index: commaTokenIndex, size: 1 } }
const closeSquareBracketTokenIndex = index if (!maybeEat("]")) throw SyntaxError(`Unexpected character "${source[index]}"`)
yield { tag: TokenTag.CloseSquareBracket, index: closeSquareBracketTokenIndex, size: 1 } } } else yield maybeParseString() || maybeParseKeyword() || maybeParseNumber()
skipWhitespace() }
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") )) throw SyntaxError(`Unexpected character "${source[index]}"`)
while (index < source.length && maybeEatDigit()); }
if (maybeEat(".")) { if (!maybeEatDigit()) throw SyntaxError(`Unexpected character "${source[index]}"`) while (index < source.length && maybeEatDigit()); }
if (maybeEat("e") || maybeEat("E")) { maybeEat("-") || maybeEat("+")
if (!maybeEatDigit()) throw SyntaxError(`Unexpected character "${source[index]}"`)
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") { throw SyntaxError(`Unexpected character "\\u${ source.charCodeAt(index).toString(16).toUpperCase().padStart(4, `0`) }"`) }
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
throw SyntaxError(`Unexpected character "${source[index]}"`) }
continue } else if (maybeEat(`"`) || maybeEat("\\") || maybeEat("/") || maybeEat("b") || maybeEat("f") || maybeEat("n") || maybeEat("r") || maybeEat("t") ) continue
throw SyntaxError(`Unexpected character "${source[index]}"`) }
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}