User:Thundercraft5/global.js/LuaMode.js

/* jshint esversion: 6, forin: true, immed: true, indent: 4, latedef: true, newcap: true, noarg: true, undef: true, undef: true, unused: true, browser: true, jquery: true, onevar: true, eqeqeq: true, multistr: true, maxerr: 999999, -W082 /* global mw, ace */ "use strict";

/*** Custom lua syntax highlighting rules ***/ ace.define('ace/mode/new_lua_highlight_rules', [], function(require, exports, module) {	if (mw.config.get('wgNamespaceNumber') !== 828 || !['edit', 'editredlink', 'submit'].includes(mw.config.get('wgAction'))) return;	var oop = require("../lib/oop");	var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;	var LuaHighlightRules = function {		var keywords = 			"not|not|break|do|else|elseif|end|for|if|in|repeat|"+			 "return|then|until|while|or|and|not";		var builtinConstants = "false|false|true|nil|_G|_VERSION";		var methods = 			"sub|upper|len|gfind|rep|find|match|char|dump|gmatch|"+			"reverse|byte|format|gsub|lower|preload|loadlib|loaded|"+			"loaders|cpath|config|path|seeall|exit|setlocale|date|"+			"getenv|difftime|remove|time|clock|tmpname|rename|execute|"+			"lines|write|close|flush|open|output|type|read|stderr|"+			"stdin|input|stdout|popen|tmpfile|log|max|acos|huge|"+ "ldexp|pi|cos|tanh|pow|deg|tan|cosh|sinh|random|randomseed|"+ "frexp|ceil|floor|rad|abs|sqrt|modf|asin|min|mod|fmod|log10|"+ "atan2|exp|sin|atan|getupvalue|debug|sethook|getmetatable|"+ "gethook|setmetatable|setlocal|traceback|setfenv|getinfo|"+ "setupvalue|getlocal|getregistry|getfenv|setn|insert|getn|"+ "foreachi|maxn|foreach|concat|sort|remove|resume|yield|"+ "status|wrap|create|running"; var stdLibaries = "mw|string|package|os|io|math|debug|table|coroutine|util|mw|frame"; // metamethods var metamethods = "__add|__sub|__mod|__unm|__concat|__lt|__index|__call|__gc|__metatable|"+ "__mul|__div|__pow|__len|__eq|__le|__newindex|__tostring|__mode|__tonumber|__pairs|__ipairs|__add"; var builtin = metamethods // regular functions + "|xpcall|package|tostring|print|os|unpack|require|"+ "getfenv|setmetatable|next|assert|tonumber|io|rawequal|"+ "collectgarbage|getmetatable|module|rawset|math|debug|"+ "pcall|table|newproxy|type|coroutine|_G|select|gcinfo|"+ "pairs|rawget|loadstring|ipairs|_VERSION|dofile|setfenv|"+ "load|error|loadfile|makeClass"; var props = "parent|constructor|static|methods|prototype|__constructorCalled|__isClass|getters|setters"; var deprecatedIn5152 = "setn|getn|foreach|foreachi|gcinfo|log10|maxn"; var custom = "checkType|checkArgs|checkTypeArgs|forEachArgs|assertTrue|assertFalse|customArgError|checkTypeField"; var storage = "constructor|local|function|self|super|constructor"; var keywordMapper = this.createKeywordMapper({			"keyword": "(?:" + keywords + "|not" + ")\\b",			"language.variable" : "(?:" + builtin + "|makeClass" + ")\\b",			"invalid.deprecated": "(?:" + deprecatedIn5152 + ")\\b",			"constant.library": "(?:" + stdLibaries + "|mw" + ")\\b",			"constant.language": "(?:" + builtinConstants + "|true" + ")\\b",			"storage": "(?:" + storage + "|constructor" + ")\\b",		}, "identifier"); var decimalInteger = "(?:(?:[0-9]\\d*)|(?:0))"; var hexInteger = "(?:0[xX][\\dA-Fa-f]+)"; var integer = "(?:" + decimalInteger + "|" + hexInteger + ")"; var fraction = "(?:\\.\\d+)"; var intPart = "(?:\\d+)"; var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))"; var floatNumber = "(?:" + pointFloat + ")"; var exponent = '(?:(?:' + floatNumber + '|' + intPart + ')[eE]\\d+)'; var keywordsAll = [ keywords, builtin, deprecatedIn5152, stdLibaries, builtinConstants, storage ].join('|'); var escapedRe = "\\\\(?:0*[0-9]{1,2}(?!\\d)|0*[0-2][0-9]{2}|.)"; var identifierRe = "[a-zA-Z_][a-zA-Z0-9_]*\\b"; var mediaWiki = { "html": [ "create", ], 			"ustring": [ "maxPatternLength", "maxStringLength", "byte", "byteoffset", "char", "codepoint", "find", "format", "gcodepoint", "gmatch", "gsub", "isutf8", "len", "lower", "match", "rep", "sub", "toNFC", "toNFD", "upper", ], 			"hash": [ "listAlgorithms", "hashValue", ], 			"message": [ "new", "newFallbackSequence", "newRawMessage", "rawParam", "numParam", "getDefaultLanguage", ], 			"site": [ "currentVersion", "scriptPath", "server", "siteName", "stylePath", "namespaces", "contentNamespaces", "subjectNamespaces", "talkNamespaces", "stats", "stats.pagesInCategory", "stats.pagesInNamespace", "stats.usersInGroup", "interwikiMap", ], 			"title": [ "equals", "compare", "getCurrentTitle", "new", "makeTitle", ], 			"text": [ "decode", "encode", "jsonDecode", "jsonEncode", "killMarkers", "listToText", "nowiki", "split", "gsplit", "tag", "trim", "truncate", "unstripNoWiki", "unstrip", ], 			"uri": [ "encode", "decode", "anchorEncode", "buildQueryString", "parseQueryString", "canonicalUrl", "fullUrl", "localUrl", "new", "validate", ], 			"ext": [ "data", "cargo", "cattools", "FlaggedRevs", "TitleBlacklist", "ParserFunctions", "articlePlaceholder", "externalData", "seo", ], 			"language": [ "fetchLanguageName", "fetchLanguageNames", "getContentLanguage", "getFallbacksFor", "isKnownLanguageTag", "isSupportedLanguage", "isValidBuiltInCode", "isValidCode", "new", ],		};		var mediaWikiLibs = Object.keys(mediaWiki).join('|'); function createMwLibObj(libName) { return { token: [ "constant.library", "keyword.operator", "constant.library", "keyword.operator", "function.method" ], regex: '(mw)(\\.)(' + libName + ')(\\.)((?:' + mediaWiki[libName].join('|') + ')\\b)', };		}		function onMatch(value, currentState, stack) { var values = this.splitRegex.exec(value).slice(1); var types = this.token.apply(this, values); stack.unshift(this.next, value.replace(/(.*)(\[=*\[)$/, '$2').length, currentState); if (typeof types === "string") return [{ type: types, value: value, }];			var tokens = []; for (var i = 0, l = types.length; i < l; i++) { if (values[i]) tokens[tokens.length] = { type: types[i], value: values[i] };			}			return tokens; }		this.$rules = { "start": [{ stateName: "bracketedComment", onMatch: function(value, currentState, stack) { stack.unshift(this.next, value.length - 2, currentState); return "comment"; },				regex: /\-\-\[=*\[/, next: [{ onMatch: function(value, currentState, stack) { if (value.length === stack[1]) { stack.shift; stack.shift; this.next = stack.shift; } else { this.next = ""; }						return "comment"; },					regex: /\]=*\]/, next: "start" }, {					defaultToken: "comment" }]			},			{				token: "comment", regex: "\\-\\-.*$" },			{				stateName: "bracketedStringInvocation", onMatch: onMatch, token: function { var methodsUsed = arguments[4].match('^(' + methods + ')$'); return [ arguments[0]?.match('^(' + storage + ')$') // jshint ignore:line ? "type.storage" : methodsUsed ? "identifier" : "function.constructor", "text", "keyword.operator", "text", methodsUsed ? "function.method" : "function.invocation", "text", "string.quasi", ];				},				regex: "(?!" + keywords + ")(?:([a-zA-Z]?\\w*)(\\s*)(\\s*))?([a-zA-Z]?\\w*)(\\s*)(\\[=*\\[)", next: [{ onMatch: function(value, currentState, stack) { if (value.length === stack[1]) { stack.shift; stack.shift; this.next = stack.shift; } else { this.next = ""; }						return "string.quasi"; },					regex: /\]=*\]/, next: "start" }, {					defaultToken: "string.quasi", }],			}, 			{				token: function { return [ "function.invocation", "text", "string" ]; },				regex: "(?!" + keywords + ")([a-zA-Z_]?\\w*)(\\s*)(\'(?=.))", next: "qstring" }, {				token: function { return [ "function.invocation", "text", "string"]; },				regex: "(?!" + keywords + ")([a-zA-Z_]?\\w*)(\\s*)(\"(?=.))",				next: "qqstring"			}, {				// function Sound.prototype:play(v) end				token: function {					return [						"storage.type", "text", "entity.constructor",						"keyword.operator", "function.support", "keyword.operator",						"entity.name.indentifier", "text", "paren.lparen",					];				},				regex: "(^\\s*function)(\\s+)(" + identifierRe + ")((?:\\.|\\:))(prototype)((?:\\:|\\.))(" + identifierRe + ")(\\s*)(\\",				next: "function_arguments",			}, {				// Sound.prototype.play = 				token: function {					return [						"type.constructor", "punctuation.operator", "support.function",						"punctuation.operator", "entity.name.function", "text", "keyword.operator",					];				},				regex: "(" + identifierRe + ")(\\.)(prototype)(\\.)(" + identifierRe +")(\\s*)(=)",			}, {				// Sound.play = function end				token: function { return [ "type.constructor", "punctuation.operator", "entity.name.function_declaration", "text", "keyword.operator", "text", "storage.type", "text", "paren.lparen" ];				},				regex: "(" + identifierRe + ")(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\",				next: "function_arguments"			}, {				// Sound = function end				token: [					"entity.name.function_declaration", "text", "keyword.operator", "text", "storage.type", "text", "paren.lparen",				],				regex: "(?!" + metamethods + ")(" + identifierRe + ")(\\s*)(=)(\\s*)(function)(\\s*)(\\", next: "function_arguments", }, {				// local Sound = function end token: [ "storage.type", "text", "entity.name.function_declaration", "text", "keyword.operator", "text", "storage.type", "text", "paren.lparen", ],				regex: "(local)(\\s+)(" + identifierRe + ")(\\s*)(=)(\\s*)(function)(\\s*)(\\",				next: "function_arguments",			}, {				// function p.test(v) end / function p:test(v) end				token: function {					return [						"text", "storage.type", "text", "function.constructor", "punctation.operator",						"entity.name.function_declaration", "text", "paren.lparen",					];				},				regex: "(^\\s*)(function)(\\s+)(" + identifierRe + ")((?:\\.|\\:))(" + identifierRe + ")(\\s*)(\\", next: "function_arguments", }, {				// function test(v) end token: [ "text", "storage.type", "text", ],				regex: "(^\\s*)(function)(\\s+)", next: "function_name", }, {				// test = function(v) end token: [ "storage.type", "text", "paren.lparen", ],				regex: "(function)(\\s*)(\\", 				next: "function_arguments",			}, {				// local function test(v) end				token: [					"storage.type", "text", "storage.type", "text",				],				regex: "(local)(\\s+)(function)(\\s+)", 				next: "function_name",			}, {				// table.concat				token: [ "punctation.operator", "method.function" ],				regex: "([\\.:])((?:" + methods + ")\\b)",			}, {				// string:gsub('')				token: function {					return [						"identifier",						"text",						"keyword.operator",						"text", 						"function.method",						"text",						"paren.lparen",					];				},				regex: "(" + identifierRe + ")(\\s*)(\\s*)((?:" + methods +"))(\\s*)([\\(\\{])", }, {				// for i, v in ipairs(t) do ... end token: [ "keyword", "text" ], regex: "(for)(\\s+)(?!\\w+\\s*=\\s*)", next: "for_in_declaration", }, {				// for i = 1, 2, 3 do ... end token: [ "keyword", "text", "variable.parameter" ], regex: "(for)(\\s+)(\\w+)", }, {				// local x = 0 / local x / local x, y, z				token: [ "storage", "text" ], regex: "(local)(\\s+)", next: "local_declaration", }, /*{				token: [ "keyword.operator", "support.constant" ], regex: "(\\.)((?:" + props + "))", },*/ {				token: [ "constant.library", "keyword.operator", "constant.library" ], regex: "(mw)(\\.)(" + mediaWikiLibs + ")", }, ...Object.keys(mediaWiki).map(createMwLibObj), { token: "constant.numeric", regex: exponent, }, {				token: "constant.numeric", // float regex: floatNumber }, {				token: "constant.numeric", // integer regex: integer + "\\b" }, {				token: "punctation.operator", regex: "[:,;.]", }, {				token: keywordMapper, regex: "(?:" + keywordsAll + ")\\b", }, {				// class:method'' token: function { var methodsUsed = arguments[4].match('^(' + methods + ')$'); if (arguments[arguments.length-1] === '\"') this.next = 'qqstring';					else if (arguments[arguments.length-1] === '\'') this.next = 'qstring';					return [						arguments[0]?.match('^(' + storage + ')$') // jshint ignore:line							? "type.storage" 						: methodsUsed 							? "identifier" 							: "function.constructor",						"text",						"keyword.operator",						"text",						methodsUsed ? 'function.method' : "function.invocation",						"text",						"string",					];				},				regex: "(" + identifierRe + ")(\\s*)(\\s*)(?!" + methods + ")(" + identifierRe +")(\\s*)([\'\"])", }, {				// class:method(v) token: function { var methodsUsed = arguments[4].match('^(' + methods + ')$'); return [ arguments[0]?.match('^(' + storage + ')$') // jshint ignore:line ? "type.storage" : methodsUsed ? "identifier" : "function.constructor", "text", "keyword.operator", "text", methodsUsed ? 'function.method' : "function.invocation", "text", "paren.lparen", ];				},				regex: "(" + identifierRe + ")(\\s*)(\\s*)(" + identifierRe +")(\\s*)([\\(\\{])",			}, {				// foo				token: function {					return [ "function.invocation", "text", "paren.lparen" ];				},				regex: "(?!(?:" + methods + ")\\b)(" + identifierRe + ")(\\s*)((?:[\\(\\{]))", }, {				token: function { return "identifier"; },				regex: identifierRe, }, {				token: "keyword.operator", regex: "\\+|\\-|\\*|\\/|%|\\#|\\^|~|<|>|<=|=>|==|~=|=|\\:|\\.\\.\\.|\\.\\." }, {				token: "paren.lparen", regex: "[\\[\\(\\{]"			}, {				token: "paren.rparen",				regex: "[\\]\\)\\}]" }],			"function_arguments": [ {					token: "variable.parameter", regex: '(?!frame)' + identifierRe, }, {					token: "punctuation.operator", regex: "[, ]+" }, {					token: "punctuation.operator", regex: "$" }, {					token: "constant.library", regex: "frame\\b", }, {					token: "empty", regex: "", next: "start", }			],			"for_in_declaration": [{ token: "variable.parameter", regex: '(?!in)' + identifierRe, }, {				 token: "punctuation.operator", regex: "[, ]+" }, {				 token: "punctuation.operator", regex: "$", }, {				 token: "keyword", regex: "in", next: "start", }],			"local_declaration": [{ token: "variable.parameter", regex: identifierRe, }, {				 token: "punctuation.operator", regex: "[, ]+" }, {				 token: "punctuation.operator", regex: "$", next: "start", }, {				token: "keyword.operator", regex: "=", next: "start", }],			"function_name": [ {					token: function { return [ "entity.constructor", "text", "keyword.operator", "text", "entity.name.function_declaration", "text", "paren.lparen", ];					},					regex: "(" + identifierRe + ")(\\s*)([:\\.])(\\s*)(" + identifierRe + ")(\\s*)(\\",					next: "function_arguments",				},				{					token: "entity.name.function_declaration",					regex: identifierRe,				}, {					token: "punctuation.operator",					regex: "[,\\.\\: ]+"				}, {					token: "paren.lparen",					regex: "\\(", next: "function_arguments", }			],			"qqstring": [ {					token: "constant.language.escape", regex: escapedRe }, {					token: "string", regex: "\\\\$", next: "qqstring" }, {					token: "string", regex: '"|$',					next: "start"				}, {					defaultToken: "string"				}			],			"qstring": [				{					token: "constant.language.escape",					regex: escapedRe				}, {					token: "string",					regex: "\\\\$",					next: "qstring"				}, {					token: "string",					regex: "'|$",					next: "start"				}, {					defaultToken: "string"				}			],		};		this.normalizeRules;	};	oop.inherits(LuaHighlightRules, TextHighlightRules);	exports.LuaHighlightRules = LuaHighlightRules; });

var luaMode = new (ace.require("ace/mode/lua").Mode); luaMode.HighlightRules = ace.require('ace/mode/new_lua_highlight_rules').LuaHighlightRules; ace.editor.session.setMode(luaMode); /** * Distributed under the BSD license, see https://opensource.org/licenses/BSD-3-Clause for fulltext * Copyright (c) 2010, Ajax.org B.V. * All rights reserved. **/