Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade tree sitter #162

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v0.12.0 -- 2024-12-12

Upgraded the `tree-sitter` dependency to version 0.24.

## v0.11.3 -- 2024-05-29

### Library
Expand Down
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tree-sitter-graph"
version = "0.11.3"
version = "0.12.0"
description = "Construct graphs from parsed source code"
homepage = "https://github.com/tree-sitter/tree-sitter-graph/"
repository = "https://github.com/tree-sitter/tree-sitter-graph/"
Expand Down Expand Up @@ -33,13 +33,13 @@ regex = "1.3.2"
serde = "1.0"
serde_json = "1.0"
smallvec = { version="1.6", features=["union"] }
string-interner = { version = "0.12", default-features = false, features = ["std", "inline-more", "backends"] }
streaming-iterator = "0.1.9"
thiserror = "1.0.7"
tree-sitter = "0.20.3"
tree-sitter-config = { version = "0.19", optional = true }
tree-sitter-loader = { version = "0.20", optional = true }
tree-sitter = "0.24"
tree-sitter-config = { version = "0.24", optional = true }
tree-sitter-loader = { version = "0.24", optional = true }

[dev-dependencies]
env_logger = "0.9"
indoc = "1.0"
tree-sitter-python = "0.20"
tree-sitter-python = "=0.23.5"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To use it as a library, add the following to your `Cargo.toml`:

``` toml
[dependencies]
tree-sitter-graph = "0.11"
tree-sitter-graph = "0.12"
```

To use it as a program, install it via `cargo install`:
Expand Down
6 changes: 3 additions & 3 deletions src/bin/tree-sitter-graph/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn main() -> Result<()> {
)?;
}

