authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-25 18:15:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-25 18:17:18-07:00
log48e0b89f2c114007ac3ffa5aec6db9f2ff8eda4e
tree6d3dfa80f62a33b2f474d3dd747fdcf689cb618d
parent5ab185460282eedb6e1d673041a4579eaacbc6ad

remove tools/extract-grammar.zig

This is not a sound way to check Zig's grammar against the spec. This is a partial revert of f8f1c6ac06636d816e5dff205d6713a337df387d.

2 files changed, 0 insertions(+), 99 deletions(-)

test/standalone.zig-1
......@@ -62,7 +62,6 @@ pub const simple_cases = [_]SimpleCase{
6262
6363 // Ensure the development tools are buildable. Alphabetically sorted.
6464 // No need to build `tools/spirv/grammar.zig`.
65 .{ .src_path = "tools/extract-grammar.zig" },
6665 .{ .src_path = "tools/gen_outline_atomics.zig" },
6766 .{ .src_path = "tools/gen_spirv_spec.zig" },
6867 .{ .src_path = "tools/gen_stubs.zig" },
tools/extract-grammar.zig deleted-98
......@@ -1,98 +0,0 @@
1//! Extract the "de facto" Zig Grammar from the parser in lib/std/zig/parse.zig.
2//!
3//! The generated file must be edited by hand, in order to remove normal doc-comments.
4
5const std = @import("std");
6const fs = std.fs;
7const heap = std.heap;
8const io = std.io;
9const mem = std.mem;
10const process = std.process;
11const zig = std.zig;
12
13const Buffer = struct {
14 const buf_size = 4096;
15
16 buf: [buf_size]u8 = undefined,
17 pos: usize = 0,
18
19 pub fn append(self: *Buffer, src: []const u8) !void {
20 if (self.pos + src.len > buf_size) {
21 return error.BufferOverflow;
22 }
23
24 mem.copy(u8, self.buf[self.pos..buf_size], src);
25 self.pos += src.len;
26 }
27
28 pub fn reset(self: *Buffer) void {
29 self.pos = 0;
30 }
31
32 pub fn slice(self: *Buffer) []const u8 {
33 return self.buf[0..self.pos];
34 }
35};
36
37/// There are many assumptions in the entire codebase that Zig source files can
38/// be byte-indexed with a u32 integer.
39const max_src_size = std.math.maxInt(u32);
40
41pub fn main() !void {
42 const stdout_wr = io.getStdOut().writer();
43 var arena = heap.ArenaAllocator.init(heap.page_allocator);
44 defer arena.deinit(); // NOTE(mperillo): Can be removed.
45 const allocator = arena.allocator();
46
47 var args_it = try process.argsWithAllocator(allocator);
48 _ = args_it.skip(); // it is safe to ignore
49
50 const path = args_it.next() orelse return error.SourceFileRequired;
51 const src = try read(path, allocator);
52
53 var tokenizer = zig.Tokenizer.init(src);
54 var buf: Buffer = Buffer{};
55 while (true) {
56 const token = tokenizer.next();
57 switch (token.tag) {
58 .eof => break,
59 .doc_comment => {
60 const line = blk: {
61 // Strip leading whitespace.
62 const len = token.loc.end - token.loc.start;
63 break :blk if (len == 3) src[token.loc.start + 3 .. token.loc.end] else src[token.loc.start + 4 .. token.loc.end];
64 };
65
66 try buf.append(line);
67 try buf.append("\n");
68 },
69 .keyword_fn => {
70 const doc = buf.slice();
71 buf.reset();
72
73 // Check if doc contains a PEG grammar block, so that normal
74 // doc-comments are ignored.
75 if (mem.indexOf(u8, doc, "<-") != null) {
76 // Separate each doc with an empty line. This in turn will
77 // ensure that rules are separate by an empty line.
78 try stdout_wr.print("{s}\n", .{doc});
79 }
80 },
81 else => {},
82 }
83 }
84}
85
86fn read(path: []const u8, allocator: mem.Allocator) ![:0]const u8 {
87 var f = try fs.cwd().openFile(path, .{ .mode = .read_only });
88 defer f.close();
89
90 const st = try f.stat();
91 if (st.size > max_src_size) return error.FileTooBig;
92
93 const src = try allocator.allocSentinel(u8, @as(usize, @intCast(st.size)), 0);
94 const n = try f.readAll(src);
95 if (n != st.size) return error.UnexpectedEndOfFile;
96
97 return src;
98}