Skip to content

Commit

Permalink
[6.0.10] update kether system & update comment api
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Mar 14, 2023
1 parent c11412e commit bbfdb63
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.tabooproject.reflex.Reflex.Companion.setProperty
import taboolib.common.util.decodeUnicode
import taboolib.common5.Coerce
import taboolib.library.configuration.ConfigurationSection
import taboolib.module.configuration.util.Commented
import taboolib.module.configuration.util.CommentedList

/**
* TabooLib
Expand Down Expand Up @@ -81,6 +83,14 @@ open class ConfigSection(var root: Config, override val name: String = "", overr
value is Collection<*> && value !is List<*> -> set(path, value.toList())
value is ConfigurationSection -> set(path, value.getConfig())
value is Map<*, *> -> set(path, value.toConfig(this))
value is Commented -> {
set(path, value.value)
setComment(path, value.comment)
}
value is CommentedList -> {
set(path, value.value)
setComments(path, value.comment)
}
else -> root.set<Any>(path, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package taboolib.module.configuration.util

/** 创建包含注释的值 */
infix fun Any.withComment(comment: String): Commented {
return Commented(this, comment)
}

/** 创建包含注释的值 */
infix fun Any.withComment(comment: List<String>): CommentedList {
return CommentedList(this, comment)
}

class Commented(val value: Any?, val comment: String)

