lint.lua revision 1.4
1#! /usr/bin/lua
2-- $NetBSD: lint.lua,v 1.4 2021/06/13 19:25:08 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
27
28local function new_errors()
29  local errors = {}
30  errors.add = function(self, fmt, ...)
31    table.insert(self, string.format(fmt, ...))
32  end
33  errors.print = function(self)
34    for _, msg in ipairs(self) do
35      print(msg)
36    end
37  end
38  return errors
39end
40
41
42local function num(s)
43  if s == nil then return nil end
44  return tonumber(s)
45end
46
47
48-- After each macro ARGC, there must be the corresponding macros for ARG.
49local function check_args(errors)
50  local fname = "curses_commands.c"
51  local lines = load_lines(fname)
52  local curr_argc, curr_arg ---@type number|nil, number|nil
53
54  for lineno, line in ipairs(lines) do
55
56    local line_argc = num(line:match("^\tARGC%((%d)"))
57    local line_arg = line:match("^\tARG_[%w_]+%(")
58
59    if line_argc and line_argc > 0 then
60      curr_argc, curr_arg = line_argc, 0
61    end
62
63    if line_arg and not curr_arg then
64      errors:add("%s:%d: ARG without preceding ARGC", fname, lineno)
65    end
66
67    if not line_arg and curr_arg and not line_argc then
68      errors:add("%s:%d: expecting ARG %d, got %s",
69        fname, lineno, curr_arg, line)
70      curr_argc, curr_arg = nil, nil
71    end
72
73    if line_arg and curr_arg then
74      curr_arg = curr_arg + 1
75      if curr_arg == curr_argc then
76        curr_argc, curr_arg = nil, nil
77      end
78    end
79  end
80end
81
82
83local function main(arg)
84  local errors = new_errors()
85  check_args(errors)
86  errors:print()
87  return #errors == 0
88end
89
90
91os.exit(main(arg))
92