All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.asahi.internal.namespacing.prefix.lang.Script.kt Maven / Gradle / Ivy

The newest version!
package com.skillw.asahi.internal.namespacing.prefix.lang

import com.skillw.asahi.api.annotation.AsahiPrefix
import com.skillw.asahi.api.prefixParser
import com.skillw.asahi.api.quest
import com.skillw.asahi.api.quester
import com.skillw.asahi.api.script.linking.NativeFunction
import com.skillw.pouvoir.Pouvoir
import com.skillw.pouvoir.util.toArgs
import taboolib.common.platform.ProxyCommandSender


private val funcRegex = Regex(".*?([a-zA-Z_$]+).*?\\((.*?)\\).*?")

@AsahiPrefix(["fun", "def"], "lang")
private fun `fun`() = prefixParser {
    val result = funcRegex.find(splitBeforeString("{"))!!
    val name = result.groups[1]?.value!!
    val params = result.groups[2]?.value!!.toArgs().filter { it.isNotEmpty() && it.isNotBlank() }.toTypedArray()
    val content = parseScript()
    result {
        NativeFunction.create(name, params, content).also { context().addInvoker(name, it) }
    }
}

@AsahiPrefix(["invoke", "call"], "lang")
private fun invoke() = prefixParser {
    val func = quest()
    val paramsGetter = if (peek() == "[") quest>() else quester { emptyArray() }
    result {
        val params = paramsGetter.get()
        invoke(func.get(), *params)
    }
}

/**
 *
 * invokeScript {
 *  path = "xxx.js"
 *  function = main
 * } [ args1 args2 ]
 *
 *
 *
 */
@AsahiPrefix(["invokeScript"], "lang")
private fun invokeJs() = prefixParser {
    val map = if (peek() == "[" || peek() == "{") questTypeMap() else quester { emptyMap() }
    val path = map.quester { it["path"] as String }
    val function = map.quester { it["function"] as String }
    val parameters = questList()
    result {
        Pouvoir.scriptManager.invoke(path.get(), function.get() , emptyMap(), null, *parameters.get().toTypedArray())
    }
}


@AsahiPrefix(["import"], "lang")
private fun import() = prefixParser {
    val paths = if (peek() == "[") quest() else quest().quester { arrayOf(it) }
    result {
        import(*paths.get())
    }
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy