Use Ident.

This commit is contained in:
Stephen Chung 2020-11-01 00:04:02 +08:00
parent abbee80e5d
commit 629e02f9da
4 changed files with 24 additions and 24 deletions

View File

@ -539,7 +539,7 @@ impl AsRef<Module> 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<IdentX>),
/// FnPtr constant.
FnPointer(Box<IdentX>),
/// Variable access - ((variable name, position), optional modules, hash, optional index)
/// Variable access - (variable name, optional modules, hash, optional index)
Variable(Box<(Ident, Option<ModuleRef>, u64, Option<NonZeroUsize>)>),
/// Property access.
Property(Box<(IdentX, (String, String))>),

View File

@ -591,7 +591,7 @@ pub fn search_imports<'s>(
state: &mut State,
modules: &ModuleRef,
) -> Result<&'s Module, Box<EvalAltResult>> {
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<EvalAltResult>> {
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))?
})
}

View File

@ -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<NonZeroUsize>);
pub struct ModuleRef(StaticVec<Ident>, Option<NonZeroUsize>);
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<Ident>;
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<StaticVec<(String, Position)>> for ModuleRef {
fn from(modules: StaticVec<(String, Position)>) -> Self {
impl From<StaticVec<Ident>> for ModuleRef {
fn from(modules: StaticVec<Ident>) -> Self {
Self(modules, None)
}
}

View File

@ -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<Expr, ParseEr
}
// lhs.module::id - syntax error
(_, Expr::Variable(x)) if x.1.is_some() => {
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(_)) => {