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 committed May 2, 2024
1 parent 0dfab92 commit 4f2404b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
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(),
}
}
}
29 changes: 3 additions & 26 deletions parsing_handlers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
mod java;
mod language;
mod parsing_handlers;

use java::get_default_java_parsing_handlers;
use model::Language;

pub type ParsingHandler = fn(root: model::CSTNode) -> model::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: model::CSTNode<'a>) -> model::CSTNode<'a> {
self.handlers.iter().fold(root, |acc, handler| handler(acc))
}
}

impl From<Language> for ParsingHandlers {
fn from(language: Language) -> Self {
match language {
Language::Java => get_default_java_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 4f2404b

Please sign in to comment.