From 629e02f9dafb21767c8eb59dead62b887c5d501c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 1 Nov 2020 00:04:02 +0800 Subject: [PATCH] Use Ident. --- src/ast.rs | 6 +++--- src/engine.rs | 8 ++++---- src/module/mod.rs | 14 +++++++------- src/parser.rs | 20 ++++++++++---------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 614ab18e..bb99d2f3 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -539,7 +539,7 @@ impl AsRef for AST { } /// An identifier containing a string name and a position. -#[derive(Debug, Clone, Hash)] +#[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct Ident { pub name: String, pub pos: Position, @@ -553,7 +553,7 @@ impl Ident { } /// An identifier containing an immutable name and a position. -#[derive(Debug, Clone, Hash)] +#[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct IdentX { pub name: ImmutableString, pub pos: Position, @@ -903,7 +903,7 @@ pub enum Expr { StringConstant(Box), /// FnPtr constant. FnPointer(Box), - /// Variable access - ((variable name, position), optional modules, hash, optional index) + /// Variable access - (variable name, optional modules, hash, optional index) Variable(Box<(Ident, Option, u64, Option)>), /// Property access. Property(Box<(IdentX, (String, String))>), diff --git a/src/engine.rs b/src/engine.rs index 3e9f12cc..ee635ed8 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -591,7 +591,7 @@ pub fn search_imports<'s>( state: &mut State, modules: &ModuleRef, ) -> Result<&'s Module, Box> { - let (root, root_pos) = &modules[0]; + let Ident { name: root, pos } = &modules[0]; // Qualified - check if the root module is directly indexed let index = if state.always_search { @@ -608,7 +608,7 @@ pub fn search_imports<'s>( .rev() .find(|(n, _)| n == root) .map(|(_, m)| m) - .ok_or_else(|| EvalAltResult::ErrorModuleNotFound(root.to_string(), *root_pos))? + .ok_or_else(|| EvalAltResult::ErrorModuleNotFound(root.to_string(), *pos))? }) } @@ -619,7 +619,7 @@ pub fn search_imports_mut<'s>( state: &mut State, modules: &ModuleRef, ) -> Result<&'s mut Module, Box> { - let (root, root_pos) = &modules[0]; + let Ident { name: root, pos } = &modules[0]; // Qualified - check if the root module is directly indexed let index = if state.always_search { @@ -636,7 +636,7 @@ pub fn search_imports_mut<'s>( .rev() .find(|(n, _)| n == root) .map(|(_, m)| m) - .ok_or_else(|| EvalAltResult::ErrorModuleNotFound(root.to_string(), *root_pos))? + .ok_or_else(|| EvalAltResult::ErrorModuleNotFound(root.to_string(), *pos))? }) } diff --git a/src/module/mod.rs b/src/module/mod.rs index 907db6f0..79dbe625 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1,6 +1,6 @@ //! Module defining external-loaded modules for Rhai. -use crate::ast::FnAccess; +use crate::ast::{FnAccess, Ident}; use crate::dynamic::{Dynamic, Variant}; use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn, NativeCallContext, SendSync}; use crate::fn_register::by_value as cast_arg; @@ -1502,7 +1502,7 @@ impl Module { /// /// This type is volatile and may change. #[derive(Clone, Eq, PartialEq, Default, Hash)] -pub struct ModuleRef(StaticVec<(String, Position)>, Option); +pub struct ModuleRef(StaticVec, Option); impl fmt::Debug for ModuleRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -1517,7 +1517,7 @@ impl fmt::Debug for ModuleRef { } impl Deref for ModuleRef { - type Target = StaticVec<(String, Position)>; + type Target = StaticVec; fn deref(&self) -> &Self::Target { &self.0 @@ -1532,15 +1532,15 @@ impl DerefMut for ModuleRef { impl fmt::Display for ModuleRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - for (m, _) in self.0.iter() { - write!(f, "{}{}", m, Token::DoubleColon.syntax())?; + for Ident { name, .. } in self.0.iter() { + write!(f, "{}{}", name, Token::DoubleColon.syntax())?; } Ok(()) } } -impl From> for ModuleRef { - fn from(modules: StaticVec<(String, Position)>) -> Self { +impl From> for ModuleRef { + fn from(modules: StaticVec) -> Self { Self(modules, None) } } diff --git a/src/parser.rs b/src/parser.rs index 6aae8d77..7acef0d1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -292,7 +292,7 @@ fn parse_fn_call( let hash_script = if let Some(modules) = namespace.as_mut() { #[cfg(not(feature = "no_module"))] - modules.set_index(state.find_module(&modules[0].0)); + modules.set_index(state.find_module(&modules[0].name)); // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, @@ -300,7 +300,7 @@ fn parse_fn_call( // 2) Calculate a second hash with no qualifiers, empty function name, // zero number of arguments, and the actual list of argument `TypeId`'s. // 3) The final hash is the XOR of the two hashes. - let qualifiers = modules.iter().map(|(m, _)| m.as_str()); + let qualifiers = modules.iter().map(|m| m.name.as_str()); calc_script_fn_hash(qualifiers, &id, 0) } else { // Qualifiers (none) + function name + no parameters. @@ -339,7 +339,7 @@ fn parse_fn_call( let hash_script = if let Some(modules) = namespace.as_mut() { #[cfg(not(feature = "no_module"))] - modules.set_index(state.find_module(&modules[0].0)); + modules.set_index(state.find_module(&modules[0].name)); // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, @@ -347,7 +347,7 @@ fn parse_fn_call( // 2) Calculate a second hash with no qualifiers, empty function name, // zero number of arguments, and the actual list of argument `TypeId`'s. // 3) The final hash is the XOR of the two hashes. - let qualifiers = modules.iter().map(|(m, _)| m.as_str()); + let qualifiers = modules.iter().map(|m| m.name.as_str()); calc_script_fn_hash(qualifiers, &id, args.len()) } else { // Qualifiers (none) + function name + number of arguments. @@ -891,13 +891,13 @@ fn parse_primary( // module access (Expr::Variable(x), Token::DoubleColon) => match input.next().unwrap() { (Token::Identifier(id2), pos2) => { - let (Ident { name, pos }, mut modules, _, index) = *x; + let (var_name_def, mut modules, _, index) = *x; if let Some(ref mut modules) = modules { - modules.push((name, pos)); + modules.push(var_name_def); } else { let mut m: ModuleRef = Default::default(); - m.push((name, pos)); + m.push(var_name_def); modules = Some(m); } @@ -929,10 +929,10 @@ fn parse_primary( let modules = modules.as_mut().unwrap(); // Qualifiers + variable name - *hash = calc_script_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, 0); + *hash = calc_script_fn_hash(modules.iter().map(|v| v.name.as_str()), name, 0); #[cfg(not(feature = "no_module"))] - modules.set_index(state.find_module(&modules[0].0)); + modules.set_index(state.find_module(&modules[0].name)); } _ => (), } @@ -1206,7 +1206,7 @@ fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result { - return Err(PERR::PropertyExpected.into_err(x.1.unwrap()[0].1)); + return Err(PERR::PropertyExpected.into_err(x.1.unwrap()[0].pos)); } // lhs.prop (lhs, prop @ Expr::Property(_)) => {