let config = Config::load()?;
let config = Config::load(None)?;
let mut loader = Loader::new()?;
let loader_config = config.get()?;
loader.find_all_languages(&loader_config)?;
Expand All @@ -98,7 +98,7 @@ fn main() -> Result<()> {
let tsg = std::fs::read(tsg_path)
.with_context(|| format!("Cannot read TSG file {}", tsg_path.display()))?;
let tsg = String::from_utf8(tsg)?;
let file = match File::from_str(language, &tsg) {
let file = match File::from_str(language.clone(), &tsg) {
Ok(file) => file,
Err(err) => {
eprintln!("{}", err.display_pretty(tsg_path, &tsg));
Expand All @@ -110,7 +110,7 @@ fn main() -> Result<()> {
.with_context(|| format!("Cannot read source file {}", source_path.display()))?;
let source = String::from_utf8(source)?;
let mut parser = Parser::new();
parser.set_language(language)?;
parser.set_language(&language)?;
let tree = parser
.parse(&source, None)
.ok_or_else(|| anyhow!("Cannot parse {}", source_path.display()))?;
Expand Down
2 changes: 1 addition & 1 deletion src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl ast::Stanza {
.expect("capture should have index")
!= self.full_match_stanza_capture_index as u32
})
.map(|cn| Identifier::from(cn.as_str()))
.map(|cn| Identifier::from(*cn))
.collect::<HashSet<_>>();
let unused_captures = all_captures
.difference(&used_captures)
Expand Down
20 changes: 10 additions & 10 deletions src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ impl File {
.iter()
.map(|name| {
let index = file_query
.capture_index_for_name(name)
.capture_index_for_name(*name)
.expect("missing index for capture");
let quantifier =
file_query.capture_quantifiers(mat.pattern_index)[index as usize];
(name, quantifier, index)
(*name, quantifier, index)
})
.filter(|c| c.2 != stanza.full_match_file_capture_index as u32)
.collect();
Expand All @@ -143,10 +143,10 @@ impl File {
.map(|name| {
let index = stanza
.query
.capture_index_for_name(name)
.capture_index_for_name(*name)
.expect("missing index for capture");
let quantifier = stanza.query.capture_quantifiers(0)[index as usize];
(name, quantifier, index)
(*name, quantifier, index)
})
.filter(|c| c.2 != stanza.full_match_stanza_capture_index as u32)
.collect();
Expand Down Expand Up @@ -179,10 +179,10 @@ impl Stanza {
.map(|name| {
let index = self
.query
.capture_index_for_name(name)
.capture_index_for_name(*name)
.expect("missing index for capture");
let quantifier = self.query.capture_quantifiers(0)[index as usize];
(name, quantifier, index)
(*name, quantifier, index)
})
.filter(|c| c.2 != self.full_match_stanza_capture_index as u32)
.collect();
Expand All @@ -197,9 +197,9 @@ impl Stanza {
}

pub struct Match<'a, 'tree> {
mat: QueryMatch<'a, 'tree>,
mat: &'a QueryMatch<'a, 'tree>,
full_capture_index: u32,
named_captures: Vec<(&'a String, CaptureQuantifier, u32)>,
named_captures: Vec<(&'a str, CaptureQuantifier, u32)>,
query_location: Location,
}

Expand All @@ -217,7 +217,7 @@ impl<'a, 'tree> Match<'a, 'tree> {
&'s self,
) -> impl Iterator<
Item = (
&String,
&'a str,
CaptureQuantifier,
impl Iterator<Item = Node<'tree>> + 's,
),
Expand All @@ -239,7 +239,7 @@ impl<'a, 'tree> Match<'a, 'tree> {
}

/// Return an iterator over all capture names.
pub fn capture_names(&self) -> impl Iterator<Item = &String> {
pub fn capture_names(&self) -> impl Iterator<Item = &str> {
self.named_captures.iter().map(|c| c.0)
}

Expand Down
8 changes: 5 additions & 3 deletions src/execution/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use tree_sitter::QueryCursor;
use tree_sitter::QueryMatch;
use tree_sitter::Tree;

use streaming_iterator::StreamingIterator;

use crate::ast;
use crate::execution::error::ExecutionError;
use crate::execution::error::ResultWithExecutionError;
Expand Down Expand Up @@ -116,12 +118,12 @@ impl ast::File {
mut visit: F,
) -> Result<(), E>
where
F: FnMut(&ast::Stanza, QueryMatch<'_, 'tree>) -> Result<(), E>,
F: FnMut(&ast::Stanza, &QueryMatch<'_, 'tree>) -> Result<(), E>,
{
let mut cursor = QueryCursor::new();
let query = self.query.as_ref().unwrap();
let matches = cursor.matches(query, tree.root_node(), source.as_bytes());
for mat in matches {
let mut matches = cursor.matches(query, tree.root_node(), source.as_bytes());
while let Some(mat) = matches.next() {
let stanza = &self.stanzas[mat.pattern_index];
visit(stanza, mat)?;
}
Expand Down
9 changes: 5 additions & 4 deletions src/execution/strict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::collections::HashSet;
use streaming_iterator::StreamingIterator;
use tree_sitter::QueryCursor;
use tree_sitter::QueryMatch;
use tree_sitter::Tree;
Expand Down Expand Up @@ -115,7 +116,7 @@ impl File {
mut visit: F,
) -> Result<(), E>
where
F: FnMut(&Stanza, QueryMatch<'_, 'tree>) -> Result<(), E>,
F: FnMut(&Stanza, &QueryMatch<'_, 'tree>) -> Result<(), E>,
{
for stanza in &self.stanzas {
stanza.try_visit_matches_strict(tree, source, |mat| visit(stanza, mat))?;
Expand Down Expand Up @@ -214,11 +215,11 @@ impl Stanza {
mut visit: F,
) -> Result<(), E>
where
F: FnMut(QueryMatch<'_, 'tree>) -> Result<(), E>,
F: FnMut(&QueryMatch<'_, 'tree>) -> Result<(), E>,
{
let mut cursor = QueryCursor::new();
let matches = cursor.matches(&self.query, tree.root_node(), source.as_bytes());
for mat in matches {
let mut matches = cursor.matches(&self.query, tree.root_node(), source.as_bytes());
while let Some(mat) = matches.next() {
visit(mat)?;
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'tree> ParseError<'tree> {
}

/// Return all parse errors in the given tree.
pub fn all(tree: &'tree Tree) -> Vec<ParseError> {
pub fn all(tree: &'tree Tree) -> Vec<ParseError<'tree>> {
let mut errors = Vec::new();
find_errors(tree, &mut errors, false);
errors
Expand Down
8 changes: 4 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ impl<'a> Parser<'a> {
let name = self.parse_identifier("inherit")?;
file.inherited_variables.insert(name);
} else {
let stanza = self.parse_stanza(file.language)?;
let stanza = self.parse_stanza(&file.language)?;
file.stanzas.push(stanza);
}
self.consume_whitespace();
}
// we can unwrap here because all queries have already been parsed before
file.query = Some(Query::new(file.language, &self.query_source).unwrap());
file.query = Some(Query::new(&file.language, &self.query_source).unwrap());
Ok(())
}

Expand Down Expand Up @@ -369,7 +369,7 @@ impl<'a> Parser<'a> {
Ok(quantifier)
}

fn parse_stanza(&mut self, language: Language) -> Result<ast::Stanza, ParseError> {
fn parse_stanza(&mut self, language: &Language) -> Result<ast::Stanza, ParseError> {
let start = self.location;
let (query, full_match_stanza_capture_index) = self.parse_query(language)?;
self.consume_whitespace();
Expand All @@ -385,7 +385,7 @@ impl<'a> Parser<'a> {
})
}

fn parse_query(&mut self, language: Language) -> Result<(Query, usize), ParseError> {
fn parse_query(&mut self, language: &Language) -> Result<(Query, usize), ParseError> {
let location = self.location;
let query_start = self.offset;
self.skip_query()?;
Expand Down
6 changes: 4 additions & 2 deletions tests/it/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ fn init_log() {
fn execute(python_source: &str, dsl_source: &str) -> Result<String, ExecutionError> {
init_log();
let mut parser = Parser::new();
parser.set_language(tree_sitter_python::language()).unwrap();
parser
.set_language(&tree_sitter_python::LANGUAGE.into())
.unwrap();
let tree = parser.parse(python_source, None).unwrap();
let file =
File::from_str(tree_sitter_python::language(), dsl_source).expect("Cannot parse file");
File::from_str(tree_sitter_python::LANGUAGE.into(), dsl_source).expect("Cannot parse file");
let functions = Functions::stdlib();
let mut globals = Variables::new();
globals
Expand Down
6 changes: 4 additions & 2 deletions tests/it/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ fn init_log() {
fn execute(python_source: &str, dsl_source: &str) -> Result<String, ExecutionError> {
init_log();
let mut parser = Parser::new();
parser.set_language(tree_sitter_python::language()).unwrap();
parser
.set_language(&tree_sitter_python::LANGUAGE.into())
.unwrap();
let tree = parser.parse(python_source, None).unwrap();
let file =
File::from_str(tree_sitter_python::language(), dsl_source).expect("Cannot parse file");
File::from_str(tree_sitter_python::LANGUAGE.into(), dsl_source).expect("Cannot parse file");
let functions = Functions::stdlib();
let mut globals = Variables::new();
globals
Expand Down
4 changes: 3 additions & 1 deletion tests/it/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ fn can_iterate_graph_edges() {
fn can_display_graph() {
let python_source = "pass";
let mut parser = Parser::new();
parser.set_language(tree_sitter_python::language()).unwrap();
parser
.set_language(&tree_sitter_python::LANGUAGE.into())
.unwrap();
let tree = parser.parse(python_source, None).unwrap();

let mut graph = Graph::new();
Expand Down
6 changes: 4 additions & 2 deletions tests/it/lazy_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ fn init_log() {
fn execute(python_source: &str, dsl_source: &str) -> Result<String, ExecutionError> {
init_log();
let mut parser = Parser::new();
parser.set_language(tree_sitter_python::language()).unwrap();
parser
.set_language(&tree_sitter_python::LANGUAGE.into())
.unwrap();
let tree = parser.parse(python_source, None).unwrap();
let file =
File::from_str(tree_sitter_python::language(), dsl_source).expect("Cannot parse file");
File::from_str(tree_sitter_python::LANGUAGE.into(), dsl_source).expect("Cannot parse file");
let functions = Functions::stdlib();
let mut globals = Variables::new();
globals
Expand Down
4 changes: 3 additions & 1 deletion tests/it/parse_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ fn init_log() {
fn parse(python_source: &str) -> Tree {
init_log();
let mut parser = Parser::new();
parser.set_language(tree_sitter_python::language()).unwrap();
parser
.set_language(&tree_sitter_python::LANGUAGE.into())
.unwrap();
parser.parse(python_source, None).unwrap()
}

Expand Down
Loading
Loading