-- lx syntax
local syntax = require("core.syntax")
local patterns = require("core.syntax.patterns")

syntax.add({
	name = "TOML",
	shortname = "TOML",
	author = "liqube",
	extensions = { "toml" },
	comment = "#",

	patterns = {
		-- Comments
		{ pattern = "#.-\n", type = "comment" },
		{ pattern = ";.-\n", type = "comment" },

		-- Table header: [[array.of.tables]]
		{ pattern = "%[%[[^%]\n]+%]%]", type = "function", line_start = true },

		-- Table header: [table]
		{ pattern = "%[[^%]\n]+%]", type = "function", line_start = true },

		-- Keys at start of line (bare, quoted, dotted), without consuming '='
		-- Bare keys: A-Z a-z 0-9 _ - (TOML allows Unicode too, but Lua patterns keep it ASCII-safe)
		{ pattern = "%s*[%w_%-%./]+%s*%f[=]", type = "symbol", line_start = true },
		{ pattern = "%s*%b\"\"%s*%f[=]", type = "symbol", line_start = true },
		{ pattern = "%s*%b''%s*%f[=]", type = "symbol", line_start = true },

		-- Multi-line strings (basic and literal)
		{ pattern = { '"""', '"""', "\\" }, type = "string" },
		{ pattern = { "'''", "'''" }, type = "string" },

		-- Single-line strings (basic and literal)
		{ pattern = { '"', '"', "\\" }, type = "string" },
		{ pattern = { "'", "'" }, type = "string" },

		-- Date and time (value context; must come before generic number patterns)
		-- Scope to right-hand side of key = value
		patterns.rfc3339ish_date_time(nil, "[^=\n]*=%s*"),

		-- Numbers (TOML allows '_' separators)
		{ pattern = "[%+%-]?0x[%da-fA-F_]+", type = "hex" },
		{ pattern = "[%+%-]?0o[0-7_]+", type = "number" },
		{ pattern = "[%+%-]?0b[01_]+", type = "number" },
		{ pattern = "[%+%-]?%d[%d_]*%.%d[%d_]*([eE][%+%-]?%d[%d_]*)?", type = "number" },
		{ pattern = "[%+%-]?%d[%d_]*([eE][%+%-]?%d[%d_]*)", type = "number" },
		{ pattern = "[%+%-]?%d[%d_]*", type = "number" },

		-- Special floats
		{ pattern = "[%+%-]?inf%f[^%w_]", type = "literal" },
		{ pattern = "[%+%-]?nan%f[^%w_]", type = "literal" },

		-- Punctuation and operators
		{ pattern = "=", type = "operator" },
		{ pattern = "[%[%]{}%,%.]", type = "operator" },

		-- Bare words (values like unquoted strings are not allowed in TOML, but these exist)
--		{ pattern = "[%a_][%w_%-]*", type = "symbol" },
	},

	outline_patterns = {
		{ pattern = "^%s*%[%[[^%]\n]+%]%]", type = "function" },
		{ pattern = "^%s*%[[^%]\n]+%]", type = "function" },
	},

	symbols = {
		-- Booleans
		["true"] = "literal",
		["false"] = "literal",

		-- Common value-like tokens (TOML v1.0)
		["inf"] = "literal",
		["+inf"] = "literal",
		["-inf"] = "literal",
		["nan"] = "literal",
		["+nan"] = "literal",
		["-nan"] = "literal",
	},
})
