Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jul 24, 2024
1 parent eeab033 commit ae42d62
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 34 deletions.
1 change: 1 addition & 0 deletions crates/core/src/built_in_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use itertools::Itertools;
use rand::prelude::SliceRandom;
use rand::Rng;
use std::collections::BTreeMap;
use grit_pattern_matcher::errors::GritResult;

// todo we can probably use a macro to generate a function that takes a vec and
// and calls the input function with the vec args unpacked.
Expand Down
5 changes: 3 additions & 2 deletions crates/core/src/foreign_function_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use grit_util::AnalysisLogs;
use marzano_externals::function::ExternalFunction;
use marzano_language::foreign_language::ForeignLanguage;
use std::borrow::Cow;
use grit_pattern_matcher::errors::{GritPatternError, GritResult};

#[derive(Debug, Clone)]
pub struct ForeignFunctionDefinition {
Expand Down Expand Up @@ -123,15 +124,15 @@ 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")),
}
}
}
1 change: 1 addition & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ mod test;
mod test_files;
#[cfg(any(test, feature = "test_utils"))]
pub mod test_utils;
mod error;
22 changes: 11 additions & 11 deletions crates/core/src/marzano_resolved_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ impl<'a> ResolvedPattern<'a, MarzanoQueryContext> for MarzanoResolvedPattern<'a>
DynamicPattern::Snippet(snippet) => {
Self::from_dynamic_snippet(snippet, state, context, logs)
}
DynamicPattern::CallBuiltIn(built_in) => built_in.call(state, context, logs),
DynamicPattern::CallFunction(func) => func.call(state, context, logs),
DynamicPattern::CallBuiltIn(built_in) => built_in.call(state, context, logs).into(),
DynamicPattern::CallFunction(func) => func.call(state, context, logs).into(),
DynamicPattern::CallForeignFunction(func) => func.call(state, context, logs),
}
}
Expand Down Expand Up @@ -467,8 +467,8 @@ impl<'a> ResolvedPattern<'a, MarzanoQueryContext> for MarzanoResolvedPattern<'a>
dynamic_snippet: Some(pattern),
..
}) => Self::from_dynamic_pattern(pattern, state, context, logs),
Pattern::CallBuiltIn(built_in) => built_in.call(state, context, logs),
Pattern::CallFunction(func) => func.call(state, context, logs),
Pattern::CallBuiltIn(built_in) => built_in.call(state, context, logs).into(),
Pattern::CallFunction(func) => func.call(state, context, logs).into(),
Pattern::CallForeignFunction(func) => func.call(state, context, logs),
Pattern::StringConstant(string) => Ok(Self::Snippets(vector![ResolvedSnippet::Text(
(&string.text).into(),
Expand Down Expand Up @@ -524,13 +524,13 @@ impl<'a> ResolvedPattern<'a, MarzanoQueryContext> for MarzanoResolvedPattern<'a>
body,
}))))
}
Pattern::Add(add_pattern) => add_pattern.call(state, context, logs),
Pattern::Subtract(subtract_pattern) => subtract_pattern.call(state, context, logs),
Pattern::Multiply(multiply_pattern) => multiply_pattern.call(state, context, logs),
Pattern::Divide(divide_pattern) => divide_pattern.call(state, context, logs),
Pattern::Modulo(modulo_pattern) => modulo_pattern.call(state, context, logs),
Pattern::Before(before) => before.prev_pattern(state, context, logs),
Pattern::After(after) => after.next_pattern(state, context, logs),
Pattern::Add(add_pattern) => add_pattern.call(state, context, logs).into(),
Pattern::Subtract(subtract_pattern) => subtract_pattern.call(state, context, logs).into(),
Pattern::Multiply(multiply_pattern) => multiply_pattern.call(state, context, logs).into(),
Pattern::Divide(divide_pattern) => divide_pattern.call(state, context, logs).into(),
Pattern::Modulo(modulo_pattern) => modulo_pattern.call(state, context, logs).into(),
Pattern::Before(before) => before.prev_pattern(state, context, logs).into(),
Pattern::After(after) => after.next_pattern(state, context, logs).into(),
Pattern::AstNode(_)
| Pattern::CodeSnippet(_)
| Pattern::Call(_)
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/pattern_compiler/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use marzano_language::{
};

use std::{collections::BTreeMap, path::Path, vec};
use grit_pattern_matcher::errors::GritResult;

