local A=100000 local function m(m)local Y={}local x=m:find("\094\047")local a=0 while x~=nil do a=a+1 local s=m:find("\047",x+1)local t=m:sub(x+1,s-1)local I=m:find("\n",s+1)local Q=m:sub(s+1,I-1)local Z=I+1 local W=m:find("\n",I+1)local D=m:sub(Z,W-1)local o={}local P=nil while D~=""do local A,Y=D:match("\094\040\091\037\100 \093\037\100\043\041\058 \063\040\046\042\041\036")if D=="\078\111 \109\097\116\099\104"then P.matches=nil elseif A~=nil then assert(P.matches,"\101\114\114\111\114 \112\097\114\115\105\110\103 \114\101\103\101\120 "..(tostring(a)..(" \116\101\115\116\115\058 \'"..(t.."\'"))))table.insert(P.matches,{index=tonumber(A),match=Y})else if P~=nil then table.insert(o,P)end P={input=D:match("\094 \042\040\046\043\041\036"),matches={}}end Z=W+1 W=m:find("\n",Z)D=m:sub(Z,W-1)end if P~=nil then table.insert(o,P)end local q={source=t;flags=Q}table.insert(Y,{source=t;flags=Q,tests=o})if q.source==nil then error("\n\n\101\114\114\111\114 \097\116 \099\111\117\110\116 \061 "..(a.."\n\n"))end x=m:find("\n\047",W)if x then x=x+1 end if a>=A then break end end return Y end return m
-- this limits the total number of regex tests that will be parsed
-- from the pcre2 test file.
local MAX = 100000

local function parseTestFile(file)
	local testCaseList = {}

	local start = file:find("^/")

	local count = 0
	while start ~= nil do
		count = count + 1

		local ending = file:find("/", start + 1)
		local regexSource = file:sub(start + 1, ending - 1)
		local endOfRegexSourceLine = file:find("\n", ending + 1)
		local flags = file:sub(ending + 1, endOfRegexSourceLine - 1)

		local nextLineStart = endOfRegexSourceLine + 1
		local nextLineEnd = file:find("\n", endOfRegexSourceLine + 1)
		local line = file:sub(nextLineStart, nextLineEnd - 1)

		local regexTests = {}
		local currentTest = nil

		while line ~= "" do
			local matchIndex, matchValue = line:match("^([%d ]%d+): ?(.*)$")
			if line == "No match" then
				currentTest.matches = nil
			elseif matchIndex ~= nil then
				assert(
					currentTest.matches,
					"error parsing regex " .. tostring(count) .. " tests: '" .. regexSource .. "'"
				)
				table.insert(currentTest.matches, {
					index = tonumber(matchIndex),
					match = matchValue,
				})
			else
				if currentTest ~= nil then
					table.insert(regexTests, currentTest)
				end
				currentTest = {
					input = line:match("^ *(.+)$"),
					matches = {},
				}
			end
			nextLineStart = nextLineEnd + 1
			nextLineEnd = file:find("\n", nextLineStart)
			line = file:sub(nextLineStart, nextLineEnd - 1)
		end

		if currentTest ~= nil then
			table.insert(regexTests, currentTest)
		end

		local regexInfo = {
			source = regexSource,
			flags = flags,
		}

		table.insert(testCaseList, {
			source = regexSource,
			flags = flags,
			tests = regexTests,
		})

		if regexInfo.source == nil then
			error("\n\nerror at count = " .. count .. "\n\n")
		end

		start = file:find("\n/", nextLineEnd)
		if start then
			start = start + 1
		end

		if count >= MAX then
			break
		end
	end

	return testCaseList
end

return parseTestFile
