robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
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 ) 11return x ^ 0 12end 13 14--- @param x vim.lpeg.Pattern 15local function rep1 ( x ) 16return x ^ 1 17end 18 19--- @param x vim.lpeg.Pattern 20local function opt ( x ) 21return 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 ) 34return fill_multi * P ( x ) * fill_multi 35end 36 37--- @param x string | vim.lpeg.Pattern 38local function Plf ( x ) 39return fill_multi * P ( x ) 40end 41 42--- @param x string 43local function Sf ( x ) 44return fill_multi * S ( x ) * fill_multi 45end 46 47--- @param x vim.lpeg.Pattern 48local function paren ( x ) 49return Pf ( '(' ) * x * fill_multi * P ( ')' ) 50end 51 52--- @param x vim.lpeg.Pattern 53local function parenOpt ( x ) 54return paren ( x ) + x 55end 56 57--- @param x vim.lpeg.Pattern 58local function comma1 ( x ) 59return parenOpt ( x * rep ( Pf ( ',' ) * x )) 60end 61 62--- @param x vim.lpeg.Pattern 63local function comma ( x ) 64return opt ( comma1 ( x )) 65end 66 67--- @type table<string,vim.lpeg.Pattern> 68local v =setmetatable ( {} , { 69__index =function ( _ , k ) 70return lpeg . V ( k ) 71end , 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 ) 120if type ( nm ) == 'string' then 121nm =P ( nm ) 122end 123if pat then 124return Ct ( Cg ( P ( nm ), 'kind' ) * fill * pat ) 125end 126return 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' , 148typedef =C ( v . type ), 149 150type =v . ty * rep_array_opt_postfix * rep ( Sf ( '|&' ) * v . ty * rep_array_opt_postfix ), 151ty =v . composite + paren ( v . typedef ), 152composite =( v . types * array_postfix ) 153+ ( v . types * opt_postfix ) 154+ ( P ( ty_ident ) * P ( '...' )) -- Generic vararg 155+ v . types , 156types =v . keyof + v . fun + v . generics + v . kv_table + v . tuple + v . dict + v . table_literal + ty_prims , 157 158keyof =P ( 'keyof' ) * ws * v . ty , 159tuple =Pf ( '[' ) * comma1 ( v . type ) * Plf ( ']' ), 160dict =Pf ( '{' ) * comma1 ( Pf ( '[' ) * v . type * Pf ( ']' ) * colon * v . type ) * Plf ( '}' ), 161kv_table =Pf ( 'table' ) * Pf ( '<' ) * v . type * Pf ( ',' ) * v . type * Plf ( '>' ), 162table_literal =Pf ( '{' ) * comma1 ( opt_ident * Pf ( ':' ) * v . type ) * Plf ( '}' ), 163fun_param =( opt_ident + ellipsis ) * opt ( colon * v . type ), 164fun_ret =v . type + ( ellipsis * opt ( colon * v . type )), 165generic_decl_list =Pf ( '<' ) * comma1 ( ty_ident * opt ( ellipsis ) * opt ( colon * v . type )) * Plf ( '>' ), 166fun =opt ( Pf ( 'async' ) + Pf ( 'sync' )) * Pf ( 'fun' ) * opt ( v . generic_decl_list ) * paren ( 167comma ( v . fun_param ) 168) * opt ( Pf ( ':' ) * comma1 ( v . fun_ret )), 169generics =P ( ty_ident ) * Pf ( '<' ) * comma1 ( v . type ) * Plf ( '>' ), 170} ) / function ( match ) 171return ( vim . trim ( match ): gsub ( '%s*\n%s*' , ' ' ): gsub ( '^%((.*)%)$' , '%1' ): gsub ( '%?+' , '?' )) 172end 173 174--- @param name string 175local function generic_opt ( name ) 176return ( 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 { 191rep1 ( P ( '@' ) * ( v . ats + v . ext_ats )), 192 193ats =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 213ext_ats =( 214annot ( 'note' , desc ) 215+ annot ( 'since' , desc ) 216+ annot ( 'nodoc' ) 217+ annot ( 'inlinedoc' ) 218+ annot ( 'brief' , desc ) 219), 220 221field_name =Cg ( lname + ( v . ty_index * opt ( P ( '?' ))), 'name' ), 222ty_index =C ( Pf ( '[' ) * typedef * fill * P ( ']' )), 223ctype =Cg ( typedef , 'type' ), 224} 225 226return grammar --[[@as nvim.luacats.grammar]]