forked from mozilla/rust-code-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalterator.rs
More file actions
150 lines (136 loc) · 5.24 KB
/
alterator.rs
File metadata and controls
150 lines (136 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::*;
/// A trait to create a richer `AST` node for a programming language, mainly
/// thought to be sent on the network.
pub trait Alterator
where
Self: Checker,
{
/// Creates a new `AST` node containing the code associated to the node,
/// its span, and its children.
///
/// This function can be overloaded according to the needs of each
/// programming language.
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
Self::get_default(node, code, span, children)
}
/// Gets the code as text and the span associated to a node.
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()
} else {
"".to_string()
};
if span {
let (spos_row, spos_column) = node.start_position();
let (epos_row, epos_column) = node.end_position();
(
text,
Some((spos_row + 1, spos_column + 1, epos_row + 1, epos_column + 1)),
)
} else {
(text, None)
}
}
/// Gets a default `AST` node containing the code associated to the node,
/// its span, and its children.
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)
}
/// Gets a new `AST` node if and only if the code is not a comment,
/// otherwise [`None`] is returned.
fn get_ast_node(
node: &Node,
code: &[u8],
children: Vec<AstNode>,
span: bool,
comment: bool,
) -> Option<AstNode> {
if comment && Self::is_comment(node) {
None
} else {
Some(Self::alterate(node, code, span, children))
}
}
}
impl Alterator for PreprocCode {}
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()) {
Cpp::StringLiteral | Cpp::CharLiteral => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
}
Cpp::PreprocDef | Cpp::PreprocFunctionDef | Cpp::PreprocCall => {
if let Some(last) = children.last()
&& last.r#type == "\n"
{
children.pop();
}
Self::get_default(node, code, span, children)
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for PythonCode {}
impl Alterator for JavaCode {}
impl Alterator for KotlinCode {}
impl Alterator for MozjsCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Mozjs::from(node.kind_id()) {
Mozjs::String | Mozjs::String2 => {
// 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())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for JavascriptCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Javascript::from(node.kind_id()) {
Javascript::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for TypescriptCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Typescript::from(node.kind_id()) {
Typescript::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for TsxCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Tsx::from(node.kind_id()) {
Tsx::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for RustCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Rust::from(node.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())
}
_ => Self::get_default(node, code, span, children),
}
}
}