_snippet_grammar.lua (6377B)
1 --- Grammar for LSP snippets, based on https://microsoft.github.io/language-server-protocol/specification/#snippet_syntax 2 3 local lpeg = vim.lpeg 4 local P, S, R, V = lpeg.P, lpeg.S, lpeg.R, lpeg.V 5 local C, Cg, Ct = lpeg.C, lpeg.Cg, lpeg.Ct 6 7 local M = {} 8 9 local alpha = R('az', 'AZ') 10 local backslash = P('\\') 11 local colon = P(':') 12 local dollar = P('$') 13 local int = R('09') ^ 1 14 local l_brace, r_brace = P('{'), P('}') 15 local pipe = P('|') 16 local slash = P('/') 17 local underscore = P('_') 18 local var = Cg((underscore + alpha) * ((underscore + alpha + int) ^ 0), 'name') 19 local format_capture = Cg(int / tonumber, 'capture') 20 local format_modifier = Cg(P('upcase') + P('downcase') + P('capitalize'), 'modifier') 21 local tabstop = Cg(int / tonumber, 'tabstop') 22 23 -- These characters are always escapable in text nodes no matter the context. 24 local escapable = '$}\\' 25 26 --- Returns a function that unescapes occurrences of "special" characters. 27 --- 28 --- @param special? string 29 --- @return fun(match: string): string 30 local function escape_text(special) 31 special = special or escapable 32 return function(match) 33 local escaped = match:gsub('\\(.)', function(c) 34 return special:find(c) and c or '\\' .. c 35 end) 36 return escaped 37 end 38 end 39 40 --- Returns a pattern for text nodes. Will match characters in `escape` when preceded by a backslash, 41 --- and will stop with characters in `stop_with`. 42 --- 43 --- @param escape string 44 --- @param stop_with? string 45 --- @return vim.lpeg.Pattern 46 local function text(escape, stop_with) 47 stop_with = stop_with or escape 48 return (backslash * S(escape)) + (P(1) - S(stop_with)) 49 end 50 51 -- For text nodes inside curly braces. It stops parsing when reaching an escapable character. 52 local braced_text = (text(escapable) ^ 0) / escape_text() 53 54 -- Within choice nodes, \ also escapes comma and pipe characters. 55 local choice_text = C(text(escapable .. ',|') ^ 1) / escape_text(escapable .. ',|') 56 57 -- Within format nodes, make sure we stop at / 58 local format_text = C(text(escapable, escapable .. '/') ^ 1) / escape_text() 59 60 local if_text, else_text = Cg(braced_text, 'if_text'), Cg(braced_text, 'else_text') 61 62 -- Within ternary condition format nodes, make sure we stop at : 63 local if_till_colon_text = Cg(C(text(escapable, escapable .. ':') ^ 1) / escape_text(), 'if_text') 64 65 -- Matches the string inside //, allowing escaping of the closing slash. 66 local regex = Cg(text('/') ^ 1, 'regex') 67 68 -- Regex constructor flags (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#parameters). 69 local options = Cg(S('dgimsuvy') ^ 0, 'options') 70 71 --- @enum vim.snippet.Type 72 local Type = { 73 Tabstop = 1, 74 Placeholder = 2, 75 Choice = 3, 76 Variable = 4, 77 Format = 5, 78 Text = 6, 79 Snippet = 7, 80 } 81 M.NodeType = Type 82 83 --- @class vim.snippet.Node<T>: { type: vim.snippet.Type, data: T } 84 --- @class vim.snippet.TabstopData: { tabstop: integer } 85 --- @class vim.snippet.TextData: { text: string } 86 --- @class vim.snippet.PlaceholderData: { tabstop: integer, value: vim.snippet.Node<any> } 87 --- @class vim.snippet.ChoiceData: { tabstop: integer, values: string[] } 88 --- @class vim.snippet.VariableData: { name: string, default?: vim.snippet.Node<any>, regex?: string, format?: vim.snippet.Node<vim.snippet.FormatData|vim.snippet.TextData>[], options?: string } 89 --- @class vim.snippet.FormatData: { capture: number, modifier?: string, if_text?: string, else_text?: string } 90 --- @class vim.snippet.SnippetData: { children: vim.snippet.Node<any>[] } 91 92 --- @type vim.snippet.Node<any> 93 local Node = {} 94 95 --- @return string 96 --- @diagnostic disable-next-line: inject-field 97 function Node:__tostring() 98 local node_text = {} 99 local type, data = self.type, self.data 100 if type == Type.Snippet then 101 --- @cast data vim.snippet.SnippetData 102 for _, child in ipairs(data.children) do 103 table.insert(node_text, tostring(child)) 104 end 105 elseif type == Type.Choice then 106 --- @cast data vim.snippet.ChoiceData 107 table.insert(node_text, data.values[1]) 108 elseif type == Type.Placeholder then 109 --- @cast data vim.snippet.PlaceholderData 110 table.insert(node_text, tostring(data.value)) 111 elseif type == Type.Text then 112 --- @cast data vim.snippet.TextData 113 table.insert(node_text, data.text) 114 end 115 return table.concat(node_text) 116 end 117 118 --- Returns a function that constructs a snippet node of the given type. 119 --- 120 --- @generic T 121 --- @param type vim.snippet.Type 122 --- @return fun(data: T): vim.snippet.Node<T> 123 local function node(type) 124 return function(data) 125 return setmetatable({ type = type, data = data }, Node) 126 end 127 end 128 129 -- stylua: ignore 130 --- @diagnostic disable-next-line: missing-fields 131 local G = P({ 132 'snippet'; 133 snippet = Ct(Cg( 134 Ct(( 135 V('any') + 136 (Ct(Cg((text(escapable, '$') ^ 1) / escape_text(), 'text')) / node(Type.Text)) 137 ) ^ 1), 'children' 138 ) * -P(1)) / node(Type.Snippet), 139 any = V('placeholder') + V('tabstop') + V('choice') + V('variable'), 140 any_or_text = V('any') + (Ct(Cg(braced_text, 'text')) / node(Type.Text)), 141 tabstop = Ct(dollar * (tabstop + (l_brace * tabstop * r_brace))) / node(Type.Tabstop), 142 placeholder = Ct(dollar * l_brace * tabstop * colon * Cg(V('any_or_text'), 'value') * r_brace) / node(Type.Placeholder), 143 choice = Ct(dollar * 144 l_brace * 145 tabstop * 146 pipe * 147 Cg(Ct(choice_text * (P(',') * choice_text) ^ 0), 'values') * 148 pipe * 149 r_brace) / node(Type.Choice), 150 variable = Ct(dollar * ( 151 var + ( 152 l_brace * var * ( 153 r_brace + 154 (colon * Cg(V('any_or_text'), 'default') * r_brace) + 155 (slash * regex * slash * Cg(Ct((V('format') + (C(format_text) / node(Type.Text))) ^ 1), 'format') * slash * options * r_brace) 156 )) 157 )) / node(Type.Variable), 158 format = Ct(dollar * ( 159 format_capture + ( 160 l_brace * format_capture * ( 161 r_brace + 162 (colon * ( 163 (slash * format_modifier * r_brace) + 164 (P('+') * if_text * r_brace) + 165 (P('?') * if_till_colon_text * colon * else_text * r_brace) + 166 (P('-') * else_text * r_brace) + 167 (else_text * r_brace) 168 )) 169 )) 170 )) / node(Type.Format), 171 }) 172 173 --- Parses the given input into a snippet tree. 174 --- @param input string 175 --- @return vim.snippet.Node<vim.snippet.SnippetData> 176 function M.parse(input) 177 local snippet = G:match(input) 178 assert(snippet, 'snippet parsing failed') 179 return snippet --- @type vim.snippet.Node<vim.snippet.SnippetData> 180 end 181 182 return M