-- lx format
local format = require("core.format")
local vocabulary = require("format.asm.vocabulary_nasm_x86")

format.register({
	name = "Assembly Source (NASM x86)",
	shortname = "ASM NASM x86",
	author = "liqube",
	internal = true,
	comment = ";",

	patterns = {
		-- NASM comments begin with ';' and double slash remains arithmetic operator
		{ pattern = ";.-\n", type = "comment" },

		-- NASM preprocessor directives begin with '%' and stay separate from arithmetic percent
		{ pattern = "%%[%a_][%w_]*", type = "preprocessor" },

		-- Labels accept ordinary, local, NASM special-symbol spelling
		{ pattern = "%s*([%.%a_][%w_%.%?@$#~]*)(:)", type = { "label", "operator" }, line_start = true },

		-- Single and double quotes verbatim; backquotes alone use C-style escapes
		{ pattern = { '"', '"' }, type = "string", delim_type = "operator" },
		{ pattern = { "'", "'" }, type = "string", delim_type = "operator" },
		{ pattern = { "`", "`", "\\" }, type = "string", delim_type = "operator" },

		-- NASM float and integer forms
		{ pattern = "%d[%d_]*%.%d*[eE][%+%-]?%d+", type = "number" },
		{ pattern = "%d[%d_]*%.%d*", type = "number" },
		{ pattern = "0[xXhH][%da-fA-F_]+", type = "number" },
		{ pattern = "0[dDtT][%d_]+", type = "number" },
		{ pattern = "0[qQoO][0-7_]+", type = "number" },
		{ pattern = "0[bByY][01_]+", type = "number" },
		{ pattern = "%d[%da-fA-F_]*[hHxX]", type = "number" },
		{ pattern = "%d[%d_]*[dDtT]", type = "number" },
		{ pattern = "[0-7][0-7_]*[qQoO]", type = "number" },
		{ pattern = "[01][01_]*[bByY]", type = "number" },
		{ pattern = "%d[%d_]*", type = "number" },

		-- Decorators, special values, instructions, registers, assembler words
		{ pattern = "%b{}", type = "keyword2" },
		{ pattern = "%.%.[%a_][%w_]*", type = "constant" },
		{ pattern = "__%?[%a_][%w_]*%?__", type = "constant" },
		{ pattern = "__[%a_][%w_]*__", type = "constant" },
		{ pattern = "%$+", type = "constant" },
		{ pattern = "[%.%a_][%w_%.%?@$#~]*", type = "symbol" },

		-- Address expressions and NASM operators
		{ pattern = "[%+%-=/%*%^%%<>!~|&%?:,%[%]%(%){}]", type = "operator" },
	},

	outline_patterns = {
		-- Named labels provide the NASM outline
		{ pattern = "%s*[%.%a_][%w_%.%?@$#~]*:", line_start = true },
	},

	symbols = vocabulary,
})
