JsonLexer structv0.1.0

Source
A JSON lexer over a byte slice.
pub struct JsonLexer {
    pub position: uint;
    // private fields
}

Fields

NameTypeDescription
positionuintHow far reading has reached.

Constructors

JsonLexer

pub func JsonLexer(
    input: char8[..],
    limits: JsonLimits
) -> JsonLexer

A lexer over input under limits.

NameTypeDescription
inputchar8[..]the text to read
limitsJsonLimitsthe bounds the parse refuses to exceed

Returns: a lexer positioned at the start of the input

Source

Methods

Position

pub func Position(self: &JsonLexer) -> uint

Where reading has reached, as a byte offset.

Returns: how many bytes have been consumed

Source

Next

pub func Next(self: &var JsonLexer) -> JsonToken

The next token.

Returns: the next token, or the end-of-input token

Source

JsonTokenKind

pub enum JsonTokenKind: int32 {
    End = 0,
    ObjectStart = 1,
    ObjectEnd = 2,
    ArrayStart = 3,
    ArrayEnd = 4,
    Colon = 5,
    Comma = 6,
    Text = 7,
    Number = 8,
    True = 9,
    False = 10,
    Null = 11,
    Error = 12
}

What kind of token this is.

Source

Cases

NameValueDescription
End0The input is exhausted.
ObjectStart1{
ObjectEnd2}
ArrayStart3[
ArrayEnd4]
Colon5:
Comma6,
Text7A string, whose span excludes the quotes.
Number8A number, whose span is its whole text.
True9true
False10false
Null11null
Error12Something that is not JSON, or a limit that was reached.

JsonLexError

pub enum JsonLexError: int32 {
    None = 0,
    UnexpectedByte = 1,
    UnterminatedText = 2,
    BadEscape = 3,
    BadUnicodeEscape = 4,
    MalformedNumber = 5,
    InvalidUtf8 = 6,
    UnescapedControl = 7,
    TooManyBytes = 8,
    TextTooLong = 9,
    TooDeep = 10,
    TooManyEntries = 11
}

Why lexing stopped.

Source

Cases

NameValueDescription
None0Nothing went wrong.
UnexpectedByte1A byte that begins no token.
UnterminatedText2A string that the input ended before closing.
BadEscape3An escape sequence JSON does not define.
BadUnicodeEscape4A \u escape that is not four hexadecimal digits.
MalformedNumber5A number whose shape JSON's grammar does not allow.
InvalidUtf86Bytes that are not well-formed UTF-8.
UnescapedControl7A control character appearing unescaped inside a string, which JSON forbids.
TooManyBytes8The document is longer than the configured limit.
TextTooLong9A string is longer than the configured limit.
TooDeep10Containers are nested deeper than the configured limit.
TooManyEntries11A container holds more entries than the configured limit.

JsonToken

pub struct JsonToken {
    pub kind: JsonTokenKind;
    pub start: uint;
    pub end: uint;
    pub escaped: bool;
    pub error: JsonLexError;
}

One token, and where in the input it came from.

Source

Fields

NameTypeDescription
kindJsonTokenKindWhat kind of token this is.
startuintWhere its text starts, as a byte offset into the input.
enduintWhere its text ends.
escapedboolFor a string, whether it contains any escape that must be resolved. A string without one can be used directly from the input, which is what keeps ordinary documents free of copying.
errorJsonLexErrorWhy lexing stopped, when the kind is Error.