robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
4d736cd41f
master
1-- lpeg grammar for building api metadata from a set of header files. It 2-- ignores comments and preprocessor commands and parses a very small subset 3-- of C prototypes with a limited set of types 4 5---@diagnostic disable: missing-fields 6 7--- @class nvim.c_grammar.Proto 8--- @field [1] 'proto' 9--- @field pos integer 10--- @field endpos integer 11--- @field name string 12--- @field return_type string 13--- @field parameters [string, string][] 14--- 15--- Decl modifiers 16--- 17--- @field static true? 18--- @field inline true? 19--- 20--- Attributes 21--- 22--- @field since integer? 23--- @field deprecated_since integer? 24--- @field fast true? 25--- @field ret_alloc true? 26--- @field noexport true? 27--- @field remote_only true? 28--- @field lua_only true? 29--- @field textlock true? 30--- @field remote_impl true? 31--- @field compositor_impl true? 32--- @field client_impl true? 33--- @field client_ignore true? 34 35--- @class nvim.c_grammar.Preproc 36--- @field [1] 'preproc' 37--- @field content string 38 39--- @class nvim.c_grammar.Keyset.Field 40--- @field type string 41--- @field name string 42--- @field dict_key? string 43 44--- @class nvim.c_grammar.Keyset 45--- @field [1] 'typedef' 46--- @field keyset_name string 47--- @field fields nvim.c_grammar.Keyset.Field[] 48 49--- @class nvim.c_grammar.Empty 50--- @field [1] 'empty' 51 52--- @class nvim.c_grammar.Comment 53--- @field comment string 54 55--- @alias nvim.c_grammar.result 56--- | nvim.c_grammar.Proto 57--- | nvim.c_grammar.Preproc 58--- | nvim.c_grammar.Empty 59--- | nvim.c_grammar.Keyset 60--- | nvim.c_grammar.Comment 61 62--- @class nvim.c_grammar 63--- @field match fun(self, input: string): nvim.c_grammar.result[] 64 65local lpeg =vim . lpeg 66 67local P , R , S , V =lpeg . P , lpeg . R , lpeg . S , lpeg . V 68local C , Ct , Cc , Cg , Cp =lpeg . C , lpeg . Ct , lpeg . Cc , lpeg . Cg , lpeg . Cp 69 70--- @param pat vim.lpeg.Pattern 71local function rep ( pat ) 72return pat ^ 0 73end 74 75--- @param pat vim.lpeg.Pattern 76local function rep1 ( pat ) 77return pat ^ 1 78end 79 80--- @param pat vim.lpeg.Pattern 81local function opt ( pat ) 82return pat ^ - 1 83end 84 85local any =P ( 1 ) 86local letter =R ( 'az' , 'AZ' ) + S ( '_$' ) 87local num =R ( '09' ) 88local alpha =letter + num 89local nl =P ( '\r\n' ) + P ( '\n' ) 90local space =S ( ' \t' ) 91local str =P ( '"' ) * rep (( P ( '\\' ) * any ) + ( 1 - P ( '"' ))) * P ( '"' ) 92local char =P ( "'" ) * ( any - P ( "'" )) * P ( "'" ) 93local ws =space + nl 94local wb =#- alpha -- word boundary 95local id =letter * rep ( alpha ) 96 97local comment_inline =P ( '/*' ) * rep ( 1 - P ( '*/' )) * P ( '*/' ) 98local comment =P ( '//' ) * rep ( 1 - nl ) * nl 99local preproc =Ct ( Cc ( 'preproc' ) * P ( '#' ) * Cg ( rep ( 1 - nl ) * nl , 'content' )) 100 101local fill =rep ( ws + comment_inline + comment + preproc ) 102 103--- @param s string 104--- @return vim.lpeg.Pattern 105local function word ( s ) 106return fill * P ( s ) * wb * fill 107end 108 109--- @param x vim.lpeg.Pattern 110local function comma1 ( x ) 111return x * rep ( fill * P ( ',' ) * fill * x ) 112end 113 114--- @param v string 115local function Pf ( v ) 116return fill * P ( v ) * fill 117end 118 119--- @param x vim.lpeg.Pattern 120local function paren ( x ) 121return P ( '(' ) * fill * x * fill * P ( ')' ) 122end 123 124local cdoc_comment =P ( '///' ) * opt ( Ct ( Cg ( rep ( space ) * rep ( 1 - nl ), 'comment' ))) 125 126local braces =P ( { 127'S' , 128A =comment_inline + comment + preproc + str + char + ( any - S ( '{}' )), 129S =P ( '{' ) * rep ( V ( 'A' )) * rep ( V ( 'S' ) + V ( 'A' )) * P ( '}' ), 130} ) 131 132--- @alias nvim.c_grammar.Container.Union ['Union', string[]] 133--- @alias nvim.c_grammar.Container.Tuple ['Tuple', string[]] 134--- @alias nvim.c_grammar.Container.Enum ['Enum', string[]] 135--- @alias nvim.c_grammar.Container.ArrayOf ['ArrayOf', string, integer?] 136--- @alias nvim.c_grammar.Container.DictOf ['DictOf', string] 137--- @alias nvim.c_grammar.Container.LuaRefOf ['LuaRefOf', [string, string][], string] 138--- @alias nvim.c_grammar.Container.Dict ['Dict', string] 139--- @alias nvim.c_grammar.Container.DictAs ['DictAs', string] 140 141--- @alias nvim.c_grammar.Container 142--- | nvim.c_grammar.Container.Union 143--- | nvim.c_grammar.Container.Tuple 144--- | nvim.c_grammar.Container.Enum 145--- | nvim.c_grammar.Container.ArrayOf 146--- | nvim.c_grammar.Container.DictOf 147--- | nvim.c_grammar.Container.LuaRefOf 148--- | nvim.c_grammar.Container.Dict 149 150-- stylua: ignore start 151local typed_container =P ( { 152'S' , 153S =Ct ( 154Cg ( opt ( P ( '*' )) * P ( 'Union' )) * paren ( Ct ( comma1 ( V ( 'TY' )))) 155+ Cg ( opt ( P ( '*' )) * P ( 'Enum' )) * paren ( Ct ( comma1 ( Cg ( str )))) 156+ Cg ( opt ( P ( '*' )) * P ( 'Tuple' )) * paren ( Ct ( comma1 ( V ( 'TY' )))) 157+ Cg ( opt ( P ( '*' )) * P ( 'ArrayOf' )) * paren ( V ( 'TY' ) * opt ( P ( ',' ) * fill * C ( rep1 ( num )))) 158+ Cg ( opt ( P ( '*' )) * P ( 'DictOf' )) * paren ( V ( 'TY' )) 159+ Cg ( opt ( P ( '*' )) * P ( 'LuaRefOf' )) 160* paren ( 161Ct ( paren ( comma1 ( Ct (( V ( 'TY' ) + C ( str )) * rep1 ( ws ) * Cg ( V ( 'ID' )))))) 162* opt ( P ( ',' ) * fill * V ( 'TY' )) 163) 164+ Cg ( opt ( P ( '*' )) * P ( 'Dict' )) * paren ( C ( id )) 165+ Cg ( opt ( P ( '*' )) * P ( 'DictAs' )) * paren ( C ( id )) 166+ Cg ( opt ( P ( '*' )) * P ( 'Set' )) * paren ( C ( id )) 167+ Cg ( opt ( P ( '*' )) * P ( 'PMap' )) * paren ( C ( id )) 168), 169-- Remove captures here (with / 0 ) as api_types will recursively run parse the type. 170TY =Cg ( V ( 'S' ) / 0 + V ( 'ID' )), 171ID =opt ( P ( '*' )) * id , 172} ) 173-- stylua: ignore end 174 175local ptr_mod =word ( 'restrict' ) + word ( '__restrict' ) + word ( 'const' ) 176local opt_ptr =rep ( Pf ( '*' ) * opt ( ptr_mod )) 177 178--- @param name string 179--- @param var string 180--- @return vim.lpeg.Pattern 181local function attr ( name , var ) 182return Cg (( P ( name ) * Cc ( true )), var ) 183end 184 185--- @param name string 186--- @param var string 187--- @return vim.lpeg.Pattern 188local function attr_num ( name , var ) 189return Cg (( P ( name ) * paren ( C ( rep1 ( num )))), var ) 190end 191 192local fattr =( 193attr_num ( 'FUNC_API_SINCE' , 'since' ) 194+ attr_num ( 'FUNC_API_DEPRECATED_SINCE' , 'deprecated_since' ) 195+ attr ( 'FUNC_API_FAST' , 'fast' ) 196+ attr ( 'FUNC_API_RET_ALLOC' , 'ret_alloc' ) 197+ attr ( 'FUNC_API_NOEXPORT' , 'noexport' ) 198+ attr ( 'FUNC_API_REMOTE_ONLY' , 'remote_only' ) 199+ attr ( 'FUNC_API_LUA_ONLY' , 'lua_only' ) 200+ attr ( 'FUNC_API_TEXTLOCK' , 'textlock' ) 201+ attr ( 'FUNC_API_REMOTE_IMPL' , 'remote_impl' ) 202+ attr ( 'FUNC_API_COMPOSITOR_IMPL' , 'compositor_impl' ) 203+ attr ( 'FUNC_API_CLIENT_IMPL' , 'client_impl' ) 204+ attr ( 'FUNC_API_CLIENT_IGNORE' , 'client_ignore' ) 205+ ( P ( 'FUNC_' ) * rep ( alpha ) * opt ( fill * paren ( rep ( 1 - P ( ')' ) * any )))) 206) 207 208local void =P ( 'void' ) * wb 209 210local api_param_type =( 211( word ( 'Error' ) * opt_ptr * Cc ( 'error' )) 212+ ( word ( 'Arena' ) * opt_ptr * Cc ( 'arena' )) 213+ ( word ( 'lua_State' ) * opt_ptr * Cc ( 'lstate' )) 214) 215 216local ctype =C ( 217opt ( word ( 'const' )) 218* ( 219typed_container / 0 220-- 'unsigned' is a type modifier, and a type itself 221+ ( word ( 'unsigned char' ) + word ( 'unsigned' )) 222+ ( word ( 'struct' ) * fill * id ) 223+ id 224) 225* opt ( word ( 'const' )) 226* opt_ptr 227) 228 229local return_type =( C ( void ) * fill ) + ctype 230 231-- stylua: ignore start 232local params =Ct ( 233( void * # P ( ')' )) 234+ comma1 ( Ct ( 235( api_param_type + ctype ) 236* fill 237* C ( id ) 238* rep ( Pf ( '[' ) * rep ( alpha ) * Pf ( ']' )) 239* rep ( fill * fattr ) 240)) 241* opt ( Pf ( ',' ) * P ( '...' )) 242) 243-- stylua: ignore end 244 245local ignore_line =rep1 ( 1 - nl ) * nl 246local empty_line =Ct ( Cc ( 'empty' ) * nl * nl ) 247 248local proto_name =opt_ptr * fill * id 249 250-- __inline is used in MSVC 251local decl_mod =( 252Cg ( word ( 'static' ) * Cc ( true ), 'static' ) 253+ Cg (( word ( 'inline' ) + word ( '__inline' )) * Cc ( true ), 'inline' ) 254) 255 256local proto =Ct ( 257Cg ( Cp (), 'pos' ) 258* Cc ( 'proto' ) 259* -# P ( 'typedef' ) 260* # alpha 261* opt ( P ( 'DLLEXPORT' ) * rep1 ( ws )) 262* rep ( decl_mod ) 263* Cg ( return_type , 'return_type' ) 264* fill 265* Cg ( proto_name , 'name' ) 266* fill 267* paren ( Cg ( params , 'parameters' )) 268* Cg ( Cc ( false ), 'fast' ) 269* rep ( fill * fattr ) 270* Cg ( Cp (), 'endpos' ) 271* ( fill * ( S ( ';' ) + braces )) 272) 273 274local keyset_field =Ct ( 275Cg ( ctype , 'type' ) 276* fill 277* Cg ( id , 'name' ) 278* fill 279* opt ( P ( 'DictKey' ) * paren ( Cg ( rep1 ( 1 - P ( ')' )), 'dict_key' ))) 280* Pf ( ';' ) 281) 282 283local keyset =Ct ( 284P ( 'typedef' ) 285* word ( 'struct' ) 286* Pf ( '{' ) 287* Cg ( Ct ( rep1 ( keyset_field )), 'fields' ) 288* Pf ( '}' ) 289* P ( 'Dict' ) 290* paren ( Cg ( id , 'keyset_name' )) 291* Pf ( ';' ) 292) 293 294local grammar = 295Ct ( rep1 ( empty_line + proto + cdoc_comment + comment + preproc + ws + keyset + ignore_line )) 296 297if arg [ 1 ] == '--test' then 298for i , t in ipairs ( { 299'void multiqueue_put_event(MultiQueue *self, Event event) {} ' , 300'void *xmalloc(size_t size) {} ' , 301{ 302'struct tm *os_localtime_r(const time_t *restrict clock,' , 303' struct tm *restrict result) FUNC_ATTR_NONNULL_ALL {}' , 304} , 305{ 306'_Bool' , 307'# 163 "src/nvim/event/multiqueue.c"' , 308' multiqueue_empty(MultiQueue *self)' , 309'{}' , 310} , 311'const char *find_option_end(const char *arg, OptIndex *opt_idxp) {}' , 312'bool semsg(const char *const fmt, ...) {}' , 313'int32_t utf_ptr2CharInfo_impl(uint8_t const *p, uintptr_t const len) {}' , 314'void ex_argdedupe(exarg_T *eap FUNC_ATTR_UNUSED) {}' , 315'static TermKeySym register_c0(TermKey *tk, TermKeySym sym, unsigned char ctrl, const char *name) {}' , 316'unsigned get_bkc_flags(buf_T *buf) {}' , 317'char *xstpcpy(char *restrict dst, const char *restrict src) {}' , 318'bool try_leave(const TryState *const tstate, Error *const err) {}' , 319'void api_set_error(ErrorType errType) {}' , 320{ 321'void nvim_subscribe(uint64_t channel_id, String event)' , 322'FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(13) FUNC_API_REMOTE_ONLY' , 323'{}' , 324} , 325 326-- Do not consume leading preproc statements 327{ 328'#line 1 "D:/a/neovim/neovim/src\\nvim/mark.h"' , 329'static __inline int mark_global_index(const char name)' , 330' FUNC_ATTR_CONST' , 331'{}' , 332} , 333{ 334'' , 335'#line 1 "D:/a/neovim/neovim/src\\nvim/mark.h"' , 336'static __inline int mark_global_index(const char name)' , 337'{}' , 338} , 339{ 340'size_t xstrlcpy(char *__restrict dst, const char *__restrict src, size_t dsize)' , 341' FUNC_ATTR_NONNULL_ALL' , 342' {}' , 343} , 344} ) do 345if type ( t ) == 'table' then 346t =table . concat ( t , '\n' ) .. '\n' 347end 348t =t : gsub ( ' +' , ' ' ) 349local r =grammar : match ( t ) 350if not r then 351( 'Test ' .. i .. ' failed' ) 352( ' |' .. table . concat ( vim . split ( t , '\n' ), '\n |' )) 353end 354end 355end 356 357--- Index of the `opts` parameter, which starts optional parameters. 358--- @param fn_params [string,string][] Raw `{type, name}` pairs of a parsed API function. 359--- @return integer? opts_idx 1-based index of `opts`, or nil. 360local function opts_index ( fn_params ) 361for i =1 , # fn_params do 362local c =typed_container : match ( fn_params [ i ][ 1 ]) 363if fn_params [ i ][ 2 ] == 'opts' and c and c [ 1 ] == 'Dict' then 364return i 365end 366end 367end 368 369return { 370grammar =grammar --[[@as nvim.c_grammar]] , 371typed_container =typed_container , 372opts_index =opts_index , 373}