robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dd5ebae7ac
master
1---@diagnostic disable: no-unknown 2-- Will generate files ex_cmds_enum.generated.h with cmdidx_T enum 3-- and ex_cmds_defs.generated.h with main Ex commands definitions. 4 5local enumfname =arg [ 1 ] -- '/ex_cmds_enum.generated.h' 6local defsfname =arg [ 2 ] -- '/ex_cmds_defs.generated.h' 7local ex_cmds_name =arg [ 3 ] -- 'ex_cmds.lua' 8 9local enumfile =assert ( io . open ( enumfname , 'w' )) 10local defsfile =assert ( io . open ( defsfname , 'w' )) 11 12local bit =require 'bit' 13local ex_cmds =loadfile ( ex_cmds_name )() 14local defs =ex_cmds . cmds 15local flags =ex_cmds . flags 16 17local byte_a =string . byte ( 'a' ) 18local byte_z =string . byte ( 'z' ) 19local a_to_z =byte_z - byte_a + 1 20 21-- Table giving the index of the first command in cmdnames[] to lookup 22-- based on the first letter of a command. 23local cmdidxs1_out =string . format ( 24[[ 25static const uint16_t cmdidxs1[%u] = { 26]] , 27a_to_z 28) 29-- Table giving the index of the first command in cmdnames[] to lookup 30-- based on the first 2 letters of a command. 31-- Values in cmdidxs2[c1][c2] are relative to cmdidxs1[c1] so that they 32-- fit in a byte. 33local cmdidxs2_out =string . format ( 34[[ 35static const uint8_t cmdidxs2[%u][%u] = { 36/* a b c d e f g h i j k l m n o p q r s t u v w x y z */ 37]] , 38a_to_z , 39a_to_z 40) 41 42enumfile : write ( [[ 43// IWYU pragma: private, include "nvim/ex_cmds_defs.h" 44 45typedef enum CMD_index { 46]] ) 47defsfile : write ( string . format ( 48[[ 49#include "nvim/arglist.h" 50#include "nvim/autocmd.h" 51#include "nvim/buffer.h" 52#include "nvim/cmdhist.h" 53#include "nvim/debugger.h" 54#include "nvim/diff.h" 55#include "nvim/digraph.h" 56#include "nvim/eval.h" 57#include "nvim/eval/userfunc.h" 58#include "nvim/eval/vars.h" 59#include "nvim/ex_cmds.h" 60#include "nvim/ex_cmds2.h" 61#include "nvim/ex_docmd.h" 62#include "nvim/ex_eval.h" 63#include "nvim/ex_session.h" 64#include "nvim/help.h" 65#include "nvim/indent.h" 66#include "nvim/lua/executor.h" 67#include "nvim/lua/secure.h" 68#include "nvim/mapping.h" 69#include "nvim/mark.h" 70#include "nvim/match.h" 71#include "nvim/menu.h" 72#include "nvim/message.h" 73#include "nvim/ops.h" 74#include "nvim/option.h" 75#include "nvim/os/lang.h" 76#include "nvim/profile.h" 77#include "nvim/quickfix.h" 78#include "nvim/register.h" 79#include "nvim/runtime.h" 80#include "nvim/sign.h" 81#include "nvim/spell.h" 82#include "nvim/spellfile.h" 83#include "nvim/syntax.h" 84#include "nvim/undo.h" 85#include "nvim/usercmd.h" 86#include "nvim/version.h" 87 88static const int command_count = %u; 89static CommandDefinition cmdnames[%u] = { 90]] , 91# defs , 92# defs 93)) 94local cmds , cmdidxs1 , cmdidxs2 ={} , {} , {} 95for _ , cmd in ipairs ( defs ) do 96if bit . band ( cmd . flags , flags . RANGE ) == flags . RANGE then 97assert ( 98cmd . addr_type ~= 'ADDR_NONE' , 99string . format ( 'ex_cmds.lua:%s: Using RANGE with ADDR_NONE\n' , cmd . command ) 100) 101else 102assert ( 103cmd . addr_type == 'ADDR_NONE' , 104string . format ( 'ex_cmds.lua:%s: Missing ADDR_NONE\n' , cmd . command ) 105) 106end 107if bit . band ( cmd . flags , flags . DFLALL ) == flags . DFLALL then 108assert ( 109cmd . addr_type ~= 'ADDR_OTHER' and cmd . addr_type ~= 'ADDR_NONE' , 110string . format ( 'ex_cmds.lua:%s: Missing misplaced DFLALL\n' , cmd . command ) 111) 112end 113if bit . band ( cmd . flags , flags . PREVIEW ) == flags . PREVIEW then 114assert ( 115cmd . preview_func ~= nil , 116string . format ( 'ex_cmds.lua:%s: Missing preview_func\n' , cmd . command ) 117) 118end 119if bit . band ( cmd . flags , flags . BUFLOCK_OK ) == flags . BUFLOCK_OK then 120assert ( 121bit . band ( cmd . flags , flags . LOCK_OK ) == flags . LOCK_OK , 122string . format ( 'ex_cmds.lua:%s: BUFLOCK_OK implies LOCK_OK\n' , cmd . command ) 123) 124end 125local enumname =cmd . enum or ( 'CMD_' .. cmd . command ) 126local byte_cmd =cmd . command : sub ( 1 , 1 ): byte () 127if byte_a <= byte_cmd and byte_cmd <= byte_z then 128table . insert ( cmds , cmd . command ) 129end 130local preview_func 131if cmd . preview_func then 132preview_func =string . format ( '&%s' , cmd . preview_func ) 133else 134preview_func ='NULL' 135end 136enumfile : write ( ' ' .. enumname .. ',\n' ) 137defsfile : write ( string . format ( 138[[ 139[%s] = { 140.cmd_name = "%s", 141.cmd_func = (ex_func_T)&%s, 142.cmd_preview_func = %s, 143.cmd_argt = %uL, 144.cmd_addr_type = %s 145}, 146]] , 147enumname , 148cmd . command , 149cmd . func , 150preview_func , 151cmd . flags , 152cmd . addr_type 153)) 154end 155for i =# cmds , 1 , - 1 do 156local cmd =cmds [ i ] 157-- First and second characters of the command 158local c1 =cmd : sub ( 1 , 1 ) 159cmdidxs1 [ c1 ] =i - 1 160if cmd : len () >= 2 then 161local c2 =cmd : sub ( 2 , 2 ) 162local byte_c2 =string . byte ( c2 ) 163if byte_a <= byte_c2 and byte_c2 <= byte_z then 164if not cmdidxs2 [ c1 ] then 165cmdidxs2 [ c1 ] ={} 166end 167cmdidxs2 [ c1 ][ c2 ] =i - 1 168end 169end 170end 171for i =byte_a , byte_z do 172local c1 =string . char ( i ) 173cmdidxs1_out =cmdidxs1_out .. ' /* ' .. c1 .. ' */ ' .. cmdidxs1 [ c1 ] .. ',\n' 174cmdidxs2_out =cmdidxs2_out .. ' /* ' .. c1 .. ' */ {' 175for j =byte_a , byte_z do 176local c2 =string . char ( j ) 177cmdidxs2_out =cmdidxs2_out 178.. (( cmdidxs2 [ c1 ] and cmdidxs2 [ c1 ][ c2 ]) and string . format ( 179'%3d' , 180cmdidxs2 [ c1 ][ c2 ] - cmdidxs1 [ c1 ] 181) or ' 0' ) 182.. ',' 183end 184cmdidxs2_out =cmdidxs2_out .. ' },\n' 185end 186enumfile : write ( [[ 187CMD_SIZE, 188CMD_USER = -1, 189CMD_USER_BUF = -2 190} cmdidx_T; 191]] ) 192defsfile : write ( string . format ( 193[[ 194}; 195%s}; 196%s}; 197]] , 198cmdidxs1_out , 199cmdidxs2_out 200))