Skip to content

Commit c16b8cf

Browse files
Merge pull request #282 from github/add-python
Add Python
2 parents 8e99165 + cd9f3eb commit c16b8cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3197
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.html
2+
/Cargo.lock
3+
/target
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog for tree-sitter-stack-graphs-python
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "tree-sitter-stack-graphs-python"
3+
version = "0.1.0"
4+
description = "Stack graphs definition for Python using tree-sitter-python"
5+
readme = "README.md"
6+
keywords = ["tree-sitter", "stack-graphs", "python"]
7+
authors = [
8+
"GitHub <[email protected]>",
9+
]
10+
license = "MIT OR Apache-2.0"
11+
edition = "2018"
12+
13+
[[bin]]
14+
name = "tree-sitter-stack-graphs-python"
15+
path = "rust/bin.rs"
16+
required-features = ["cli"]
17+
18+
[lib]
19+
path = "rust/lib.rs"
20+
test = false
21+
22+
[[test]]
23+
name = "test"
24+
path = "rust/test.rs"
25+
harness = false
26+
27+
[features]
28+
cli = ["anyhow", "clap", "tree-sitter-stack-graphs/cli"]
29+
30+
[dependencies]
31+
anyhow = { version = "1.0", optional = true }
32+
clap = { version = "4", optional = true, features = ["derive"] }
33+
tree-sitter-stack-graphs = { version = "0.7", path = "../../tree-sitter-stack-graphs" }
34+
tree-sitter-python = "=0.20.4"
35+
36+
[dev-dependencies]
37+
anyhow = "1.0"
38+
tree-sitter-stack-graphs = { version = "0.7", path = "../../tree-sitter-stack-graphs", features = ["cli"] }

languages/tree-sitter-stack-graphs-python/LICENSE

