From 5d8818103bae002694fa0ceb7496f40105de2634 Mon Sep 17 00:00:00 2001 From: MattMX Date: Fri, 28 Jun 2024 13:50:48 +0100 Subject: [PATCH] Rewrite argument consumer --- .../commands/declarative/arg/Argument.kt | 2 +- .../commands/declarative/arg/argTests.kt | 46 +++------------- .../arg/consumers/ArgumentConsumer.kt | 55 +++++++++++++++++++ 3 files changed, 64 insertions(+), 39 deletions(-) create mode 100644 api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/consumers/ArgumentConsumer.kt diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/Argument.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/Argument.kt index 1bc4759..6063098 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/Argument.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/Argument.kt @@ -110,7 +110,7 @@ open class Argument( open fun validate(stringValue: String?) = true - fun getDefaultSuggestions(): List? { + open fun getDefaultSuggestions(): List? { val context = SuggestionInvocation(Optional.empty(), "", emptyList()) return if (suggests.isPresent) { suggests.get().getSuggestion(context) diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/argTests.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/argTests.kt index d8fd23f..deaea1a 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/argTests.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/argTests.kt @@ -1,12 +1,7 @@ package com.mattmx.ktgui.commands.declarative.arg -import com.google.gson.JsonParser +import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer import com.mattmx.ktgui.commands.declarative.arg.impl.* -import com.mattmx.ktgui.commands.declarative.div -import com.mattmx.ktgui.commands.declarative.invoke -import com.mattmx.ktgui.utils.not -import org.bukkit.entity.Player -import kotlin.math.min class ArgumentProcessor( val args: List @@ -43,31 +38,6 @@ class ArgumentProcessor( return current() } - - fun takeOne(argId: String) { - values[argId] = next() ?: return - } - - fun takeWhile(argId: String, block: String.() -> Boolean) { - var current = next() - val list = arrayListOf() - - if (current != null) { - list.add(current) - } - - while (current != null && block(current)) { - current = next() - if (current != null) { - list.add(current) - } - } - values[argId] = list.joinToString(" ") - } - - fun takeRemaining(argId: String) { - takeWhile(argId) { pointer < args.size } - } } fun main() { @@ -75,18 +45,18 @@ fun main() { val option by optionArgument() val t by optionArgument() - val args = "msg MattMX foo bar --t 5 --ping --option 'hello world'".split(" ") + val args = "msg MattMX foo bar --t 5 --ping --option test".split(" ") val processor = ArgumentProcessor(args) processor.permittedFlags.add(ping) // We should abstract this using the `ArgumentConsumer` interface - processor.takeOne("username") - processor.takeRemaining("msg") + println("username" + ArgumentConsumer.single().consume(processor)) + println("msg" + ArgumentConsumer.remaining().consume(processor)) -// username consumes single() -// msg consumes until { false } -// msg consumes remaining() + println(processor.values) - println(processor) + val regexArgumentConsumer = ArgumentConsumer.until { _, s -> s.matches("\\{.+}".toRegex()) } + val regexProcessor = ArgumentProcessor("msg {hello world}".split(" ")) + println(regexArgumentConsumer.consume(regexProcessor)) } \ No newline at end of file diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/consumers/ArgumentConsumer.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/consumers/ArgumentConsumer.kt new file mode 100644 index 0000000..e84252a --- /dev/null +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/consumers/ArgumentConsumer.kt @@ -0,0 +1,55 @@ +package com.mattmx.ktgui.commands.declarative.arg.consumers + +import com.mattmx.ktgui.commands.declarative.arg.ArgumentProcessor + +fun interface ArgumentConsumer { + fun consume(processor: ArgumentProcessor): String? + + companion object { + fun single() = ArgumentConsumer { processor -> processor.next() } + + infix fun untilFalse(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor -> + var current = processor.next() + val list = arrayListOf() + + if (current != null) { + list.add(current) + } + + while (current != null && predicate(processor, current)) { + current = processor.next() + if (current != null) { + list.add(current) + } + } + list.joinToString(" ") + } + + infix fun until(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor -> + var current = processor.next() + var fullString = current ?: "" + + while (current != null) { + current = processor.next() + + if (current == null) { + return@ArgumentConsumer null + } + + fullString += " $current" + + if (predicate(processor, fullString)) { + return@ArgumentConsumer fullString + } + } + null + } + + fun remaining() = untilFalse { processor, _ -> processor.pointer < processor.args.size } + + infix fun variable(amount: Int): ArgumentConsumer { + var i = amount + return untilFalse { _, _ -> i-- == 0 } + } + } +} \ No newline at end of file