pub type CallbackMatchFn = dyn for<'a> Fn(
&<problem::MarzanoQueryContext as grit_pattern_matcher::context::QueryContext>::ResolvedPattern<'a>,
Expand Down
4 changes: 2 additions & 2 deletions crates/grit-pattern-matcher/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub trait Binding<'a, Q: QueryContext>: Clone + std::fmt::Debug + PartialEq + Si
if let Some(binding) = resolved.get_last_binding() {
Ok(binding.clone())
} else {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"cannot create binding from pattern without binding",
));
))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/grit-pattern-matcher/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl GritPatternError {
Self::Matcher(reason.into())
}

pub(crate) fn new(reason: impl Into<String>) -> Self {
pub fn new(reason: impl Into<String>) -> Self {
Self::Generic(reason.into())
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/grit-pattern-matcher/src/pattern/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ impl<Q: QueryContext> Accessor<Q> {
Some(PatternOrResolved::Resolved(resolved)) => match resolved.get_map() {
Some(m) => Ok(m.get(key.as_ref()).map(PatternOrResolved::Resolved)),
None => {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"left side of an accessor must be a map",
))
}
},
Some(_) => {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"left side of an accessor must be a map",
))
}
Expand All @@ -93,13 +93,13 @@ impl<Q: QueryContext> Accessor<Q> {
Some(PatternOrResolvedMut::Resolved(resolved)) => match resolved.get_map_mut() {
Some(m) => Ok(m.get_mut(key.as_ref()).map(PatternOrResolvedMut::Resolved)),
None => {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"left side of an accessor must be a map",
))
}
},
Some(_) => {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"left side of an accessor must be a map",
))
}
Expand All @@ -124,9 +124,9 @@ impl<Q: QueryContext> Accessor<Q> {
m.insert(key.to_string(), value);
Ok(true)
} else {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"accessor can only mutate a resolved map",
));
))
}
}
Some(_) => Err(GritPatternError::new(
Expand Down
12 changes: 6 additions & 6 deletions crates/grit-pattern-matcher/src/pattern/accumulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ impl<Q: QueryContext> Matcher<Q> for Accumulate<Q> {
base.extend(append, &mut state.effects, context.language())?;
Ok(true)
} else {
return Err(GritPatternError::new(format!(
Err(GritPatternError::new(format!(
"Variable {} is not bound",
state.bindings[var.scope].last().unwrap()[var.index].name
)));
)))
}
} else {
let resolved = if !self.left.execute(context_node, state, context, logs)? {
Expand Down Expand Up @@ -120,15 +120,15 @@ impl<Q: QueryContext> Evaluator<Q> for Accumulate<Q> {
ret_val: None,
})
} else {
return Err(GritPatternError::new(format!(
Err(GritPatternError::new(format!(
"Variable {} is not bound",
state.bindings[var.scope].last().unwrap()[var.index].name
)));
)))
}
} else {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"Insert side-conditions must have variable on left-hand side",
));
))
}
}
}
12 changes: 6 additions & 6 deletions crates/grit-pattern-matcher/src/pattern/list_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<Q: QueryContext> ListIndex<Q> {
}
}
Some(s) => {
return Err(GritPatternError::new(format!(
Err(GritPatternError::new(format!(
"left side of a listIndex must be a list but got {:?}",
s
)))
Expand Down Expand Up @@ -127,13 +127,13 @@ impl<Q: QueryContext> ListIndex<Q> {
.get_list_item_at_mut(index)
.map(PatternOrResolvedMut::Resolved))
} else {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"left side of a listIndex must be a list",
));
))
}
}
Some(s) => {
return Err(GritPatternError::new(format!(
Err(GritPatternError::new(format!(
"left side of a listIndex must be a list but got {:?}",
s
)))
Expand All @@ -157,13 +157,13 @@ impl<Q: QueryContext> ListIndex<Q> {
resolved.set_list_item_at_mut(index, value)
}
Some(_) => {
return Err(GritPatternError::new(
Err(GritPatternError::new(
"accessor can only mutate a resolved list",
))
}
},
ListOrContainer::List(_) => {
return Err(GritPatternError::new("cannot mutate a list literal"))
Err(GritPatternError::new("cannot mutate a list literal"))
}
}
}
Expand Down

0 comments on commit ae42d62

Please sign in to comment.