robin/laufey

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

Lewis Russellbuild: move all generator scripts to `src/gen/`0f24b0826a

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