Code Monkey home page Code Monkey logo

zig-toml's Introduction

zig-toml

Zig TOML v1.0.0 parser.

This is a top-down LL parser that parses directly into Zig structs.

Features

  • TOML Syntax
    • Integers, hexadecimal, octal, and binary numbers
    • Floats
    • Booleans
    • Comments
    • Arrays
    • Tables
    • Array of Tables
    • Inline Table
    • Single-line strings
    • String escapes (also unicode)
    • Multi-line strings
    • Multi-line string leading space trimming
    • Trailing backslash in multi-line strings
    • Date, time, date-time, time offset
  • Struct mapping
    • Mapping to structs
    • Mapping to enums
    • Mapping to slices
    • Mapping to arrays
    • Mapping to pointers
    • Mapping to integer and floats with lower bit number than defined by TOML, i.e. i16, f32.
    • Mapping to optional fields

Example

See example1.zig for the complete code that parses example.toml

Run it with zig build examples

// .... 

const Address = struct {
    port: i64,
    host: []const u8,
};

const Config = struct {
    master: bool,
    expires_at: toml.DateTime,
    description: []const u8,

    local: *Address,
    peers: []const Address,
};

pub fn main() anyerror!void {
    var parser = toml.Parser(Config).init(allocator);
    defer parser.deinit();

    var result = try parser.parseFile("./examples/example1.toml");
    defer result.deinit();

    var config = result.value;
    std.debug.print("{s}\nlocal address: {s}:{}\n", .{ config.description, config.local.host, config.local.port });
    std.debug.print("peer0: {s}:{}\n", .{ config.peers[0].host, config.peers[0].port });
}

Error Handling

TODO

License

MIT

zig-toml's People

Contributors

sam701 avatar iacore avatar spadi0 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.