cdoc_grammar.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. --[[!
  2. LPEG grammar for C doc comments
  3. ]]
  4. --- @class nvim.cdoc.Param
  5. --- @field kind 'param'
  6. --- @field name string
  7. --- @field desc? string
  8. --- @class nvim.cdoc.Return
  9. --- @field kind 'return'
  10. --- @field desc string
  11. --- @class nvim.cdoc.Note
  12. --- @field desc? string
  13. --- @alias nvim.cdoc.grammar.result
  14. --- | nvim.cdoc.Param
  15. --- | nvim.cdoc.Return
  16. --- | nvim.cdoc.Note
  17. --- @class nvim.cdoc.grammar
  18. --- @field match fun(self, input: string): nvim.cdoc.grammar.result?
  19. local lpeg = vim.lpeg
  20. local P, R, S = lpeg.P, lpeg.R, lpeg.S
  21. local Ct, Cg = lpeg.Ct, lpeg.Cg
  22. --- @param x vim.lpeg.Pattern
  23. local function rep(x)
  24. return x ^ 0
  25. end
  26. --- @param x vim.lpeg.Pattern
  27. local function rep1(x)
  28. return x ^ 1
  29. end
  30. --- @param x vim.lpeg.Pattern
  31. local function opt(x)
  32. return x ^ -1
  33. end
  34. local nl = P('\r\n') + P('\n')
  35. local ws = rep1(S(' \t') + nl)
  36. local any = P(1) -- (consume one character)
  37. local letter = R('az', 'AZ') + S('_$')
  38. local ident = letter * rep(letter + R('09'))
  39. local io = P('[') * (P('in') + P('out') + P('inout')) * P(']')
  40. --- @param x string
  41. local function Pf(x)
  42. return opt(ws) * P(x) * opt(ws)
  43. end
  44. --- @type table<string,vim.lpeg.Pattern>
  45. local v = setmetatable({}, {
  46. __index = function(_, k)
  47. return lpeg.V(k)
  48. end,
  49. })
  50. local grammar = P {
  51. rep1(P('@') * v.ats),
  52. ats = v.at_param + v.at_return + v.at_deprecated + v.at_see + v.at_brief + v.at_note + v.at_nodoc,
  53. at_param = Ct(
  54. Cg(P('param'), 'kind') * opt(io) * ws * Cg(ident, 'name') * opt(ws * Cg(rep(any), 'desc'))
  55. ),
  56. at_return = Ct(Cg(P('return'), 'kind') * opt(S('s')) * opt(ws * Cg(rep(any), 'desc'))),
  57. at_deprecated = Ct(Cg(P('deprecated'), 'kind')),
  58. at_see = Ct(Cg(P('see'), 'kind') * ws * opt(Pf('#')) * Cg(rep(any), 'desc')),
  59. at_brief = Ct(Cg(P('brief'), 'kind') * ws * Cg(rep(any), 'desc')),
  60. at_note = Ct(Cg(P('note'), 'kind') * ws * Cg(rep(any), 'desc')),
  61. at_nodoc = Ct(Cg(P('nodoc'), 'kind')),
  62. }
  63. return grammar --[[@as nvim.cdoc.grammar]]