Skip to content

Commit

Permalink
Introduce Context trait
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Mar 24, 2024
1 parent 746fc0a commit b56b112
Show file tree
Hide file tree
Showing 64 changed files with 410 additions and 360 deletions.
30 changes: 30 additions & 0 deletions crates/core/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::pattern::{
built_in_functions::BuiltIns,
function_definition::{ForeignFunctionDefinition, GritFunctionDefinition},
pattern_definition::PatternDefinition,
predicate_definition::PredicateDefinition,
FileOwners,
};
use marzano_language::target_language::TargetLanguage;

pub trait Context<'a> {
fn pattern_definitions(&self) -> &[PatternDefinition];

fn predicate_definitions(&self) -> &[PredicateDefinition];

fn function_definitions(&self) -> &[GritFunctionDefinition];

fn foreign_function_definitions(&self) -> &[ForeignFunctionDefinition];

fn ignore_limit_pattern(&self) -> bool;

// FIXME: Don't depend on Grit's file handling in Context.
fn files(&self) -> &FileOwners;

fn built_ins(&self) -> &BuiltIns;

// FIXME: This introduces a dependency on TreeSitter.
fn language(&self) -> &TargetLanguage;

fn name(&self) -> Option<&str>;
}
1 change: 1 addition & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(clippy::wildcard_enum_match_arm)]
pub mod binding;
pub mod compact_api;
mod context;
mod effects_dependency_graph;
mod equivalence;
pub mod errors;
Expand Down
53 changes: 47 additions & 6 deletions crates/core/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod every;
mod file_pattern;
mod files;
mod float_constant;
mod function_definition;
pub mod function_definition;
mod functions;
mod r#if;
mod includes;
Expand All @@ -44,9 +44,9 @@ mod multiply;
pub mod not;
pub mod or;
mod paths;
mod pattern_definition;
pub mod pattern_definition;
pub mod patterns;
mod predicate_definition;
pub mod predicate_definition;
mod predicate_return;
mod predicates;
mod range;
Expand All @@ -66,6 +66,7 @@ mod r#where;
mod within;
use crate::{
binding::Binding,
context::Context,
pattern::{compiler::DEFAULT_FILE_NAME, patterns::Matcher, resolved_pattern::ResolvedPattern},
};
use ::log::error;
Expand Down Expand Up @@ -653,7 +654,7 @@ impl Problem {
) -> Result<Vec<MatchResult>> {
let mut user_logs = vec![].into();

let context = Context::new(
let context = MarzanoContext::new(
&self.pattern_definitions,
&self.predicate_definitions,
&self.function_definitions,
Expand Down Expand Up @@ -771,7 +772,7 @@ impl Debug for FileOwners {
}
}

pub struct Context<'a> {
pub struct MarzanoContext<'a> {
pub pattern_definitions: &'a Vec<PatternDefinition>,
pub predicate_definitions: &'a Vec<PredicateDefinition>,
pub function_definitions: &'a Vec<GritFunctionDefinition>,
Expand All @@ -783,7 +784,7 @@ pub struct Context<'a> {
pub name: Option<String>,
}

impl<'a> Context<'a> {
impl<'a> MarzanoContext<'a> {
#[allow(clippy::too_many_arguments)]
pub fn new(
pattern_definitions: &'a Vec<PatternDefinition>,
Expand All @@ -809,3 +810,43 @@ impl<'a> Context<'a> {
}
}
}

impl<'a> Context<'a> for MarzanoContext<'a> {
fn pattern_definitions(&self) -> &[PatternDefinition] {
self.pattern_definitions
}

fn predicate_definitions(&self) -> &[PredicateDefinition] {
self.predicate_definitions
}

fn function_definitions(&self) -> &[GritFunctionDefinition] {
self.function_definitions
}

fn foreign_function_definitions(&self) -> &[ForeignFunctionDefinition] {
self.foreign_function_definitions
}

fn ignore_limit_pattern(&self) -> bool {
self.runtime.ignore_limit_pattern
}

// FIXME: Don't depend on Grit's file handling in context.
fn files(&self) -> &FileOwners {
self.files
}

fn built_ins(&self) -> &BuiltIns {
self.built_ins
}

// FIXME: This introduces a dependency on TreeSitter.
fn language(&self) -> &TargetLanguage {
self.language
}

fn name(&self) -> Option<&str> {
self.name.as_deref()
}
}
5 changes: 2 additions & 3 deletions crates/core/src/pattern/accessor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Cow, collections::BTreeMap};

use crate::{binding::Constant, equivalence::are_bindings_equivalent};
use crate::{binding::Constant, context::Context, equivalence::are_bindings_equivalent};

