-
-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add Grit node compilers (#2689)
- Loading branch information
Showing
64 changed files
with
2,713 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use biome_diagnostics::Diagnostic; | ||
use biome_rowan::TextRange; | ||
|
||
#[derive(Debug, Diagnostic)] | ||
#[diagnostic(severity = Warning)] | ||
pub(crate) struct CompilerDiagnostic { | ||
#[message] | ||
#[description] | ||
message: String, | ||
|
||
#[location(span)] | ||
range: TextRange, | ||
} | ||
|
||
impl CompilerDiagnostic { | ||
pub(crate) fn new_warning(message: impl Into<String>, range: TextRange) -> Self { | ||
Self { | ||
message: message.into(), | ||
range, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::diagnostics::CompilerDiagnostic; | ||
use crate::grit_context::{GritExecContext, GritQueryContext}; | ||
use crate::grit_target_language::GritTargetLanguage; | ||
use crate::pattern_compiler::PatternCompiler; | ||
use crate::pattern_compiler::{ | ||
compilation_context::CompilationContext, compilation_context::NodeCompilationContext, | ||
}; | ||
use crate::resolved_pattern::GritResolvedPattern; | ||
use crate::variables::{VarRegistry, VariableLocations}; | ||
use crate::CompileError; | ||
use anyhow::Result; | ||
use biome_grit_syntax::{GritRoot, GritRootExt}; | ||
use grit_pattern_matcher::pattern::{Matcher, Pattern, State}; | ||
use std::collections::BTreeMap; | ||
|
||
/// Represents a top-level Grit query. | ||
/// | ||
/// Grit queries provide the | ||
pub struct GritQuery { | ||
pub(crate) pattern: Pattern<GritQueryContext>, | ||
|
||
/// Diagnostics discovered during compilation of the query. | ||
diagnostics: Vec<CompilerDiagnostic>, | ||
|
||
/// All variables discovered during query compilation. | ||
locations: VariableLocations, | ||
} | ||
|
||
impl GritQuery { | ||
pub fn execute(&self) -> Result<bool> { | ||
let var_registry = VarRegistry::from_locations(&self.locations); | ||
|
||
let binding = GritResolvedPattern; | ||
let context = GritExecContext; | ||
let mut state = State::new(var_registry.into(), Vec::new()); | ||
let mut logs = Vec::new().into(); | ||
|
||
self.pattern | ||
.execute(&binding, &mut state, &context, &mut logs) | ||
} | ||
|
||
pub fn from_node(root: GritRoot, lang: GritTargetLanguage) -> Result<Self, CompileError> { | ||
let context = CompilationContext::new_anonymous(lang); | ||
|
||
let mut vars_array = Vec::new(); | ||
let mut global_vars = BTreeMap::new(); | ||
let mut diagnostics = Vec::new(); | ||
|
||
// We're not in a local scope yet, so this map is kinda useless. | ||
// It's just there because all node compilers expect one. | ||
let mut vars = BTreeMap::new(); | ||
|
||
let mut node_context = NodeCompilationContext::new( | ||
&context, | ||
&mut vars, | ||
&mut vars_array, | ||
&mut global_vars, | ||
&mut diagnostics, | ||
); | ||
|
||
let pattern = PatternCompiler::from_node( | ||
&root.pattern().ok_or(CompileError::MissingPattern)?, | ||
&mut node_context, | ||
)?; | ||
|
||
let locations = VariableLocations::new(vars_array); | ||
|
||
Ok(Self { | ||
pattern, | ||
diagnostics, | ||
locations, | ||
}) | ||
} | ||
} |
Oops, something went wrong.