mirror of
https://github.com/KoboldAI/KoboldAI-Client.git
synced 2024-12-13 00:56:18 +01:00
7 lines
7.0 KiB
Lua
7 lines
7.0 KiB
Lua
-- Pegex @857e48682791ba55c98a7dd99c5fc7d71c02597d | /pegex.lua | https://github.com/jugglerchris/ta-regex | License: MIT | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
|
|
-- Copyright (c) 2014 Chris Emerson
|
|
-- 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.
|
|
local a={}local b=require('lpeg')local c=b.P;local d=b.R;local e=b.S;local f=b.C;local g=b.V;local h=b.B;local i=b.Carg;local j=b.Cb;local k=b.Cc;local l=b.Cf;local m=b.Cp;local n=b.Cg;local o=b.Ct;local p=b.Cmt;local function q(r)return r-1 end;local s=n(m(),"_start")local t=n(m()/q,"_end")local u={__index={match=function(v,w,x)local y=v._pat:match(w,x)if y==nil then return y end;local z=nil;for A,B in pairs(y)do if A:sub(1,1)=="s"then local C=A:sub(2)local D=y["e"..C]if B and D then if C:match("(%d+)")then C=tonumber(C)end;z=z or{}z[C]={B,D}y[A]=nil;y["e"..C]=nil end end end;y.groups=z;return y end}}local function E()return{{" \t\v\n\r",[0]="set"},[0]="charset"}end;local function F()return{{" \t\v\n\r",[0]="set"},[0]="charset",negate=true}end;local function G()return{[0]="charset",{[0]="range","a","z"},{[0]="range","A","Z"},{[0]="range","0","9"},{[0]="char","_"}}end;local function H()return{[0]="charset",{[0]="range","a","z"},{[0]="range","A","Z"},{[0]="range","0","9"},{[0]="char","_"},negate=true}end;local function I()return{[0]="charset",{[0]="range","0","9"}}end;local function J()return{[0]="charset",{[0]="range","0","9"},negate=true}end;local function K(L)return function()return{[0]="charset",{[0]="char",L}}end end;local function M(L)return function()return{[0]="char",L}end end;local N=e"()\\?*+|.^$"local O=c"."*k({[0]="."})local P=c"\\s"/E;local Q=c"\\S"/F;local R=c"\\w"/G;local S=c"\\W"/H;local T=c"\\d"/I;local U=c"\\D"/J;local V=c"\\t"/K('\t')local W=c"\\n"/K('\n')local X=c"\\r"/K('\r')local Y=c"\\f"/K('\f')local Z=c"\\e"/K('\x1b')local _=c"\\a"/K('\x07')local a0=P+Q+R+S+T+U+V+W+X+Y+Z+_;local a1=e"]-"local a2=(V+W+X+Y+Z+_)/function(L)return L[1]end;local a3=f(c(1)-a1)/function(L)return{[0]="char",L}end;local a4=f(c(1)-a1)*c"-"*f(c(1)-a1)/function(a5,a6)return{[0]="range",a5,a6}end;local a7=c"["*o((n(c"^"*k(true),"negate")+c(0))*(a4+a2+a3)^0)*c"]"/function(r)r[0]="charset"return r end;local a8=f(c(1)-N)/function(L)return{[0]="char",L}end;local a9=c"\\"*f(N)/function(L)return{[0]="char",L}end;local aa=c"\\"*f(d"19")/function(L)return{tonumber(L),[0]="backref"}end;local ab=d("AZ","az","09")+e("_")local ac=1-ab;local ad=c"\\<"*k({[0]="\\<"})local ae=c"\\>"*k({[0]="\\>"})local af=c"{"*f(d"09"^1)*c"}"/function(L)return tonumber(L),tonumber(L)end;local ag=c"{"*f(d"09"^1)*c","*f(d"09"^1)*c"}"/function(ah,ai)return tonumber(ah),tonumber(ai)end;local aj=c"{"*f(d"09"^1)*c",}"/function(L)return tonumber(L),nil end;local ak=af+ag+aj;local al=j("groups")*m()/function(z,am)local an=#z+1;z[an]={am}z.open[#z.open+1]=an end;local ao=j("groups")*m()/function(z,am)local an=z.open[#z.open]z.open[#z.open]=nil;z[an][2]=am;return an end;local ap=c"("*al;local aq=c")"*ao;local ar=c"(?:"local as=c")"local at=c{"pattern",pattern=n(i(1),"groups")*o((c"^"*n(k(1),"anchorstart")+c(0))*g"subpat"*(c"$"*-c(1)*n(k(1),"anchorend")+-c(1)))/function(v)v[0]="pattern"return v end,subpat=g"branch"*(c"|"*g"branch")^0/function(...)return{[0]="alt",...}end,branch=g"concat",concat=k(nil)*g"piece"^0/function(au,...)return{[0]="concat",...}end,piece=g"atom_multi",atom_multi=g"atom_plus"+g"atom_star"+g"atom_query"+g"atom_count"+g"atom",atom_plus=g"atom"*c"+"/function(av)return{[0]="+",av}end,atom_star=g"atom"*c"*"/function(av)return{[0]="*",av}end,atom_query=g"atom"*c"?"/function(av)return{[0]="?",av}end,atom_count=g"atom"*ak/function(av,ah,ai)return{[0]="{}",min=ah,max=ai,av}end,anongroup=ar*g"subpat"*as,group=ap*g"subpat"*aq/function(aw,C)return{[0]="group",aw,C}end,atom=O+ad+ae+a9+a7+g"anongroup"+g"group"+a8+aa+a0}local function ax(ay,v,az)local aA=az;local aB=#v;if aA==nil then aA=v[aB]aB=aB-1 end;for aC=aB,1,-1 do aA=ay(v[aC],aA)end;return aA end;local function aD(ay,v)local y={}for aC=1,#v do y[aC]=ay(v[aC])end;return y end;local function aE(a5,a6)return a5+a6 end;local function aF(aG)local v=aG[0]if v=="char"then assert(#aG==1)return c(aG[1])elseif v=="range"then assert(#aG==2)return d(aG[1]..aG[2])elseif v=="set"then return e(aG[1])else error("Got charset bit: "..tostring(v).."/"..tostring(v and v[0]))end end;local function aH(aI,A,aJ)local v=aI[0]if v=="pattern"then assert(#aI==1)local aK=aH(aI[1],A,aJ)if aI.anchorend then aK=aK*-c(1)end;aK=s*aK*t;if not aI.anchorstart then aK=c{aK+1*g(1)}end;return aK elseif v=="group"then assert(#aI==2)aJ.numGroups=aJ.numGroups+1;local C=tostring(aI[2])local aL=n(m()/q,"e"..C)*A;local aK=aH(aI[1],aL,aJ)aK=n(m(),"s"..C)*aK;return aK elseif v=="alt"then if#aI==1 then return aH(aI[1],A,aJ)else local aM=aD(function(r)return aH(r,A,aJ)end,aI)return ax(aE,aM)end elseif v=="concat"then return ax(function(aN,aO)return aH(aN,aO,aJ)end,aI,A)elseif v=="char"then assert(#aI==1)return c(aI[1])*A elseif v=="charset"then local aP=ax(aE,aD(aF,aI))if aI.negate then aP=1-aP end;return aP*A elseif v=="*"then return c{"A",A=aH(aI[1],g"A",aJ)+A}elseif v=="+"then return aH(aI[1],c{"A",A=aH(aI[1],g"A",aJ)+A},aJ)elseif v=="."then assert(#aI==0)return(c(1)-c"\n")*A elseif v=="?"then assert(#aI==1)return aH(aI[1],A,aJ)+A elseif v=="{}"then assert(#aI==1)local aw=aI[1]local ah=aI.min;local ai=aI.max;local aQ={[0]="concat"}for aC=1,ah do aQ[#aQ+1]=aw end;if ai==nil then aQ[#aQ+1]={[0]="*",aw}else local aR={[0]="?",aw}for aC=ah+1,ai do aQ[#aQ+1]=aR end end;return aH(aQ,A,aJ)elseif v=="\\<"then assert(#aI==0)return-h(ab)*#ab*A elseif v=="\\>"then assert(#aI==0)return h(ab)*-#ab*A elseif v=="backref"then local C=aI[1]return p(c(0)*j("s"..C)*j("e"..C),function(aS,am,w,aT)local aU=aS:sub(w,aT)local aV=aS:sub(am,am+aT-w)if aU==aV then return am+aT-w+1 else return false end end)else error("Not implemented op: "..tostring(v).."/"..tostring(aI))end end;function a.parse(aW)return at:match(aW,1,{open={}})end;function a.compile(aW)local aI=a.parse(aW)if aI==nil then error("Failed to parse regular expression: {"..aW.."}",2)end;local aJ={numGroups=0}local aX=aH(aI,c(0),aJ)return setmetatable({_pat=o(aX),numgroups=aJ.numGroups},u)end;b.setmaxstack(1000)return a
|