// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Lex and parse a corpus of real MIB files, and report what cannot be read. //! //! This is how the lexical rules in `Lexer.zig` were settled. The standard //! says one thing, net-snmp's defaults say another, and 2,852 published files //! say several more -- and the only way to find out which readings matter is //! to run them all and look at what breaks. //! //! ```console //! $ zig build lexcheck -- /path/to/mibs/*.txt //! 65 files, 63220 tokens, 0 files with invalid tokens //! ``` //! //! A file that will not lex is named with the line and the offending text, so //! that it can be looked at rather than merely counted -- which matters, //! because every one found so far was either a rule worth implementing or a //! genuinely broken file that net-snmp rejects too. const std = @import("std"); const Io = std.Io; const smi = @import("smi"); pub fn main(init: std.process.Init) !void { const io = init.io; const gpa = init.arena.allocator(); var ob: [64 * 1024]u8 = undefined; var out: Io.File.Writer = .init(.stdout(), io, &ob); defer out.interface.flush() catch {}; const args = try init.minimal.args.toSlice(gpa); var total_files: usize = 0; var total_tokens: usize = 0; var files_with_invalid: usize = 0; var parsed: usize = 0; var failed_parse: usize = 0; var total_assignments: usize = 0; var empty: usize = 0; for (args[1..]) |path| { var file = Io.Dir.cwd().openFile(io, path, .{}) catch continue; defer file.close(io); var buf: [4096]u8 = undefined; var reader = file.readerStreaming(io, &buf); const source = reader.interface.allocRemaining(gpa, .unlimited) catch continue; total_files += 1; // Parse it too, and report where that fails. A file that lexes and // does not parse is the interesting case: it means the grammar here // is missing something real rather than that the file is rubbish. { var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); var diagnostic: ?smi.Parser.Diagnostic = null; if (smi.Parser.parseWithDiagnostic(arena.allocator(), source, .{}, &diagnostic)) |m| { total_assignments += m.assignments.len; parsed += 1; } else |err| switch (err) { // A file with no module in it is not a failure. error.NoModule => empty += 1, else => { failed_parse += 1; if (diagnostic) |d| { try out.interface.print("{s}: line {d}: {s}\n", .{ std.fs.path.basename(path), d.line(source), d.message, }); } }, } } var l: smi.Lexer = .init(source, .{}); var invalid: usize = 0; var first_invalid: ?smi.Token = null; while (true) { const t = l.next(); if (t.tag == .end_of_file) break; // Stop where a parser would. A MIB may have anything after END -- // one of Cisco's has an HTML footer -- and reporting that as a // lexical failure would be measuring the wrong thing. if (smi.Lexer.isModuleEnd(t, source)) break; total_tokens += 1; if (t.tag == .invalid) { invalid += 1; if (first_invalid == null) first_invalid = t; } } if (invalid != 0) { files_with_invalid += 1; const t = first_invalid.?; // Which line, and what the offending text is. var line: usize = 1; for (source[0..t.start]) |c| { if (c == '\n') line += 1; } const shown = source[t.start..@min(t.end + 20, source.len)]; try out.interface.print("{s}: {d} invalid, first at line {d}: {s}\n", .{ std.fs.path.basename(path), invalid, line, std.mem.trim(u8, shown[0..@min(shown.len, 30)], " \t\r\n"), }); } } try out.interface.print( "\n{d} files, {d} tokens, {d} with invalid tokens; " ++ "{d} parsed ({d} assignments), {d} failed, {d} with no module\n", .{ total_files, total_tokens, files_with_invalid, parsed, total_assignments, failed_parse, empty }, ); }