toml-mbt

A TOML 1.0 parser and serializer for MoonBit

toml
parser
serializer
config
moon add yufenfei111/toml-mbt@0.1.0
Download zip
Version
0.1.0
License
Apache-2.0
Last updated
2 days ago
Downloads
2
README

#yufenfei111/toml-mbt

#
Table

type Table = Map[String, Value]

A table is a map from string keys to TOML values. The root of any parsed document is a Table, and inline tables inside a document are also tables.

#
TomlError

pub suberror TomlError {
ParseError(Int, Int, String)
} derive(
Debug
)

A TOML parse error carrying a 1-based source position and a description.

line and column locate the error in the input; message describes it.

#
TomlError::new

fn TomlError::new(line : Int, column : Int, message : String) -> TomlError

Constructs a TomlError at the given 1-based line and column.

#
TomlError::to_string

fn TomlError::to_string(self : TomlError) -> String

Renders the error as line N, column M: message.

#
Lexer

type Lexer

A hand-written lexer for TOML source text.

It produces a flat token stream with 1-based source positions, leaving semantic interpretation (numbers, dates, dotted keys) to the parser. It understands the hard parts of TOML lexical structure: the four string forms (basic, literal, and their multiline variants) and the line-ending backslash in multiline basic strings.

#
Lexer::tokenize

fn Lexer::tokenize(input : String) -> Array[Token] raise TomlError

Tokenizes input into a token stream, or raises TomlError on lexical errors such as an unterminated string or an invalid escape.

#
LocalDate

pub struct LocalDate {
year : Int
month : Int
day : Int
} derive(Eq,
Debug
)

A calendar date (year-month-day) with no time and no timezone.

#
LocalDate::new

fn LocalDate::new(year : Int, month : Int, day : Int) -> LocalDate

Constructors for LocalDate.

#
LocalDateTime

pub struct LocalDateTime {
date : LocalDate
time : LocalTime
} derive(Eq,
Debug
)

A local date-time (a date plus a time, no timezone).

#
LocalDateTime::new

fn LocalDateTime::new(date : LocalDate, time : LocalTime) -> LocalDateTime

Constructors for LocalDateTime.

#
LocalTime

pub struct LocalTime {
hour : Int
minute : Int
second : Int
nanosecond : Int
} derive(Eq,
Debug
)

A wall-clock time (hour:minute:second.nanosecond) with no timezone.

#
LocalTime::new

fn LocalTime::new(hour : Int, minute : Int, second : Int, nanosecond : Int) -> LocalTime

Constructors for LocalTime.

#
OffsetDateTime

pub struct OffsetDateTime {
date : LocalDate
time : LocalTime
offset_minutes : Int
} derive(Eq,
Debug
)

A date-time with a UTC offset. The offset is stored as a whole number of minutes east of UTC (-07:00 becomes -420, Z becomes 0).

#
OffsetDateTime::new

fn OffsetDateTime::new(date : LocalDate, time : LocalTime, offset_minutes : Int) -> OffsetDateTime

Constructors for OffsetDateTime.

#
Parser

type Parser

#
Token

pub struct Token {
kind : TokenKind
line : Int
column : Int
} derive(Eq,
Debug
)

A token together with its 1-based source position (line and column).

#
Token::new

fn Token::new(kind : TokenKind, line : Int, column : Int) -> Token

Constructs a token at the given 1-based line and column.

#
TokenKind

pub enum TokenKind {
Word(String)
Str(String)
Equal
Comma
LBracket
RBracket
LBrace
RBrace
Newline
EOF
} derive(Eq,
Debug
)

The lexical kind of a token produced by the lexer.

#
Value

pub enum Value {
String(String)
Integer(Int64)
Float(Double)
Boolean(Bool)
OffsetDateTime(OffsetDateTime)
LocalDateTime(LocalDateTime)
LocalDate(LocalDate)
LocalTime(LocalTime)
Array(Array[Value])
Table(Map[String, Value])
} derive(
Debug
)

A TOML value. Mirrors every value kind defined by the TOML 1.0 spec.
impl Eq for Value

#
parse

fn parse(text : String) -> Result[Map[String, Value], TomlError]

Parses a TOML document into a Table, or returns a TomlError.

#
serialize

fn serialize(table : Map[String, Value]) -> String

Serializes a table into TOML text.

#
to_json

fn to_json(table : Map[String, Value]) -> Json

Converts a table to the toml-test tagged JSON representation.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io