From 0a35c4cb414dbc7c62970a8f982d410fe1daa0a9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 26 Dec 2020 17:42:19 +0800 Subject: [PATCH] Change custom precedence to Option. --- src/engine_settings.rs | 14 ++++++++++---- src/syntax.rs | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) 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,