Fix definitions API code styles and formatting.

This commit is contained in:
Stephen Chung 2022-07-26 22:38:40 +08:00
parent a891b54d0f
commit 8e21c4727b
10 changed files with 314 additions and 108 deletions

View File

@ -15,10 +15,19 @@ Bug fixes
New features New features
------------ ------------
* A new feature, `no_custom_syntax`, is added to remove custom syntax support from Rhai for applications that do not require it (which should be most). ### New feature flag
* A new feature flag, `no_custom_syntax`, is added to remove custom syntax support from Rhai for applications that do not require it (which should be most).
### Module documentation
* Comment lines beginning with `//!` (requires the `metadata` feature) are now collected as the script file's _module documentation_. * Comment lines beginning with `//!` (requires the `metadata` feature) are now collected as the script file's _module documentation_.
* `AST` and `Module` have methods to access and manipulate documentation. * `AST` and `Module` have methods to access and manipulate documentation.
### Output definition files
* An API is added to automatically generate definition files from a fully-configured `Engine`, for use with the Rhai Language Server.
Enhancements Enhancements
------------ ------------

View File

@ -4,18 +4,18 @@ Sample Applications
Standard Examples Standard Examples
----------------- -----------------
| Example | Description | | Example | Description |
| --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | | --------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`arrays_and_structs`](arrays_and_structs.rs) | shows how to register a Rust type and using it with arrays | | [`arrays_and_structs`](arrays_and_structs.rs) | shows how to register a Rust type and using it with arrays |
| [`callback`](callback.rs) | shows how to store a Rhai closure and call it later within Rust | | [`callback`](callback.rs) | shows how to store a Rhai closure and call it later within Rust |
| [`custom_types_and_methods`](custom_types_and_methods.rs) | shows how to register a Rust type and methods/getters/setters for it | | [`custom_types_and_methods`](custom_types_and_methods.rs) | shows how to register a Rust type and methods/getters/setters for it |
| [`hello`](hello.rs) | simple example that evaluates an expression and prints the result | | [`definitions`](./definitions) | shows how to generate definition files for use with the [Rhai Language Server](https://github.com/rhaiscript/lsp) (requires the `metadata` feature) |
| [`reuse_scope`](reuse_scope.rs) | evaluates two pieces of code in separate runs, but using a common `Scope` | | [`hello`](hello.rs) | simple example that evaluates an expression and prints the result |
| [`serde`](serde.rs) | example to serialize and deserialize Rust types with [`serde`](https://crates.io/crates/serde) (requires the `serde` feature) | | [`reuse_scope`](reuse_scope.rs) | evaluates two pieces of code in separate runs, but using a common `Scope` |
| [`simple_fn`](simple_fn.rs) | shows how to register a simple Rust function | | [`serde`](serde.rs) | example to serialize and deserialize Rust types with [`serde`](https://crates.io/crates/serde) (requires the `serde` feature) |
| [`strings`](strings.rs) | shows different ways to register Rust functions taking string arguments | | [`simple_fn`](simple_fn.rs) | shows how to register a simple Rust function |
| [`threading`](threading.rs) | shows how to communicate with an `Engine` running in a separate thread via an MPSC channel | | [`strings`](strings.rs) | shows different ways to register Rust functions taking string arguments |
| [`definitions`](./definitions) | shows how to generate definition files for manual inspection or for use with [Rhai LSP](https://github.com/rhaiscript/lsp), requires the `metadata` feature | | [`threading`](threading.rs) | shows how to communicate with an `Engine` running in a separate thread via an MPSC channel |
Scriptable Event Handler With State Examples Scriptable Event Handler With State Examples

View File

@ -249,3 +249,11 @@ op +=(Blob, Blob);
op +=(Blob, i64); op +=(Blob, i64);
op +=(Blob, char); op +=(Blob, char);
op +=(Blob, String); op +=(Blob, String);
op in(?, Array) -> bool;
op in(String, String) -> bool;
op in(char, String) -> bool;
op in(int, Range<int>) -> bool;
op in(int, RangeInclusive<int>) -> bool;
op in(String, Map) -> bool;
op in(int, Blob) -> bool;

View File

@ -112,7 +112,7 @@ fn curry(fn_ptr: FnPtr, ...args: ?) -> FnPtr;
/// print(is_def_fn("foo", 0)); // prints false /// print(is_def_fn("foo", 0)); // prints false
/// print(is_def_fn("bar", 1)); // prints false /// print(is_def_fn("bar", 1)); // prints false
/// ``` /// ```
fn is_def_fn(fn_name: String, num_params: i64) -> bool; fn is_def_fn(fn_name: String, num_params: int) -> bool;
/// Return `true` if a variable matching a specified name is defined. /// Return `true` if a variable matching a specified name is defined.
/// ///
@ -162,9 +162,100 @@ fn is_shared(variable: ?) -> bool;
/// ``` /// ```
fn eval(script: String) -> ?; fn eval(script: String) -> ?;
/// Return `true` if the string contains another string.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let x = "hello world!";
///
/// // The 'in' operator calls 'contains' in the background
/// if "world" in x {
/// print("found!");
/// }
/// ```
fn contains(string: String, find: String) -> bool; fn contains(string: String, find: String) -> bool;
fn contains(range: Range<i64>, value: i64) -> bool;
fn contains(range: RangeInclusive<i64>, value: i64) -> bool; /// Return `true` if the string contains a character.
fn contains(map: Map, string: String) -> bool; ///
fn contains(blob: Blob, value: i64) -> bool; /// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let x = "hello world!";
///
/// // The 'in' operator calls 'contains' in the background
/// if 'w' in x {
/// print("found!");
/// }
/// ```
fn contains(string: String, ch: char) -> bool; fn contains(string: String, ch: char) -> bool;
/// Return `true` if a value falls within the exclusive range.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let r = 1..100;
///
/// // The 'in' operator calls 'contains' in the background
/// if 42 in r {
/// print("found!");
/// }
/// ```
fn contains(range: Range<int>, value: int) -> bool;
/// Return `true` if a value falls within the inclusive range.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let r = 1..=100;
///
/// // The 'in' operator calls 'contains' in the background
/// if 42 in r {
/// print("found!");
/// }
/// ```
fn contains(range: RangeInclusive<int>, value: int) -> bool;
/// Return `true` if a key exists within the object map.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let m = #{a:1, b:2, c:3};
///
/// // The 'in' operator calls 'contains' in the background
/// if "c" in m {
/// print("found!");
/// }
/// ```
fn contains(map: Map, string: String) -> bool;
/// Return `true` if a value is found within the BLOB.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let b = blob();
///
/// b += 1; b += 2; b += 3; b += 4; b += 5;
///
/// // The 'in' operator calls 'contains' in the background
/// if 3 in b {
/// print("found!");
/// }
/// ```
fn contains(blob: Blob, value: int) -> bool;

View File

@ -1,5 +1,4 @@
module general_kenobi; module general_kenobi;
/// Returns a string where `hello there ` /// Returns a string where "hello there" is repeated `n` times.
/// is repeated `n` times.
fn hello_there(n: i64) -> String; fn hello_there(n: i64) -> String;

View File

@ -1,21 +1,20 @@
use rhai::{plugin::*, Engine, Scope}; use rhai::plugin::*;
use rhai::{Engine, EvalAltResult, Scope};
#[export_module] #[export_module]
pub mod general_kenobi { pub mod general_kenobi {
/// Returns a string where `hello there ` /// Returns a string where "hello there" is repeated `n` times.
/// is repeated `n` times.
pub fn hello_there(n: i64) -> String { pub fn hello_there(n: i64) -> String {
use std::convert::TryInto; use std::convert::TryInto;
"hello there ".repeat(n.try_into().unwrap()) "hello there ".repeat(n.try_into().unwrap())
} }
} }
fn main() { fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
let mut scope = Scope::new(); let mut scope = Scope::new();
// This variable will also show up in the definitions, // This variable will also show up in the definitions, since it will be part of the scope.
// since it will be part of the scope.
scope.push("hello_there", "hello there"); scope.push("hello_there", "hello there");
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
@ -28,17 +27,15 @@ fn main() {
engine.register_fn("minus", |a: i64, b: i64| a - b); engine.register_fn("minus", |a: i64, b: i64| a - b);
} }
engine engine.run_with_scope(
.eval_with_scope::<()>( &mut scope,
&mut scope, "hello_there = general_kenobi::hello_there(4 minus 2);",
r#" )?;
hello_there = general_kenobi::hello_there(4 minus 2);
"#,
)
.unwrap();
engine engine
.definitions_with_scope(&scope) .definitions_with_scope(&scope)
.write_to_dir("examples/definitions/.rhai/definitions") .write_to_dir("examples/definitions/.rhai/definitions")
.unwrap(); .unwrap();
Ok(())
} }

View File

@ -249,3 +249,11 @@ op +=(Blob, Blob);
op +=(Blob, i64); op +=(Blob, i64);
op +=(Blob, char); op +=(Blob, char);
op +=(Blob, String); op +=(Blob, String);
op in(?, Array) -> bool;
op in(String, String) -> bool;
op in(char, String) -> bool;
op in(int, Range<int>) -> bool;
op in(int, RangeInclusive<int>) -> bool;
op in(String, Map) -> bool;
op in(int, Blob) -> bool;

View File

@ -112,7 +112,7 @@ fn curry(fn_ptr: FnPtr, ...args: ?) -> FnPtr;
/// print(is_def_fn("foo", 0)); // prints false /// print(is_def_fn("foo", 0)); // prints false
/// print(is_def_fn("bar", 1)); // prints false /// print(is_def_fn("bar", 1)); // prints false
/// ``` /// ```
fn is_def_fn(fn_name: String, num_params: i64) -> bool; fn is_def_fn(fn_name: String, num_params: int) -> bool;
/// Return `true` if a variable matching a specified name is defined. /// Return `true` if a variable matching a specified name is defined.
/// ///
@ -162,9 +162,100 @@ fn is_shared(variable: ?) -> bool;
/// ``` /// ```
fn eval(script: String) -> ?; fn eval(script: String) -> ?;
/// Return `true` if the string contains another string.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let x = "hello world!";
///
/// // The 'in' operator calls 'contains' in the background
/// if "world" in x {
/// print("found!");
/// }
/// ```
fn contains(string: String, find: String) -> bool; fn contains(string: String, find: String) -> bool;
fn contains(range: Range<i64>, value: i64) -> bool;
fn contains(range: RangeInclusive<i64>, value: i64) -> bool; /// Return `true` if the string contains a character.
fn contains(map: Map, string: String) -> bool; ///
fn contains(blob: Blob, value: i64) -> bool; /// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let x = "hello world!";
///
/// // The 'in' operator calls 'contains' in the background
/// if 'w' in x {
/// print("found!");
/// }
/// ```
fn contains(string: String, ch: char) -> bool; fn contains(string: String, ch: char) -> bool;
/// Return `true` if a value falls within the exclusive range.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let r = 1..100;
///
/// // The 'in' operator calls 'contains' in the background
/// if 42 in r {
/// print("found!");
/// }
/// ```
fn contains(range: Range<int>, value: int) -> bool;
/// Return `true` if a value falls within the inclusive range.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let r = 1..=100;
///
/// // The 'in' operator calls 'contains' in the background
/// if 42 in r {
/// print("found!");
/// }
/// ```
fn contains(range: RangeInclusive<int>, value: int) -> bool;
/// Return `true` if a key exists within the object map.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let m = #{a:1, b:2, c:3};
///
/// // The 'in' operator calls 'contains' in the background
/// if "c" in m {
/// print("found!");
/// }
/// ```
fn contains(map: Map, string: String) -> bool;
/// Return `true` if a value is found within the BLOB.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let b = blob();
///
/// b += 1; b += 2; b += 3; b += 4; b += 5;
///
/// // The 'in' operator calls 'contains' in the background
/// if 3 in b {
/// print("found!");
/// }
/// ```
fn contains(blob: Blob, value: int) -> bool;

View File

@ -1,21 +1,18 @@
use crate::{ //! Module that defines functions to output definition files for [`Engine`].
module::FuncInfo, plugin::*, tokenizer::is_valid_function_name, Engine, Module, Scope, #![cfg(feature = "metadata")]
};
use core::{cmp::Ordering, fmt, iter}; use crate::module::FuncInfo;
use crate::plugin::*;
use crate::tokenizer::is_valid_function_name;
use crate::{Engine, Module, Scope};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{borrow::Cow, cmp::Ordering, fmt};
#[cfg(feature = "no_std")]
use alloc::borrow::Cow;
#[cfg(not(feature = "no_std"))]
use std::borrow::Cow;
impl Engine { impl Engine {
/// Return [`Definitions`] that can be used to /// Return [`Definitions`] that can be used to generate definition files for the [`Engine`].
/// generate definition files that contain all /// Exported under the `metadata` feature only.
/// the visible items in the engine.
/// ///
/// # Example /// # Example
/// ///
@ -23,12 +20,15 @@ impl Engine {
/// # use rhai::Engine; /// # use rhai::Engine;
/// # fn main() -> std::io::Result<()> { /// # fn main() -> std::io::Result<()> {
/// let engine = Engine::new(); /// let engine = Engine::new();
///
/// engine /// engine
/// .definitions() /// .definitions()
/// .write_to_dir(".rhai/definitions")?; /// .write_to_dir(".rhai/definitions")?;
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
#[inline(always)]
#[must_use]
pub fn definitions(&self) -> Definitions { pub fn definitions(&self) -> Definitions {
Definitions { Definitions {
engine: self, engine: self,
@ -36,10 +36,9 @@ impl Engine {
} }
} }
/// Return [`Definitions`] that can be used to /// Return [`Definitions`] that can be used to generate definition files for the [`Engine`] and
/// generate definition files that contain all /// the given [`Scope`].
/// the visible items in the engine and the /// Exported under the `metadata` feature only.
/// given scope.
/// ///
/// # Example /// # Example
/// ///
@ -54,6 +53,8 @@ impl Engine {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
#[inline(always)]
#[must_use]
pub fn definitions_with_scope<'e>(&'e self, scope: &'e Scope<'e>) -> Definitions<'e> { pub fn definitions_with_scope<'e>(&'e self, scope: &'e Scope<'e>) -> Definitions<'e> {
Definitions { Definitions {
engine: self, engine: self,
@ -62,19 +63,20 @@ impl Engine {
} }
} }
/// Definitions helper that is used to generate /// Definitions helper type to generate definition files based on the contents of an [`Engine`].
/// definition files based on the contents of an [`Engine`].
#[must_use] #[must_use]
pub struct Definitions<'e> { pub struct Definitions<'e> {
/// The [`Engine`].
engine: &'e Engine, engine: &'e Engine,
/// Optional [`Scope`] to include.
scope: Option<&'e Scope<'e>>, scope: Option<&'e Scope<'e>>,
} }
impl<'e> Definitions<'e> { impl<'e> Definitions<'e> {
/// Write all the definition files returned from [`iter_files`] to a directory. /// Output all definition files returned from [`iter_files`][Definitions::iter_files] to a
/// specified directory.
/// ///
/// This function will create the directory path if it does not yet exist, /// This function creates the directory if it does not exist, and overrides any existing files.
/// it will also override any existing files as needed.
#[cfg(all(not(feature = "no_std"), not(target_family = "wasm")))] #[cfg(all(not(feature = "no_std"), not(target_family = "wasm")))]
pub fn write_to_dir(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> { pub fn write_to_dir(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
use std::fs; use std::fs;
@ -98,6 +100,7 @@ impl<'e> Definitions<'e> {
fs::write(path.join("__scope__.d.rhai"), self.scope())?; fs::write(path.join("__scope__.d.rhai"), self.scope())?;
} }
#[cfg(not(feature = "no_module"))]
for (name, decl) in self.modules() { for (name, decl) in self.modules() {
fs::write(path.join(format!("{name}.d.rhai")), decl)?; fs::write(path.join(format!("{name}.d.rhai")), decl)?;
} }
@ -105,9 +108,11 @@ impl<'e> Definitions<'e> {
Ok(()) Ok(())
} }
/// Iterate over the generated definition files. /// Iterate over generated definition files.
/// ///
/// The returned iterator yields all the definition files as (filename, content) pairs. /// The returned iterator yields all definition files as (filename, content) pairs.
#[inline]
#[must_use]
pub fn iter_files(&self) -> impl Iterator<Item = (String, String)> + '_ { pub fn iter_files(&self) -> impl Iterator<Item = (String, String)> + '_ {
IntoIterator::into_iter([ IntoIterator::into_iter([
( (
@ -120,23 +125,27 @@ impl<'e> Definitions<'e> {
), ),
("__static__.d.rhai".to_string(), self.static_module()), ("__static__.d.rhai".to_string(), self.static_module()),
]) ])
.chain(iter::from_fn(move || {
if self.scope.is_some() {
Some(("__scope__.d.rhai".to_string(), self.scope()))
} else {
None
}
}))
.chain( .chain(
self.modules() self.scope
.map(|(name, def)| (format!("{name}.d.rhai"), def)), .iter()
.map(move |_| ("__scope__.d.rhai".to_string(), self.scope())),
)
.chain(
#[cfg(not(feature = "no_module"))]
{
self.modules()
.map(|(name, def)| (format!("{name}.d.rhai"), def))
},
#[cfg(feature = "no_module")]
{
std::iter::empty()
},
) )
} }
/// Return the definitions for the globally available /// Return definitions for all globally available functions.
/// items of the engine.
/// ///
/// The definitions will always start with `module static;`. /// Always starts with `module static;`.
#[must_use] #[must_use]
pub fn static_module(&self) -> String { pub fn static_module(&self) -> String {
let mut s = String::from("module static;\n\n"); let mut s = String::from("module static;\n\n");
@ -153,11 +162,9 @@ impl<'e> Definitions<'e> {
s s
} }
/// Return the definitions for the available /// Return definitions for all items inside the [`Scope`], if any.
/// items of the scope.
/// ///
/// The definitions will always start with `module static;`, /// Always starts with `module static;` even if the [`Scope`] is empty or none was provided.
/// even if the scope is empty or none was provided.
#[must_use] #[must_use]
pub fn scope(&self) -> String { pub fn scope(&self) -> String {
let mut s = String::from("module static;\n\n"); let mut s = String::from("module static;\n\n");
@ -169,44 +176,41 @@ impl<'e> Definitions<'e> {
s s
} }
/// Return module name and definition pairs for each registered module. /// Return a (module name, definitions) pair for each registered static [module][Module].
/// ///
/// The definitions will always start with `module <module name>;`. /// Not available under `no_module`.
/// ///
/// If the feature `no_module` is enabled, this will yield no elements. /// Always starts with `module <module name>;`.
#[cfg(not(feature = "no_module"))]
#[must_use]
pub fn modules(&self) -> impl Iterator<Item = (String, String)> + '_ { pub fn modules(&self) -> impl Iterator<Item = (String, String)> + '_ {
#[cfg(not(feature = "no_module"))] let mut m = self
let m = { .engine
let mut m = self .global_sub_modules
.engine .iter()
.global_sub_modules .map(move |(name, module)| {
.iter() (
.map(move |(name, module)| { name.to_string(),
( format!("module {name};\n\n{}", module.definition(self)),
name.to_string(), )
format!("module {name};\n\n{}", module.definition(self)), })
) .collect::<Vec<_>>();
})
.collect::<Vec<_>>();
m.sort_by(|(name1, _), (name2, _)| name1.cmp(name2)); m.sort_by(|(name1, _), (name2, _)| name1.cmp(name2));
m
};
#[cfg(feature = "no_module")]
let m = Vec::new();
m.into_iter() m.into_iter()
} }
} }
impl Module { impl Module {
/// Return definitions for all items inside the [`Module`].
fn definition(&self, def: &Definitions) -> String { fn definition(&self, def: &Definitions) -> String {
let mut s = String::new(); let mut s = String::new();
self.write_definition(&mut s, def).unwrap(); self.write_definition(&mut s, def).unwrap();
s s
} }
/// Output definitions for all items inside the [`Module`].
fn write_definition(&self, writer: &mut dyn fmt::Write, def: &Definitions) -> fmt::Result { fn write_definition(&self, writer: &mut dyn fmt::Write, def: &Definitions) -> fmt::Result {
let mut first = true; let mut first = true;
@ -259,6 +263,7 @@ impl Module {
} }
impl FuncInfo { impl FuncInfo {
/// Output definitions for a function.
fn write_definition( fn write_definition(
&self, &self,
writer: &mut dyn fmt::Write, writer: &mut dyn fmt::Write,
@ -324,14 +329,11 @@ impl FuncInfo {
/// We have to transform some of the types. /// We have to transform some of the types.
/// ///
/// This is highly inefficient and is currently based on /// This is highly inefficient and is currently based on trial and error with the core packages.
/// trial and error with the core packages.
/// ///
/// It tries to flatten types, removing `&` and `&mut`, /// It tries to flatten types, removing `&` and `&mut`, and paths, while keeping generics.
/// and paths, while keeping generics.
/// ///
/// Associated generic types are also rewritten into regular /// Associated generic types are also rewritten into regular generic type parameters.
/// generic type parameters.
fn def_type_name<'a>(ty: &'a str, engine: &'a Engine) -> Cow<'a, str> { fn def_type_name<'a>(ty: &'a str, engine: &'a Engine) -> Cow<'a, str> {
let ty = engine.format_type_name(ty).replace("crate::", ""); let ty = engine.format_type_name(ty).replace("crate::", "");
let ty = ty.strip_prefix("&mut").unwrap_or(&*ty).trim(); let ty = ty.strip_prefix("&mut").unwrap_or(&*ty).trim();
@ -353,6 +355,7 @@ fn def_type_name<'a>(ty: &'a str, engine: &'a Engine) -> Cow<'a, str> {
} }
impl Scope<'_> { impl Scope<'_> {
/// Return definitions for all items inside the [`Scope`].
fn write_definition(&self, writer: &mut dyn fmt::Write) -> fmt::Result { fn write_definition(&self, writer: &mut dyn fmt::Write) -> fmt::Result {
let mut first = true; let mut first = true;
for (name, constant, _) in self.iter_raw() { for (name, constant, _) in self.iter_raw() {

View File

@ -568,7 +568,7 @@ pub enum Token {
Reserved(SmartString), Reserved(SmartString),
/// A custom keyword. /// A custom keyword.
/// ///
/// Not available under the `no_custom_syntax` feature. /// Not available under `no_custom_syntax`.
#[cfg(not(feature = "no_custom_syntax"))] #[cfg(not(feature = "no_custom_syntax"))]
Custom(SmartString), Custom(SmartString),
/// End of the input stream. /// End of the input stream.