From 64b889fb95361cb35611da860d7c7f2db07799f9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 13 Nov 2021 22:36:23 +0800 Subject: [PATCH] Restructure code base. --- src/{ => api}/deprecated.rs | 0 src/api/mod.rs | 5 ++ src/{engine_api.rs => api/public.rs} | 40 ++++++----- src/{engine_settings.rs => api/settings.rs} | 2 +- src/ast.rs | 6 +- src/custom_syntax.rs | 6 +- src/engine.rs | 31 ++++---- src/{fn_args.rs => func/args.rs} | 2 +- src/{fn_builtin.rs => func/builtin.rs} | 5 +- src/{fn_call.rs => func/call.rs} | 12 ++-- src/{fn_func.rs => func/func.rs} | 2 +- src/{fn_hash.rs => func/hashing.rs} | 0 src/func/mod.rs | 10 +++ src/{fn_native.rs => func/native.rs} | 4 +- src/{ => func}/plugin.rs | 4 +- src/{fn_register.rs => func/register.rs} | 8 +-- src/lib.rs | 72 ++++++++----------- src/module/mod.rs | 14 ++-- src/module/resolvers/file.rs | 2 +- src/module/resolvers/mod.rs | 2 +- src/{optimize.rs => optimizer.rs} | 10 +-- src/packages/fn_basic.rs | 2 +- src/packages/iter_basic.rs | 2 +- src/packages/lang_core.rs | 2 +- src/packages/math_basic.rs | 5 +- src/{parse.rs => parser.rs} | 12 ++-- src/serde/de.rs | 2 +- src/serde/serialize.rs | 4 +- src/{token.rs => tokenizer.rs} | 2 +- src/{ => types}/dynamic.rs | 12 ++-- src/{ => types}/error.rs | 0 src/{ => types}/fn_ptr.rs | 2 +- src/{ => types}/immutable_string.rs | 2 +- src/types/mod.rs | 8 +++ .../parse_error.rs} | 0 src/{ => types}/scope.rs | 2 +- 36 files changed, 154 insertions(+), 140 deletions(-) rename src/{ => api}/deprecated.rs (100%) create mode 100644 src/api/mod.rs rename src/{engine_api.rs => api/public.rs} (98%) rename src/{engine_settings.rs => api/settings.rs} (99%) rename src/{fn_args.rs => func/args.rs} (98%) rename src/{fn_builtin.rs => func/builtin.rs} (99%) rename src/{fn_call.rs => func/call.rs} (99%) rename src/{fn_func.rs => func/func.rs} (99%) rename src/{fn_hash.rs => func/hashing.rs} (100%) create mode 100644 src/func/mod.rs rename src/{fn_native.rs => func/native.rs} (99%) rename src/{ => func}/plugin.rs (92%) rename src/{fn_register.rs => func/register.rs} (98%) rename src/{optimize.rs => optimizer.rs} (99%) rename src/{parse.rs => parser.rs} (99%) rename src/{token.rs => tokenizer.rs} (99%) rename src/{ => types}/dynamic.rs (99%) rename src/{ => types}/error.rs (100%) rename src/{ => types}/fn_ptr.rs (99%) rename src/{ => types}/immutable_string.rs (99%) create mode 100644 src/types/mod.rs rename src/{error_parsing.rs => types/parse_error.rs} (100%) rename src/{ => types}/scope.rs (99%) diff --git a/src/deprecated.rs b/src/api/deprecated.rs similarity index 100% rename from src/deprecated.rs rename to src/api/deprecated.rs diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 00000000..6b560f94 --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1,5 @@ +//! Module defining the public API of the Rhai engine. + +pub mod deprecated; +pub mod public; +pub mod settings; diff --git a/src/engine_api.rs b/src/api/public.rs similarity index 98% rename from src/engine_api.rs rename to src/api/public.rs index 3dc9d82e..f29ea0d6 100644 --- a/src/engine_api.rs +++ b/src/api/public.rs @@ -1,14 +1,12 @@ -//! Module that defines the extern API of [`Engine`]. +//! Module that defines the public API of [`Engine`]. -use crate::dynamic::Variant; use crate::engine::{EvalContext, EvalState, Imports}; -use crate::fn_call::FnCallArgs; -use crate::fn_native::SendSync; -use crate::fn_register::RegisterNativeFunction; -use crate::parse::ParseState; +use crate::func::{call::FnCallArgs, native::SendSync, register::RegisterNativeFunction}; +use crate::parser::ParseState; +use crate::types::dynamic::Variant; use crate::{ - scope::Scope, Dynamic, Engine, EvalAltResult, FnAccess, FnNamespace, Identifier, Module, - NativeCallContext, ParseError, Position, RhaiResult, Shared, AST, + Dynamic, Engine, EvalAltResult, FnAccess, FnNamespace, Identifier, Module, NativeCallContext, + ParseError, Position, RhaiResult, Scope, Shared, AST, }; use std::any::{type_name, TypeId}; #[cfg(feature = "no_std")] @@ -937,12 +935,12 @@ impl Engine { name: impl AsRef + Into, module: Shared, ) { - let separator = crate::token::Token::DoubleColon.syntax(); + let separator = crate::tokenizer::Token::DoubleColon.syntax(); if !name.as_ref().contains(separator.as_ref()) { if !module.is_indexed() { // Index the module (making a clone copy if necessary) if it is not indexed - let mut module = crate::fn_native::shared_take_or_clone(module); + let mut module = crate::func::native::shared_take_or_clone(module); module.build_index(); root.insert(name.into(), module.into()); } else { @@ -960,7 +958,7 @@ impl Engine { root.insert(sub_module.into(), m.into()); } else { let m = root.remove(sub_module).expect("contains sub-module"); - let mut m = crate::fn_native::shared_take_or_clone(m); + let mut m = crate::func::native::shared_take_or_clone(m); register_static_module_raw(m.sub_modules_mut(), remainder, module); m.build_index(); root.insert(sub_module.into(), m.into()); @@ -1053,7 +1051,7 @@ impl Engine { ) -> Result> { use crate::{ ast::{ASTNode, Expr, Stmt}, - fn_native::shared_take_or_clone, + func::native::shared_take_or_clone, module::resolvers::StaticModuleResolver, }; use std::collections::BTreeSet; @@ -1349,7 +1347,7 @@ impl Engine { json: impl AsRef, has_null: bool, ) -> Result> { - use crate::token::Token; + use crate::tokenizer::Token; fn parse_json_inner( engine: &Engine, @@ -2026,7 +2024,7 @@ impl Engine { // Check for data race. #[cfg(not(feature = "no_closure"))] - crate::fn_call::ensure_no_data_race(name, args, false)?; + crate::func::call::ensure_no_data_race(name, args, false)?; self.call_script_fn( scope, @@ -2084,7 +2082,7 @@ impl Engine { let statements = std::mem::take(ast.statements_mut()); - crate::optimize::optimize_into_ast(self, scope, statements, lib, optimization_level) + crate::optimizer::optimize_into_ast(self, scope, statements, lib, optimization_level) } /// _(metadata)_ Generate a list of all registered functions. /// Exported under the `metadata` feature only. @@ -2184,14 +2182,14 @@ impl Engine { /// > `Fn(token: Token, pos: Position, state: &TokenizeState) -> Token` /// /// where: - /// * [`token`][crate::token::Token]: current token parsed + /// * [`token`][crate::tokenizer::Token]: current token parsed /// * [`pos`][`Position`]: location of the token - /// * [`state`][crate::token::TokenizeState]: current state of the tokenizer + /// * [`state`][crate::tokenizer::TokenizeState]: current state of the tokenizer /// /// ## Raising errors /// /// It is possible to raise a parsing error by returning - /// [`Token::LexError`][crate::token::Token::LexError] as the mapped token. + /// [`Token::LexError`][crate::tokenizer::Token::LexError] as the mapped token. /// /// # Example /// @@ -2225,7 +2223,11 @@ impl Engine { #[inline(always)] pub fn on_parse_token( &mut self, - callback: impl Fn(crate::token::Token, Position, &crate::token::TokenizeState) -> crate::token::Token + callback: impl Fn( + crate::tokenizer::Token, + Position, + &crate::tokenizer::TokenizeState, + ) -> crate::tokenizer::Token + SendSync + 'static, ) -> &mut Self { diff --git a/src/engine_settings.rs b/src/api/settings.rs similarity index 99% rename from src/engine_settings.rs rename to src/api/settings.rs index 600434f9..8a5682f3 100644 --- a/src/engine_settings.rs +++ b/src/api/settings.rs @@ -1,6 +1,6 @@ //! Configuration settings for [`Engine`]. -use crate::token::Token; +use crate::tokenizer::Token; use crate::Engine; use crate::{engine::Precedence, Identifier}; #[cfg(feature = "no_std")] diff --git a/src/ast.rs b/src/ast.rs index 2a19abc1..4a6a81d9 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,10 +1,10 @@ //! Module defining the AST (abstract syntax tree). use crate::calc_fn_hash; -use crate::dynamic::Union; -use crate::fn_native::shared_make_mut; +use crate::func::native::shared_make_mut; use crate::module::NamespaceRef; -use crate::token::Token; +use crate::tokenizer::Token; +use crate::types::dynamic::Union; use crate::{ Dynamic, FnNamespace, Identifier, ImmutableString, Module, Position, Shared, StaticVec, INT, }; diff --git a/src/custom_syntax.rs b/src/custom_syntax.rs index 9de6af3f..a2040dbe 100644 --- a/src/custom_syntax.rs +++ b/src/custom_syntax.rs @@ -1,11 +1,11 @@ //! Module implementing custom syntax for [`Engine`]. use crate::ast::Expr; -use crate::dynamic::Variant; use crate::engine::EvalContext; -use crate::fn_native::SendSync; +use crate::func::native::SendSync; use crate::r#unsafe::unsafe_try_cast; -use crate::token::{is_valid_identifier, Token}; +use crate::tokenizer::{is_valid_identifier, Token}; +use crate::types::dynamic::Variant; use crate::{ Engine, Identifier, ImmutableString, LexError, ParseError, Position, RhaiResult, Shared, StaticVec, INT, diff --git a/src/engine.rs b/src/engine.rs index 99845865..700225df 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -2,16 +2,18 @@ use crate::ast::{Expr, FnCallExpr, Ident, OpAssignment, Stmt, AST_OPTION_FLAGS::*}; use crate::custom_syntax::CustomSyntax; -use crate::dynamic::{map_std_type_name, AccessMode, Union, Variant}; -use crate::fn_hash::get_hasher; -use crate::fn_native::{ - CallableFunction, IteratorFn, OnDebugCallback, OnParseTokenCallback, OnPrintCallback, - OnVarCallback, +use crate::func::{ + hashing::get_hasher, + native::{ + CallableFunction, IteratorFn, OnDebugCallback, OnParseTokenCallback, OnPrintCallback, + OnVarCallback, + }, }; use crate::module::NamespaceRef; use crate::packages::{Package, StandardPackage}; use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; -use crate::token::Token; +use crate::tokenizer::Token; +use crate::types::dynamic::{map_std_type_name, AccessMode, Union, Variant}; use crate::{ Dynamic, EvalAltResult, Identifier, ImmutableString, Module, Position, RhaiResult, Scope, Shared, StaticVec, INT, @@ -214,7 +216,7 @@ impl Imports { &'a mut self, ) -> Option> + 'a> { if let Some(ref global_constants) = self.global_constants { - Some(crate::fn_native::shared_write_lock(global_constants)) + Some(crate::func::native::shared_write_lock(global_constants)) } else { None } @@ -228,7 +230,7 @@ impl Imports { self.global_constants = Some(dict.into()); } - crate::fn_native::shared_write_lock(self.global_constants.as_mut().expect("`Some`")) + crate::func::native::shared_write_lock(self.global_constants.as_mut().expect("`Some`")) .insert(name.into(), value); } /// Get the pre-calculated index getter hash. @@ -470,7 +472,12 @@ pub enum Target<'a> { /// The target is a mutable reference to a Shared `Dynamic` value. /// It holds both the access guard and the original shared value. #[cfg(not(feature = "no_closure"))] - LockGuard((crate::dynamic::DynamicWriteLock<'a, Dynamic>, Dynamic)), + LockGuard( + ( + crate::types::dynamic::DynamicWriteLock<'a, Dynamic>, + Dynamic, + ), + ), /// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects). TempValue(Dynamic), /// The target is a bit inside an [`INT`][crate::INT]. @@ -1004,7 +1011,7 @@ pub struct Engine { pub(crate) debug: Option, /// Callback closure for progress reporting. #[cfg(not(feature = "unchecked"))] - pub(crate) progress: Option, + pub(crate) progress: Option, /// Optimize the AST after compilation. #[cfg(not(feature = "no_optimize"))] @@ -3081,7 +3088,7 @@ impl Engine { if let Some(name) = export.as_ref().map(|x| x.name.clone()) { if !module.is_indexed() { // Index the module (making a clone copy if necessary) if it is not indexed - let mut module = crate::fn_native::shared_take_or_clone(module); + let mut module = crate::func::native::shared_take_or_clone(module); module.build_index(); mods.push(name, module); } else { @@ -3139,7 +3146,7 @@ impl Engine { fn check_return_value(&self, mut result: RhaiResult) -> RhaiResult { if let Ok(ref mut r) = result { // Concentrate all empty strings into one instance to save memory - if let Dynamic(crate::dynamic::Union::Str(s, _, _)) = r { + if let Dynamic(crate::types::dynamic::Union::Str(s, _, _)) = r { if s.is_empty() { if !s.ptr_eq(&self.empty_string) { *s = self.const_empty_string(); diff --git a/src/fn_args.rs b/src/func/args.rs similarity index 98% rename from src/fn_args.rs rename to src/func/args.rs index 3f6f11de..46653724 100644 --- a/src/fn_args.rs +++ b/src/func/args.rs @@ -3,7 +3,7 @@ #![cfg(not(feature = "no_function"))] #![allow(non_snake_case)] -use crate::dynamic::Variant; +use crate::types::dynamic::Variant; use crate::Dynamic; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/fn_builtin.rs b/src/func/builtin.rs similarity index 99% rename from src/fn_builtin.rs rename to src/func/builtin.rs index d2328b0c..321ad7d7 100644 --- a/src/fn_builtin.rs +++ b/src/func/builtin.rs @@ -1,9 +1,8 @@ //! Built-in implementations for common operators. +use super::call::FnCallArgs; use crate::engine::OP_CONTAINS; -use crate::fn_call::FnCallArgs; -use crate::fn_native::NativeCallContext; -use crate::{Dynamic, ImmutableString, RhaiResult, INT}; +use crate::{Dynamic, ImmutableString, NativeCallContext, RhaiResult, INT}; use std::any::TypeId; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/fn_call.rs b/src/func/call.rs similarity index 99% rename from src/fn_call.rs rename to src/func/call.rs index fb3dc56e..ec49af73 100644 --- a/src/fn_call.rs +++ b/src/func/call.rs @@ -1,21 +1,19 @@ //! Implement function-calling mechanism for [`Engine`]. +use super::builtin::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn}; +use super::native::{CallableFunction, FnAny}; use crate::ast::FnCallHashes; use crate::engine::{ EvalState, FnResolutionCacheEntry, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_TYPE_OF, MAX_DYNAMIC_PARAMETERS, }; -use crate::fn_builtin::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn}; -use crate::fn_native::FnAny; use crate::module::NamespaceRef; -use crate::token::Token; +use crate::tokenizer::Token; use crate::{ ast::{Expr, Stmt}, - calc_fn_hash, calc_fn_params_hash, combine_hashes, - fn_native::CallableFunction, - Dynamic, Engine, EvalAltResult, FnPtr, Identifier, ImmutableString, Module, ParseErrorType, - Position, RhaiResult, Scope, StaticVec, + calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, EvalAltResult, FnPtr, + Identifier, ImmutableString, Module, ParseErrorType, Position, RhaiResult, Scope, StaticVec, }; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/fn_func.rs b/src/func/func.rs similarity index 99% rename from src/fn_func.rs rename to src/func/func.rs index b33e273f..f442b6fb 100644 --- a/src/fn_func.rs +++ b/src/func/func.rs @@ -3,7 +3,7 @@ #![cfg(not(feature = "no_function"))] #![allow(non_snake_case)] -use crate::dynamic::Variant; +use crate::types::dynamic::Variant; use crate::{Engine, EvalAltResult, ParseError, Scope, SmartString, AST}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/fn_hash.rs b/src/func/hashing.rs similarity index 100% rename from src/fn_hash.rs rename to src/func/hashing.rs diff --git a/src/func/mod.rs b/src/func/mod.rs new file mode 100644 index 00000000..59e83937 --- /dev/null +++ b/src/func/mod.rs @@ -0,0 +1,10 @@ +//! Module defining mechanisms to handle function calls in Rhai. + +pub mod args; +pub mod builtin; +pub mod call; +pub mod func; +pub mod hashing; +pub mod native; +pub mod plugin; +pub mod register; diff --git a/src/fn_native.rs b/src/func/native.rs similarity index 99% rename from src/fn_native.rs rename to src/func/native.rs index 7225e5c9..cf8064c7 100644 --- a/src/fn_native.rs +++ b/src/func/native.rs @@ -1,10 +1,10 @@ //! Module defining interfaces to native-Rust functions. +use super::call::FnCallArgs; use crate::ast::{FnAccess, FnCallHashes}; use crate::engine::{EvalState, Imports}; -use crate::fn_call::FnCallArgs; use crate::plugin::PluginFunction; -use crate::token::{Token, TokenizeState}; +use crate::tokenizer::{Token, TokenizeState}; use crate::{ calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, Module, Position, RhaiResult, }; diff --git a/src/plugin.rs b/src/func/plugin.rs similarity index 92% rename from src/plugin.rs rename to src/func/plugin.rs index 779f3fd7..b508c52d 100644 --- a/src/plugin.rs +++ b/src/func/plugin.rs @@ -1,7 +1,7 @@ //! Module defining macros for developing _plugins_. -use crate::fn_call::FnCallArgs; -pub use crate::fn_native::CallableFunction; +use super::call::FnCallArgs; +pub use super::native::CallableFunction; pub use crate::{ Dynamic, Engine, EvalAltResult, FnAccess, FnNamespace, ImmutableString, Module, NativeCallContext, Position, diff --git a/src/fn_register.rs b/src/func/register.rs similarity index 98% rename from src/fn_register.rs rename to src/func/register.rs index 8fab0805..e317f51b 100644 --- a/src/fn_register.rs +++ b/src/func/register.rs @@ -2,11 +2,11 @@ #![allow(non_snake_case)] -use crate::dynamic::{DynamicWriteLock, Variant}; -use crate::fn_call::FnCallArgs; -use crate::fn_native::{CallableFunction, FnAny, SendSync}; +use crate::func::call::FnCallArgs; +use crate::func::native::{CallableFunction, FnAny, SendSync}; use crate::r#unsafe::unsafe_try_cast; -use crate::token::Position; +use crate::tokenizer::Position; +use crate::types::dynamic::{DynamicWriteLock, Variant}; use crate::{Dynamic, EvalAltResult, NativeCallContext}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/lib.rs b/src/lib.rs index 96c1391e..861bee3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,33 +69,19 @@ use std::prelude::v1::*; // Internal modules +mod api; mod ast; mod custom_syntax; -mod deprecated; -mod dynamic; mod engine; -mod engine_api; -mod engine_settings; -mod error; -mod error_parsing; -mod fn_args; -mod fn_builtin; -mod fn_call; -mod fn_func; -mod fn_hash; -mod fn_native; -mod fn_ptr; -mod fn_register; -mod immutable_string; +mod func; mod module; #[cfg(not(feature = "no_optimize"))] -mod optimize; +mod optimizer; pub mod packages; -mod parse; -pub mod plugin; -mod scope; +mod parser; mod tests; -mod token; +mod tokenizer; +mod types; mod r#unsafe; type RhaiResult = Result>; @@ -132,17 +118,18 @@ pub type FLOAT = f32; pub use ast::{FnAccess, AST}; pub use custom_syntax::Expression; -pub use dynamic::Dynamic; pub use engine::{Engine, EvalContext, OP_CONTAINS, OP_EQUALS}; -pub use error::EvalAltResult; -pub use error_parsing::{LexError, ParseError, ParseErrorType}; -pub use fn_native::NativeCallContext; -pub use fn_ptr::FnPtr; -pub use fn_register::RegisterNativeFunction; -pub use immutable_string::ImmutableString; +pub use func::{native::NativeCallContext, register::RegisterNativeFunction}; pub use module::{FnNamespace, Module}; -pub use scope::Scope; -pub use token::Position; +pub use tokenizer::Position; +pub use types::{ + dynamic::Dynamic, + error::EvalAltResult, + fn_ptr::FnPtr, + immutable_string::ImmutableString, + parse_error::{LexError, ParseError, ParseErrorType}, + scope::Scope, +}; /// An identifier in Rhai. [`SmartString`](https://crates.io/crates/smartstring) is used because most /// identifiers are ASCII and short, fewer than 23 characters, so they can be stored inline. @@ -169,23 +156,22 @@ pub type Identifier = SmartString; pub type Identifier = ImmutableString; /// Alias to [`Rc`][std::rc::Rc] or [`Arc`][std::sync::Arc] depending on the `sync` feature flag. -pub use fn_native::Shared; +pub use func::native::Shared; -//// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag. -pub use fn_native::Locked; +/// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag. +pub use func::native::Locked; -pub(crate) use fn_hash::{ +pub(crate) use func::hashing::{ calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash, combine_hashes, }; pub use rhai_codegen::*; -#[cfg(not(feature = "no_function"))] -pub use fn_func::Func; +pub use func::plugin; #[cfg(not(feature = "no_function"))] -pub use fn_args::FuncArgs; +pub use func::{args::FuncArgs, func::Func}; #[cfg(not(feature = "no_function"))] pub use ast::ScriptFnMetadata; @@ -211,28 +197,28 @@ pub use module::resolvers as module_resolvers; pub mod serde; #[cfg(not(feature = "no_optimize"))] -pub use optimize::OptimizationLevel; +pub use optimizer::OptimizationLevel; + +// Expose internal data structures. #[cfg(feature = "internals")] #[deprecated = "this type is volatile and may change"] -pub use dynamic::{AccessMode, DynamicReadLock, DynamicWriteLock, Variant}; +pub use types::dynamic::{AccessMode, DynamicReadLock, DynamicWriteLock, Variant}; -// Expose internal data structures. #[cfg(feature = "internals")] #[deprecated = "this function is volatile and may change"] -pub use token::{get_next_token, parse_string_literal}; +pub use tokenizer::{get_next_token, parse_string_literal}; -// Expose internal data structures. #[cfg(feature = "internals")] #[deprecated = "this type is volatile and may change"] -pub use token::{ +pub use tokenizer::{ InputStream, MultiInputsStream, Token, TokenIterator, TokenizeState, TokenizerControl, TokenizerControlBlock, }; #[cfg(feature = "internals")] #[deprecated = "this type is volatile and may change"] -pub use parse::{IdentifierBuilder, ParseState}; +pub use parser::{IdentifierBuilder, ParseState}; #[cfg(feature = "internals")] #[deprecated = "this type is volatile and may change"] diff --git a/src/module/mod.rs b/src/module/mod.rs index 78ffba6b..6d5f3975 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1,12 +1,14 @@ //! Module defining external-loaded modules for Rhai. use crate::ast::{FnAccess, Ident}; -use crate::dynamic::Variant; -use crate::fn_call::FnCallArgs; -use crate::fn_native::{shared_take_or_clone, CallableFunction, IteratorFn, SendSync}; -use crate::fn_register::RegisterNativeFunction; -use crate::parse::IdentifierBuilder; -use crate::token::Token; +use crate::func::{ + call::FnCallArgs, + native::{shared_take_or_clone, CallableFunction, IteratorFn, SendSync}, + register::RegisterNativeFunction, +}; +use crate::parser::IdentifierBuilder; +use crate::tokenizer::Token; +use crate::types::dynamic::Variant; use crate::{ calc_fn_params_hash, calc_qualified_fn_hash, combine_hashes, Dynamic, EvalAltResult, Identifier, ImmutableString, NativeCallContext, Shared, StaticVec, diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index 1cf096f8..7af23527 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -1,4 +1,4 @@ -use crate::fn_native::shared_write_lock; +use crate::func::native::shared_write_lock; use crate::{Engine, EvalAltResult, Identifier, Module, ModuleResolver, Position, Scope, Shared}; #[cfg(feature = "no_std")] diff --git a/src/module/resolvers/mod.rs b/src/module/resolvers/mod.rs index 13030ad7..17f48f99 100644 --- a/src/module/resolvers/mod.rs +++ b/src/module/resolvers/mod.rs @@ -1,4 +1,4 @@ -use crate::fn_native::SendSync; +use crate::func::native::SendSync; use crate::{Engine, EvalAltResult, Module, Position, Shared, AST}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/optimize.rs b/src/optimizer.rs similarity index 99% rename from src/optimize.rs rename to src/optimizer.rs index 3afd75d5..dc2ed085 100644 --- a/src/optimize.rs +++ b/src/optimizer.rs @@ -1,13 +1,13 @@ //! Module implementing the [`AST`] optimizer. use crate::ast::{Expr, OpAssignment, Stmt, AST_OPTION_FLAGS::*}; -use crate::dynamic::AccessMode; use crate::engine::{ EvalState, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF, }; -use crate::fn_builtin::get_builtin_binary_op_fn; -use crate::fn_hash::get_hasher; -use crate::token::Token; +use crate::func::builtin::get_builtin_binary_op_fn; +use crate::func::hashing::get_hasher; +use crate::tokenizer::Token; +use crate::types::dynamic::AccessMode; use crate::{ calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, FnPtr, ImmutableString, Module, Position, Scope, StaticVec, AST, @@ -1157,7 +1157,7 @@ pub fn optimize_into_ast( _functions .into_iter() .map(|fn_def| { - let mut fn_def = crate::fn_native::shared_take_or_clone(fn_def); + let mut fn_def = crate::func::native::shared_take_or_clone(fn_def); // Optimize the function body let body = mem::take(fn_def.body.deref_mut()); diff --git a/src/packages/fn_basic.rs b/src/packages/fn_basic.rs index ab154217..5ec75874 100644 --- a/src/packages/fn_basic.rs +++ b/src/packages/fn_basic.rs @@ -112,7 +112,7 @@ fn collect_fn_metadata(ctx: NativeCallContext) -> crate::Array { let ns = format!( "{}{}{}", namespace, - crate::token::Token::DoubleColon.literal_syntax(), + crate::tokenizer::Token::DoubleColon.literal_syntax(), ns ); scan_module(list, dict, ns.into(), m.as_ref()) diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 16d3d5e4..6f334973 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -1,4 +1,4 @@ -use crate::dynamic::Variant; +use crate::types::dynamic::Variant; use crate::{def_package, EvalAltResult, INT}; use std::iter::{ExactSizeIterator, FusedIterator}; use std::ops::Range; diff --git a/src/packages/lang_core.rs b/src/packages/lang_core.rs index 91c78dba..cab9ebc3 100644 --- a/src/packages/lang_core.rs +++ b/src/packages/lang_core.rs @@ -1,6 +1,6 @@ use crate::def_package; -use crate::dynamic::Tag; use crate::plugin::*; +use crate::types::dynamic::Tag; use crate::{Dynamic, EvalAltResult, INT}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index b97fb9db..1e65f66f 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -6,10 +6,7 @@ use crate::{def_package, Position, INT}; use std::prelude::v1::*; #[cfg(not(feature = "no_float"))] -use crate::FLOAT; - -#[cfg(not(feature = "no_float"))] -use crate::error::EvalAltResult; +use crate::{EvalAltResult, FLOAT}; #[cfg(feature = "no_std")] #[cfg(not(feature = "no_float"))] diff --git a/src/parse.rs b/src/parser.rs similarity index 99% rename from src/parse.rs rename to src/parser.rs index 7258f2f3..6cfa647b 100644 --- a/src/parse.rs +++ b/src/parser.rs @@ -5,14 +5,14 @@ use crate::ast::{ StmtBlock, AST_OPTION_FLAGS::*, }; use crate::custom_syntax::{markers::*, CustomSyntax}; -use crate::dynamic::AccessMode; use crate::engine::{Precedence, KEYWORD_THIS, OP_CONTAINS}; -use crate::fn_hash::get_hasher; +use crate::func::hashing::get_hasher; use crate::module::NamespaceRef; -use crate::token::{ +use crate::tokenizer::{ is_keyword_function, is_valid_function_name, is_valid_identifier, Token, TokenStream, TokenizerControl, }; +use crate::types::dynamic::AccessMode; use crate::{ calc_fn_hash, calc_qualified_fn_hash, calc_qualified_var_hash, Engine, Identifier, ImmutableString, LexError, ParseError, ParseErrorType, Position, Scope, Shared, StaticVec, AST, @@ -2745,7 +2745,7 @@ fn parse_stmt( comments_pos = *pos; } - if !crate::token::is_doc_comment(comment) { + if !crate::tokenizer::is_doc_comment(comment) { unreachable!("expecting doc-comment, but gets {:?}", comment); } @@ -3278,7 +3278,7 @@ impl Engine { statements.push(Stmt::Expr(expr)); #[cfg(not(feature = "no_optimize"))] - return Ok(crate::optimize::optimize_into_ast( + return Ok(crate::optimizer::optimize_into_ast( self, _scope, statements, @@ -3363,7 +3363,7 @@ impl Engine { let (statements, _lib) = self.parse_global_level(input, state)?; #[cfg(not(feature = "no_optimize"))] - return Ok(crate::optimize::optimize_into_ast( + return Ok(crate::optimizer::optimize_into_ast( self, _scope, statements, diff --git a/src/serde/de.rs b/src/serde/de.rs index fc255f02..8c7420e2 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -1,7 +1,7 @@ //! Implement deserialization support of [`Dynamic`][crate::Dynamic] for [`serde`]. use super::str::StringSliceDeserializer; -use crate::dynamic::Union; +use crate::types::dynamic::Union; use crate::{Dynamic, EvalAltResult, ImmutableString, LexError, Position}; use serde::de::{DeserializeSeed, Error, IntoDeserializer, MapAccess, SeqAccess, Visitor}; use serde::{Deserialize, Deserializer}; diff --git a/src/serde/serialize.rs b/src/serde/serialize.rs index 560d489d..d2bd511e 100644 --- a/src/serde/serialize.rs +++ b/src/serde/serialize.rs @@ -1,6 +1,6 @@ //! Implementations of [`serde::Serialize`]. -use crate::dynamic::Union; +use crate::types::dynamic::Union; use crate::{Dynamic, ImmutableString}; use serde::ser::{Serialize, Serializer}; #[cfg(feature = "no_std")] @@ -10,7 +10,7 @@ use std::prelude::v1::*; use serde::ser::SerializeMap; #[cfg(not(feature = "no_std"))] -use crate::dynamic::Variant; +use crate::types::dynamic::Variant; impl Serialize for Dynamic { fn serialize(&self, ser: S) -> Result { diff --git a/src/token.rs b/src/tokenizer.rs similarity index 99% rename from src/token.rs rename to src/tokenizer.rs index 13144243..abc6f34f 100644 --- a/src/token.rs +++ b/src/tokenizer.rs @@ -4,7 +4,7 @@ use crate::engine::{ Precedence, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_THIS, KEYWORD_TYPE_OF, }; -use crate::fn_native::OnParseTokenCallback; +use crate::func::native::OnParseTokenCallback; use crate::{Engine, LexError, StaticVec, INT}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/dynamic.rs b/src/types/dynamic.rs similarity index 99% rename from src/dynamic.rs rename to src/types/dynamic.rs index ae530593..f401e104 100644 --- a/src/dynamic.rs +++ b/src/types/dynamic.rs @@ -1,6 +1,6 @@ //! Helper module which defines the [`Any`] trait to to allow dynamic value handling. -use crate::fn_native::SendSync; +use crate::func::native::SendSync; use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; use crate::{FnPtr, ImmutableString, INT}; #[cfg(feature = "no_std")] @@ -38,7 +38,7 @@ use instant::Instant; const CHECKED: &str = "data type was checked"; mod private { - use crate::fn_native::SendSync; + use crate::func::native::SendSync; use std::any::Any; /// A sealed trait that prevents other crates from implementing [`Variant`]. @@ -287,7 +287,7 @@ enum DynamicWriteLockInner<'d, T: Clone> { /// /// Not available under `no_closure`. #[cfg(not(feature = "no_closure"))] - Guard(crate::fn_native::LockGuard<'d, Dynamic>), + Guard(crate::func::native::LockGuard<'d, Dynamic>), } impl<'d, T: Any + Clone> Deref for DynamicWriteLock<'d, T> { @@ -1472,7 +1472,7 @@ impl Dynamic { pub fn flatten(self) -> Self { match self.0 { #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, _, _) => crate::fn_native::shared_try_take(cell).map_or_else( + Union::Shared(cell, _, _) => crate::func::native::shared_try_take(cell).map_or_else( #[cfg(not(feature = "sync"))] |cell| cell.borrow().clone(), #[cfg(feature = "sync")] @@ -1497,7 +1497,7 @@ impl Dynamic { #[cfg(not(feature = "no_closure"))] Union::Shared(_, _, _) => match std::mem::take(self).0 { Union::Shared(cell, _, _) => { - *self = crate::fn_native::shared_try_take(cell).map_or_else( + *self = crate::func::native::shared_try_take(cell).map_or_else( #[cfg(not(feature = "sync"))] |cell| cell.borrow().clone(), #[cfg(feature = "sync")] @@ -1590,7 +1590,7 @@ impl Dynamic { match self.0 { #[cfg(not(feature = "no_closure"))] Union::Shared(ref cell, _, _) => { - let value = crate::fn_native::shared_write_lock(cell); + let value = crate::func::native::shared_write_lock(cell); if (*value).type_id() != TypeId::of::() && TypeId::of::() != TypeId::of::() diff --git a/src/error.rs b/src/types/error.rs similarity index 100% rename from src/error.rs rename to src/types/error.rs diff --git a/src/fn_ptr.rs b/src/types/fn_ptr.rs similarity index 99% rename from src/fn_ptr.rs rename to src/types/fn_ptr.rs index 47197920..7e2de87c 100644 --- a/src/fn_ptr.rs +++ b/src/types/fn_ptr.rs @@ -1,6 +1,6 @@ //! The `FnPtr` type. -use crate::token::is_valid_identifier; +use crate::tokenizer::is_valid_identifier; use crate::{ Dynamic, EvalAltResult, Identifier, NativeCallContext, Position, RhaiResult, StaticVec, }; diff --git a/src/immutable_string.rs b/src/types/immutable_string.rs similarity index 99% rename from src/immutable_string.rs rename to src/types/immutable_string.rs index 83bede60..5b446eb0 100644 --- a/src/immutable_string.rs +++ b/src/types/immutable_string.rs @@ -1,6 +1,6 @@ //! The `ImmutableString` type. -use crate::fn_native::{shared_make_mut, shared_take}; +use crate::func::native::{shared_make_mut, shared_take}; use crate::{Shared, SmartString}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/types/mod.rs b/src/types/mod.rs new file mode 100644 index 00000000..ee2ff553 --- /dev/null +++ b/src/types/mod.rs @@ -0,0 +1,8 @@ +//! Module defining Rhai data types. + +pub mod dynamic; +pub mod error; +pub mod fn_ptr; +pub mod immutable_string; +pub mod parse_error; +pub mod scope; diff --git a/src/error_parsing.rs b/src/types/parse_error.rs similarity index 100% rename from src/error_parsing.rs rename to src/types/parse_error.rs diff --git a/src/scope.rs b/src/types/scope.rs similarity index 99% rename from src/scope.rs rename to src/types/scope.rs index 408922fa..e5fdd8fe 100644 --- a/src/scope.rs +++ b/src/types/scope.rs @@ -1,6 +1,6 @@ //! Module that defines the [`Scope`] type representing a function call-stack scope. -use crate::dynamic::{AccessMode, Variant}; +use super::dynamic::{AccessMode, Variant}; use crate::{Dynamic, Identifier, StaticVec}; use std::iter::FromIterator; #[cfg(feature = "no_std")]