toml::lex

The toml::lex module provides a lexer for TOML documents. A lexer can be created with init and progressed with lex.

Index

Types

type lexer = struct {
	in: *bufio::scanner,
	pos: location,
	prev_pos: (location | void),
	buf: memio::stream,
};
type location = struct {
	file: str,
	line: int,
	column: int,
};
type token = struct {
	token: tokens::token,
	pos: location,
};

Errors

type error = !(io::error | fs::error | utf8::invalid | unexpected_eof | invalid_syntax | invalid_character_escape | invalid_unicode_escape | overflow);
type invalid_character_escape = !struct {
	pos: location,
	escape: rune,
};
type invalid_syntax = !location;
type invalid_unicode_escape = !location;
type overflow = !location;
type unexpected_eof = !location;

Functions

fn finish(lex: *lexer) void;
fn init(in: *bufio::scanner, path: str) lexer;
fn lex(lex: *lexer, expects_key: bool) (token | error);
fn strerror(err: error) str;

Types

type lexer[link]

type lexer = struct {
	in: *bufio::scanner,
	pos: location,
	prev_pos: (location | void),
	buf: memio::stream,
};

Contains the state of a lexer. You can create one with init.

type location[link]

type location = struct {
	file: str,
	line: int,
	column: int,
};

A location in a file. The path is borrowed from the one given to init.

type token[link]

type token = struct {
	token: tokens::token,
	pos: location,
};

A token returned by the lexer. The actual contents of the token is a toml::tokens::token.

Errors

type error[link]

type error = !(io::error | fs::error | utf8::invalid | unexpected_eof | invalid_syntax | invalid_character_escape | invalid_unicode_escape | overflow);

An error encountered while lexing the TOML document.

type invalid_character_escape[link]

type invalid_character_escape = !struct {
	pos: location,
	escape: rune,
};

Invalid character escape sequence.

type invalid_syntax[link]

type invalid_syntax = !location;

Invalid syntax somewhere around the contained location.

type invalid_unicode_escape[link]

type invalid_unicode_escape = !location;

Invalid unicode escape sequence.

type overflow[link]

type overflow = !location;

Parsing a number caused an overflow.

type unexpected_eof[link]

type unexpected_eof = !location;

Returned when the lexer encountered the end of the document before it expected to.

Functions

fn finish[link]

fn finish(lex: *lexer) void;

Frees lexer resources.

fn init[link]

fn init(in: *bufio::scanner, path: str) lexer;

Initializes a new lexer. The path is used for error messages. finish should be called once the user is finished with the lexer.

fn lex[link]

fn lex(lex: *lexer, expects_key: bool) (token | error);

Gets the next token from the lexer.

If the returned token contains a string, that string is owned by the lexer. It may be invalidated after any future operation on the lexer.

Because TOML puts very little restrictions on what a key can look like, the difference between a key and a value can be ambiguous if no context about the surrounding tokens is known. Because of that, responsibility is put on the user to keep track of whether or not the next token might be a key. If expects_key is passed as true, then text that could otherwise be interpreted as a value will be interpreted as a token instead, if possible. Additionally, only if a key is expected will the toml::tokens::lbracket2 and toml::tokens::rbracket2 tokens be returned.

fn strerror[link]

fn strerror(err: error) str;

Converts an error into a user-friendly string. The result may be statically allocated.