Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 67c930a

Browse files
author
Hendrik van Antwerpen
committed
Use LanguageConfiguration in more places
- `LanguageConfiguration` now contains a `StackGraphLanguage` and a builtins `StackGraph`, instead of sources that still need to be parsed. - `Loader` now returns `LanguageConfiguration` instances, instead of `StackGraph`. This makes it possible to find special file analyzers, and checking language file types, etc.
1 parent 5083df3 commit 67c930a

File tree

8 files changed

+126
-128
lines changed

8 files changed

+126
-128
lines changed

languages/tree-sitter-stack-graphs-typescript/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ harness = false
2424
[dependencies]
2525
anyhow = "1.0"
2626
clap = "3"
27+
stack-graphs = { version = "~0.10.1", path = "../../stack-graphs" }
2728
tree-sitter-stack-graphs = { version = "~0.4.0", path = "../../tree-sitter-stack-graphs", features=["cli"] }
2829
tree-sitter-typescript = { git = "https://github.com/tree-sitter/tree-sitter-typescript", rev="082da44a5263599186dadafd2c974c19f3a73d28" }

languages/tree-sitter-stack-graphs-typescript/rust/bin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
// ------------------------------------------------------------------------------------------------
77

88
use tree_sitter_stack_graphs::cli::LanguageConfigurationsCli as Cli;
9+
use tree_sitter_stack_graphs::NoCancellation;
910

1011
fn main() -> anyhow::Result<()> {
1112
Cli::main(vec![
12-
tree_sitter_stack_graphs_typescript::language_configuration(),
13+
tree_sitter_stack_graphs_typescript::language_configuration(&NoCancellation),
1314
])
1415
}

languages/tree-sitter-stack-graphs-typescript/rust/lib.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
66
// ------------------------------------------------------------------------------------------------
77

8-
use tree_sitter_stack_graphs::loader::BuiltinsConfiguration;
8+
use stack_graphs::graph::StackGraph;
99
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
10+
use tree_sitter_stack_graphs::loader::Loader;
11+
use tree_sitter_stack_graphs::CancellationFlag;
12+
use tree_sitter_stack_graphs::StackGraphLanguage;
13+
use tree_sitter_stack_graphs::Variables;
1014

1115
/// The stack graphs tsg source for this language
1216
const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
@@ -16,16 +20,28 @@ const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
1620
/// The stack graphs builtins source for this language
1721
const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.ts");
1822

19-
pub fn language_configuration() -> LanguageConfiguration {
23+
pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
24+
let language = tree_sitter_typescript::language_typescript();
25+
let sgl = StackGraphLanguage::from_str(language, STACK_GRAPHS_TSG_SOURCE).unwrap();
26+
let mut builtins = StackGraph::new();
27+
let file = builtins.add_file("<builtins>").unwrap();
28+
let mut builtins_globals = Variables::new();
29+
Loader::load_globals_from_config_str(STACK_GRAPHS_BUILTINS_CONFIG, &mut builtins_globals)
30+
.unwrap();
31+
sgl.build_stack_graph_into(
32+
&mut builtins,
33+
file,
34+
STACK_GRAPHS_BUILTINS_SOURCE,
35+
&builtins_globals,
36+
cancellation_flag,
37+
)
38+
.unwrap();
2039
LanguageConfiguration {
21-
language: tree_sitter_typescript::language_typescript(),
40+
language,
2241
scope: Some(String::from("source.ts")),
2342
content_regex: None,
2443
file_types: vec![String::from("ts")],
25-
tsg_source: STACK_GRAPHS_TSG_SOURCE.to_string(),
26-
builtins: Some(BuiltinsConfiguration {
27-
source: STACK_GRAPHS_BUILTINS_SOURCE.to_string(),
28-
config: STACK_GRAPHS_BUILTINS_CONFIG.to_string(),
29-
}),
44+
sgl,
45+
builtins,
3046
}
3147
}

languages/tree-sitter-stack-graphs-typescript/rust/test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
use std::path::PathBuf;
99
use tree_sitter_stack_graphs::cli::CiTester;
10+
use tree_sitter_stack_graphs::NoCancellation;
1011

