neovim

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

luacats_grammar.lua (5938B)


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