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
use super::Token;
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub enum Delimiter {
/// Zero or more operands with no delimiters.
#[default]
None,
/// Parens surrounding zero or more operands.
Paren,
/// Square brackets surrounding zero or more operands.
Bracket,
/// <> brackets surrounding zero or more operands.
Angle,
/// {} brackets surrounding zero or more operands.
Brace,
/// Parens supporting zero or more operands, or nothing.
OptionalParen,
/// Square brackets supporting zero or more ops, or nothing.
OptionalBracket,
/// <> brackets supporting zero or more ops, or nothing.
OptionalAngle,
/// {} brackets surrounding zero or more operands, or nothing.
OptionalBrace,
}
impl Delimiter {
/// Returns true if this delimiter is required
pub fn is_optional(&self) -> bool {
matches!(
self,
Self::OptionalParen | Self::OptionalBracket | Self::OptionalAngle | Self::OptionalBrace
)
}
/// Returns true if `tok` is the opening token type of this delimiter
pub fn is_open(&self, tok: &Token<'_>) -> bool {
self.open().is_some_and(|open| open.eq(tok))
}
/// Returns true if `c` is the closing character of this delimiter
pub fn is_close(&self, tok: &Token<'_>) -> bool {
self.close().is_some_and(|close| close.eq(tok))
}
/// Returns the character which opens this delimiter, if applicable
pub fn open(&self) -> Option<Token<'static>> {
Some(match self {
Self::None => return None,
Self::Paren | Self::OptionalParen => Token::Lparen,
Self::Bracket | Self::OptionalBracket => Token::Lbracket,
Self::Angle | Self::OptionalAngle => Token::Langle,
Self::Brace | Self::OptionalBrace => Token::Lbrace,
})
}
/// Returns the character which closes this delimiter, if applicable
pub fn close(&self) -> Option<Token<'static>> {
Some(match self {
Self::None => return None,
Self::Paren | Self::OptionalParen => Token::Rparen,
Self::Bracket | Self::OptionalBracket => Token::Rbracket,
Self::Angle | Self::OptionalAngle => Token::Rangle,
Self::Brace | Self::OptionalBrace => Token::Rbrace,
})
}
/// Returns a string which can be used in `expected '('`-like messages
pub fn expected_open(&self) -> &'static str {
match self {
Self::None => "",
Self::Paren | Self::OptionalParen => "'('",
Self::Bracket | Self::OptionalBracket => "'['",
Self::Angle | Self::OptionalAngle => "'<'",
Self::Brace | Self::OptionalBrace => "'{'",
}
}
/// Returns a string which can be used in `expected ')'`-like messages
pub fn expected_close(&self) -> &'static str {
match self {
Self::None => "",
Self::Paren | Self::OptionalParen => "')'",
Self::Bracket | Self::OptionalBracket => "']'",
Self::Angle | Self::OptionalAngle => "'>'",
Self::Brace | Self::OptionalBrace => "'}'",
}
}
}