From 315f2258190b33c63df7797a97178019f5aea02b Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 9 Jan 2022 22:33:45 -0600 Subject: [PATCH] add some more stuff from brigadier --- .gitignore | 1 + Cargo.lock | 3 + azalea-brigadier/Cargo.toml | 3 +- .../src/arguments/argument_type.rs | 4 +- .../src/arguments/bool_argument_type.rs | 25 ++- azalea-brigadier/src/arguments/mod.rs | 6 + .../src/builder/argument_builder.rs | 106 +++++++++++ azalea-brigadier/src/builder/mod.rs | 3 + azalea-brigadier/src/command.rs | 10 + azalea-brigadier/src/command_dispatcher.rs | 23 +++ .../src/context/command_context.rs | 87 +++++++++ .../src/context/command_context_builder.rs | 180 ++++++++++++++++++ .../src/context/parsed_argument.rs | 24 +++ .../src/context/parsed_command_node.rs | 22 +++ azalea-brigadier/src/context/string_range.rs | 45 +++++ .../src/context/suggestion_context.rs | 6 + azalea-brigadier/src/lib.rs | 3 + azalea-brigadier/src/redirect_modifier.rs | 8 + .../src/single_redirect_modifier.rs | 8 + .../src/suggestion/suggestion_provider.rs | 14 ++ .../src/tree/argument_command_node.rs | 118 ++++++++++++ azalea-brigadier/src/tree/command_node.rs | 52 +++++ .../src/tree/literal_command_node.rs | 96 ++++++++++ azalea-brigadier/src/tree/mod.rs | 4 + .../src/tree/root_command_node.rs | 61 ++++++ 25 files changed, 902 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf7..f97818c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/doc \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 67259fef..0eeb6520 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,6 +70,9 @@ dependencies = [ [[package]] name = "azalea-brigadier" version = "0.1.0" +dependencies = [ + "lazy_static", +] [[package]] name = "azalea-chat" diff --git a/azalea-brigadier/Cargo.toml b/azalea-brigadier/Cargo.toml index c617ffb1..3694a4b7 100644 --- a/azalea-brigadier/Cargo.toml +++ b/azalea-brigadier/Cargo.toml @@ -1,8 +1,9 @@ [package] +edition = "2021" name = "azalea-brigadier" version = "0.1.0" -edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +lazy_static = "^1.4" diff --git a/azalea-brigadier/src/arguments/argument_type.rs b/azalea-brigadier/src/arguments/argument_type.rs index 4c48d6bb..34d57285 100644 --- a/azalea-brigadier/src/arguments/argument_type.rs +++ b/azalea-brigadier/src/arguments/argument_type.rs @@ -5,7 +5,7 @@ use crate::{ suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, }; -pub trait ArgumentType { +pub trait ArgumentType { // T parse(StringReader reader) throws CommandSyntaxException; // default CompletableFuture listSuggestions(final CommandContext context, final SuggestionsBuilder builder) { @@ -16,7 +16,7 @@ pub trait ArgumentType { // return Collections.emptyList(); // } - fn parse(reader: &mut StringReader) -> Result; + fn parse(reader: &mut StringReader) -> Result; fn list_suggestions( context: &CommandContext, diff --git a/azalea-brigadier/src/arguments/bool_argument_type.rs b/azalea-brigadier/src/arguments/bool_argument_type.rs index d4a33517..f4c03373 100644 --- a/azalea-brigadier/src/arguments/bool_argument_type.rs +++ b/azalea-brigadier/src/arguments/bool_argument_type.rs @@ -1,8 +1,19 @@ -struct BoolArgumentType { - // private static final Collection EXAMPLES = Arrays.asList("true", "false"); - const EXAMPLES: &'static [&'static str] = &["true", "false"]; -} +use crate::context::command_context::CommandContext; -impl ArgumentType for BoolArgumentType { - -} \ No newline at end of file +use super::argument_type::ArgumentType; + +struct BoolArgumentType {} + +impl ArgumentType for BoolArgumentType {} + +impl BoolArgumentType { + const EXAMPLES: &'static [&'static str] = &["true", "false"]; + + fn bool() -> Self { + Self {} + } + + fn get_bool(context: CommandContext, name: String) { + context.get_argument::(name) + } +} diff --git a/azalea-brigadier/src/arguments/mod.rs b/azalea-brigadier/src/arguments/mod.rs index 50b0f09b..487c5db7 100644 --- a/azalea-brigadier/src/arguments/mod.rs +++ b/azalea-brigadier/src/arguments/mod.rs @@ -1 +1,7 @@ pub mod argument_type; +pub mod bool_argument_type; +pub mod double_argument_type; +pub mod float_argument_type; +pub mod integer_argument_type; +pub mod long_argument_type; +pub mod string_argument_type; diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index e69de29b..8a64a9e4 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -0,0 +1,106 @@ +use crate::{ + command::Command, + redirect_modifier::RedirectModifier, + single_redirect_modifier::SingleRedirectModifier, + tree::{command_node::CommandNode, root_command_node::RootCommandNode}, +}; + +pub struct BaseArgumentBuilder +where + T: ArgumentBuilder, +{ + arguments: RootCommandNode, + command: dyn Command, + requirement: dyn Fn(&S) -> bool, + target: Option>, + modifier: Option>, + forks: bool, +} + +pub trait ArgumentBuilder { + fn this() -> T; + fn build(self) -> dyn CommandNode; +} + +impl BaseArgumentBuilder +where + T: ArgumentBuilder, +{ + pub fn then(&mut self, command: dyn CommandNode) -> Result<&mut T, String> { + if self.target.is_some() { + return Err("Cannot add children to a redirected node".to_string()); + } + self.command = command; + Ok(self) + } + + pub fn arguments(&self) -> &Vec> { + &self.arguments.get_children() + } + + pub fn executes(&mut self, command: dyn Command) -> &mut T { + self.command = command; + self + } + + pub fn command(&self) -> dyn Command { + self.command + } + + pub fn requires(&mut self, requirement: dyn Fn(&S) -> bool) -> &mut T { + self.requirement = requirement; + self + } + + pub fn requirement(&self) -> dyn Fn(&S) -> bool { + self.requirement + } + + pub fn redirect(&mut self, target: dyn CommandNode) -> &mut T { + self.forward(target, None, false) + } + + pub fn redirect_modifier( + &mut self, + target: dyn CommandNode, + modifier: dyn SingleRedirectModifier, + ) -> &mut T { + // forward(target, modifier == null ? null : o -> Collections.singleton(modifier.apply(o)), false); + self.forward(target, modifier.map(|m| |o| vec![m.apply(o)]), false) + } + + pub fn fork( + &mut self, + target: dyn CommandNode, + modifier: dyn RedirectModifier, + ) -> &mut T { + self.forward(target, Some(modifier), true) + } + + pub fn forward( + &mut self, + target: dyn CommandNode, + modifier: Option>, + fork: bool, + ) -> Result<&mut T, String> { + if !self.arguments.get_children().is_empty() { + return Err("Cannot forward a node with children".to_string()); + } + self.target = Some(target); + self.modifier = modifier; + self.forks = fork; + Ok(self) + } + + pub fn redirect(&self) -> Option<&dyn CommandNode> { + self.target.as_ref() + } + + pub fn redirect_modifier(&self) -> Option<&dyn RedirectModifier> { + self.modifier.as_ref() + } + + pub fn is_fork(&self) -> bool { + self.forks + } +} diff --git a/azalea-brigadier/src/builder/mod.rs b/azalea-brigadier/src/builder/mod.rs index e69de29b..26f2f644 100644 --- a/azalea-brigadier/src/builder/mod.rs +++ b/azalea-brigadier/src/builder/mod.rs @@ -0,0 +1,3 @@ +pub mod argument_builder; +pub mod literal_argument_builder; +pub mod required_argument_builder; diff --git a/azalea-brigadier/src/command.rs b/azalea-brigadier/src/command.rs index e69de29b..a76454b7 100644 --- a/azalea-brigadier/src/command.rs +++ b/azalea-brigadier/src/command.rs @@ -0,0 +1,10 @@ +use crate::{ + context::command_context::CommandContext, + exceptions::command_syntax_exception::CommandSyntaxException, +}; + +pub const SINGLE_SUCCESS: i32 = 1; + +pub trait Command { + fn run(&self, context: &mut CommandContext) -> Result; +} diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs index e69de29b..c476a39b 100644 --- a/azalea-brigadier/src/command_dispatcher.rs +++ b/azalea-brigadier/src/command_dispatcher.rs @@ -0,0 +1,23 @@ +/// The core command dispatcher, for registering, parsing, and executing commands. +/// The `S` generic is a custom "source" type, such as a user or originator of a command +pub struct CommandDispatcher { + root: RootCommandNode, +} + +impl CommandDispatcher { + /// The string required to separate individual arguments in an input string + /// + /// See: [`ARGUMENT_SEPARATOR_CHAR`] + const ARGUMENT_SEPARATOR: &'static str = " "; + + /// The char required to separate individual arguments in an input string + /// + /// See: [`ARGUMENT_SEPARATOR`] + const ARGUMENT_SEPARATOR_CHAR: char = ' '; + + const USAGE_OPTIONAL_OPEN: &'static str = "["; + const USAGE_OPTIONAL_CLOSE: &'static str = "]"; + const USAGE_REQUIRED_OPEN: &'static str = "("; + const USAGE_REQUIRED_CLOSE: &'static str = ")"; + const USAGE_OR: &'static str = "|"; +} diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs index ddbb447e..c6210a88 100644 --- a/azalea-brigadier/src/context/command_context.rs +++ b/azalea-brigadier/src/context/command_context.rs @@ -1,3 +1,90 @@ +use super::{ + parsed_argument::ParsedArgument, parsed_command_node::ParsedCommandNode, + string_range::StringRange, +}; +use crate::{ + arguments::argument_type::ArgumentType, command::Command, redirect_modifier::RedirectModifier, + tree::command_node::CommandNode, +}; +use std::collections::HashMap; + pub struct CommandContext { source: S, + input: String, + command: dyn Command, + arguments: HashMap>, + root_node: dyn CommandNode, + nodes: Vec>, + range: StringRange, + child: Option>, + modifier: Option>, + forks: bool, +} + +impl CommandContext { + pub fn clone_for(&self, source: S) -> Self { + if self.source == source { + return self.clone(); + } + Self { + source, + input: self.input.clone(), + command: self.command.clone(), + arguments: self.arguments.clone(), + root_node: self.root_node.clone(), + nodes: self.nodes.clone(), + range: self.range.clone(), + child: self.child.clone(), + modifier: self.modifier.clone(), + forks: self.forks, + } + } + + fn child(&self) -> &Option> { + &self.child + } + + fn last_child(&self) -> &CommandContext { + let mut result = self; + while result.child.is_some() { + result = result.child.as_ref().unwrap(); + } + result + } + + fn command(&self) -> &dyn Command { + &self.command + } + + fn source(&self) -> &S { + &self.source + } + + // public V getArgument(final String name, final Class clazz) { + // final ParsedArgument argument = arguments.get(name); + + // if (argument == null) { + // throw new IllegalArgumentException("No such argument '" + name + "' exists on this command"); + // } + + // final Object result = argument.getResult(); + // if (PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(result.getClass())) { + // return (V) result; + // } else { + // throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz); + // } + // } + fn get_argument(&self, name: &str) -> Result { + let argument = self.arguments.get(name); + + if argument.is_none() { + return Err(format!( + "No such argument '{}' exists on this command", + name + )); + } + + let result = argument.unwrap().result(); + Ok(result) + } } diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs index e69de29b..e74b5b1c 100644 --- a/azalea-brigadier/src/context/command_context_builder.rs +++ b/azalea-brigadier/src/context/command_context_builder.rs @@ -0,0 +1,180 @@ +use std::collections::HashMap; + +use crate::{ + arguments::argument_type::ArgumentType, command::Command, + command_dispatcher::CommandDispatcher, redirect_modifier::RedirectModifier, + tree::command_node::CommandNode, +}; + +use super::{ + command_context::CommandContext, parsed_argument::ParsedArgument, + parsed_command_node::ParsedCommandNode, string_range::StringRange, + suggestion_context::SuggestionContext, +}; + +// public class CommandContextBuilder { +// private final Map> arguments = new LinkedHashMap<>(); +// private final CommandNode rootNode; +// private final List> nodes = new ArrayList<>(); +// private final CommandDispatcher dispatcher; +// private S source; +// private Command command; +// private CommandContextBuilder child; +// private StringRange range; +// private RedirectModifier modifier = null; +// private boolean forks; + +#[derive(Clone)] +pub struct CommandContextBuilder { + arguments: HashMap>, + root_node: dyn CommandNode, + nodes: Vec>, + dispatcher: CommandDispatcher, + source: S, + command: Box>, + child: Option>, + range: StringRange, + modifier: Option>>, + forks: bool, +} + +// public CommandContextBuilder(final CommandDispatcher dispatcher, final S source, final CommandNode rootNode, final int start) { +// this.rootNode = rootNode; +// this.dispatcher = dispatcher; +// this.source = source; +// this.range = StringRange.at(start); +// } + +impl CommandContextBuilder { + pub fn new( + dispatcher: CommandDispatcher, + source: S, + root_node: dyn CommandNode, + start: usize, + ) -> Self { + Self { + root_node, + dispatcher, + source, + range: StringRange::at(start), + ..Default::default() + } + } + + pub fn with_source(mut self, source: S) -> Self { + self.source = source; + self + } + + pub fn source(&self) -> &S { + &self.source + } + + pub fn root_node(&self) -> &dyn CommandNode { + &self.root_node + } + + pub fn with_argument( + mut self, + name: String, + argument: ParsedArgument, + ) -> Self { + self.arguments.insert(name, argument); + self + } + + pub fn arguments(&self) -> &HashMap> { + &self.arguments + } + + pub fn with_command(mut self, command: Box>) -> Self { + self.command = command; + self + } + + pub fn with_node(mut self, node: dyn CommandNode, range: StringRange) -> Self { + self.nodes.push(ParsedCommandNode::new(node, range)); + self.range = StringRange::encompassing(&self.range, &range); + self.modifier = node.redirect_modifier(); + self.forks = node.is_fork(); + self + } + + pub fn with_child(mut self, child: CommandContextBuilder) -> Self { + self.child = Some(child); + self + } + + pub fn child(&self) -> Option<&CommandContextBuilder> { + self.child.as_ref() + } + + pub fn last_child(&self) -> Option<&CommandContextBuilder> { + let mut result = self; + while let Some(child) = result.child() { + result = child; + } + Some(result) + } + + pub fn command(&self) -> &dyn Command { + &*self.command + } + + pub fn nodes(&self) -> &Vec> { + &self.nodes + } + + pub fn build(self, input: &str) -> CommandContext { + CommandContext { + source: self.source, + input, + arguments: self.arguments, + command: self.command, + root_node: self.root_node, + nodes: self.nodes, + range: self.range, + child: self.child.map(|child| child.build(input)), + modifier: self.modifier, + forks: self.forks, + } + } + + pub fn dispatcher(&self) -> &CommandDispatcher { + &self.dispatcher + } + + pub fn range(&self) -> &StringRange { + &self.range + } + + pub fn find_suggestion_context(&self, cursor: i32) -> Result, String> { + if self.range.start() <= cursor { + if self.range.end() < cursor { + if let Some(child) = self.child() { + child.find_suggestion_context(cursor); + } else if !self.nodes.is_empty() { + let last = self.nodes.last().unwrap(); + let end = last.range().end() + 1; + return SuggestionContext::new(last.node(), end); + } else { + return SuggestionContext::new(self.root_node, self.range.start()); + } + } else { + let prev = self.root_node; + for node in &self.nodes { + let node_range = node.range(); + if node_range.start() <= cursor && cursor <= node_range.end() { + return SuggestionContext::new(prev, node_range.start()); + } + prev = node.node(); + } + if prev.is_none() { + return Err(String::from("Can't find node before cursor")); + } + return SuggestionContext::new(prev.unwrap(), self.range.start()); + } + } + Err(String::from("Can't find node before cursor")) + } +} diff --git a/azalea-brigadier/src/context/parsed_argument.rs b/azalea-brigadier/src/context/parsed_argument.rs index e69de29b..5f9c2cdb 100644 --- a/azalea-brigadier/src/context/parsed_argument.rs +++ b/azalea-brigadier/src/context/parsed_argument.rs @@ -0,0 +1,24 @@ +use super::string_range::StringRange; + +#[derive(PartialEq, Eq, Hash)] +pub struct ParsedArgument { + range: StringRange, + result: T, +} + +impl ParsedArgument { + fn new(start: usize, end: usize, result: T) -> Self { + Self { + range: StringRange::between(start, end), + result, + } + } + + fn range(&self) -> &StringRange { + &self.range + } + + fn result(&self) -> &T { + &self.result + } +} diff --git a/azalea-brigadier/src/context/parsed_command_node.rs b/azalea-brigadier/src/context/parsed_command_node.rs index e69de29b..98e99959 100644 --- a/azalea-brigadier/src/context/parsed_command_node.rs +++ b/azalea-brigadier/src/context/parsed_command_node.rs @@ -0,0 +1,22 @@ +use super::string_range::StringRange; +use crate::tree::command_node::CommandNode; + +#[derive(Hash, PartialEq, Eq, Debug, Clone)] +pub struct ParsedCommandNode { + node: dyn CommandNode, + range: StringRange, +} + +impl ParsedCommandNode { + fn new(node: dyn CommandNode, range: StringRange) -> Self { + Self { node, range } + } + + fn node(&self) -> &dyn CommandNode { + &self.node + } + + fn range(&self) -> &StringRange { + &self.range + } +} diff --git a/azalea-brigadier/src/context/string_range.rs b/azalea-brigadier/src/context/string_range.rs index e69de29b..d775ab68 100644 --- a/azalea-brigadier/src/context/string_range.rs +++ b/azalea-brigadier/src/context/string_range.rs @@ -0,0 +1,45 @@ +use std::cmp; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct StringRange { + start: usize, + end: usize, +} + +impl StringRange { + pub fn new(start: usize, end: usize) -> Self { + Self { start, end } + } + + pub fn at(pos: usize) -> Self { + Self::new(pos, pos) + } + + pub fn between(start: usize, end: usize) -> Self { + Self::new(start, end) + } + + pub fn encompassing(a: &Self, b: &Self) -> Self { + Self::new(cmp::min(a.start, b.start), cmp::max(a.end, b.end)) + } + + pub fn start(&self) -> usize { + self.start + } + + pub fn end(&self) -> usize { + self.end + } + + pub fn get(&self, reader: &str) -> &str { + &reader[self.start..self.end] + } + + pub fn is_empty(&self) -> bool { + self.start == self.end + } + + pub fn length(&self) -> usize { + self.end - self.start + } +} diff --git a/azalea-brigadier/src/context/suggestion_context.rs b/azalea-brigadier/src/context/suggestion_context.rs index e69de29b..540a5f23 100644 --- a/azalea-brigadier/src/context/suggestion_context.rs +++ b/azalea-brigadier/src/context/suggestion_context.rs @@ -0,0 +1,6 @@ +use crate::tree::command_node::CommandNode; + +pub struct SuggestionContext { + parent: dyn CommandNode, + start_pos: usize, +} diff --git a/azalea-brigadier/src/lib.rs b/azalea-brigadier/src/lib.rs index d0966de3..b2345abb 100644 --- a/azalea-brigadier/src/lib.rs +++ b/azalea-brigadier/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +extern crate lazy_static; + mod ambiguity_consumer; mod arguments; mod builder; diff --git a/azalea-brigadier/src/redirect_modifier.rs b/azalea-brigadier/src/redirect_modifier.rs index e69de29b..cfefd120 100644 --- a/azalea-brigadier/src/redirect_modifier.rs +++ b/azalea-brigadier/src/redirect_modifier.rs @@ -0,0 +1,8 @@ +use crate::{ + context::command_context::CommandContext, + exceptions::command_syntax_exception::CommandSyntaxException, +}; + +pub trait RedirectModifier { + fn apply(&self, context: CommandContext) -> Result, CommandSyntaxException>; +} diff --git a/azalea-brigadier/src/single_redirect_modifier.rs b/azalea-brigadier/src/single_redirect_modifier.rs index e69de29b..dd63244d 100644 --- a/azalea-brigadier/src/single_redirect_modifier.rs +++ b/azalea-brigadier/src/single_redirect_modifier.rs @@ -0,0 +1,8 @@ +use crate::{ + context::command_context::CommandContext, + exceptions::command_syntax_exception::CommandSyntaxException, +}; + +pub trait SingleRedirectModifier { + fn apply(&self, context: CommandContext) -> Result; +} diff --git a/azalea-brigadier/src/suggestion/suggestion_provider.rs b/azalea-brigadier/src/suggestion/suggestion_provider.rs index e69de29b..3027d460 100644 --- a/azalea-brigadier/src/suggestion/suggestion_provider.rs +++ b/azalea-brigadier/src/suggestion/suggestion_provider.rs @@ -0,0 +1,14 @@ +use crate::{ + context::command_context::CommandContext, + exceptions::command_syntax_exception::CommandSyntaxException, +}; + +use super::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}; + +pub trait SuggestionProvider { + fn suggestions( + &self, + context: &CommandContext, + builder: &SuggestionsBuilder, + ) -> Result; +} diff --git a/azalea-brigadier/src/tree/argument_command_node.rs b/azalea-brigadier/src/tree/argument_command_node.rs index e69de29b..df7d3f5c 100644 --- a/azalea-brigadier/src/tree/argument_command_node.rs +++ b/azalea-brigadier/src/tree/argument_command_node.rs @@ -0,0 +1,118 @@ +use std::fmt::{Display, Formatter}; + +use crate::{ + arguments::argument_type::ArgumentType, + context::{ + command_context::CommandContext, command_context_builder::CommandContextBuilder, + parsed_argument::ParsedArgument, + }, + exceptions::command_syntax_exception::CommandSyntaxException, + string_reader::StringReader, + suggestion::{ + suggestion_provider::SuggestionProvider, suggestions::Suggestions, + suggestions_builder::SuggestionsBuilder, + }, +}; + +use super::command_node::{BaseCommandNode, CommandNode}; + +const USAGE_ARGUMENT_OPEN: &str = "<"; +const USAGE_ARGUMENT_CLOSE: &str = ">"; + +#[derive(Hash, PartialEq, Eq, Debug, Clone)] +pub struct ArgumentCommandNode { + name: String, + type_: dyn ArgumentType, + custom_suggestions: dyn SuggestionProvider, + // Since Rust doesn't have extending, we put the struct this is extending as the "base" field + pub base: BaseCommandNode, +} + +impl ArgumentCommandNode { + fn get_type(&self) -> &dyn ArgumentType { + &self.type_ + } + + fn custom_suggestions(&self) -> &dyn SuggestionProvider { + &self.custom_suggestions + } +} + +impl CommandNode for ArgumentCommandNode { + fn name(&self) -> &str { + &self.name + } + + fn parse( + &self, + reader: StringReader, + context_builder: CommandContextBuilder, + ) -> Result<(), CommandSyntaxException> { + // final int start = reader.getCursor(); + // final T result = type.parse(reader); + // final ParsedArgument parsed = new ParsedArgument<>(start, reader.getCursor(), result); + + // contextBuilder.withArgument(name, parsed); + // contextBuilder.withNode(this, parsed.getRange()); + + let start = reader.get_cursor(); + let result = self.get_type().parse(reader)?; + let parsed = ParsedArgument::new(start, reader.get_cursor(), result); + + context_builder.with_argument(&self.name, parsed); + context_builder.with_node(self, parsed.get_range()); + + Ok(()) + } + + fn list_suggestions( + &self, + context: CommandContext, + builder: SuggestionsBuilder, + ) -> Result { + if self.custom_suggestions.is_none() { + self.get_type().list_suggestions(context, builder) + } else { + self.custom_suggestions.get_suggestions(context, builder) + } + } + + fn is_valid_input(&self, input: &str) -> bool { + let reader = StringReader::new(input); + let result = self.get_type().parse(reader); + if result.is_ok() { + return !reader.can_read() || reader.peek() == ' '; + } else { + return false; + } + } + + fn usage_text(&self) -> &str { + USAGE_ARGUMENT_OPEN + self.name + USAGE_ARGUMENT_CLOSE + } + + fn create_builder(&self) -> RequiredArgumentBuilder { + let builder = RequiredArgumentBuilder::argument(&self.name, &self.type_); + builder.requires(self.base.get_requirement()); + builder.forward( + self.base.get_redirect(), + self.base.get_redirect_modifier(), + self.base.is_fork(), + ); + builder.suggests(self.custom_suggestions()); + if self.base.get_command() != None { + builder.executes(self.base.get_command().unwrap()); + } + builder + } + + fn get_examples(&self) -> Vec { + self.type_.get_examples() + } +} + +impl Display for ArgumentCommandNode { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "", self.name, self.type_) + } +} diff --git a/azalea-brigadier/src/tree/command_node.rs b/azalea-brigadier/src/tree/command_node.rs index e69de29b..286820b9 100644 --- a/azalea-brigadier/src/tree/command_node.rs +++ b/azalea-brigadier/src/tree/command_node.rs @@ -0,0 +1,52 @@ +use std::collections::HashMap; + +use crate::{ + builder::argument_builder::ArgumentBuilder, + command::Command, + context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, + exceptions::command_syntax_exception::CommandSyntaxException, + redirect_modifier::RedirectModifier, + string_reader::StringReader, + suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, +}; + +use super::{argument_command_node::ArgumentCommandNode, literal_command_node::LiteralCommandNode}; + +pub struct BaseCommandNode { + // private final Map> children = new LinkedHashMap<>(); + // private final Map> literals = new LinkedHashMap<>(); + // private final Map> arguments = new LinkedHashMap<>(); + // private final Predicate requirement; + // private final CommandNode redirect; + // private final RedirectModifier modifier; + // private final boolean forks; + // private Command command; + children: HashMap>, + literals: HashMap>, + arguments: HashMap>, + requirement: Option bool>, + redirect: Option>, + modifier: Option>, + forks: bool, + command: Option>, +} + +impl BaseCommandNode {} + +pub trait CommandNode { + fn name(&self) -> &str; + fn usage_text(&self) -> &str; + fn parse( + &self, + reader: StringReader, + context_builder: CommandContextBuilder, + ) -> Result<(), CommandSyntaxException>; + fn list_suggestions( + &self, + context: CommandContext, + builder: SuggestionsBuilder, + ) -> Result; + fn is_valid_input(&self, input: &str) -> bool; + fn create_builder(&self) -> dyn ArgumentBuilder; + fn get_examples(&self) -> Vec; +} diff --git a/azalea-brigadier/src/tree/literal_command_node.rs b/azalea-brigadier/src/tree/literal_command_node.rs index e69de29b..bb0e613c 100644 --- a/azalea-brigadier/src/tree/literal_command_node.rs +++ b/azalea-brigadier/src/tree/literal_command_node.rs @@ -0,0 +1,96 @@ +use crate::{ + context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, + exceptions::{ + builtin_exceptions::BuiltInExceptions, command_syntax_exception::CommandSyntaxException, + }, + string_reader::StringReader, + suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, +}; + +use super::command_node::{BaseCommandNode, CommandNode}; + +#[derive(Hash, PartialEq, Eq, Debug, Clone)] +pub struct LiteralCommandNode { + literal: String, + literal_lowercase: String, + // Since Rust doesn't have extending, we put the struct this is extending as the "base" field + pub base: BaseCommandNode, +} + +impl LiteralCommandNode { + pub fn literal(&self) -> &String { + &self.literal + } + + pub fn parse(&self, reader: StringReader) -> i32 { + let start = reader.get_cursor(); + if reader.can_read(self.literal.len()) { + let end = start + self.literal.len(); + if reader.get_string()[start..end].eq(&self.literal) { + reader.set_cursor(end); + if !reader.can_read() || reader.peek() == ' ' { + return end as i32; + } else { + reader.set_cursor(start); + } + } + } + -1 + } +} + +impl CommandNode for LiteralCommandNode { + fn name(&self) -> &str { + &self.literal + } + + fn parse( + &self, + reader: StringReader, + context_builder: CommandContextBuilder, + ) -> Result<(), CommandSyntaxException> { + let start = reader.get_cursor(); + let end = self.parse(reader); + if end > -1 { + return Ok(()); + } + + Err(BuiltInExceptions::LiteralIncorrect { + expected: self.literal(), + } + .create_with_context(reader)) + } + + fn list_suggestions( + &self, + context: CommandContext, + builder: SuggestionsBuilder, + ) -> Result { + if self + .literal_lowercase + .starts_with(&builder.remaining_lowercase()) + { + builder.suggest(self.literal()) + } else { + Suggestions::empty() + } + } + + fn is_valid_input(&self, input: &str) -> bool { + self.parse(StringReader::from(input)) > -1 + } + + fn usage_text(&self) -> &str { + self.literal + } + + fn create_builder(&self) -> LiteralArgumentBuilder { + let builder = LiteralArgumentBuilder::literal(self.literal()); + builder.requires(self.requirement()); + builder.forward(self.redirect(), self.redirect_modifier(), self.is_fork()); + if self.command().is_some() { + builder.executes(self.command().unwrap()); + } + builder + } +} diff --git a/azalea-brigadier/src/tree/mod.rs b/azalea-brigadier/src/tree/mod.rs index e69de29b..3dc22583 100644 --- a/azalea-brigadier/src/tree/mod.rs +++ b/azalea-brigadier/src/tree/mod.rs @@ -0,0 +1,4 @@ +pub mod argument_command_node; +pub mod command_node; +pub mod literal_command_node; +pub mod root_command_node; diff --git a/azalea-brigadier/src/tree/root_command_node.rs b/azalea-brigadier/src/tree/root_command_node.rs index e69de29b..004ab6a8 100644 --- a/azalea-brigadier/src/tree/root_command_node.rs +++ b/azalea-brigadier/src/tree/root_command_node.rs @@ -0,0 +1,61 @@ +use std::fmt::{Display, Formatter}; + +use crate::{ + context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, + exceptions::{ + builtin_exceptions::BuiltInExceptions, command_syntax_exception::CommandSyntaxException, + }, + string_reader::StringReader, + suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, +}; + +use super::command_node::{BaseCommandNode, CommandNode}; + +#[derive(Hash, PartialEq, Eq, Debug, Clone)] +pub struct RootCommandNode { + // Since Rust doesn't have extending, we put the struct this is extending as the "base" field + pub base: BaseCommandNode, +} + +impl CommandNode for RootCommandNode { + fn name(&self) -> &str { + "" + } + + fn parse( + &self, + reader: StringReader, + context_builder: CommandContextBuilder, + ) -> Result<(), CommandSyntaxException> { + } + + fn list_suggestions( + &self, + context: CommandContext, + builder: SuggestionsBuilder, + ) -> Result { + Suggestions::empty() + } + + fn is_valid_input(&self, input: &str) -> bool { + false + } + + fn usage_text(&self) -> &str { + "" + } + + fn create_builder(&self) -> () { + panic!("Cannot convert root into a builder"); + } + + fn get_examples(&self) -> Vec { + vec![] + } +} + +impl Display for RootCommandNode<()> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "") + } +}