# The tokens of a safetensors header (JSON), from its bytes: one step a byte, so every step is a plain state change. # Strings are decoded to code points (UTF-8 and \u escapes, surrogate pairs joined); numbers are unsigned decimal # integers up to 2^64 - 1, which is all a header holds. Anything else becomes a TBad token, and the rest is ignored. import Base import ./w64.bend as W type Tok is Data: TOpen{} TClose{} TLBr{} TRBr{} TColon{} TComma{} TStr{s: String} TNum{v: W.W64} TBad{why: String} type Lx is Data: LxTop{} LxStr{cs: List<&2, U32>} LxUtf{cs: List<&2, U32>, need: U32, cp: U32, min: U32} LxEsc{cs: List<&2, U32>} LxHex{cs: List<&2, U32>, left: U32, v: U32, hi: U32} LxLow{cs: List<&2, U32>, hi: U32} LxLowU{cs: List<&2, U32>, hi: U32} LxNum{v: W.W64} LxBad{} type Out is Data: Out{st: Lx, ts: List<&2, Tok>} def bad(why: String, ts: List<&2, Tok>) -> Out: Out{LxBad{}, TBad{why} <> ts} def in_range(+b: U32, +lo: U32, +hi: U32) -> Bool: Bool.and(U32.is_le(lo, b), U32.is_le(b, hi)) # a string's code points, kept reversed while lexing, as a String def chars(cs: List<&2, U32>, acc: String) -> String: match cs: case []: acc case h <> t: chars(t, SCon{Chr{h}, acc}) # ---- between tokens ---- type Cb is Data: CbWs{} CbOpen{} CbClose{} CbLBr{} CbRBr{} CbColon{} CbComma{} CbQuote{} CbDigit{} CbOther{} def cls_top(+b: U32) -> Cb: Bool.pick(Cb, Bool.or(Bool.or(U32.is_eq(b, 32), U32.is_eq(b, 9)), Bool.or(U32.is_eq(b, 10), U32.is_eq(b, 13))), CbWs{}, Bool.pick(Cb, U32.is_eq(b, 123), CbOpen{}, Bool.pick(Cb, U32.is_eq(b, 125), CbClose{}, Bool.pick(Cb, U32.is_eq(b, 91), CbLBr{}, Bool.pick(Cb, U32.is_eq(b, 93), CbRBr{}, Bool.pick(Cb, U32.is_eq(b, 58), CbColon{}, Bool.pick(Cb, U32.is_eq(b, 44), CbComma{}, Bool.pick(Cb, U32.is_eq(b, 34), CbQuote{}, Bool.pick(Cb, in_range(b, 48, 57), CbDigit{}, CbOther{}))))))))) def top_step(c: Cb, +b: U32, ts: List<&2, Tok>) -> Out: match c: case CbWs{}: Out{LxTop{}, ts} case CbOpen{}: Out{LxTop{}, TOpen{} <> ts} case CbClose{}: Out{LxTop{}, TClose{} <> ts} case CbLBr{}: Out{LxTop{}, TLBr{} <> ts} case CbRBr{}: Out{LxTop{}, TRBr{} <> ts} case CbColon{}: Out{LxTop{}, TColon{} <> ts} case CbComma{}: Out{LxTop{}, TComma{} <> ts} case CbQuote{}: Out{LxStr{[]}, ts} case CbDigit{}: Out{LxNum{W.W64.of((b - 48 : U32))}, ts} case CbOther{}: bad("unexpected byte " ++ U32.show(b) ++ " (only objects, arrays, strings and unsigned integers)", ts) # ---- numbers ---- def num_more(r: Maybe<&2, W.W64>, ts: List<&2, Tok>) -> Out: match r: case None{}: bad("a number past 2^64 - 1", ts) case Some{v}: Out{LxNum{v}, ts} def num_step(isd: Bool, v: W.W64, +b: U32, ts: List<&2, Tok>) -> Out: match isd: case True{}: num_more(W.W64.digit(v, (b - 48 : U32)), ts) case False{}: top_step(cls_top(b), b, TNum{v} <> ts) # ---- strings ---- type Cs is Data: CsQuote{} CsBack{} CsCtl{} CsAscii{} CsLead2{} CsLead3{} CsLead4{} CsBadB{} def cls_str(+b: U32) -> Cs: Bool.pick(Cs, U32.is_eq(b, 34), CsQuote{}, Bool.pick(Cs, U32.is_eq(b, 92), CsBack{}, Bool.pick(Cs, U32.is_lt(b, 32), CsCtl{}, Bool.pick(Cs, U32.is_lt(b, 128), CsAscii{}, Bool.pick(Cs, in_range(b, 194, 223), CsLead2{}, Bool.pick(Cs, in_range(b, 224, 239), CsLead3{}, Bool.pick(Cs, in_range(b, 240, 244), CsLead4{}, CsBadB{}))))))) def str_step(c: Cs, +b: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match c: case CsQuote{}: Out{LxTop{}, TStr{chars(cs, SNil{})} <> ts} case CsBack{}: Out{LxEsc{cs}, ts} case CsCtl{}: bad("a control byte inside a string", ts) case CsAscii{}: Out{LxStr{b <> cs}, ts} case CsLead2{}: Out{LxUtf{cs, 1, U32.and(b, 31), 128}, ts} case CsLead3{}: Out{LxUtf{cs, 2, U32.and(b, 15), 2048}, ts} case CsLead4{}: Out{LxUtf{cs, 3, U32.and(b, 7), 65536}, ts} case CsBadB{}: bad("invalid UTF-8", ts) def is_scalar(+cp: U32) -> Bool: Bool.and(U32.is_le(cp, 1114111), Bool.not(in_range(cp, 55296, 57343))) def utf_done(ok: Bool, +cp: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match ok: case True{}: Out{LxStr{cp <> cs}, ts} case False{}: bad("invalid UTF-8", ts) def utf_next(last: Bool, +need: U32, +cp: U32, +min: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match last: case True{}: utf_done(Bool.and(U32.is_le(min, cp), is_scalar(cp)), cp, cs, ts) case False{}: Out{LxUtf{cs, (need - 1 : U32), cp, min}, ts} def utf_step(cont: Bool, +b: U32, +need: U32, +cp: U32, +min: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match cont: case True{}: utf_next(U32.is_eq(need, 1), need, ((cp * 64 : U32) + U32.and(b, 63) : U32), min, cs, ts) case False{}: bad("invalid UTF-8", ts) # ---- escapes ---- # the character a one-letter escape stands for, 1 for \u, 0 for none def esc_val(+b: U32) -> U32: Bool.pick(U32, U32.is_eq(b, 34), 34, Bool.pick(U32, U32.is_eq(b, 92), 92, Bool.pick(U32, U32.is_eq(b, 47), 47, Bool.pick(U32, U32.is_eq(b, 98), 8, Bool.pick(U32, U32.is_eq(b, 102), 12, Bool.pick(U32, U32.is_eq(b, 110), 10, Bool.pick(U32, U32.is_eq(b, 114), 13, Bool.pick(U32, U32.is_eq(b, 116), 9, Bool.pick(U32, U32.is_eq(b, 117), 1, 0))))))))) def esc_fin(isu: Bool, +v: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match isu: case True{}: Out{LxHex{cs, 4, 0, 0}, ts} case False{}: Out{LxStr{v <> cs}, ts} def esc_step(none: Bool, +v: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match none: case True{}: bad("an unknown escape", ts) case False{}: esc_fin(U32.is_eq(v, 1), v, cs, ts) # the value of a hex digit, 16 for none def hex_val(+b: U32) -> U32: Bool.pick(U32, in_range(b, 48, 57), (b - 48 : U32), Bool.pick(U32, in_range(b, 97, 102), (b - 87 : U32), Bool.pick(U32, in_range(b, 65, 70), (b - 55 : U32), 16))) def is_high(+v: U32) -> Bool: in_range(v, 55296, 56319) def is_low(+v: U32) -> Bool: in_range(v, 56320, 57343) # a \uXXXX with no high surrogate before it def u_alone(high: Bool, +v: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match high: case True{}: Out{LxLow{cs, v}, ts} case False{}: utf_done(Bool.not(is_low(v)), v, cs, ts) # a \uXXXX after a high surrogate: it must be the low half def u_pair(low: Bool, +hi: U32, +v: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match low: case True{}: Out{LxStr{((65536 + ((hi - 55296 : U32) * 1024 : U32) : U32) + (v - 56320 : U32) : U32) <> cs}, ts} case False{}: bad("an unpaired surrogate escape", ts) def u_done(first: Bool, +hi: U32, +v: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match first: case True{}: u_alone(is_high(v), v, cs, ts) case False{}: u_pair(is_low(v), hi, v, cs, ts) def hex_next(last: Bool, +left: U32, +v: U32, +hi: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match last: case True{}: u_done(U32.is_eq(hi, 0), hi, v, cs, ts) case False{}: Out{LxHex{cs, (left - 1 : U32), v, hi}, ts} def hex_step(none: Bool, +d: U32, +left: U32, +v: U32, +hi: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match none: case True{}: bad("a bad \\u escape", ts) case False{}: hex_next(U32.is_eq(left, 1), left, ((v * 16 : U32) + d : U32), hi, cs, ts) def low_step(ok: Bool, +hi: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match ok: case True{}: Out{LxLowU{cs, hi}, ts} case False{}: bad("an unpaired surrogate escape", ts) def lowu_step(ok: Bool, +hi: U32, cs: List<&2, U32>, ts: List<&2, Tok>) -> Out: match ok: case True{}: Out{LxHex{cs, 4, 0, hi}, ts} case False{}: bad("an unpaired surrogate escape", ts) # ---- one byte ---- def step(o: Out, +b: U32) -> Out: match o: case Out{LxTop{}, ts}: top_step(cls_top(b), b, ts) case Out{LxStr{cs}, ts}: str_step(cls_str(b), b, cs, ts) case Out{LxUtf{cs, +need, +cp, +min}, ts}: utf_step(in_range(b, 128, 191), b, need, cp, min, cs, ts) case Out{LxEsc{cs}, ts}: +v = esc_val(b) esc_step(U32.is_eq(v, 0), v, cs, ts) case Out{LxHex{cs, +left, +v, +hi}, ts}: +d = hex_val(b) hex_step(U32.is_eq(d, 16), d, left, v, hi, cs, ts) case Out{LxLow{cs, +hi}, ts}: low_step(U32.is_eq(b, 92), hi, cs, ts) case Out{LxLowU{cs, +hi}, ts}: lowu_step(U32.is_eq(b, 117), hi, cs, ts) case Out{LxNum{v}, ts}: num_step(in_range(b, 48, 57), v, b, ts) case Out{LxBad{}, ts}: Out{LxBad{}, ts} def finish(o: Out) -> List<&2, Tok>: match o: case Out{LxTop{}, ts}: List.reverse(&2, Tok, ts) case Out{LxNum{v}, ts}: List.reverse(&2, Tok, TNum{v} <> ts) case Out{LxBad{}, ts}: List.reverse(&2, Tok, ts) case Out{st, ts}: List.reverse(&2, Tok, TBad{"the header ends inside a string"} <> ts) def go(bs: List<&2, U32>, o: Out) -> List<&2, Tok>: match bs: case []: finish(o) case h <> t: go(t, step(o, h)) # the tokens of a header's bytes def tokens(bs: List<&2, U32>) -> List<&2, Tok>: go(bs, Out{LxTop{}, []})