Skip to content

Commit

Permalink
Move default cursor implementation to algorithm.
Browse files Browse the repository at this point in the history
Similar to how algorithm provides CloneCheckpoint.
  • Loading branch information
gabomatute committed Mar 22, 2023
1 parent 58ee884 commit ce38632
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 60 deletions.
26 changes: 26 additions & 0 deletions src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,29 @@ pub trait Traverse {
#[must_use]
fn move_next_sibling(&mut self) -> bool;
}

impl<C: crate::tree::Traverse> Traverse for C {
type Leaf = C::Node;

fn move_first_leaf(&mut self) -> Self::Leaf {
while self.goto_first_child() {}
self.node()
}

fn move_first_child(&mut self) -> bool {
self.goto_first_child()
}

fn move_next_subtree(&mut self) -> bool {
while !self.goto_next_sibling() {
if !self.goto_parent() {
return false;
}
}
true
}

fn move_next_sibling(&mut self) -> bool {
self.goto_next_sibling()
}
}
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ pub use tree::document;

pub mod lang;

use tree::mts::{Cursor, Node, Tree};

impl pattern::Pattern<String> {
pub fn from_query(query: String, target: &lang::Language) -> Self {
let pattern_language = lang::Select::Semgrep.load().nest("text", target);
let document = document::Document::new(query, &pattern_language, Default::default());
let tree = Tree::new(&query, &pattern_language, Default::default());
let document = document::new::<String, Tree>(query, tree);

use document::{Subtree, Traverse};

Expand All @@ -31,10 +34,10 @@ impl pattern::Pattern<String> {
}
}

impl PartialEq<document::Node<'_>> for String {
fn eq(&self, other: &document::Node) -> bool {
impl PartialEq<document::Node<'_, Node<'_>>> for String {
fn eq(&self, other: &document::Node<Node>) -> bool {
self == other.text()
}
}

impl<'d> algorithm::CloneCheckpoint for document::Cursor<'d> {}
impl algorithm::CloneCheckpoint for document::Cursor<'_, Cursor<'_>> {}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::Parser;
use stsearch as st;

use st::document::{Subtree, Traverse};
use st::tree::mts::Tree;

#[derive(Parser)]
#[command(version)]
Expand All @@ -20,7 +21,8 @@ fn main() {
let pattern = st::pattern::Pattern::from_query(args.query, &language);

let text = std::fs::read_to_string(&args.file).unwrap();
let document = st::document::Document::new(text, &language, Default::default());
let document =
st::document::new::<&str, Tree>(&text, Tree::new(&text, &language, Default::default()));

for m in pattern.find_iter(document.walk()) {
let start = m.start.node().start_position();
Expand Down
26 changes: 0 additions & 26 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,3 @@ impl<Cursor: Traverse> Iterator for Leaves<Cursor> {
})
}
}

impl<C: Traverse> crate::algorithm::Traverse for C {
type Leaf = C::Node;

fn move_first_leaf(&mut self) -> Self::Leaf {
while self.goto_first_child() {}
self.node()
}

fn move_first_child(&mut self) -> bool {
self.goto_first_child()
}

fn move_next_subtree(&mut self) -> bool {
while !self.goto_next_sibling() {
if !self.goto_parent() {
return false;
}
}
true
}

fn move_next_sibling(&mut self) -> bool {
self.goto_next_sibling()
}
}
49 changes: 20 additions & 29 deletions src/tree/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ pub use super::{Subtree, Traverse};

use std::borrow::Borrow;

use super::mts; // default tree

pub struct Document<S = String, T = mts::Tree>
pub struct Document<S, T>
where
S: Borrow<str>,
for<'t> &'t T: Subtree,
Expand All @@ -13,6 +11,14 @@ where
tree: T,
}

pub fn new<S, T>(text: S, tree: T) -> Document<S, T>
where
S: Borrow<str>,
for<'t> &'t T: Subtree,
{
Document::<S, T> { text, tree }
}

impl<S, T> Document<S, T>
where
S: Borrow<str>,
Expand Down Expand Up @@ -46,7 +52,7 @@ where
use std::ops::Deref;

#[derive(Clone)]
pub struct Cursor<'d, C = mts::Cursor<'d>>
pub struct Cursor<'d, C>
where
C: Traverse,
{
Expand Down Expand Up @@ -92,22 +98,25 @@ where
}

#[derive(Clone, Copy)]
pub struct Node<'d, N: Subtree = mts::Node<'d>> {
pub struct Node<'d, N: Subtree> {
text: &'d str,
node: N,
}

use std::ops::Index;

impl<'d, N: Subtree> Node<'d, N>
impl<'d, N: Subtree + Deref> Node<'d, N>
where
for<'n> str: Index<&'n N>,
N::Target: ByteRange,
{
pub fn text(&self) -> &'d <str as Index<&'_ N>>::Output {
&self.text[&self.node]
pub fn text(&self) -> &'d str {
&self.text[self.node.byte_range()]
}
}

pub trait ByteRange {
#[must_use]
fn byte_range(&self) -> std::ops::Range<usize>;
}

impl<'d, N: Subtree> Subtree for Node<'d, N> {
type Cursor = Cursor<'d, N::Cursor>;
type Node = Self;
Expand All @@ -127,21 +136,3 @@ impl<'d, N: Subtree> Deref for Node<'d, N> {
&self.node
}
}

impl<S> Document<S, mts::Tree>
where
S: Borrow<str>,
{
pub fn new(text: S, language: &mts::Language, params: mts::Params) -> Self {
let tree = mts::Tree::new(text.borrow(), language, params);
Self { text, tree }
}
}

impl<'n> Index<&'n mts::Node<'_>> for str {
type Output = str;

fn index(&self, index: &'n mts::Node) -> &Self::Output {
&self[index.byte_range()]
}
}
6 changes: 6 additions & 0 deletions src/tree/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ impl<'t> Subtree for Node<'t> {
(&self).walk()
}
}

impl super::document::ByteRange for Node<'_> {
fn byte_range(&self) -> std::ops::Range<usize> {
self.byte_range()
}
}

0 comments on commit ce38632

Please sign in to comment.