Whitespace-only changes.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# tree-sitter-stack-graphs definition for Python
2+
3+
This project defines tree-sitter-stack-graphs rules for Python using the [tree-sitter-python][] grammar.
4+
5+
[tree-sitter-python]: https://crates.io/crates/tree-sitter-python
6+
7+
- [API documentation](https://docs.rs/tree-sitter-stack-graphs-python/)
8+
- [Release notes](https://github.com/github/stack-graphs/blob/main/languages/tree-sitter-stack-graphs-python/CHANGELOG.md)
9+
10+
## Using the API
11+
12+
To use this library, add the following to your `Cargo.toml`:
13+
14+
```toml
15+
[dependencies]
16+
tree-sitter-stack-graphs-python = "0.1.0"
17+
```
18+
19+
Check out our [documentation](https://docs.rs/tree-sitter-stack-graphs-python/*/) for more details on how to use this library.
20+
21+
## Using the Command-line Program
22+
23+
The command-line program for `tree-sitter-stack-graphs-python` lets you do stack graph based analysis and lookup from the command line.
24+
25+
The CLI can be run as follows:
26+
27+
1. _(Installed)_ Install the CLI using Cargo as follows:
28+
29+
```sh
30+
cargo install --features cli tree-sitter-stack-graphs-python
31+
```
32+
33+
After this, the CLI should be available as `tree-sitter-stack-graphs-python`.
34+
35+
2. _(From source)_ Instead of installing the CLI, it can also be run directly from the crate directory, as a replacement for a `tree-sitter-stack-graphs-python` invocation, as follows:
36+
37+
```sh
38+
cargo run --features cli --
39+
```
40+
41+
The basic CLI workflow for the command-line program is to index source code and issue queries against the resulting database:
42+
43+
1. Index a source folder as follows:
44+
45+
```sh
46+
tree-sitter-stack-graphs-python index SOURCE_DIR
47+
```
48+
49+
_Indexing will skip any files that have already be indexed. To force a re-index, add the `-f` flag._
50+
51+
To check the status if a source folder, run:
52+
53+
```sh
54+
tree-sitter-stack-graphs-python status SOURCE_DIR
55+
```
56+
57+
To clean the database and start with a clean slate, run:
58+
59+
```sh
60+
tree-sitter-stack-graphs-python clean
61+
```
62+
63+
_Pass the `--delete` flag to not just empty the database, but also delete it. This is useful to resolve `unsupported database version` errors that may occur after a version update._
64+
65+
2. Run a query to find the definition(s) for a reference on a given line and column, run:
66+
67+
```sh
68+
tree-sitter-stack-graphs-python query definition SOURCE_PATH:LINE:COLUMN
69+
```
70+
71+
Resulting definitions are printed, including a source line if the source file is available.
72+
73+
Discover all available commands and flags by passing the `-h` flag to the CLI directly, or to any of the subcommands.
74+
75+
## Development
76+
77+
The project is written in Rust, and requires a recent version installed. Rust can be installed and updated using [rustup][].
78+
79+
[rustup]: https://rustup.rs/
80+
81+
The project is organized as follows:
82+
83+
- The stack graph rules are defined in `src/stack-graphs.tsg`.
84+
- Builtins sources and configuration are defined in `src/builtins.it` and `builtins.cfg` respectively.
85+
- Tests are put into the `test` directory.
86+
87+
### Running Tests
88+
89+
Run the tests as follows:
90+
91+
```sh
92+
cargo test
93+
```
94+
95+
The project consists of a library and a CLI. By default, running `cargo` only applies to the library. To run `cargo` commands on the CLI as well, add `--features cli` or `--all-features`.
96+
97+
Run the CLI from source as follows:
98+
99+
```sh
100+
cargo run --features cli -- ARGS
101+
```
102+
103+
Sources are formatted using the standard Rust formatted, which is applied by running:
104+
105+
```sh
106+
cargo fmt
107+
```
108+
109+
### Writing TSG
110+
111+
The stack graph rules are written in [tree-sitter-graph][]. Checkout the [examples][],
112+
which contain self-contained TSG rules for specific language features. A VSCode
113+
[extension][] is available that provides syntax highlighting for TSG files.
114+
115+
[tree-sitter-graph]: https://github.com/tree-sitter/tree-sitter-graph
116+
[examples]: https://github.com/github/stack-graphs/blob/main/tree-sitter-stack-graphs/examples/
117+
[extension]: https://marketplace.visualstudio.com/items?itemName=tree-sitter.tree-sitter-graph
118+
119+
Parse and test a single file by executing the following commands:
120+
121+
```sh
122+
cargo run --features cli -- parse FILES...
123+
cargo run --features cli -- test TESTFILES...
124+
```
125+
126+
Generate a visualization to debug failing tests by passing the `-V` flag:
127+
128+
```sh
129+
cargo run --features cli -- test -V TESTFILES...
130+
```
131+
132+
To generate the visualization regardless of test outcome, execute:
133+
134+
```sh
135+
cargo run --features cli -- test -V --output-mode=always TESTFILES...
136+
```
137+
138+
Go to <https://crates.io/crates/tree-sitter-stack-graphs> for links to examples and documentation.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2023, 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+
8+
use anyhow::anyhow;
9+
use clap::Parser;
10+
use tree_sitter_stack_graphs::cli::database::default_user_database_path_for_crate;
11+
use tree_sitter_stack_graphs::cli::provided_languages::Subcommands;
12+
use tree_sitter_stack_graphs::NoCancellation;
13+
14+
fn main() -> anyhow::Result<()> {
15+
let lc = match tree_sitter_stack_graphs_python::try_language_configuration(&NoCancellation) {
16+
Ok(lc) => lc,
17+
Err(err) => {
18+
eprintln!("{}", err.display_pretty());
19+
return Err(anyhow!("Language configuration error"));
20+
}
21+
};
22+
let cli = Cli::parse();
23+
let default_db_path = default_user_database_path_for_crate(env!("CARGO_PKG_NAME"))?;
24+
cli.subcommand.run(default_db_path, vec![lc])
25+
}
26+
27+
#[derive(Parser)]
28+
#[clap(about, version)]
29+
pub struct Cli {
30+
#[clap(subcommand)]
31+
subcommand: Subcommands,
32+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2023, 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+
8+
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
9+
use tree_sitter_stack_graphs::loader::LoadError;
10+
use tree_sitter_stack_graphs::CancellationFlag;
11+
12+
/// The stack graphs tsg source for this language.
13+
pub const STACK_GRAPHS_TSG_PATH: &str = "src/stack-graphs.tsg";
14+
/// The stack graphs tsg source for this language.
15+
pub const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
16+
17+
/// The stack graphs builtins configuration for this language.
18+
pub const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
19+
/// The stack graphs builtins path for this language
20+
pub const STACK_GRAPHS_BUILTINS_PATH: &str = "src/builtins.py";
21+
/// The stack graphs builtins source for this language.
22+
pub const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.py");
23+
24+
/// The name of the file path global variable.
25+
pub const FILE_PATH_VAR: &str = "FILE_PATH";
26+
27+
pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
28+
try_language_configuration(cancellation_flag).unwrap_or_else(|err| panic!("{}", err))
29+
}
30+
31+
pub fn try_language_configuration(
32+
cancellation_flag: &dyn CancellationFlag,
33+
) -> Result<LanguageConfiguration, LoadError> {
34+
LanguageConfiguration::from_sources(
35+
tree_sitter_python::language(),
36+
Some(String::from("source.py")),
37+
None,
38+
vec![String::from("py")],
39+
STACK_GRAPHS_TSG_PATH.into(),
40+
STACK_GRAPHS_TSG_SOURCE,
41+
Some((
42+
STACK_GRAPHS_BUILTINS_PATH.into(),
43+
STACK_GRAPHS_BUILTINS_SOURCE,
44+
)),
45+
Some(STACK_GRAPHS_BUILTINS_CONFIG),
46+
cancellation_flag,
47+
)
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2023, 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+
8+
use anyhow::anyhow;
9+
use std::path::PathBuf;
10+
use tree_sitter_stack_graphs::ci::Tester;
11+
use tree_sitter_stack_graphs::NoCancellation;
12+
13+
fn main() -> anyhow::Result<()> {
14+
let lc = match tree_sitter_stack_graphs_python::try_language_configuration(&NoCancellation) {
15+
Ok(lc) => lc,
16+
Err(err) => {
17+
eprintln!("{}", err.display_pretty());
18+
return Err(anyhow!("Language configuration error"));
19+
}
20+
};
21+
let test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test");
22+
Tester::new(vec![lc], vec![test_path]).run()
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[globals]

languages/tree-sitter-stack-graphs-python/src/builtins.py

Whitespace-only changes.

0 commit comments

Comments
 (0)