Skip to content

Commit d5c133f

Browse files
authored
Merge pull request github#152 from github/add-java-crate
Add initial support for Java via tssg
2 parents 2926cba + 10ae494 commit d5c133f

19 files changed

+735
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class TestClass extends SecondTestClass {
2+
public static void main(String[] args){
3+
super.bar();
4+
// ^ defined: 14
5+
this.bar();
6+
// ^ defined: 9
7+
}
8+
9+
public void bar() {
10+
}
11+
}
12+
13+
class SecondTestClass {
14+
public void bar() {
15+
System.out.println("Hello");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/Cargo.lock
2+
/*.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[package]
2+
name = "tree-sitter-stack-graphs-java"
3+
version = "0.1.0"
4+
description = "Stack graphs for the Java programming language"
5+
6+
homepage = "https://github.com/github/stack-graphs/tree/main/stack-graphs/languages/tree-sitter-stack-graphs-java"
7+
repository = "https://github.com/github/stack-graphs"
8+
license = "MIT OR Apache-2.0"
9+
readme = "languages/tree-sitter-stack-graphs-java/README.md"
10+
edition = "2021"
11+
12+
authors = [
13+
"Beka Valentine <[email protected]>",
14+
"Douglas Creager <[email protected]>",
15+
"GitHub <[email protected]>",
16+
"Hendrik van Antwerpen <[email protected]>",
17+
"Nina Kaufman <[email protected]>",
18+
"Rob Rix <[email protected]>",
19+
]
20+
21+
keywords = ["tree-sitter", "stack-graphs", "java"]
22+
23+
[[bin]]
24+
name = "tree-sitter-stack-graphs-java"
25+
path = "rust/bin.rs"
26+
27+
[lib]
28+
path = "rust/lib.rs"
29+
test = false
30+
31+
[[test]]
32+
name = "test"
33+
path = "rust/test.rs"
34+
harness = false # need to provide own main function to handle running tests
35+
36+
[dependencies]
37+
anyhow = "1.0"
38+
clap = "3"
39+
tree-sitter-stack-graphs = { version = "~0.6.0", path = "../../tree-sitter-stack-graphs", features=["cli"] }
40+
tree-sitter-java = { git = "http://github.com/tree-sitter/tree-sitter-java", rev="09d650def6cdf7f479f4b78f595e9ef5b58ce31e" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# tree-sitter-stack-graphs definition for Java
2+
3+
This project defines tree-sitter-stack-graphs rules for Java using the [tree-sitter-java](https://www.npmjs.com/package/tree-sitter-java) grammar.
4+
5+
## Local Development
6+
7+
The project is organized as follows:
8+
9+
- The stack graph rules are defined in `src/stack-graphs.tsg`.
10+
- Tests are put into the `tests` directory.
11+
12+
The following commands are intended to be run from the repo root.
13+
14+
Run all tests in the project by executing the following:
15+
16+
cargo test -p tree-sitter-stack-graphs-java
17+
18+
Parse a single test file:
19+
`cargo run -p tree-sitter-stack-graphs-java -- parse OPTIONS FILENAME`
20+
21+
Test a single file:
22+
`cargo run -p tree-sitter-stack-graphs-java -- test OPTIONS FILENAME`
23+
24+
For debugging purposes, it is useful to run the visualization tool to generate graphs for the tests being run.
25+
26+
To run a test and generate the visualization:
27+
28+
`cargo run -p tree-sitter-stack-graphs-java -- test --output-mode=always -V=%r/%d/%n.html FILENAME`
29+
30+
Go to https://crates.io/crates/tree-sitter-stack-graphs for links to examples and documentation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use clap::Parser;
2+
use tree_sitter_stack_graphs::cli::provided_languages::Subcommands;
3+
use tree_sitter_stack_graphs::NoCancellation;
4+
5+
fn main() -> anyhow::Result<()> {
6+
let cli = Cli::parse();
7+
cli.subcommand
8+
.run(vec![tree_sitter_stack_graphs_java::language_configuration(
9+
&NoCancellation,
10+
)])
11+
}
12+
13+
#[derive(Parser)]
14+
#[clap(about, version)]
15+
pub struct Cli {
16+
#[clap(subcommand)]
17+
subcommand: Subcommands,
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use tree_sitter_stack_graphs::loader::FileAnalyzers;
2+
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
3+
use tree_sitter_stack_graphs::CancellationFlag;
4+
5+
/// The stack graphs tsg source for this language
6+
pub const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
7+
8+
/// The stack graphs builtins configuration for this language
9+
pub const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
10+
/// The stack graphs builtins source for this language
11+
pub const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.java");
12+
13+
pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
14+
LanguageConfiguration::from_tsg_str(
15+
tree_sitter_java::language(),
16+
Some(String::from("source.java")),
17+
None,
18+
vec![String::from("java")],
19+
STACK_GRAPHS_TSG_SOURCE,
20+
Some(STACK_GRAPHS_BUILTINS_SOURCE),
21+
Some(STACK_GRAPHS_BUILTINS_CONFIG),
22+
FileAnalyzers::new(),
23+
cancellation_flag,
24+
)
25+
.unwrap()
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::path::PathBuf;
2+
use tree_sitter_stack_graphs::{ci::Tester, NoCancellation};
3+
4+
fn main() -> anyhow::Result<()> {
5+
let test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test");
6+
Tester::new(
7+
vec![tree_sitter_stack_graphs_java::language_configuration(
8+
&NoCancellation,
9+
)],
10+
vec![test_path],
11+
)
12+
.run()
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[globals]

languages/tree-sitter-stack-graphs-java/src/builtins.java

Whitespace-only changes.

0 commit comments

Comments
 (0)