From 1d92666865b35341e076efbefddf6e73b5e1542e Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 7 Sep 2022 15:53:06 +0200 Subject: Add support for recoverable syntax errors --- src/state.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index 3294a2f..e8bd17a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,10 +2,15 @@ use crate::construct; use crate::tokenizer::Tokenizer; +use alloc::string::{String, ToString}; /// Result of a state. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub enum State { + /// Syntax error. + /// + /// Only used by MDX. + Error(String), /// Move to [`Name`][] next. Next(Name), /// Retry in [`Name`][]. @@ -16,6 +21,24 @@ pub enum State { Nok, } +impl State { + /// Turn a final state into a result. + /// + /// This doesn’t work on future states ([`State::Next`], [`State::Retry`]), + /// or on an attempt ([`State::Nok`]). + /// + /// But it turns the final result into an error if crashed. + pub fn to_result(&self) -> Result<(), String> { + match self { + State::Nok | State::Next(_) | State::Retry(_) => { + unreachable!("cannot turn intermediate state into result") + } + State::Ok => Ok(()), + State::Error(x) => Err(x.to_string()), + } + } +} + /// Names of states to move to. #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[allow(clippy::enum_variant_names)] -- cgit