mymarkdown

My markdown
git clone https://git.grace.moe/mymarkdown
Log | Files | Refs

commit 030761391983961fe6f7218a5d7df909a29869fd
parent 627faa642d6d6a91925ad705440d210d25e6c897
Author: gracefu <81774659+gracefuu@users.noreply.github.com>
Date:   Wed, 21 May 2025 02:16:43 +0800

Remove dependency on ziggy, update tracy frames for AstGen2/3

Diffstat:
Mbuild.zig | 1-
Mbuild.zig.zon | 3---
Msrc/Ast.zig | 5-----
Msrc/AstGen.zig | 1-
Msrc/AstGen2.zig | 33++++++++++++++++++---------------
Msrc/AstGen3.zig | 125+++++++++++--------------------------------------------------------------------
Msrc/utils.zig | 64++--------------------------------------------------------------
7 files changed, 37 insertions(+), 195 deletions(-)

diff --git a/build.zig b/build.zig @@ -23,7 +23,6 @@ pub fn build(b: *std.Build) !void { .target = target, .optimize = optimize, }); - mymarkdown.addImport("ziggy", b.dependency("ziggy", .{}).module("ziggy")); mymarkdown.addImport("tracy", tracy.module("tracy")); const mymarkdown_cli = b.addModule("mymarkdown", .{ .root_source_file = b.path("src/main.zig"), diff --git a/build.zig.zon b/build.zig.zon @@ -4,9 +4,6 @@ .fingerprint = 0x680fc5b268bbdd89, // Changing this has security and trust implications. .minimum_zig_version = "0.14.0", .dependencies = .{ - .ziggy = .{ - .path = "../../../manual-software/ziggy", - }, .tracy = .{ .path = "../../../manual-software/tracy-zig", }, diff --git a/src/Ast.zig b/src/Ast.zig @@ -124,11 +124,6 @@ test "Tracking size of Error struct" { try std.testing.expectEqual(8, @sizeOf(Error)); } -pub const format = utils.ziggyFormat(@This(), .{ - .whitespace = .space_2, - .omit_top_level_curly = false, -}); - pub const Tagged = struct { nodes: []const Node.Tagged, errors: []const Error.Tagged, diff --git a/src/AstGen.zig b/src/AstGen.zig @@ -2,7 +2,6 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; -const ziggy = @import("ziggy"); const tracy = @import("tracy"); const utils = @import("utils.zig"); diff --git a/src/AstGen2.zig b/src/AstGen2.zig @@ -2,7 +2,6 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; -const ziggy = @import("ziggy"); const tracy = @import("tracy"); const utils = @import("utils.zig"); @@ -230,10 +229,6 @@ fn parseColumn( OutOfErrorIdx, OutOfMemory, }!Column { - // const tracy_frame = if (parsing_context == .block_context) tracy.trace(@src()); - // defer if (parsing_context == .block_context) tracy_frame.end(); - const tracy_frame = tracy.trace(@src()); - defer tracy_frame.end(); return self.parseColumnInline(parent_idx, parent_col, cursor_col, parsing_context); } @@ -328,6 +323,8 @@ inline fn parseColumnInline( var indentation_idx: Column = undefined; finish_parsing_block: { + const tracy_frame = tracy.trace(@src()); + defer tracy_frame.end(); switch (self.cursor[0]) { // Par-like repeatable markers inline '-', '.', ':', '#' => |m| { @@ -346,6 +343,7 @@ inline fn parseColumnInline( { if (marker_len + potential_task_item > std.math.maxInt(Column)) return error.IndentationTooLong; + tracy_frame.setName(@tagName(.task_item)); indentation_idx = try self.parseMarkerItem( .task_item, parent_idx, @@ -358,14 +356,16 @@ inline fn parseColumnInline( break :finish_parsing_block; } } + const tag = switch (m) { + '-' => .unordered_item, + '.' => .ordered_item, + ':' => .term_item, + '#' => .heading, + else => unreachable, + }; + tracy_frame.setName(@tagName(tag)); indentation_idx = try self.parseMarkerItem( - switch (m) { - '-' => .unordered_item, - '.' => .ordered_item, - ':' => .term_item, - '#' => .heading, - else => unreachable, - }, + tag, parent_idx, block_col, marker_len, @@ -378,6 +378,7 @@ inline fn parseColumnInline( // Div-like repeatable markers inline '+' => |m| { + tracy_frame.setName(@tagName(.elaboration)); const marker_len = try self.findMarkerEnd(m); indentation_idx = try self.parseMarkerItem( switch (m) { @@ -396,6 +397,7 @@ inline fn parseColumnInline( // Par-like single markers inline ';' => |m| { + tracy_frame.setName(@tagName(.paragraph)); indentation_idx = try self.parseMarkerItem( switch (m) { ';' => .paragraph, @@ -413,6 +415,7 @@ inline fn parseColumnInline( // Div-like single markers inline '>' => |m| { + tracy_frame.setName(@tagName(.quote)); indentation_idx = try self.parseMarkerItem( switch (m) { '>' => .quote, @@ -433,6 +436,7 @@ inline fn parseColumnInline( const after_stars = self.cursor[3..]; const skip_whitespace_idx = str.indexOfNone(after_stars, " \t") orelse unreachable; if (after_stars[skip_whitespace_idx] == '\n') { + tracy_frame.setName(@tagName(.thematic_break)); _ = try self.appendLeafNodeAtCursor(parent_idx, .thematic_break, 3); self.advanceCursor(3 + skip_whitespace_idx + 1); while (true) { @@ -467,6 +471,7 @@ inline fn parseColumnInline( // Handle paragraph switch (parsing_context) { .inline_context => { + tracy_frame.setName(@tagName(.text)); if (parsed_first_paragraph_for_inline_context) { try self.appendNodeErrorAtCursor(.unexpected_block_in_inline_context, self.nextNodeIdx()); indentation_idx = try self.parseParagraph(.space_text, parent_idx, parent_col, block_col); @@ -476,6 +481,7 @@ inline fn parseColumnInline( } }, .block_context => { + tracy_frame.setName(@tagName(.paragraph)); const paragraph_idx = try self.appendContainerNodeAtCursor(parent_idx, .paragraph); indentation_idx = try self.parseParagraph(.text, paragraph_idx, parent_col, block_col); }, @@ -754,9 +760,6 @@ fn parseParagraph( parent_col: Column, block_col: Column, ) !Column { - // const tracy_frame = tracy.trace(@src()); - // defer tracy_frame.end(); - { const newline = str.indexOfChar(self.cursor, '\n') orelse unreachable; try self.insertTextLine(first_text_tag, .text, parent_idx, newline); diff --git a/src/AstGen3.zig b/src/AstGen3.zig @@ -2,7 +2,6 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; -const ziggy = @import("ziggy"); const tracy = @import("tracy"); const utils = @import("utils.zig"); @@ -28,11 +27,6 @@ nodes: std.ArrayListUnmanaged(Node), errors: std.ArrayListUnmanaged(Error), extra: std.ArrayListUnmanaged(u32), -// fn scannerPeek(self: AstGen) ?struct { IndentAlignment, PaddedSlice, StrOffset } { -// const alignment, const line = self.scanner.peek() orelse return null; -// return .{ alignment, line, line.ptr.calcOffsetTo(self.input.ptr) }; -// } - fn getNode(self: *const AstGen, idx: Node.Idx) *Node { @setRuntimeSafety(true); return &self.nodes.items[@intFromEnum(idx)]; @@ -201,8 +195,6 @@ fn parseColumn( OutOfNodeIdx, OutOfErrorIdx, } || Allocator.Error)!void { - const tracy_frame = tracy.trace(@src()); - defer tracy_frame.end(); return self.parseColumnImpl(parent_idx); } @@ -215,10 +207,12 @@ fn parseInlineColumn( self: *AstGen, parent_idx: Node.Idx, ) !void { - const tracy_frame = tracy.trace(@src()); - defer tracy_frame.end(); // TODO: Inline parsing next_paragraph: { + if (self.scanner.peek() == null) return; + const tracy_frame = tracy.trace(@src()); + defer tracy_frame.end(); + tracy_frame.setName(@tagName(.text)); if (self.scanner.peek()) |peek| { const alignment, const line = peek; try self.insertTextLine(.text, .text, parent_idx, line); @@ -243,6 +237,10 @@ fn parseInlineColumn( if (line.len != line.indexOfNotSpaceOrTab()) break; self.scanner.advance(); } + if (self.scanner.peek() == null) return; + const tracy_frame = tracy.trace(@src()); + defer tracy_frame.end(); + tracy_frame.setName(@tagName(.text)); if (self.scanner.peek()) |peek| { const alignment, const line = peek; try self.insertTextLine(.space_text, .text, parent_idx, line); @@ -282,6 +280,8 @@ inline fn parseColumnImpl( continue :parsing_blocks; } + const tracy_frame = tracy.trace(@src()); + defer tracy_frame.end(); // Parse one block and get its id so we can attach an error to it if the block was misaligned. const block_idx: Node.Idx = block_idx: { switch (line.index(0)) { @@ -300,6 +300,7 @@ inline fn parseColumnImpl( line.index(marker_len + potential_task_item - 2) == 'X') and std.mem.allEqual(u8, line.toUnpaddedSlice()[marker_len .. marker_len + potential_task_item - 3], ' ')) { + tracy_frame.setName(@tagName(.task_item)); const block_idx = try self.appendContainerNode(parent_idx, .task_item, line.ptr); _ = try self.appendLeafNode(block_idx, .marker, line.ptr, try castStrLen(marker_len + potential_task_item, error.MarkerTooLong)); try self.scanner.indent(.{ .skip = line_start + marker_len + potential_task_item }); @@ -308,6 +309,7 @@ inline fn parseColumnImpl( break :block_idx block_idx; } } + tracy_frame.setName(@tagName(@as(Node.Tag, @enumFromInt(m)))); const block_idx = try self.appendContainerNode(parent_idx, @enumFromInt(m), line.ptr); _ = try self.appendLeafNode(block_idx, .marker, line.ptr, try castStrLen(marker_len, error.MarkerTooLong)); try self.scanner.indent(.{ .skip = line_start + marker_len }); @@ -318,6 +320,7 @@ inline fn parseColumnImpl( // Div-like repeatable markers inline '+' => |m| { + tracy_frame.setName(@tagName(@as(Node.Tag, @enumFromInt(m)))); const marker_len = try castStrLen(line.indexOfNotChar(m), error.MarkerTooLong); const block_idx = try self.appendContainerNode(parent_idx, @enumFromInt(m), line.ptr); _ = try self.appendLeafNode(block_idx, .marker, line.ptr, marker_len); @@ -329,6 +332,7 @@ inline fn parseColumnImpl( // Par-like single markers inline ';' => |m| { + tracy_frame.setName(@tagName(@as(Node.Tag, @enumFromInt(m)))); const block_idx = try self.appendContainerNode(parent_idx, @enumFromInt(m), line.ptr); try self.scanner.indent(.{ .skip = line_start + 1 }); try self.parseInlineColumn(block_idx); @@ -338,6 +342,7 @@ inline fn parseColumnImpl( // Div-like single markers inline '>' => |m| { + tracy_frame.setName(@tagName(@as(Node.Tag, @enumFromInt(m)))); const block_idx = try self.appendContainerNode(parent_idx, @enumFromInt(m), line.ptr); try self.scanner.indent(.{ .skip = line_start + 1 }); try self.parseColumn(block_idx); @@ -351,6 +356,7 @@ inline fn parseColumnImpl( const skip_whitespace_idx = after_stars.indexOfNone(" \t"); if (after_stars.index(skip_whitespace_idx) == '\n') { self.scanner.advance(); + tracy_frame.setName(@tagName(.thematic_break)); break :block_idx try self.appendLeafNode(parent_idx, .thematic_break, line.ptr, 3); } } @@ -361,8 +367,7 @@ inline fn parseColumnImpl( // Parse paragraph { - // const tracy_frame = tracy.traceNamed(@src(), "paragraph"); - // defer tracy_frame.end(); + tracy_frame.setName(@tagName(.paragraph)); const paragraph_idx = try self.appendContainerNode(parent_idx, .paragraph, line.ptr); try self.insertTextLine(.text, .text, paragraph_idx, line); self.scanner.advance(); @@ -413,99 +418,3 @@ fn insertTextLine( } } } - -// fn parseParagraph( -// self: *AstGen, -// parent_idx: Node.Idx, -// ) !void { -// const tracy_frame = tracy.trace(@src()); -// defer tracy_frame.end(); - -// { -// const newline = str.indexOfChar(self.cursor, '\n') orelse unreachable; -// try self.insertTextLine(.text, .text, parent_idx, newline); -// self.advanceCursor(1); -// } - -// while (true) { -// if (self.cursor.len == 0) return 0; - -// const indentation_idx = str.indexOfNone(self.cursor, " \t") orelse unreachable; -// if (str.isAnyOf(self.cursor[indentation_idx], "-.:+>#;")) { -// // block line found, exit -// const verified_indentation_idx = std.mem.indexOfDiff( -// u8, -// self.cursor, -// self.indentation[0..block_col], -// ) orelse unreachable; -// if (verified_indentation_idx == block_col) { -// return @intCast(indentation_idx); -// } else { -// return @intCast(verified_indentation_idx); -// } -// } -// if (self.cursor[indentation_idx] == '*') { -// const after_stars = self.cursor[3..]; -// const skip_whitespace_idx = str.indexOfNone(after_stars, " \t") orelse unreachable; -// if (after_stars[skip_whitespace_idx] == '\n') { -// // block line found, exit -// const verified_indentation_idx = std.mem.indexOfDiff( -// u8, -// self.cursor, -// self.indentation[0..block_col], -// ) orelse unreachable; -// if (verified_indentation_idx == block_col) { -// return @intCast(indentation_idx); -// } else { -// return @intCast(verified_indentation_idx); -// } -// } -// } -// // empty line found, consume to next nonwhitespace and exit -// if (self.cursor[indentation_idx] == '\n') { -// self.advanceCursor(indentation_idx + 1); -// while (true) { -// if (self.cursor.len == 0) return 0; - -// const next_idx = str.indexOfNone(self.cursor, " \t") orelse unreachable; -// if (self.cursor[next_idx] == '\n') { -// self.advanceCursor(next_idx + 1); -// continue; -// } - -// const verified_indentation_idx = std.mem.indexOfDiff( -// u8, -// self.cursor, -// self.indentation[0..block_col], -// ) orelse unreachable; -// if (verified_indentation_idx == block_col) { -// return @intCast(next_idx); -// } else { -// return @intCast(verified_indentation_idx); -// } -// } -// } - -// // verify indentation -// const verified_indentation_idx = std.mem.indexOfDiff( -// u8, -// self.cursor, -// self.indentation[0..block_col], -// ) orelse unreachable; -// if (verified_indentation_idx == block_col) { -// self.advanceCursor(verified_indentation_idx); -// const newline = str.indexOfChar(self.cursor, '\n') orelse unreachable; -// try self.insertTextLine(.space_text, .text, parent_idx, newline); -// self.advanceCursor(1); -// } else if (verified_indentation_idx > parent_col) { -// self.advanceCursor(parent_col); -// try self.appendPointErrorAtCursor(.inconsistent_indentation, parent_idx); -// self.advanceCursor(indentation_idx - parent_col); -// const newline = str.indexOfChar(self.cursor, '\n') orelse unreachable; -// try self.insertTextLine(.space_text, .text, parent_idx, newline); -// self.advanceCursor(1); -// } else { -// return @intCast(verified_indentation_idx); -// } -// } -// } diff --git a/src/utils.zig b/src/utils.zig @@ -1,14 +1,13 @@ const std = @import("std"); -const ziggy = @import("ziggy"); - -pub fn NewType(comptime IntType_: type, comptime DummyType_: type) type { +pub fn NewType(comptime IntType_: type, comptime DummyType: type) type { return enum(IntType_) { root = 0, _, const Self = @This(); pub const IntType = IntType_; + const _DummyType = DummyType; pub fn next(self: @This()) ?@This() { if (@intFromEnum(self) == std.math.maxInt(IntType_)) @@ -19,26 +18,6 @@ pub fn NewType(comptime IntType_: type, comptime DummyType_: type) type { pub fn format(self: @This(), comptime _: []const u8, _: anytype, writer: anytype) !void { try writer.print("@enumFromInt({})", .{@intFromEnum(self)}); } - - pub const ziggy_options = struct { - const DummyType = DummyType_; - pub fn parse( - self: *ziggy.Parser, - first_tok: ziggy.Tokenizer.Token, - ) !Self { - return @enumFromInt(try self.parseValue(u32, first_tok)); - } - pub fn stringify( - self: Self, - opts: ziggy.serializer.StringifyOptions, - indent_level: usize, - depth: usize, - writer: anytype, - ) !void { - const serialized: u32 = @intFromEnum(self); - return ziggy.serializer.stringifyInner(serialized, opts, indent_level, depth, writer); - } - }; }; } @@ -98,32 +77,6 @@ pub fn Packed(comptime Tagged_: type) type { } } - pub const ziggy_options = struct { - pub fn parse( - self: *ziggy.Parser, - first_tok: ziggy.Tokenizer.Token, - ) !Self { - return .fromNode( - try self.parseValue(Tagged_, first_tok), - ); - } - pub fn stringify( - self: Self, - opts: ziggy.serializer.StringifyOptions, - indent_level: usize, - depth: usize, - writer: anytype, - ) !void { - return ziggy.serializer.stringifyInner( - self.toTagged(), - opts, - indent_level, - depth, - writer, - ); - } - }; - /// May not exist, but we can define it anyway thanks to lazy decl analysis. pub const Idx = Tagged_.Idx; /// May not exist, but we can define it anyway thanks to lazy decl analysis. @@ -141,19 +94,6 @@ pub fn Packed(comptime Tagged_: type) type { }; } -fn ZiggyFormat(comptime T: type, opts: ziggy.serializer.StringifyOptions) type { - return struct { - pub fn format(self: T, comptime _: []const u8, _: anytype, writer: anytype) !void { - _ = try writer.writeAll("."); - try ziggy.stringify(self, opts, writer); - } - }; -} - -pub fn ziggyFormat(comptime T: type, opts: ziggy.serializer.StringifyOptions) @TypeOf(ZiggyFormat(T, opts).format) { - return ZiggyFormat(T, opts).format; -} - fn UnionFormat(comptime T: type) type { return struct { pub fn format(self: T, comptime _: []const u8, _: anytype, writer: anytype) !void {