use super::{
compiler::CompilationContext,
Expand All @@ -10,7 +10,6 @@ use super::{
resolved_pattern::ResolvedPattern,
state::State,
variable::{Variable, VariableSourceLocations},
Context,
};
use anyhow::{anyhow, bail, Result};
use marzano_util::analysis_logs::AnalysisLogs;
Expand Down Expand Up @@ -174,7 +173,7 @@ impl Matcher for Accessor {
&'a self,
binding: &ResolvedPattern<'a>,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<bool> {
match self.get(state)? {
Expand Down
13 changes: 7 additions & 6 deletions crates/core/src/pattern/accumulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use super::{
patterns::Pattern,
patterns::{Matcher, Name},
resolved_pattern::ResolvedPattern,
Context, State,
State,
};
use super::{Effect, EffectKind};
use crate::context::Context;
use crate::smart_insert::normalize_insert;
use tree_sitter::Node;

Expand Down Expand Up @@ -143,7 +144,7 @@ impl Matcher for Accumulate {
&'a self,
context_node: &ResolvedPattern<'a>,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<bool> {
if let Pattern::Variable(var) = &self.left {
Expand All @@ -153,7 +154,7 @@ impl Matcher for Accumulate {
.value
.as_mut()
{
base.extend(append, &mut state.effects, context.language)?;
base.extend(append, &mut state.effects, context.language())?;
Ok(true)
} else {
bail!(
Expand Down Expand Up @@ -202,7 +203,7 @@ impl Matcher for Accumulate {
.iter()
.map(|b| {
let is_first = !state.effects.iter().any(|e| e.binding == *b);
normalize_insert(b, &mut replacement, is_first, context.language)?;
normalize_insert(b, &mut replacement, is_first, context.language())?;
Ok(Effect {
binding: b.clone(),
pattern: replacement.clone(),
Expand All @@ -221,7 +222,7 @@ impl Evaluator for Accumulate {
fn execute_func<'a>(
&'a self,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<FuncEvaluation> {
if let Pattern::Variable(var) = &self.left {
Expand All @@ -231,7 +232,7 @@ impl Evaluator for Accumulate {
.value
.as_mut()
{
base.extend(append, &mut state.effects, context.language)?;
base.extend(append, &mut state.effects, context.language())?;
Ok(FuncEvaluation {
predicator: true,
ret_val: None,
Expand Down
9 changes: 4 additions & 5 deletions crates/core/src/pattern/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use super::{
resolved_pattern::ResolvedPattern,
state::State,
variable::VariableSourceLocations,
Context,
};
use crate::binding::Constant;
use crate::{binding::Constant, context::Context};
use anyhow::{anyhow, Result};
use marzano_util::analysis_logs::AnalysisLogs;
use tree_sitter::Node;
Expand Down Expand Up @@ -67,7 +66,7 @@ impl Add {
pub(crate) fn call<'a>(
&'a self,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let res = self.evaluate(state, context, logs)?;
Expand All @@ -77,7 +76,7 @@ impl Add {
fn evaluate<'a>(
&'a self,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<f64> {
let lhs = self.lhs.float(state, context, logs)?;
Expand All @@ -98,7 +97,7 @@ impl Matcher for Add {
&'a self,
binding: &ResolvedPattern<'a>,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<bool> {
let binding_text = binding.text(&state.files)?;
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/pattern/after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use super::{
patterns::{Matcher, Name, Pattern},
resolved_pattern::{pattern_to_binding, ResolvedPattern},
variable::VariableSourceLocations,
Context, Node, State,
Node, State,
};
use crate::{binding::Binding, resolve};
use crate::{binding::Binding, context::Context, resolve};
use crate::{binding::Constant, errors::debug};
use anyhow::{anyhow, bail, Result};
use core::fmt::Debug;
Expand Down Expand Up @@ -52,7 +52,7 @@ impl After {
pub(crate) fn next_pattern<'a>(
&'a self,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let binding = pattern_to_binding(&self.after, state, context, logs)?;
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Matcher for After {
&'a self,
binding: &ResolvedPattern<'a>,
init_state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<bool> {
let binding = match binding {
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/pattern/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use super::{
predicates::Predicate,
resolved_pattern::ResolvedPattern,
variable::VariableSourceLocations,
Context, State,
State,
};
use std::collections::BTreeMap;

use crate::context::Context;
use anyhow::Result;
use marzano_util::analysis_logs::AnalysisLogs;
use std::collections::BTreeMap;
use tree_sitter::Node;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Matcher for And {
&'a self,
binding: &ResolvedPattern<'a>,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<bool> {
for p in self.patterns.iter() {
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Evaluator for PrAnd {
fn execute_func<'a>(
&'a self,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<FuncEvaluation> {
for p in self.predicates.iter() {
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/pattern/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use super::{
predicates::Predicate,
resolved_pattern::ResolvedPattern,
variable::VariableSourceLocations,
Context, State,
State,
};
use std::collections::BTreeMap;

use crate::context::Context;
use anyhow::Result;
use core::fmt::Debug;
use marzano_util::analysis_logs::AnalysisLogs;
use std::collections::BTreeMap;
use tree_sitter::Node;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Matcher for Any {
&'a self,
binding: &ResolvedPattern<'a>,
init_state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<bool> {
let mut matched = false;
Expand Down Expand Up @@ -146,7 +146,7 @@ impl Evaluator for PrAny {
fn execute_func<'a>(
&'a self,
init_state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<FuncEvaluation> {
let mut matched = false;
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/pattern/assignment.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::collections::BTreeMap;

use super::{
compiler::CompilationContext,
container::Container,
functions::{Evaluator, FuncEvaluation},
patterns::{Matcher, Name, Pattern},
resolved_pattern::ResolvedPattern,
variable::{is_reserved_metavariable, VariableSourceLocations},
Context, State,
State,
};
use crate::context::Context;
use anyhow::{anyhow, bail, Result};
use marzano_language::{language::GRIT_METAVARIABLE_PREFIX, target_language::TargetLanguage};
use marzano_util::analysis_logs::AnalysisLogs;
use std::collections::BTreeMap;
use tree_sitter::Node;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Matcher for Assignment {
&'a self,
_context_node: &ResolvedPattern<'a>,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<bool> {
let resolved = ResolvedPattern::from_pattern(&self.pattern, state, context, logs)?;
Expand All @@ -92,7 +92,7 @@ impl Evaluator for Assignment {
fn execute_func<'a>(
&'a self,
state: &mut State<'a>,
context: &Context<'a>,
context: &'a impl Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<FuncEvaluation> {
let resolved: ResolvedPattern<'_> =
Expand Down
Loading

0 comments on commit b56b112

Please sign in to comment.