class CommentedList(val value: Any?, val comment: List<String>)
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ public int getIndex() {
public int getMark() {
return mark;
}

public String getRemain() {
return new String(content, index, content.length - index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public List<ParsedAction<?>> readActions() {
} catch (Exception e) {
if (e instanceof LocalizedException) {
String source = new String(this.content, reader.getMark(), Math.min(this.content.length, reader.getIndex()) - reader.getMark()).trim();
// 优化 EOF 错误提示
if (batch && ((LocalizedException) e).getError() == LoadError.EOF) {
source = source.substring(0, source.lastIndexOf('}') - 1);
}
throw LoadError.BLOCK_ERROR.create(this.currentBlock, lineOf(this.content, reader.getMark()), source).then((LocalizedException) e);
} else {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public enum LoadError {
UNHANDLED;

public LocalizedException create(Object... args) {
return LocalizedException.of("load-error." + name().toLowerCase().replace("_", "-"), args);
return LocalizedException.of(this, "load-error." + name().toLowerCase().replace("_", "-"), args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@

public class LocalizedException extends RuntimeException {

private final LoadError error;
private final String node;
private final Object[] params;

public LocalizedException(String node, Object[] params) {
public LocalizedException(LoadError error, String node, Object[] params) {
this.error = error;
this.node = node;
this.params = params;
}

public LoadError getError() {
return error;
}

public String getNode() {
return node;
}
Expand All @@ -36,8 +42,12 @@ public LocalizedException then(LocalizedException e) {
return batch(this, e);
}

public static LocalizedException of(LoadError error, String node, Object... params) {
return new LocalizedException(error, node, params);
}

public static LocalizedException of(String node, Object... params) {
return new LocalizedException(node, params);
return new LocalizedException(LoadError.UNKNOWN_ACTION, node, params);
}

public static Supplier<LocalizedException> supply(String node, Object... params) {
Expand All @@ -53,7 +63,7 @@ private static class Concat extends LocalizedException {
private final LocalizedException[] exceptions;

public Concat(LocalizedException... exceptions) {
super(exceptions[0].node, exceptions[0].params);
super(LoadError.UNKNOWN_ACTION, exceptions[0].node, exceptions[0].params);
this.exceptions = exceptions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mojang.datafixers.util.Either;
import com.mojang.datafixers.util.Function3;
import com.mojang.datafixers.util.Function4;
import com.mojang.datafixers.util.Function5;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -129,7 +130,9 @@ public static Instance instance() {
return Instance.INSTANCE;
}

@SuppressWarnings("DuplicatedCode")
public enum Instance implements Applicative<Mu, Instance.Mu> {

INSTANCE;

private static final class Mu implements Applicative.Mu {
Expand Down Expand Up @@ -176,7 +179,8 @@ public <A, B, R> App<Parser.Mu, R> ap2(App<Parser.Mu, BiFunction<A, B, R>> func,
return frame -> af.run(frame).thenCompose(
f1 -> aa.run(frame).thenCompose(
f2 -> ab.run(frame).thenApply(
f3 -> f1.apply(f2, f3))
f3 -> f1.apply(f2, f3)
)
)
);
});
Expand Down Expand Up @@ -232,5 +236,36 @@ public <T1, T2, T3, T4, R> App<Parser.Mu, R> ap4(App<Parser.Mu, Function4<T1, T2
);
});
}

@Override
public <T1, T2, T3, T4, T5, R> App<Parser.Mu, R> ap5(App<Parser.Mu, Function5<T1, T2, T3, T4, T5, R>> func, App<Parser.Mu, T1> t1, App<Parser.Mu, T2> t2, App<Parser.Mu, T3> t3, App<Parser.Mu, T4> t4, App<Parser.Mu, T5> t5) {
Parser<Function5<T1, T2, T3, T4, T5, R>> f = unbox(func);
Parser<T1> fa = unbox(t1);
Parser<T2> fb = unbox(t2);
Parser<T3> fc = unbox(t3);
Parser<T4> fd = unbox(t4);
Parser<T5> fe = unbox(t5);
return new Parser<>(r -> {
Action<Function5<T1, T2, T3, T4, T5, R>> af = f.reader.apply(r);
Action<T1> aa = fa.reader.apply(r);
Action<T2> ab = fb.reader.apply(r);
Action<T3> ac = fc.reader.apply(r);
Action<T4> ad = fd.reader.apply(r);
Action<T5> ae = fe.reader.apply(r);
return frame -> af.run(frame).thenCompose(
f1 -> aa.run(frame).thenCompose(
f2 -> ab.run(frame).thenCompose(
f3 -> ac.run(frame).thenCompose(
f4 -> ad.run(frame).thenCompose(
f5 -> ae.run(frame).thenApply(
f6 -> f1.apply(f2, f3, f4, f5, f6)
)
)
)
)
)
);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ fun <T> CompletableFuture<Any?>.double(then: (Double) -> T): CompletableFuture<T

fun <T> CompletableFuture<Any?>.float(then: (Float) -> T): CompletableFuture<T> {
return thenApply { then(Coerce.toFloat(it)) }
}

fun <T> CompletableFuture<T>.orNull(): T? {
return getNow(null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ object KetherFunction {

fun parse(input: String, options: ScriptOptions = ScriptOptions()): String {
fun process() = parse(input, options.useCache, options.namespace, options.cache, options.sender, options.vars, options.context)
return if (options.exception) process() else runKether { process() } ?: "ERROR"
return if (options.sandbox) runKether(detailError = options.detailError) { process() } ?: "ERROR" else process()
}

@Deprecated(
"use parse(input: String, options: ScriptOptions = ScriptOptions()) instead", ReplaceWith(
"parse(input, ScriptOptions.builder().namespace(namespace).sender(sender).build())",
"taboolib.module.kether.KetherFunction.parse"
)
)
fun parse(
input: List<String>,
cacheScript: Boolean = true,
Expand All @@ -31,6 +37,12 @@ object KetherFunction {
return input.map { parse(it, cacheScript, namespace, cache, sender, vars, context) }
}

@Deprecated(
"use parse(input: String, options: ScriptOptions = ScriptOptions()) instead", ReplaceWith(
"parse(input, ScriptOptions.builder().namespace(namespace).sender(sender).build())",
"taboolib.module.kether.KetherFunction.parse"
)
)
fun parse(
input: String,
cacheScript: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ typealias ScriptFrame = QuestContext.Frame
/**
* 运行 Kether 语句并打印错误
*/
fun <T> runKether(el: T? = null, function: () -> T): T? {
fun <T> runKether(el: T? = null, detailError: Boolean = false, function: () -> T): T? {
try {
return function()
} catch (ex: Exception) {
ex.printKetherErrorMessage()
ex.printKetherErrorMessage(detailError)
}
return el
}
Expand Down Expand Up @@ -96,13 +96,17 @@ fun ScriptFrame.deepVars(): HashMap<String, Any?> {
/**
* 打印 Kether 错误信息
*/
fun Throwable.printKetherErrorMessage() {
if (javaClass.name.endsWith("kether.LocalizedException")) {
fun Throwable.printKetherErrorMessage(detailError: Boolean = false) {
if (localizedMessage == null || detailError) {
printStackTrace()
return
}
if (javaClass.name.endsWith("kether.LocalizedException") || javaClass.name.endsWith("kether.LocalizedException\$Concat")) {
warning("Unexpected exception while parsing kether script:")
localizedMessage.split('\n').forEach { warning(it) }
} else {
printStackTrace()
warning("Unexpected exception while running the kether script.")
}
localizedMessage.split('\n').forEach { warning(it) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ object KetherShell {

fun eval(source: String, options: ScriptOptions = ScriptOptions()): CompletableFuture<Any?> {
fun process() = eval(source, options.useCache, options.namespace, options.cache, options.sender, options.vars, options.context)
return if (options.exception) process() else runKether { process() } ?: CompletableFuture.completedFuture(null)
return if (options.sandbox) runKether(detailError = options.detailError) { process() } ?: CompletableFuture.completedFuture(null) else process()
}

@Deprecated(
"use eval(source: String, options: ScriptOptions = ScriptOptions()) instead", ReplaceWith(
"eval(source, ScriptOptions.builder().namespace(namespace).sender(sender).build())",
"taboolib.module.kether.KetherShell.eval"
)
)
fun eval(
source: List<String>,
cacheScript: Boolean = true,
Expand All @@ -31,6 +37,12 @@ object KetherShell {
return eval(source.joinToString("\n"), cacheScript, namespace, cache, sender, vars, context)
}

@Deprecated(
"use eval(source: String, options: ScriptOptions = ScriptOptions()) instead", ReplaceWith(
"eval(source, ScriptOptions.builder().namespace(namespace).sender(sender).build())",
"taboolib.module.kether.KetherShell.eval"
)
)
fun eval(
source: String,
cacheScript: Boolean = true,
Expand All @@ -55,11 +67,15 @@ object KetherShell {
}.runActions()
}

class VariableMap(val map: Map<String, Any?>) {
/** 临时变量容器 */
class VariableMap(map: Map<String, Any?>) {

val map = map.toMutableMap()

constructor(vararg map: Pair<String, Any?>) : this(map.toMap())
}

/** 脚本缓存容器 */
class Cache {

val scriptMap = ConcurrentHashMap<String, Script>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,94 @@
package taboolib.module.kether

import taboolib.common5.*
import taboolib.library.kether.ArgTypes
import taboolib.library.kether.ParsedAction
import taboolib.library.kether.Parser
import taboolib.library.kether.Parser.Action
import java.util.concurrent.CompletableFuture

object ParserHolder {

/** 取任意类型 */
fun any(): Parser<Any?> {
return Parser.frame { r ->
val action = r.nextParsedAction()
Action { it.run(action) }
}
}

/** 取任意类型列表 */
fun anyList(): Parser<List<Any?>> = any().listOf()

/** 取特定类型 */
inline fun <reified T> type(): Parser<T> = any().map { it as T }

/** 取动作 */
fun action(): Parser<ParsedAction<*>> = Parser.of { it.nextParsedAction() }

/** 取动作列表 */
fun actionList(): Parser<List<ParsedAction<*>>> = Parser.of { it.next(ArgTypes.listOf(ArgTypes.ACTION)) }

/** 取文本(固定的)*/
fun symbol(): Parser<String> = Parser.of { it.nextToken() }

/** 取文本 */
fun text(): Parser<String> = any().map(Coerce::toString).orElse(symbol())

/** 取整数 */
fun int(): Parser<Int> = Parser.of { it.nextInt() }.orElse(any().map { it.cint })

/** 取长整数 */
fun long(): Parser<Long> = Parser.of { it.nextLong() }.orElse(any().map { it.clong })

/** 取双精度浮点数 */
fun double(): Parser<Double> = Parser.of { it.nextDouble() }.orElse(any().map { it.cdouble })

/** 取浮点数 */
fun float(): Parser<Float> = double().map { it.toFloat() }

/** 取布尔值 */
fun bool(): Parser<Boolean> = Parser.of { Coerce.asBoolean(it.nextToken()).get() }.orElse(any().map { it.cbool })

/** 运行并返回结果 */
fun <T> now(action: ScriptFrame.() -> T): Action<T> {
return Action { CompletableFuture.completedFuture(action(it)) }
}

/** 运行并返回回调函数 */
fun <T> future(action: ScriptFrame.() -> CompletableFuture<T>): Action<T> {
return Action { action(it) }
}

/** 二元并列参数 */
fun <A, B> Parser<A>.and(b: Parser<B>): Parser<Pair<A, B>> {
return fold(b) { fa, fb -> Pair(fa, fb) }
}

/** 三元并列参数 */
fun <A, B, C> Parser<A>.and(b: Parser<B>, c: Parser<C>): Parser<Triple<A, B, C>> {
return fold(b, c) { fa, fb, fc -> Triple(fa, fb, fc) }
}

/** 为可选 */
fun <A> Parser<A>.option(): Parser<A?> {
return optional().map { it.orElse(null) }
}

/** 默认值 */
fun <A> Parser<A?>.defaultsTo(value: A): Parser<A> {
return map { it ?: value }
}

/** 子语句 */
fun <A> command(vararg s: String, then: Parser<A>): Parser<A> {
return Parser.frame { r ->
r.expects(*s)
then.reader.apply(r)
}
}

/** 快速构建完成的 [CompletableFuture] */
fun <T> completedFuture(value: T): CompletableFuture<T> {
return CompletableFuture.completedFuture(value)
}
Expand Down
Loading

0 comments on commit bbfdb63

Please sign in to comment.