Skip to content

Commit

Permalink
Use the node variant in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Luni-4 committed Jun 5, 2020
1 parent f250749 commit 862ee71
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 158 deletions.
43 changes: 21 additions & 22 deletions src/alterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use tree_sitter::Node;

use crate::*;

pub trait Alterator
Expand All @@ -12,13 +10,14 @@ where

fn get_text_span(node: &Node, code: &[u8], span: bool, text: bool) -> (String, Span) {
let text = if text {
String::from_utf8(code[node.start_byte()..node.end_byte()].to_vec()).unwrap()
String::from_utf8(code[node.object().start_byte()..node.object().end_byte()].to_vec())
.unwrap()
} else {
"".to_string()
};
if span {
let spos = node.start_position();
let epos = node.end_position();
let spos = node.object().start_position();
let epos = node.object().end_position();
(
text,
Some((spos.row + 1, spos.column + 1, epos.row + 1, epos.column + 1)),
Expand All @@ -29,8 +28,8 @@ where
}

fn get_default(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
let (text, span) = Self::get_text_span(node, code, span, node.child_count() == 0);
AstNode::new(node.kind(), text, span, children)
let (text, span) = Self::get_text_span(node, code, span, node.object().child_count() == 0);
AstNode::new(node.object().kind(), text, span, children)
}

fn get_ast_node(
Expand All @@ -54,10 +53,10 @@ impl Alterator for CcommentCode {}

impl Alterator for CppCode {
fn alterate(node: &Node, code: &[u8], span: bool, mut children: Vec<AstNode>) -> AstNode {
match Cpp::from(node.kind_id()) {
match Cpp::from(node.object().kind_id()) {
Cpp::StringLiteral | Cpp::CharLiteral => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
AstNode::new(node.object().kind(), text, span, Vec::new())
}
Cpp::PreprocDef | Cpp::PreprocFunctionDef | Cpp::PreprocCall => {
if let Some(last) = children.last() {
Expand All @@ -80,12 +79,12 @@ impl Alterator for JavaCode {}

impl Alterator for MozjsCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Mozjs::from(node.kind_id()) {
match Mozjs::from(node.object().kind_id()) {
Mozjs::String => {
// TODO: have a thought about template_strings:
// they may have children for replacement...
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
Expand All @@ -94,10 +93,10 @@ impl Alterator for MozjsCode {

impl Alterator for JavascriptCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Javascript::from(node.kind_id()) {
match Javascript::from(node.object().kind_id()) {
Javascript::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
Expand All @@ -106,10 +105,10 @@ impl Alterator for JavascriptCode {

impl Alterator for TypescriptCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Typescript::from(node.kind_id()) {
match Typescript::from(node.object().kind_id()) {
Typescript::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
Expand All @@ -118,10 +117,10 @@ impl Alterator for TypescriptCode {

impl Alterator for TsxCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Tsx::from(node.kind_id()) {
match Tsx::from(node.object().kind_id()) {
Tsx::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
Expand All @@ -130,10 +129,10 @@ impl Alterator for TsxCode {

impl Alterator for GoCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Go::from(node.kind_id()) {
match Go::from(node.object().kind_id()) {
Go::InterpretedStringLiteral => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
Expand All @@ -144,7 +143,7 @@ impl Alterator for CssCode {}

impl Alterator for HtmlCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Html::from(node.kind_id()) {
match Html::from(node.object().kind_id()) {
Html::QuotedAttributeValue => {
if let [q1, attr, q2] = &children[..] {
let span = if span {
Expand All @@ -167,10 +166,10 @@ impl Alterator for HtmlCode {

impl Alterator for RustCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Rust::from(node.kind_id()) {
match Rust::from(node.object().kind_id()) {
Rust::StringLiteral | Rust::CharLiteral => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
Expand Down
10 changes: 5 additions & 5 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl AstNode {
fn build<T: TSParserTrait>(parser: &T, span: bool, comment: bool) -> Option<AstNode> {
let code = parser.get_code();
let root = parser.get_root();
let mut cursor = root.walk();
let mut cursor = root.object().walk();
let mut node_stack = Vec::new();
let mut child_stack = Vec::new();

Expand All @@ -92,11 +92,11 @@ fn build<T: TSParserTrait>(parser: &T, span: bool, comment: bool) -> Option<AstN
So once we have built the array of children we can build the node itself until the root. */
loop {
let ts_node = node_stack.last().unwrap();
cursor.reset(*ts_node);
cursor.reset(ts_node.object());
if cursor.goto_first_child() {
let node = cursor.node();
child_stack.push(Vec::with_capacity(node.child_count()));
node_stack.push(node);
node_stack.push(Node::new(node));
} else {
loop {
let ts_node = node_stack.pop().unwrap();
Expand All @@ -113,9 +113,9 @@ fn build<T: TSParserTrait>(parser: &T, span: bool, comment: bool) -> Option<AstN
return Some(node);
}
}
if let Some(next_node) = ts_node.next_sibling() {
if let Some(next_node) = ts_node.object().next_sibling() {
child_stack.push(Vec::with_capacity(next_node.child_count()));
node_stack.push(next_node);
node_stack.push(Node::new(next_node));
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/asttools.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use tree_sitter::Node;
use crate::node::Node;

#[allow(dead_code)]
pub fn get_parent<'a>(node: &'a Node<'a>, level: usize) -> Option<Node<'a>> {
let mut level = level;
let mut node = *node;
while level != 0 {
if let Some(parent) = node.parent() {
node = parent;
if let Some(parent) = node.object().parent() {
node = Node::new(parent);
} else {
return None;
}
Expand All @@ -24,10 +24,10 @@ macro_rules! has_ancestors {
loop {
let mut node = *$node;
$(
if let Some(parent) = node.parent() {
if let Some(parent) = node.object().parent() {
match parent.kind_id().into() {
$typ => {
node = parent;
node = Node::new(parent);
},
_ => {
break;
Expand All @@ -37,7 +37,7 @@ macro_rules! has_ancestors {
break;
}
)*
if let Some(parent) = node.parent() {
if let Some(parent) = node.object().parent() {
match parent.kind_id().into() {
$( $typs )|+ => {
res = true;
Expand Down
14 changes: 7 additions & 7 deletions src/checker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use aho_corasick::AhoCorasick;
use regex::bytes::Regex;
use tree_sitter::Node;

use crate::*;

Expand All @@ -19,7 +18,7 @@ pub trait Checker {
fn is_non_arg(node: &Node) -> bool;

fn is_error(node: &Node) -> bool {
node.is_error()
node.object().is_error()
}
}

Expand All @@ -44,7 +43,7 @@ impl Checker for CcommentCode {
lazy_static! {
static ref AC: AhoCorasick = AhoCorasick::new(vec![b"<div rustbindgen"]);
}
let code = &code[node.start_byte()..node.end_byte()];
let code = &code[node.object().start_byte()..node.object().end_byte()];
AC.is_match(code)
}
}
Expand Down Expand Up @@ -79,7 +78,7 @@ impl Checker for CppCode {
lazy_static! {
static ref AC: AhoCorasick = AhoCorasick::new(vec![b"<div rustbindgen"]);
}
let code = &code[node.start_byte()..node.end_byte()];
let code = &code[node.object().start_byte()..node.object().end_byte()];
AC.is_match(code)
}
mk_checker!(is_non_arg, LPAREN, LPAREN2, COMMA, RPAREN);
Expand Down Expand Up @@ -117,7 +116,8 @@ impl Checker for PythonCode {
// comment containing coding info are useful
static ref RE: Regex = Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap();
}
node.start_position().row <= 1 && RE.is_match(&code[node.start_byte()..node.end_byte()])
node.object().start_position().row <= 1
&& RE.is_match(&code[node.object().start_byte()..node.object().end_byte()])
}

mk_checker!(is_string, String, ConcatenatedString);
Expand Down Expand Up @@ -286,13 +286,13 @@ impl Checker for RustCode {
mk_checker!(is_comment, LineComment, BlockComment);

fn is_useful_comment(node: &Node, code: &[u8]) -> bool {
if let Some(parent) = node.parent() {
if let Some(parent) = node.object().parent() {
if parent.kind_id() == Rust::TokenTree {
// A comment could be a macro token
return true;
}
}
let code = &code[node.start_byte()..node.end_byte()];
let code = &code[node.object().start_byte()..node.object().end_byte()];
code.starts_with(b"/// cbindgen:")
}

Expand Down
12 changes: 7 additions & 5 deletions src/comment_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::io::{self, Write};
use std::path::PathBuf;

use crate::checker::Checker;
use crate::node::Node;

use crate::tools::*;
use crate::traits::*;

Expand All @@ -11,21 +13,21 @@ const CR: [u8; 8192] = [b'\n'; 8192];
pub fn rm_comments<T: TSParserTrait>(parser: &T) -> Option<Vec<u8>> {
let node = parser.get_root();
let mut stack = Vec::new();
let mut cursor = node.walk();
let mut cursor = node.object().walk();
let mut spans = Vec::new();

stack.push(node);

while let Some(node) = stack.pop() {
if T::Checker::is_comment(&node) && !T::Checker::is_useful_comment(&node, parser.get_code())
{
let lines = node.end_position().row - node.start_position().row;
spans.push((node.start_byte(), node.end_byte(), lines));
let lines = node.object().end_position().row - node.object().start_position().row;
spans.push((node.object().start_byte(), node.object().end_byte(), lines));
} else {
cursor.reset(node);
cursor.reset(node.object());
if cursor.goto_first_child() {
loop {
stack.push(cursor.node());
stack.push(Node::new(cursor.node()));
if !cursor.goto_next_sibling() {
break;
}
Expand Down
7 changes: 4 additions & 3 deletions src/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use num_format::{Locale, ToFormattedString};
use std::fmt;
use std::sync::{Arc, Mutex};

use crate::node::Node;
use crate::traits::*;

/// Counts the types of nodes specified in the input slice
/// and the number of nodes in a code.
pub fn count<'a, T: TSParserTrait>(parser: &'a T, filters: &[String]) -> (usize, usize) {
let filters = parser.get_filters(filters);
let node = parser.get_root();
let mut cursor = node.walk();
let mut cursor = node.object().walk();
let mut stack = Vec::new();
let mut good = 0;
let mut total = 0;
Expand All @@ -23,10 +24,10 @@ pub fn count<'a, T: TSParserTrait>(parser: &'a T, filters: &[String]) -> (usize,
if filters.any(&node) {
good += 1;
}
cursor.reset(node);
cursor.reset(node.object());
if cursor.goto_first_child() {
loop {
stack.push(cursor.node());
stack.push(Node::new(cursor.node()));
if !cursor.goto_next_sibling() {
break;
}
Expand Down
9 changes: 5 additions & 4 deletions src/find.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;
use tree_sitter::Node;

use crate::node::Node;

use crate::dump::*;
use crate::traits::*;
Expand All @@ -8,7 +9,7 @@ use crate::traits::*;
pub fn find<'a, T: TSParserTrait>(parser: &'a T, filters: &[String]) -> Option<Vec<Node<'a>>> {
let filters = parser.get_filters(filters);
let node = parser.get_root();
let mut cursor = node.walk();
let mut cursor = node.object().walk();
let mut stack = Vec::new();
let mut good = Vec::new();
let mut children = Vec::new();
Expand All @@ -19,10 +20,10 @@ pub fn find<'a, T: TSParserTrait>(parser: &'a T, filters: &[String]) -> Option<V
if filters.any(&node) {
good.push(node);
}
cursor.reset(node);
cursor.reset(node.object());
if cursor.goto_first_child() {
loop {
children.push(cursor.node());
children.push(Node::new(cursor.node()));
if !cursor.goto_next_sibling() {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub fn function<T: TSParserTrait>(parser: &T) -> Vec<FunctionSpan> {
let mut spans = Vec::new();
root.act_on_node(&mut |n| {
if T::Checker::is_func(n) {
let start_line = n.start_position().row + 1;
let end_line = n.end_position().row + 1;
let start_line = n.object().start_position().row + 1;
let end_line = n.object().end_position().row + 1;
if let Some(name) = T::Getter::get_func_name(n, code) {
spans.push(FunctionSpan {
name: name.to_string(),
Expand Down
Loading

0 comments on commit 862ee71

Please sign in to comment.