TomlLexer structv0.1.0

Source
A TOML lexer over a byte slice.
pub struct TomlLexer {
    pub line: uint;
    // private fields
}

Fields

NameTypeDescription
lineuintWhich line that is on.

Constructors

TomlLexer

pub func TomlLexer(input: char8[..]) -> TomlLexer

A lexer over input.

NameTypeDescription
inputchar8[..]the text to read

Returns: a lexer positioned at the start of the input

Source

Methods

Next

pub func Next(self: &var TomlLexer) -> TomlToken

The next token, or End at the end of the input.

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

Source

TextOf

pub func TextOf(
    self: &TomlLexer,
    token: &TomlToken
) -> char8[..]

The bytes a token spans, which for a text token excludes the quotes and is still escaped.

NameTypeDescription
token&TomlTokenthe token to read

Returns: the token's text, borrowed from the input

Source

TomlTokenKind

pub enum TomlTokenKind: int32 {
    End = 0,
    BareKey = 1,
    Text = 2,
    Integer = 3,
    Float = 4,
    Boolean = 5,
    DateTime = 6,
    Equals = 7,
    Dot = 8,
    Comma = 9,
    LeftBracket = 10,
    RightBracket = 11,
    LeftBrace = 12,
    RightBrace = 13,
    Newline = 14,
    Error = 15
}

What kind of token this is.

Source

Cases

NameValueDescription
End0The input is exhausted.
BareKey1A bare key: letters, digits, underscores and hyphens.
Text2A quoted key or a string value; the span excludes the quotes and the text may need unescaping.
Integer3An integer, in any of the four bases.
Float4A float, including the infinities and NaN that TOML — unlike JSON — can spell.
Boolean5true or false.
DateTime6An offset date-time, a local date-time, a local date, or a local time.
Equals7=
Dot8.
Comma9,
LeftBracket10[
RightBracket11]
LeftBrace12{
RightBrace13}
Newline14One or more newlines, which TOML treats as significant.
Error15Something that is not TOML.

TomlTextForm

pub enum TomlTextForm: int32 {
    NotText = 0,
    Basic = 1,
    MultiLineBasic = 2,
    Literal = 3,
    MultiLineLiteral = 4,
    Bare = 5
}

Which of the four string forms a text token was written in.

Source

Cases

NameValueDescription
NotText0Not a text token.
Basic1A basic string: escapes honoured, no raw newlines.
MultiLineBasic2A multi-line basic string: escapes honoured, newlines allowed, a leading newline trimmed.
Literal3A literal string: no escapes at all, which is what makes it right for Windows paths and regular expressions.
MultiLineLiteral4A multi-line literal string: no escapes, newlines allowed.
Bare5A bare key, which has no quotes and no escapes.

TomlLexError

pub enum TomlLexError: int32 {
    None = 0,
    UnexpectedByte = 1,
    UnterminatedText = 2,
    BadEscape = 3,
    BadUnicodeEscape = 4,
    MalformedNumber = 5,
    NewlineInText = 6,
    UnescapedControl = 7,
    MalformedDateTime = 8
}

Why lexing stopped.

Source

Cases

NameValueDescription
None0Nothing went wrong.
UnexpectedByte1A byte that begins no token.
UnterminatedText2A string the input ended before closing.
BadEscape3An escape sequence TOML does not define.
BadUnicodeEscape4A unicode escape that is not the right number of digits, or names no scalar value.
MalformedNumber5A number whose shape TOML grammar does not allow.
NewlineInText6A raw newline inside a single-line string.
UnescapedControl7A control character appearing unescaped where TOML forbids it.
MalformedDateTime8A date or time that is not the shape RFC 3339 defines.

TomlToken

pub struct TomlToken {
    pub kind: TomlTokenKind;
    pub form: TomlTextForm;
    pub start: uint;
    pub end: uint;
    pub line: uint;
    pub column: uint;
    pub error: TomlLexError;
}

One token, and where it came from.

Source

Fields

NameTypeDescription
kindTomlTokenKindWhat kind of token this is.
formTomlTextFormWhich string form, for a text token.
startuintWhere its text starts, as a byte offset.
enduintWhere its text ends.
lineuintWhich line it starts on, counting from one.
columnuintWhich column it starts at, counting from one, in bytes.
errorTomlLexErrorWhy lexing stopped, when the kind is Error.

TomlDecodeText

pub func TomlDecodeText(
    allocator: Allocator,
    span: char8[..],
    form: TomlTextForm,
    bad: *var bool
) -> String ! TextError

Decodes a text token's span into an owned string, honouring the escapes its form allows. A literal string is copied unchanged — that is what makes it the right form for a Windows path — while a basic string has its escapes resolved, including the line-ending backslash that lets a multi-line string be wrapped in the source without the wrapping appearing in the value.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhere the storage comes from
spanchar8[..]the raw text the token covers
formTomlTextFormwhich quoting form the text was written in
bad*var boolreceives the offset of the first byte that could not be decoded

Returns: the decoded text, or why it could not be stored