pub fn register_custom_operator(
        &mut self,
        keyword: impl AsRef<str>,
        precedence: u8,
    ) -> Result<&mut Self, String> {
        let precedence = Precedence::new(precedence);

        if precedence.is_none() {
            return Err("precedence cannot be zero".into());
        }

        match Token::lookup_from_syntax(keyword.as_ref()) {
            // Standard identifiers, reserved keywords and custom keywords are OK
            None | Some(Token::Reserved(..)) | Some(Token::Custom(..)) => (),
            // Active standard keywords cannot be made custom
            // Disabled keywords are OK
            Some(token) if token.is_standard_keyword() => {
                if self.disabled_symbols.is_empty()
                    || !self.disabled_symbols.contains(&*token.syntax())
                {
                    return Err(format!("'{}' is a reserved keyword", keyword.as_ref()));
                }
            }
            // Active standard symbols cannot be made custom
            Some(token) if token.is_standard_symbol() => {
                if self.disabled_symbols.is_empty()
                    || !self.disabled_symbols.contains(&*token.syntax())
                {
                    return Err(format!("'{}' is a reserved operator", keyword.as_ref()));
                }
            }
            // Active standard symbols cannot be made custom
            Some(token)
                if self.disabled_symbols.is_empty()
                    || !self.disabled_symbols.contains(&*token.syntax()) =>
            {
                return Err(format!("'{}' is a reserved symbol", keyword.as_ref()))
            }
            // Disabled symbols are OK
            Some(_) => (),
        }

        // Add to custom keywords
        self.custom_keywords
            .insert(keyword.as_ref().into(), precedence);

        Ok(self)
    }
/// Register a custom operator with a precedence into the language.
///
/// The operator can be a valid identifier, a reserved symbol, a disabled operator or a disabled keyword.
///
/// The precedence cannot be zero.
///
/// # Example
///
/// ```rust
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let mut engine = Engine::new();
///
/// // Register a custom operator called '#' and give it
/// // a precedence of 160 (i.e. between +|- and *|/).
/// engine.register_custom_operator("#", 160).expect("should succeed");
///
/// // Register a binary function named '#'
/// engine.register_fn("#", |x: i64, y: i64| (x * y) - (x + y));
///
/// assert_eq!(
///     engine.eval_expression::<i64>("1 + 2 * 3 # 4 - 5 / 6")?,
///     15
/// );
/// # Ok(())
/// # }
/// ```
Register a custom operator with a precedence into the language.
The operator can be a valid identifier, a reserved symbol, a disabled operator or a disabled keyword.

The precedence cannot be zero.
Register a custom operator with a precedence into the language . The operator can be a valid identifier a reserved symbol a disabled operator or a disabled keyword . The precedence cannot be zero .
pub fn register_custom_operator(
        &mut self,
        keyword: impl AsRef<str>,
        precedence: u8,
    ) -> Result<&mut Self, String> {
        let precedence = Precedence::new(precedence);
        if precedence.is_none() {
            return Err("precedence cannot be zero".into());
        }
        match Token::lookup_from_syntax(keyword.as_ref()) {
            // Standard identifiers, reserved keywords and custom keywords are OK
            None | Some(Token::Reserved(..)) | Some(Token::Custom(..)) => (),
            // Active standard keywords cannot be made custom
            // Disabled keywords are OK
            Some(token) if token.is_standard_keyword() => {
                if self.disabled_symbols.is_empty()
                    || !self.disabled_symbols.contains(&*token.syntax())
                {
                    return Err(format!("'{}' is a reserved keyword", keyword.as_ref()));
                }
            }
            // Active standard symbols cannot be made custom
            Some(token) if token.is_standard_symbol() => {
                if self.disabled_symbols.is_empty()
                    || !self.disabled_symbols.contains(&*token.syntax())
                {
                    return Err(format!("'{}' is a reserved operator", keyword.as_ref()));
                }
            }
            // Active standard symbols cannot be made custom
            Some(token)
                if self.disabled_symbols.is_empty()
                    || !self.disabled_symbols.contains(&*token.syntax()) =>
            {
                return Err(format!("'{}' is a reserved symbol", keyword.as_ref()))
            }
            // Disabled symbols are OK
            Some(_) => (),
        }
        // Add to custom keywords
        self.custom_keywords
            .insert(keyword.as_ref().into(), precedence);
        Ok(self)
    }
pub fn register_custom_operator ( & mut self , keyword : impl AsRef < str > , precedence : u8 , ) -> Result < & mut Self , String > { let precedence = Precedence :: new ( precedence ) ; if precedence . is_none ( ) { return Err ( " " . into ( ) ) ; } match Token :: lookup_from_syntax ( keyword . as_ref ( ) ) { // Standard identifiers, reserved keywords and custom keywords are OK None | Some ( Token :: Reserved ( .. ) ) | Some ( Token :: Custom ( .. ) ) => ( ) , // Active standard keywords cannot be made custom // Disabled keywords are OK Some ( token ) if token . is_standard_keyword ( ) => { if self . disabled_symbols . is_empty ( ) || ! self . disabled_symbols . contains ( & * token . syntax ( ) ) { return Err ( format ! ( " " keyword as_ref ( ) ) ) ; } } // Active standard symbols cannot be made custom Some ( token ) if token . is_standard_symbol ( ) => { if self . disabled_symbols . is_empty ( ) || ! self . disabled_symbols . contains ( & * token . syntax ( ) ) { return Err ( format ! ( " " keyword as_ref ( ) ) ) ; } } // Active standard symbols cannot be made custom Some ( token ) if self . disabled_symbols . is_empty ( ) || ! self . disabled_symbols . contains ( & * token . syntax ( ) ) => { return Err ( format ! ( " " keyword as_ref ( ) ) ) } // Disabled symbols are OK Some ( _ ) => ( ) , } // Add to custom keywords self . custom_keywords . insert ( keyword . as_ref ( ) . into ( ) , precedence ) ; Ok ( self ) }
Register a custom operator with a precedence into the language.
Register a custom operator with a precedence into the language .
&mut self
keyword
impl AsRef<str>
precedence
u8
example
None
fn main() -> result<(), box<rhai::evalaltresult>> {
use rhai::Engine.

let mut engine = Engine::new().

Register a custom operator called '#' and give it
a precedence of 160 .
engine.register_custom_operator("#", 160).expect("should succeed").

Register a binary function named '#'
engine.register_fn("#", |x: i64, y: i64| (x * y) - (x + y)).


use rhai :: Engine . let mut engine = Engine :: new () . Register a custom operator called ' # ' and give it a precedence of 160 . engine . register_custom_operator ( " # " 160 ) . expect ( " should succeed " ) . Register a binary function named ' # ' engine . register_fn ( " # " |x : i64 y : i64| ( x * y ) - ( x + y )) .
ok(())
None
}
None