robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0f24b0826a
master
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 ) 31return x ^ 0 32end 33 34--- @param x vim.lpeg.Pattern 35local function rep1 ( x ) 36return x ^ 1 37end 38 39--- @param x vim.lpeg.Pattern 40local function opt ( x ) 41return 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 ) 55return opt ( ws ) * P ( x ) * opt ( ws ) 56end 57 58--- @type table<string,vim.lpeg.Pattern> 59local v =setmetatable ( {} , { 60__index =function ( _ , k ) 61return lpeg . V ( k ) 62end , 63} ) 64 65local grammar =P { 66rep1 ( P ( '@' ) * v . ats ), 67 68ats =v . at_param + v . at_return + v . at_deprecated + v . at_see + v . at_brief + v . at_note + v . at_nodoc , 69 70at_param =Ct ( 71Cg ( P ( 'param' ), 'kind' ) * opt ( io ) * ws * Cg ( ident , 'name' ) * opt ( ws * Cg ( rep ( any ), 'desc' )) 72), 73 74at_return =Ct ( Cg ( P ( 'return' ), 'kind' ) * opt ( S ( 's' )) * opt ( ws * Cg ( rep ( any ), 'desc' ))), 75 76at_deprecated =Ct ( Cg ( P ( 'deprecated' ), 'kind' )), 77 78at_see =Ct ( Cg ( P ( 'see' ), 'kind' ) * ws * opt ( Pf ( '#' )) * Cg ( rep ( any ), 'desc' )), 79 80at_brief =Ct ( Cg ( P ( 'brief' ), 'kind' ) * ws * Cg ( rep ( any ), 'desc' )), 81 82at_note =Ct ( Cg ( P ( 'note' ), 'kind' ) * ws * Cg ( rep ( any ), 'desc' )), 83 84at_nodoc =Ct ( Cg ( P ( 'nodoc' ), 'kind' )), 85} 86 87return grammar --[[@as nvim.cdoc.grammar]]