robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
256a88d0ac
master
1local cdoc_grammar =require ( 'gen.cdoc_grammar' ) 2local c_grammar =require ( 'gen.c_grammar' ) 3local api_type =require ( 'gen.api_types' ) 4 5--- @class nvim.cdoc.parser.param 6--- @field name string 7--- @field type string 8--- @field desc string 9 10--- @class nvim.cdoc.parser.return 11--- @field name string 12--- @field type string 13--- @field desc? string 14 15--- @class nvim.cdoc.parser.note 16--- @field desc string 17 18--- @class nvim.cdoc.parser.brief 19--- @field kind 'brief' 20--- @field desc string 21 22--- @class nvim.cdoc.parser.fun 23--- @field name string 24--- @field params nvim.cdoc.parser.param[] 25--- @field returns nvim.cdoc.parser.return[] 26--- @field desc string 27--- @field deprecated? true 28--- @field deprecated_since? integer 29--- @field since? string|integer 30--- @field attrs? string[] 31--- @field nodoc? true 32--- @field notes? nvim.cdoc.parser.note[] 33--- @field see? nvim.cdoc.parser.note[] 34 35--- @class nvim.cdoc.parser.State 36--- @field doc_lines? string[] 37--- @field cur_obj? nvim.cdoc.parser.obj 38--- @field last_doc_item? nvim.cdoc.parser.param|nvim.cdoc.parser.return|nvim.cdoc.parser.note 39--- @field last_doc_item_indent? integer 40 41--- @alias nvim.cdoc.parser.obj 42--- | nvim.cdoc.parser.fun 43--- | nvim.cdoc.parser.brief 44 45--- If we collected any `---` lines. Add them to the existing (or new) object 46--- Used for function/class descriptions and multiline param descriptions. 47--- @param state nvim.cdoc.parser.State 48local function add_doc_lines_to_obj ( state ) 49if state . doc_lines then 50state . cur_obj =state . cur_obj or {} 51local cur_obj =assert ( state . cur_obj ) 52local txt =table . concat ( state . doc_lines , '\n' ) 53if cur_obj . desc then 54cur_obj . desc =cur_obj . desc .. '\n' .. txt 55else 56cur_obj . desc =txt 57end 58state . doc_lines =nil 59end 60end 61 62--- @param line string 63--- @param state nvim.cdoc.parser.State 64local function process_doc_line ( line , state ) 65line =line : gsub ( '^%s+@' , '@' ) 66 67local parsed =cdoc_grammar : match ( line ) 68 69if not parsed then 70if line : match ( '^ ' ) then 71line =line : sub ( 2 ) 72end 73 74if state . last_doc_item then 75if not state . last_doc_item_indent then 76state . last_doc_item_indent =# line : match ( '^%s*' ) + 1 77end 78state . last_doc_item . desc =( state . last_doc_item . desc or '' ) 79.. '\n' 80.. line : sub ( state . last_doc_item_indent or 1 ) 81else 82state . doc_lines =state . doc_lines or {} 83table . insert ( state . doc_lines , line ) 84end 85return 86end 87 88state . last_doc_item_indent =nil 89state . last_doc_item =nil 90 91local kind =parsed . kind 92 93state . cur_obj =state . cur_obj or {} 94local cur_obj =assert ( state . cur_obj ) 95 96if kind == 'brief' then 97state . cur_obj ={ 98kind ='brief' , 99desc =parsed . desc , 100} 101elseif kind == 'param' then 102state . last_doc_item_indent =nil 103cur_obj . params =cur_obj . params or {} 104state . last_doc_item ={ 105name =parsed . name , 106desc =parsed . desc , 107} 108table . insert ( cur_obj . params , state . last_doc_item ) 109elseif kind == 'return' then 110cur_obj . returns ={ { 111name ='' , 112type ='' , 113desc =parsed . desc , 114} } 115state . last_doc_item_indent =nil 116state . last_doc_item =cur_obj . returns [ 1 ] 117elseif kind == 'deprecated' then 118cur_obj . deprecated =true 119elseif kind == 'nodoc' then 120cur_obj . nodoc =true 121elseif kind == 'since' then 122cur_obj . since =parsed . desc 123elseif kind == 'see' then 124cur_obj . see =cur_obj . see or {} 125table . insert ( cur_obj . see , { desc =parsed . desc } ) 126elseif kind == 'note' then 127state . last_doc_item_indent =nil 128state . last_doc_item ={ 129desc =parsed . desc , 130} 131cur_obj . notes =cur_obj . notes or {} 132table . insert ( cur_obj . notes , state . last_doc_item ) 133else 134error ( 'Unhandled' .. vim . inspect ( parsed )) 135end 136end 137 138--- @param item nvim.c_grammar.Proto 139--- @param state nvim.cdoc.parser.State 140local function process_proto ( item , state ) 141state . cur_obj =state . cur_obj or {} 142local cur_obj =assert ( state . cur_obj ) 143cur_obj . name =item . name 144cur_obj . params =cur_obj . params or {} 145 146local documented ={} --- @type table<string,nvim.cdoc.parser.param> 147local matched ={} --- @type table<nvim.cdoc.parser.param,true> 148local params ={} --- @type nvim.cdoc.parser.param[] 149 150for _ , p in ipairs ( cur_obj . params ) do 151documented [ p . name ] =documented [ p . name ] or p 152end 153 154for _ , p in ipairs ( item . parameters ) do 155local event_type ='vim.api.keyset.events|vim.api.keyset.events[]' 156local event =( item . name == 'nvim_create_autocmd' or item . name == 'nvim_exec_autocmds' ) 157and p [ 2 ] == 'event' 158local param =documented [ p [ 2 ]] 159if param then 160matched [ param ] =true 161else 162param ={ name =p [ 2 ] } --[[@as nvim.cdoc.parser.param]] 163end 164param . type =event and event_type or api_type ( p [ 1 ]) 165params [ # params + 1 ] =param 166end 167 168for _ , p in ipairs ( cur_obj . params ) do 169if not matched [ p ] then 170params [ # params + 1 ] =p 171end 172end 173 174cur_obj . params =params 175 176cur_obj . returns =cur_obj . returns or { { 177name ='' , 178type ='' , 179desc =nil , 180} } 181cur_obj . returns [ 1 ]. type =api_type ( item . return_type ) 182 183for _ , a in ipairs ( { 184'fast' , 185'remote_only' , 186'lua_only' , 187'textlock' , 188} ) do 189if item [ a ] then 190cur_obj . attrs =cur_obj . attrs or {} 191table . insert ( cur_obj . attrs , a ) 192end 193end 194 195cur_obj . since =item . since 196cur_obj . deprecated_since =item . deprecated_since 197 198-- Remove some arguments 199for i =# cur_obj . params , 1 , - 1 do 200local p =cur_obj . params [ i ] 201if p . name == 'channel_id' or vim . tbl_contains ( { 'lstate' , 'arena' , 'error' } , p . type ) then 202table . remove ( cur_obj . params , i ) 203end 204end 205 206-- HACK: Mark optional params (:help api-contract) with "?" so docs render them as optional. 207if c_grammar . opts_index ( item . parameters ) then 208local optional =false 209for _ , p in ipairs ( cur_obj . params ) do 210optional =optional or p . name == 'opts' 211if optional and not p . type : match ( '%?$' ) then 212p . type =p . type .. '?' 213end 214end 215end 216end 217 218local M ={} 219 220--- @param filename string 221--- @return table<string,nvim.luacats.parser.class> classes 222--- @return nvim.cdoc.parser.fun[] funs 223--- @return string[] briefs 224function M . parse ( filename ) 225local funs ={} --- @type nvim.cdoc.parser.fun[] 226local briefs ={} --- @type string[] 227local state ={} --- @type nvim.cdoc.parser.State 228 229local txt =assert ( io . open ( filename , 'r' )): read ( '*all' ) 230 231local parsed =c_grammar . grammar : match ( txt ) 232for _ , item in ipairs ( parsed ) do 233if item . comment then 234process_doc_line ( item . comment , state ) 235else 236add_doc_lines_to_obj ( state ) 237if item [ 1 ] == 'proto' then 238process_proto ( item --[[@as nvim.c_grammar.Proto]] , state ) 239table . insert ( funs , state . cur_obj ) 240end 241local cur_obj =state . cur_obj 242if cur_obj and not item . static then 243if cur_obj . kind == 'brief' then 244table . insert ( briefs , cur_obj . desc ) 245end 246end 247state ={} 248end 249end 250 251return {} , funs , briefs 252end 253 254-- M.parse('src/nvim/api/vim.c') 255 256return M