toml-mbt

    Format-preserving TOML inspection and safe editing workbench for MoonBit

    toml
    workbench
    lossless
    diagnostics
    editor
    cli
    Download zip
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    2 months ago
    Downloads
    39

    #yufenfei111/toml-mbt

    MoonBit TOML Workbench 的包级接口说明。根包保留独立解析器原型的兼容 API;新增包提供无损源文本、诊断、编辑与纯命令层。原生文件 I/O 位于 cmd/workbench,不属于可移植库 API。

    发布状态:yufenfei111/toml-mbt@0.1.0 已发布;仓库已将模块版本准备为 Workbench 0.2.0,但尚未发布。在赛事验收前发布并回填可验证版本链接是必做事项。

    #根包:语义兼容层

    pub fn parse(String) -> Result[Table, TomlError]
    pub fn serialize(Table) -> String
    pub fn to_json(Table) -> Json
    pub fn decode_tagged_json(String) -> Result[String, TomlError]
    pub fn encode_tagged_value(Value) -> String

    根包以 TOML 1.0 为语法目标,不开启 TOML 1.1 扩展。当前官方 toml-test v2.2.0 结果为有效 181/205、无效 421/474,因此不应视为完整规范覆盖。

    #syntax:无损语句和 UTF-8 范围

    pub fn scan(String) -> ScanResult
    pub fn parse_key_path(String) -> Result[Array[String], SyntaxError]
    pub fn Document::find_entries(Self, Array[String]) -> Array[KeyValueEntry]
    pub fn Statement::text(Self, String) -> String

    SourceSpan 使用半开区间 UTF-8 字节偏移,位置为 1-based 行列。扫描失败时仍保留原始文档和 Invalid 语句。

    #diagnostics:稳定诊断

    pub fn sort_diagnostics(Array[Diagnostic]) -> Array[Diagnostic]
    pub fn render_text(String, String, Diagnostic) -> String
    pub fn render_json(Array[Diagnostic]) -> String

    人类输出包含文件、行列、稳定代码、源行和插入符;JSON 输出包含数字范围与可选建议编辑。

    #edit:验证后的最小补丁

    pub fn validate_edits(String, Array[TextEdit]) -> Result[Array[TextEdit], EditError]
    pub fn apply_edits(String, Array[TextEdit]) -> Result[String, EditError]
    pub fn set_value(String, Array[String], String) -> Result[String, OperationError]
    pub fn remove_key(String, Array[String]) -> Result[String, OperationError]

    范围是 UTF-8 字节偏移。重叠、越界、歧义路径、不安全插入和重解析失败均返回错误;API 不会在失败时暴露候选文本。

    #workbench:纯命令层

    pub fn parse_args(Array[String]) -> Result[Command, CommandError]
    pub fn run(Command, String) -> RunResult
    pub fn usage() -> String

    run 不访问文件系统。RunResult.proposed_source 只在成功编辑后携带完整、已验证候选文本;是否执行 --write 由原生适配器决定。

    完整用法、验证数据和限制见 README。生成接口以各包的 pkg.generated.mbti 为准。

    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.

    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

    decode_tagged_json

    fn decode_tagged_json(source : String) -> Result[String, TomlError]

    Parses TOML and emits the tagged JSON representation required by toml-test decoders. Object keys are sorted recursively for stable output.

    encode_tagged_value

    fn encode_tagged_value(value : Value) -> String

    Encodes one already-parsed TOML value with the same deterministic tagged JSON contract as decode_tagged_json.

    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.