import Foundation /// JSON with comments allowed, since tsconfig-style files carry them. enum JSONGrammar { private static let literals: Set = ["true", "false", "null"] static func scan(_ lx: inout Lexer, _ st: inout GrammarState) { while lx.i < lx.end { if st.mode == .blockComment { if lx.blockComment(from: lx.i, close: Literals.blockClose) { st.mode = .code } continue } let start = lx.i let c = lx.u[start] switch c { case Unit.slash where lx.peek(1) == Unit.slash: lx.lineComment() case Unit.slash where lx.peek(1) == Unit.star: lx.i += 2 if !lx.blockComment(from: start, close: Literals.blockClose) { st.mode = .blockComment } case Unit.quote: lx.i += 1 var closed = false while lx.i < lx.end && !closed { if lx.u[lx.i] == Unit.backslash { lx.i += 1 } else if lx.u[lx.i] == Unit.quote { closed = true } lx.i += 1 } lx.i = min(lx.i, lx.end) lx.emit(start, lx.i, lx.nextNonSpace() == Unit.colon ? .variable : .string) case _ where Unit.isDigit(c) || (c == Unit.minus && Unit.isDigit(lx.peek(1))): if c == Unit.minus { lx.i += 1 lx.emit(start, lx.i, .number) } lx.number() case _ where Unit.isAlpha(c): lx.identifier() if literals.contains(lx.text(start, lx.i)) { lx.emit(start, lx.i, .number) } default: lx.i += 1 } } } }