~andreafeletto/zig-scfg

8e20d767feac2cdeb452f3f4cf03f3a3e654a423 — Andrea Feletto 1 year, 7 months ago 83fd4ea
remove null-termination requirement
5 files changed, 16 insertions(+), 14 deletions(-)

M LICENSE
M README.md
M example.zig
M scfg.zig
M src/Parser.zig
M LICENSE => LICENSE +1 -1
@@ 1,6 1,6 @@
MIT License

Copyright (c) 2022 Andrea Feletto
Copyright (c) 2023 Andrea Feletto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

M README.md => README.md +4 -4
@@ 47,10 47,10 @@ pub fn main() !void {
    defer arena.deinit();
    const allocator = arena.allocator();

    const file = try std.fs.cwd().openFile("example.scfg", .{});
    // `source` must be a null-terminated string
    const source = try file.readToEndAllocOptions(
        allocator, 1_000_000, null, @alignOf(u8), 0
    const source = try std.fs.cwd().readFileAlloc(
        allocator,
        "example.scfg",
        1_000_000,
    );

    const root = try scfg.parse(allocator, source);

M example.zig => example.zig +5 -3
@@ 6,9 6,11 @@ pub fn main() !void {
    defer arena.deinit();
    const allocator = arena.allocator();

    const file = try std.fs.cwd().openFile("example.scfg", .{});
    // `source` must be a null-terminated string
    const source = try file.readToEndAllocOptions(allocator, 1_000_000, null, @alignOf(u8), 0);
    const source = try std.fs.cwd().readFileAlloc(
        allocator,
        "example.scfg",
        1_000_000,
    );

    const root = try scfg.parse(allocator, source);
    std.log.info("identifier of the first directive: {s}", .{root[0].name});

M scfg.zig => scfg.zig +2 -2
@@ 13,13 13,13 @@ const Directive = struct {
    blocks: []Block,
};

pub fn parse(allocator: Allocator, source: [:0]const u8) !Block {
pub fn parse(allocator: Allocator, source: []const u8) !Block {
    var tokenizer = try Tokenizer.init(source);
    var parser: Parser = .{ .allocator = allocator, .source = source };

    while (true) {
        const token = tokenizer.next();
        try parser.feed(&token);
        try parser.feed(token);
        if (token.tag == .eof) {
            break;
        }

M src/Parser.zig => src/Parser.zig +4 -4
@@ 18,7 18,7 @@ const Directive = struct {
const Block = ArrayListUnmanaged(usize);

allocator: mem.Allocator,
source: [:0]const u8,
source: []const u8,

state: enum { new, update } = .new,
directive_idx: usize = 0,


@@ 27,7 27,7 @@ directives: ArrayListUnmanaged(Directive) = .{},
blocks: ArrayListUnmanaged(Block) = .{},
path: ArrayListUnmanaged(usize) = .{},

pub fn feed(self: *Parser, token: *const Token) !void {
pub fn feed(self: *Parser, token: Token) !void {
    switch (self.state) {
        .new => switch (token.tag) {
            .newline => {},


@@ 115,7 115,7 @@ test "parser: minimal" {

    while (true) {
        const token = tokenizer.next();
        try parser.feed(&token);
        try parser.feed(token);
        if (token.tag == .eof) break;
    }



@@ 145,7 145,7 @@ test "parser: directive with a block" {

    while (true) {
        const token = tokenizer.next();
        try parser.feed(&token);
        try parser.feed(token);
        if (token.tag == .eof) break;
    }