UNPKG

asajs

Version:

Make your Minecraft JsonUI with ScriptingAPI

459 lines (458 loc) 15.7 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var BindingCompiler_exports = {}; __export(BindingCompiler_exports, { BindingCompiler: () => BindingCompiler }); module.exports = __toCommonJS(BindingCompiler_exports); var import_Random = require("../components/Random"); var import_BindingFunctions = require("./BindingFunctions"); var import_Log = require("./generator/Log"); var import_CurrentLine = require("./reader/CurrentLine"); class BindingCompiler { static compile(propertyName, arg) { return this.build(this.getCompilePart(propertyName), arg).replace(/__BREAK_LINE__/g, "\n"); } static getCompilePart(propertyName) { return Array.isArray(propertyName) ? propertyName[0] : propertyName.slice(1, propertyName.length - 1); } static build(propertyName, arg) { const tokens = this.lexer(propertyName, arg); let build = "("; for (let index = 0; index < tokens.length; index++) { let token = tokens[index]; if (this.isScientificNotation(token)) { const [left, right] = token.split("e"); token = "" + Number(left) * Math.pow(10, Number(right)); } if (token === "!") build += "not "; else { if (this.isNegativeNumber(token) && !this.isOperator(tokens[index - 1]) && tokens[index - 1] !== void 0) { build += "- ".concat(token.slice(1), " "); } else build += "".concat(token, " "); } } build = "".concat(build.slice(0, build.length - 1), ")"); return build; } static buildNewPropertyBag(token, arg) { const bindingName = import_Random.Random.bindingName(); arg.setProperties({ property_bag: { [bindingName]: BindingCompiler.build(token, arg) } }); return bindingName; } static checkAndBuild(token, arg) { return this.isHasBinding(token) ? this.build(token, arg) : this.buildNewPropertyBag(token, arg); } static compileSpecialOperator(tokens, arg) { let firstTokens = []; if (tokens.includes("?")) { const startIndex = tokens.indexOf("?"); firstTokens.push(...tokens.slice(0, startIndex)); let elseCount = 0; const secondTokens = []; const thirdTokens = []; let questionCount = 1; let endIndex = -1; for (let index = startIndex + 1; index < tokens.length; index++) { const token = tokens[index]; if (token === "?") questionCount++; else if (token === ":" && --questionCount == 0) { endIndex = index; elseCount++; break; } secondTokens.push(token); } thirdTokens.push(...tokens.slice(endIndex + 1)); const generateBindingName = import_Random.Random.bindingName(); const firstBinding = this.checkAndBuild(firstTokens.join(""), arg); const secondBinding = "".concat(generateBindingName, "true"); const thirdBinding = "".concat(generateBindingName, "false"); arg.addBindings([ { source_property_name: this.checkAndBuild(secondTokens.join(""), arg), target_property_name: secondBinding }, { source_property_name: ["'".concat(generateBindingName, "{").concat(firstBinding, "}'")], target_property_name: generateBindingName } ]); if (elseCount !== 0) { arg.addBindings({ source_property_name: this.checkAndBuild(thirdTokens.join(""), arg), target_property_name: thirdBinding }); } return [generateBindingName]; } else if (tokens.includes("==")) { const preBuild = []; let strToken = []; for (const token of tokens) { if (token === "==") { const binding = this.buildNewBinding(strToken.join(""), arg); strToken = []; preBuild.push(...[binding, "="]); } else { strToken.push(token); } } preBuild.push(this.buildNewBinding(strToken.join(""), arg)); strToken = []; const build = []; for (let i = 0; i < preBuild.length; i++) { if (preBuild[i] === "=") { if (build.length > 0) build.push("&"); const [preToken, token, nextToken] = [ preBuild[i - 1], preBuild[i], preBuild[i + 1] ]; build.push(this.buildNewBinding("".concat(preToken, " ").concat(token, " ").concat(nextToken), arg)); } } return build; } else { for (let index = 0; index < tokens.length; index++) { const token = tokens[index]; if (["&&", "||", "&", "|"].includes(token)) { const secondTokens = tokens.slice(index + 1); const firstBinding = firstTokens.length === 1 ? firstTokens[0] : this.buildNewBinding(firstTokens.join(" "), arg); const secondBinding = secondTokens.length === 1 ? secondTokens[0] : this.buildNewBinding(tokens.slice(index + 1).join(" "), arg); return [ firstBinding, ["&&", "&"].includes(token) ? "and" : "or", secondBinding ]; } else firstTokens.push(token); } firstTokens = firstTokens.slice(firstTokens.length); for (let index = 0; index < tokens.length; index++) { const token = tokens[index]; if (["%", ">=", "<=", "!="].includes(token)) { const secondTokens = tokens.slice(index + 1); const firstBinding = firstTokens.length === 1 ? firstTokens[0] : this.buildNewBinding(firstTokens.join(""), arg); const secondBinding = secondTokens.length === 1 ? secondTokens[0] : this.buildNewBinding(tokens.slice(index + 1).join(""), arg); switch (token) { case ">=": { return [ "(", firstBinding, ">", secondBinding, ")", "or", "(", firstBinding, "=", secondBinding, ")" ]; } case "<=": { return [ "(", firstBinding, "<", secondBinding, ")", "or", "(", firstBinding, "=", secondBinding, ")" ]; } case "!=": { return [ "not", this.buildNewBinding("".concat(firstBinding, " = ").concat(secondBinding), arg) ]; } case "%": { return [ firstBinding, "-", firstBinding, "/", secondBinding, "*", secondBinding ]; } } } else firstTokens.push(token); } } return firstTokens; } static lexer(propertyName, arg) { const getTokens = this.compileSpecialOperator( this.readTokens(this.getTokens(this.splitString(propertyName))).map((token) => { if (this.isCodeBlock(token)) return this.buildNewBinding(token.slice(1, token.length - 1), arg); else if (this.isFunction(token)) { return this.functionHandler(token, arg); } else if (this.isString(token)) { return this.stringHandler(token, arg); } else return token; }), arg ); return getTokens; } static splitString(propertyName) { const tokens = []; let token = "", openFormatCount = 0, isString = false; for (const char of propertyName) { if (char === "'" && openFormatCount === 0) { isString = !isString; if (token !== "" || !isString) tokens.push(isString ? token : "'".concat(token, "'")); token = ""; } else if (char === "\n" && isString) { token += "__BREAK_LINE__"; } else if (char === "{" && isString) { openFormatCount++; token += "{"; } else if (char === "}" && isString) { openFormatCount--; token += "}"; } else if (char !== " " || isString) { token += char; } } if (token !== "") tokens.push(token); return tokens; } static stringHandler(token, arg) { const tokens = this.getStringTokens(token.slice(1, token.length - 1)).map((token2) => { if (this.isStringCode(token2)) return BindingCompiler.build(token2.slice(1, token2.length - 1), arg); else return "'".concat(token2, "'"); }).filter((token2) => !["'{}'", ")"].includes(token2)); return tokens.length > 1 ? "(".concat(tokens.join(" + "), ")") : tokens[0] || "''"; } static getStringTokens(token) { const tokens = []; let bracketsCount = 0, strToken = ""; for (const char of token) { if (char === "{") { if (bracketsCount++ === 0 && strToken !== "") { tokens.push(strToken); strToken = ""; } strToken += char; } else if (char === "}") { strToken += char; if (--bracketsCount === 0 && strToken !== "") { tokens.push(strToken); strToken = ""; } } else strToken += char; } if (strToken !== "") tokens.push(strToken); return tokens; } static functionHandler(token, arg) { const func = this.readFunctionFromToken(this.getTokens(this.splitString(token)), arg); let str = ""; if (import_BindingFunctions.funcObj[func.name]) { str = import_BindingFunctions.funcObj[func.name]( arg, func.params.map((token2) => { token2 = token2.replace(/__BREAK_LINE__/g, "\n"); return this.isStringPattern(token2) ? this.build("(".concat(token2, ")"), arg) : token2; }) ); } else { import_Log.Log.error("".concat((0, import_CurrentLine.CurrentLine)(), " binding function '").concat(func.name, "' does not exist!")); str = "'[Compile Error]: function >>".concat(func.name, "<< not found!'"); } return str; } static readFunctionFromToken(tokens, arg) { const name = tokens[0]; tokens = tokens.slice(2, tokens.length - 1); const params = []; let param = "", $ = 0; for (const token of tokens) { if (token === "," && !$) { params.push(param); param = ""; } else if (token === "(") { $++; param += token; } else if (token === ")") { $--; param += token; } else param += token; } if (param !== "") params.push(param); return { name, params: params.map((token) => { if (this.getTokens(this.splitString(token)).length > 1) return this.build(this.isCodeBlock(token) ? token : "(".concat(token, ")"), arg); else return token; }) }; } static readTokens(strTokens) { const tokens = []; let strToken = ""; let functionName = null, brackets = 0; for (const token of strTokens) { if (this.maybeFunctionName(token) && !brackets) { if (functionName) tokens.push(functionName); functionName = token; } else if (["(", ")"].includes(token)) { if (functionName) { strToken += functionName; functionName = null; } if (token === "(") brackets++; else brackets--; strToken += token; if (brackets === 0) { tokens.push(strToken); strToken = ""; } } else { if (brackets) strToken += token; else if (functionName) { tokens.push(functionName); functionName = null; tokens.push(token); } else tokens.push(token); } } if (functionName) tokens.push(functionName); return tokens; } static getTokens(strTokens) { var _a; const tokens = []; for (const token of strTokens) { if (this.isString(token)) tokens.push(token); else tokens.push( ...(_a = token.match( /-?((\d+\.)?\d+)(e-?\d+)?|[#$]?\w+|[><=!]?=|(&&|\|\|)|[\[\]()+\-*\/=><!,&%|?:]/gm )) != null ? _a : [] ); } return tokens; } static buildNewBinding(token, arg) { const rndName = import_Random.Random.bindingName(); if (this.isHasBinding(token)) { if (this.isBindingOrVariable(token)) return token; arg.addBindings({ source_property_name: [token], target_property_name: rndName }); } else { arg.setProperties({ property_bag: { [rndName]: this.compile(token, arg) } }); } return rndName; } static findSourceBindings(token, sourceControlsName, lastResourceBindings) { const reSourceBindings = lastResourceBindings || {}; const newTokens = []; this.getTokens(this.splitString(token)).forEach((token2) => { var _a; if (this.isBindingOrVariable(token2)) { newTokens.push( reSourceBindings[_a = "".concat(token2, ":").concat(sourceControlsName)] || (reSourceBindings[_a] = import_Random.Random.bindingName()) ); } else if (this.isString(token2)) { newTokens.push( this.getStringTokens(token2).map((token3) => { if (this.isStringCode(token3)) { const sourceBindings = this.findSourceBindings( token3.slice(1, token3.length - 1), sourceControlsName, reSourceBindings ); for (const key in sourceBindings.reSourceBindings) reSourceBindings[key] = sourceBindings.reSourceBindings[key]; return "{".concat(sourceBindings.newTokens.join(""), "}"); } else return token3; }).join("") ); } else newTokens.push(token2); }); return { reSourceBindings, newTokens }; } static isCanCompile(token) { return Array.isArray(token) || token.startsWith("[") && token.endsWith("]"); } static isString(token) { return /^'.+'$/.test(token) || token === "''"; } static isStringCode(token) { return /^{.+}$/.test(token); } static isStringPattern(token) { return this.isString(token) && /\{.+\}/.test(token); } static isNegativeNumber(token) { return /^-((\d+\.)?\d+)$/.test(token); } static isScientificNotation(token) { return /^-?((\d+\.)?\d+)(e-?\d+)$/.test(token); } static isFunction(token) { return /^\w+\(.*\)$/.test(token); } static isArray(token) { return /^\[.*\]$/.test(token); } static isCodeBlock(token) { return /^\(.+\)$/.test(token); } static maybeFunctionName(token) { return /^\w+$/.test(token); } static isBindingOrVariable(token) { return /^[#$]\w+$/.test(token); } static isOperator(token) { return /^[+\-*\/%><=!&|]$|^[><=!]=$/.test(token); } static isHasBinding(token) { return /#\w+/.test(token); } static isNumber(value) { return /^(\d+|\d+\.\d+|-\d+|-\d+\.\d+)$/.test(value); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BindingCompiler });