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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#[cfg(doc)]
use crate::item_iterator::PublicItem;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Token {
Symbol(String),
Qualifier(String),
Kind(String),
Whitespace,
Identifier(String),
Self_(String),
Function(String),
Lifetime(String),
Keyword(String),
Generic(String),
Primitive(String),
Type(String),
}
impl Token {
pub(crate) fn symbol(text: impl Into<String>) -> Self {
Self::Symbol(text.into())
}
pub(crate) fn qualifier(text: impl Into<String>) -> Self {
Self::Qualifier(text.into())
}
pub(crate) fn kind(text: impl Into<String>) -> Self {
Self::Kind(text.into())
}
pub(crate) fn identifier(text: impl Into<String>) -> Self {
Self::Identifier(text.into())
}
pub(crate) fn self_(text: impl Into<String>) -> Self {
Self::Self_(text.into())
}
pub(crate) fn function(text: impl Into<String>) -> Self {
Self::Function(text.into())
}
pub(crate) fn lifetime(text: impl Into<String>) -> Self {
Self::Lifetime(text.into())
}
pub(crate) fn keyword(text: impl Into<String>) -> Self {
Self::Keyword(text.into())
}
pub(crate) fn generic(text: impl Into<String>) -> Self {
Self::Generic(text.into())
}
pub(crate) fn primitive(text: impl Into<String>) -> Self {
Self::Primitive(text.into())
}
pub(crate) fn type_(text: impl Into<String>) -> Self {
Self::Type(text.into())
}
#[allow(clippy::len_without_is_empty)]
#[must_use]
pub fn len(&self) -> usize {
self.text().len()
}
#[must_use]
pub fn text(&self) -> &str {
match self {
Self::Symbol(l)
| Self::Qualifier(l)
| Self::Kind(l)
| Self::Identifier(l)
| Self::Self_(l)
| Self::Function(l)
| Self::Lifetime(l)
| Self::Keyword(l)
| Self::Generic(l)
| Self::Primitive(l)
| Self::Type(l) => l,
Self::Whitespace => " ",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Ord, PartialOrd)]
pub struct TokenStream {
pub tokens: Vec<Token>,
}
impl TokenStream {
pub fn extend(&mut self, tokens: impl Into<Self>) {
self.tokens.extend(tokens.into().tokens);
}
pub(crate) fn push(&mut self, token: Token) {
self.tokens.push(token);
}
#[must_use]
pub fn len(&self) -> usize {
self.tokens.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.tokens.is_empty()
}
pub(crate) fn remove_from_back(&mut self, len: usize) {
self.tokens
.resize(self.tokens.len() - len, Token::Whitespace);
}
pub fn tokens(&self) -> impl Iterator<Item = &Token> + '_ {
self.tokens.iter()
}
#[must_use]
pub fn tokens_len(&self) -> usize {
self.tokens().map(Token::len).sum()
}
}
impl std::fmt::Display for TokenStream {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.tokens().map(Token::text).collect::<String>())
}
}
impl From<Vec<Token>> for TokenStream {
fn from(tokens: Vec<Token>) -> Self {
Self { tokens }
}
}
impl From<&[Token]> for TokenStream {
fn from(tokens: &[Token]) -> Self {
Self {
tokens: tokens.to_vec(),
}
}
}
impl From<Token> for TokenStream {
fn from(token: Token) -> Self {
Self {
tokens: vec![token],
}
}
}