circuits-parser
Safe HaskellNone
LanguageGHC2024

Circuit.Parser.Json.Lexer

Description

Fast imperative JSON tokenizer over strict ByteString.

Complementary to the combinator path in Circuit.Parser.Json: it trades compositionality for raw speed — a single unsafeIndex pass emitting zero-copy slices. Structural tokens only; validation (number grammar, escape legality, nesting) belongs to the layer above.

Synopsis

Tokens

data JsonToken Source #

A flat JSON structural token. TString and TNumber payloads are zero-copy slices of the input; a TString keeps its escapes intact.

Instances

Instances details
Eq JsonToken Source # 
Instance details

Defined in Circuit.Parser.Json.Lexer

Show JsonToken Source # 
Instance details

Defined in Circuit.Parser.Json.Lexer

Running

runJsonLexerBS :: ByteString -> Either (Int, String) [JsonToken] Source #

Tokenize a strict ByteString.

On failure, returns the byte offset and a message.

>>> runJsonLexerBS (C.pack "{\"a\": [1, true]}")
Right [TBraceOpen,TString "a",TColon,TBrackOpen,TNumber "1",TComma,TTrue,TBrackClose,TBraceClose]
>>> runJsonLexerBS (C.pack "[1")
Right [TBrackOpen,TNumber "1"]
>>> runJsonLexerBS (C.pack "\"ab")
Left (3,"unterminated string")
>>> runJsonLexerBS (C.pack "nul")
Left (0,"bad literal")
>>> runJsonLexerBS (C.pack "@")
Left (0,"unexpected byte 64")