luacats_grammar.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. --[[!
  2. LPEG grammar for LuaCATS
  3. ]]
  4. local lpeg = vim.lpeg
  5. local P, R, S = lpeg.P, lpeg.R, lpeg.S
  6. local Ct, Cg = lpeg.Ct, lpeg.Cg
  7. --- @param x vim.lpeg.Pattern
  8. local function rep(x)
  9. return x ^ 0
  10. end
  11. --- @param x vim.lpeg.Pattern
  12. local function rep1(x)
  13. return x ^ 1
  14. end
  15. --- @param x vim.lpeg.Pattern
  16. local function opt(x)
  17. return x ^ -1
  18. end
  19. local ws = rep1(S(' \t'))
  20. local fill = opt(ws)
  21. local any = P(1) -- (consume one character)
  22. local letter = R('az', 'AZ') + S('_$')
  23. local num = R('09')
  24. local ident = letter * rep(letter + num + S '-.')
  25. local string_single = P "'" * rep(any - P "'") * P "'"
  26. local string_double = P('"') * rep(any - P('"')) * P('"')
  27. local literal = (string_single + string_double + (opt(P('-')) * num) + P('false') + P('true'))
  28. local lname = (ident + P('...')) * opt(P('?'))
  29. --- @param x string
  30. local function Pf(x)
  31. return fill * P(x) * fill
  32. end
  33. --- @param x string
  34. local function Sf(x)
  35. return fill * S(x) * fill
  36. end
  37. --- @param x vim.lpeg.Pattern
  38. local function paren(x)
  39. return Pf('(') * x * fill * P(')')
  40. end
  41. --- @param x vim.lpeg.Pattern
  42. local function parenOpt(x)
  43. return paren(x) + x
  44. end
  45. --- @param x vim.lpeg.Pattern
  46. local function comma1(x)
  47. return parenOpt(x * rep(Pf(',') * x))
  48. end
  49. --- @param x vim.lpeg.Pattern
  50. local function comma(x)
  51. return opt(comma1(x))
  52. end
  53. --- @type table<string,vim.lpeg.Pattern>
  54. local v = setmetatable({}, {
  55. __index = function(_, k)
  56. return lpeg.V(k)
  57. end,
  58. })
  59. local colon = Pf(':')
  60. local opt_exact = opt(Cg(Pf('(exact)'), 'access'))
  61. local access = P('private') + P('protected') + P('package')
  62. local caccess = Cg(access, 'access')
  63. local desc_delim = Sf '#:' + ws
  64. local desc = Cg(rep(any), 'desc')
  65. local opt_desc = opt(desc_delim * desc)
  66. local cname = Cg(ident, 'name')
  67. local opt_parent = opt(colon * Cg(ident, 'parent'))
  68. --- @class nvim.luacats.Param
  69. --- @field kind 'param'
  70. --- @field name string
  71. --- @field type string
  72. --- @field desc? string
  73. --- @class nvim.luacats.Return
  74. --- @field kind 'return'
  75. --- @field [integer] { type: string, name?: string}
  76. --- @field desc? string
  77. --- @class nvim.luacats.Generic
  78. --- @field kind 'generic'
  79. --- @field name string
  80. --- @field type? string
  81. --- @class nvim.luacats.Class
  82. --- @field kind 'class'
  83. --- @field name string
  84. --- @field parent? string
  85. --- @field access? 'private'|'protected'|'package'
  86. --- @class nvim.luacats.Field
  87. --- @field kind 'field'
  88. --- @field name string
  89. --- @field type string
  90. --- @field desc? string
  91. --- @field access? 'private'|'protected'|'package'
  92. --- @class nvim.luacats.Note
  93. --- @field desc? string
  94. --- @alias nvim.luacats.grammar.result
  95. --- | nvim.luacats.Param
  96. --- | nvim.luacats.Return
  97. --- | nvim.luacats.Generic
  98. --- | nvim.luacats.Class
  99. --- | nvim.luacats.Field
  100. --- | nvim.luacats.Note
  101. --- @class nvim.luacats.grammar
  102. --- @field match fun(self, input: string): nvim.luacats.grammar.result?
  103. local function annot(nm, pat)
  104. if type(nm) == 'string' then
  105. nm = P(nm)
  106. end
  107. if pat then
  108. return Ct(Cg(P(nm), 'kind') * fill * pat)
  109. end
  110. return Ct(Cg(P(nm), 'kind'))
  111. end
  112. local grammar = P {
  113. rep1(P('@') * (v.ats + v.ext_ats)),
  114. ats = annot('param', Cg(lname, 'name') * ws * v.ctype * opt_desc)
  115. + annot('return', comma1(Ct(v.ctype * opt(ws * cname))) * opt_desc)
  116. + annot('type', comma1(Ct(v.ctype)) * opt_desc)
  117. + annot('cast', cname * ws * opt(Sf('+-')) * v.ctype)
  118. + annot('generic', cname * opt(colon * v.ctype))
  119. + annot('class', opt_exact * opt(paren(caccess)) * fill * cname * opt_parent)
  120. + annot('field', opt(caccess * ws) * v.field_name * ws * v.ctype * opt_desc)
  121. + annot('operator', cname * opt(paren(Cg(v.ltype, 'argtype'))) * colon * v.ctype)
  122. + annot(access)
  123. + annot('deprecated')
  124. + annot('alias', cname * opt(ws * v.ctype))
  125. + annot('enum', cname)
  126. + annot('overload', v.ctype)
  127. + annot('see', opt(desc_delim) * desc)
  128. + annot('diagnostic', opt(desc_delim) * desc)
  129. + annot('meta'),
  130. --- Custom extensions
  131. ext_ats = (
  132. annot('note', desc)
  133. + annot('since', desc)
  134. + annot('nodoc')
  135. + annot('inlinedoc')
  136. + annot('brief', desc)
  137. ),
  138. field_name = Cg(lname + (v.ty_index * opt(P('?'))), 'name'),
  139. ctype = parenOpt(Cg(v.ltype, 'type')),
  140. ltype = parenOpt(v.ty_union),
  141. ty_union = v.ty_opt * rep(Pf('|') * v.ty_opt),
  142. ty = v.ty_fun + ident + v.ty_table + literal + paren(v.ty) + v.ty_generic,
  143. ty_param = Pf('<') * comma1(v.ltype) * fill * P('>'),
  144. ty_opt = v.ty * opt(v.ty_param) * opt(P('[]')) * opt(P('?')),
  145. ty_index = (Pf('[') * (v.ltype + ident + rep1(num)) * fill * P(']')),
  146. table_key = v.ty_index + lname,
  147. table_elem = v.table_key * colon * v.ltype,
  148. ty_table = Pf('{') * comma1(v.table_elem) * fill * P('}'),
  149. fun_param = lname * opt(colon * v.ltype),
  150. ty_fun = Pf('fun') * paren(comma(lname * opt(colon * v.ltype))) * opt(colon * comma1(v.ltype)),
  151. ty_generic = P('`') * letter * P('`'),
  152. }
  153. return grammar --[[@as nvim.luacats.grammar]]