Skip to content

Commit

Permalink
Rewrite argument consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jun 28, 2024
1 parent 7e5690b commit 5d88181
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ open class Argument<T : Any>(

open fun validate(stringValue: String?) = true

fun getDefaultSuggestions(): List<String>? {
open fun getDefaultSuggestions(): List<String>? {
val context = SuggestionInvocation(Optional.empty(), "", emptyList())
return if (suggests.isPresent) {
suggests.get().getSuggestion(context)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>
Expand Down Expand Up @@ -43,50 +38,25 @@ 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<String>()

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() {
val ping by flag()
val option by optionArgument<String>()
val t by optionArgument<Int>()

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))
}
Original file line number Diff line number Diff line change
@@ -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<String>()

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 }
}
}
}

0 comments on commit 5d88181

Please sign in to comment.