diff --git a/src/engine_settings.rs b/src/engine_settings.rs index 56e9104c..e7757636 100644 --- a/src/engine_settings.rs +++ b/src/engine_settings.rs @@ -1,6 +1,6 @@ //! Configuration settings for [`Engine`]. -use crate::stdlib::{format, string::String}; +use crate::stdlib::{format, num::NonZeroU8, string::String}; use crate::token::{is_valid_identifier, Token}; use crate::Engine; @@ -214,10 +214,12 @@ impl Engine { self.disabled_symbols.insert(symbol.into()); self } - /// Register a custom operator into the language. + /// Register a custom operator with a precedence into the language. /// /// The operator must be a valid identifier (i.e. it cannot be a symbol). /// + /// The precedence cannot be zero. + /// /// # Example /// /// ```rust @@ -245,6 +247,11 @@ impl Engine { keyword: &str, precedence: u8, ) -> Result<&mut Self, String> { + let precedence = NonZeroU8::new(precedence); + + if precedence.is_none() { + return Err("precedence cannot be zero".into()); + } if !is_valid_identifier(keyword.chars()) { return Err(format!("not a valid identifier: '{}'", keyword).into()); } @@ -259,8 +266,7 @@ impl Engine { } // Add to custom keywords - self.custom_keywords - .insert(keyword.into(), Some(precedence)); + self.custom_keywords.insert(keyword.into(), precedence); Ok(self) } diff --git a/src/syntax.rs b/src/syntax.rs index 4ceec8f9..55db8cce 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -208,7 +208,7 @@ impl Engine { /// * `parse` is the parsing function. /// * `func` is the implementation function. /// - /// All custom keywords must be manually registered via [`register_custom_operator`][Engine::register_custom_operator]. + /// All custom keywords must be manually registered via [`Engine::register_custom_operator`]. /// Otherwise, custom keywords won't be recognized. pub fn register_custom_syntax_raw( &mut self,