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