From: Huck Boles Date: Sat, 20 May 2023 21:35:34 +0000 (-0500) Subject: added: UnreachableRule error + made parser functions return Result X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=9854e1d509667cd1179d02bfacdd31e56efb554b;p=metaforge.git added: UnreachableRule error + made parser functions return Result --- diff --git a/src/error.rs b/src/error.rs index 8bd0893..171b849 100644 --- a/src/error.rs +++ b/src/error.rs @@ -23,6 +23,8 @@ pub enum MetaError { UndefinedExpand { val: String, path: String }, #[error("undefined call to default.meta: {pattern}\n\tin {path}")] UndefinedDefault { pattern: String, path: String }, + #[error("the parser cannot resolve this input: {input}")] + UnreachableRule { input: String }, #[error(transparent)] MetaError(#[from] Box), #[error(transparent)] diff --git a/src/parser/array.rs b/src/parser/array.rs index bb72a23..8358f58 100644 --- a/src/parser/array.rs +++ b/src/parser/array.rs @@ -1,19 +1,20 @@ -use crate::{Rule, Scope}; +use crate::{MetaError, Rule, Scope}; +use eyre::Result; use pest::iterators::{Pair, Pairs}; use std::collections::HashMap; -pub fn parse_array_defs(pairs: Pairs) -> HashMap> { +pub fn parse_array_defs(pairs: Pairs) -> Result>> { let mut map = HashMap::new(); for pair in pairs { if Rule::assign == pair.as_rule() { - let (key, val) = parse_assign_array(pair); + let (key, val) = parse_assign_array(pair)?; map.insert(key, val); } } - map + Ok(map) } -fn parse_assign_array(pair: Pair) -> (Scope, Vec) { +fn parse_assign_array(pair: Pair) -> Result<(Scope, Vec)> { let mut key = ""; let mut val = Vec::default(); let mut global = true; @@ -29,14 +30,19 @@ fn parse_assign_array(pair: Pair) -> (Scope, Vec) { } Rule::key => key = pair.as_str(), Rule::value => val = parse_array(pair.into_inner()), - _ => unreachable!(), + _ => { + return Err(MetaError::UnreachableRule { + input: pair.to_string(), + } + .into()) + } } } if global { - (Scope::into_global(key), val) + Ok((Scope::into_global(key), val)) } else { - (Scope::into_local(key), val) + Ok((Scope::into_local(key), val)) } } diff --git a/src/parser/def_block.rs b/src/parser/def_block.rs index df5485d..26333ef 100644 --- a/src/parser/def_block.rs +++ b/src/parser/def_block.rs @@ -1,19 +1,20 @@ -use crate::{Rule, Scope}; +use crate::{MetaError, Rule, Scope}; +use eyre::Result; use pest::iterators::{Pair, Pairs}; use std::collections::HashMap; -pub fn parse_defs(pairs: Pairs) -> HashMap { +pub fn parse_defs(pairs: Pairs) -> Result> { let mut map = HashMap::new(); for pair in pairs { if Rule::assign == pair.as_rule() { - let (key, val) = parse_assign(pair); + let (key, val) = parse_assign(pair)?; map.insert(key, val.to_string()); } } - map + Ok(map) } -fn parse_assign(pair: Pair) -> (Scope, &str) { +fn parse_assign(pair: Pair) -> Result<(Scope, &str)> { let mut key = ""; let mut val = ""; let mut global = true; @@ -36,7 +37,12 @@ fn parse_assign(pair: Pair) -> (Scope, &str) { } } // nothing else is an acceptable assignment - _ => unreachable!(), + _ => { + return Err(MetaError::UnreachableRule { + input: pair.to_string(), + } + .into()) + } } } @@ -49,8 +55,8 @@ fn parse_assign(pair: Pair) -> (Scope, &str) { } if global { - (Scope::into_global(key), val) + Ok((Scope::into_global(key), val)) } else { - (Scope::into_local(key), val) + Ok((Scope::into_local(key), val)) } }