Skip to content

Commit

Permalink
feat(parsing,parsing_handlers): add API to enhance parsing result (#49)
Browse files Browse the repository at this point in the history
This PR introduces an API that enables users to augment Tree Sitter's
output, enriching the tree structure with additional information.
  • Loading branch information
jpedroh authored May 2, 2024
1 parent d5b3c87 commit 2c3f041
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 5 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions parsing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ edition = "2021"
tree-sitter = "0.20.9"
tree-sitter-java = "0.20.0"
model = { path = "../model" }
parsing_handlers = { path = "../parsing_handlers" }
log = { workspace = true }
uuid = { workspace = true }
10 changes: 5 additions & 5 deletions parsing/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ pub fn parse_string<'a>(
.set_language(config.language)
.map_err(|_| "There was an error while setting the parser language")?;

let parsed = parser.parse(src, None);
match parsed {
Some(parsed) => Result::Ok(explore_node(parsed.root_node(), src, config)),
None => Result::Err("It was not possible to parse the tree."),
}
let parsed = parser
.parse(src, None)
.ok_or("It was not possible to parse the tree.")?;
let root = explore_node(parsed.root_node(), src, config);
Ok(config.handlers.run(root))
}
3 changes: 3 additions & 0 deletions parsing/src/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use model::Language;
use parsing_handlers::ParsingHandlers;
use std::collections::HashSet;

pub struct ParserConfiguration {
pub(crate) language: tree_sitter::Language,
pub(crate) stop_compilation_at: HashSet<&'static str>,
pub(crate) kinds_with_unordered_children: HashSet<&'static str>,
pub(crate) block_end_delimiters: HashSet<&'static str>,
pub(crate) handlers: ParsingHandlers,
}

impl From<Language> for ParserConfiguration {
Expand All @@ -21,6 +23,7 @@ impl From<Language> for ParserConfiguration {
]
.into(),
block_end_delimiters: ["}"].into(),
handlers: ParsingHandlers::from(Language::Java),
},
}
}
Expand Down
11 changes: 11 additions & 0 deletions parsing_handlers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "parsing_handlers"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
model = { path = "../model" }
log = { workspace = true }
uuid = { workspace = true }
5 changes: 5 additions & 0 deletions parsing_handlers/src/java/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::ParsingHandlers;

pub fn get_default_java_parsing_handlers() -> ParsingHandlers {
ParsingHandlers::new(vec![])
}
10 changes: 10 additions & 0 deletions parsing_handlers/src/language.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::{java::get_default_java_parsing_handlers, ParsingHandlers};
use model::Language;

impl From<Language> for ParsingHandlers {
fn from(language: Language) -> Self {
match language {
Language::Java => get_default_java_parsing_handlers(),
}
}
}
5 changes: 5 additions & 0 deletions parsing_handlers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod java;
mod language;
mod parsing_handlers;

pub use parsing_handlers::{ParsingHandler, ParsingHandlers};
17 changes: 17 additions & 0 deletions parsing_handlers/src/parsing_handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use model::CSTNode;

pub type ParsingHandler = fn(root: CSTNode) -> CSTNode;

pub struct ParsingHandlers {
handlers: Vec<ParsingHandler>,
}

impl ParsingHandlers {
pub fn new(handlers: Vec<ParsingHandler>) -> Self {
Self { handlers }
}

pub fn run<'a>(&'a self, root: CSTNode<'a>) -> CSTNode<'a> {
self.handlers.iter().fold(root, |acc, handler| handler(acc))
}
}

0 comments on commit 2c3f041

Please sign in to comment.