2014-04-10 11:51:44 -07:00
|
|
|
lpeg = require('lpeg')
|
2016-04-13 05:21:32 -07:00
|
|
|
mpack = require('mpack')
|
2014-04-10 11:51:44 -07:00
|
|
|
|
2014-05-07 13:59:16 -07:00
|
|
|
-- lpeg grammar for building api metadata from a set of header files. It
|
|
|
|
-- ignores comments and preprocessor commands and parses a very small subset
|
|
|
|
-- of C prototypes with a limited set of types
|
2014-04-10 11:51:44 -07:00
|
|
|
P, R, S = lpeg.P, lpeg.R, lpeg.S
|
|
|
|
C, Ct, Cc, Cg = lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cg
|
|
|
|
|
|
|
|
any = P(1) -- (consume one character)
|
|
|
|
letter = R('az', 'AZ') + S('_$')
|
2016-10-24 08:48:24 -07:00
|
|
|
num = R('09')
|
|
|
|
alpha = letter + num
|
2014-12-08 12:19:12 -07:00
|
|
|
nl = P('\r\n') + P('\n')
|
2014-04-10 11:51:44 -07:00
|
|
|
not_nl = any - nl
|
|
|
|
ws = S(' \t') + nl
|
|
|
|
fill = ws ^ 0
|
|
|
|
c_comment = P('//') * (not_nl ^ 0)
|
|
|
|
c_preproc = P('#') * (not_nl ^ 0)
|
2014-09-12 07:24:01 -07:00
|
|
|
typed_container =
|
|
|
|
(P('ArrayOf(') + P('DictionaryOf(')) * ((any - P(')')) ^ 1) * P(')')
|
|
|
|
c_id = (
|
|
|
|
typed_container +
|
|
|
|
(letter * (alpha ^ 0))
|
|
|
|
)
|
2014-04-10 11:51:44 -07:00
|
|
|
c_void = P('void')
|
|
|
|
c_param_type = (
|
2014-05-07 13:59:16 -07:00
|
|
|
((P('Error') * fill * P('*') * fill) * Cc('error')) +
|
2014-05-16 04:00:31 -07:00
|
|
|
(C(c_id) * (ws ^ 1))
|
2014-04-10 11:51:44 -07:00
|
|
|
)
|
2014-05-07 13:59:16 -07:00
|
|
|
c_type = (C(c_void) * (ws ^ 1)) + c_param_type
|
2014-04-10 11:51:44 -07:00
|
|
|
c_param = Ct(c_param_type * C(c_id))
|
|
|
|
c_param_list = c_param * (fill * (P(',') * fill * c_param) ^ 0)
|
|
|
|
c_params = Ct(c_void + c_param_list)
|
|
|
|
c_proto = Ct(
|
2014-05-07 13:59:16 -07:00
|
|
|
Cg(c_type, 'return_type') * Cg(c_id, 'name') *
|
|
|
|
fill * P('(') * fill * Cg(c_params, 'parameters') * fill * P(')') *
|
2015-07-24 05:55:31 -07:00
|
|
|
Cg(Cc(false), 'async') *
|
2016-10-24 08:48:24 -07:00
|
|
|
(fill * Cg((P('FUNC_API_SINCE(') * C(num ^ 1)) * P(')'), 'since') ^ -1) *
|
2016-04-14 11:47:01 -07:00
|
|
|
(fill * Cg((P('FUNC_API_ASYNC') * Cc(true)), 'async') ^ -1) *
|
|
|
|
(fill * Cg((P('FUNC_API_NOEXPORT') * Cc(true)), 'noexport') ^ -1) *
|
2016-06-20 05:40:57 -07:00
|
|
|
(fill * Cg((P('FUNC_API_NOEVAL') * Cc(true)), 'noeval') ^ -1) *
|
2014-04-10 11:51:44 -07:00
|
|
|
fill * P(';')
|
|
|
|
)
|
|
|
|
grammar = Ct((c_proto + c_comment + c_preproc + ws) ^ 1)
|
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
-- we need at least 4 arguments since the last two are output files
|
|
|
|
assert(#arg >= 3)
|
2014-09-11 17:56:52 -07:00
|
|
|
functions = {}
|
2014-09-07 16:40:07 -07:00
|
|
|
|
2016-09-14 02:15:01 -07:00
|
|
|
local nvimsrcdir = arg[1]
|
|
|
|
package.path = nvimsrcdir .. '/?.lua;' .. package.path
|
2016-06-28 12:45:19 -07:00
|
|
|
|
2016-06-01 16:14:09 -07:00
|
|
|
-- names of all headers relative to the source root (for inclusion in the
|
2014-05-07 13:59:16 -07:00
|
|
|
-- generated file)
|
|
|
|
headers = {}
|
2016-10-24 08:48:24 -07:00
|
|
|
|
2016-09-27 07:54:08 -07:00
|
|
|
-- output h file with generated dispatch functions
|
|
|
|
dispatch_outputf = arg[#arg-2]
|
|
|
|
-- output h file with packed metadata
|
|
|
|
funcs_metadata_outputf = arg[#arg-1]
|
|
|
|
-- output metadata mpack file, for use by other build scripts
|
2016-06-18 03:06:50 -07:00
|
|
|
mpack_outputf = arg[#arg]
|
2014-04-10 11:51:44 -07:00
|
|
|
|
2016-09-14 02:15:01 -07:00
|
|
|
-- set of function names, used to detect duplicates
|
|
|
|
function_names = {}
|
|
|
|
|
2014-05-07 13:59:16 -07:00
|
|
|
-- read each input file, parse and append to the api metadata
|
2016-09-27 07:54:08 -07:00
|
|
|
for i = 2, #arg - 3 do
|
2014-05-07 13:59:16 -07:00
|
|
|
local full_path = arg[i]
|
|
|
|
local parts = {}
|
|
|
|
for part in string.gmatch(full_path, '[^/]+') do
|
|
|
|
parts[#parts + 1] = part
|
|
|
|
end
|
|
|
|
headers[#headers + 1] = parts[#parts - 1]..'/'..parts[#parts]
|
2014-04-10 11:51:44 -07:00
|
|
|
|
2014-05-07 13:59:16 -07:00
|
|
|
local input = io.open(full_path, 'rb')
|
|
|
|
local tmp = grammar:match(input:read('*all'))
|
|
|
|
for i = 1, #tmp do
|
2014-09-03 13:19:55 -07:00
|
|
|
local fn = tmp[i]
|
2016-06-01 16:14:09 -07:00
|
|
|
if not fn.noexport then
|
|
|
|
functions[#functions + 1] = tmp[i]
|
2016-09-14 02:15:01 -07:00
|
|
|
function_names[fn.name] = true
|
2016-06-01 16:14:09 -07:00
|
|
|
if #fn.parameters ~= 0 and fn.parameters[1][2] == 'channel_id' then
|
|
|
|
-- this function should receive the channel id
|
|
|
|
fn.receives_channel_id = true
|
|
|
|
-- remove the parameter since it won't be passed by the api client
|
|
|
|
table.remove(fn.parameters, 1)
|
|
|
|
end
|
|
|
|
if #fn.parameters ~= 0 and fn.parameters[#fn.parameters][1] == 'error' then
|
|
|
|
-- function can fail if the last parameter type is 'Error'
|
|
|
|
fn.can_fail = true
|
|
|
|
-- remove the error parameter, msgpack has it's own special field
|
|
|
|
-- for specifying errors
|
|
|
|
fn.parameters[#fn.parameters] = nil
|
|
|
|
end
|
2016-04-14 11:47:01 -07:00
|
|
|
end
|
2014-05-07 13:59:16 -07:00
|
|
|
end
|
|
|
|
input:close()
|
2014-04-10 11:51:44 -07:00
|
|
|
end
|
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
local function shallowcopy(orig)
|
|
|
|
local copy = {}
|
|
|
|
for orig_key, orig_value in pairs(orig) do
|
|
|
|
copy[orig_key] = orig_value
|
|
|
|
end
|
|
|
|
return copy
|
|
|
|
end
|
|
|
|
|
|
|
|
local function startswith(String,Start)
|
|
|
|
return string.sub(String,1,string.len(Start))==Start
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Export functions under older deprecated names.
|
|
|
|
-- These will be removed eventually.
|
2016-09-14 02:15:01 -07:00
|
|
|
local deprecated_aliases = require("api.dispatch_deprecated")
|
2016-06-28 12:45:19 -07:00
|
|
|
for i,f in ipairs(shallowcopy(functions)) do
|
|
|
|
local ismethod = false
|
2016-09-27 05:26:19 -07:00
|
|
|
if startswith(f.name, "nvim_") then
|
2016-10-24 08:48:24 -07:00
|
|
|
if f.since == nil then
|
|
|
|
print("Function "..f.name.." lacks since field.\n")
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
f.since = tonumber(f.since)
|
2016-09-27 05:26:19 -07:00
|
|
|
if startswith(f.name, "nvim_buf_") then
|
|
|
|
ismethod = true
|
|
|
|
elseif startswith(f.name, "nvim_win_") then
|
|
|
|
ismethod = true
|
|
|
|
elseif startswith(f.name, "nvim_tabpage_") then
|
|
|
|
ismethod = true
|
|
|
|
end
|
|
|
|
else
|
2016-06-28 12:45:19 -07:00
|
|
|
f.noeval = true
|
2016-09-27 05:26:19 -07:00
|
|
|
f.since = 0
|
2016-06-28 12:45:19 -07:00
|
|
|
f.deprecated_since = 1
|
|
|
|
end
|
|
|
|
f.method = ismethod
|
|
|
|
local newname = deprecated_aliases[f.name]
|
|
|
|
if newname ~= nil then
|
2016-09-14 02:15:01 -07:00
|
|
|
if function_names[newname] then
|
|
|
|
-- duplicate
|
|
|
|
print("Function "..f.name.." has deprecated alias\n"
|
|
|
|
..newname.." which has a separate implementation.\n"..
|
|
|
|
"Please remove it from src/nvim/api/dispatch_deprecated.lua")
|
|
|
|
os.exit(1)
|
|
|
|
end
|
2016-06-28 12:45:19 -07:00
|
|
|
local newf = shallowcopy(f)
|
|
|
|
newf.name = newname
|
2016-09-26 09:53:32 -07:00
|
|
|
if newname == "ui_try_resize" then
|
|
|
|
-- The return type was incorrectly set to Object in 0.1.5.
|
|
|
|
-- Keep it that way for clients that rely on this.
|
|
|
|
newf.return_type = "Object"
|
|
|
|
end
|
2016-06-28 12:45:19 -07:00
|
|
|
newf.impl_name = f.name
|
|
|
|
newf.noeval = true
|
2016-09-27 05:26:19 -07:00
|
|
|
newf.since = 0
|
2016-06-28 12:45:19 -07:00
|
|
|
newf.deprecated_since = 1
|
|
|
|
functions[#functions+1] = newf
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-26 10:05:13 -07:00
|
|
|
-- don't expose internal attributes like "impl_name" in public metadata
|
|
|
|
exported_attributes = {'name', 'parameters', 'return_type', 'method',
|
|
|
|
'since', 'deprecated_since'}
|
|
|
|
exported_functions = {}
|
|
|
|
for _,f in ipairs(functions) do
|
|
|
|
local f_exported = {}
|
|
|
|
for _,attr in ipairs(exported_attributes) do
|
|
|
|
f_exported[attr] = f[attr]
|
|
|
|
end
|
|
|
|
exported_functions[#exported_functions+1] = f_exported
|
|
|
|
end
|
|
|
|
|
2014-05-07 13:59:16 -07:00
|
|
|
|
2016-09-27 07:54:08 -07:00
|
|
|
funcs_metadata_output = io.open(funcs_metadata_outputf, 'wb')
|
|
|
|
funcs_metadata_output:write([[
|
|
|
|
static const uint8_t funcs_metadata[] = {
|
2014-05-07 13:59:16 -07:00
|
|
|
]])
|
|
|
|
|
|
|
|
-- serialize the API metadata using msgpack and embed into the resulting
|
2014-06-18 16:04:39 -07:00
|
|
|
-- binary for easy querying by clients
|
2016-09-26 10:05:13 -07:00
|
|
|
packed_exported_functions = mpack.pack(exported_functions)
|
|
|
|
for i = 1, #packed_exported_functions do
|
2016-09-27 07:54:08 -07:00
|
|
|
funcs_metadata_output:write(string.byte(packed_exported_functions, i)..', ')
|
2014-04-10 11:51:44 -07:00
|
|
|
if i % 10 == 0 then
|
2016-09-27 07:54:08 -07:00
|
|
|
funcs_metadata_output:write('\n ')
|
2014-04-10 11:51:44 -07:00
|
|
|
end
|
|
|
|
end
|
2016-09-27 07:54:08 -07:00
|
|
|
funcs_metadata_output:write([[
|
2014-04-10 11:51:44 -07:00
|
|
|
};
|
|
|
|
]])
|
2016-09-27 07:54:08 -07:00
|
|
|
funcs_metadata_output:close()
|
|
|
|
|
|
|
|
-- start building the dispatch wrapper output
|
|
|
|
output = io.open(dispatch_outputf, 'wb')
|
2014-04-10 11:51:44 -07:00
|
|
|
|
2014-09-12 07:24:01 -07:00
|
|
|
local function real_type(type)
|
|
|
|
local rv = type
|
|
|
|
if typed_container:match(rv) then
|
|
|
|
if rv:match('Array') then
|
|
|
|
rv = 'Array'
|
|
|
|
else
|
|
|
|
rv = 'Dictionary'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return rv
|
|
|
|
end
|
|
|
|
|
2014-09-11 17:56:52 -07:00
|
|
|
-- start the handler functions. Visit each function metadata to build the
|
|
|
|
-- handler function with code generated for validating arguments and calling to
|
|
|
|
-- the real API.
|
|
|
|
for i = 1, #functions do
|
|
|
|
local fn = functions[i]
|
2016-06-28 12:45:19 -07:00
|
|
|
if fn.impl_name == nil then
|
|
|
|
local args = {}
|
|
|
|
|
2016-04-13 07:37:09 -07:00
|
|
|
output:write('Object handle_'..fn.name..'(uint64_t channel_id, Array args, Error *error)')
|
2016-06-28 12:45:19 -07:00
|
|
|
output:write('\n{')
|
|
|
|
output:write('\n Object ret = NIL;')
|
|
|
|
-- Declare/initialize variables that will hold converted arguments
|
|
|
|
for j = 1, #fn.parameters do
|
|
|
|
local param = fn.parameters[j]
|
|
|
|
local converted = 'arg_'..j
|
2016-09-14 04:10:04 -07:00
|
|
|
output:write('\n '..param[1]..' '..converted..';')
|
2014-10-20 16:07:01 -07:00
|
|
|
end
|
2016-06-28 12:45:19 -07:00
|
|
|
output:write('\n')
|
|
|
|
output:write('\n if (args.size != '..#fn.parameters..') {')
|
|
|
|
output:write('\n snprintf(error->msg, sizeof(error->msg), "Wrong number of arguments: expecting '..#fn.parameters..' but got %zu", args.size);')
|
|
|
|
output:write('\n error->set = true;')
|
|
|
|
output:write('\n goto cleanup;')
|
|
|
|
output:write('\n }\n')
|
2014-10-20 16:07:01 -07:00
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
-- Validation/conversion for each argument
|
|
|
|
for j = 1, #fn.parameters do
|
|
|
|
local converted, convert_arg, param, arg
|
|
|
|
param = fn.parameters[j]
|
|
|
|
converted = 'arg_'..j
|
|
|
|
local rt = real_type(param[1])
|
|
|
|
if rt ~= 'Object' then
|
2016-10-30 20:44:36 -07:00
|
|
|
if rt:match('^Buffer$') or rt:match('^Window$') or rt:match('^Tabpage$') then
|
|
|
|
-- Buffer, Window, and Tabpage have a specific type, but are stored in integer
|
|
|
|
output:write('\n if (args.items['..(j - 1)..'].type == kObjectType'..rt..' && args.items['..(j - 1)..'].data.integer >= 0) {')
|
|
|
|
output:write('\n '..converted..' = (handle_T)args.items['..(j - 1)..'].data.integer;')
|
|
|
|
else
|
|
|
|
output:write('\n if (args.items['..(j - 1)..'].type == kObjectType'..rt..') {')
|
|
|
|
output:write('\n '..converted..' = args.items['..(j - 1)..'].data.'..rt:lower()..';')
|
|
|
|
end
|
2016-06-28 12:45:19 -07:00
|
|
|
if rt:match('^Buffer$') or rt:match('^Window$') or rt:match('^Tabpage$') or rt:match('^Boolean$') then
|
|
|
|
-- accept nonnegative integers for Booleans, Buffers, Windows and Tabpages
|
|
|
|
output:write('\n } else if (args.items['..(j - 1)..'].type == kObjectTypeInteger && args.items['..(j - 1)..'].data.integer >= 0) {')
|
|
|
|
output:write('\n '..converted..' = (handle_T)args.items['..(j - 1)..'].data.integer;')
|
|
|
|
end
|
|
|
|
output:write('\n } else {')
|
|
|
|
output:write('\n snprintf(error->msg, sizeof(error->msg), "Wrong type for argument '..j..', expecting '..param[1]..'");')
|
|
|
|
output:write('\n error->set = true;')
|
|
|
|
output:write('\n goto cleanup;')
|
|
|
|
output:write('\n }\n')
|
|
|
|
else
|
|
|
|
output:write('\n '..converted..' = args.items['..(j - 1)..'];\n')
|
|
|
|
end
|
2014-06-18 16:04:39 -07:00
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
args[#args + 1] = converted
|
|
|
|
end
|
2014-05-07 13:59:16 -07:00
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
-- function call
|
|
|
|
local call_args = table.concat(args, ', ')
|
|
|
|
output:write('\n ')
|
|
|
|
if fn.return_type ~= 'void' then
|
|
|
|
-- has a return value, prefix the call with a declaration
|
|
|
|
output:write(fn.return_type..' rv = ')
|
|
|
|
end
|
2014-05-28 04:42:08 -07:00
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
-- write the function name and the opening parenthesis
|
|
|
|
output:write(fn.name..'(')
|
|
|
|
|
|
|
|
if fn.receives_channel_id then
|
|
|
|
-- if the function receives the channel id, pass it as first argument
|
|
|
|
if #args > 0 or fn.can_fail then
|
|
|
|
output:write('channel_id, '..call_args)
|
|
|
|
else
|
|
|
|
output:write('channel_id')
|
|
|
|
end
|
2014-05-28 04:42:08 -07:00
|
|
|
else
|
2016-06-28 12:45:19 -07:00
|
|
|
output:write(call_args)
|
2014-05-28 04:42:08 -07:00
|
|
|
end
|
2014-05-07 13:59:16 -07:00
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
if fn.can_fail then
|
|
|
|
-- if the function can fail, also pass a pointer to the local error object
|
|
|
|
if #args > 0 then
|
|
|
|
output:write(', error);\n')
|
|
|
|
else
|
|
|
|
output:write('error);\n')
|
|
|
|
end
|
|
|
|
-- and check for the error
|
|
|
|
output:write('\n if (error->set) {')
|
|
|
|
output:write('\n goto cleanup;')
|
|
|
|
output:write('\n }\n')
|
2014-05-07 13:59:16 -07:00
|
|
|
else
|
2016-06-28 12:45:19 -07:00
|
|
|
output:write(');\n')
|
2014-04-10 11:51:44 -07:00
|
|
|
end
|
2014-05-07 13:59:16 -07:00
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
if fn.return_type ~= 'void' then
|
|
|
|
output:write('\n ret = '..string.upper(real_type(fn.return_type))..'_OBJ(rv);')
|
|
|
|
end
|
|
|
|
output:write('\n\ncleanup:');
|
2014-05-07 13:59:16 -07:00
|
|
|
|
2016-06-28 12:45:19 -07:00
|
|
|
output:write('\n return ret;\n}\n\n');
|
2014-06-18 16:04:39 -07:00
|
|
|
end
|
2014-04-10 11:51:44 -07:00
|
|
|
end
|
2014-05-07 13:59:16 -07:00
|
|
|
|
2014-08-28 11:46:21 -07:00
|
|
|
-- Generate a function that initializes method names with handler functions
|
2014-06-18 16:04:39 -07:00
|
|
|
output:write([[
|
2014-10-20 03:35:10 -07:00
|
|
|
void msgpack_rpc_init_method_table(void)
|
2014-08-28 11:46:21 -07:00
|
|
|
{
|
2014-10-20 16:07:01 -07:00
|
|
|
methods = map_new(String, MsgpackRpcRequestHandler)();
|
2014-08-28 11:46:21 -07:00
|
|
|
|
|
|
|
]])
|
|
|
|
|
2014-09-11 17:56:52 -07:00
|
|
|
for i = 1, #functions do
|
|
|
|
local fn = functions[i]
|
2014-12-04 06:38:10 -07:00
|
|
|
output:write(' msgpack_rpc_add_method_handler('..
|
2014-09-03 10:26:16 -07:00
|
|
|
'(String) {.data = "'..fn.name..'", '..
|
2014-10-20 16:07:01 -07:00
|
|
|
'.size = sizeof("'..fn.name..'") - 1}, '..
|
2016-06-28 12:45:19 -07:00
|
|
|
'(MsgpackRpcRequestHandler) {.fn = handle_'.. (fn.impl_name or fn.name)..
|
2015-07-24 05:55:31 -07:00
|
|
|
', .async = '..tostring(fn.async)..'});\n')
|
2014-09-03 10:26:16 -07:00
|
|
|
|
2014-08-28 11:46:21 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
output:write('\n}\n\n')
|
2014-04-10 11:51:44 -07:00
|
|
|
output:close()
|
2016-06-18 03:06:50 -07:00
|
|
|
|
|
|
|
mpack_output = io.open(mpack_outputf, 'wb')
|
2016-09-26 10:05:13 -07:00
|
|
|
mpack_output:write(mpack.pack(functions))
|
2016-06-18 03:06:50 -07:00
|
|
|
mpack_output:close()
|