#include <eepp/ui/doc/languages/typst.hpp>
#include <eepp/ui/doc/syntaxdefinitionmanager.hpp>

namespace EE { namespace UI { namespace Doc { namespace Language {

SyntaxDefinition& addTypst() {

	return SyntaxDefinitionManager::instance()
		->add(

			{ "Typst",
			  { "%.typ$" },
			  {
				  { { "include", "#markup" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::LuaPattern },

			  },
			  {

			  },
			  "",
			  {}

			} )
		.addRepositories( {

			{ "arguments",
			  {
				  { { "\\b[[:alpha:]_][[:alnum:]_-]*(?=:)" },
					"parameter",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "include", "#code" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::LuaPattern },

			  } },
			{ "code",
			  {
				  { { "include", "#common" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::LuaPattern },
				  { { "{", "}" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#code" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "\\[", "\\]" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#markup" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "//", "\n" },
					{ "comment", "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx },
				  { { ":" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "," }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "=>|\\.\\." }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "==|!=|<=|<|>=|>" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "\\+=|-=|\\*=|/=|=" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "\\+|\\*|/|(?<![[:alpha:]_][[:alnum:]_-]*)-(?![:alnum:]_-]*[[:alpha:]_])" },
					"operator",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(and|or|not)\\b" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "\\b(let|as|in|set|show|context)\\b" },
					"keyword",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(if|else)\\b" }, "keyword", "", SyntaxPatternMatchType::RegEx },
				  { { "\\b(for|while|break|continue)\\b" },
					"keyword",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(import|include|export)\\b" },
					"keyword",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(return)\\b" }, "keyword", "", SyntaxPatternMatchType::RegEx },
				  { { "include", "#constants" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::LuaPattern },
				  { { "\\b[[:alpha:]_][[:alnum:]_-]*!?(?=\\[|\\()" },
					"function",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "(?<=\\bshow\\s*)\\b[[:alpha:]_][[:alnum:]_-]*(?=\\s*[:.])" },
					"function",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "(?<=\\b[[:alpha:]_][[:alnum:]_-]*!?)\\(", "\\)" },
					{ "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#arguments" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "\\b[[:alpha:]_][[:alnum:]_-]*\\b" },
					"normal",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\(", "\\)|(?=;)" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#code" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },

			  } },
			{ "constants",
			  {
				  { { "\\bnone\\b" }, "literal", "", SyntaxPatternMatchType::RegEx },
				  { { "\\bauto\\b" }, "literal", "", SyntaxPatternMatchType::RegEx },
				  { { "\\b(true|false)\\b" }, "literal", "", SyntaxPatternMatchType::RegEx },
				  { { "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?(mm|pt|cm|in|em)\\b" },
					"number",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?(rad|deg)\\b" },
					"number",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?%" },
					"number",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?fr" },
					"number",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(0x[0-9a-zA-Z]+|(0b|0o)?\\d+)\\b" },
					"number",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\b(\\d*)?\\.?\\d+([eE][+-]?\\d+)?\\b" },
					"number",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\"", "\"", "\\\\([\\\\\"nrt]|u\\{?[0-9a-zA-Z]*\\}?)" },
					{ "string" },
					{},
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\$", "\\$" }, { "string" }, {}, "", SyntaxPatternMatchType::RegEx },

			  } },
			{ "markup",
			  {
				  { { "include", "#common" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::LuaPattern },
				  { { "\\\\([\\\\/\\[\\]{}#*_=~`$-.]|u\\{[0-9a-zA-Z]*\\}?)" },
					"string",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "\\\\" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "~" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "-\\?" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "---" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "--" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "\\.\\.\\." }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { ":([a-zA-Z0-9]+:)+" }, "literal", "", SyntaxPatternMatchType::RegEx },
				  { { "(^\\*|\\*$|((?<=\\W|_)\\*)|(\\*(?=\\W|_)))",
					  "(^\\*|\\*$|((?<=\\W|_)\\*)|(\\*(?=\\W|_)))|(?=\\])" },
					{ "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#markup" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "(^_|_$|((?<=\\W|_)_)|(_(?=\\W|_)))",
					  "(^_|_$|((?<=\\W|_)_)|(_(?=\\W|_)))|\n|(?=\\])" },
					{ "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#markup" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "https?://[0-9a-zA-Z~/%#&=',;\\.\\+\\?]*" },
					"link",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "`{3,}", "\\0" }, { "normal" }, {}, "", SyntaxPatternMatchType::RegEx },
				  { { "`", "`" }, { "normal" }, {}, "", SyntaxPatternMatchType::RegEx },
				  { { "\\$", "\\$" }, { "string" }, {}, "", SyntaxPatternMatchType::RegEx },
				  { { "^\\s*=+\\s+", "\n|(?=<)" },
					{ "keyword", "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#markup" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "^\\s*-\\s+" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "^\\s*([0-9]*\\.|\\+)\\s+" }, "operator", "", SyntaxPatternMatchType::RegEx },
				  { { "^\\s*(/)\\s+([^:]*:)" },
					{ "normal", "operator", "normal" },
					{},
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "<[[:alpha:]_][[:alnum:]_-]*>" },
					"normal",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "(@)[[:alpha:]_][[:alnum:]_-]*" },
					"normal",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "(#)(let|set|show|context)\\b", "\n|(;)|(?=])" },
					{ "keyword", "operator" },
					{ "normal", "operator" },
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#code" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "(#)(as|in)\\b" }, "keyword", "", SyntaxPatternMatchType::RegEx },
				  { { "((#)if|(?<=(}|])\\s*)else)\\b", "\n|(?=])|(?<=}|])" },
					{ "keyword", "normal", "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#code" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "(#)(for|while)\\b", "\n|(?=])|(?<=}|])" },
					{ "keyword", "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#code" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "(#)(break|continue)\\b" }, "keyword", "", SyntaxPatternMatchType::RegEx },
				  { { "(#)(import|include|export)\\b", "\n|(;)|(?=])" },
					{ "keyword", "operator" },
					{ "normal", "operator" },
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#code" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "(#)(return)\\b" }, "keyword", "", SyntaxPatternMatchType::RegEx },
				  { { "((#)[[:alpha:]_][[:alnum:]_-]*!?)(?=\\[|\\()" },
					"function",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "(?<=#[[:alpha:]_][[:alnum:]_-]*!?)\\(", "\\)" },
					{ "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#arguments" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "(#)[[:alpha:]_][.[:alnum:]_-]*" },
					"normal",
					"",
					SyntaxPatternMatchType::RegEx },
				  { { "#", "\\s" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#code" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },

			  } },
			{ "comments",
			  {
				  { { "/\\*", "\\*/" },
					{ "comment" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#comments" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },
				  { { "(?<!:)//", "\n" },
					{ "comment", "operator" },
					{},
					"",
					SyntaxPatternMatchType::RegEx,
					{
						{ { "include", "#comments" },
						  { "normal" },
						  {},
						  "",
						  SyntaxPatternMatchType::LuaPattern },

					} },

			  } },
			{ "common",
			  {
				  { { "include", "#comments" },
					{ "normal" },
					{},
					"",
					SyntaxPatternMatchType::LuaPattern },

			  } },
		} ).setBlockComment( { "/*", "*/" } );
}

}}}} // namespace EE::UI::Doc::Language
