--- Build strict file-reference rules for PHP source files
-- @module formats.php.references

local php_references = {}

-- Pattern breakdown: frontiers keep PHP inclusion keywords standalone; spaces and one parenthesis are optional
local include_prefix = "%f[%w_%.:>%-]include%f[^%w_]%s*%(?%s*"
local include_once_prefix = "%f[%w_%.:>%-]include_once%f[^%w_]%s*%(?%s*"
local require_prefix = "%f[%w_%.:>%-]require%f[^%w_]%s*%(?%s*"
local require_once_prefix = "%f[%w_%.:>%-]require_once%f[^%w_]%s*%(?%s*"

--- Return quoted path pieces from PHP inclusion statements
-- @return table rules
function php_references.new()
	local rules = {}

	for _, prefix in ipairs({ include_prefix, include_once_prefix, require_prefix, require_once_prefix }) do
		for _, quote in ipairs({ "\"", "'" }) do
			-- Any quoted path piece inside the inclusion statement is worth trying
			rules[#rules + 1] = {
				-- Pattern breakdown: stop at the statement semicolon so later strings cannot become references
				prefix = prefix .. "[^;]-",
				open = quote,
				close = quote,
			}
		end
	end

	return rules
end

return php_references
