KoboldAI-Client/extern/lualibs/pl/luabalanced.lua

7 lines
4.4 KiB
Lua

-- Penlight 1.11.0-1 | /lua/pl/luabalanced.lua | https://github.com/lunarmodules/Penlight | License: MIT | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (C) 2009-2016 Steve Donovan, David Manura.
-- 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 assert=assert;local b={['(']=')',['{']='}',['[']=']'}local c={}for d,e in pairs(b)do c[e]=d end;local function f(g,h)h=h or 1;local i=h;local j=g:sub(h,h)if j=='"'or j=="'"then h=h+1;while 1 do h=assert(g:find("["..j.."\\]",h),'syntax error')if g:sub(h,h)==j then local k=g:sub(i,h)return k,h+1 else h=h+2 end end else local l=g:match("^%[(=*)%[",h)if l then local m;m,h=g:find("%]"..l.."%]",h)assert(h)local k=g:sub(i,h)return k,h+1 else return nil,h end end end;a.match_string=f;local function n(g,h)h=h or 1;local i=h;local o=g:sub(h,h)if not b[o]then return nil,h end;local p={}while 1 do h=g:find('[%(%{%[%)%}%]\"\']',h)assert(h,'syntax error: unbalanced')local j=g:sub(h,h)if j=='"'or j=="'"then local k;k,h=f(g,h)assert(k)elseif b[j]then local q,r;if j=='['then q,r=g:match('^%[(=*)%[()',h)end;if q then h=g:match('%]'..q..'%]()',r)assert(h,'syntax error: long string not terminated')if#p==0 then local k=g:sub(i,h-1)return k,h end else p[#p+1]=j;h=h+1 end else assert(p[#p]==assert(c[j]),'syntax error: unbalanced')p[#p]=nil;if#p==0 then local k=g:sub(i,h)return k,h+1 end;h=h+1 end end end;a.match_bracketed=n;local function s(g,h)h=h or 1;if g:sub(h,h+1)~='--'then return nil,h end;h=h+2;local t,u=f(g,h)if t then return'--'..t,u end;local k;k,h=g:match('^([^\n]*\n?)()',h)return'--'..k,h end;local v={['and']=true,['or']=true,['not']=true}local w={['>']=true,['<']=true,['~']=true}local function x(g,h)h=h or 1;local m;local i=h;local y;local z,A;while h do local j=g:sub(h,h)if j=='"'or j=="'"or j=='['and g:find('^[=%[]',h+1)then local k;k,h=f(g,h)assert(k,'syntax error')elseif j=='-'and g:sub(h+1,h+1)=='-'then z=h;while g:sub(h,h+1)=='--'do local k;k,h=s(g,h)assert(k)h=g:match('^%s*()',h)A=h end elseif j=='('or j=='{'or j=='['then m,h=n(g,h)elseif j=='='and g:sub(h+1,h+1)=='='then h=h+2 elseif j=='='and w[g:sub(h-1,h-1)]then h=h+1 elseif j:match'^[%)%}%];,=]'then local k=g:sub(i,h-1)return k,h elseif j:match'^[%w_]'then local B,C=g:match('^([%w_]+)()',h)if h~=i and not v[B]then local D=(A==h and z or h)-1;while g:match('^%s',D)do D=D-1 end;local E=g:sub(D,D)if E:match'[%)%}\'\"%]]'or E:match'[%w_]'and not v[y]then local k=g:sub(i,h-1)return k,h end end;y,h=B,C else h=h+1 end;h=g:find('[%(%{%[%)%}%]\"\';,=%w_%-]',h)end;local k=g:sub(i,#g)return k,#g+1 end;a.match_expression=x;local function F(g,h)h=h or 1;local G={}while 1 do local j=#G==0 and'^'or'^%s*,%s*'local H,u=g:match(j..'([%a_][%w_]*)%s*()',h)if H then h=u else break end;G[#G+1]=H end;return G,h end;a.match_namelist=F;local function I(g,h)h=h or 1;local G={}while 1 do if#G~=0 then local u=g:match('^%s*,%s*()',h)if u then h=u else break end end;local H;H,h=x(g,h)assert(H,'syntax error')G[#G+1]=H end;return G,h end;a.match_explist=I;local function J(g,K)local h=1;local i=1;local L=''while 1 do h=g:find('[%-\'\"%[]',h)if not h then break end;if g:match('^%-%-',h)then local M=g:sub(i,h-1)if#M>0 then L=L..(K('e',M)or M)end;local N;N,h=s(g,h)L=L..(K('c',assert(N))or N)i=h else local r=g:find('^[\'\"%[]',h)local O;if r then O,h=f(g,r)end;if O then local M=g:sub(i,r-1)if#M>0 then L=L..(K('e',M)or M)end;L=L..(K('s',O)or O)i=h else h=h+1 end end end;local M=g:sub(i)if#M>0 then L=L..(K('e',M)or M)end;return L end;a.gsub=J;return a