local A={cacheSize=256,unicodeData=false}local m=A.unicodeData and require(script:WaitForChild("\095\117\110\105\099\111\100\101\099\104\097\114\095\099\097\116\101\103\111\114\121"))local Y=A.unicodeData and require(script:WaitForChild("\095\115\099\114\105\112\116\115"))local x=A.unicodeData and require(script:WaitForChild("\095\120\117\099"))local a=setmetatable({},{__mode="\107"})local s,t,I={},{},{}local Q local function Z(A,m)if m then A=string.sub(A,utf8.offset(A,m))end local Y=utf8.len(A)if Y<=1999 then return{n=Y;s=A;utf8.codepoint(A,1,#A)}end local x=math.ceil(Y/1999)local a=table.create(Y)local s=1 for m=1,x,1 do local t=table.pack(utf8.codepoint(A,utf8.offset(A,m*1999-1998),utf8.offset(A,m*1999-(m==x and 1998-((Y-1)%1999+1)or-1))-1))table.move(t,1,t.n,s,a)s+=t.n end a.s,a.n=A,Y return a end local function W(A)local m=A.n or#A if m<=7997 then return utf8.char(table.unpack(A))end local Y=math.ceil(m/7997)local x=table.create(Y)for a=1,Y,1 do x[a]=utf8.char(table.unpack(A,a*7997-7996,a*7997-(a==Y and 7997-((m-1)%7997+1)or 0)))end return table.concat(x)end local function D(A,m,Y)Y=utf8.offset(A,Y)return string.sub(A,utf8.offset(A,m),Y and Y-1)end local o={a="\097\110\099\104\111\114\101\100";i="\099\097\115\101\108\101\115\115",m="\109\117\108\116\105\108\105\110\101",s="\100\111\116\097\108\108",u="\117\110\105\099\111\100\101";U="\117\110\103\114\101\101\100\121";x="\101\120\116\101\110\100\101\100"}local P={alnum=true,alpha=true;ascii=true;blank=true,cntrl=true;digit=true;graph=true;lower=true,print=true;punct=true,space=true,upper=true,word=true;xdigit=true}local q={[68]={"\099\108\097\115\115";"\100\105\103\105\116",true};[83]={"\099\108\097\115\115";"\115\112\097\099\101";true};[87]={"\099\108\097\115\115";"\119\111\114\100",true};[100]={"\099\108\097\115\115","\100\105\103\105\116",false};[115]={"\099\108\097\115\115","\115\112\097\099\101",false},[119]={"\099\108\097\115\115","\119\111\114\100",false},[72]={"\099\108\097\115\115";"\098\108\097\110\107",true};[86]={"\099\108\097\115\115";"\118\101\114\116\105\099\097\108\095\116\097\098";true};[104]={"\099\108\097\115\115";"\098\108\097\110\107";false};[118]={"\099\108\097\115\115";"\118\101\114\116\105\099\097\108\095\116\097\098",false};[78]={78};[82]={82},[66]=8,[110]=10;[114]=13,[116]=9}local i={[98]={98,{"\099\108\097\115\115";"\119\111\114\100";false}},[66]={66,{"\099\108\097\115\115";"\119\111\114\100",false}},[75]={75};[71]={71},[74]={74};[90]={90};[122]={122}}local c={C=true,Cc=true;Cf=true;Cn=true;Co=true,Cs=true;L=true,Ll=true,Lm=true;Lo=true,Lt=true;Lu=true;M=true,Mc=true;Me=true,Mn=true;N=true;Nd=true,Nl=true;No=true;P=true;Pc=true,Pd=true;Pe=true,Pf=true;Pi=true;Po=true,Ps=true,S=true;Sc=true;Sk=true;Sm=true;So=true;Z=true,Zl=true;Zp=true;Zs=true,Xan=true,Xps=true;Xsp=true,Xuc=true,Xwd=true}local j={[33]=true;[34]=true;[35]=true,[36]=true,[37]=true,[38]=true;[39]=true,[40]=true,[41]=true;[42]=true,[43]=true;[44]=true,[45]=true,[46]=true,[47]=true,[58]=true,[59]=true;[60]=true;[61]=true;[62]=true;[63]=true;[64]=true,[91]=true,[92]=true,[93]=true,[94]=true,[95]=true,[96]=true,[123]=true;[124]=true;[125]=true;[126]=true}local r={36}local B={46}local b={94}local p={124}local function M(A,m,Y)if A=="\077\097\116\099\104"then return function(...)local A=select("\035",...)if A<1 then error("\109\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\049 \040\077\097\116\099\104 \101\120\112\101\099\116\101\100\041",2)end local x,s=...if not(a[x]and a[x].name=="\077\097\116\099\104")then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\049 \116\111 \037\113 \040\077\097\116\099\104 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",m,typeof(x)),2)else x=a[x]end if m=="\103\114\111\117\112"or m=="\115\112\097\110"then if s==nil then s=0 end end return Y(x,s)end end return function(...)local A=select("\035",...)if A<1 then error("\109\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\049 \040\082\101\103\069\120 \101\120\112\101\099\116\101\100\041",2)elseif A<2 then error("\109\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\050 \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\041",2)end local x,t,I,Q,Z,W=...if not(a[x]and a[x].name=="\082\101\103\069\120")then if type(x)~="\115\116\114\105\110\103"and type(x)~="\110\117\109\098\101\114"then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\049 \116\111 \037\113 \040\082\101\103\069\120 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",m,typeof(x)),2)end x=s.fromstring(x)elseif m=="\115\117\098"then if type(I)=="\110\117\109\098\101\114"then I..=""elseif type(I)~="\115\116\114\105\110\103"then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\051 \116\111 \'\115\117\098\' \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(I)),2)end elseif type(t)=="\110\117\109\098\101\114"then t..=""elseif type(t)~="\115\116\114\105\110\103"then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\050 \116\111 \037\113 \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",m,typeof(t)),2)end if m~="\115\117\098"and m~="\115\112\108\105\116"then local A=typeof(I)if A~="\110\105\108"then I=tonumber(I)if not I then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\051 \116\111 \037\113 \040\110\117\109\098\101\114 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",m,A),2)elseif I<0 then I=(#t+math.floor(I+.5))+1 else I=math.max(math.floor(I+.5),1)end end end x=a[x]if m=="\109\097\116\099\104"or m=="\109\097\116\099\104\105\116\101\114"then Q=...elseif m=="\115\117\098"then W=...end return Y(x,t,I,Q,Z,W)end end local function v(A)local m=a[A].spans local Y,x=m[0][1],m[0][2]if x<=Y then return string.format("\077\097\116\099\104 \040\037\100\046\046\037\100\044 \101\109\112\116\121\041",Y,x-1)end return string.format("\077\097\116\099\104 \040\037\100\046\046\037\100\041\058 \037\115",Y,x-1,D(m.input,Y,x))end local function H(A,m,Y,x)A.source,A.input=Y,x local s=newproxy(true)local t=getmetatable(s)t.__metatable=Q t.__index=setmetatable(A,I)t.__tostring=v a[s]={name="\077\097\116\099\104";spans=A;group_id=m}return s end I.group=M("\077\097\116\099\104","\103\114\111\117\112",function(A,m)local Y=A.spans[type(m)=="\110\117\109\098\101\114"and m or A.group_id[m]]if not Y then return nil end return D(A.spans.input,Y[1],Y[2])end)I.span=M("\077\097\116\099\104","\115\112\097\110",function(A,m)local Y=A.spans[type(m)=="\110\117\109\098\101\114"and m or A.group_id[m]]if not Y then return nil end return Y[1],Y[2]-1 end)I.groups=M("\077\097\116\099\104","\103\114\111\117\112\115",function(A)local m=A.spans if m.n>0 then local A=table.create(m.n)for Y=0,m.n,1 do local x=m[Y]if x then A[Y]=D(m.input,x[1],x[2])end end return table.unpack(A,1,m.n)end return D(m.input,m[0][1],m[0][2])end)I.groupdict=M("\077\097\116\099\104","\103\114\111\117\112\100\105\099\116",function(A)local m=A.spans local Y={}for A,x in pairs(A.group_id)do x=m[x]if x then Y[A]=D(m.input,x[1],x[2])end end return Y end)I.grouparr=M("\077\097\116\099\104","\103\114\111\117\112\100\105\099\116",function(A)local m=A.spans local Y=table.create(m.n)for A=0,m.n,1 do local x=m[A]if x then Y[A]=D(m.input,x[1],x[2])end end Y.n=m.n return Y end)local R={CR=0;LF=1;CRLF=2;ANYRLF=3;ANY=4,NUL=5}local function F(A,m,Y)local x=Y.newline local a=A[m]if x==0 then return a==13 elseif x==2 then return a==10 and A[m-1]==32 elseif x==3 then return a==10 or a==13 elseif x==4 then return a==10 or a==11 or a==12 or a==13 or a==133 or a==8232 or a==8233 elseif x==5 then return a==0 end return a==10 end local function E(A,Y,a,s,t)local I=Y[a]if not I then return false elseif s.ignoreCase and(I>=97 and I<=122)then I-=32 end if type(A)=="\110\117\109\098\101\114"then return A==I elseif A[1]=="\099\104\097\114\115\101\116"then for m,x in ipairs(A[3])do if E(x,Y,a,s,t)then return not A[2]end end return A[2]elseif A[1]=="\114\097\110\103\101"then return I>=A[2]and I<=A[3]or s.ignoreCase and(I>=65 and(I<=90 and(I+32>=A[2]and I+32<=A[3])))elseif A[1]=="\099\108\097\115\115"then local Y=A[2]local x=A[3]local a=false if Y=="\120\100\105\103\105\116"then a=I>=48 and I<=57 or I>=65 and I<=70 or I>=97 and I<=102 elseif Y=="\097\115\099\105\105"then a=I<=127 elseif Y=="\118\101\114\116\105\099\097\108\095\116\097\098"then a=I>=10 and I<=13 or I==8232 or I==8233 elseif s.unicode then local A=m[I]or"\067\110"local x=A:sub(1,1)if Y=="\097\108\110\117\109"then a=x=="\076"or A=="\078\108"or A=="\078\100"elseif Y=="\097\108\112\104\097"then a=x=="\076"or A=="\078\108"elseif Y=="\098\108\097\110\107"then a=A=="\090\115"or I==9 elseif Y=="\099\110\116\114\108"then a=A=="\067\099"elseif Y=="\100\105\103\105\116"then a=A=="\078\100"elseif Y=="\103\114\097\112\104"then a=x~="\080"and x~="\067"elseif Y=="\108\111\119\101\114"then a=A=="\076\108"elseif Y=="\112\114\105\110\116"then a=x~="\067"elseif Y=="\112\117\110\099\116"then a=x=="\080"elseif Y=="\115\112\097\099\101"then a=x=="\090"or I>=9 and I<=13 elseif Y=="\117\112\112\101\114"then a=A=="\076\117"elseif Y=="\119\111\114\100"then a=x=="\076"or A=="\078\108"or A=="\078\100"or A=="\080\099"end elseif Y=="\097\108\110\117\109"then a=I>=48 and I<=57 or I>=65 and I<=90 or I>=97 and I<=122 elseif Y=="\097\108\112\104\097"then a=I>=65 and I<=90 or I>=97 and I<=122 elseif Y=="\098\108\097\110\107"then a=I==9 or I==32 elseif Y=="\099\110\116\114\108"then a=I<=31 or I==127 elseif Y=="\100\105\103\105\116"then a=I>=48 and I<=57 elseif Y=="\103\114\097\112\104"then a=I>=33 and I<=126 elseif Y=="\108\111\119\101\114"then a=I>=97 and I<=122 elseif Y=="\112\114\105\110\116"then a=I>=32 and I<=126 elseif Y=="\112\117\110\099\116"then a=j[I]elseif Y=="\115\112\097\099\101"then a=I>=9 and I<=13 or I==32 elseif Y=="\117\112\112\101\114"then a=I>=65 and I<=90 elseif Y=="\119\111\114\100"then a=I>=48 and I<=57 or I>=65 and I<=90 or I>=97 and I<=122 or I==95 end if x then return not a end return a elseif A[1]=="\099\097\116\101\103\111\114\121"then local Q=m[I]or"\067\110"local Z=A[3]local W=#Z if W==3 then local m=false if Z=="\088\097\110"or Z=="\088\119\100"then m=Q:find("\094\091\076\078\093")or Z=="\088\119\100"and I==95 elseif Z=="\088\112\115"or Z=="\088\115\112"then m=Q:sub(1,1)=="\090"or I>=9 and I<=13 elseif Z=="\088\117\099"then m=E(x,Y,a,s,t)end if A[2]then return not m end return m elseif Q:sub(1,W)==Z then return not A[2]end return A[2]elseif A[1]==46 then return s.dotAll or not F(Y,a,t)elseif A[1]==78 then return not F(Y,a,t)elseif A[1]==82 then if t.newline_seq==0 then return I==10 or I==13 end return I==10 or I==11 or I==12 or I==13 or I==133 or I==8232 or I==8233 end return false end local function G(A,m,Y)while true do local x=A[m]local a=type(x)=="\116\097\098\108\101"if x==p then return m,Y elseif a and x[1]==40 then if Y then Y+=x.count end m=x[3]elseif a and(x[1]=="\113\117\097\110\116\105\102\105\101\114"and(type(x[5])=="\116\097\098\108\101"and x[5][1]==40))then if Y then Y+=x[5].count end m=x[5][3]elseif not x or a and x[1]==41 then return nil,Y elseif Y then if a and x[1]=="\113\117\097\110\116\105\102\105\101\114"then Y+=x[3]else Y+=1 end end m+=1 end end local function d(A,m,Y,x,a,s)local t,I,Q=0,Y,Y local Z={}while t do if t==0 then t+=1 local m=G(A,t)if m then table.insert(Z,1,{"\097\108\116\101\114\110\097\116\105\111\110",m,I})end continue end local W=A[t]local o=type(W)=="\116\097\098\108\101"and W[1]if not W then break elseif W=="\065\067\067\069\080\084"then local m=true local Y=t repeat Y+=1 local x=type(A[Y])=="\116\097\098\108\101"local a=A[Y]if x and(a[1]==40 or a[1]=="\113\117\097\110\116\105\102\105\101\114"and(type(a[5])=="\116\097\098\108\101"and a[5][1]==40))then Y=a[1]=="\113\117\097\110\116\105\102\105\101\114"and a[5][3]or a[3]elseif x and(a[1]==41 and(a[4]==33 or a[4]==61))then m=false t=Y break end until not a if m then break end elseif W=="\080\082\085\078\069"or W=="\083\075\073\080"then table.insert(Z,1,{W;I})t+=1 elseif o==40 then table.insert(Z,1,{"\103\114\111\117\112";t;I;nil;W[2];W[3];W[4]})t+=1 local m,Y=G(A,t,(W[4]==33 or W[4]==61)and(W[5]and 0))if m then table.insert(Z,1,{"\097\108\116\101\114\110\097\116\105\111\110";m,I})end if Y then I-=Y end elseif o==41 and W[4]~=33 then if W[4]==33 or W[4]==61 then while true do local A local m=table.remove(Z,1)if m[1]=="\103\114\111\117\112"and m[2]==W[3]then if(W[4]==33 or W[4]==61)and not W[5]then I=m[3]end if A then table.insert(Z,1,A)end break elseif m[1]=="\109\097\116\099\104\083\116\097\114\116"and(not A and W[4]==61)then A=m end end elseif W[4]==62 then repeat local A=table.remove(Z,1)until not A or A[1]=="\103\114\111\117\112"and A[2]==W[3]else for m,Y in ipairs(Z)do if Y[1]=="\103\114\111\117\112"and Y[2]==W[3]then if Y.jmp then t=Y.jmp end Y[4]=I if Y[7]=="\113\117\097\110\116\105\102\105\101\114"and Y[10]+1<Y[9]then if A[W[3]][4]~="\108\097\122\121"or Y[10]+1<Y[8]then t=W[3]end local m=A[W[3]]local x={"\103\114\111\117\112";Y[2];I;nil;m[5][2];m[5][3],"\113\117\097\110\116\105\102\105\101\114";m[2],m[3],Y[10]+1;Y[11],m[4]}table.insert(Z,1,x)if Y[11]then table.insert(Z,1,{"\097\108\116\101\114\110\097\116\105\111\110",Y[11];I})end end break end end end t+=1 elseif o==75 then table.insert(Z,1,{"\109\097\116\099\104\083\116\097\114\116",I})t+=1 elseif o==124 then local m=t repeat m+=1 local Y=type(A[m])=="\116\097\098\108\101"local x=A[m]if Y and(x[1]==40 or x[1]=="\113\117\097\110\116\105\102\105\101\114"and(type(x[5])=="\116\097\098\108\101"and x[5][1]==40))then m=x[1]=="\113\117\097\110\116\105\102\105\101\114"and x[5][3]or x[3]end until Y and x[1]==41 or not x if A[m]then for A,Y in ipairs(Z)do if Y[1]=="\103\114\111\117\112"and Y[6]==m then t=Y[6]break end end else t=m end elseif o=="\114\101\099\117\114\109\097\116\099\104"then table.insert(Z,1,{"\103\114\111\117\112",W[3];I,nil;nil,A[W[3]][3];nil,jmp=t})t=W[3]+1 local m,Y=G(A,t)if m then table.insert(Z,1,{"\097\108\116\101\114\110\097\116\105\111\110",m,I})end else local P if W=="\070\065\073\076"then P=false elseif o==41 then repeat local A=table.remove(Z,1)until A[1]=="\103\114\111\117\112"and A[2]==W[3]elseif o=="\113\117\097\110\116\105\102\105\101\114"then if type(W[5])=="\116\097\098\108\101"and W[5][1]==40 then local m=G(A,t+1)if m then table.insert(Z,1,{"\097\108\116\101\114\110\097\116\105\111\110",m,I})end table.insert(Z,m and 2 or 1,{"\103\114\111\117\112";t,I,nil,W[5][2];W[5][3];"\113\117\097\110\116\105\102\105\101\114";W[2],W[3],0,m,W[4]})if W[4]=="\108\097\122\121"and W[2]==0 then t=W[5][3]end P=true else local A,Y local s=1 local Q=type(W[5])=="\116\097\098\108\101"and W[5][1]=="\098\097\099\107\114\101\102"if Q then s=0 local m=W[5][2]for x,a in ipairs(Z)do if a[1]=="\103\114\111\117\112"and a[5]==m then A,Y=a[3],a[4]s=Y-A break end end end local o=I+W[2]*s local q=0 while q<W[3]do if Q then if A and Y then local x=I if D(m.s,A,Y)~=D(m.s,x,I+s)then break end else break end elseif not E(W[5],m,I,x,a)then break end I+=s q+=1 end P=q>=W[2]if P and W[4]~="\112\111\115\115\101\115\115\105\118\101"then if W[4]=="\108\097\122\121"then o,I=I,o end table.insert(Z,1,{"\113\117\097\110\116\105\102\105\101\114";t,I,math.min(o,m.n+1),(W[4]=="\108\097\122\121"and 1 or-1)*s})end end elseif o=="\098\097\099\107\114\101\102"then local A,Y local x=W[2]for m,a in ipairs(Z)do if a[1]=="\103\114\111\117\112"and a[5]==x then A,Y=a[3],a[4]break end end if A and Y then local x=I I+=Y-A P=D(m.s,A,Y)==D(m.s,x,I)end else local A=m[I]if o==36 or o==90 or o==122 then P=I==m.n+1 or o==36 and(x.multiline and F(m,I+1,a))or o==90 and(I==m.n and F(m,I,a))elseif o==94 or o==65 or o==71 then P=I==1 or o==94 and(x.multiline and F(m,I-1,a))or o==71 and I==Y elseif o==66 or o==98 then local Y=I==1 or x.multiline and F(m,I-1,a)local s=I==m.n+1 or x.multiline and F(m,I,a)local t=E(W[2],m[I-1],x)and 0 or E(W[2],A,x)and 1 if t==0 then P=s or not E(W[2],A,x)elseif t then P=Y or not E(W[2],m[I-1],x)end if o==66 then P=not P end else P=E(W,m,I,x,a)I+=1 end end if not P then while true do local Y,x=Z[1]and Z[1][1],Z[1]if not Y or Y=="\080\082\085\078\069"or Y=="\083\075\073\080"then if Y then table.clear(Z)end if Q>m.n then if s then return false end return nil end Q=Y=="\083\075\073\080"and x[2]or Q+1 t,I=0,Q break elseif Y=="\097\108\116\101\114\110\097\116\105\111\110"then t,I=x[2],x[3]local m,Y=G(A,t+1)if m then x[2]=m else table.remove(Z,1)end if Y then I-=Y end break elseif Y=="\103\114\111\117\112"then if x[7]=="\113\117\097\110\116\105\102\105\101\114"then if x[12]=="\103\114\101\101\100\121"and x[10]>=x[8]or x[12]=="\108\097\122\121"and(x[10]<x[9]and not x[13])then t,I=x[12]=="\103\114\101\101\100\121"and x[6]or x[2],x[3]if x[12]=="\103\114\101\101\100\121"then table.remove(Z,1)break elseif x[10]>=x[8]then x[13]=true break end end elseif x[7]==33 then table.remove(Z,1)t,I=x[6],x[3]break end elseif Y=="\113\117\097\110\116\105\102\105\101\114"then if math.sign(x[4]-x[3])==math.sign(x[5])then x[3]+=x[5]t,I=x[2],x[3]break end end table.remove(Z,1)end end t+=1 end end if s then return true end local W=false local o=table.create(A.group_n)o[0],o.n={Q;I},A.group_n for A,m in ipairs(Z)do if m[1]=="\109\097\116\099\104\083\116\097\114\116"and not W then o[0][1],W=m[2],true elseif m[1]=="\103\114\111\117\112"and(m[5]and not o[m[5]])then o[m[5]]={m[3];m[4]}end end return o end t.test=M("\082\101\103\069\120","\116\101\115\116",function(A,m,Y)return d(A.token,Z(m,Y),1,A.flags,A.verb_flags,true)end)t.match=M("\082\101\103\069\120","\109\097\116\099\104",function(A,m,Y,x)local a=d(A.token,Z(m,Y),1,A.flags,A.verb_flags,false)if not a then return nil end return H(a,A.group_id,x,m)end)t.matchall=M("\082\101\103\069\120","\109\097\116\099\104\097\108\108",function(A,m,Y,x)m=Z(m,Y)local a=1 return function()local Y=a<=m.n+1 and d(A.token,m,a,A.flags,A.verb_flags,false)if not Y then return nil end a=Y[0][2]+(Y[0][1]>=Y[0][2]and 1 or 0)return H(Y,A.group_id,x,m.s)end end)local function g(A,m,Y,x)for x,a in ipairs(x)do if type(a)=="\116\097\098\108\101"then if a[1]=="\099\111\110\100\105\116\105\111\110"then if Y[a[2]]then if a[3]then g(A,m,Y,a[3])else table.move(m,Y[a[2]][1],Y[a[2]][2]-1,#A+1,A)end elseif a[4]then g(A,m,Y,a[4])end else table.move(a,1,#a,#A+1,A)end elseif Y[a]then table.move(m,Y[a][1],Y[a][2]-1,#A+1,A)end end A.n=#A return A end t.sub=M("\082\101\103\069\120","\115\117\098",function(A,m,Y,x,a,s)if a~=nil and(type(a)~="\110\117\109\098\101\114"and type(a)~="\115\116\114\105\110\103")then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\053 \116\111 \'\115\117\098\' \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(a)),3)end local t={l=false;o=false,u=false}for A in string.gmatch(a or"",utf8.charpattern)do if t[A]~=false then error("\105\110\118\097\108\105\100 \114\101\103\117\108\097\114 \101\120\112\114\101\115\115\105\111\110 \115\117\098\115\116\105\116\117\116\105\111\110 \102\108\097\103 "..A,3)end t[A]=true end local I=type(m)if I=="\110\117\109\098\101\114"then m..=""elseif I~="\115\116\114\105\110\103"and(I~="\102\117\110\099\116\105\111\110"and(not t.o or I~="\116\097\098\108\101"))then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\050 \116\111 \'\115\117\098\' \040\115\116\114\105\110\103\047\102\117\110\099\116\105\111\110\037\115 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",t.o and"\047\116\097\098\108\101"or"",typeof(m)),3)end if tonumber(x)then x=tonumber(x)if x<=-1 or x~=x then x=math.huge end elseif x~=nil then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\052 \116\111 \'\115\117\098\' \040\110\117\109\098\101\114 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(x)),3)else x=math.huge end if x<1 then return Y,0 end local Q=0 if I=="\115\116\114\105\110\103"then m=Z(m)if not t.l then local Y=0 local x=table.create(3)local a=A.token.group_n local s={}while Y<m.n do local I=Y repeat I+=1 until not m[I]or m[I]==36 or m[I]==92 or(m[I]==58 or m[I]==125)and s[1]Q+=(I-Y)-1 if I-Y>1 then table.insert(x,table.move(m,Y+1,I-1,1,table.create((I-Y)-1)))end if m[I]==58 then local A=s[1]if A[2]then error("\109\097\108\102\111\114\109\101\100 \115\117\098\115\116\105\116\117\116\105\111\110 \112\097\116\116\101\114\110",3)end A[2]=table.move(x,A[3],#x,1,table.create((#x+1)-A[3]))for A=#x,A[3],-1 do x[A]=nil end elseif m[I]==125 then local A=table.remove(s,1)local m=table.move(x,A[3],#x,1,table.create((#x+1)-A[3]))for A=#x,A[3],-1 do x[A]=nil end table.insert(x,{"\099\111\110\100\105\116\105\111\110";A[1],A[2]~=true and(A[2]or m);A[2]and m})elseif m[I]then I+=1 local Y=m[I]if not Y then if m[I-1]==92 then error("\114\101\112\108\097\099\101\109\101\110\116 \115\116\114\105\110\103 \109\117\115\116 \110\111\116 \101\110\100 \119\105\116\104 \097 \116\114\097\105\108\105\110\103 \098\097\099\107\115\108\097\115\104",3)end local A=x[#x]if type(A)=="\116\097\098\108\101"then table.insert(A,m[I-1])else table.insert(x,{m[I-1]})end elseif Y==92 and m[I-1]==36 then local A=x[#x]if type(A)=="\116\097\098\108\101"then table.insert(A,36)else table.insert(x,{36})end I-=1 Q+=1 elseif Y==48 then table.insert(x,0)elseif Y>48 and Y<=57 then local A=I local s=Y-48 while m[I+1]and(m[I+1]>=48 and m[I+1]<=57)do s..=m[I+1]-48 I+=1 end s=tonumber(s)if not t.u and s>a then error("\114\101\102\101\114\101\110\099\101 \116\111 \110\111\110-\101\120\105\115\116\101\110\116 \115\117\098\112\097\116\116\101\114\110",3)end table.insert(x,s)elseif Y==123 and m[I-1]==36 then I+=1 local Y=I while m[I]and(m[I]>=48 and m[I]<=57 or m[I]>=65 and m[I]<=90 or m[I]>=97 and m[I]<=122 or m[I]==95)do I+=1 end if(m[I]==125 or m[I]==58 and(m[I+1]==43 or m[I+1]==45))and I~=Y then local Q=D(m.s,Y,I)if m[Y]>=48 and m[Y]<=57 then Q=tonumber(Q)if not t.u and Q>a then error("\114\101\102\101\114\101\110\099\101 \116\111 \110\111\110-\101\120\105\115\116\101\110\116 \115\117\098\112\097\116\116\101\114\110",3)end else Q=A.group_id[Q]if not t.u and(not Q or Q>a)then error("\114\101\102\101\114\101\110\099\101 \116\111 \110\111\110-\101\120\105\115\116\101\110\116 \115\117\098\112\097\116\116\101\114\110",3)end end if m[I]==58 then I+=1 table.insert(s,{Q;m[I]==45,#x+1})else table.insert(x,Q)end else error("\109\097\108\102\111\114\109\101\100 \115\117\098\115\116\105\116\117\116\105\111\110 \112\097\116\116\101\114\110",3)end else local A if m[I-1]==36 then if Y~=36 then local A=x[#x]if type(A)=="\116\097\098\108\101"then table.insert(A,36)else table.insert(x,{36})end end else A=q[m[I]]if type(A)~="\110\117\109\098\101\114"then A=nil end end local a=x[#x]if type(a)=="\116\097\098\108\101"then table.insert(a,A or m[I])else table.insert(x,{A or m[I]})end Q+=1 end end Y=I end if s[1]then error("\109\097\108\102\111\114\109\101\100 \115\117\098\115\116\105\116\117\116\105\111\110 \112\097\116\116\101\114\110",3)end if not x[2]and(type(x[1])=="\116\097\098\108\101"and x[1][1]~="\099\111\110\100\105\116\105\111\110")then m,m.n=x[1],#x[1]else m,I=x,"\115\117\098\115\116\095\115\116\114\105\110\103"end end end Y=Z(Y)local o,P,i=0,1,0 while P<=(Y.n+o)+1 do local a=d(A.token,Y,P,A.flags,A.verb_flags,false)if not a then break end local W if I=="\115\116\114\105\110\103"then W=m elseif I=="\115\117\098\115\116\095\115\116\114\105\110\103"then W=g(table.create(Q),Y,a,m)else local x local Q if I=="\116\097\098\108\101"then x=D(Y.s,a[0][1],a[0][2])Q=m[x]else x=H(a,A.group_id,s,Y.s)Q=m(x)end if Q==x or t.o and not Q then local A=a[0][2]-a[0][1]W=table.move(Y,a[0][1],a[0][2]-1,1,table.create(A))W.n=A elseif type(Q)=="\115\116\114\105\110\103"then W=Z(Q)elseif type(Q)=="\110\117\109\098\101\114"then W=Z(Q.."")elseif t.o then error(string.format("\105\110\118\097\108\105\100 \114\101\112\108\097\099\101\109\101\110\116 \118\097\108\117\101 \040\097 \037\115\041",type(Q)),3)else W={n=0}end end local q=a[0][2]-a[0][1]local c=math.min(W.n,q)for A=0,c-1,1 do Y[a[0][1]+A]=W[A+1]end local j=a[0][1]+c P=a[0][2]if q>W.n then for A=1,q-W.n,1 do table.remove(Y,j)o-=1 P-=1 end elseif W.n>q then for A=1,W.n-q,1 do table.insert(Y,(j+A)-1,W[c+A])o+=1 P+=1 end end if q<=0 then P+=1 end i+=1 if x<i+1 then break end end return W(Y),i end)t.split=M("\082\101\103\069\120","\115\112\108\105\116",function(A,m,Y)if tonumber(Y)then Y=tonumber(Y)if Y<=-1 or Y~=Y then Y=math.huge end elseif Y~=nil then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\051 \116\111 \'\115\112\108\105\116\' \040\110\117\109\098\101\114 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(Y)),3)else Y=math.huge end m=Z(m)local x,a=1,0 local s={}local t=0 while x<=m.n+1 do a+=1 local I=Y>=a and d(A.token,m,x,A.flags,A.verb_flags,false)if not I then break end table.insert(s,D(m.s,x-t,I[0][1]))t=I[0][1]>=I[0][2]and 1 or 0 x=I[0][2]+t end table.insert(s,string.sub(m.s,utf8.offset(m.s,x-t)))return s end)local function K(A,m)return t[m]or a[A].flags[m]end local function V(A)return a[A].pattern_repr..a[A].flag_repr end local w={[58]=true,[33]=true;[61]=true;[62]=true,[124]=true}local function X(m,x)if x.unicode and not A.unicodeData then return"\111\112\116\105\111\110\115\046\117\110\105\099\111\100\101\068\097\116\097 \099\097\110\110\111\116 \098\101 \116\117\114\110\101\100 \111\102\102 \119\104\105\108\101 \104\097\118\105\110\103 \117\110\105\099\111\100\101 \102\108\097\103"end local a,s=1,m.n local t=0 local I,Q,Z={},{},{newline=1;newline_seq=1;not_empty=0}while a<=s do local s=m[a]if s==40 then local A if m[a+1]==42 then a+=2 local Y=a while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=90 or m[a]>=97 and m[a]<=122 or m[a]==95 or m[a]==58)do a+=1 end if m[a]~=41 and m[a-1]~=58 then return"\113\117\097\110\116\105\102\105\101\114 \100\111\101\115\110\'\116 \102\111\108\108\111\119 \097 \114\101\112\101\097\116\097\098\108\101 \112\097\116\116\101\114\110"end local x=D(m.s,Y,a)if x=="\112\111\115\105\116\105\118\101\095\108\111\111\107\097\104\101\097\100\058"or x=="\110\101\103\097\116\105\118\101\095\108\111\111\107\104\101\097\100\058"or x=="\112\111\115\105\116\105\118\101\095\108\111\111\107\098\101\104\105\110\100\058"or x=="\110\101\103\097\116\105\118\101\095\108\111\111\107\098\101\104\105\110\100\058"or x:find("\094\091\112\110\093\108\091\097\098\093\058\036")then A={40,nil;nil;x:find("\094\110")and 33 or 61;x:find("\098",3,true)and 1}elseif x=="\097\116\111\109\105\099\058"then A={40;nil,nil;62;nil}elseif x=="\065\067\067\069\080\084"or x=="\070\065\073\076"or x=="\070"or x=="\080\082\085\078\069"or x=="\083\075\073\080"then A=x=="\070"and"\070\065\073\076"or x else if R[x]then Z.newline=x elseif x=="\066\083\082\095\065\078\089\067\082\076\070"or x=="\066\083\082\095\085\078\073\067\079\068\069"then Z.newline_seq=x=="\066\083\082\095\085\078\073\067\079\068\069"and 1 or 0 elseif x=="\078\079\084\069\077\080\084\089"or x=="\078\079\084\069\077\080\084\089\095\065\084\083\084\065\082\084"then Z.not_empty=x=="\078\079\084\069\077\080\084\089"and 1 or 2 else return"\117\110\107\110\111\119\110 \111\114 \109\097\108\102\111\114\109\101\100 \118\101\114\098"end if I[1]then return"\116\104\105\115 \118\101\114\098 \109\117\115\116 \098\101 \112\108\097\099\101\100 \097\116 \116\104\101 \098\101\103\105\110\110\105\110\103 \111\102 \116\104\101 \114\101\103\101\120"end end elseif m[a+1]==63 then a+=2 if m[a]==35 then a=table.find(m,41,a)if not a then return"\117\110\116\101\114\109\105\110\097\116\101\100 \112\097\114\101\110\116\104\101\116\105\099\097\108"end a+=1 continue elseif not m[a]then return"\117\110\116\101\114\109\105\110\097\116\101\100 \112\097\114\101\110\116\104\101\116\105\099\097\108"end A={40;nil,nil;m[a];nil}if m[a]==48 and m[a+1]==41 then A[1],A[2],A[3],A[5]="\114\101\099\117\114\109\097\116\099\104",0,0,nil elseif m[a]>48 and m[a]<=57 then local Y=a a+=1 while m[a]>=48 and m[a]<=48 do a+=1 end if m[a]~=41 then return"\105\110\118\097\108\105\100 \103\114\111\117\112 \115\116\114\117\099\116\117\114\101"end A[1],A[2],A[4]="\114\101\099\117\114\109\097\116\099\104",tonumber(D(m.s,Y,a)),nil elseif m[a]==60 and m[a+1]==33 or m[a+1]==61 then a+=1 A[4],A[5]=m[a],1 elseif m[a]==124 then A[5]=t elseif m[a]==80 or m[a]==60 or m[a]==39 then if m[a]==80 then a+=1 end if m[a]==61 then local Y=a+1 while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=90 or m[a]>=97 and m[a]<=122 or m[a]==95)do a+=1 end if not m[a]then return"\117\110\116\101\114\109\105\110\097\116\101\100 \112\097\114\101\110\116\104\101\116\105\099\097\108"elseif m[a]~=41 or a==Y then return"\105\110\118\097\108\105\100 \103\114\111\117\112 \115\116\114\117\099\116\117\114\101"end A={"\098\097\099\107\114\101\102",D(m.s,Y,a)}elseif m[a]==60 or m[a-1]~=80 and m[a]==39 then local Y=m[a]==39 and 39 or 62 local x=a+1 a+=1 if m[a]==41 then return"\109\105\115\115\105\110\103 \099\104\097\114\097\099\116\101\114 \105\110 \115\117\098\112\097\116\116\101\114\110"elseif m[a]>=48 and m[a]<=57 then return"\115\117\098\112\097\116\116\101\114\110 \110\097\109\101 \109\117\115\116 \110\111\116 \098\101\103\105\110 \119\105\116\104 \097 \100\105\103\105\116"elseif not(m[a]>=65 and m[a]<=90 or m[a]>=97 and m[a]<=122 or m[a]==95)then return"\105\110\118\097\108\105\100 \099\104\097\114\097\099\116\101\114 \105\110 \115\117\098\112\097\116\116\101\114\110"end a+=1 while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=90 or m[a]>=97 and m[a]<=122 or m[a]==95)do a+=1 end if not m[a]then return"\117\110\116\101\114\109\105\110\097\116\101\100 \112\097\114\101\110\116\104\101\116\105\099\097\108"elseif m[a]~=Y then return"\105\110\118\097\108\105\100 \099\104\097\114\097\099\116\101\114 \105\110 \115\117\098\112\097\116\116\101\114\110"end local s=D(m.s,x,a)t+=1 if(Q[s]or t)~=t then return"\115\117\098\112\097\116\116\101\114\110 \110\097\109\101 \097\108\114\101\097\100\121 \101\120\105\115\116\115"end for A,m in pairs(Q)do if s~=A and t==m then return"\100\105\102\102\101\114\101\110\116 \110\097\109\101\115 \102\111\114 \115\117\098\112\097\116\116\101\114\110\115 \111\102 \116\104\101 \115\097\109\101 \110\117\109\098\101\114 \097\114\101\110\'\116 \112\101\114\109\105\116\116\101\100"end end Q[s]=t A[2],A[4]=t,nil else return"\105\110\118\097\108\105\100 \103\114\111\117\112 \115\116\114\117\099\116\117\114\101"end elseif not w[m[a]]then return"\105\110\118\097\108\105\100 \103\114\111\117\112 \115\116\114\117\099\116\117\114\101"end else t+=1 A={40;t,nil;nil}end if A then table.insert(I,A)end elseif s==41 then local A=#I+1 local m=-1 local Y=0 local x,a=0,0 repeat A-=1 local s,Q=I[A],type(I[A])=="\116\097\098\108\101"if Q and s[1]==40 then a+=1 if Y and s.count then Y+=s.count end if not s[3]then if s[4]==124 then t=s[5]+math.max(x,a)end if Y~=m and m~=-1 then m=nil else m=Y end break end elseif s==p then if Y~=m and m~=-1 then m,Y=nil,nil else m,Y=Y,0 end x,a=math.max(x,a),0 elseif Y then if Q and s[1]=="\113\117\097\110\116\105\102\105\101\114"then if s[2]==s[3]then Y+=s[2]else Y=nil end else Y+=1 end end until A<1 if A<1 then return"\117\110\109\097\116\099\104\101\100 \041 \105\110 \114\101\103\117\108\097\114 \101\120\112\114\101\115\115\105\111\110"end local s=I[A]local Q=#I+1 local Z={41;s[2];A,s[4],s[5];count=m}if(s[4]==33 or s[4]==61)and(s[5]and not m)then return"\108\111\111\107\098\101\104\105\110\100 \097\115\115\101\114\116\105\111\110 \105\115 \110\111\116 \102\105\120\101\100 \119\105\100\116\104"end s[3]=Q table.insert(I,Z)elseif s==46 then table.insert(I,B)elseif s==91 then local s,t=false,nil a+=1 local Q=a if m[a]==94 then s=true a+=1 elseif m[a]==46 or m[a]==58 or m[a]==61 then t=m[a]end local Z if m[a]==91 or m[a]==92 then Z={}else Z={m[a]}a+=1 end while m[a]~=93 do if not m[a]then return"\117\110\116\101\114\109\105\110\097\116\101\100 \099\104\097\114\097\099\116\101\114 \099\108\097\115\115"elseif m[a]==45 and(Z[1]and type(Z[1])=="\110\117\109\098\101\114")then if m[a+1]==93 then table.insert(Z,1,45)else a+=1 local A=m[a]if A==91 then if m[a+1]==46 or m[a+1]==58 or m[a+1]==61 then local A=a+2 repeat A=table.find(m,93,A)until not A or m[A-1]~=92 if not A then return"\117\110\116\101\114\109\105\110\097\116\101\100 \099\104\097\114\097\099\116\101\114 \099\108\097\115\115"elseif m[A-1]==m[a+1]and A-1~=a+1 then return"\105\110\118\097\108\105\100 \114\097\110\103\101 \105\110 \099\104\097\114\097\099\116\101\114 \099\108\097\115\115"end end if Z[1]>91 then return"\105\110\118\097\108\105\100 \114\097\110\103\101 \105\110 \099\104\097\114\097\099\116\101\114 \099\108\097\115\115"end elseif A==92 then a+=1 if m[a]==120 then local Y,x a+=1 if m[a]and(m[a]>=48 and m[a]<=57)or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102 then Y=m[a]-((m[a]>=65 and m[a]<=90)and 55 or(m[a]>=97 and m[a]<=122)and 87 or 48)a+=1 if m[a]and(m[a]>=48 and m[a]<=57)or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102 then x=m[a]-((m[a]>=65 and m[a]<=90)and 55 or(m[a]>=97 and m[a]<=122)and 87 or 48)else a-=1 end else a-=1 end A=Y and(x and 16*Y+x or Y)or 0 elseif m[a]>=48 and m[a]<=55 then local Y,x,s=m[a]-48,nil,nil a+=1 if m[a]and(m[a]>=48 and m[a]<=55)then x=m[a]-48 a+=1 if m[a]and(m[a]>=48 and m[a]<=55)then s=m[a]-48 else a-=1 end else a-=1 end A=x and(s and(64*Y+8*x)+s or 8*Y+x)or Y else A=q[m[a]]or m[a]if type(A)~="\110\117\109\098\101\114"then return"\105\110\118\097\108\105\100 \114\097\110\103\101 \105\110 \099\104\097\114\097\099\116\101\114 \099\108\097\115\115"end end elseif Z[1]>A then return"\105\110\118\097\108\105\100 \114\097\110\103\101 \105\110 \099\104\097\114\097\099\116\101\114 \099\108\097\115\115"end Z[1]={"\114\097\110\103\101";Z[1];A}end elseif m[a]==91 then if m[a+1]==46 or m[a+1]==58 or m[a+1]==61 then local A=a+2 repeat A=table.find(m,93,A)until not A or m[A-1]~=92 if not A then return"\117\110\116\101\114\109\105\110\097\116\101\100 \099\104\097\114\097\099\116\101\114 \099\108\097\115\115"elseif m[A-1]~=m[a+1]or A-1==a+1 then table.insert(Z,1,91)elseif m[A-1]==46 or m[A-1]==61 then return"\080\079\083\073\088 \099\111\108\108\097\116\105\110\103 \101\108\101\109\101\110\116\115 \097\114\101\110\'\116 \115\117\112\112\111\114\116\101\100"elseif m[A-1]==58 then local Y=m[a+3]==94 local x=D(m.s,a+(Y and 3 or 2),A-1)if not P[x]then return"\117\110\107\110\111\119\110 \080\079\083\073\088 \099\108\097\115\115 \110\097\109\101"end table.insert(Z,1,{"\099\108\097\115\115";x,Y})a=A end else table.insert(Z,1,91)end elseif m[a]==92 then a+=1 if m[a]==120 then local A,Y a+=1 if m[a]==123 then a+=1 local A=a while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102)do a+=1 end if m[a]~=125 or a==A then return"\109\097\108\102\111\114\109\101\100 \104\101\120\097\100\101\099\105\109\097\108 \099\104\097\114\097\099\116\101\114"elseif a-A>4 then return"\099\104\097\114\097\099\116\101\114 \111\102\102\115\101\116 \116\111\111 \108\097\114\103\101"end table.insert(Z,1,tonumber(D(m.s,A,a),16))else if m[a]and(m[a]>=48 and m[a]<=57)or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102 then A=m[a]-((m[a]>=65 and m[a]<=90)and 55 or(m[a]>=97 and m[a]<=122)and 87 or 48)a+=1 if m[a]and(m[a]>=48 and m[a]<=57)or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102 then Y=m[a]-((m[a]>=65 and m[a]<=90)and 55 or(m[a]>=97 and m[a]<=122)and 87 or 48)else a-=1 end else a-=1 end table.insert(Z,1,A and(Y and 16*A+Y or A)or 0)end elseif m[a]>=48 and m[a]<=55 then local A,Y,x=m[a]-48,nil,nil a+=1 if m[a]and(m[a]>=48 and m[a]<=55)then Y=m[a]-48 a+=1 if m[a]and(m[a]>=48 and m[a]<=55)then x=m[a]-48 else a-=1 end else a-=1 end table.insert(Z,1,Y and(x and(64*A+8*Y)+x or 8*A+Y)or A)elseif m[a]==69 then elseif m[a]==81 then local A=a+1 repeat a=table.find(m,92,a+1)until not a or m[a+1]==69 table.move(m,A,a and a-1 or#m,#I+1,I)if not a then break end a+=1 elseif m[a]==78 then if m[a+1]==123 and(m[a+2]==85 and(m[a+3]==43 and x.unicode))then a+=4 local A=a while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102)do a+=1 end if m[a]~=125 or a==A then return"\109\097\108\102\111\114\109\101\100 \085\110\105\099\111\100\101 \099\111\100\101 \112\111\105\110\116"end local Y=tonumber(D(m.s,A,a))table.insert(Z,1,Y)else return"\105\110\118\097\108\105\100 \101\115\099\097\112\101 \115\101\113\117\101\110\099\101"end elseif m[a]==80 or m[a]==112 then if not A.unicodeData then return"\111\112\116\105\111\110\115\046\117\110\105\099\111\100\101\068\097\116\097 \099\097\110\110\111\116 \098\101 \116\117\114\110\101\100 \111\102\102 \119\104\101\110 \117\115\105\110\103 \092\112"end a+=1 if m[a]~=123 then local A=utf8.char(m[a]or 0)if not c[A]then return"\117\110\107\110\111\119\110 \111\114 \109\097\108\102\111\114\109\101\100 \115\099\114\105\112\116 \110\097\109\101"end table.insert(Z,1,{"\099\097\116\101\103\111\114\121",false,A})else local A=m[a]==80 a+=1 if m[a]==94 then a+=1 A=not A end local x=a while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=90 or m[a]>=97 and m[a]<=122 or m[a]==95)do a+=1 end if m[a]~=125 then return"\117\110\107\110\111\119\110 \111\114 \109\097\108\102\111\114\109\101\100 \115\099\114\105\112\116 \110\097\109\101"end local s=D(m.s,x,a)local t=Y[s]if t then table.insert(Z,1,{"\099\104\097\114\115\101\116",A;t})elseif not c[s]then return"\117\110\107\110\111\119\110 \111\114 \109\097\108\102\111\114\109\101\100 \115\099\114\105\112\116 \110\097\109\101"else table.insert(Z,1,{"\099\097\116\101\103\111\114\121";A;s})end end elseif m[a]==111 then a+=1 if m[a]~=123 then return"\109\097\108\102\111\114\109\101\100 \111\099\116\097\108 \099\111\100\101"end a+=1 local A=a while m[a]and(m[a]>=48 and m[a]<=55)do a+=1 end if m[a]~=125 or a==A then return"\109\097\108\102\111\114\109\101\100 \111\099\116\097\108 \099\111\100\101"end local Y=tonumber(D(m.s,A,a),8)if Y>65535 then return"\099\104\097\114\097\099\116\101\114 \111\102\102\115\101\116 \116\111\111 \108\097\114\103\101"end table.insert(Z,1,Y)else local A=q[m[a]]table.insert(Z,1,type(A)=="\115\116\114\105\110\103"and{"\099\108\097\115\115",A,false}or A or m[a])end elseif x.ignoreCase and(m[a]>=97 and m[a]<=122)then table.insert(Z,1,m[a]-32)else table.insert(Z,1,m[a])end a+=1 end if m[a-1]==t and a-1~=Q then return t==58 and"\080\079\083\073\088 \110\097\109\101\100 \099\108\097\115\115\101\115 \097\114\101 \111\110\108\121 \115\117\112\112\111\114\116 \119\105\116\104\105\110 \097 \099\104\097\114\097\099\116\101\114 \115\101\116"or"\080\079\083\073\088 \099\111\108\108\097\116\105\110\103 \101\108\101\109\101\110\116\115 \097\114\101\110\'\116 \115\117\112\112\111\114\116\101\100"end if not Z[2]and not s then table.insert(I,Z[1])else table.insert(I,{"\099\104\097\114\115\101\116",s,Z})end elseif s==92 then a+=1 local s=m[a]if not s then return"\112\097\116\116\101\114\110 \109\097\121 \110\111\116 \101\110\100 \119\105\116\104 \097 \116\114\097\105\108\105\110\103 \098\097\099\107\115\108\097\115\104"elseif s>=48 and s<=57 then local A=a while m[a+1]and(m[a+1]>=48 and m[a+1]<=57)do a+=1 end local Y=tonumber(D(m.s,A,a+1))if Y>t and a~=A then a=A local Y,x,s if m[a]<=55 then Y=m[a]-48 a+=1 if m[a]and(m[a]>=48 and m[a]<=55)then x=m[a]-48 a+=1 if m[a]and(m[a]>=48 and m[a]<=55)then s=m[a]-48 else a-=1 end else a-=1 end end table.insert(I,Y and(x and(s and(64*Y+8*x)+s or 8*Y+x)or Y)or m[A])else table.insert(I,{"\098\097\099\107\114\101\102";Y})end elseif s==69 then elseif s==81 then local A=a+1 repeat a=table.find(m,92,a+1)until not a or m[a+1]==69 table.move(m,A,a and a-1 or#m,#I+1,I)if not a then break end a+=1 elseif s==78 then if m[a+1]==123 and(m[a+2]==85 and(m[a+3]==43 and x.unicode))then a+=4 local A=a while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102)do a+=1 end if m[a]~=125 or a==A then return"\109\097\108\102\111\114\109\101\100 \085\110\105\099\111\100\101 \099\111\100\101 \112\111\105\110\116"end local Y=tonumber(D(m.s,A,a))table.insert(I,Y)else table.insert(I,q[78])end elseif s==80 or s==112 then if not A.unicodeData then return"\111\112\116\105\111\110\115\046\117\110\105\099\111\100\101\068\097\116\097 \099\097\110\110\111\116 \098\101 \116\117\114\110\101\100 \111\102\102 \119\104\101\110 \117\115\105\110\103 \092\112"end a+=1 if m[a]~=123 then local A=utf8.char(m[a]or 0)if not c[A]then return"\117\110\107\110\111\119\110 \111\114 \109\097\108\102\111\114\109\101\100 \115\099\114\105\112\116 \110\097\109\101"end table.insert(I,{"\099\097\116\101\103\111\114\121",false;A})else local A=s==80 a+=1 if m[a]==94 then a+=1 A=not A end local x=a while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=90 or m[a]>=97 and m[a]<=122 or m[a]==95)do a+=1 end if m[a]~=125 then return"\117\110\107\110\111\119\110 \111\114 \109\097\108\102\111\114\109\101\100 \115\099\114\105\112\116 \110\097\109\101"end local t=D(m.s,x,a)local Q=Y[t]if Q then table.insert(I,{"\099\104\097\114\115\101\116";A;Q})elseif not c[t]then return"\117\110\107\110\111\119\110 \111\114 \109\097\108\102\111\114\109\101\100 \115\099\114\105\112\116 \110\097\109\101"else table.insert(I,{"\099\097\116\101\103\111\114\121";A,t})end end elseif s==103 and(m[a+1]==123 or m[a+1]>=48 and m[a+1]<=57)then local A=false a+=1 if m[a]==123 then a+=1 A=true elseif m[a]<48 or m[a]>57 then return"\109\097\108\102\111\114\109\101\100 \114\101\102\101\114\101\110\099\101 \099\111\100\101"end local Y=a while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102)do a+=1 end if A and m[a]~=125 then return"\109\097\108\102\111\114\109\101\100 \114\101\102\101\114\101\110\099\101 \099\111\100\101"end local x=tonumber(D(m.s,Y,a+(A and 0 or 1)))table.insert(I,{"\098\097\099\107\114\101\102",x})if not A then a-=1 end elseif s==111 then a+=1 if m[a+1]~=123 then return"\109\097\108\102\111\114\109\101\100 \111\099\116\097\108 \099\111\100\101"end a+=1 local A=a while m[a]and(m[a]>=48 and m[a]<=55)do a+=1 end if m[a]~=125 or a==A then return"\109\097\108\102\111\114\109\101\100 \111\099\116\097\108 \099\111\100\101"end local Y=tonumber(D(m.s,A,a),8)if Y>65535 then return"\099\104\097\114\097\099\116\101\114 \111\102\102\115\101\116 \116\111\111 \108\097\114\103\101"end table.insert(I,Y)elseif s==120 then local A,Y a+=1 if m[a]==123 then a+=1 local A=a while m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102)do a+=1 end if m[a]~=125 or a==A then return"\109\097\108\102\111\114\109\101\100 \104\101\120\097\100\101\099\105\109\097\108 \099\111\100\101"elseif a-A>4 then return"\099\104\097\114\097\099\116\101\114 \111\102\102\115\101\116 \116\111\111 \108\097\114\103\101"end table.insert(I,tonumber(D(m.s,A,a),16))else if m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102)then A=m[a]-((m[a]>=65 and m[a]<=90)and 55 or(m[a]>=97 and m[a]<=122)and 87 or 48)a+=1 if m[a]and(m[a]>=48 and m[a]<=57 or m[a]>=65 and m[a]<=70 or m[a]>=97 and m[a]<=102)then Y=m[a]-((m[a]>=65 and m[a]<=90)and 55 or(m[a]>=97 and m[a]<=122)and 87 or 48)else a-=1 end else a-=1 end table.insert(I,A and(Y and 16*A+Y or A)or 0)end else local A=i[s]or q[s]table.insert(I,A or s)end elseif s==42 or s==43 or s==63 or s==123 then local A,Y if s==123 then local x=a+1 local s while m[a+1]and(m[a+1]>=48 and m[a+1]<=57 or m[a+1]==44 and(not s and a+1~=x))do a+=1 if m[a]==44 then s=a end end if m[a+1]==125 then a+=1 if not s then A=tonumber(D(m.s,x,a))Y=A else A,Y=tonumber(D(m.s,x,s)),s+1==a and math.huge or tonumber(D(m.s,s+1,a))if Y<A then return"\110\117\109\098\101\114\115 \111\117\116 \111\102 \111\114\100\101\114 \105\110 \123\125 \113\117\097\110\116\105\102\105\101\114"end end else table.move(m,x-1,a,#I+1,I)end else A,Y=s==43 and 1 or 0,s==63 and 1 or math.huge end if A then local s=x.ungreedy and"\108\097\122\121"or"\103\114\101\101\100\121"if m[a+1]==43 or m[a+1]==63 then a+=1 s=m[a]==43 and"\112\111\115\115\101\115\115\105\118\101"or x.ungreedy and"\103\114\101\101\100\121"or"\108\097\122\121"end local t=#I local Q=I[t]if not Q or type(Q)=="\116\097\098\108\101"and(Q[1]=="\113\117\097\110\116\105\102\105\101\114"or Q[1]==40 or i[Q[1]])or Q==p or type(Q)=="\115\116\114\105\110\103"then return"\113\117\097\110\116\105\102\105\101\114 \100\111\101\115\110\'\116 \102\111\108\108\111\119 \097 \114\101\112\101\097\116\097\098\108\101 \112\097\116\116\101\114\110"end if Y==0 then table.remove(I)elseif A~=1 or Y~=1 then if type(Q)=="\116\097\098\108\101"and Q[1]==41 then t=Q[3]end I[t]={"\113\117\097\110\116\105\102\105\101\114",A,Y,s;I[t]}end end elseif s==124 then table.insert(I,p)local A=#I repeat A-=1 local m,Y=I[A],type(I[A])=="\116\097\098\108\101"if Y and m[1]==41 then A=I[A][3]elseif Y and m[1]==40 then if m[4]==124 then t=m[5]end break end until not m elseif s==36 or s==94 then table.insert(I,s==94 and b or r)elseif x.ignoreCase and(s>=97 and s<=122)then table.insert(I,s-32)elseif x.extended and(s>=9 and s<=13 or s==32 or s==35)then if s==35 then repeat a+=1 until not m[a]or m[a]==10 or m[a]==13 end else table.insert(I,s)end a+=1 end local W=0 for A,m in ipairs(I)do if type(m)=="\116\097\098\108\101"and(m[1]==40 or m[1]=="\113\117\097\110\116\105\102\105\101\114"and(type(m[5])=="\116\097\098\108\101"and m[5][1]==40))then if m[1]=="\113\117\097\110\116\105\102\105\101\114"then m=m[5]end if not m[3]then return"\117\110\116\101\114\109\105\110\097\116\101\100 \112\097\114\101\110\116\104\101\116\105\099\097\108"elseif m[2]then W=math.max(W,m[2])end elseif type(m)=="\116\097\098\108\101"and(m[1]=="\098\097\099\107\114\101\102"or m[1]=="\114\101\099\117\114\109\097\116\099\104")then if not Q[m[2]]and(type(m[2])~="\110\117\109\098\101\114"or m[2]>t)then return"\114\101\102\101\114\101\110\099\101 \116\111 \097 \110\111\110-\101\120\105\115\116\101\110\116 \111\114 \105\110\118\097\108\105\100 \115\117\098\112\097\116\116\101\114\110"elseif m[1]=="\114\101\099\117\114\109\097\116\099\104"and m[2]~=0 then for A,Y in ipairs(I)do if type(Y)=="\116\097\098\108\101"and(Y[1]==40 and Y[2]==m[2])then m[3]=A break end end elseif type(m[2])=="\115\116\114\105\110\103"then m[2]=Q[m[2]]end end end I.group_n=W return I,Q,Z end if not tonumber(A.cacheSize)then error(string.format("\101\120\112\101\099\116\101\100 \110\117\109\098\101\114 \102\111\114 \111\112\116\105\111\110\115\046\099\097\099\104\101\083\105\122\101\044 \103\111\116 \037\115",typeof(A.cacheSize)),2)end local z=math.floor(A.cacheSize or 0)~=0 and tonumber(A.cacheSize)local J,f if not z then elseif z<0 or z~=z then error("\099\097\099\104\101 \115\105\122\101 \099\097\110\110\111\116 \098\101 \097 \110\101\103\097\116\105\118\101 \110\117\109\098\101\114 \111\114 \097 \078\097\078",2)elseif z==math.huge then J,f={nil},{nil}elseif z>=4294967296.0 then error("\099\097\099\104\101 \115\105\122\101 \116\111\111 \108\097\114\103\101",2)else J,f=table.create(A.cacheSize),table.create(A.cacheSize)end if z then function s.pruge()table.clear(f)table.clear(J)end end local function n(A,m,Y,x)local s,I,Z local W=z and string.format("\037\115\124\037\115",A.s,Y)local D=z and J[table.find(f,W)]if D then s,I,Z=table.unpack(D,1,3)else s,I,Z=X(A,m)if type(s)=="\115\116\114\105\110\103"then error(s,2)end if z and s[1]then table.insert(f,1,W)table.insert(J,1,{s;I,Z})if z~=math.huge then table.remove(f,z+1)table.remove(J,z+1)end end end local o=newproxy(true)a[o]={name="\082\101\103\069\120",flags=m;flag_repr=Y,pattern_repr=x,token=s;group_id=I;verb_flags=Z}local P=getmetatable(o)P.__index=setmetatable(m,t)P.__tostring=V P.__metatable=Q return o end local function k(A)return(#A%2==0 and"\092"or"")..(A.."\046")end local function u(A,m)return A:lower()<m:lower()end function s.new(...)if select("\035",...)==0 then error("\109\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\049 \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\041",2)end local A,m=...if type(A)=="\110\117\109\098\101\114"then A..=""elseif type(A)~="\115\116\114\105\110\103"then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\049 \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(A)),2)end if type(m)~="\115\116\114\105\110\103"and(type(m)~="\110\117\109\098\101\114"and m~=nil)then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\050 \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(m)),2)end local Y={anchored=false;caseless=false,multiline=false;dotall=false;unicode=false;ungreedy=false,extended=false}local x={}for A in string.gmatch(m or"",utf8.charpattern)do if Y[o[A]]~=false then error("\105\110\118\097\108\105\100 \114\101\103\117\108\097\114 \101\120\112\114\101\115\115\105\111\110 \102\108\097\103 "..A,3)end Y[o[A]]=true table.insert(x,A)end table.sort(x,u)x=table.concat(x)return n(Z(A),Y,x,string.format("\047\037\115\047",A:gsub("\040\092\042\041\047",k)))end function s.fromstring(...)if select("\035",...)==0 then error("\109\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\049 \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\041",2)end local A=...if type(A)=="\110\117\109\098\101\114"then A..=""elseif type(A)~="\115\116\114\105\110\103"then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\049 \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(A),2))end local m=Z(A)local Y=m[1]if not Y then error("\101\109\112\116\121 \114\101\103\101\120",2)elseif Y==92 or Y>=48 and Y<=57 or Y>=65 and Y<=90 or Y>=97 and Y<=122 then error("\100\101\108\105\109\105\116\101\114 \109\117\115\116 \110\111\116 \098\101 \097\108\112\104\097\110\117\109\101\114\105\099 \111\114 \097 \098\097\099\107\115\108\097\115\104",2)end local x=1 repeat x=table.find(m,Y,x+1)if not x then error(string.format("\110\111 \101\110\100\105\110\103 \100\101\108\105\109\105\116\101\114 \040\'\037\115\'\041 \102\111\117\110\100",utf8.char(Y)),2)end local A=1 while m[x-A]==92 do A+=1 end until A%2==1 local a={anchored=false;caseless=false;multiline=false,dotall=false,unicode=false,ungreedy=false,extended=false}local s={}while m.n>x do local A=utf8.char(table.remove(m))m.n-=1 if a[o[A]]~=false then error("\105\110\118\097\108\105\100 \114\101\103\117\108\097\114 \101\120\112\114\101\115\115\105\111\110 \102\108\097\103 "..A,3)end a[o[A]]=true table.insert(s,A)end table.sort(s,u)s=table.concat(s)table.remove(m,1)table.remove(m)m.n-=2 m.s=string.sub(m.s,2,1+m.n)return n(m,a,s,string.sub(A,1,2+m.n))end local O={["\000"]="\092\120\048\048";["\n"]="\092\110";["\t"]="\092\116";["\r"]="\092\114",["\012"]="\092\102"}function s.escape(...)if select("\035",...)==0 then error("\109\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\049 \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\041",2)end local A,m,Y=...if type(A)=="\110\117\109\098\101\114"then A..=""elseif type(A)~="\115\116\114\105\110\103"then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\049 \116\111 \'\101\115\099\097\112\101\' \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(A)),2)end if Y==nil then Y=""elseif type(Y)=="\110\117\109\098\101\114"then Y..=""elseif type(Y)~="\115\116\114\105\110\103"then error(string.format("\105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \035\051 \116\111 \'\101\115\099\097\112\101\' \040\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041",typeof(Y)),2)end if utf8.len(Y)>1 or Y:match("\094\091\037\097\092\093\036")then error("\100\101\108\105\109\105\116\101\114 \104\097\118\101 \110\111\116 \098\101 \097\108\112\104\097\110\117\109\101\114\105\099",2)end return(string.gsub(A,"\091\000\012\n\r\t\093",O)):gsub(string.format("\091\092\037\115\035\040\041\037\037\037\037\042\043\046\063\091\037\037\093\094\123\124\037\115\093",m and"\037\115"or"",(Y:find("\094\091\037\037\037\093\093\036")and"\037"or"")..Y),"\092\037\049")end function s.type(...)if select("\035",...)==0 then error("\109\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\049",2)end return a[...]and a[...].name end for A,m in pairs(t)do s[A]=m end t={__index=t}Q=s.fromstring("\047\084\104\101\092\115\042\109\101\116\097\116\097\098\108\101\092\115\042\105\115\092\115\042\040\063\058\108\111\099\107\101\100\124\105\110\097\099\099\101\115\115\105\098\108\101\041\040\063\035\078\105\099\101 \116\114\121 \058\093\041\047\105");(getmetatable(Q)).__metatable=Q local function e()error("\065\116\116\101\109\112\116 \116\111 \109\111\100\105\102\121 \097 \114\101\097\100\111\110\108\121 \116\097\098\108\101",2)end I={__index=I,__metatable=Q;__newindex=e}s.Match=setmetatable({},I)return setmetatable({},{__index=s,__metatable=Q;__newindex=e})
--!nolint
--!nocheck
--[[
	PCRE2-based RegEx implemention for Luau
	Version 1.0.0a2 (2020)
	Expat Licence
	Copyright © 2020, 2023 - Blockzez (devforum.roblox.com/u/Blockzez and github.com/Blockzez)
	All rights reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
]]
--[[ Settings ]]--
-- You can change them here
local options = {
	-- The maximum cache size for regex so the patterns are cached so it doesn't recompile the pattern
	-- The only accepted value are number values >= 0, strings that can be automatically coered to numbers that are >= 0, false and nil
	-- Do note that empty regex patterns (comment-only patterns included) are never cached regardless
	-- The default is 256
	cacheSize = 256,

	-- A boolean that determines whether this use unicode data
	-- If this value evalulates to false, you can remove _unicodechar_category, _scripts and _xuc safely and it'll now error if:
	-- - You try to compile a RegEx with unicode flag
	-- - You try to use the \p pattern
	-- The default is true
	unicodeData = false,
};

--
local u_categories = options.unicodeData and require(script:WaitForChild("_unicodechar_category"));
local chr_scripts = options.unicodeData and require(script:WaitForChild("_scripts"));
local xuc_chr = options.unicodeData and require(script:WaitForChild("_xuc"));
local proxy = setmetatable({ }, { __mode = 'k' });
local re, re_m, match_m = { }, { }, { };
local lockmsg;

--[[ Functions ]]--
local function to_str_arr(self, init)
	if init then
		self = string.sub(self, utf8.offset(self, init));
	end;
	local len = utf8.len(self);
	if len <= 1999 then
		return { n = len, s = self, utf8.codepoint(self, 1, #self) };
	end;
	local clen = math.ceil(len / 1999);
	local ret = table.create(len);
	local p = 1;
	for i = 1, clen do
		local c = table.pack(utf8.codepoint(self, utf8.offset(self, i * 1999 - 1998), utf8.offset(self, i * 1999 - (i == clen and 1998 - ((len - 1) % 1999 + 1) or - 1)) - 1));
		table.move(c, 1, c.n, p, ret);
		p += c.n;
	end;
	ret.s, ret.n = self, len;
	return ret;
end;

local function from_str_arr(self)
	local len = self.n or #self;
	if len <= 7997 then
		return utf8.char(table.unpack(self));
	end;
	local clen = math.ceil(len / 7997);
	local r = table.create(clen);
	for i = 1, clen do
		r[i] = utf8.char(table.unpack(self, i * 7997 - 7996, i * 7997 - (i == clen and 7997 - ((len - 1) % 7997 + 1) or 0)));
	end;
	return table.concat(r);
end;

local function utf8_sub(self, i, j)
	j = utf8.offset(self, j);
	return string.sub(self, utf8.offset(self, i), j and j - 1);
end;

--
local flag_map = {
	a = 'anchored', i = 'caseless', m = 'multiline', s = 'dotall', u = 'unicode', U = 'ungreedy', x ='extended',
};

local posix_class_names = {
	alnum = true, alpha = true, ascii = true, blank = true, cntrl = true, digit = true, graph = true, lower = true, print = true, punct = true, space = true, upper = true, word = true, xdigit = true,
};

local escape_chars = {
	-- grouped
	-- digit, spaces and words
	[0x44] = { "class", "digit", true }, [0x53] = { "class", "space", true }, [0x57] = { "class", "word", true },
	[0x64] = { "class", "digit", false }, [0x73] = { "class", "space", false }, [0x77] = { "class", "word", false },
	-- horizontal/vertical whitespace and newline
	[0x48] = { "class", "blank", true }, [0x56] = { "class", "vertical_tab", true },
	[0x68] = { "class", "blank", false }, [0x76] = { "class", "vertical_tab", false },
	[0x4E] = { 0x4E }, [0x52] = { 0x52 },

	-- not grouped
	[0x42] = 0x08,
	[0x6E] = 0x0A, [0x72] = 0x0D, [0x74] = 0x09,
};

local b_escape_chars = {
	-- word boundary and not word boundary
	[0x62] = { 0x62, { "class", "word", false } }, [0x42] = { 0x42, { "class", "word", false } },

	-- keep match out
	[0x4B] = { 0x4B },

	-- start & end of string
	[0x47] = { 0x47 }, [0x4A] = { 0x4A }, [0x5A] = { 0x5A }, [0x7A] = { 0x7A },
};

local valid_categories = {
	C = true, Cc = true, Cf = true, Cn = true, Co = true, Cs = true,
	L = true, Ll = true, Lm = true, Lo = true, Lt = true, Lu = true,
	M = true, Mc = true, Me = true, Mn = true,
	N = true, Nd = true, Nl = true, No = true,
	P = true, Pc = true, Pd = true, Pe = true, Pf = true, Pi = true, Po = true, Ps = true,
	S = true, Sc = true, Sk = true, Sm = true, So = true,
	Z = true, Zl = true, Zp = true, Zs = true,

	Xan = true, Xps = true, Xsp = true, Xuc = true, Xwd = true,
};

local class_ascii_punct = {
	[0x21] = true, [0x22] = true, [0x23] = true, [0x24] = true, [0x25] = true, [0x26] = true, [0x27] = true, [0x28] = true, [0x29] = true, [0x2A] = true, [0x2B] = true, [0x2C] = true, [0x2D] = true, [0x2E] = true, [0x2F] = true,
	[0x3A] = true, [0x3B] = true, [0x3C] = true, [0x3D] = true, [0x3E] = true, [0x3F] = true, [0x40] = true, [0x5B] = true, [0x5C] = true, [0x5D] = true, [0x5E] = true, [0x5F] = true, [0x60] = true, [0x7B] = true, [0x7C] = true,
	[0x7D] = true, [0x7E] = true,
};

local end_str = { 0x24 };
local dot = { 0x2E };
local beginning_str = { 0x5E };
local alternation = { 0x7C };

local function check_re(re_type, name, func)
	if re_type == "Match" then
		return function(...)
			local arg_n = select('#', ...);
			if arg_n < 1 then
				error("missing argument #1 (Match expected)", 2);
			end;
			local arg0, arg1 = ...;
			if not (proxy[arg0] and proxy[arg0].name == "Match") then
				error(string.format("invalid argument #1 to %q (Match expected, got %s)", name, typeof(arg0)), 2);
			else
				arg0 = proxy[arg0];
			end;
			if name == "group" or name == "span" then
				if arg1 == nil then
					arg1 = 0;
				end;
			end;
			return func(arg0, arg1);
		end;
	end;
	return function(...)
		local arg_n = select('#', ...);
		if arg_n < 1 then
			error("missing argument #1 (RegEx expected)", 2);
		elseif arg_n < 2 then
			error("missing argument #2 (string expected)", 2);
		end;
		local arg0, arg1, arg2, arg3, arg4, arg5 = ...;
		if not (proxy[arg0] and proxy[arg0].name == "RegEx") then
			if type(arg0) ~= "string" and type(arg0) ~= "number" then
				error(string.format("invalid argument #1 to %q (RegEx expected, got %s)", name, typeof(arg0)), 2);
			end;
			arg0 = re.fromstring(arg0);
		elseif name == "sub" then
			if type(arg2) == "number" then
				arg2 ..= '';
			elseif type(arg2) ~= "string" then
				error(string.format("invalid argument #3 to 'sub' (string expected, got %s)", typeof(arg2)), 2);
			end;
		elseif type(arg1) == "number" then
			arg1 ..= '';
		elseif type(arg1) ~= "string" then
			error(string.format("invalid argument #2 to %q (string expected, got %s)", name, typeof(arg1)), 2);
		end;
		if name ~= "sub" and name ~= "split" then
			local init_type = typeof(arg2);
			if init_type ~= 'nil' then
				arg2 = tonumber(arg2);
				if not arg2 then
					error(string.format("invalid argument #3 to %q (number expected, got %s)", name, init_type), 2);
				elseif arg2 < 0 then
					arg2 = #arg1 + math.floor(arg2 + 0.5) + 1;
				else
					arg2 = math.max(math.floor(arg2 + 0.5), 1);
				end;
			end;
		end;
		arg0 = proxy[arg0];
		if name == "match" or name == "matchiter" then
			arg3 = ...;
		elseif name == "sub" then
			arg5 = ...;
		end;
		return func(arg0, arg1, arg2, arg3, arg4, arg5);
	end;
end;

--[[ Matches ]]--
local function match_tostr(self)
	local spans = proxy[self].spans;
	local s_start, s_end = spans[0][1], spans[0][2];
	if s_end <= s_start then
		return string.format("Match (%d..%d, empty)", s_start, s_end - 1);
	end;
	return string.format("Match (%d..%d): %s", s_start, s_end - 1, utf8_sub(spans.input, s_start, s_end));
end;

local function new_match(span_arr, group_id, re, str)
	span_arr.source, span_arr.input = re, str;
	local object = newproxy(true);
	local object_mt = getmetatable(object);
	object_mt.__metatable = lockmsg;
	object_mt.__index = setmetatable(span_arr, match_m);
	object_mt.__tostring = match_tostr;

	proxy[object] = { name = "Match", spans = span_arr, group_id = group_id };
	return object;
end;

match_m.group = check_re('Match', 'group', function(self, group_id)
	local span = self.spans[type(group_id) == "number" and group_id or self.group_id[group_id]];
	if not span then
		return nil;
	end;
	return utf8_sub(self.spans.input, span[1], span[2]);
end);

match_m.span = check_re('Match', 'span', function(self, group_id)
	local span = self.spans[type(group_id) == "number" and group_id or self.group_id[group_id]];
	if not span then
		return nil;
	end;
	return span[1], span[2] - 1;
end);

match_m.groups = check_re('Match', 'groups', function(self)
	local spans = self.spans;
	if spans.n > 0 then
		local ret = table.create(spans.n);
		for i = 0, spans.n do
			local v = spans[i];
			if v then
				ret[i] = utf8_sub(spans.input, v[1], v[2]);
			end;
		end;
		return table.unpack(ret, 1, spans.n);
	end;
	return utf8_sub(spans.input, spans[0][1], spans[0][2]);
end);

match_m.groupdict = check_re('Match', 'groupdict', function(self)
	local spans = self.spans;
	local ret = { };
	for k, v in pairs(self.group_id) do
		v = spans[v];
		if v then
			ret[k] = utf8_sub(spans.input, v[1], v[2]);
		end;
	end;
	return ret;
end);

match_m.grouparr = check_re('Match', 'groupdict', function(self)
	local spans = self.spans;
	local ret = table.create(spans.n);
	for i = 0, spans.n do
		local v = spans[i];
		if v then
			ret[i] = utf8_sub(spans.input, v[1], v[2]);
		end;
	end;
	ret.n = spans.n;
	return ret;
end);

--
local line_verbs = {
	CR = 0, LF = 1, CRLF = 2, ANYRLF = 3, ANY = 4, NUL = 5,
};
local function is_newline(str_arr, i, verb_flags)
	local line_verb_n = verb_flags.newline;
	local chr = str_arr[i];
	if line_verb_n == 0 then
		-- carriage return
		return chr == 0x0D;
	elseif line_verb_n == 2 then
		-- carriage return followed by line feed
		return chr == 0x0A and str_arr[i - 1] == 0x20;
	elseif line_verb_n == 3 then
		-- any of the above
		return chr == 0x0A or chr == 0x0D;
	elseif line_verb_n == 4 then
		-- any of Unicode newlines
		return chr == 0x0A or chr == 0x0B or chr == 0x0C or chr == 0x0D or chr == 0x85 or chr == 0x2028 or chr == 0x2029;
	elseif line_verb_n == 5 then
		-- null
		return chr == 0;
	end;
	-- linefeed
	return chr == 0x0A;
end;


local function tkn_char_match(tkn_part, str_arr, i, flags, verb_flags)
	local chr = str_arr[i];
	if not chr then
		return false;
	elseif flags.ignoreCase and chr >= 0x61 and chr <= 0x7A then
		chr -= 0x20;
	end;
	if type(tkn_part) == "number" then
		return tkn_part == chr;
	elseif tkn_part[1] == "charset" then
		for _, v in ipairs(tkn_part[3]) do
			if tkn_char_match(v, str_arr, i, flags, verb_flags) then
				return not tkn_part[2];
			end;
		end;
		return tkn_part[2];
	elseif tkn_part[1] == "range" then
		return chr >= tkn_part[2] and chr <= tkn_part[3] or flags.ignoreCase and chr >= 0x41 and chr <= 0x5A and (chr + 0x20) >= tkn_part[2] and (chr + 0x20) <= tkn_part[3];
	elseif tkn_part[1] == "class" then
		local char_class = tkn_part[2];
		local negate = tkn_part[3];
		local match = false;
		-- if and elseifs :(
		-- Might make these into tables in the future
		if char_class == "xdigit" then
			match = chr >= 0x30 and chr <= 0x39 or chr >= 0x41 and chr <= 0x46 or chr >= 0x61 and chr <= 0x66;
		elseif char_class == "ascii" then
			match = chr <= 0x7F;
		-- cannot be accessed through POSIX classes
		elseif char_class == "vertical_tab" then
			match = chr >= 0x0A and chr <= 0x0D or chr == 0x2028 or chr == 0x2029;
		--
		elseif flags.unicode then
			local current_category = u_categories[chr] or 'Cn';
			local first_category = current_category:sub(1, 1);
			if char_class == "alnum" then
				match = first_category == 'L' or current_category == 'Nl' or current_category == 'Nd';
			elseif char_class == "alpha" then
				match = first_category == 'L' or current_category == 'Nl';
			elseif char_class == "blank" then
				match = current_category == 'Zs' or chr == 0x09;
			elseif char_class == "cntrl" then
				match = current_category == 'Cc';
			elseif char_class == "digit" then
				match = current_category == 'Nd';
			elseif char_class == "graph" then
				match = first_category ~= 'P' and first_category ~= 'C';
			elseif char_class == "lower" then
				match = current_category == 'Ll';
			elseif char_class == "print" then
				match = first_category ~= 'C';
			elseif char_class == "punct" then
				match = first_category == 'P';
			elseif char_class == "space" then
				match = first_category == 'Z' or chr >= 0x09 and chr <= 0x0D;
			elseif char_class == "upper" then
				match = current_category == 'Lu';
			elseif char_class == "word" then
				match = first_category == 'L' or current_category == 'Nl' or current_category == 'Nd' or current_category == 'Pc';
			end;
		elseif char_class == "alnum" then
			match = chr >= 0x30 and chr <= 0x39 or chr >= 0x41 and chr <= 0x5A or chr >= 0x61 and chr <= 0x7A;
		elseif char_class == "alpha" then
			match = chr >= 0x41 and chr <= 0x5A or chr >= 0x61 and chr <= 0x7A;
		elseif char_class == "blank" then
			match = chr == 0x09 or chr == 0x20;
		elseif char_class == "cntrl" then
			match = chr <= 0x1F or chr == 0x7F;
		elseif char_class == "digit" then
			match = chr >= 0x30 and chr <= 0x39;
		elseif char_class == "graph" then
			match = chr >= 0x21 and chr <= 0x7E;
		elseif char_class == "lower" then
			match = chr >= 0x61 and chr <= 0x7A;
		elseif char_class == "print" then
			match = chr >= 0x20 and chr <= 0x7E;
		elseif char_class == "punct" then
			match = class_ascii_punct[chr];
		elseif char_class == "space" then
			match = chr >= 0x09 and chr <= 0x0D or chr == 0x20;
		elseif char_class == "upper" then
			match = chr >= 0x41 and chr <= 0x5A;
		elseif char_class == "word" then
			match = chr >= 0x30 and chr <= 0x39 or chr >= 0x41 and chr <= 0x5A or chr >= 0x61 and chr <= 0x7A or chr == 0x5F;
		end;
		if negate then
			return not match;
		end;
		return match;
	elseif tkn_part[1] == "category" then
		local chr_category = u_categories[chr] or 'Cn';
		local category_v = tkn_part[3];
		local category_len = #category_v;
		if category_len == 3 then
			local match = false;
			if category_v == "Xan" or category_v == "Xwd" then
				match = chr_category:find("^[LN]") or category_v == "Xwd" and chr == 0x5F;
			elseif category_v == "Xps" or category_v == "Xsp" then
				match = chr_category:sub(1, 1) == 'Z' or chr >= 0x09 and chr <= 0x0D;
			elseif category_v == "Xuc" then
				match = tkn_char_match(xuc_chr, str_arr, i, flags, verb_flags);
			end;
			if tkn_part[2] then
				return not match;
			end
			return match;
		elseif chr_category:sub(1, category_len) == category_v then
			return not tkn_part[2];
		end;
		return tkn_part[2];
	elseif tkn_part[1] == 0x2E then
		return flags.dotAll or not is_newline(str_arr, i, verb_flags);
	elseif tkn_part[1] == 0x4E then
		return not is_newline(str_arr, i, verb_flags);
	elseif tkn_part[1] == 0x52 then
		if verb_flags.newline_seq == 0 then
			-- CR, LF or CRLF
			return chr == 0x0A or chr == 0x0D;
		end;
		-- any unicode newline
		return chr == 0x0A or chr == 0x0B or chr == 0x0C or chr == 0x0D or chr == 0x85 or chr == 0x2028 or chr == 0x2029;
	end;
	return false;
end;

local function find_alternation(token, i, count)
	while true do
		local v = token[i];
		local is_table = type(v) == "table";
		if v == alternation then
			return i, count;
		elseif is_table and v[1] == 0x28 then
			if count then
				count += v.count;
			end;
			i = v[3];
		elseif is_table and v[1] == "quantifier" and type(v[5]) == "table" and v[5][1] == 0x28 then
			if count then
				count += v[5].count;
			end;
			i = v[5][3];
		elseif not v or is_table and v[1] == 0x29 then
			return nil, count;
		elseif count then
			if is_table and v[1] == "quantifier" then
				count += v[3];
			else
				count += 1;
			end;
		end;
		i += 1;
	end;
end;

local function re_rawfind(token, str_arr, init, flags, verb_flags, as_bool)
	local tkn_i, str_i, start_i = 0, init, init;
	local states = { };
	while tkn_i do
		if tkn_i == 0 then
			tkn_i += 1;
			local next_alt = find_alternation(token, tkn_i);
			if next_alt then
				table.insert(states, 1, { "alternation", next_alt, str_i });
			end;
			continue;
		end;
		local ctkn = token[tkn_i];
		local tkn_type = type(ctkn) == "table" and ctkn[1];
		if not ctkn then
			break;
		elseif ctkn == "ACCEPT" then
			local not_lookaround = true;
			local close_i = tkn_i;
			repeat
				close_i += 1;
				local is_table = type(token[close_i]) == "table";
				local close_i_tkn = token[close_i];
				if is_table and (close_i_tkn[1] == 0x28 or close_i_tkn[1] == "quantifier" and type(close_i_tkn[5]) == "table" and close_i_tkn[5][1] == 0x28) then
					close_i = close_i_tkn[1] == "quantifier" and close_i_tkn[5][3] or close_i_tkn[3];
				elseif is_table and close_i_tkn[1] == 0x29 and (close_i_tkn[4] == 0x21 or close_i_tkn[4] == 0x3D) then
					not_lookaround = false;
					tkn_i = close_i;
					break;
				end;
			until not close_i_tkn;
			if not_lookaround then
				break;
			end;
		elseif ctkn == "PRUNE" or ctkn == "SKIP" then
			table.insert(states, 1, { ctkn, str_i });
			tkn_i += 1;
		elseif tkn_type == 0x28 then
			table.insert(states, 1, { "group", tkn_i, str_i, nil, ctkn[2], ctkn[3], ctkn[4] });
			tkn_i += 1;
			local next_alt, count = find_alternation(token, tkn_i, (ctkn[4] == 0x21 or ctkn[4] == 0x3D) and ctkn[5] and 0);
			if next_alt then
				table.insert(states, 1, { "alternation", next_alt, str_i });
			end;
			if count then
				str_i -= count;
			end;
		elseif tkn_type == 0x29 and ctkn[4] ~= 0x21 then
			if ctkn[4] == 0x21 or ctkn[4] == 0x3D then
				while true do
					local selected_match_start;
					local selected_state = table.remove(states, 1);
					if selected_state[1] == "group" and selected_state[2] == ctkn[3] then
						if (ctkn[4] == 0x21 or ctkn[4] == 0x3D) and not ctkn[5] then
							str_i = selected_state[3];
						end;
						if selected_match_start then
							table.insert(states, 1, selected_match_start);
						end;
						break;
					elseif selected_state[1] == "matchStart" and not selected_match_start and ctkn[4] == 0x3D then
						selected_match_start = selected_state;
					end;
				end;
			elseif ctkn[4] == 0x3E then
				repeat
					local selected_state = table.remove(states, 1);
				until not selected_state or selected_state[1] == "group" and selected_state[2] == ctkn[3];
			else
				for i, v in ipairs(states) do
					if v[1] == "group" and v[2] == ctkn[3] then
						if v.jmp then
							-- recursive match
							tkn_i = v.jmp;
						end;
						v[4] = str_i;
						if v[7] == "quantifier" and v[10] + 1 < v[9] then
							if token[ctkn[3]][4] ~= "lazy" or v[10] + 1 < v[8] then
								tkn_i = ctkn[3];
							end;
							local ctkn1 = token[ctkn[3]];
							local new_group = { "group", v[2], str_i, nil, ctkn1[5][2], ctkn1[5][3], "quantifier", ctkn1[2], ctkn1[3], v[10] + 1, v[11], ctkn1[4] };
							table.insert(states, 1, new_group);
							if v[11] then
								table.insert(states, 1, { "alternation", v[11], str_i });
							end;
						end;
						break;
					end;
				end;
			end;
			tkn_i += 1;
		elseif tkn_type == 0x4B then
			table.insert(states, 1, { "matchStart", str_i });
			tkn_i += 1;
		elseif tkn_type == 0x7C then
			local close_i = tkn_i;
			repeat
				close_i += 1;
				local is_table = type(token[close_i]) == "table";
				local close_i_tkn = token[close_i];
				if is_table and (close_i_tkn[1] == 0x28 or close_i_tkn[1] == "quantifier" and type(close_i_tkn[5]) == "table" and close_i_tkn[5][1] == 0x28) then
					close_i = close_i_tkn[1] == "quantifier" and close_i_tkn[5][3] or close_i_tkn[3];
				end;
			until is_table and close_i_tkn[1] == 0x29 or not close_i_tkn;
			if token[close_i] then
				for _, v in ipairs(states) do
					if v[1] == "group" and v[6] == close_i then
						tkn_i = v[6];
						break;
					end;
				end;
			else
				tkn_i = close_i;
			end;
		elseif tkn_type == "recurmatch" then
			table.insert(states, 1, { "group", ctkn[3], str_i, nil, nil, token[ctkn[3]][3], nil, jmp = tkn_i });
			tkn_i = ctkn[3] + 1;
			local next_alt, count = find_alternation(token, tkn_i);
			if next_alt then
				table.insert(states, 1, { "alternation", next_alt, str_i });
			end;
		else
			local match;
			if ctkn == "FAIL" then
				match = false;
			elseif tkn_type == 0x29 then
				repeat
					local selected_state = table.remove(states, 1);
				until selected_state[1] == "group" and selected_state[2] == ctkn[3];
			elseif tkn_type == "quantifier" then
				if type(ctkn[5]) == "table" and ctkn[5][1] == 0x28 then
					local next_alt = find_alternation(token, tkn_i + 1);
					if next_alt then
						table.insert(states, 1, { "alternation", next_alt, str_i });
					end;
					table.insert(states, next_alt and 2 or 1, { "group", tkn_i, str_i, nil, ctkn[5][2], ctkn[5][3], "quantifier", ctkn[2], ctkn[3], 0, next_alt, ctkn[4] });
					if ctkn[4] == "lazy" and ctkn[2] == 0 then
						tkn_i = ctkn[5][3];
					end;
					match = true;
				else
					local start_i, end_i;
					local pattern_count = 1;
					local is_backref = type(ctkn[5]) == "table" and ctkn[5][1] == "backref";
					if is_backref then
						pattern_count = 0;
						local group_n = ctkn[5][2];
						for _, v in ipairs(states) do
							if v[1] == "group" and v[5] == group_n then
								start_i, end_i = v[3], v[4];
								pattern_count = end_i - start_i;
								break;
							end;
						end;
					end;
					local min_max_i = str_i + ctkn[2] * pattern_count;
					local mcount = 0;
					while mcount < ctkn[3] do
						if is_backref then
							if start_i and end_i then
								local org_i = str_i;
								if utf8_sub(str_arr.s, start_i, end_i) ~= utf8_sub(str_arr.s, org_i, str_i + pattern_count) then
									break;
								end;
							else
								break;
							end;
						elseif not tkn_char_match(ctkn[5], str_arr, str_i, flags, verb_flags) then
							break;
						end;
						str_i += pattern_count;
						mcount += 1;
					end;
					match = mcount >= ctkn[2];
					if match and ctkn[4] ~= "possessive" then
						if ctkn[4] == "lazy" then
							min_max_i, str_i = str_i, min_max_i;
						end;
						table.insert(states, 1, { "quantifier", tkn_i, str_i, math.min(min_max_i, str_arr.n + 1), (ctkn[4] == "lazy" and 1 or -1) * pattern_count });
					end;
				end;
			elseif tkn_type == "backref" then
				local start_i, end_i;
				local group_n = ctkn[2];
				for _, v in ipairs(states) do
					if v[1] == "group" and v[5] == group_n then
						start_i, end_i = v[3], v[4];
						break;
					end;
				end;
				if start_i and end_i then
					local org_i = str_i;
					str_i += end_i - start_i;
					match = utf8_sub(str_arr.s, start_i, end_i) == utf8_sub(str_arr.s, org_i, str_i);
				end;
			else
				local chr = str_arr[str_i];
				if tkn_type == 0x24 or tkn_type == 0x5A or tkn_type == 0x7A then
					match = str_i == str_arr.n + 1 or tkn_type == 0x24 and flags.multiline and is_newline(str_arr, str_i + 1, verb_flags) or tkn_type == 0x5A and str_i == str_arr.n and is_newline(str_arr, str_i, verb_flags);
				elseif tkn_type == 0x5E or tkn_type == 0x41 or tkn_type == 0x47 then
					match = str_i == 1 or tkn_type == 0x5E and flags.multiline and is_newline(str_arr, str_i - 1, verb_flags) or tkn_type == 0x47 and str_i == init;
				elseif tkn_type == 0x42 or tkn_type == 0x62 then
					local start_m = str_i == 1 or flags.multiline and is_newline(str_arr, str_i - 1, verb_flags);
					local end_m = str_i == str_arr.n + 1 or flags.multiline and is_newline(str_arr, str_i, verb_flags);
					local w_m = tkn_char_match(ctkn[2], str_arr[str_i - 1], flags) and 0 or tkn_char_match(ctkn[2], chr, flags) and 1;
					if w_m == 0 then
						match = end_m or not tkn_char_match(ctkn[2], chr, flags);
					elseif w_m then
						match = start_m or not tkn_char_match(ctkn[2], str_arr[str_i - 1], flags);
					end;
					if tkn_type == 0x42 then
						match = not match;
					end;
				else
					match = tkn_char_match(ctkn, str_arr, str_i, flags, verb_flags);
					str_i += 1;
				end;
			end;
			if not match then
				while true do
					local prev_type, prev_state = states[1] and states[1][1], states[1];
					if not prev_type or prev_type == "PRUNE" or prev_type == "SKIP" then
						if prev_type then
							table.clear(states);
						end;
						if start_i > str_arr.n then
							if as_bool then
								return false;
							end;
							return nil;
						end;
						start_i = prev_type == "SKIP" and prev_state[2] or start_i + 1;
						tkn_i, str_i = 0, start_i;
						break;
					elseif prev_type == "alternation" then
						tkn_i, str_i = prev_state[2], prev_state[3];
						local next_alt, count = find_alternation(token, tkn_i + 1);
						if next_alt then
							prev_state[2] = next_alt;
						else
							table.remove(states, 1);
						end;
						if count then
							str_i -= count;
						end;
						break;
					elseif prev_type == "group" then
						if prev_state[7] == "quantifier" then
							if prev_state[12] == "greedy" and prev_state[10] >= prev_state[8]
								or prev_state[12] == "lazy" and prev_state[10] < prev_state[9] and not prev_state[13] then
								tkn_i, str_i = prev_state[12] == "greedy" and prev_state[6] or prev_state[2], prev_state[3];
								if prev_state[12] == "greedy" then
									table.remove(states, 1);
									break;
								elseif prev_state[10] >= prev_state[8] then
									prev_state[13] = true;
									break;
								end;
							end;
						elseif prev_state[7] == 0x21 then
							table.remove(states, 1);
							tkn_i, str_i = prev_state[6], prev_state[3];
							break;
						end;
					elseif prev_type == "quantifier" then
						if math.sign(prev_state[4] - prev_state[3]) == math.sign(prev_state[5]) then
							prev_state[3] += prev_state[5];
							tkn_i, str_i = prev_state[2], prev_state[3];
							break;
						end;
					end;
					-- keep match out state and recursive state, can be safely removed
					-- prevents infinite loop
					table.remove(states, 1);
				end;
			end;
			tkn_i += 1;
		end;
	end;
	if as_bool then
		return true;
	end;
	local match_start_ran = false;
	local span = table.create(token.group_n);
	span[0], span.n = { start_i, str_i }, token.group_n;
	for _, v in ipairs(states) do
		if v[1] == "matchStart" and not match_start_ran then
			span[0][1], match_start_ran = v[2], true;
		elseif v[1] == "group" and v[5] and not span[v[5]] then
			span[v[5]] = { v[3], v[4] };
		end;
	end;
	return span;
end;

--[[ Methods ]]--
re_m.test = check_re('RegEx', 'test', function(self, str, init)
	return re_rawfind(self.token, to_str_arr(str, init), 1, self.flags, self.verb_flags, true);
end);

re_m.match = check_re('RegEx', 'match', function(self, str, init, source)
	local span = re_rawfind(self.token, to_str_arr(str, init), 1, self.flags, self.verb_flags, false);
	if not span then
		return nil;
	end;
	return new_match(span, self.group_id, source, str);
end);

re_m.matchall = check_re('RegEx', 'matchall', function(self, str, init, source)
	str = to_str_arr(str, init);
	local i = 1;
	return function()
		local span = i <= str.n + 1 and re_rawfind(self.token, str, i, self.flags, self.verb_flags, false);
		if not span then
			return nil;
		end;
		i = span[0][2] + (span[0][1] >= span[0][2] and 1 or 0);
		return new_match(span, self.group_id, source, str.s);
	end;
end);

local function insert_tokenized_sub(repl_r, str, span, tkn)
	for _, v in ipairs(tkn) do
		if type(v) == "table" then
			if v[1] == "condition" then
				if span[v[2]] then
					if v[3] then
						insert_tokenized_sub(repl_r, str, span, v[3]);
					else
						table.move(str, span[v[2]][1], span[v[2]][2] - 1, #repl_r + 1, repl_r);
					end;
				elseif v[4] then
					insert_tokenized_sub(repl_r, str, span, v[4]);
				end;
			else
				table.move(v, 1, #v, #repl_r + 1, repl_r);
			end;
		elseif span[v] then
			table.move(str, span[v][1], span[v][2] - 1, #repl_r + 1, repl_r);
		end;
	end;
	repl_r.n = #repl_r;
	return repl_r;
end;

re_m.sub = check_re('RegEx', 'sub', function(self, repl, str, n, repl_flag_str, source)
	if repl_flag_str ~= nil and type(repl_flag_str) ~= "number" and type(repl_flag_str) ~= "string" then
		error(string.format("invalid argument #5 to 'sub' (string expected, got %s)", typeof(repl_flag_str)), 3);
	end
	local repl_flags = {
		l = false, o = false, u = false,
	};
	for f in string.gmatch(repl_flag_str or '', utf8.charpattern) do
		if repl_flags[f] ~= false then
			error("invalid regular expression substitution flag " .. f, 3);
		end;
		repl_flags[f] = true;
	end;
	local repl_type = type(repl);
	if repl_type == "number" then
		repl ..= '';
	elseif repl_type ~= "string" and repl_type ~= "function" and (not repl_flags.o or repl_type ~= "table") then
		error(string.format("invalid argument #2 to 'sub' (string/function%s expected, got %s)", repl_flags.o and "/table" or '', typeof(repl)), 3);
	end;
	if tonumber(n) then
		n = tonumber(n);
		if n <= -1 or n ~= n then
			n = math.huge;
		end;
	elseif n ~= nil then
		error(string.format("invalid argument #4 to 'sub' (number expected, got %s)", typeof(n)), 3);
	else
		n = math.huge;
	end;
	if n < 1 then
		return str, 0;
	end;
	local min_repl_n = 0;
	if repl_type == "string" then
		repl = to_str_arr(repl);
		if not repl_flags.l then
			local i1 = 0;
			local repl_r = table.create(3);
			local group_n = self.token.group_n;
			local conditional_c = { };
			while i1 < repl.n do
				local i2 = i1;
				repeat
					i2 += 1;
				until not repl[i2] or repl[i2] == 0x24 or repl[i2] == 0x5C or (repl[i2] == 0x3A or repl[i2] == 0x7D) and conditional_c[1];
				min_repl_n += i2 - i1 - 1;
				if i2 - i1 > 1 then
					table.insert(repl_r, table.move(repl, i1 + 1, i2 - 1, 1, table.create(i2 - i1 - 1)));
				end;
				if repl[i2] == 0x3A then
					local current_conditional_c = conditional_c[1];
					if current_conditional_c[2] then
						error("malformed substitution pattern", 3);
					end;
					current_conditional_c[2] = table.move(repl_r, current_conditional_c[3], #repl_r, 1, table.create(#repl_r + 1 - current_conditional_c[3]));
					for i3 = #repl_r, current_conditional_c[3], -1 do
						repl_r[i3] = nil;
					end;
				elseif repl[i2] == 0x7D then
					local current_conditional_c = table.remove(conditional_c, 1);
					local second_c = table.move(repl_r, current_conditional_c[3], #repl_r, 1, table.create(#repl_r + 1 - current_conditional_c[3]));
					for i3 = #repl_r, current_conditional_c[3], -1 do
						repl_r[i3] = nil;
					end;
					table.insert(repl_r, { "condition", current_conditional_c[1], current_conditional_c[2] ~= true and (current_conditional_c[2] or second_c), current_conditional_c[2] and second_c });
				elseif repl[i2] then
					i2 += 1;
					local subst_c = repl[i2];
					if not subst_c then
						if repl[i2 - 1] == 0x5C then
							error("replacement string must not end with a trailing backslash", 3);
						end;
						local prev_repl_f = repl_r[#repl_r];
						if type(prev_repl_f) == "table" then
							table.insert(prev_repl_f, repl[i2 - 1]);
						else
							table.insert(repl_r, { repl[i2 - 1] });
						end;
					elseif subst_c == 0x5C and repl[i2 - 1] == 0x24 then
						local prev_repl_f = repl_r[#repl_r];
						if type(prev_repl_f) == "table" then
							table.insert(prev_repl_f, 0x24);
						else
							table.insert(repl_r, { 0x24 });
						end;
						i2 -= 1;
						min_repl_n += 1;
					elseif subst_c == 0x30 then
						table.insert(repl_r, 0);
					elseif subst_c > 0x30 and subst_c <= 0x39 then
						local start_i2 = i2;
						local group_i = subst_c - 0x30;
						while repl[i2 + 1] and repl[i2 + 1] >= 0x30 and repl[i2 + 1] <= 0x39 do
							group_i ..= repl[i2 + 1] - 0x30;
							i2 += 1;
						end;
						group_i = tonumber(group_i);
						if not repl_flags.u and group_i > group_n then
							error("reference to non-existent subpattern", 3);
						end;
						table.insert(repl_r, group_i);
					elseif subst_c == 0x7B and repl[i2 - 1] == 0x24 then
						i2 += 1;
						local start_i2 = i2;
						while repl[i2] and
							(repl[i2] >= 0x30 and repl[i2] <= 0x39
								or repl[i2] >= 0x41 and repl[i2] <= 0x5A
								or repl[i2] >= 0x61 and repl[i2] <= 0x7A
								or repl[i2] == 0x5F) do
							i2 += 1;
						end;
						if (repl[i2] == 0x7D or repl[i2] == 0x3A and (repl[i2 + 1] == 0x2B or repl[i2 + 1] == 0x2D)) and i2 ~= start_i2 then
							local group_k = utf8_sub(repl.s, start_i2, i2);
							if repl[start_i2] >= 0x30 and repl[start_i2] <= 0x39 then
								group_k = tonumber(group_k);
								if not repl_flags.u and group_k > group_n then
									error("reference to non-existent subpattern", 3);
								end;
							else
								group_k = self.group_id[group_k];
								if not repl_flags.u and (not group_k or group_k > group_n) then
									error("reference to non-existent subpattern", 3);
								end;
							end;
							if repl[i2] == 0x3A then
								i2 += 1;
								table.insert(conditional_c, { group_k, repl[i2] == 0x2D, #repl_r + 1 });
							else
								table.insert(repl_r, group_k);
							end;
						else
							error("malformed substitution pattern", 3);
						end;
					else
						local c_escape_char;
						if repl[i2 - 1] == 0x24 then
							if subst_c ~= 0x24 then
								local prev_repl_f = repl_r[#repl_r];
								if type(prev_repl_f) == "table" then
									table.insert(prev_repl_f, 0x24);
								else
									table.insert(repl_r, { 0x24 });
								end;
							end;
						else
							c_escape_char = escape_chars[repl[i2]];
							if type(c_escape_char) ~= "number" then
								c_escape_char = nil;
							end;
						end;
						local prev_repl_f = repl_r[#repl_r];
						if type(prev_repl_f) == "table" then
							table.insert(prev_repl_f, c_escape_char or repl[i2]);
						else
							table.insert(repl_r, { c_escape_char or repl[i2] });
						end;
						min_repl_n += 1;
					end;
				end;
				i1 = i2;
			end;
			if conditional_c[1] then
				error("malformed substitution pattern", 3);
			end;
			if not repl_r[2] and type(repl_r[1]) == "table" and repl_r[1][1] ~= "condition" then
				repl, repl.n = repl_r[1], #repl_r[1];
			else
				repl, repl_type = repl_r, "subst_string";
			end;
		end;
	end;
	str = to_str_arr(str);
	local incr, i0, count = 0, 1, 0;
	while i0 <= str.n + incr + 1 do
		local span = re_rawfind(self.token, str, i0, self.flags, self.verb_flags, false);
		if not span then
			break;
		end;
		local repl_r;
		if repl_type == "string" then
			repl_r = repl;
		elseif repl_type == "subst_string" then
			repl_r = insert_tokenized_sub(table.create(min_repl_n), str, span, repl);
		else
			local re_match;
			local repl_c;
			if repl_type == "table" then
				re_match = utf8_sub(str.s, span[0][1], span[0][2]);
				repl_c = repl[re_match];
			else
				re_match = new_match(span, self.group_id, source, str.s);
				repl_c = repl(re_match);
			end;
			if repl_c == re_match or repl_flags.o and not repl_c then
				local repl_n = span[0][2] - span[0][1];
				repl_r = table.move(str, span[0][1], span[0][2] - 1, 1, table.create(repl_n));
				repl_r.n = repl_n;
			elseif type(repl_c) == "string" then
				repl_r = to_str_arr(repl_c);
			elseif type(repl_c) == "number" then
				repl_r = to_str_arr(repl_c .. '');
			elseif repl_flags.o then
				error(string.format("invalid replacement value (a %s)", type(repl_c)), 3);
			else
				repl_r = { n = 0 };
			end;
		end;
		local match_len = span[0][2] - span[0][1];
		local repl_len = math.min(repl_r.n, match_len);
		for i1 = 0, repl_len - 1 do
			str[span[0][1] + i1] = repl_r[i1 + 1];
		end;
		local i1 = span[0][1] + repl_len;
		i0 = span[0][2];
		if match_len > repl_r.n then
			for i2 = 1, match_len - repl_r.n do
				table.remove(str, i1);
				incr -= 1;
				i0 -= 1;
			end;
		elseif repl_r.n > match_len then
			for i2 = 1, repl_r.n - match_len do
				table.insert(str, i1 + i2 - 1, repl_r[repl_len + i2]);
				incr += 1;
				i0 += 1;
			end;
		end;
		if match_len <= 0 then
			i0 += 1;
		end;
		count += 1;
		if n < count + 1 then
			break;
		end;
	end;
	return from_str_arr(str), count;
end);

re_m.split = check_re('RegEx', 'split', function(self, str, n)
	if tonumber(n) then
		n = tonumber(n);
		if n <= -1 or n ~= n then
			n = math.huge;
		end;
	elseif n ~= nil then
		error(string.format("invalid argument #3 to 'split' (number expected, got %s)", typeof(n)), 3);
	else
		n = math.huge;
	end;
	str = to_str_arr(str);
	local i, count = 1, 0;
	local ret = { };
	local prev_empty = 0;
	while i <= str.n + 1 do
		count += 1;
		local span = n >= count and re_rawfind(self.token, str, i, self.flags, self.verb_flags, false);
		if not span then
			break;
		end;
		table.insert(ret, utf8_sub(str.s, i - prev_empty, span[0][1]));
		prev_empty = span[0][1] >= span[0][2] and 1 or 0;
		i = span[0][2] + prev_empty;
	end;
	table.insert(ret, string.sub(str.s, utf8.offset(str.s, i - prev_empty)));
	return ret;
end);

--
local function re_index(self, index)
	return re_m[index] or proxy[self].flags[index];
end;

local function re_tostr(self)
	return proxy[self].pattern_repr .. proxy[self].flag_repr;
end;
--

local other_valid_group_char = {
	-- non-capturing group
	[0x3A] = true,
	-- lookarounds
	[0x21] = true, [0x3D] = true,
	-- atomic
	[0x3E] = true,
	-- branch reset
	[0x7C] = true,
};

local function tokenize_ptn(codes, flags)
	if flags.unicode and not options.unicodeData then
		return "options.unicodeData cannot be turned off while having unicode flag";
	end;
	local i, len = 1, codes.n;
	local group_n = 0;
	local outln, group_id, verb_flags = { }, { }, {
		newline = 1, newline_seq = 1, not_empty = 0,
	};
	while i <= len do
		local c = codes[i];
		if c == 0x28 then
			-- Match
			local ret;
			if codes[i + 1] == 0x2A then
				i += 2;
				local start_i = i;
				while codes[i]
					and (codes[i] >= 0x30 and codes[i] <= 0x39
					or codes[i] >= 0x41 and codes[i] <= 0x5A
					or codes[i] >= 0x61 and codes[i] <= 0x7A
					or codes[i] == 0x5F or codes[i] == 0x3A) do
					i += 1;
				end;
				if codes[i] ~= 0x29 and codes[i - 1] ~= 0x3A then
					-- fallback as normal and ( can't be repeated
					return "quantifier doesn't follow a repeatable pattern";
				end;
				local selected_verb = utf8_sub(codes.s, start_i, i);
				if selected_verb == "positive_lookahead:" or selected_verb == "negative_lookhead:"
					or selected_verb == "positive_lookbehind:" or selected_verb == "negative_lookbehind:"
					or selected_verb:find("^[pn]l[ab]:$") then
					ret = { 0x28, nil, nil, selected_verb:find('^n') and 0x21 or 0x3D, selected_verb:find('b', 3, true) and 1 };
				elseif selected_verb == "atomic:" then
					ret = { 0x28, nil, nil, 0x3E, nil };
				elseif selected_verb == "ACCEPT" or selected_verb == "FAIL" or selected_verb == 'F' or selected_verb == "PRUNE" or selected_verb == "SKIP" then
					ret = selected_verb == 'F' and "FAIL" or selected_verb;
				else
					if line_verbs[selected_verb] then
						verb_flags.newline = selected_verb;
					elseif selected_verb == "BSR_ANYCRLF" or selected_verb == "BSR_UNICODE" then
						verb_flags.newline_seq = selected_verb == "BSR_UNICODE" and 1 or 0;
					elseif selected_verb == "NOTEMPTY" or selected_verb == "NOTEMPTY_ATSTART" then
						verb_flags.not_empty = selected_verb == "NOTEMPTY" and 1 or 2;
					else
						return "unknown or malformed verb";
					end;
					if outln[1] then
						return "this verb must be placed at the beginning of the regex";
					end;
				end;
			elseif codes[i + 1] == 0x3F then
				-- ? syntax
				i += 2;
				if codes[i] == 0x23 then
					-- comments
					i = table.find(codes, 0x29, i);
					if not i then
						return "unterminated parenthetical";
					end;
					i += 1;
					continue;
				elseif not codes[i] then
					return "unterminated parenthetical";
				end;
				ret = { 0x28, nil, nil, codes[i], nil };
				if codes[i] == 0x30 and codes[i + 1] == 0x29 then
					-- recursive match entire pattern
					ret[1], ret[2], ret[3], ret[5] = "recurmatch", 0, 0, nil;
				elseif codes[i] > 0x30 and codes[i] <= 0x39 then
					-- recursive match
					local org_i = i;
					i += 1;
					while codes[i] >= 0x30 and codes[i] <= 0x30 do
						i += 1;
					end;
					if codes[i] ~= 0x29 then
						return "invalid group structure";
					end;
					ret[1], ret[2], ret[4] = "recurmatch", tonumber(utf8_sub(codes.s, org_i, i)), nil;
				elseif codes[i] == 0x3C and codes[i + 1] == 0x21 or codes[i + 1] == 0x3D then
					-- lookbehinds
					i += 1;
					ret[4], ret[5] = codes[i], 1;
				elseif codes[i] == 0x7C then
					-- branch reset
					ret[5] = group_n;
				elseif codes[i] == 0x50 or codes[i] == 0x3C or codes[i] == 0x27 then
					if codes[i] == 0x50 then
						i += 1;
					end;
					if codes[i] == 0x3D then
						-- backref
						local start_i = i + 1;
						while codes[i] and
							(codes[i] >= 0x30 and codes[i] <= 0x39
								or codes[i] >= 0x41 and codes[i] <= 0x5A
								or codes[i] >= 0x61 and codes[i] <= 0x7A
								or codes[i] == 0x5F) do
							i += 1;
						end;
						if not codes[i] then
							return "unterminated parenthetical";
						elseif codes[i] ~= 0x29 or i == start_i then
							return "invalid group structure";
						end;
						ret = { "backref", utf8_sub(codes.s, start_i, i) };
					elseif codes[i] == 0x3C or codes[i - 1] ~= 0x50 and codes[i] == 0x27 then
						-- named capture
						local delimiter = codes[i] == 0x27 and 0x27 or 0x3E;
						local start_i = i + 1;
						i += 1;
						if codes[i] == 0x29 then
							return "missing character in subpattern";
						elseif codes[i] >= 0x30 and codes[i] <= 0x39 then
							return "subpattern name must not begin with a digit";
						elseif not (codes[i] >= 0x41 and codes[i] <= 0x5A or codes[i] >= 0x61 and codes[i] <= 0x7A or codes[i] == 0x5F) then
							return "invalid character in subpattern";
						end;
						i += 1;
						while codes[i] and
							(codes[i] >= 0x30 and codes[i] <= 0x39
								or codes[i] >= 0x41 and codes[i] <= 0x5A
								or codes[i] >= 0x61 and codes[i] <= 0x7A
								or codes[i] == 0x5F) do
							i += 1;
						end;
						if not codes[i] then
							return "unterminated parenthetical";
						elseif codes[i] ~= delimiter then
							return "invalid character in subpattern";
						end;
						local name = utf8_sub(codes.s, start_i, i);
						group_n += 1;
						if (group_id[name] or group_n) ~= group_n then
							return "subpattern name already exists";
						end;
						for name1, group_n1 in pairs(group_id) do
							if name ~= name1 and group_n == group_n1 then
								return "different names for subpatterns of the same number aren't permitted";
							end;
						end;
						group_id[name] = group_n;
						ret[2], ret[4] = group_n, nil;
					else
						return "invalid group structure";
					end;
				elseif not other_valid_group_char[codes[i]] then
					return "invalid group structure";
				end;
			else
				group_n += 1;
				ret = { 0x28, group_n, nil, nil };
			end;
			if ret then
				table.insert(outln, ret);
			end;
		elseif c == 0x29 then
			-- Close parenthesis
			local i1 = #outln + 1;
			local lookbehind_c = -1;
			local current_lookbehind_c = 0;
			local max_c, group_c = 0, 0;
			repeat
				i1 -= 1;
				local v, is_table = outln[i1], type(outln[i1]) == "table";
				if is_table and v[1] == 0x28 then
					group_c += 1;
					if current_lookbehind_c and v.count then
						current_lookbehind_c += v.count;
					end;
					if not v[3] then
						if v[4] == 0x7C then
							group_n = v[5] + math.max(max_c, group_c);
						end;
						if current_lookbehind_c ~= lookbehind_c and lookbehind_c ~= -1 then
							lookbehind_c = nil;
						else
							lookbehind_c = current_lookbehind_c;
						end;
						break;
					end;
				elseif v == alternation then
					if current_lookbehind_c ~= lookbehind_c and lookbehind_c ~= -1 then
						lookbehind_c, current_lookbehind_c = nil, nil;
					else
						lookbehind_c, current_lookbehind_c = current_lookbehind_c, 0;
					end;
					max_c, group_c = math.max(max_c, group_c), 0;
				elseif current_lookbehind_c then
					if is_table and v[1] == "quantifier" then
						if v[2] == v[3] then
							current_lookbehind_c += v[2];
						else
							current_lookbehind_c = nil;
						end;
					else
						current_lookbehind_c += 1;
					end;
				end;
			until i1 < 1;
			if i1 < 1 then
				return "unmatched ) in regular expression";
			end;
			local v = outln[i1];
			local outln_len_p_1 = #outln + 1;
			local ret = { 0x29, v[2], i1, v[4], v[5], count = lookbehind_c };
			if (v[4] == 0x21 or v[4] == 0x3D) and v[5] and not lookbehind_c then
				return "lookbehind assertion is not fixed width";
			end;
			v[3] = outln_len_p_1;
			table.insert(outln, ret);
		elseif c == 0x2E then
			table.insert(outln, dot);
		elseif c == 0x5B then
			-- Character set
			local negate, char_class = false, nil;
			i += 1;
			local start_i = i;
			if codes[i] == 0x5E then
				negate = true;
				i += 1;
			elseif codes[i] == 0x2E or codes[i] == 0x3A or codes[i] == 0x3D then
				-- POSIX character classes
				char_class = codes[i];
			end;
			local ret;
			if codes[i] == 0x5B or codes[i] == 0x5C then
				ret = { };
			else
				ret = { codes[i] };
				i += 1;
			end;
			while codes[i] ~= 0x5D do
				if not codes[i] then
					return "unterminated character class";
				elseif codes[i] == 0x2D and ret[1] and type(ret[1]) == "number" then
					if codes[i + 1] == 0x5D then
						table.insert(ret, 1, 0x2D);
					else
						i += 1;
						local ret_c = codes[i];
						if ret_c == 0x5B then
							if codes[i + 1] == 0x2E or codes[i + 1] == 0x3A or codes[i + 1] == 0x3D then
								-- Check for POSIX character class, name does not matter
								local i1 = i + 2;
								repeat
									i1 = table.find(codes, 0x5D, i1);
								until not i1 or codes[i1 - 1] ~= 0x5C;
								if not i1 then
									return "unterminated character class";
								elseif codes[i1 - 1] == codes[i + 1] and i1 - 1 ~= i + 1 then
									return "invalid range in character class";
								end;
							end;
							if ret[1] > 0x5B then
								return "invalid range in character class";
							end;
						elseif ret_c == 0x5C then
							i += 1;
							if codes[i] == 0x78 then
								local radix0, radix1;
								i += 1;
								if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x39 or codes[i] >= 0x41 and codes[i] <= 0x46 or codes[i] >= 0x61 and codes[i] <= 0x66 then
									radix0 = codes[i] - ((codes[i] >= 0x41 and codes[i] <= 0x5A) and 0x37 or (codes[i] >= 0x61 and codes[i] <= 0x7A) and 0x57 or 0x30);
									i += 1;
									if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x39 or codes[i] >= 0x41 and codes[i] <= 0x46 or codes[i] >= 0x61 and codes[i] <= 0x66 then
										radix1 = codes[i] - ((codes[i] >= 0x41 and codes[i] <= 0x5A) and 0x37 or (codes[i] >= 0x61 and codes[i] <= 0x7A) and 0x57 or 0x30);
									else
										i -= 1;
									end;
								else
									i -= 1;
								end;
								ret_c = radix0 and (radix1 and 16 * radix0 + radix1 or radix0) or 0;
							elseif codes[i] >= 0x30 and codes[i] <= 0x37 then
								local radix0, radix1, radix2 = codes[i] - 0x30, nil, nil;
								i += 1;
								if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x37 then
									radix1 = codes[i] - 0x30;
									i += 1;
									if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x37 then
										radix2 = codes[i] - 0x30;
									else
										i -= 1;
									end;
								else
									i -= 1;
								end;
								ret_c = radix1 and (radix2 and 64 * radix0 + 8 * radix1 + radix2 or 8 * radix0 + radix1) or radix0;
							else
								ret_c = escape_chars[codes[i]] or codes[i];
								if type(ret_c) ~= "number" then
									return "invalid range in character class";
								end;
							end;
						elseif ret[1] > ret_c then
							return "invalid range in character class";
						end;
						ret[1] = { "range", ret[1], ret_c };
					end;
				elseif codes[i] == 0x5B then
					if codes[i + 1] == 0x2E or codes[i + 1] == 0x3A or codes[i + 1] == 0x3D then
						local i1 = i + 2;
						repeat
							i1 = table.find(codes, 0x5D, i1);
						until not i1 or codes[i1 - 1] ~= 0x5C;
						if not i1 then
							return "unterminated character class";
						elseif codes[i1 - 1] ~= codes[i + 1] or i1 - 1 == i + 1 then
							table.insert(ret, 1, 0x5B);
						elseif codes[i1 - 1] == 0x2E or codes[i1 - 1] == 0x3D then
							return "POSIX collating elements aren't supported";
						elseif codes[i1 - 1] == 0x3A then
							-- I have no plans to support escape codes (\) in character class names
							local negate = codes[i + 3] == 0x5E;
							local class_name = utf8_sub(codes.s, i + (negate and 3 or 2), i1 - 1);
							--  If not valid then throw an error
							if not posix_class_names[class_name] then
								return "unknown POSIX class name";
							end;
							table.insert(ret, 1, { "class", class_name, negate });
							i = i1;
						end;
					else
						table.insert(ret, 1, 0x5B);
					end;
				elseif codes[i] == 0x5C then
					i += 1;
					if codes[i] == 0x78 then
						local radix0, radix1;
						i += 1;
						if codes[i] == 0x7B then
							i += 1;
							local org_i = i;
							while codes[i] and
								(codes[i] >= 0x30 and codes[i] <= 0x39
									or codes[i] >= 0x41 and codes[i] <= 0x46
									or codes[i] >= 0x61 and codes[i] <= 0x66) do
								i += 1;
							end;
							if codes[i] ~= 0x7D or i == org_i then
								return "malformed hexadecimal character";
							elseif i - org_i > 4 then
								return "character offset too large";
							end;
							table.insert(ret, 1, tonumber(utf8_sub(codes.s, org_i, i), 16));
						else
							if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x39 or codes[i] >= 0x41 and codes[i] <= 0x46 or codes[i] >= 0x61 and codes[i] <= 0x66 then
								radix0 = codes[i] - ((codes[i] >= 0x41 and codes[i] <= 0x5A) and 0x37 or (codes[i] >= 0x61 and codes[i] <= 0x7A) and 0x57 or 0x30);
								i += 1;
								if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x39 or codes[i] >= 0x41 and codes[i] <= 0x46 or codes[i] >= 0x61 and codes[i] <= 0x66 then
									radix1 = codes[i] - ((codes[i] >= 0x41 and codes[i] <= 0x5A) and 0x37 or (codes[i] >= 0x61 and codes[i] <= 0x7A) and 0x57 or 0x30);
								else
									i -= 1;
								end;
							else
								i -= 1;
							end;
							table.insert(ret, 1, radix0 and (radix1 and 16 * radix0 + radix1 or radix0) or 0);
						end;
					elseif codes[i] >= 0x30 and codes[i] <= 0x37 then
						local radix0, radix1, radix2 = codes[i] - 0x30, nil, nil;
						i += 1;
						if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x37 then
							radix1 = codes[i] - 0x30;
							i += 1;
							if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x37 then
								radix2 = codes[i] - 0x30;
							else
								i -= 1;
							end;
						else
							i -= 1;
						end;
						table.insert(ret, 1, radix1 and (radix2 and 64 * radix0 + 8 * radix1 + radix2 or 8 * radix0 + radix1) or radix0);
					elseif codes[i] == 0x45 then
						-- intentionally left blank, \E that's not preceded \Q is ignored
					elseif codes[i] == 0x51 then
						local start_i = i + 1;
						repeat
							i = table.find(codes, 0x5C, i + 1);
						until not i or codes[i + 1] == 0x45;
						table.move(codes, start_i, i and i - 1 or #codes, #outln + 1, outln);
						if not i then
							break;
						end;
						i += 1;
					elseif codes[i] == 0x4E then
						if codes[i + 1] == 0x7B and codes[i + 2] == 0x55 and codes[i + 3] == 0x2B and flags.unicode then
							i += 4;
							local start_i = i;
							while codes[i] and
								(codes[i] >= 0x30 and codes[i] <= 0x39
									or codes[i] >= 0x41 and codes[i] <= 0x46
									or codes[i] >= 0x61 and codes[i] <= 0x66) do
								i += 1;
							end;
							if codes[i] ~= 0x7D or i == start_i then
								return "malformed Unicode code point";
							end;
							local code_point = tonumber(utf8_sub(codes.s, start_i, i));
							table.insert(ret, 1, code_point);
						else
							return "invalid escape sequence";
						end;
					elseif codes[i] == 0x50 or codes[i] == 0x70 then
						if not options.unicodeData then
							return "options.unicodeData cannot be turned off when using \\p";
						end;
						i += 1;
						if codes[i] ~= 0x7B then
							local c_name = utf8.char(codes[i] or 0);
							if not valid_categories[c_name] then
								return "unknown or malformed script name";
							end;
							table.insert(ret, 1, { "category", false, c_name });
						else
							local negate = codes[i] == 0x50;
							i += 1;
							if codes[i] == 0x5E then
								i += 1;
								negate = not negate;
							end;
							local start_i = i;
							while codes[i] and
								(codes[i] >= 0x30 and codes[i] <= 0x39
									or codes[i] >= 0x41 and codes[i] <= 0x5A
									or codes[i] >= 0x61 and codes[i] <= 0x7A
									or codes[i] == 0x5F) do
								i += 1;
							end;
							if codes[i] ~= 0x7D then
								return "unknown or malformed script name";
							end;
							local c_name = utf8_sub(codes.s, start_i, i);
							local script_set = chr_scripts[c_name];
							if script_set then
								table.insert(ret, 1, { "charset", negate, script_set });
							elseif not valid_categories[c_name] then
								return "unknown or malformed script name";
							else
								table.insert(ret, 1, { "category", negate, c_name });
							end;
						end;
					elseif codes[i] == 0x6F then
						i += 1;
						if codes[i] ~= 0x7B then
							return "malformed octal code";
						end;
						i += 1;
						local org_i = i;
						while codes[i] and codes[i] >= 0x30 and codes[i] <= 0x37 do
							i += 1;
						end;
						if codes[i] ~= 0x7D or i == org_i then
							return "malformed octal code";
						end;
						local ret_chr = tonumber(utf8_sub(codes.s, org_i, i), 8);
						if ret_chr > 0xFFFF then
							return "character offset too large";
						end;
						table.insert(ret, 1, ret_chr);
					else
						local esc_char = escape_chars[codes[i]];
						table.insert(ret, 1, type(esc_char) == "string" and { "class", esc_char, false } or esc_char or codes[i]);
					end;
				elseif flags.ignoreCase and codes[i] >= 0x61 and codes[i] <= 0x7A then
					table.insert(ret, 1, codes[i] - 0x20);
				else
					table.insert(ret, 1, codes[i]);
				end;
				i += 1;
			end;
			if codes[i - 1] == char_class and i - 1 ~= start_i then
				return char_class == 0x3A and "POSIX named classes are only support within a character set" or "POSIX collating elements aren't supported";
			end;
			if not ret[2] and not negate then
				table.insert(outln, ret[1]);
			else
				table.insert(outln, { "charset", negate, ret });
			end;
		elseif c == 0x5C then
			-- Escape char
			i += 1;
			local escape_c = codes[i];
			if not escape_c then
				return "pattern may not end with a trailing backslash";
			elseif escape_c >= 0x30 and escape_c <= 0x39 then
				local org_i = i;
				while codes[i + 1] and codes[i + 1] >= 0x30 and codes[i + 1] <= 0x39 do
					i += 1;
				end;
				local escape_d = tonumber(utf8_sub(codes.s, org_i, i + 1));
				if escape_d > group_n and i ~= org_i then
					i = org_i;
					local radix0, radix1, radix2;
					if codes[i] <= 0x37 then
						radix0 = codes[i] - 0x30;
						i += 1;
						if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x37 then
							radix1 = codes[i] - 0x30;
							i += 1;
							if codes[i] and codes[i] >= 0x30 and codes[i] <= 0x37 then
								radix2 = codes[i] - 0x30;
							else
								i -= 1;
							end;
						else
							i -= 1;
						end;
					end;
					table.insert(outln, radix0 and (radix1 and (radix2 and 64 * radix0 + 8 * radix1 + radix2 or 8 * radix0 + radix1) or radix0) or codes[org_i]);
				else
					table.insert(outln, { "backref", escape_d });
				end;
			elseif escape_c == 0x45 then
				-- intentionally left blank, \E that's not preceded \Q is ignored
			elseif escape_c == 0x51 then
				local start_i = i + 1;
				repeat
					i = table.find(codes, 0x5C, i + 1);
				until not i or codes[i + 1] == 0x45;
				table.move(codes, start_i, i and i - 1 or #codes, #outln + 1, outln);
				if not i then
					break;
				end;
				i += 1;
			elseif escape_c == 0x4E then
				if codes[i + 1] == 0x7B and codes[i + 2] == 0x55 and codes[i + 3] == 0x2B and flags.unicode then
					i += 4;
					local start_i = i;
					while codes[i] and
						(codes[i] >= 0x30 and codes[i] <= 0x39
							or codes[i] >= 0x41 and codes[i] <= 0x46
							or codes[i] >= 0x61 and codes[i] <= 0x66) do
						i += 1;
					end;
					if codes[i] ~= 0x7D or i == start_i then
						return "malformed Unicode code point";
					end;
					local code_point = tonumber(utf8_sub(codes.s, start_i, i));
					table.insert(outln, code_point);
				else
					table.insert(outln, escape_chars[0x4E]);
				end;
			elseif escape_c == 0x50 or escape_c == 0x70 then
				if not options.unicodeData then
					return "options.unicodeData cannot be turned off when using \\p";
				end;
				i += 1;
				if codes[i] ~= 0x7B then
					local c_name = utf8.char(codes[i] or 0);
					if not valid_categories[c_name] then
						return "unknown or malformed script name";
					end;
					table.insert(outln, { "category", false, c_name });
				else
					local negate = escape_c == 0x50;
					i += 1;
					if codes[i] == 0x5E then
						i += 1;
						negate = not negate;
					end;
					local start_i = i;
					while codes[i] and
						(codes[i] >= 0x30 and codes[i] <= 0x39
							or codes[i] >= 0x41 and codes[i] <= 0x5A
							or codes[i] >= 0x61 and codes[i] <= 0x7A
							or codes[i] == 0x5F) do
						i += 1;
					end;
					if codes[i] ~= 0x7D then
						return "unknown or malformed script name";
					end;
					local c_name = utf8_sub(codes.s, start_i, i);
					local script_set = chr_scripts[c_name];
					if script_set then
						table.insert(outln, { "charset", negate, script_set });
					elseif not valid_categories[c_name] then
						return "unknown or malformed script name";
					else
						table.insert(outln, { "category", negate, c_name });
					end;
				end;
			elseif escape_c == 0x67 and (codes[i + 1] == 0x7B or codes[i + 1] >= 0x30 and codes[i + 1] <= 0x39) then
				local is_grouped = false;
				i += 1;
				if codes[i] == 0x7B then
					i += 1;
					is_grouped = true;
				elseif codes[i] < 0x30 or codes[i] > 0x39 then
					return "malformed reference code";
				end;
				local org_i = i;
				while codes[i] and
					(codes[i] >= 0x30 and codes[i] <= 0x39
						or codes[i] >= 0x41 and codes[i] <= 0x46
						or codes[i] >= 0x61 and codes[i] <= 0x66) do
					i += 1;
				end;
				if is_grouped and codes[i] ~= 0x7D then
					return "malformed reference code";
				end;
				local ref_name = tonumber(utf8_sub(codes.s, org_i, i + (is_grouped and 0 or 1)));
				table.insert(outln, { "backref", ref_name });
				if not is_grouped then
					i -= 1;
				end;
			elseif escape_c == 0x6F then
				i += 1;
				if codes[i + 1] ~= 0x7B then
					return "malformed octal code";
				end
				i += 1;
				local org_i = i;
				while codes[i] and codes[i] >= 0x30 and codes[i] <= 0x37 do
					i += 1;
				end;
				if codes[i] ~= 0x7D or i == org_i then
					return "malformed octal code";
				end;
				local ret_chr = tonumber(utf8_sub(codes.s, org_i, i), 8);
				if ret_chr > 0xFFFF then
					return "character offset too large";
				end;
				table.insert(outln, ret_chr);
			elseif escape_c == 0x78 then
				local radix0, radix1;
				i += 1;
				if codes[i] == 0x7B then
					i += 1;
					local org_i = i;
					while codes[i] and
						(codes[i] >= 0x30 and codes[i] <= 0x39
							or codes[i] >= 0x41 and codes[i] <= 0x46
							or codes[i] >= 0x61 and codes[i] <= 0x66) do
						i += 1;
					end;
					if codes[i] ~= 0x7D or i == org_i then
						return "malformed hexadecimal code";
					elseif i - org_i > 4 then
						return "character offset too large";
					end;
					table.insert(outln, tonumber(utf8_sub(codes.s, org_i, i), 16));
				else
					if codes[i] and (codes[i] >= 0x30 and codes[i] <= 0x39 or codes[i] >= 0x41 and codes[i] <= 0x46 or codes[i] >= 0x61 and codes[i] <= 0x66) then
						radix0 = codes[i] - ((codes[i] >= 0x41 and codes[i] <= 0x5A) and 0x37 or (codes[i] >= 0x61 and codes[i] <= 0x7A) and 0x57 or 0x30);
						i += 1;
						if codes[i] and (codes[i] >= 0x30 and codes[i] <= 0x39 or codes[i] >= 0x41 and codes[i] <= 0x46 or codes[i] >= 0x61 and codes[i] <= 0x66) then
							radix1 = codes[i] - ((codes[i] >= 0x41 and codes[i] <= 0x5A) and 0x37 or (codes[i] >= 0x61 and codes[i] <= 0x7A) and 0x57 or 0x30);
						else
							i -= 1;
						end;
					else
						i -= 1;
					end;
					table.insert(outln, radix0 and (radix1 and 16 * radix0 + radix1 or radix0) or 0);
				end;
			else
				local esc_char = b_escape_chars[escape_c] or escape_chars[escape_c];
				table.insert(outln, esc_char or escape_c);
			end;
		elseif c == 0x2A or c == 0x2B or c == 0x3F or c == 0x7B then
			-- Quantifier
			local start_q, end_q;
			if c == 0x7B then
				local org_i = i + 1;
				local start_i;
				while codes[i + 1] and (codes[i + 1] >= 0x30 and codes[i + 1] <= 0x39 or codes[i + 1] == 0x2C and not start_i and i + 1 ~= org_i) do
					i += 1;
					if codes[i] == 0x2C then
						start_i = i;
					end;
				end;
				if codes[i + 1] == 0x7D then
					i += 1;
					if not start_i then
						start_q = tonumber(utf8_sub(codes.s, org_i, i));
						end_q = start_q;
					else
						start_q, end_q = tonumber(utf8_sub(codes.s, org_i, start_i)), start_i + 1 == i and math.huge or tonumber(utf8_sub(codes.s, start_i + 1, i));
						if end_q < start_q then
							return "numbers out of order in {} quantifier";
						end;
					end;
				else
					table.move(codes, org_i - 1, i, #outln + 1, outln);
				end;
			else
				start_q, end_q = c == 0x2B and 1 or 0, c == 0x3F and 1 or math.huge;
			end;
			if start_q then
				local quantifier_type = flags.ungreedy and "lazy" or "greedy";
				if codes[i + 1] == 0x2B or codes[i + 1] == 0x3F then
					i += 1;
					quantifier_type = codes[i] == 0x2B and "possessive" or flags.ungreedy and "greedy" or "lazy";
				end;
				local outln_len = #outln;
				local last_outln_value = outln[outln_len];
				if not last_outln_value or type(last_outln_value) == "table" and (last_outln_value[1] == "quantifier" or last_outln_value[1] == 0x28 or b_escape_chars[last_outln_value[1]])
					or last_outln_value == alternation or type(last_outln_value) == "string" then
					return "quantifier doesn't follow a repeatable pattern";
				end;
				if end_q == 0 then
					table.remove(outln);
				elseif start_q ~= 1 or end_q ~= 1 then
					if type(last_outln_value) == "table" and last_outln_value[1] == 0x29 then
						outln_len = last_outln_value[3];
					end;
					outln[outln_len] = { "quantifier", start_q, end_q, quantifier_type, outln[outln_len] };
				end;
			end;
		elseif c == 0x7C then
			-- Alternation
			table.insert(outln, alternation);
			local i1 = #outln;
			repeat
				i1 -= 1;
				local v1, is_table = outln[i1], type(outln[i1]) == "table";
				if is_table and v1[1] == 0x29 then
					i1 = outln[i1][3];
				elseif is_table and v1[1] == 0x28 then
					if v1[4] == 0x7C then
						group_n = v1[5];
					end;
					break;
				end;
			until not v1;
		elseif c == 0x24 or c == 0x5E then
			table.insert(outln, c == 0x5E and beginning_str or end_str);
		elseif flags.ignoreCase and c >= 0x61 and c <= 0x7A then
			table.insert(outln, c - 0x20);
		elseif flags.extended and (c >= 0x09 and c <= 0x0D or c == 0x20 or c == 0x23) then
			if c == 0x23 then
				repeat
					i += 1;
				until not codes[i] or codes[i] == 0x0A or codes[i] == 0x0D;
			end;
		else
			table.insert(outln, c);
		end;
		i += 1;
	end;
	local max_group_n = 0;
	for i, v in ipairs(outln) do
		if type(v) == "table" and (v[1] == 0x28 or v[1] == "quantifier" and type(v[5]) == "table" and v[5][1] == 0x28) then
			if v[1] == "quantifier" then
				v = v[5];
			end;
			if not v[3] then
				return "unterminated parenthetical";
			elseif v[2] then
				max_group_n = math.max(max_group_n, v[2]);
			end;
		elseif type(v) == "table" and (v[1] == "backref" or v[1] == "recurmatch") then
			if not group_id[v[2]] and (type(v[2]) ~= "number" or v[2] > group_n) then
				return "reference to a non-existent or invalid subpattern";
			elseif v[1] == "recurmatch" and v[2] ~= 0 then
				for i1, v1 in ipairs(outln) do
					if type(v1) == "table" and v1[1] == 0x28 and v1[2] == v[2] then
						v[3] = i1;
						break;
					end;
				end;
			elseif type(v[2]) == "string" then
				v[2] = group_id[v[2]];
			end;
		end;
	end;
	outln.group_n = max_group_n;
	return outln, group_id, verb_flags;
end;

if not tonumber(options.cacheSize) then
	error(string.format("expected number for options.cacheSize, got %s", typeof(options.cacheSize)), 2);
end;
local cacheSize = math.floor(options.cacheSize or 0) ~= 0 and tonumber(options.cacheSize);
local cache_pattern, cache_pattern_names;
if not cacheSize then
elseif cacheSize < 0 or cacheSize ~= cacheSize then
	error("cache size cannot be a negative number or a NaN", 2);
elseif cacheSize == math.huge then
	cache_pattern, cache_pattern_names = { nil }, { nil };
elseif cacheSize >= 2 ^ 32 then
	error("cache size too large", 2);
else
	cache_pattern, cache_pattern_names = table.create(options.cacheSize), table.create(options.cacheSize);
end;
if cacheSize then
	function re.pruge()
		table.clear(cache_pattern_names);
		table.clear(cache_pattern);
	end;
end;

local function new_re(str_arr, flags, flag_repr, pattern_repr)
	local tokenized_ptn, group_id, verb_flags;
	local cache_format = cacheSize and string.format("%s|%s", str_arr.s, flag_repr);
	local cached_token = cacheSize and cache_pattern[table.find(cache_pattern_names, cache_format)];
	if cached_token then
		tokenized_ptn, group_id, verb_flags = table.unpack(cached_token, 1, 3);
	else
		tokenized_ptn, group_id, verb_flags = tokenize_ptn(str_arr, flags);
		if type(tokenized_ptn) == "string" then
			error(tokenized_ptn, 2);
		end;
		if cacheSize and tokenized_ptn[1] then
			table.insert(cache_pattern_names, 1, cache_format);
			table.insert(cache_pattern, 1, { tokenized_ptn, group_id, verb_flags });
			if cacheSize ~= math.huge then
				table.remove(cache_pattern_names, cacheSize + 1);
				table.remove(cache_pattern, cacheSize + 1);
			end;
		end;
	end;

	local object = newproxy(true);
	proxy[object] = { name = "RegEx", flags = flags, flag_repr = flag_repr, pattern_repr = pattern_repr, token = tokenized_ptn, group_id = group_id, verb_flags = verb_flags };
	local object_mt = getmetatable(object);
	object_mt.__index = setmetatable(flags, re_m);
	object_mt.__tostring = re_tostr;
	object_mt.__metatable = lockmsg;

	return object;
end;

local function escape_fslash(pre)
	return (#pre % 2 == 0 and '\\' or '') .. pre .. '.';
end;

local function sort_flag_chr(a, b)
	return a:lower() < b:lower();
end;

function re.new(...)
	if select('#', ...) == 0 then
		error("missing argument #1 (string expected)", 2);
	end;
	local ptn, flags_str = ...;
	if type(ptn) == "number" then
		ptn ..= '';
	elseif type(ptn) ~= "string" then
		error(string.format("invalid argument #1 (string expected, got %s)", typeof(ptn)), 2);
	end;
	if type(flags_str) ~= "string" and type(flags_str) ~= "number" and flags_str ~= nil then
		error(string.format("invalid argument #2 (string expected, got %s)", typeof(flags_str)), 2);
	end;

	local flags = {
		anchored = false, caseless = false, multiline = false, dotall = false, unicode = false, ungreedy = false, extended = false,
	};
	local flag_repr = { };
	for f in string.gmatch(flags_str or '', utf8.charpattern) do
		if flags[flag_map[f]] ~= false then
			error("invalid regular expression flag " .. f, 3);
		end;
		flags[flag_map[f]] = true;
		table.insert(flag_repr, f);
	end;
	table.sort(flag_repr, sort_flag_chr);
	flag_repr = table.concat(flag_repr);
	return new_re(to_str_arr(ptn), flags, flag_repr, string.format("/%s/", ptn:gsub("(\\*)/", escape_fslash)));
end;

function re.fromstring(...)
	if select('#', ...) == 0 then
		error("missing argument #1 (string expected)", 2);
	end;
	local ptn = ...;
	if type(ptn) == "number" then
		ptn ..= '';
	elseif type(ptn) ~= "string" then
		error(string.format("invalid argument #1 (string expected, got %s)", typeof(ptn), 2));
	end;
	local str_arr = to_str_arr(ptn);
	local delimiter = str_arr[1];
	if not delimiter then
		error("empty regex", 2);
	elseif delimiter == 0x5C or (delimiter >= 0x30 and delimiter <= 0x39) or (delimiter >= 0x41 and delimiter <= 0x5A) or (delimiter >= 0x61 and delimiter <= 0x7A) then
		error("delimiter must not be alphanumeric or a backslash", 2);
	end;

	local i0 = 1;
	repeat
		i0 = table.find(str_arr, delimiter, i0 + 1);
		if not i0 then
			error(string.format("no ending delimiter ('%s') found", utf8.char(delimiter)), 2);
		end;
		local escape_count = 1;
		while str_arr[i0 - escape_count] == 0x5C do
			escape_count += 1;
		end;
	until escape_count % 2 == 1;

	local flags = {
		anchored = false, caseless = false, multiline = false, dotall = false, unicode = false, ungreedy = false, extended = false,
	};
	local flag_repr = { };
	while str_arr.n > i0 do
		local f = utf8.char(table.remove(str_arr));
		str_arr.n -= 1;
		if flags[flag_map[f]] ~= false then
			error("invalid regular expression flag " .. f, 3);
		end;
		flags[flag_map[f]] = true;
		table.insert(flag_repr, f);
	end;
	table.sort(flag_repr, sort_flag_chr);
	flag_repr = table.concat(flag_repr);
	table.remove(str_arr, 1);
	table.remove(str_arr);
	str_arr.n -= 2;
	str_arr.s = string.sub(str_arr.s, 2, 1 + str_arr.n);
	return new_re(str_arr, flags, flag_repr, string.sub(ptn, 1, 2 + str_arr.n));
end;

local re_escape_line_chrs = {
	['\0'] = '\\x00', ['\n'] = '\\n', ['\t'] = '\\t', ['\r'] = '\\r', ['\f'] = '\\f',
};

function re.escape(...)
	if select('#', ...) == 0 then
		error("missing argument #1 (string expected)", 2);
	end;
	local str, extended, delimiter = ...;
	if type(str) == "number" then
		str ..= '';
	elseif type(str) ~= "string" then
		error(string.format("invalid argument #1 to 'escape' (string expected, got %s)", typeof(str)), 2);
	end;
	if delimiter == nil then
		delimiter = '';
	elseif type(delimiter) == "number" then
		delimiter ..= '';
	elseif type(delimiter) ~= "string" then
		error(string.format("invalid argument #3 to 'escape' (string expected, got %s)", typeof(delimiter)), 2);
	end;
	if utf8.len(delimiter) > 1 or delimiter:match("^[%a\\]$") then
		error("delimiter have not be alphanumeric", 2);
	end;
	return (string.gsub(str, "[\0\f\n\r\t]", re_escape_line_chrs):gsub(string.format("[\\%s#()%%%%*+.?[%%]^{|%s]", extended and '%s' or '', (delimiter:find'^[%%%]]$' and '%' or '') .. delimiter), "\\%1"));
end;

function re.type(...)
	if select('#', ...) == 0 then
		error("missing argument #1", 2);
	end;
	return proxy[...] and proxy[...].name;
end;

for k, f in pairs(re_m) do
	re[k] = f;
end;

re_m = { __index = re_m };

lockmsg = re.fromstring([[/The\s*metatable\s*is\s*(?:locked|inaccessible)(?#Nice try :])/i]]);
getmetatable(lockmsg).__metatable = lockmsg;

local function readonly_table()
	error("Attempt to modify a readonly table", 2);
end;

match_m = {
	__index = match_m,
	__metatable = lockmsg,
	__newindex = readonly_table,
};

re.Match = setmetatable({ }, match_m);

return setmetatable({ }, {
	__index = re,
	__metatable = lockmsg,
	__newindex = readonly_table,
});