1112
fn main() -> anyhow::Result<()> {
1213
let test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test");
1314
CiTester::new(
14-
vec![tree_sitter_stack_graphs_typescript::language_configuration()],
15+
vec![tree_sitter_stack_graphs_typescript::language_configuration(
16+
&NoCancellation,
17+
)],
1518
vec![test_path],
1619
)
1720
.run()

tree-sitter-stack-graphs/src/cli/test.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use walkdir::WalkDir;
2424
use crate::cli::util::map_parse_errors;
2525
use crate::cli::util::path_exists;
2626
use crate::cli::util::PathSpec;
27+
use crate::loader::LanguageConfiguration;
2728
use crate::loader::Loader;
2829
use crate::test::Test;
2930
use crate::test::TestResult;
@@ -193,7 +194,7 @@ impl TestArgs {
193194
loader: &mut Loader,
194195
) -> anyhow::Result<TestResult> {
195196
let source = std::fs::read_to_string(test_path)?;
196-
let sgl = match loader.load_for_file(test_path, Some(&source), &NoCancellation)? {
197+
let lc = match loader.load_for_file(test_path, Some(&source), &NoCancellation)? {
197198
Some(sgl) => sgl,
198199
None => {
199200
if self.show_ignored {
@@ -204,28 +205,29 @@ impl TestArgs {
204205
};
205206
let default_fragment_path = test_path.strip_prefix(test_root).unwrap();
206207
let mut test = Test::from_source(&test_path, &source, default_fragment_path)?;
207-
self.load_builtins_into(sgl, &mut test.graph)
208+
self.load_builtins_into(&lc, &mut test.graph)
208209
.with_context(|| format!("Loading builtins into {}", test_path.display()))?;
209210
let mut globals = Variables::new();
210211
for test_fragment in &test.fragments {
211212
let fragment_path = Path::new(test.graph[test_fragment.file].name()).to_path_buf();
212-
if test_path.extension() != fragment_path.extension() {
213+
if lc.matches_file(&fragment_path, Some(&test_fragment.source)) {
214+
globals.clear();
215+
test_fragment.add_globals_to(&mut globals);
216+
self.build_fragment_stack_graph_into(
217+
&fragment_path,
218+
&lc.sgl,
219+
test_fragment.file,
220+
&test_fragment.source,
221+
&globals,
222+
&mut test.graph,
223+
)?;
224+
} else {
213225
return Err(anyhow!(
214-
"Test fragment {} has different file extension than test file {}",
226+
"Test fragment {} not supported by language of test file {}",
215227
fragment_path.display(),
216228
test_path.display()
217229
));
218230
}
219-
globals.clear();
220-
test_fragment.add_globals_to(&mut globals);
221-
self.build_fragment_stack_graph_into(
222-
&fragment_path,
223-
sgl,
224-
test_fragment.file,
225-
&test_fragment.source,
226-
&globals,
227-
&mut test.graph,
228-
)?;
229231
}
230232
let result = test.run(&NoCancellation)?;
231233
let success = self.handle_result(test_path, &result)?;
@@ -245,10 +247,10 @@ impl TestArgs {
245247

246248
fn load_builtins_into(
247249
&self,
248-
sgl: &mut StackGraphLanguage,
250+
lc: &LanguageConfiguration,
249251
graph: &mut StackGraph,
250252
) -> anyhow::Result<()> {
251-
if let Err(h) = graph.add_from_graph(sgl.builtins()) {
253+
if let Err(h) = graph.add_from_graph(&lc.builtins) {
252254
return Err(anyhow!("Duplicate builtin file {}", &graph[h]));
253255
}
254256
Ok(())
@@ -257,7 +259,7 @@ impl TestArgs {
257259
fn build_fragment_stack_graph_into(
258260
&self,
259261
test_path: &Path,
260-
sgl: &mut StackGraphLanguage,
262+
sgl: &StackGraphLanguage,
261263
file: Handle<File>,
262264
source: &str,
263265
globals: &Variables,

tree-sitter-stack-graphs/src/lib.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,16 @@ use tree_sitter_graph::graph::Value;
331331
use tree_sitter_graph::parse_error::ParseError;
332332
use tree_sitter_graph::parse_error::TreeWithParseErrorVec;
333333
use tree_sitter_graph::ExecutionConfig;
334-
use tree_sitter_graph::Variables;
335334

336335
#[cfg(feature = "cli")]
337336
pub mod cli;
338337
pub mod functions;
339338
pub mod loader;
340339
pub mod test;
341340

341+
pub use tree_sitter_graph::VariableError;
342+
pub use tree_sitter_graph::Variables;
343+
342344
// Node type values
343345
static DROP_SCOPES_TYPE: &'static str = "drop_scopes";
344346
static POP_SCOPED_SYMBOL_TYPE: &'static str = "pop_scoped_symbol";
@@ -387,7 +389,6 @@ pub struct StackGraphLanguage {
387389
language: tree_sitter::Language,
388390
tsg: tree_sitter_graph::ast::File,
389391
functions: Functions,
390-
builtins: StackGraph,
391392
}
392393

393394
impl StackGraphLanguage {
@@ -402,7 +403,6 @@ impl StackGraphLanguage {
402403
language,
403404
tsg,
404405
functions: Self::default_functions(),
405-
builtins: StackGraph::new(),
406406
})
407407
}
408408

@@ -417,7 +417,6 @@ impl StackGraphLanguage {
417417
language,
418418
tsg,
419419
functions: Self::default_functions(),
420-
builtins: StackGraph::new(),
421420
})
422421
}
423422

@@ -431,14 +430,6 @@ impl StackGraphLanguage {
431430
&mut self.functions
432431
}
433432

434-
pub fn builtins(&self) -> &StackGraph {
435-
&self.builtins
436-
}
437-
438-
pub fn builtins_mut(&mut self) -> &mut StackGraph {
439-
&mut self.builtins
440-
}
441-
442433
pub fn language(&self) -> tree_sitter::Language {
443434
self.language
444435
}

0 commit comments

Comments
 (0)