Skip to content

Commit eb24d76

Browse files
Add skeleton Ruby project
1 parent 4e9457f commit eb24d76

File tree

13 files changed

+338
-0
lines changed

13 files changed

+338
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Publish tree-sitter-stack-graphs-ruby release
2+
3+
on:
4+
push:
5+
tags:
6+
- tree-sitter-stack-graphs-ruby-v*
7+
8+
jobs:
9+
publish-crate:
10+
runs-on: ubuntu-latest
11+
env:
12+
CARGO_TERM_COLOR: always
13+
CARGO_INCREMENTAL: 0
14+
CRATE_DIR: './languages/tree-sitter-stack-graphs-ruby'
15+
steps:
16+
- name: Install Rust environment
17+
uses: hecrj/setup-rust-action@v1
18+
- name: Checkout repository
19+
uses: actions/checkout@v3
20+
# TODO Verify the crate version matches the tag
21+
- name: Test crate
22+
run: cargo test --all-features
23+
working-directory: ${{ env.CRATE_DIR }}
24+
- name: Verify publish crate
25+
run: cargo publish --dry-run
26+
working-directory: ${{ env.CRATE_DIR }}
27+
- name: Publish crate
28+
run: cargo publish
29+
working-directory: ${{ env.CRATE_DIR }}
30+
env:
31+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
32+
create-release:
33+
needs: publish-crate
34+
runs-on: ubuntu-latest
35+
permissions:
36+
contents: write
37+
steps:
38+
- name: Checkout repository
39+
uses: actions/checkout@v3
40+
- name: Create GitHub release
41+
uses: ncipollo/release-action@v1
42+
with:
43+
body: |
44+
Find more info on all releases at https://crates.io/crates/tree-sitter-stack-graphs-ruby.
45+
token: ${{ secrets.GITHUB_TOKEN }}
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-ruby
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-ruby"
3+
version = "0.1.0"
4+
description = "Stack graphs definition for Ruby using tree-sitter-ruby"
5+
readme = "README.md"
6+
keywords = ["tree-sitter", "stack-graphs", "ruby"]
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-ruby"
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 }
33+
tree-sitter-stack-graphs = { version = "0.6.0", path = "../../tree-sitter-stack-graphs" }
34+
tree-sitter-ruby = "0.20.0"
35+
36+
[dev-dependencies]
37+
anyhow = "1.0"
38+
tree-sitter-stack-graphs = { version = "0.6.0", path = "../../tree-sitter-stack-graphs", features = ["cli"] }

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

