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
use crate::nodes::{Expression, Token};
/// Contains token information for a parenthesized expression.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParentheseTokens {
/// The left (opening) parenthesis token
pub left_parenthese: Token,
/// The right (closing) parenthesis token
pub right_parenthese: Token,
}
impl ParentheseTokens {
super::impl_token_fns!(target = [left_parenthese, right_parenthese]);
}
/// Represents a parenthesized expression.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParentheseExpression {
expression: Expression,
tokens: Option<ParentheseTokens>,
}
impl ParentheseExpression {
/// Creates a new parenthesized expression containing the given expression.
pub fn new<E: Into<Expression>>(expression: E) -> Self {
Self {
expression: expression.into(),
tokens: None,
}
}
/// Returns a reference to the inner expression contained in the parentheses.
#[inline]
pub fn inner_expression(&self) -> &Expression {
&self.expression
}
/// Consumes this parenthesized expression and returns the inner expression.
#[inline]
pub fn into_inner_expression(self) -> Expression {
self.expression
}
/// Returns a mutable reference to the inner expression contained in the parentheses.
#[inline]
pub fn mutate_inner_expression(&mut self) -> &mut Expression {
&mut self.expression
}
/// Attaches tokens to this parenthesized expression.
pub fn with_tokens(mut self, tokens: ParentheseTokens) -> Self {
self.tokens = Some(tokens);
self
}
/// Attaches tokens to this parenthesized expression.
#[inline]
pub fn set_tokens(&mut self, tokens: ParentheseTokens) {
self.tokens = Some(tokens);
}
/// Returns a reference to the tokens attached to this parenthesized expression, if any.
#[inline]
pub fn get_tokens(&self) -> Option<&ParentheseTokens> {
self.tokens.as_ref()
}
/// Returns a mutable reference to the tokens attached to this parenthesized expression, if any.
#[inline]
pub fn mutate_tokens(&mut self) -> Option<&mut ParentheseTokens> {
self.tokens.as_mut()
}
/// Returns a mutable reference to the first token of this parenthesized expression.
///
/// Ensures the left parenthesis token exists and returns it.
pub fn mutate_first_token(&mut self) -> &mut Token {
self.set_default_tokens();
&mut self.mutate_tokens().unwrap().left_parenthese
}
/// Returns a mutable reference to the last token of this parenthesized expression,
/// creating it if missing.
pub fn mutate_last_token(&mut self) -> &mut Token {
self.set_default_tokens();
&mut self.mutate_tokens().unwrap().right_parenthese
}
fn set_default_tokens(&mut self) {
if self.tokens.is_none() {
self.tokens = Some(ParentheseTokens {
left_parenthese: Token::from_content("("),
right_parenthese: Token::from_content(")"),
});
}
}
super::impl_token_fns!(iter = [tokens]);
}