1#! /usr/bin/lua 2-- $NetBSD: lint.lua,v 1.6 2021/06/13 19:50:18 rillig Exp $ 3 4--[[ 5 6usage: lua ./lint.lua 7 8Check that the argument handling code does not contain unintended 9inconsistencies. 10 11]] 12 13---@return string[] 14local function load_lines(fname) 15 local lines = {} 16 17 local f = assert(io.open(fname, "r")) 18 for line in f:lines() do 19 table.insert(lines, line) 20 end 21 22 f:close() 23 24 return lines 25end 26 27local had_errors = false 28local function print_error(fmt, ...) 29 print(fmt:format(...)) 30 had_errors = true 31end 32 33 34local function num(s) 35 if s == nil then return nil end 36 return tonumber(s) 37end 38 39 40-- After each macro ARGC, there must be the corresponding macros for ARG. 41local function check_args() 42 local fname = "curses_commands.c" 43 local lines = load_lines(fname) 44 local curr_argc, curr_arg ---@type number|nil, number|nil 45 46 for lineno, line in ipairs(lines) do 47 48 local line_argc = num(line:match("^\tARGC%((%d+)")) 49 if line_argc and line_argc > 0 then 50 curr_argc, curr_arg = line_argc, 0 51 goto next 52 end 53 54 local line_arg = line:match("^\tARG_[%w_]+%(") 55 if line_arg and curr_arg then 56 curr_arg = curr_arg + 1 57 if curr_arg == curr_argc then 58 curr_argc, curr_arg = nil, nil 59 end 60 elseif line_arg then 61 print_error("%s:%d: ARG without preceding ARGC", fname, lineno) 62 elseif curr_arg then 63 print_error("%s:%d: expecting ARG %d, got %s", 64 fname, lineno, curr_arg, line) 65 curr_argc, curr_arg = nil, nil 66 end 67 68 ::next:: 69 end 70end 71 72check_args() 73os.exit(not had_errors) 74