Whitespace-only changes.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# tree-sitter-stack-graphs definition for Ruby
2+
3+
This project defines tree-sitter-stack-graphs rules for Ruby using the [tree-sitter-ruby][] grammar.
4+
5+
[tree-sitter-ruby]: https://crates.io/crates/tree-sitter-ruby
6+
7+
## Usage
8+
9+
To use this library, add the following to your `Cargo.toml`:
10+
11+
``` toml
12+
[dependencies]
13+
tree-sitter-stack-graphs-ruby = "0.0.1"
14+
```
15+
16+
Check out our [documentation](https://docs.rs/tree-sitter-stack-graphs-ruby/*/) for more details on how to use this library.
17+
18+
## Command-line Program
19+
20+
The command-line program for `tree-sitter-stack-graphs-ruby` lets you do stack graph based analysis and lookup from the command line.
21+
22+
Install the program using `cargo install` as follows:
23+
24+
``` sh
25+
$ cargo install --features cli tree-sitter-stack-graphs-ruby
26+
$ tree-sitter-stack-graphs-ruby --help
27+
```
28+
29+
## Development
30+
31+
The project is written in Rust, and requires a recent version installed. Rust can be installed and updated using [rustup][].
32+
33+
[rustup]: https://rustup.rs/
34+
35+
The project is organized as follows:
36+
37+
- The stack graph rules are defined in `src/stack-graphs.tsg`.
38+
- Builtins sources and configuration are defined in `src/builtins.rb` and `builtins.cfg` respectively.
39+
- Tests are put into the `test` directory.
40+
41+
### Building and Running Tests
42+
43+
Build the project by running:
44+
45+
``` sh
46+
$ cargo build
47+
```
48+
49+
Run the tests as follows:
50+
51+
``` sh
52+
$ cargo test
53+
```
54+
55+
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`.
56+
57+
Run the CLI from source as follows:
58+
59+
``` sh
60+
$ cargo run --features cli -- ARGS
61+
```
62+
63+
Sources are formatted using the standard Rust formatted, which is applied by running:
64+
65+
``` sh
66+
$ cargo fmt
67+
```
68+
69+
### Writing TSG
70+
71+
The stack graph rules are written in [tree-sitter-graph][]. Checkout the [examples][],
72+
which contain self-contained TSG rules for specific language features. A VSCode
73+
[extension][] is available that provides syntax highlighting for TSG files.
74+
75+
[tree-sitter-graph]: https://github.com/tree-sitter/tree-sitter-graph
76+
[examples]: https://github.com/github/stack-graphs/blob/main/tree-sitter-stack-graphs/examples/
77+
[extension]: https://marketplace.visualstudio.com/items?itemName=tree-sitter.tree-sitter-graph
78+
79+
Parse and test a single file by executing the following commands:
80+
81+
``` sh
82+
$ cargo run --features cli -- parse FILES...
83+
$ cargo run --features cli -- test TESTFILES...
84+
```
85+
86+
Generate a visualization to debug failing tests by passing the `-V` flag:
87+
88+
``` sh
89+
$ cargo run --features cli -- test -V TESTFILES...
90+
```
91+
92+
To generate the visualization regardless of test outcome, execute:
93+
94+
``` sh
95+
$ cargo run --features cli -- test -V --output-mode=always TESTFILES...
96+
```
97+
98+
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_ruby::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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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::FileAnalyzers;
9+
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
10+
use tree_sitter_stack_graphs::loader::LoadError;
11+
use tree_sitter_stack_graphs::CancellationFlag;
12+
13+
/// The stack graphs tsg source for this language.
14+
pub const STACK_GRAPHS_TSG_PATH: &str = "src/stack-graphs.tsg";
15+
/// The stack graphs tsg source for this language.
16+
pub const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
17+
18+
/// The stack graphs builtins configuration for this language.
19+
pub const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
20+
/// The stack graphs builtins path for this language
21+
pub const STACK_GRAPHS_BUILTINS_PATH: &str = "src/builtins.rb";
22+
/// The stack graphs builtins source for this language.
23+
pub const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.rb");
24+
25+
/// The name of the file path global variable.
26+
pub const FILE_PATH_VAR: &str = "FILE_PATH";
27+
28+
pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
29+
try_language_configuration(cancellation_flag).unwrap_or_else(|err| panic!("{}", err))
30+
}
31+
32+
pub fn try_language_configuration(
33+
cancellation_flag: &dyn CancellationFlag,
34+
) -> Result<LanguageConfiguration, LoadError> {
35+
LanguageConfiguration::from_sources(
36+
tree_sitter_ruby::language(),
37+
Some(String::from("source.rb")),
38+
None,
39+
vec![String::from("rb")],
40+
STACK_GRAPHS_TSG_PATH.into(),
41+
STACK_GRAPHS_TSG_SOURCE,
42+
Some((
43+
STACK_GRAPHS_BUILTINS_PATH.into(),
44+
STACK_GRAPHS_BUILTINS_SOURCE,
45+
)),
46+
Some(STACK_GRAPHS_BUILTINS_CONFIG),
47+
FileAnalyzers::new(),
48+
cancellation_flag,
49+
)
50+
}
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_ruby::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-ruby/src/builtins.rb

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9+
;; Stack graphs definition for Ruby
10+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11+
12+
;; Global Variables
13+
;; ^^^^^^^^^^^^^^^^
14+
15+
global FILE_PATH
16+
global ROOT_NODE
17+
global JUMP_TO_SCOPE_NODE
18+
19+
;; Attribute Shorthands
20+
;; ^^^^^^^^^^^^^^^^^^^^
21+
22+
attribute node_definition = node => type = "pop_symbol", node_symbol = node, is_definition
23+
attribute node_reference = node => type = "push_symbol", node_symbol = node, is_reference
24+
attribute pop_node = node => type = "pop_symbol", node_symbol = node
25+
attribute pop_scoped_node = node => type = "pop_scoped_symbol", node_symbol = node
26+
attribute pop_scoped_symbol = symbol => type = "pop_scoped_symbol", symbol = symbol
27+
attribute pop_symbol = symbol => type = "pop_symbol", symbol = symbol
28+
attribute push_node = node => type = "push_symbol", node_symbol = node
29+
attribute push_scoped_node = node => type = "push_scoped_symbol", node_symbol = node
30+
attribute push_scoped_symbol = symbol => type = "push_scoped_symbol", symbol = symbol
31+
attribute push_symbol = symbol => type = "push_symbol", symbol = symbol
32+
attribute scoped_node_definition = node => type = "pop_scoped_symbol", node_symbol = node, is_definition
33+
attribute scoped_node_reference = node => type = "push_scoped_symbol", node_symbol = node, is_reference
34+
attribute symbol_definition = symbol => type = "pop_symbol", symbol = symbol, is_definition
35+
attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, is_reference
36+
37+
attribute node_symbol = node => symbol = (source-text node), source_node = node
38+
39+
;; Stack Graph Rules
40+
;; ^^^^^^^^^^^^^^^^^
41+
42+
; Have fun!

languages/tree-sitter-stack-graphs-ruby/test/test.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)