_snippet_grammar.lua 6.2 KB

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