Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/plotnik-lib/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod printer;
pub mod source_map;

pub use printer::QueryPrinter;
pub use query::{Query, QueryBuilder};
pub use query::{Query, QueryBuilder, QueryContext};
pub use source_map::{SourceId, SourceMap};

#[allow(clippy::module_inception)]
Expand Down
30 changes: 24 additions & 6 deletions crates/plotnik-lib/src/query/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ impl QueryParsed {

pub type Query = QueryAnalyzed;

/// A unified view of the core analysis context.
///
/// Bundles references to the three main analysis artifacts that downstream
/// modules (compile, emit) commonly need together.
#[derive(Clone, Copy)]
pub struct QueryContext<'a> {
pub interner: &'a Interner,
pub type_ctx: &'a TypeContext,
pub symbol_table: &'a SymbolTable,
}

pub struct QueryAnalyzed {
query_parsed: QueryParsed,
interner: Interner,
Expand All @@ -161,6 +172,15 @@ impl QueryAnalyzed {
!self.diag.has_errors()
}

/// Returns a unified context view for downstream modules.
pub fn context(&self) -> QueryContext<'_> {
QueryContext {
interner: &self.interner,
type_ctx: &self.type_context,
symbol_table: &self.symbol_table,
}
}

pub fn get_arity(&self, node: &SyntaxNode) -> Option<Arity> {
use crate::parser::ast;

Expand Down Expand Up @@ -224,8 +244,7 @@ impl QueryAnalyzed {

LinkedQuery {
inner: self,
node_type_ids: output.node_type_ids,
node_field_ids: output.node_field_ids,
linking: output,
}
}
}
Expand Down Expand Up @@ -256,8 +275,7 @@ impl TryFrom<&str> for QueryAnalyzed {

pub struct LinkedQuery {
inner: QueryAnalyzed,
node_type_ids: IndexMap<Symbol, NodeTypeId>,
node_field_ids: IndexMap<Symbol, NodeFieldId>,
linking: link::LinkOutput,
}

impl LinkedQuery {
Expand All @@ -266,11 +284,11 @@ impl LinkedQuery {
}

pub fn node_type_ids(&self) -> &IndexMap<Symbol, NodeTypeId> {
&self.node_type_ids
&self.linking.node_type_ids
}

pub fn node_field_ids(&self) -> &IndexMap<Symbol, NodeFieldId> {
&self.node_field_ids
&self.linking.node_field_ids
}

/// Emit bytecode with node type/field symbols from language linking.
Expand Down