robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

bfredlMerge pull request #38675 from bfredl/errdefer7673adc952

master
7.0 KiB221 linesraw
1---@diagnostic disable: no-unknown
2local mpack = vim.mpack
3
4assert(#arg == 5)
5local input = assert(io.open(arg[1], 'rb'))
6local call_output = assert(io.open(arg[2], 'wb'))
7local remote_output = assert(io.open(arg[3], 'wb'))
8local metadata_output = assert(io.open(arg[4], 'wb'))
9local client_output = assert(io.open(arg[5], 'wb'))
10
11local c_grammar = require('gen.c_grammar')
12local events = c_grammar.grammar:match(input:read('*all'))
13
14local hashy = require 'gen.hashy'
15
16local function write_signature(output, ev, prefix, notype)
17  output:write('(' .. prefix)
18  if prefix == '' and #ev.parameters == 0 then
19    output:write('void')
20  end
21  for j = 1, #ev.parameters do
22    if j > 1 or prefix ~= '' then
23      output:write(', ')
24    end
25    local param = ev.parameters[j]
26    if not notype then
27      output:write(param[1] .. ' ')
28    end
29    output:write(param[2])
30  end
31  output:write(')')
32end
33
34local function write_arglist(output, ev)
35  if #ev.parameters == 0 then
36    return
37  end
38  output:write('  MAXSIZE_TEMP_ARRAY(args, ' .. #ev.parameters .. ');\n')
39  for j = 1, #ev.parameters do
40    local param = ev.parameters[j]
41    local kind = string.upper(param[1])
42    output:write('  ADD_C(args, ')
43    output:write(kind .. '_OBJ(' .. param[2] .. ')')
44    output:write(');\n')
45  end
46end
47
48local function call_ui_event_method(output, ev)
49  output:write('void ui_client_event_' .. ev.name .. '(Array args)\n{\n')
50
51  local hlattrs_args_count = 0
52  if #ev.parameters > 0 then
53    output:write('  if (args.size < ' .. #ev.parameters)
54    for j = 1, #ev.parameters do
55      local kind = ev.parameters[j][1]
56      if kind ~= 'Object' then
57        if kind == 'HlAttrs' then
58          kind = 'Dict'
59        end
60        output:write('\n      || args.items[' .. (j - 1) .. '].type != kObjectType' .. kind .. '')
61      end
62    end
63    output:write(') {\n')
64    output:write('    ELOG("Error handling ui event \'' .. ev.name .. '\'");\n')
65    output:write('    return;\n')
66    output:write('  }\n')
67  end
68
69  for j = 1, #ev.parameters do
70    local param = ev.parameters[j]
71    local kind = param[1]
72    output:write('  ' .. kind .. ' arg_' .. j .. ' = ')
73    if kind == 'HlAttrs' then
74      -- The first HlAttrs argument is rgb_attrs and second is cterm_attrs
75      output:write(
76        'ui_client_dict2hlattrs(args.items['
77          .. (j - 1)
78          .. '].data.dict, '
79          .. (hlattrs_args_count == 0 and 'true' or 'false')
80          .. ');\n'
81      )
82      hlattrs_args_count = hlattrs_args_count + 1
83    elseif kind == 'Object' then
84      output:write('args.items[' .. (j - 1) .. '];\n')
85    elseif kind == 'Window' then
86      output:write('(Window)args.items[' .. (j - 1) .. '].data.integer;\n')
87    else
88      output:write('args.items[' .. (j - 1) .. '].data.' .. string.lower(kind) .. ';\n')
89    end
90  end
91
92  output:write('  tui_' .. ev.name .. '(tui')
93  for j = 1, #ev.parameters do
94    output:write(', arg_' .. j)
95  end
96  output:write(');\n')
97
98  output:write('}\n\n')
99end
100
101events = vim.tbl_filter(function(ev)
102  return ev[1] ~= 'empty' and ev[1] ~= 'preproc'
103end, events)
104
105for i = 1, #events do
106  local ev = events[i]
107  assert(ev.return_type == 'void')
108
109  -- Allow unstabilized events starting with "_". Compare "nvim__" for methods.
110  if ev.since == nil and not ev.noexport and not vim.startswith(ev.name, '_') then
111    print('Ui event ' .. ev.name .. ' lacks since field.\n')
112    os.exit(1)
113  end
114  ev.since = tonumber(ev.since)
115
116  local args = #ev.parameters > 0 and 'args' or 'noargs'
117  if not ev.remote_only then
118    if not ev.remote_impl and not ev.noexport then
119      remote_output:write('void remote_ui_' .. ev.name)
120      write_signature(remote_output, ev, 'RemoteUI *ui')
121      remote_output:write('\n{\n')
122      write_arglist(remote_output, ev)
123      remote_output:write('  push_call(ui, "' .. ev.name .. '", ' .. args .. ');\n')
124      remote_output:write('}\n\n')
125    end
126  end
127
128  if not (ev.remote_only and ev.remote_impl) then
129    call_output:write('void ui_call_' .. ev.name)
130    write_signature(call_output, ev, '')
131    call_output:write('\n{\n')
132    if ev.remote_only then
133      -- Lua callbacks may emit other events or the same event again. Avoid the latter
134      -- by adding a recursion guard to each generated function that may call a Lua callback.
135      call_output:write('  static bool entered = false;\n')
136      call_output:write('  if (entered) {\n')
137      call_output:write('    return;\n')
138      call_output:write('  }\n')
139      call_output:write('  entered = true;\n')
140      write_arglist(call_output, ev)
141      call_output:write(('  ui_call_event("%s", %s)'):format(ev.name, args))
142      call_output:write(';\n  entered = false;\n')
143    elseif ev.compositor_impl then
144      call_output:write('  ui_comp_' .. ev.name)
145      write_signature(call_output, ev, '', true)
146      call_output:write(';\n')
147      call_output:write('  UI_CALL')
148      write_signature(call_output, ev, '!ui->composed, ' .. ev.name .. ', ui', true)
149      call_output:write(';\n')
150    else
151      call_output:write('  UI_CALL')
152      write_signature(call_output, ev, 'true, ' .. ev.name .. ', ui', true)
153      call_output:write(';\n')
154    end
155    call_output:write('}\n\n')
156  end
157
158  if ev.compositor_impl then
159    call_output:write('void ui_composed_call_' .. ev.name)
160    write_signature(call_output, ev, '')
161    call_output:write('\n{\n')
162    call_output:write('  UI_CALL')
163    write_signature(call_output, ev, 'ui->composed, ' .. ev.name .. ', ui', true)
164    call_output:write(';\n')
165    call_output:write('}\n\n')
166  end
167
168  if (not ev.remote_only) and not ev.noexport and not ev.client_impl and not ev.client_ignore then
169    call_ui_event_method(client_output, ev)
170  end
171end
172
173local client_events = {}
174for _, ev in ipairs(events) do
175  if (not ev.noexport) and ((not ev.remote_only) or ev.client_impl) and not ev.client_ignore then
176    client_events[ev.name] = ev
177  end
178end
179
180local hashorder, hashfun = hashy.hashy_hash(
181  'ui_client_handler',
182  vim.tbl_keys(client_events),
183  function(idx)
184    return 'event_handlers[' .. idx .. '].name'
185  end
186)
187
188client_output:write('static const UIClientHandler event_handlers[] = {\n')
189
190for _, name in ipairs(hashorder) do
191  client_output:write('  { .name = "' .. name .. '", .fn = ui_client_event_' .. name .. '},\n')
192end
193
194client_output:write('\n};\n\n')
195client_output:write(hashfun)
196
197call_output:close()
198remote_output:close()
199client_output:close()
200
201-- don't expose internal attributes like "impl_name" in public metadata
202local exported_attributes = { 'name', 'parameters', 'since', 'deprecated_since' }
203local exported_events = {}
204for _, ev in ipairs(events) do
205  local ev_exported = {}
206  for _, attr in ipairs(exported_attributes) do
207    ev_exported[attr] = ev[attr]
208  end
209  for _, p in ipairs(ev_exported.parameters) do
210    if p[1] == 'HlAttrs' or p[1] == 'Dict' then
211      -- TODO(justinmk): for back-compat, but do clients actually look at this?
212      p[1] = 'Dictionary'
213    end
214  end
215  if not ev.noexport and not vim.startswith(ev.name, '_') then
216    exported_events[#exported_events + 1] = ev_exported
217  end
218end
219
220metadata_output:write(mpack.encode(exported_events))
221metadata_output:close()