com.skillw.asahi.api.member.parser.infix.namespacing.BaseInfix.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Pouvoir Show documentation
Show all versions of Pouvoir Show documentation
Bukkit Script Engine Plugin.
package com.skillw.asahi.api.member.parser.infix.namespacing
import com.skillw.asahi.api.AsahiManager
import com.skillw.asahi.api.member.AsahiRegistrable
import com.skillw.asahi.api.member.context.InfixContext
import com.skillw.asahi.api.member.namespace.Namespacing
/**
* @className BaseInfix
*
* @author Glom
* @date 2022年12月27日 Copyright 2022 user.
*/
abstract class BaseInfix(override val key: Class, override val namespace: String = "common") :
AsahiRegistrable>, Namespacing {
constructor(type: Class, vararg pairs: Pair>) : this(type) {
infixMap.putAll(pairs)
}
/** 中缀Token 中缀解释器执行内容 */
internal val infixMap = HashMap>()
/**
* 执行中缀解释器
*
* @param obj 对象
* @return 结果
*/// 动作上下文 执行动作
fun InfixContext.infix(obj: T): Any? {
return infixMap[token]?.run { execute(obj) }
}
/**
* 添加中缀解释器执行内容
*
* @param executor 执行内容
* @receiver
*/
infix fun String.to(executor: InfixContext.(T) -> Any?) {
infix(this, executor = executor)
}
/**
* 添加中缀解释器执行内容
*
* @param executor 执行内容
* @receiver
*/
infix fun Collection.to(executor: InfixContext.(T) -> Any?) {
infix(*toTypedArray(), executor = executor)
}
/**
* 添加中缀解释器执行内容
*
* @param executor 执行内容
* @receiver
*/
infix fun Array.to(executor: InfixContext.(T) -> Any?) {
infix(*this, executor = executor)
}
/**
* 添加中缀解释器执行内容
*
* @param keys 中缀token
* @param executor 执行内容
* @return
* @receiver
*/
fun infix(vararg keys: String, executor: InfixContext.(T) -> Any?): BaseInfix {
keys.forEach { key ->
infixMap[key] = InfixExecutor { executor(it) }
}
return this
}
/**
* 添加中缀解释器执行内容
*
* @param pair 中缀token to 执行内容
*/
infix fun `infix`(pair: Pair Any?>) {
infix(pair.first, executor = pair.second)
}
/**
* 删除中缀解释器执行内容
*
* @param token 中缀token
* @return
*/
fun removeInfix(token: String): BaseInfix {
infixMap.remove(token)
return this
}
override fun register() {
AsahiManager.getNamespace(namespace).registerInfix(this)
}
/**
* 填入其它中缀解释器的执行内容
*
* @param other 其它中缀解释器
*/
fun putAll(other: BaseInfix<*>) {
if (other.key != key && !other.key.isAssignableFrom(key)) return
other.infixMap.forEach { (key, action) ->
infix(key) {
action.run(this, it)
}
}
}
override fun toString(): String {
return "AsahiInfix { $key ${infixMap.keys} }"
}
companion object {
//给Java用的
@JvmStatic
fun createInfix(type: Class, namespace: String = "common"): BaseInfix {
return object : BaseInfix(type, namespace) {}
}
/**
* 创建中缀解释器
*
* @param type Class 类型
* @param namespace String 命名空间
* @param receiver 处理中缀解释器
* @return BaseInfix 中缀解释器
*/
@JvmStatic
fun createInfix(
type: Class,
namespace: String = "common",
receiver: BaseInfix.() -> Unit,
): BaseInfix {
return object : BaseInfix(type, namespace) {}.apply(receiver)
}
/**
* 中缀解释器执行内容
*
* @param type Class 类型
* @param keys Array 中缀token
* @param namespace String 命名空间
* @param executor 执行内容
*/
@JvmStatic
fun infix(
type: Class,
vararg keys: String,
namespace: String = "common",
executor: InfixContext.(T) -> Any?,
) {
AsahiManager.getNamespace(namespace).getInfix(type).infix(*keys) { executor(this, it) }
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy