neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

cdoc_grammar.lua (1962B)


      1 --[[!
      2 LPEG 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 
     25 local lpeg = vim.lpeg
     26 local P, R, S = lpeg.P, lpeg.R, lpeg.S
     27 local Ct, Cg = lpeg.Ct, lpeg.Cg
     28 
     29 --- @param x vim.lpeg.Pattern
     30 local function rep(x)
     31  return x ^ 0
     32 end
     33 
     34 --- @param x vim.lpeg.Pattern
     35 local function rep1(x)
     36  return x ^ 1
     37 end
     38 
     39 --- @param x vim.lpeg.Pattern
     40 local function opt(x)
     41  return x ^ -1
     42 end
     43 
     44 local nl = P('\r\n') + P('\n')
     45 local ws = rep1(S(' \t') + nl)
     46 
     47 local any = P(1) -- (consume one character)
     48 local letter = R('az', 'AZ') + S('_$')
     49 local ident = letter * rep(letter + R('09'))
     50 
     51 local io = P('[') * (P('in') + P('out') + P('inout')) * P(']')
     52 
     53 --- @param x string
     54 local function Pf(x)
     55  return opt(ws) * P(x) * opt(ws)
     56 end
     57 
     58 --- @type table<string,vim.lpeg.Pattern>
     59 local v = setmetatable({}, {
     60  __index = function(_, k)
     61    return lpeg.V(k)
     62  end,
     63 })
     64 
     65 local 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 
     87 return grammar --[[@as nvim.cdoc.grammar]]