luacats_grammar.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 C, Ct, Cg = lpeg.C, 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')
  23. local num = R('09')
  24. --- @param x string | vim.lpeg.Pattern
  25. local function Pf(x)
  26. return fill * P(x) * fill
  27. end
  28. --- @param x string | vim.lpeg.Pattern
  29. local function Plf(x)
  30. return fill * P(x)
  31. end
  32. --- @param x string
  33. local function Sf(x)
  34. return fill * S(x) * fill
  35. end
  36. --- @param x vim.lpeg.Pattern
  37. local function paren(x)
  38. return Pf('(') * x * fill * P(')')
  39. end
  40. --- @param x vim.lpeg.Pattern
  41. local function parenOpt(x)
  42. return paren(x) + x
  43. end
  44. --- @param x vim.lpeg.Pattern
  45. local function comma1(x)
  46. return parenOpt(x * rep(Pf(',') * x))
  47. end
  48. --- @param x vim.lpeg.Pattern
  49. local function comma(x)
  50. return opt(comma1(x))
  51. end
  52. --- @type table<string,vim.lpeg.Pattern>
  53. local v = setmetatable({}, {
  54. __index = function(_, k)
  55. return lpeg.V(k)
  56. end,
  57. })
  58. --- @class nvim.luacats.Param
  59. --- @field kind 'param'
  60. --- @field name string
  61. --- @field type string
  62. --- @field desc? string
  63. --- @class nvim.luacats.Return
  64. --- @field kind 'return'
  65. --- @field [integer] { type: string, name?: string}
  66. --- @field desc? string
  67. --- @class nvim.luacats.Generic
  68. --- @field kind 'generic'
  69. --- @field name string
  70. --- @field type? string
  71. --- @class nvim.luacats.Class
  72. --- @field kind 'class'
  73. --- @field name string
  74. --- @field parent? string
  75. --- @field access? 'private'|'protected'|'package'
  76. --- @class nvim.luacats.Field
  77. --- @field kind 'field'
  78. --- @field name string
  79. --- @field type string
  80. --- @field desc? string
  81. --- @field access? 'private'|'protected'|'package'
  82. --- @class nvim.luacats.Note
  83. --- @field desc? string
  84. --- @alias nvim.luacats.grammar.result
  85. --- | nvim.luacats.Param
  86. --- | nvim.luacats.Return
  87. --- | nvim.luacats.Generic
  88. --- | nvim.luacats.Class
  89. --- | nvim.luacats.Field
  90. --- | nvim.luacats.Note
  91. --- @class nvim.luacats.grammar
  92. --- @field match fun(self, input: string): nvim.luacats.grammar.result?
  93. local function annot(nm, pat)
  94. if type(nm) == 'string' then
  95. nm = P(nm)
  96. end
  97. if pat then
  98. return Ct(Cg(P(nm), 'kind') * fill * pat)
  99. end
  100. return Ct(Cg(P(nm), 'kind'))
  101. end
  102. local colon = Pf(':')
  103. local ellipsis = P('...')
  104. local ident_first = P('_') + letter
  105. local ident = ident_first * rep(ident_first + num)
  106. local opt_ident = ident * opt(P('?'))
  107. local ty_ident_sep = S('-._')
  108. local ty_ident = ident * rep(ty_ident_sep * ident)
  109. local string_single = P "'" * rep(any - P "'") * P "'"
  110. local string_double = P('"') * rep(any - P('"')) * P('"')
  111. local generic = P('`') * ty_ident * P('`')
  112. local literal = string_single + string_double + (opt(P('-')) * rep1(num)) + P('false') + P('true')
  113. local ty_prims = ty_ident + literal + generic
  114. local array_postfix = rep1(Plf('[]'))
  115. local opt_postfix = rep1(Plf('?'))
  116. local rep_array_opt_postfix = rep(array_postfix + opt_postfix)
  117. local typedef = P({
  118. 'typedef',
  119. typedef = C(v.type),
  120. type = v.ty * rep_array_opt_postfix * rep(Pf('|') * v.ty * rep_array_opt_postfix),
  121. ty = v.composite + paren(v.typedef),
  122. composite = (v.types * array_postfix) + (v.types * opt_postfix) + v.types,
  123. types = v.generics + v.kv_table + v.tuple + v.dict + v.table_literal + v.fun + ty_prims,
  124. tuple = Pf('[') * comma1(v.type) * Plf(']'),
  125. dict = Pf('{') * comma1(Pf('[') * v.type * Pf(']') * colon * v.type) * Plf('}'),
  126. kv_table = Pf('table') * Pf('<') * v.type * Pf(',') * v.type * Plf('>'),
  127. table_literal = Pf('{') * comma1(opt_ident * Pf(':') * v.type) * Plf('}'),
  128. fun_param = (opt_ident + ellipsis) * opt(colon * v.type),
  129. fun_ret = v.type + (ellipsis * opt(colon * v.type)),
  130. fun = Pf('fun') * paren(comma(v.fun_param)) * opt(Pf(':') * comma1(v.fun_ret)),
  131. generics = P(ty_ident) * Pf('<') * comma1(v.type) * Plf('>'),
  132. }) / function(match)
  133. return vim.trim(match):gsub('^%((.*)%)$', '%1'):gsub('%?+', '?')
  134. end
  135. local opt_exact = opt(Cg(Pf('(exact)'), 'access'))
  136. local access = P('private') + P('protected') + P('package')
  137. local caccess = Cg(access, 'access')
  138. local desc_delim = Sf '#:' + ws
  139. local desc = Cg(rep(any), 'desc')
  140. local opt_desc = opt(desc_delim * desc)
  141. local ty_name = Cg(ty_ident, 'name')
  142. local opt_parent = opt(colon * Cg(ty_ident, 'parent'))
  143. local lname = (ident + ellipsis) * opt(P('?'))
  144. local grammar = P {
  145. rep1(P('@') * (v.ats + v.ext_ats)),
  146. ats = annot('param', Cg(lname, 'name') * ws * v.ctype * opt_desc)
  147. + annot('return', comma1(Ct(v.ctype * opt(ws * (ty_name + Cg(ellipsis, 'name'))))) * opt_desc)
  148. + annot('type', comma1(Ct(v.ctype)) * opt_desc)
  149. + annot('cast', ty_name * ws * opt(Sf('+-')) * v.ctype)
  150. + annot('generic', ty_name * opt(colon * v.ctype))
  151. + annot('class', opt_exact * opt(paren(caccess)) * fill * ty_name * opt_parent)
  152. + annot('field', opt(caccess * ws) * v.field_name * ws * v.ctype * opt_desc)
  153. + annot('operator', ty_name * opt(paren(Cg(v.ctype, 'argtype'))) * colon * v.ctype)
  154. + annot(access)
  155. + annot('deprecated')
  156. + annot('alias', ty_name * opt(ws * v.ctype))
  157. + annot('enum', ty_name)
  158. + annot('overload', v.ctype)
  159. + annot('see', opt(desc_delim) * desc)
  160. + annot('diagnostic', opt(desc_delim) * desc)
  161. + annot('meta'),
  162. --- Custom extensions
  163. ext_ats = (
  164. annot('note', desc)
  165. + annot('since', desc)
  166. + annot('nodoc')
  167. + annot('inlinedoc')
  168. + annot('brief', desc)
  169. ),
  170. field_name = Cg(lname + (v.ty_index * opt(P('?'))), 'name'),
  171. ty_index = C(Pf('[') * typedef * fill * P(']')),
  172. ctype = Cg(typedef, 'type'),
  173. }
  174. return grammar --[[@as nvim.luacats.grammar]]