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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::nodes::{Block, Identifier, Token};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LocalFunctionTokens {
pub local: Token,
pub function: Token,
pub opening_parenthese: Token,
pub closing_parenthese: Token,
pub end: Token,
pub parameter_commas: Vec<Token>,
pub variable_arguments: Option<Token>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LocalFunctionStatement {
identifier: Identifier,
block: Block,
parameters: Vec<Identifier>,
is_variadic: bool,
tokens: Option<Box<LocalFunctionTokens>>,
}
impl LocalFunctionStatement {
pub fn new(
identifier: Identifier,
block: Block,
parameters: Vec<Identifier>,
is_variadic: bool,
) -> Self {
Self {
identifier,
block,
parameters,
is_variadic,
tokens: None,
}
}
pub fn from_name<S: Into<Identifier>, B: Into<Block>>(identifier: S, block: B) -> Self {
Self {
identifier: identifier.into(),
block: block.into(),
parameters: Vec::new(),
is_variadic: false,
tokens: None,
}
}
pub fn with_tokens(mut self, tokens: LocalFunctionTokens) -> Self {
self.tokens = Some(tokens.into());
self
}
#[inline]
pub fn set_tokens(&mut self, tokens: LocalFunctionTokens) {
self.tokens = Some(tokens.into());
}
#[inline]
pub fn get_tokens(&self) -> Option<&LocalFunctionTokens> {
self.tokens.as_ref().map(|tokens| tokens.as_ref())
}
pub fn with_parameter<S: Into<Identifier>>(mut self, parameter: S) -> Self {
self.parameters.push(parameter.into());
self
}
pub fn variadic(mut self) -> Self {
self.is_variadic = true;
self
}
#[inline]
pub fn mutate_parameters(&mut self) -> &mut Vec<Identifier> {
&mut self.parameters
}
#[inline]
pub fn mutate_block(&mut self) -> &mut Block {
&mut self.block
}
#[inline]
pub fn mutate_identifier(&mut self) -> &mut Identifier {
&mut self.identifier
}
#[inline]
pub fn get_block(&self) -> &Block {
&self.block
}
#[inline]
pub fn get_parameters(&self) -> &Vec<Identifier> {
&self.parameters
}
#[inline]
pub fn iter_parameters(&self) -> impl Iterator<Item = &Identifier> {
self.parameters.iter()
}
#[inline]
pub fn get_identifier(&self) -> &Identifier {
&self.identifier
}
#[inline]
pub fn get_name(&self) -> &str {
self.identifier.get_name()
}
#[inline]
pub fn has_parameter(&self, name: &str) -> bool {
self.parameters
.iter()
.any(|parameter| parameter.get_name() == name)
}
#[inline]
pub fn has_parameters(&self) -> bool {
!self.parameters.is_empty()
}
#[inline]
pub fn is_variadic(&self) -> bool {
self.is_variadic
}
#[inline]
pub fn parameters_count(&self) -> usize {
self.parameters.len()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn has_parameter_is_true_when_single_param_matches() {
let func = LocalFunctionStatement::from_name("foo", Block::default()).with_parameter("bar");
assert!(func.has_parameter("bar"));
}
#[test]
fn has_parameter_is_true_when_at_least_one_param_matches() {
let func = LocalFunctionStatement::from_name("foo", Block::default())
.with_parameter("bar")
.with_parameter("baz");
assert!(func.has_parameter("baz"));
}
#[test]
fn has_parameter_is_false_when_none_matches() {
let func = LocalFunctionStatement::from_name("foo", Block::default())
.with_parameter("bar")
.with_parameter("baz");
assert!(!func.has_parameter("foo"));
}
}