luacats_parser.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. local luacats_grammar = require('scripts.luacats_grammar')
  2. --- @class nvim.luacats.parser.param : nvim.luacats.Param
  3. --- @class nvim.luacats.parser.return
  4. --- @field name string
  5. --- @field type string
  6. --- @field desc string
  7. --- @class nvim.luacats.parser.note
  8. --- @field desc string
  9. --- @class nvim.luacats.parser.brief
  10. --- @field kind 'brief'
  11. --- @field desc string
  12. --- @class nvim.luacats.parser.alias
  13. --- @field kind 'alias'
  14. --- @field type string[]
  15. --- @field desc string
  16. --- @class nvim.luacats.parser.fun
  17. --- @field name string
  18. --- @field params nvim.luacats.parser.param[]
  19. --- @field returns nvim.luacats.parser.return[]
  20. --- @field desc string
  21. --- @field access? 'private'|'package'|'protected'
  22. --- @field class? string
  23. --- @field module? string
  24. --- @field modvar? string
  25. --- @field classvar? string
  26. --- @field deprecated? true
  27. --- @field since? string
  28. --- @field attrs? string[]
  29. --- @field nodoc? true
  30. --- @field generics? table<string,string>
  31. --- @field table? true
  32. --- @field notes? nvim.luacats.parser.note[]
  33. --- @field see? nvim.luacats.parser.note[]
  34. --- @class nvim.luacats.parser.field : nvim.luacats.Field
  35. --- @field classvar? string
  36. --- @field nodoc? true
  37. --- @class nvim.luacats.parser.class : nvim.luacats.Class
  38. --- @field desc? string
  39. --- @field nodoc? true
  40. --- @field inlinedoc? true
  41. --- @field fields nvim.luacats.parser.field[]
  42. --- @field notes? string[]
  43. --- @class nvim.luacats.parser.State
  44. --- @field doc_lines? string[]
  45. --- @field cur_obj? nvim.luacats.parser.obj
  46. --- @field last_doc_item? nvim.luacats.parser.param|nvim.luacats.parser.return|nvim.luacats.parser.note
  47. --- @field last_doc_item_indent? integer
  48. --- @alias nvim.luacats.parser.obj
  49. --- | nvim.luacats.parser.class
  50. --- | nvim.luacats.parser.fun
  51. --- | nvim.luacats.parser.brief
  52. --- | nvim.luacats.parser.alias
  53. -- Remove this when we document classes properly
  54. --- Some doc lines have the form:
  55. --- param name some.complex.type (table) description
  56. --- if so then transform the line to remove the complex type:
  57. --- param name (table) description
  58. --- @param line string
  59. local function use_type_alt(line)
  60. for _, type in ipairs({ 'table', 'function' }) do
  61. line = line:gsub('@param%s+([a-zA-Z_?]+)%s+.*%((' .. type .. ')%)', '@param %1 %2')
  62. line = line:gsub('@param%s+([a-zA-Z_?]+)%s+.*%((' .. type .. '|nil)%)', '@param %1 %2')
  63. line = line:gsub('@param%s+([a-zA-Z_?]+)%s+.*%((' .. type .. '%?)%)', '@param %1 %2')
  64. line = line:gsub('@return%s+.*%((' .. type .. ')%)', '@return %1')
  65. line = line:gsub('@return%s+.*%((' .. type .. '|nil)%)', '@return %1')
  66. line = line:gsub('@return%s+.*%((' .. type .. '%?)%)', '@return %1')
  67. end
  68. return line
  69. end
  70. --- If we collected any `---` lines. Add them to the existing (or new) object
  71. --- Used for function/class descriptions and multiline param descriptions.
  72. --- @param state nvim.luacats.parser.State
  73. local function add_doc_lines_to_obj(state)
  74. if state.doc_lines then
  75. state.cur_obj = state.cur_obj or {}
  76. local cur_obj = assert(state.cur_obj)
  77. local txt = table.concat(state.doc_lines, '\n')
  78. if cur_obj.desc then
  79. cur_obj.desc = cur_obj.desc .. '\n' .. txt
  80. else
  81. cur_obj.desc = txt
  82. end
  83. state.doc_lines = nil
  84. end
  85. end
  86. --- @param line string
  87. --- @param state nvim.luacats.parser.State
  88. local function process_doc_line(line, state)
  89. line = line:sub(4):gsub('^%s+@', '@')
  90. line = use_type_alt(line)
  91. local parsed = luacats_grammar:match(line)
  92. if not parsed then
  93. if line:match('^ ') then
  94. line = line:sub(2)
  95. end
  96. if state.last_doc_item then
  97. if not state.last_doc_item_indent then
  98. state.last_doc_item_indent = #line:match('^%s*') + 1
  99. end
  100. state.last_doc_item.desc = (state.last_doc_item.desc or '')
  101. .. '\n'
  102. .. line:sub(state.last_doc_item_indent or 1)
  103. else
  104. state.doc_lines = state.doc_lines or {}
  105. table.insert(state.doc_lines, line)
  106. end
  107. return
  108. end
  109. state.last_doc_item_indent = nil
  110. state.last_doc_item = nil
  111. state.cur_obj = state.cur_obj or {}
  112. local cur_obj = assert(state.cur_obj)
  113. local kind = parsed.kind
  114. if kind == 'brief' then
  115. state.cur_obj = {
  116. kind = 'brief',
  117. desc = parsed.desc,
  118. }
  119. elseif kind == 'class' then
  120. --- @cast parsed nvim.luacats.Class
  121. cur_obj.kind = 'class'
  122. cur_obj.name = parsed.name
  123. cur_obj.parent = parsed.parent
  124. cur_obj.access = parsed.access
  125. cur_obj.desc = state.doc_lines and table.concat(state.doc_lines, '\n') or nil
  126. state.doc_lines = nil
  127. cur_obj.fields = {}
  128. elseif kind == 'field' then
  129. --- @cast parsed nvim.luacats.Field
  130. parsed.desc = parsed.desc or state.doc_lines and table.concat(state.doc_lines, '\n') or nil
  131. if parsed.desc then
  132. parsed.desc = vim.trim(parsed.desc)
  133. end
  134. table.insert(cur_obj.fields, parsed)
  135. state.doc_lines = nil
  136. elseif kind == 'operator' then
  137. parsed.desc = parsed.desc or state.doc_lines and table.concat(state.doc_lines, '\n') or nil
  138. if parsed.desc then
  139. parsed.desc = vim.trim(parsed.desc)
  140. end
  141. table.insert(cur_obj.fields, parsed)
  142. state.doc_lines = nil
  143. elseif kind == 'param' then
  144. state.last_doc_item_indent = nil
  145. cur_obj.params = cur_obj.params or {}
  146. if vim.endswith(parsed.name, '?') then
  147. parsed.name = parsed.name:sub(1, -2)
  148. parsed.type = parsed.type .. '?'
  149. end
  150. state.last_doc_item = {
  151. name = parsed.name,
  152. type = parsed.type,
  153. desc = parsed.desc,
  154. }
  155. table.insert(cur_obj.params, state.last_doc_item)
  156. elseif kind == 'return' then
  157. cur_obj.returns = cur_obj.returns or {}
  158. for _, t in ipairs(parsed) do
  159. table.insert(cur_obj.returns, {
  160. name = t.name,
  161. type = t.type,
  162. desc = parsed.desc,
  163. })
  164. end
  165. state.last_doc_item_indent = nil
  166. state.last_doc_item = cur_obj.returns[#cur_obj.returns]
  167. elseif kind == 'private' then
  168. cur_obj.access = 'private'
  169. elseif kind == 'package' then
  170. cur_obj.access = 'package'
  171. elseif kind == 'protected' then
  172. cur_obj.access = 'protected'
  173. elseif kind == 'deprecated' then
  174. cur_obj.deprecated = true
  175. elseif kind == 'inlinedoc' then
  176. cur_obj.inlinedoc = true
  177. elseif kind == 'nodoc' then
  178. cur_obj.nodoc = true
  179. elseif kind == 'since' then
  180. cur_obj.since = parsed.desc
  181. elseif kind == 'see' then
  182. cur_obj.see = cur_obj.see or {}
  183. table.insert(cur_obj.see, { desc = parsed.desc })
  184. elseif kind == 'note' then
  185. state.last_doc_item_indent = nil
  186. state.last_doc_item = {
  187. desc = parsed.desc,
  188. }
  189. cur_obj.notes = cur_obj.notes or {}
  190. table.insert(cur_obj.notes, state.last_doc_item)
  191. elseif kind == 'type' then
  192. cur_obj.desc = parsed.desc
  193. parsed.desc = nil
  194. parsed.kind = nil
  195. cur_obj.type = parsed
  196. elseif kind == 'alias' then
  197. state.cur_obj = {
  198. kind = 'alias',
  199. desc = parsed.desc,
  200. }
  201. elseif kind == 'enum' then
  202. -- TODO
  203. state.doc_lines = nil
  204. elseif
  205. vim.tbl_contains({
  206. 'diagnostic',
  207. 'cast',
  208. 'overload',
  209. 'meta',
  210. }, kind)
  211. then
  212. -- Ignore
  213. return
  214. elseif kind == 'generic' then
  215. cur_obj.generics = cur_obj.generics or {}
  216. cur_obj.generics[parsed.name] = parsed.type or 'any'
  217. else
  218. error('Unhandled' .. vim.inspect(parsed))
  219. end
  220. end
  221. --- @param fun nvim.luacats.parser.fun
  222. --- @return nvim.luacats.parser.field
  223. local function fun2field(fun)
  224. local parts = { 'fun(' }
  225. local params = {} ---@type string[]
  226. for _, p in ipairs(fun.params or {}) do
  227. params[#params + 1] = string.format('%s: %s', p.name, p.type)
  228. end
  229. parts[#parts + 1] = table.concat(params, ', ')
  230. parts[#parts + 1] = ')'
  231. if fun.returns then
  232. parts[#parts + 1] = ': '
  233. local tys = {} --- @type string[]
  234. for _, p in ipairs(fun.returns) do
  235. tys[#tys + 1] = p.type
  236. end
  237. parts[#parts + 1] = table.concat(tys, ', ')
  238. end
  239. return {
  240. name = fun.name,
  241. type = table.concat(parts, ''),
  242. access = fun.access,
  243. desc = fun.desc,
  244. nodoc = fun.nodoc,
  245. }
  246. end
  247. --- Function to normalize known form for declaring functions and normalize into a more standard
  248. --- form.
  249. --- @param line string
  250. --- @return string
  251. local function filter_decl(line)
  252. -- M.fun = vim._memoize(function(...)
  253. -- ->
  254. -- function M.fun(...)
  255. line = line:gsub('^local (.+) = memoize%([^,]+, function%((.*)%)$', 'local function %1(%2)')
  256. line = line:gsub('^(.+) = memoize%([^,]+, function%((.*)%)$', 'function %1(%2)')
  257. return line
  258. end
  259. --- @param line string
  260. --- @param state nvim.luacats.parser.State
  261. --- @param classes table<string,nvim.luacats.parser.class>
  262. --- @param classvars table<string,string>
  263. --- @param has_indent boolean
  264. local function process_lua_line(line, state, classes, classvars, has_indent)
  265. line = filter_decl(line)
  266. if state.cur_obj and state.cur_obj.kind == 'class' then
  267. local nm = line:match('^local%s+([a-zA-Z0-9_]+)%s*=')
  268. if nm then
  269. classvars[nm] = state.cur_obj.name
  270. end
  271. return
  272. end
  273. do
  274. local parent_tbl, sep, fun_or_meth_nm =
  275. line:match('^function%s+([a-zA-Z0-9_]+)([.:])([a-zA-Z0-9_]+)%s*%(')
  276. if parent_tbl then
  277. -- Have a decl. Ensure cur_obj
  278. state.cur_obj = state.cur_obj or {}
  279. local cur_obj = assert(state.cur_obj)
  280. -- Match `Class:foo` methods for defined classes
  281. local class = classvars[parent_tbl]
  282. if class then
  283. --- @cast cur_obj nvim.luacats.parser.fun
  284. cur_obj.name = fun_or_meth_nm
  285. cur_obj.class = class
  286. cur_obj.classvar = parent_tbl
  287. -- Add self param to methods
  288. if sep == ':' then
  289. cur_obj.params = cur_obj.params or {}
  290. table.insert(cur_obj.params, 1, {
  291. name = 'self',
  292. type = class,
  293. })
  294. end
  295. -- Add method as the field to the class
  296. local cls = classes[class]
  297. local field = fun2field(cur_obj)
  298. field.classvar = cur_obj.classvar
  299. table.insert(cls.fields, field)
  300. return
  301. end
  302. -- Match `M.foo`
  303. if cur_obj and parent_tbl == cur_obj.modvar then
  304. cur_obj.name = fun_or_meth_nm
  305. return
  306. end
  307. end
  308. end
  309. do
  310. -- Handle: `function A.B.C.foo(...)`
  311. local fn_nm = line:match('^function%s+([.a-zA-Z0-9_]+)%s*%(')
  312. if fn_nm then
  313. state.cur_obj = state.cur_obj or {}
  314. state.cur_obj.name = fn_nm
  315. return
  316. end
  317. end
  318. do
  319. -- Handle: `M.foo = {...}` where `M` is the modvar
  320. local parent_tbl, tbl_nm = line:match('([a-zA-Z_]+)%.([a-zA-Z0-9_]+)%s*=')
  321. if state.cur_obj and parent_tbl and parent_tbl == state.cur_obj.modvar then
  322. state.cur_obj.name = tbl_nm
  323. state.cur_obj.table = true
  324. return
  325. end
  326. end
  327. do
  328. -- Handle: `foo = {...}`
  329. local tbl_nm = line:match('^([a-zA-Z0-9_]+)%s*=')
  330. if tbl_nm and not has_indent then
  331. state.cur_obj = state.cur_obj or {}
  332. state.cur_obj.name = tbl_nm
  333. state.cur_obj.table = true
  334. return
  335. end
  336. end
  337. do
  338. -- Handle: `vim.foo = {...}`
  339. local tbl_nm = line:match('^(vim%.[a-zA-Z0-9_]+)%s*=')
  340. if state.cur_obj and tbl_nm and not has_indent then
  341. state.cur_obj.name = tbl_nm
  342. state.cur_obj.table = true
  343. return
  344. end
  345. end
  346. if state.cur_obj then
  347. if line:find('^%s*%-%- luacheck:') then
  348. state.cur_obj = nil
  349. elseif line:find('^%s*local%s+') then
  350. state.cur_obj = nil
  351. elseif line:find('^%s*return%s+') then
  352. state.cur_obj = nil
  353. elseif line:find('^%s*[a-zA-Z_.]+%(%s+') then
  354. state.cur_obj = nil
  355. end
  356. end
  357. end
  358. --- Determine the table name used to export functions of a module
  359. --- Usually this is `M`.
  360. --- @param str string
  361. --- @return string?
  362. local function determine_modvar(str)
  363. local modvar --- @type string?
  364. for line in vim.gsplit(str, '\n') do
  365. do
  366. --- @type string?
  367. local m = line:match('^return%s+([a-zA-Z_]+)')
  368. if m then
  369. modvar = m
  370. end
  371. end
  372. do
  373. --- @type string?
  374. local m = line:match('^return%s+setmetatable%(([a-zA-Z_]+),')
  375. if m then
  376. modvar = m
  377. end
  378. end
  379. end
  380. return modvar
  381. end
  382. --- @param obj nvim.luacats.parser.obj
  383. --- @param funs nvim.luacats.parser.fun[]
  384. --- @param classes table<string,nvim.luacats.parser.class>
  385. --- @param briefs string[]
  386. --- @param uncommitted nvim.luacats.parser.obj[]
  387. local function commit_obj(obj, classes, funs, briefs, uncommitted)
  388. local commit = false
  389. if obj.kind == 'class' then
  390. --- @cast obj nvim.luacats.parser.class
  391. if not classes[obj.name] then
  392. classes[obj.name] = obj
  393. commit = true
  394. end
  395. elseif obj.kind == 'alias' then
  396. -- Just pretend
  397. commit = true
  398. elseif obj.kind == 'brief' then
  399. --- @cast obj nvim.luacats.parser.brief`
  400. briefs[#briefs + 1] = obj.desc
  401. commit = true
  402. else
  403. --- @cast obj nvim.luacats.parser.fun`
  404. if obj.name then
  405. funs[#funs + 1] = obj
  406. commit = true
  407. end
  408. end
  409. if not commit then
  410. table.insert(uncommitted, obj)
  411. end
  412. return commit
  413. end
  414. --- @param filename string
  415. --- @param uncommitted nvim.luacats.parser.obj[]
  416. -- luacheck: no unused
  417. local function dump_uncommitted(filename, uncommitted)
  418. local out_path = 'luacats-uncommited/' .. filename:gsub('/', '%%') .. '.txt'
  419. if #uncommitted > 0 then
  420. print(string.format('Could not commit %d objects in %s', #uncommitted, filename))
  421. vim.fn.mkdir(vim.fs.dirname(out_path), 'p')
  422. local f = assert(io.open(out_path, 'w'))
  423. for i, x in ipairs(uncommitted) do
  424. f:write(i)
  425. f:write(': ')
  426. f:write(vim.inspect(x))
  427. f:write('\n')
  428. end
  429. f:close()
  430. else
  431. vim.fn.delete(out_path)
  432. end
  433. end
  434. local M = {}
  435. function M.parse_str(str, filename)
  436. local funs = {} --- @type nvim.luacats.parser.fun[]
  437. local classes = {} --- @type table<string,nvim.luacats.parser.class>
  438. local briefs = {} --- @type string[]
  439. local mod_return = determine_modvar(str)
  440. --- @type string
  441. local module = filename:match('.*/lua/([a-z_][a-z0-9_/]+)%.lua') or filename
  442. module = module:gsub('/', '.')
  443. local classvars = {} --- @type table<string,string>
  444. local state = {} --- @type nvim.luacats.parser.State
  445. -- Keep track of any partial objects we don't commit
  446. local uncommitted = {} --- @type nvim.luacats.parser.obj[]
  447. for line in vim.gsplit(str, '\n') do
  448. local has_indent = line:match('^%s+') ~= nil
  449. line = vim.trim(line)
  450. if vim.startswith(line, '---') then
  451. process_doc_line(line, state)
  452. else
  453. add_doc_lines_to_obj(state)
  454. if state.cur_obj then
  455. state.cur_obj.modvar = mod_return
  456. state.cur_obj.module = module
  457. end
  458. process_lua_line(line, state, classes, classvars, has_indent)
  459. -- Commit the object
  460. local cur_obj = state.cur_obj
  461. if cur_obj then
  462. if not commit_obj(cur_obj, classes, funs, briefs, uncommitted) then
  463. --- @diagnostic disable-next-line:inject-field
  464. cur_obj.line = line
  465. end
  466. end
  467. state = {}
  468. end
  469. end
  470. -- dump_uncommitted(filename, uncommitted)
  471. return classes, funs, briefs, uncommitted
  472. end
  473. --- @param filename string
  474. function M.parse(filename)
  475. local f = assert(io.open(filename, 'r'))
  476. local txt = f:read('*all')
  477. f:close()
  478. return M.parse_str(txt, filename)
  479. end
  480. return M