From 784a3d79530ba3a9a18e31f98d7143315a71d86a Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Mon, 14 Feb 2022 14:39:17 -0300 Subject: Attempt to make trees --- Cargo.toml | 3 ++ src/args.rs | 67 +++++++++++++++++++----- src/lib.rs | 8 ++- src/tree.rs | 169 ++++++++++++++++++++++++++++++++++++------------------------ 4 files changed, 165 insertions(+), 82 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c21b5d8..bbd897b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,6 @@ homepage = "https://soniex2.github.io/ganarchy/project/139eede493cff4b24a253d2e7 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +#ordered-float = "2.0" +#qcell = { version = "0.5", default-features = false, features = ["alloc"] } +anycast = "1.0" diff --git a/src/args.rs b/src/args.rs index 5b9ce72..e7d76c3 100644 --- a/src/args.rs +++ b/src/args.rs @@ -10,6 +10,7 @@ use ::std::borrow::Cow; use ::std::fmt::Display; use ::std::fmt::Formatter; use ::std::future::Future; +use ::std::hash::Hash; use ::std::io::Cursor; use ::std::marker::PhantomData; use ::std::num::ParseFloatError; @@ -19,6 +20,9 @@ use ::std::ops::RangeBounds; use ::std::pin::Pin; use ::std::str::FromStr; +use anycast::Anycast; +//use ::ordered_float::OrderedFloat; + use crate::error::RangeError; use crate::error::ReadError; use crate::strcursor::StringReader; @@ -27,7 +31,7 @@ use crate::suggestion::SuggestionsBuilder; // FIXME delete when implemented /// The parsing context of a command. -pub struct CommandContext<'i, S, E>(::std::marker::PhantomData<(&'i str, S, E)>); +pub struct CommandContext<'i, S, E>(PhantomData<(&'i str, S, E)>); /// An argument parser/validator. /// @@ -36,8 +40,8 @@ pub struct CommandContext<'i, S, E>(::std::marker::PhantomData<(&'i str, S, E)>) /// this trait. Nevertheless, Iosonism doesn't itself use threads, so a /// [workaround] can be used if one needs non-`Send + Sync` argument types. /// -/// Additionally, argument types must be `Display`. This *is* reflected in this -/// trait. +/// Additionally, argument types must be `Display`, `Eq` and `Hash`. This *is* +/// reflected in this trait. /// /// [workaround]: https://users.rust-lang.org/t/how-to-check-send-at-runtime-similar-to-how-refcell-checks-borrowing-at-runtime/68269 /// @@ -78,7 +82,7 @@ pub struct CommandContext<'i, S, E>(::std::marker::PhantomData<(&'i str, S, E)>) /// } /// } /// ``` -pub trait ArgumentType: Display { +pub trait ArgumentType: Display + Eq + ::std::hash::Hash { /// The parsed type of the argument. type Result: Sized + 'static + Any; @@ -105,8 +109,8 @@ pub trait ArgumentType: Display { } } -/// Internal wrapper around `ArgumentType`, but with `Any`. -pub(crate) trait ArgumentTypeAny: Send + Sync + Display { +/// Internal wrapper around `ArgumentType`, but with more `Any`. +pub(crate) trait ArgumentTypeAny: Send + Sync + Display + Anycast { /// Parses an argument of this type, returning the parsed argument. fn parse<'i>( &self, @@ -122,11 +126,18 @@ pub(crate) trait ArgumentTypeAny: Send + Sync + Display { /// Returns examples for this argument. fn get_examples(&self) -> Cow<'static, [&str]>; + + /// Compares this `ArgumentType` with another. + fn dyn_eq(&self, other: &dyn ArgumentTypeAny) -> bool; + + /// Hashes this `ArgumentType`. + fn dyn_hash(&self, hasher: &mut dyn ::std::hash::Hasher); } -/// Any `ArgumentType` that is also `Send` and `Sync` is an `ArgumentTypeAny`. +/// Any `ArgumentType` that is also `Send`, `Sync`, `Eq` and `Hash` is an +/// `ArgumentTypeAny`. impl ArgumentTypeAny for T -where T: ArgumentType + Send + Sync { +where T: ArgumentType + Send + Sync + Eq + ::std::hash::Hash + Any { fn parse<'i>( &self, reader: &mut Cursor<&'i str>, @@ -145,6 +156,18 @@ where T: ArgumentType + Send + Sync { fn get_examples(&self) -> Cow<'static, [&str]> { self.get_examples() } + + fn dyn_eq(&self, other: &dyn ArgumentTypeAny) -> bool { + match other.any_ref().downcast_ref() { + Some(other) => self == other, + None => false, + } + } + + fn dyn_hash(&self, mut hasher: &mut dyn ::std::hash::Hasher) { + // rust-lang/rust#44015 + self.hash(&mut hasher) + } } /// Any `dyn ArgumentTypeAny` (note the `dyn`!) is an `ArgumentType`. @@ -171,6 +194,21 @@ impl ArgumentType for dyn ArgumentTypeAny { } } +impl PartialEq for dyn ArgumentTypeAny { + fn eq(&self, other: &Self) -> bool { + self.dyn_eq(other) + } +} + +impl Eq for dyn ArgumentTypeAny { +} + +impl ::std::hash::Hash for dyn ArgumentTypeAny { + fn hash(&self, hasher: &mut H) { + self.dyn_hash(hasher as &mut dyn ::std::hash::Hasher) + } +} + /// A boolean argument. // FIXME add examples/expand docs #[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)] @@ -270,8 +308,8 @@ impl ArgumentType for IntegerArgumentType where for<'i> E: ReadError<'i, Cursor<&'i str>>, for<'i> E: RangeError<'i, Cursor<&'i str>, T, R>, - R: RangeBounds, - T: PartialOrd + FromStr + Any + Display, + R: RangeBounds + Eq + Hash, + T: PartialOrd + FromStr + Any + Display + Eq + Hash, { /// An `IntegerArgumentType` parses an integer type. type Result = T; @@ -299,8 +337,8 @@ where /// Formats this `IntegerArgumentType`. /// -/// The resulting string follows the syntax `"integer(start,end)"`, with `start` -/// and `end` being one of the below: +/// The resulting string follows the syntax `"integer(start,end)"`, with +/// `start` and `end` being one of the below: /// /// - `value` if the bound is inclusive. /// - `value*` if the bound is exclusive. @@ -328,6 +366,7 @@ impl> Display for IntegerArgumentType { } } +// FIXME implement Eq and Hash properly /// A float argument. #[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)] pub struct FloatArgumentType> { @@ -376,8 +415,8 @@ impl ArgumentType for FloatArgumentType where for<'i> E: ReadError<'i, Cursor<&'i str>>, for<'i> E: RangeError<'i, Cursor<&'i str>, T, R>, - R: RangeBounds, - T: PartialOrd + FromStr + Any + Display, + R: RangeBounds + Eq + Hash, + T: PartialOrd + FromStr + Any + Display + Eq + Hash, { /// A `FloatArgumentType` parses a float type. type Result = T; diff --git a/src/lib.rs b/src/lib.rs index 1895244..116add0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,8 @@ pub mod strcursor; pub mod suggestion; pub mod tree; +use ::std::sync::Arc; + use crate::args::CommandContext; /// Type of a command handler. @@ -34,8 +36,10 @@ use crate::args::CommandContext; /// - `T`: The type returned by the command. /// - `S`: The source type accepted by this argument type. /// - `E`: The error type accepted by this argument type. -pub type Command = Box< - dyn for<'a, 'i> Fn(&'a CommandContext<'i, S, E>) -> Result + Send + Sync +pub type Command = Arc< + dyn for<'a, 'i> Fn( + &'a CommandContext<'i, Source, Error> + ) -> Result + Send + Sync >; #[cfg(test)] diff --git a/src/tree.rs b/src/tree.rs index 71243be..ae408a8 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -6,100 +6,137 @@ //! Command syntax tree. use ::std::borrow::Cow; +use ::std::collections::HashMap; +use ::std::collections::HashSet; +use ::std::hash::Hash; +use ::std::sync::Arc; use crate::Command; use crate::args::{ArgumentType, ArgumentTypeAny}; use crate::strcursor::StringReader; -// FIXME +// FIXME move use crate::args::CommandContext; /// The kind of node. -enum NodeKind { +enum NodeKind<'a, Source, Error> { /// Root node. Root, /// Literal node. Literal { /// The literal of the node. - literal: Cow<'static, str>, + literal: Cow<'a, str>, }, /// Argument node. Argument { /// The label of the node. - label: Cow<'static, str>, + label: Cow<'a, str>, /// The argument type. - arg: Box>, + arg: Arc>, }, } -/// A node in the syntax tree. -pub struct CommandNode { +impl<'a, Source, Error> Clone for NodeKind<'a, Source, Error> { + fn clone(&self) -> Self { + match self { + &Self::Root => Self::Root, + &Self::Literal { + ref literal, + } => Self::Literal { + literal: Cow::clone(literal), + }, + &Self::Argument { + ref label, + ref arg, + } => Self::Argument { + label: Cow::clone(label), + arg: Arc::clone(arg), + }, + } + } +} + +/// A node of the command parse graph. +pub struct CommandNode<'a, Type, Source, Error> { /// The command to run for this node. - command: Option>, + command: Option>, /// Nodes. - nodes: (), + nodes: HashMap, Cow<'a, CommandNode<'a, Type, Source, Error>>>, /// Literal nodes. - literals: (), + literals: HashSet>, /// Argument nodes. - arguments: (), + arguments: HashSet>, /// The kind of node. - kind: NodeKind, + kind: NodeKind<'a, Source, Error>, } -impl CommandNode { - /// Creates a new root node. - pub fn root() -> Self { - CommandNode { - command: None, - nodes: (), - literals: (), - arguments: (), - kind: NodeKind::Root, - } - } - - /// Creates a new literal node. - pub fn literal( - word: Cow<'static, str>, - command: Option>, - ) -> Self { - CommandNode { - command: command, - nodes: (), - literals: (), - arguments: (), - kind: NodeKind::Literal { - literal: word, - }, +impl<'a, Type, Source, Error> Clone for CommandNode<'a, Type, Source, Error> { + fn clone(&self) -> Self { + Self { + command: self.command.clone(), + nodes: self.nodes.clone(), + literals: self.literals.clone(), + arguments: self.arguments.clone(), + kind: self.kind.clone(), } } +} - /// Creates a new argument node. - pub fn argument + 'static + Send + Sync>( - name: Cow<'static, str>, - command: Option>, - argument: A, - ) -> Self { - CommandNode { - command: command, - nodes: (), - literals: (), - arguments: (), - kind: NodeKind::Argument { - label: name, - arg: Box::new(argument), - }, - } - } - /// Returns the name of this node. - /// - /// For literal nodes, this is the literal itself. For argument nodes, this - /// is the name of the argument. - pub fn get_name(&self) -> &str { - match self.kind { - NodeKind::Root => "", - NodeKind::Literal { ref literal, .. } => literal, - NodeKind::Argument { ref label, .. } => label, - } - } -} +//impl CommandNode { +// /// Creates a new root node. +// pub fn root() -> Self { +// CommandNode { +// command: None, +// nodes: (), +// literals: (), +// arguments: (), +// kind: NodeKind::Root, +// } +// } +// +// /// Creates a new literal node. +// pub fn literal( +// word: Cow<'static, str>, +// command: Option>, +// ) -> Self { +// CommandNode { +// command: command, +// nodes: (), +// literals: (), +// arguments: (), +// kind: NodeKind::Literal { +// literal: word, +// }, +// } +// } +// +// /// Creates a new argument node. +// pub fn argument + 'static + Send + Sync + Eq + Hash>( +// name: Cow<'static, str>, +// command: Option>, +// argument: A, +// ) -> Self { +// CommandNode { +// command: command, +// nodes: (), +// literals: (), +// arguments: (), +// kind: NodeKind::Argument { +// label: name, +// arg: Box::new(argument), +// }, +// } +// } +// +// /// Returns the name of this node. +// /// +// /// For literal nodes, this is the literal itself. For argument nodes, this +// /// is the name of the argument. +// pub fn get_name(&self) -> &str { +// match self.kind { +// NodeKind::Root => "", +// NodeKind::Literal { ref literal, .. } => literal, +// NodeKind::Argument { ref label, .. } => label, +// } +// } +//} -- cgit 1.4.1