com.skillw.asahi.api.Extension.kt Maven / Gradle / Ivy
package com.skillw.asahi.api
import com.skillw.asahi.api.member.context.AsahiContext
import com.skillw.asahi.api.member.lexer.AsahiLexer
import com.skillw.asahi.api.member.parser.infix.InfixParser
import com.skillw.asahi.api.member.parser.prefix.TopPrefixParser.Companion.tryLangParse
import com.skillw.asahi.api.member.parser.prefix.namespacing.PrefixCreator
import com.skillw.asahi.api.member.parser.prefix.namespacing.PrefixParser
import com.skillw.asahi.api.member.parser.prefix.type.TypeParser
import com.skillw.asahi.api.member.quest.LazyQuester
import com.skillw.asahi.api.member.quest.Quester
import com.skillw.asahi.api.member.quest.VarBeanQuester
import com.skillw.asahi.api.script.AsahiScriptException
import com.skillw.asahi.util.cast
import com.skillw.asahi.util.castSafely
/**
* @className ReaderExt
*
* @author Glom
* @date 2023/1/13 20:40 Copyright 2024 Glom.
*/
/**
* 安全寻求下一个值
*
* @param R 类型
* @return 结果
*/
@Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
inline fun AsahiLexer.questSafely(): Quester {
val token = next()
var getter =
tryLangParse(token)
?: if (AsahiManager.hasParser(R::class.java)) {
AsahiManager.getParser(R::class.java)?.parseWith(this)
} else quester { token.cast() }
getter ?: error("Cannot quest $token")
getter = if (getter !is VarBeanQuester) InfixParser.get().parseInfix(this, getter) else getter
val index = currentIndex()
return object : Quester {
override fun AsahiContext.execute(): R? {
return runCatching { getter.get().castSafely() }.run {
if (isSuccess) getOrThrow()
else exceptionOrNull().let {
if (it is AsahiScriptException) throw it
else throw AsahiScriptException(info("Error occurred", index), it)
}
}
}
override fun toString(): String {
return getter.toString()
}
}
}
/**
* 直接通过指定类型解释器寻求值 (安全)
*
* @return Quester
* @receiver AsahiLexer
*/
@Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
inline fun AsahiLexer.questTypeSafely(): Quester {
val token = next()
var getter: Quester? = AsahiManager.getParser(R::class.java)?.parseWith(this) as Quester
getter ?: error("Cannot quest $token")
getter = if (getter !is VarBeanQuester) InfixParser.get().parseInfix(this, getter) else getter
val index = currentIndex()
return object : Quester {
override fun AsahiContext.execute(): R? {
return runCatching { getter.get().castSafely() }.run {
if (isSuccess) getOrThrow()
else exceptionOrNull().let {
if (it is AsahiScriptException) throw it
else throw AsahiScriptException(info("Error occurred", index), it)
}
}
}
override fun toString(): String {
return getter.toString()
}
}
}
/**
* 直接通过指定类型解释器寻求值 (强制)
*
* @param R 类型
* @return 结果
*/
@Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
inline fun AsahiLexer.questType(): Quester {
return questTypeSafely() as Quester
}
/**
* 强制寻求下一个值
*
* @param R 类型
* @return 结果
*/
@Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
inline fun AsahiLexer.quest(): Quester {
return questSafely() as Quester
}
/**
* 创建对象寻求者
*
* @param quest 执行内容
* @param R 类型
* @return 对象寻求者
* @receiver
*/
fun quester(quest: AsahiContext.() -> R): Quester {
return Quester { quest() }
}
/**
* 将对象寻求者转为其它类型的对象寻求者
*
* @param quest 原对象寻求者
* @param T 原类型
* @param R 返回类型
* @return 返回类型的对象寻求者
* @receiver
*/
fun Quester.quester(quest: AsahiContext.(T) -> R): Quester {
return Quester {
quest(get())
}
}
/**
* 懒人式对象寻求着
*
* @param quest
* @param R
* @return
* @receiver
*/
fun lazyQuester(quest: AsahiContext.() -> R): LazyQuester {
return LazyQuester { quest() }
}
/**
* 创建前缀解释器
*
* @param parseFunc 解释内容
* @param R 返回类型
* @return 前缀解释器
* @receiver
*/
fun prefixParser(
parseFunc: PrefixParser.() -> Quester,
): PrefixCreator {
return object : PrefixCreator {
override fun PrefixParser.parse(): Quester {
return parseFunc()
}
}
}
/**
* 创建类型解释器
*
* @param types 类型
* @param parseFunc 解释内容
* @param R 返回类型
* @return 类型解释器
* @receiver
*/
fun typeParser(
vararg types: Class<*>,
parseFunc: AsahiLexer.() -> Quester,
): TypeParser {
return object : TypeParser(*types) {
override fun AsahiLexer.parse() = parseFunc()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy