sway_ast/
token.rs

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use crate::priv_prelude::*;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Spacing {
    Joint,
    Alone,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct Punct {
    pub span: Span,
    pub kind: PunctKind,
    pub spacing: Spacing,
}

impl Spanned for Punct {
    fn span(&self) -> Span {
        self.span.clone()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct GenericGroup<T> {
    pub delimiter: Delimiter,
    pub token_stream: T,
    pub span: Span,
}

pub type Group = GenericGroup<TokenStream>;
pub type CommentedGroup = GenericGroup<CommentedTokenStream>;

impl<T> Spanned for GenericGroup<T> {
    fn span(&self) -> Span {
        self.span.clone()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum CommentKind {
    /// A newlined comment is a comment with a preceding newline before another token.
    ///
    /// # Examples
    ///
    /// ```sway
    /// pub fn main() -> bool {
    ///
    ///     // Newlined comment
    ///     true
    /// }
    /// ```
    Newlined,

    /// A trailing comment is a comment without a preceding newline before another token.
    ///
    /// # Examples
    ///
    /// ```sway
    /// var foo = 1; // Trailing comment
    /// ```
    Trailing,

    /// An inlined comment is a block comment nested between 2 tokens without a newline after it.
    ///
    /// # Examples
    ///
    /// ```sway
    /// fn some_function(baz: /* inlined comment */ u64) {}
    /// ```
    Inlined,

    /// A multiline comment is a block comment that may be nested between 2 tokens with 1 or more newlines within it.
    ///
    /// # Examples
    ///
    /// ```sway
    /// fn some_function(baz: /* multiline
    ///                          comment */ u64) {}
    /// ```
    Multilined,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct Comment {
    pub span: Span,
    pub comment_kind: CommentKind,
}

impl Spanned for Comment {
    fn span(&self) -> Span {
        self.span.clone()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum DocStyle {
    Outer,
    Inner,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct DocComment {
    pub span: Span,
    pub content_span: Span,
    pub doc_style: DocStyle,
}

impl Spanned for DocComment {
    fn span(&self) -> Span {
        self.span.clone()
    }
}

/// Allows for generalizing over commented and uncommented token streams.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum GenericTokenTree<T> {
    Punct(Punct),
    Ident(Ident),
    Group(GenericGroup<T>),
    Literal(Literal),
    DocComment(DocComment),
}

pub type TokenTree = GenericTokenTree<TokenStream>;
pub type CommentedTree = GenericTokenTree<CommentedTokenStream>;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum CommentedTokenTree {
    Comment(Comment),
    Tree(CommentedTree),
}

impl CommentedGroup {
    pub fn strip_comments(self) -> Group {
        Group {
            delimiter: self.delimiter,
            token_stream: self.token_stream.strip_comments(),
            span: self.span,
        }
    }
}

impl<T> Spanned for GenericTokenTree<T> {
    fn span(&self) -> Span {
        match self {
            Self::Punct(punct) => punct.span(),
            Self::Ident(ident) => ident.span(),
            Self::Group(group) => group.span(),
            Self::Literal(literal) => literal.span(),
            Self::DocComment(doc_comment) => doc_comment.span(),
        }
    }
}

impl Spanned for CommentedTokenTree {
    fn span(&self) -> Span {
        match self {
            Self::Comment(cmt) => cmt.span(),
            Self::Tree(tt) => tt.span(),
        }
    }
}

impl<T> From<Punct> for GenericTokenTree<T> {
    fn from(punct: Punct) -> Self {
        Self::Punct(punct)
    }
}

impl<T> From<Ident> for GenericTokenTree<T> {
    fn from(ident: Ident) -> Self {
        Self::Ident(ident)
    }
}

impl<T> From<GenericGroup<T>> for GenericTokenTree<T> {
    fn from(group: GenericGroup<T>) -> Self {
        Self::Group(group)
    }
}

impl<T> From<Literal> for GenericTokenTree<T> {
    fn from(lit: Literal) -> Self {
        Self::Literal(lit)
    }
}

impl<T> From<DocComment> for GenericTokenTree<T> {
    fn from(doc_comment: DocComment) -> Self {
        Self::DocComment(doc_comment)
    }
}

impl From<Comment> for CommentedTokenTree {
    fn from(comment: Comment) -> Self {
        Self::Comment(comment)
    }
}

impl From<CommentedTree> for CommentedTokenTree {
    fn from(tree: CommentedTree) -> Self {
        Self::Tree(tree)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct TokenStream {
    token_trees: Vec<TokenTree>,
    full_span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct CommentedTokenStream {
    pub token_trees: Vec<CommentedTokenTree>,
    pub full_span: Span,
}

#[extension_trait]
impl CharExt for char {
    fn as_open_delimiter(self) -> Option<Delimiter> {
        match self {
            '(' => Some(Delimiter::Parenthesis),
            '{' => Some(Delimiter::Brace),
            '[' => Some(Delimiter::Bracket),
            _ => None,
        }
    }

    fn as_close_delimiter(self) -> Option<Delimiter> {
        match self {
            ')' => Some(Delimiter::Parenthesis),
            '}' => Some(Delimiter::Brace),
            ']' => Some(Delimiter::Bracket),
            _ => None,
        }
    }

    fn as_punct_kind(self) -> Option<PunctKind> {
        match self {
            ';' => Some(PunctKind::Semicolon),
            ':' => Some(PunctKind::Colon),
            '/' => Some(PunctKind::ForwardSlash),
            ',' => Some(PunctKind::Comma),
            '*' => Some(PunctKind::Star),
            '+' => Some(PunctKind::Add),
            '-' => Some(PunctKind::Sub),
            '<' => Some(PunctKind::LessThan),
            '>' => Some(PunctKind::GreaterThan),
            '=' => Some(PunctKind::Equals),
            '.' => Some(PunctKind::Dot),
            '!' => Some(PunctKind::Bang),
            '%' => Some(PunctKind::Percent),
            '&' => Some(PunctKind::Ampersand),
            '^' => Some(PunctKind::Caret),
            '|' => Some(PunctKind::Pipe),
            '_' => Some(PunctKind::Underscore),
            '#' => Some(PunctKind::Sharp),
            _ => None,
        }
    }
}

impl TokenStream {
    pub fn token_trees(&self) -> &[TokenTree] {
        &self.token_trees
    }
}

impl Spanned for TokenStream {
    fn span(&self) -> Span {
        self.full_span.clone()
    }
}

impl CommentedTokenTree {
    pub fn strip_comments(self) -> Option<TokenTree> {
        let commented_tt = match self {
            Self::Comment(_) => return None,
            Self::Tree(commented_tt) => commented_tt,
        };
        let tt = match commented_tt {
            CommentedTree::Punct(punct) => punct.into(),
            CommentedTree::Ident(ident) => ident.into(),
            CommentedTree::Group(group) => group.strip_comments().into(),
            CommentedTree::Literal(lit) => lit.into(),
            CommentedTree::DocComment(doc_comment) => doc_comment.into(),
        };
        Some(tt)
    }
}

impl CommentedTokenStream {
    pub fn token_trees(&self) -> &[CommentedTokenTree] {
        &self.token_trees
    }

    pub fn strip_comments(self) -> TokenStream {
        let token_trees = self
            .token_trees
            .into_iter()
            .filter_map(|tree| tree.strip_comments())
            .collect();
        TokenStream {
            token_trees,
            full_span: self.full_span,
        }
    }
}

impl Spanned for CommentedTokenStream {
    fn span(&self) -> Span {
        self.full_span.clone()
    }
}