Skip to content

Commit

Permalink
chore: refactor grit error (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
seren5240 committed Aug 12, 2024
1 parent e13eec0 commit 234aa75
Show file tree
Hide file tree
Showing 83 changed files with 735 additions and 540 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions crates/core/src/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use grit_pattern_matcher::{
ResolvedPattern, State,
},
};
use grit_util::{AnalysisLogs, AstNode, Language};
use grit_util::{error::GritResult, AnalysisLogs, AstNode, Language};
use marzano_language::language::{FieldId, LeafEquivalenceClass, MarzanoLanguage, SortId};
use marzano_util::node_with_source::NodeWithSource;

Expand Down Expand Up @@ -58,7 +58,7 @@ impl Matcher<MarzanoQueryContext> for ASTNode {
init_state: &mut State<'a, MarzanoQueryContext>,
context: &'a MarzanoContext,
logs: &mut AnalysisLogs,
) -> Result<bool> {
) -> GritResult<bool> {
let Some(binding) = binding.get_last_binding() else {
return Ok(false);
};
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Matcher<MarzanoQueryContext> for AstLeafNode {
_state: &mut State<'a, MarzanoQueryContext>,
_context: &'a MarzanoContext<'a>,
_logs: &mut AnalysisLogs,
) -> Result<bool> {
) -> GritResult<bool> {
let Some(node) = binding.get_last_binding().and_then(Binding::singleton) else {
return Ok(false);
};
Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/clean.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use grit_util::{traverse, AstNode, Language, Order, Replacement};
use grit_util::{error::GritResult, traverse, AstNode, Language, Order, Replacement};
use itertools::Itertools;

pub fn merge_ranges(ranges: Vec<Replacement>) -> Vec<Replacement> {
Expand Down Expand Up @@ -32,7 +31,7 @@ pub fn merge_ranges(ranges: Vec<Replacement>) -> Vec<Replacement> {
pub(crate) fn replace_cleaned_ranges(
replacement_ranges: Vec<Replacement>,
src: &str,
) -> Result<Option<String>> {
) -> GritResult<Option<String>> {
if replacement_ranges.is_empty() {
return Ok(None);
}
Expand Down
5 changes: 4 additions & 1 deletion crates/core/src/equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub fn are_equivalent(node1: &NodeWithSource, node2: &NodeWithSource) -> bool {
// If the source is identical, we consider the nodes equivalent.
// This covers most cases of constant nodes.
// We may want a more precise check here eventually, but this is a good start.
if node1.text() == node2.text() {
if node1
.text()
.is_ok_and(|text1| node2.text().is_ok_and(|text2| text1 == text2))
{
return true;
}

Expand Down
47 changes: 31 additions & 16 deletions crates/core/src/foreign_function_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
marzano_context::MarzanoContext, marzano_resolved_pattern::MarzanoResolvedPattern,
problem::MarzanoQueryContext,
};
use anyhow::{bail, Result};
use grit_pattern_matcher::{
constant::Constant,
context::ExecContext,
Expand All @@ -11,6 +10,8 @@ use grit_pattern_matcher::{
ResolvedPattern, State, Variable,
},
};
use grit_util::error::GritPatternError;
use grit_util::error::GritResult;
use grit_util::AnalysisLogs;
#[cfg(feature = "external_functions")]
use marzano_externals::function::ExternalFunction;
Expand Down Expand Up @@ -61,7 +62,7 @@ impl FunctionDefinition<MarzanoQueryContext> for ForeignFunctionDefinition {
context: &'a MarzanoContext<'a>,
args: &'a [Option<Pattern<MarzanoQueryContext>>],
logs: &mut AnalysisLogs,
) -> Result<FuncEvaluation<MarzanoQueryContext>> {
) -> GritResult<FuncEvaluation<MarzanoQueryContext>> {
let param_names = self
.params
.iter()
Expand All @@ -75,38 +76,50 @@ impl FunctionDefinition<MarzanoQueryContext> for ForeignFunctionDefinition {
match r {
Some(r) => match r.text(&state.files, context.language()) {
Ok(t) => cow_resolved.push(t),
Err(e) => bail!("failed to get text from resolved pattern: {}", e),
Err(e) => {
return Err(GritPatternError::new(format!(
"failed to get text from resolved pattern: {}",
e,
)))
}
},
None => bail!("Foreign function references unbound variable"),
None => {
return Err(GritPatternError::new(
"Foreign function references unbound variable",
))
}
}
}

let resolved_str: Vec<&str> = cow_resolved.iter().map(Cow::as_ref).collect();

// START Simple externalized version
#[cfg(all(feature = "external_functions_ffi", target_arch = "wasm32"))]
let result = context.exec_external(&self.code, param_names, &resolved_str)?;
let result = context
.exec_external(&self.code, param_names, &resolved_str)
.map_err(|e| GritPatternError::new(e.to_string()))?;

// END Simple externalized version

// START embedded version
// Really, we should compile ahead of time and then call the compiled function
// But, the WebAssembly function model is currently *mutable* so state would be contaminated
#[cfg(feature = "external_functions")]
let mut function = ExternalFunction::new_js(&self.code, param_names)?;
let mut function = ExternalFunction::new_js(&self.code, param_names).map_err(|e| {
GritPatternError::new(format!("failed to create function {}: {}", self.name, e))
})?;

#[cfg(feature = "external_functions")]
let result = function
.call(&resolved_str)
.or_else(|e| bail!("failed to call function {}: {}", self.name, e))?;
let result = function.call(&resolved_str).map_err(|e| GritPatternError::new(format!(
"failed to call function {}: {}",
self.name, e
)))?;
// END embedded version

let string = String::from_utf8(result).or_else(|_| {
bail!(
let string = String::from_utf8(result).map_err(|_| GritPatternError::new(format!(
"function {} returned did not return a UTF-8 string",
self.name
)
})?;
self.name,
)))?;

Ok(FuncEvaluation {
predicator: true,
Expand All @@ -123,15 +136,17 @@ impl GritCall<MarzanoQueryContext> for CallForeignFunction<MarzanoQueryContext>
state: &mut State<'a, MarzanoQueryContext>,
context: &'a MarzanoContext<'a>,
logs: &mut AnalysisLogs,
) -> Result<MarzanoResolvedPattern<'a>> {
) -> GritResult<MarzanoResolvedPattern<'a>> {
let function_definition = &context.foreign_function_definitions()[self.index];

match function_definition
.call(state, context, &self.args, logs)?
.ret_val
{
Some(pattern) => Ok(pattern),
None => bail!("Function call did not return a value"),
None => Err(GritPatternError::new(
"Function call did not return a value",
)),
}
}
}
Loading

0 comments on commit 234aa75

Please sign in to comment.