~/bend-docscommunity

json.bend checks

raw source on the hub · import bend-bpe-tokenizer@0.1.0.0/json.bend as Json

JSON (RFC 8259) from bytes, in one pass: a lexer state machine whose finished tokens drive a parser stack, so the text never becomes a token list. Strings are decoded to code points (escapes, surrogate pairs, UTF-8); a number is kept as an unsigned 32-bit integer when it is one, else marked as not one. Commas and colons are separators: the parser checks that containers nest and close, and that object keys are strings, not every comma.

2 imports
import Base
import ./utf8.bend as U

Types

type J source · line 8 · raw

Data

type KV source · line 16 · raw

Data

type Fr source · line 20 · raw

Data

---- the parser: a stack of open containers ----

type Ps source · line 25 · raw

Data

stack: open containers, innermost first; top: the finished top-level value; bad: the first error

type Ev source · line 120 · raw

Data

---- the lexer: bytes to events, which the parser folds (parse), or chunks lexed in parallel (parse_par) ----

type Lx source · line 133 · raw

Data

type St source · line 144 · raw

Data

type ET source · line 516 · raw

Data

events in a tree of lists, so the halves' results join without copying

Definitions

def p_bad source · line 28 · raw

@why:String -> @p:Ps -> Ps

def key_of source · line 33 · raw

@v:J -> Maybe<&2, List<&2, U32>>

def obj_key source · line 48 · raw

@k:Maybe<&2, List<&2, U32>> -> @kvs:List<&2, KV> -> @rest:List<&2, Fr> -> @top:Maybe<&2, J> -> @bad:Maybe<&2, String> -> Ps

def obj_value source · line 55 · raw

@key:Maybe<&2, List<&2, U32>> -> @v:J -> @kvs:List<&2, KV> -> @rest:List<&2, Fr> -> @top:Maybe<&2, J> -> @bad:Maybe<&2, String> -> Ps

def add_frame source · line 62 · raw

@f:Fr -> @v:J -> @rest:List<&2, Fr> -> @top:Maybe<&2, J> -> @bad:Maybe<&2, String> -> Ps

def add_top source · line 69 · raw

@top:Maybe<&2, J> -> @v:J -> @bad:Maybe<&2, String> -> Ps

def add_in source · line 77 · raw

@stack:List<&2, Fr> -> @v:J -> @top:Maybe<&2, J> -> @bad:Maybe<&2, String> -> Ps

a finished value goes into the innermost open container, or becomes the top-level value

def add source · line 84 · raw

@p:Ps -> @v:J -> Ps

def open source · line 89 · raw

@p:Ps -> @f:Fr -> Ps

def close_obj source · line 94 · raw

@key:Maybe<&2, List<&2, U32>> -> @kvs:List<&2, KV> -> @rest:List<&2, Fr> -> @top:Maybe<&2, J> -> @bad:Maybe<&2, String> -> Ps

def close_frame source · line 101 · raw

@f:Fr -> @arr:Bool -> @rest:List<&2, Fr> -> @top:Maybe<&2, J> -> @bad:Maybe<&2, String> -> Ps

def close_in source · line 112 · raw

@stack:List<&2, Fr> -> @arr:Bool -> @top:Maybe<&2, J> -> @bad:Maybe<&2, String> -> Ps

def close source · line 128 · raw

@p:Ps -> @arr:Bool -> Ps

def is_digit source · line 147 · raw

@+b:U32 -> Bool

def is_space source · line 150 · raw

@+b:U32 -> Bool

def hex_val source · line 153 · raw

@+b:U32 -> U32

def str_done source · line 159 · raw

@r:Result<&2, &2, U32, List<&2, U32>> -> @p:List<&2, Ev> -> List<&2, Ev>

a finished string: its bytes (last first), decoded

def when source · line 167 · raw

@c:Bool -> @yes:(@_:Unit -> St) -> @no:(@_:Unit -> St) -> St

a byte between tokens

def top source · line 174 · raw

@+b:U32 -> @+p:List<&2, Ev> -> St

def num_more source · line 188 · raw

@+b:U32 -> @+v:U32 -> @+int:Bool -> @+p:List<&2, Ev> -> St

def lit_next source · line 194 · raw

@ok:Bool -> @t:List<&2, U32> -> @val:J -> @p:List<&2, Ev> -> St

def lit_more source · line 201 · raw

@rest:List<&2, U32> -> @+b:U32 -> @val:J -> @p:List<&2, Ev> -> St

def esc source · line 208 · raw

@+b:U32 -> @+bs:List<&2, U32> -> @+p:List<&2, Ev> -> St

def hex_done source · line 214 · raw

@+v:U32 -> @+hi:U32 -> @+bs:List<&2, U32> -> @+p:List<&2, Ev> -> St

four hex digits read: a high surrogate waits for its low half; a low half joins a waiting high one

def hex source · line 221 · raw

@+b:U32 -> @+bs:List<&2, U32> -> @+left:U32 -> @+v:U32 -> @+hi:U32 -> @+p:List<&2, Ev> -> St

def in_str source · line 226 · raw

@+b:U32 -> @+bs:List<&2, U32> -> @+p:List<&2, Ev> -> St

def step_lx source · line 230 · raw

@lx:Lx -> @+b:U32 -> @+p:List<&2, Ev> -> St

def step source · line 249 · raw

@s:St -> @+b:U32 -> St

def run source · line 254 · raw

@bs:List<&2, U32> -> @s:St -> St

