robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
6.5 KiB226 linesraw
1--[[!
2LPEG grammar for LuaCATS
3]]
4
5local lpeg = vim.lpeg
6local P, R, S = lpeg.P, lpeg.R, lpeg.S
7local C, Ct, Cg = lpeg.C, lpeg.Ct, lpeg.Cg
8
9--- @param x vim.lpeg.Pattern
10local function rep(x)
11  return x ^ 0
12end
13
14--- @param x vim.lpeg.Pattern
15local function rep1(x)
16  return x ^ 1
17end
18
19--- @param x vim.lpeg.Pattern
20local function opt(x)
21  return x ^ -1
22end
23
24local ws = rep1(S(' \t'))
25local fill = opt(ws)
26-- Types may span lines, but return names and inline descriptions may not.
27local fill_multi = rep(S(' \t\n'))
28local any = P(1) -- (consume one character)
29local letter = R('az', 'AZ')
30local num = R('09')
31
32--- @param x string | vim.lpeg.Pattern
33local function Pf(x)
34  return fill_multi * P(x) * fill_multi
35end
36
37--- @param x string | vim.lpeg.Pattern
38local function Plf(x)
39  return fill_multi * P(x)
40end
41
42--- @param x string
43local function Sf(x)
44  return fill_multi * S(x) * fill_multi
45end
46
47--- @param x vim.lpeg.Pattern
48local function paren(x)
49  return Pf('(') * x * fill_multi * P(')')
50end
51
52--- @param x vim.lpeg.Pattern
53local function parenOpt(x)
54  return paren(x) + x
55end
56
57--- @param x vim.lpeg.Pattern
58local function comma1(x)
59  return parenOpt(x * rep(Pf(',') * x))
60end
61
62--- @param x vim.lpeg.Pattern
63local function comma(x)
64  return opt(comma1(x))
65end
66
67--- @type table<string,vim.lpeg.Pattern>
68local v = setmetatable({}, {
69  __index = function(_, k)
70    return lpeg.V(k)
71  end,
72})
73
74--- @class nvim.luacats.Param
75--- @field kind 'param'
76--- @field name string
77--- @field type string
78--- @field desc? string
79
80--- @class nvim.luacats.Return
81--- @field kind 'return'
82--- @field [integer] { type: string, name?: string}
83--- @field desc? string
84
85--- @class nvim.luacats.Generic
86--- @field kind 'generic'
87--- @field name string
88--- @field type? string
89
90--- @class nvim.luacats.Class
91--- @field kind 'class'
92--- @field name string
93--- @field generics? string[]
94--- @field parent? string
95--- @field parent_generics? string[]
96--- @field access? 'private'|'protected'|'package'|'internal'
97
98--- @class nvim.luacats.Field
99--- @field kind 'field'
100--- @field name string
101--- @field type string
102--- @field desc? string
103--- @field access? 'private'|'protected'|'package'|'internal'
104
105--- @class nvim.luacats.Note
106--- @field desc? string
107
108--- @alias nvim.luacats.grammar.result
109--- | nvim.luacats.Param
110--- | nvim.luacats.Return
111--- | nvim.luacats.Generic
112--- | nvim.luacats.Class
113--- | nvim.luacats.Field
114--- | nvim.luacats.Note
115
116--- @class nvim.luacats.grammar : vim.lpeg.Pattern
117--- @field match fun(self, input: string): nvim.luacats.grammar.result?
118
119local function annot(nm, pat)
120  if type(nm) == 'string' then
121    nm = P(nm)
122  end
123  if pat then
124    return Ct(Cg(P(nm), 'kind') * fill * pat)
125  end
126  return Ct(Cg(P(nm), 'kind'))
127end
128
129local colon = Pf(':')
130local ellipsis = P('...')
131local ident_first = P('_') + letter
132local ident = ident_first * rep(ident_first + num)
133local opt_ident = ident * opt(P('?'))
134local ty_ident_sep = S('-._')
135local ty_ident = ident * rep(ty_ident_sep * ident)
136local string_single = P "'" * rep(any - S "'\n") * P "'"
137local string_double = P('"') * rep(any - S('"\n')) * P('"')
138local generic = P('`') * ty_ident * P('`')
139local literal = string_single + string_double + (opt(P('-')) * rep1(num)) + P('false') + P('true')
140local ty_prims = ty_ident + literal + generic
141
142local array_postfix = rep1(Plf('[]'))
143local opt_postfix = rep1(Plf('?'))
144local rep_array_opt_postfix = rep(array_postfix + opt_postfix)
145
146local typedef = P({
147  'typedef',
148  typedef = C(v.type),
149
150  type = v.ty * rep_array_opt_postfix * rep(Sf('|&') * v.ty * rep_array_opt_postfix),
151  ty = v.composite + paren(v.typedef),
152  composite = (v.types * array_postfix)
153    + (v.types * opt_postfix)
154    + (P(ty_ident) * P('...')) -- Generic vararg
155    + v.types,
156  types = v.keyof + v.fun + v.generics + v.kv_table + v.tuple + v.dict + v.table_literal + ty_prims,
157
158  keyof = P('keyof') * ws * v.ty,
159  tuple = Pf('[') * comma1(v.type) * Plf(']'),
160  dict = Pf('{') * comma1(Pf('[') * v.type * Pf(']') * colon * v.type) * Plf('}'),
161  kv_table = Pf('table') * Pf('<') * v.type * Pf(',') * v.type * Plf('>'),
162  table_literal = Pf('{') * comma1(opt_ident * Pf(':') * v.type) * Plf('}'),
163  fun_param = (opt_ident + ellipsis) * opt(colon * v.type),
164  fun_ret = v.type + (ellipsis * opt(colon * v.type)),
165  generic_decl_list = Pf('<') * comma1(ty_ident * opt(ellipsis) * opt(colon * v.type)) * Plf('>'),
166  fun = opt(Pf('async') + Pf('sync')) * Pf('fun') * opt(v.generic_decl_list) * paren(
167    comma(v.fun_param)
168  ) * opt(Pf(':') * comma1(v.fun_ret)),
169  generics = P(ty_ident) * Pf('<') * comma1(v.type) * Plf('>'),
170}) / function(match)
171  return (vim.trim(match):gsub('%s*\n%s*', ' '):gsub('^%((.*)%)$', '%1'):gsub('%?+', '?'))
172end
173
174--- @param name string
175local function generic_opt(name)
176  return (Pf('<') * Cg(Ct(comma1(typedef)), name) * Plf('>')) + -Plf('<')
177end
178
179local access = P('private') + P('protected') + P('package') + P('internal')
180local caccess = Cg(access, 'access')
181local cattr = Cg(comma(access + P('exact')), 'access')
182local desc_delim = fill * S('#:') * fill + ws
183local desc = Cg(rep(any - P('\n')), 'desc')
184local opt_desc = opt(desc_delim * desc)
185local ty_name = Cg(ty_ident, 'name')
186local opt_parent = opt(colon * Cg(ty_ident, 'parent') * generic_opt('parent_generics'))
187local lname = (ident + ellipsis) * opt(P('?'))
188
189-- stylua: ignore
190local grammar = P {
191  rep1(P('@') * (v.ats + v.ext_ats)),
192
193  ats = annot('param', Cg(lname, 'name') * ws * v.ctype * opt_desc)
194    + annot('return_cast', desc)
195    + annot('return', comma1(Ct(v.ctype * opt(ws * (ty_name + Cg(ellipsis, 'name'))))) * opt_desc)
196    + annot('type', comma1(Ct(v.ctype)) * opt_desc)
197    + annot('cast', ty_name * ws * opt(Sf('+-')) * v.ctype)
198    + annot('generic', ty_name * opt(colon * v.ctype))
199    + annot('class', opt(paren(cattr)) * fill * ty_name * generic_opt('generics') * opt_parent)
200    + annot('field', opt(caccess * ws) * v.field_name * ws * v.ctype * opt_desc)
201    + annot('operator', ty_name * opt(paren(Cg(v.ctype, 'argtype'))) * colon * v.ctype)
202    + annot(access)
203    + annot('deprecated')
204    + annot('async')
205    + annot('alias', ty_name * opt(ws * v.ctype))
206    + annot('enum', ty_name)
207    + annot('overload', v.ctype)
208    + annot('see', opt(desc_delim) * desc)
209    + annot('diagnostic', opt(desc_delim) * desc)
210    + annot('meta'),
211
212  --- Custom extensions
213  ext_ats = (
214    annot('note', desc)
215    + annot('since', desc)
216    + annot('nodoc')
217    + annot('inlinedoc')
218    + annot('brief', desc)
219  ),
220
221  field_name = Cg(lname + (v.ty_index * opt(P('?'))), 'name'),
222  ty_index = C(Pf('[') * typedef * fill * P(']')),
223  ctype = Cg(typedef, 'type'),
224}
225
226return grammar --[[@as nvim.luacats.grammar]]