| ... | @@ -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 | |
| 5 | const std = @import("std"); |
| 6 | const fs = std.fs; |
| 7 | const heap = std.heap; |
| 8 | const io = std.io; |
| 9 | const mem = std.mem; |
| 10 | const process = std.process; |
| 11 | const zig = std.zig; |
| 12 | |
| 13 | const 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. |
| 39 | const max_src_size = std.math.maxInt(u32); |
| 40 | |
| 41 | pub 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 | |
| 86 | fn 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 | } |