robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @diagnostic disable: no-unknown 2-- 3-- Copyright 2007-2023, Lua.org & PUC-Rio (see 'lpeg.html' for license) 4-- written by Roberto Ierusalimschy 5-- 6--- vendored from lpeg-1.1.0 7--- documentation available at runtime/lua/vim/_meta/re.lua 8 9-- imported functions and modules 10local tonumber , type , error =tonumber , type , error 11local setmetatable =setmetatable 12local m =require "lpeg" 13 14-- 'm' will be used to parse expressions, and 'mm' will be used to 15-- create expressions; that is, 're' runs on 'm', creating patterns 16-- on 'mm' 17local mm =m 18 19-- patterns' metatable 20local mt =getmetatable ( mm . P ( 0 )) 21 22 23local version =_VERSION 24 25-- No more global accesses after this point 26_ENV =nil -- does no harm in Lua 5.1 27 28 29local any =m . P ( 1 ) 30 31 32-- Pre-defined names 33local Predef ={ nl =m . P "\n" } 34 35 36local mem 37local fmem 38local gmem 39 40 41local function updatelocale () 42mm . locale ( Predef ) 43Predef . a =Predef . alpha 44Predef . c =Predef . cntrl 45Predef . d =Predef . digit 46Predef . g =Predef . graph 47Predef . l =Predef . lower 48Predef . p =Predef . punct 49Predef . s =Predef . space 50Predef . u =Predef . upper 51Predef . w =Predef . alnum 52Predef . x =Predef . xdigit 53Predef . A =any - Predef . a 54Predef . C =any - Predef . c 55Predef . D =any - Predef . d 56Predef . G =any - Predef . g 57Predef . L =any - Predef . l 58Predef . P =any - Predef . p 59Predef . S =any - Predef . s 60Predef . U =any - Predef . u 61Predef . W =any - Predef . w 62Predef . X =any - Predef . x 63mem ={} -- restart memoization 64fmem ={} 65gmem ={} 66local mt0 ={ __mode ="v" } 67setmetatable ( mem , mt0 ) 68setmetatable ( fmem , mt0 ) 69setmetatable ( gmem , mt0 ) 70end 71 72 73updatelocale () 74 75 76 77-- local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end) 78 79 80local function patt_error ( s , i ) 81local msg =( # s < i + 20 ) and s : sub ( i ) 82or s : sub ( i , i + 20 ) .. "..." 83msg =( "pattern error near '%s'" ): format ( msg ) 84error ( msg , 2 ) 85end 86 87local function mult ( p , n ) 88local np =mm . P ( true ) 89while n >= 1 do 90if n % 2 >= 1 then np =np * p end 91p =p * p 92n =n / 2 93end 94return np 95end 96 97local function equalcap ( s , i , c ) 98if type ( c ) ~= "string" then return nil end 99local e =# c + i 100if s : sub ( i , e - 1 ) == c then return e else return nil end 101end 102 103 104local S =( Predef . space + "--" * ( any - Predef . nl ) ^ 0 ) ^ 0 105 106local name =m . R ( "AZ" , "az" , "__" ) * m . R ( "AZ" , "az" , "__" , "09" ) ^ 0 107 108local arrow =S * "<-" 109 110local seq_follow =m . P "/" + ")" + "}" + ":}" + "~}" + "|}" + ( name * arrow ) + - 1 111 112name =m . C ( name ) 113 114 115-- a defined name only have meaning in a given environment 116local Def =name * m . Carg ( 1 ) 117 118 119local function getdef ( id , defs ) 120local c =defs and defs [ id ] 121if not c then error ( "undefined name: " .. id ) end 122return c 123end 124 125-- match a name and return a group of its corresponding definition 126-- and 'f' (to be folded in 'Suffix') 127local function defwithfunc ( f ) 128return m . Cg ( Def / getdef * m . Cc ( f )) 129end 130 131 132local num =m . C ( m . R "09" ^ 1 ) * S / tonumber 133 134local String ="'" * m . C (( any - "'" ) ^ 0 ) * "'" + 135'"' * m . C (( any - '"' ) ^ 0 ) * '"' 136 137 138local defined ="%" * Def / function ( c , Defs ) 139local cat =Defs and Defs [ c ] or Predef [ c ] 140if not cat then error ( "name '" .. c .. "' undefined" ) end 141return cat 142end 143 144local Range =m . Cs ( any * ( m . P "-" / "" ) * ( any - "]" )) / mm . R 145 146local item =( defined + Range + m . C ( any )) / m . P 147 148local Class = 149"[" 150* ( m . C ( m . P "^" ^- 1 )) -- optional complement symbol 151* ( item * (( item % mt . __add ) - "]" ) ^ 0 ) / 152function ( c , p ) return c == "^" and any - p or p end 153* "]" 154 155local function adddef ( t , k , exp ) 156if t [ k ] then 157error ( "'" .. k .. "' already defined as a rule" ) 158else 159t [ k ] =exp 160end 161return t 162end 163 164local function firstdef ( n , r ) return adddef ( { n } , n , r ) end 165 166 167local function NT ( n , b ) 168if not b then 169error ( "rule '" .. n .. "' used outside a grammar" ) 170else return mm . V ( n ) 171end 172end 173 174 175local exp =m . P { "Exp" , 176Exp =S * ( m . V "Grammar" 177+ m . V "Seq" * ( "/" * S * m . V "Seq" % mt . __add ) ^ 0 ); 178Seq =( m . Cc ( m . P "" ) * ( m . V "Prefix" % mt . __mul ) ^ 0 ) 179* ( # seq_follow + patt_error ); 180Prefix ="&" * S * m . V "Prefix" / mt . __len 181+ "!" * S * m . V "Prefix" / mt . __unm 182+ m . V "Suffix" ; 183Suffix =m . V "Primary" * S * 184( ( m . P "+" * m . Cc ( 1 , mt . __pow ) 185+ m . P "*" * m . Cc ( 0 , mt . __pow ) 186+ m . P "?" * m . Cc ( - 1 , mt . __pow ) 187+ "^" * ( m . Cg ( num * m . Cc ( mult )) 188+ m . Cg ( m . C ( m . S "+-" * m . R "09" ^ 1 ) * m . Cc ( mt . __pow )) 189) 190+ "->" * S * ( m . Cg (( String + num ) * m . Cc ( mt . __div )) 191+ m . P "{}" * m . Cc ( nil , m . Ct ) 192+ defwithfunc ( mt . __div ) 193) 194+ "=>" * S * defwithfunc ( mm . Cmt ) 195+ ">>" * S * defwithfunc ( mt . __mod ) 196+ "~>" * S * defwithfunc ( mm . Cf ) 197) % function ( a , b , f ) return f ( a , b ) end * S 198) ^ 0 ; 199Primary ="(" * m . V "Exp" * ")" 200+ String / mm . P 201+ Class 202+ defined 203+ "{:" * ( name * ":" + m . Cc ( nil )) * m . V "Exp" * ":}" / 204function ( n , p ) return mm . Cg ( p , n ) end 205+ "=" * name / function ( n ) return mm . Cmt ( mm . Cb ( n ), equalcap ) end 206+ m . P "{}" / mm . Cp 207+ "{~" * m . V "Exp" * "~}" / mm . Cs 208+ "{|" * m . V "Exp" * "|}" / mm . Ct 209+ "{" * m . V "Exp" * "}" / mm . C 210+ m . P "." * m . Cc ( any ) 211+ ( name * - arrow + "<" * name * ">" ) * m . Cb ( "G" ) / NT ; 212Definition =name * arrow * m . V "Exp" ; 213Grammar =m . Cg ( m . Cc ( true ), "G" ) * 214(( m . V "Definition" / firstdef ) * ( m . V "Definition" % adddef ) ^ 0 ) / mm . P 215} 216 217--- @type vim.lpeg.Pattern 218local pattern =S * m . Cg ( m . Cc ( false ), "G" ) * exp / mm . P * ( - any + patt_error ) 219 220 221local function compile ( p , defs ) 222if mm . type ( p ) == "pattern" then return p end -- already compiled 223local cp =pattern : match ( p , 1 , defs ) 224if not cp then error ( "incorrect pattern" , 3 ) end 225return cp 226end 227 228local function match ( s , p , i ) 229local cp =mem [ p ] 230if not cp then 231cp =compile ( p ) 232mem [ p ] =cp 233end 234return cp : match ( s , i or 1 ) 235end 236 237local function find ( s , p , i ) 238local cp =fmem [ p ] 239if not cp then 240cp =compile ( p ) / 0 241cp =mm . P { mm . Cp () * cp * mm . Cp () + 1 * mm . V ( 1 ) } 242fmem [ p ] =cp 243end 244local e 245i , e =cp : match ( s , i or 1 ) 246if i then return i , e - 1 247else return i 248end 249end 250 251local function gsub ( s , p , rep ) 252local g =gmem [ p ] or {} -- ensure gmem[p] is not collected while here 253gmem [ p ] =g 254local cp =g [ rep ] 255if not cp then 256cp =compile ( p ) 257cp =mm . Cs (( cp / rep + 1 ) ^ 0 ) 258g [ rep ] =cp 259end 260return cp : match ( s ) 261end 262 263 264-- exported names 265local re ={ 266compile =compile , 267match =match , 268find =find , 269gsub =gsub , 270updatelocale =updatelocale , 271} 272 273if version == "Lua 5.1" then _G . re =re end 274 275return re