def is_nil_u source · line 261 · raw

@xs:List<&2, U32> -> Bool

def lit_end source · line 268 · raw

@done:Bool -> @val:J -> @p:List<&2, Ev> -> List<&2, Ev>

def end_lx source · line 275 · raw

@lx:Lx -> @p:List<&2, Ev> -> List<&2, Ev>

def is_nil_f source · line 294 · raw

@xs:List<&2, Fr> -> Bool

def result3 source · line 301 · raw

@top:Maybe<&2, J> -> Result<&2, &2, String, J>

def result2 source · line 308 · raw

@bad:Maybe<&2, String> -> @closed:Bool -> @top:Maybe<&2, J> -> Result<&2, &2, String, J>

def result source · line 317 · raw

@p:Ps -> Result<&2, &2, String, J>

def events source · line 322 · raw

@s:St -> List<&2, Ev>

def lex source · line 328 · raw

@bs:List<&2, U32> -> List<&2, Ev>

the events of bs, in order

def apply source · line 332 · raw

@p:Ps -> @e:Ev -> Ps

the parser, one event at a time

def fold source · line 347 · raw

@es:List<&2, Ev> -> @p:Ps -> Ps

def parse_events source · line 354 · raw

@es:List<&2, Ev> -> Result<&2, &2, String, J>

def parse source · line 358 · raw

@bs:List<&2, U32> -> Result<&2, &2, String, J>

the JSON value of bs, or why it is not one

def key_is source · line 362 · raw

@k:List<&2, U32> -> @s:String -> Bool

---- reading a parsed value ----

def field_pick source · line 373 · raw

@hit:Bool -> @v:J -> @rest:(@_:Unit -> Maybe<&2, J>) -> Maybe<&2, J>

def field_go source · line 380 · raw

@kvs:List<&2, KV> -> @+name:String -> Maybe<&2, J>

def field source · line 388 · raw

@v:J -> @+name:String -> Maybe<&2, J>

an object's field, if the value is an object and has it

def blocks source · line 408 · raw

@fuel:Nat -> @+size:Nat -> @bs:List<&2, U32> -> List<&2, List<&2, U32>>

---- parsing in parallel ---- A raw newline never occurs inside a JSON token (strings must escape it), so text cut just after newlines lexes piece by piece to the same events, and the pieces can be lexed at the same time. The text is cut into blocks of 2^16 bytes by count (any size gives the same events: LAWS.bend); block i's lexer starts after its first newline (block 0's at its start) and runs on through block i + 1 up to and including that block's first newline, so the pieces it lexes start and end at newlines.

def line_if source · line 418 · raw

@nl:Bool -> @+b:U32 -> @more:(@_:Unit -> List<&2, U32>) -> List<&2, U32>

the bytes up to and including the first newline

def head_line source · line 425 · raw

@bs:List<&2, U32> -> List<&2, U32>

def after_if source · line 433 · raw

@nl:Bool -> @t:List<&2, U32> -> @more:(@_:Unit -> List<&2, U32>) -> List<&2, U32>

the bytes after the first newline ([] when there is none: the block is inside one line, lexed by the block before)

def after_line source · line 440 · raw

@bs:List<&2, U32> -> List<&2, U32>

def start_of source · line 447 · raw

@first:Bool -> @b:List<&2, U32> -> List<&2, U32>

def nl_or source · line 454 · raw

@nl:Bool -> @more:(@_:Unit -> Bool) -> Bool

def has_nl source · line 461 · raw

@bs:List<&2, U32> -> Bool

def tail_if source · line 468 · raw

@nl:Bool -> @+b:List<&2, U32> -> @st:St -> @more:(@_:St -> St) -> St

def tail_run source · line 476 · raw

@bs:List<&2, List<&2, U32>> -> @st:St -> St

on through the following blocks up to and including the first newline (a block without one is lexed whole)

def lex_owned source · line 485 · raw

@owns:Bool -> @first:Bool -> @b:List<&2, U32> -> @rest:List<&2, List<&2, U32>> -> List<&2, Ev>

one block's events: from its start (first) or after its first newline, on through the following blocks to the next newline; a block with no newline lexes nothing (the block before it lexes it)

def lex_block source · line 492 · raw

@+first:Bool -> @+b:List<&2, U32> -> @rest:List<&2, List<&2, U32>> -> List<&2, Ev>

def seq_blocks source · line 496 · raw

@n:Nat -> @+first:Bool -> @bs:List<&2, List<&2, U32>> -> List<&2, Ev>

blocks i .. i + n (their successors alongside), halves lexed at the same time, depth times over

def half_go source · line 505 · raw

@n:Nat -> @+odd:Bool -> @+acc:Nat -> Nat

def half source · line 512 · raw

@n:Nat -> Nat

def par_blocks source · line 520 · raw

@depth:Nat -> @+n:Nat -> @+first:Bool -> @+bs:List<&2, List<&2, U32>> -> ET

def fold_tree source · line 528 · raw

@t:ET -> @p:Ps -> Ps

def par_events source · line 536 · raw

@+bs:List<&2, List<&2, U32>> -> ET

n / 2, rounded down: every second step counts

def parse_blocks source · line 540 · raw

@+bs:List<&2, List<&2, U32>> -> Result<&2, &2, String, J>

parse text given as blocks (as Tok.open reads a file), the blocks lexed at the same time

def parse_par source · line 544 · raw

@+bs:List<&2, U32> -> Result<&2, &2, String, J>

parse, with the lexing spread over the CPU's threads