Explorer

definy.json-parse-scalar

description
Parse true, false, null or an integer at index (other numbers are an error)
type
Fn(source: Record{text: String, bytes: List<Number>}, index: Number) -> Union{#ok(Record{value: @definy.json, next: Number}), #error(String)}
expression
fn(source$0, index$1) =>
  match @definy.text-expect(source: $0, index: $1, text: "true") {
    #ok(parsed$2) =>
      #ok({value: #boolean<@definy.json>(true), next: $2.next}),
    #error(_) =>
      match @definy.text-expect(source: $0, index: $1, text: "false") {
        #ok(parsed$3) =>
          #ok({value: #boolean<@definy.json>(false), next: $3.next}),
        #error(_) =>
          match @definy.text-expect(source: $0, index: $1, text: "null") {
            #ok(parsed$4) =>
              #ok({value: #null<@definy.json>, next: $4.next}),
            #error(_) =>
              match @definy.text-read-integer(source: $0, index: $1) {
                #error(_) =>
                  #error(@definy.text-error(message: "expected a JSON value", index: $1)),
                #ok(number$5) =>
                  let after$6 = @definy.text-byte-at(source: $0, index: $5.next);
                  match @core.or(
                      left: @core.equal(left: $6, right: 46),
                      right: @core.or(left: @core.equal(left: $6, right: 101), right: @core.equal(left: $6, right: 69))
                    ) {
                    true =>
                      #error(@definy.text-error(message: "only integer JSON numbers are supported", index: $5.next)),
                    false =>
                      #ok({value: #number<@definy.json>($5.value), next: $5.next})
                  }
              }
          }
      }
  }
visibility
public
definy.json-parse-scalar (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: Whr5olJ5GLpXbwkwFsUxeCMOepAOnIcCGKYANRHAFWM
content hash: iRDXG4dLukQ7amUqTrcx4GqkKWS0979BZVlFs1zGqx0