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
|