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

Commit bca02ae

Browse files
author
Hendrik van Antwerpen
authored
Merge pull request #154 from github/use-language-configuration-more
Reorganize loader
2 parents f89a9e0 + 604033a commit bca02ae

File tree

9 files changed

+193
-147
lines changed

9 files changed

+193
-147
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" }
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2022, stack-graphs authors.
4+
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5+
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6+
// ------------------------------------------------------------------------------------------------
7+
18
use tree_sitter_stack_graphs::cli::LanguageConfigurationsCli as Cli;
9+
use tree_sitter_stack_graphs::NoCancellation;
210

311
fn main() -> anyhow::Result<()> {
412
Cli::main(vec![
5-
tree_sitter_stack_graphs_typescript::language_configuration(),
13+
tree_sitter_stack_graphs_typescript::language_configuration(&NoCancellation),
614
])
715
}
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
use tree_sitter_stack_graphs::loader::BuiltinsConfiguration;
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2022, stack-graphs authors.
4+
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5+
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6+
// ------------------------------------------------------------------------------------------------
7+
28
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
9+
use tree_sitter_stack_graphs::CancellationFlag;
310

411
/// The stack graphs tsg source for this language
512
const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
@@ -9,16 +16,16 @@ const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
916
/// The stack graphs builtins source for this language
1017
const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.ts");
1118

12-
pub fn language_configuration() -> LanguageConfiguration {
13-
LanguageConfiguration {
14-
language: tree_sitter_typescript::language_typescript(),
15-
scope: Some(String::from("source.ts")),
16-
content_regex: None,
17-
file_types: vec![String::from("ts")],
18-
tsg_source: STACK_GRAPHS_TSG_SOURCE.to_string(),
19-
builtins: Some(BuiltinsConfiguration {
20-
source: STACK_GRAPHS_BUILTINS_SOURCE.to_string(),
21-
config: STACK_GRAPHS_BUILTINS_CONFIG.to_string(),
22-
}),
23-
}
19+
pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
20+
LanguageConfiguration::from_tsg_str(
21+
tree_sitter_typescript::language_typescript(),
22+
Some(String::from("source.ts")),
23+
None,
24+
vec![String::from("ts")],
25+
STACK_GRAPHS_TSG_SOURCE,
26+
Some(STACK_GRAPHS_BUILTINS_SOURCE),
27+
Some(STACK_GRAPHS_BUILTINS_CONFIG),
28+
cancellation_flag,
29+
)
30+
.unwrap()
2431
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2022, stack-graphs authors.
4+
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5+
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6+
// ------------------------------------------------------------------------------------------------
7+
18
use std::path::PathBuf;
29
use tree_sitter_stack_graphs::cli::CiTester;
10+
use tree_sitter_stack_graphs::NoCancellation;
311

412
fn main() -> anyhow::Result<()> {
513
let test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test");
614
CiTester::new(
7-
vec![tree_sitter_stack_graphs_typescript::language_configuration()],
15+
vec![tree_sitter_stack_graphs_typescript::language_configuration(
16+
&NoCancellation,
17+
)],
818
vec![test_path],
919
)
1020
.run()

languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
; -*- coding: utf-8 -*-
2+
; ------------------------------------------------------------------------------------------------
3+
; Copyright © 2022, stack-graphs authors.
4+
; Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5+
; Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6+
; ------------------------------------------------------------------------------------------------
7+
18
; ########################################################################################
29
;
310
; ######## ## ## ######## ######## ###### ###### ######## #### ######## ########

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)