syntax/ast/
expr_ext.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Various extension methods to ast Expr Nodes, which are hard to code-generate.
//!
//! These methods should only do simple, shallow tasks related to the syntax of the node itself.

use crate::{
    ast::{
        self,
        operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
        support, ArgList, AstChildren, AstNode, BlockExpr, ClosureExpr, Const, Expr, Fn,
        FormatArgsArg, FormatArgsExpr, MacroDef, Static, TokenTree,
    },
    AstToken,
    SyntaxKind::*,
    SyntaxNode, SyntaxToken, T,
};

use super::RangeItem;

impl ast::HasAttrs for ast::Expr {}

impl ast::Expr {
    pub fn is_block_like(&self) -> bool {
        matches!(
            self,
            ast::Expr::IfExpr(_)
                | ast::Expr::LoopExpr(_)
                | ast::Expr::ForExpr(_)
                | ast::Expr::WhileExpr(_)
                | ast::Expr::BlockExpr(_)
                | ast::Expr::MatchExpr(_)
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ElseBranch {
    Block(ast::BlockExpr),
    IfExpr(ast::IfExpr),
}

impl From<ast::BlockExpr> for ElseBranch {
    fn from(block_expr: ast::BlockExpr) -> Self {
        Self::Block(block_expr)
    }
}

impl From<ast::IfExpr> for ElseBranch {
    fn from(if_expr: ast::IfExpr) -> Self {
        Self::IfExpr(if_expr)
    }
}

impl ast::IfExpr {
    pub fn condition(&self) -> Option<ast::Expr> {
        // If the condition is a BlockExpr, check if the then body is missing.
        // If it is assume the condition is the expression that is missing instead.
        let mut exprs = support::children(self.syntax());
        let first = exprs.next();
        match first {
            Some(ast::Expr::BlockExpr(_)) => exprs.next().and(first),
            first => first,
        }
    }

    pub fn then_branch(&self) -> Option<ast::BlockExpr> {
        match support::children(self.syntax()).nth(1)? {
            ast::Expr::BlockExpr(block) => Some(block),
            _ => None,
        }
    }

    pub fn else_branch(&self) -> Option<ElseBranch> {
        match support::children(self.syntax()).nth(2)? {
            ast::Expr::BlockExpr(block) => Some(ElseBranch::Block(block)),
            ast::Expr::IfExpr(elif) => Some(ElseBranch::IfExpr(elif)),
            _ => None,
        }
    }
}

#[test]
fn if_block_condition() {
    let parse = ast::SourceFile::parse(
        r#"
        fn test() {
            if { true } { "if" }
            else if { false } { "first elif" }
            else if true { "second elif" }
            else if (true) { "third elif" }
            else { "else" }
        }
        "#,
        parser::Edition::CURRENT,
    );
    let if_ = parse.tree().syntax().descendants().find_map(ast::IfExpr::cast).unwrap();
    assert_eq!(if_.then_branch().unwrap().syntax().text(), r#"{ "if" }"#);
    let elif = match if_.else_branch().unwrap() {
        ElseBranch::IfExpr(elif) => elif,
        ElseBranch::Block(_) => panic!("should be `else if`"),
    };
    assert_eq!(elif.then_branch().unwrap().syntax().text(), r#"{ "first elif" }"#);
    let elif = match elif.else_branch().unwrap() {
        ElseBranch::IfExpr(elif) => elif,
        ElseBranch::Block(_) => panic!("should be `else if`"),
    };
    assert_eq!(elif.then_branch().unwrap().syntax().text(), r#"{ "second elif" }"#);
    let elif = match elif.else_branch().unwrap() {
        ElseBranch::IfExpr(elif) => elif,
        ElseBranch::Block(_) => panic!("should be `else if`"),
    };
    assert_eq!(elif.then_branch().unwrap().syntax().text(), r#"{ "third elif" }"#);
    let else_ = match elif.else_branch().unwrap() {
        ElseBranch::Block(else_) => else_,
        ElseBranch::IfExpr(_) => panic!("should be `else`"),
    };
    assert_eq!(else_.syntax().text(), r#"{ "else" }"#);
}

#[test]
fn if_condition_with_if_inside() {
    let parse = ast::SourceFile::parse(
        r#"
        fn test() {
            if if true { true } else { false } { "if" }
            else { "else" }
        }
        "#,
        parser::Edition::CURRENT,
    );
    let if_ = parse.tree().syntax().descendants().find_map(ast::IfExpr::cast).unwrap();
    assert_eq!(if_.then_branch().unwrap().syntax().text(), r#"{ "if" }"#);
    let else_ = match if_.else_branch().unwrap() {
        ElseBranch::Block(else_) => else_,
        ElseBranch::IfExpr(_) => panic!("should be `else`"),
    };
    assert_eq!(else_.syntax().text(), r#"{ "else" }"#);
}

impl ast::PrefixExpr {
    pub fn op_kind(&self) -> Option<UnaryOp> {
        let res = match self.op_token()?.kind() {
            T![*] => UnaryOp::Deref,
            T![!] => UnaryOp::Not,
            T![-] => UnaryOp::Neg,
            _ => return None,
        };
        Some(res)
    }

    pub fn op_token(&self) -> Option<SyntaxToken> {
        self.syntax().first_child_or_token()?.into_token()
    }
}

impl ast::BinExpr {
    pub fn op_details(&self) -> Option<(SyntaxToken, BinaryOp)> {
        self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| {
            #[rustfmt::skip]
            let bin_op = match c.kind() {
                T![||] => BinaryOp::LogicOp(LogicOp::Or),
                T![&&] => BinaryOp::LogicOp(LogicOp::And),

                T![==] => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
                T![!=] => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
                T![<=] => BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less,    strict: false }),
                T![>=] => BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false }),
                T![<]  => BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less,    strict: true }),
                T![>]  => BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true }),

                T![+]  => BinaryOp::ArithOp(ArithOp::Add),
                T![*]  => BinaryOp::ArithOp(ArithOp::Mul),
                T![-]  => BinaryOp::ArithOp(ArithOp::Sub),
                T![/]  => BinaryOp::ArithOp(ArithOp::Div),
                T![%]  => BinaryOp::ArithOp(ArithOp::Rem),
                T![<<] => BinaryOp::ArithOp(ArithOp::Shl),
                T![>>] => BinaryOp::ArithOp(ArithOp::Shr),
                T![^]  => BinaryOp::ArithOp(ArithOp::BitXor),
                T![|]  => BinaryOp::ArithOp(ArithOp::BitOr),
                T![&]  => BinaryOp::ArithOp(ArithOp::BitAnd),

                T![=]   => BinaryOp::Assignment { op: None },
                T![+=]  => BinaryOp::Assignment { op: Some(ArithOp::Add) },
                T![*=]  => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
                T![-=]  => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
                T![/=]  => BinaryOp::Assignment { op: Some(ArithOp::Div) },
                T![%=]  => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
                T![<<=] => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
                T![>>=] => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
                T![^=]  => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
                T![|=]  => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
                T![&=]  => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },

                _ => return None,
            };
            Some((c, bin_op))
        })
    }

    pub fn op_kind(&self) -> Option<BinaryOp> {
        self.op_details().map(|t| t.1)
    }

    pub fn op_token(&self) -> Option<SyntaxToken> {
        self.op_details().map(|t| t.0)
    }

    pub fn lhs(&self) -> Option<ast::Expr> {
        support::children(self.syntax()).next()
    }

    pub fn rhs(&self) -> Option<ast::Expr> {
        support::children(self.syntax()).nth(1)
    }

    pub fn sub_exprs(&self) -> (Option<ast::Expr>, Option<ast::Expr>) {
        let mut children = support::children(self.syntax());
        let first = children.next();
        let second = children.next();
        (first, second)
    }
}

impl ast::RangeExpr {
    fn op_details(&self) -> Option<(usize, SyntaxToken, RangeOp)> {
        self.syntax().children_with_tokens().enumerate().find_map(|(ix, child)| {
            let token = child.into_token()?;
            let bin_op = match token.kind() {
                T![..] => RangeOp::Exclusive,
                T![..=] => RangeOp::Inclusive,
                _ => return None,
            };
            Some((ix, token, bin_op))
        })
    }

    pub fn is_range_full(&self) -> bool {
        support::children::<Expr>(&self.syntax).next().is_none()
    }
}

impl RangeItem for ast::RangeExpr {
    type Bound = ast::Expr;

    fn start(&self) -> Option<ast::Expr> {
        let op_ix = self.op_details()?.0;
        self.syntax()
            .children_with_tokens()
            .take(op_ix)
            .find_map(|it| ast::Expr::cast(it.into_node()?))
    }

    fn end(&self) -> Option<ast::Expr> {
        let op_ix = self.op_details()?.0;
        self.syntax()
            .children_with_tokens()
            .skip(op_ix + 1)
            .find_map(|it| ast::Expr::cast(it.into_node()?))
    }

    fn op_token(&self) -> Option<SyntaxToken> {
        self.op_details().map(|t| t.1)
    }

    fn op_kind(&self) -> Option<RangeOp> {
        self.op_details().map(|t| t.2)
    }
}

impl ast::IndexExpr {
    pub fn base(&self) -> Option<ast::Expr> {
        support::children(self.syntax()).next()
    }
    pub fn index(&self) -> Option<ast::Expr> {
        support::children(self.syntax()).nth(1)
    }
}

pub enum ArrayExprKind {
    Repeat { initializer: Option<ast::Expr>, repeat: Option<ast::Expr> },
    ElementList(AstChildren<ast::Expr>),
}

impl ast::ArrayExpr {
    pub fn kind(&self) -> ArrayExprKind {
        if self.is_repeat() {
            ArrayExprKind::Repeat {
                initializer: support::children(self.syntax()).next(),
                repeat: support::children(self.syntax()).nth(1),
            }
        } else {
            ArrayExprKind::ElementList(support::children(self.syntax()))
        }
    }

    fn is_repeat(&self) -> bool {
        self.semicolon_token().is_some()
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum LiteralKind {
    String(ast::String),
    ByteString(ast::ByteString),
    CString(ast::CString),
    IntNumber(ast::IntNumber),
    FloatNumber(ast::FloatNumber),
    Char(ast::Char),
    Byte(ast::Byte),
    Bool(bool),
}

impl ast::Literal {
    pub fn token(&self) -> SyntaxToken {
        self.syntax()
            .children_with_tokens()
            .find(|e| e.kind() != ATTR && !e.kind().is_trivia())
            .and_then(|e| e.into_token())
            .unwrap()
    }

    pub fn kind(&self) -> LiteralKind {
        let token = self.token();

        if let Some(t) = ast::IntNumber::cast(token.clone()) {
            return LiteralKind::IntNumber(t);
        }
        if let Some(t) = ast::FloatNumber::cast(token.clone()) {
            return LiteralKind::FloatNumber(t);
        }
        if let Some(t) = ast::String::cast(token.clone()) {
            return LiteralKind::String(t);
        }
        if let Some(t) = ast::ByteString::cast(token.clone()) {
            return LiteralKind::ByteString(t);
        }
        if let Some(t) = ast::CString::cast(token.clone()) {
            return LiteralKind::CString(t);
        }
        if let Some(t) = ast::Char::cast(token.clone()) {
            return LiteralKind::Char(t);
        }
        if let Some(t) = ast::Byte::cast(token.clone()) {
            return LiteralKind::Byte(t);
        }

        match token.kind() {
            T![true] => LiteralKind::Bool(true),
            T![false] => LiteralKind::Bool(false),
            _ => unreachable!(),
        }
    }
}

pub enum BlockModifier {
    Async(SyntaxToken),
    Unsafe(SyntaxToken),
    Try(SyntaxToken),
    Const(SyntaxToken),
    AsyncGen(SyntaxToken),
    Gen(SyntaxToken),
    Label(ast::Label),
}

impl ast::BlockExpr {
    pub fn modifier(&self) -> Option<BlockModifier> {
        self.gen_token()
            .map(|v| {
                if self.async_token().is_some() {
                    BlockModifier::AsyncGen(v)
                } else {
                    BlockModifier::Gen(v)
                }
            })
            .or_else(|| self.async_token().map(BlockModifier::Async))
            .or_else(|| self.unsafe_token().map(BlockModifier::Unsafe))
            .or_else(|| self.try_token().map(BlockModifier::Try))
            .or_else(|| self.const_token().map(BlockModifier::Const))
            .or_else(|| self.label().map(BlockModifier::Label))
    }
    /// false if the block is an intrinsic part of the syntax and can't be
    /// replaced with arbitrary expression.
    ///
    /// ```not_rust
    /// fn foo() { not_stand_alone }
    /// const FOO: () = { stand_alone };
    /// ```
    pub fn is_standalone(&self) -> bool {
        let parent = match self.syntax().parent() {
            Some(it) => it,
            None => return true,
        };
        match parent.kind() {
            FOR_EXPR | IF_EXPR => parent
                .children()
                .find(|it| ast::Expr::can_cast(it.kind()))
                .map_or(true, |it| it == *self.syntax()),
            LET_ELSE | FN | WHILE_EXPR | LOOP_EXPR | CONST_BLOCK_PAT => false,
            _ => true,
        }
    }
}

#[test]
fn test_literal_with_attr() {
    let parse =
        ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#, parser::Edition::CURRENT);
    let lit = parse.tree().syntax().descendants().find_map(ast::Literal::cast).unwrap();
    assert_eq!(lit.token().text(), r#""Hello""#);
}

impl ast::RecordExprField {
    pub fn parent_record_lit(&self) -> ast::RecordExpr {
        self.syntax().ancestors().find_map(ast::RecordExpr::cast).unwrap()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CallableExpr {
    Call(ast::CallExpr),
    MethodCall(ast::MethodCallExpr),
}

impl ast::HasAttrs for CallableExpr {}
impl ast::HasArgList for CallableExpr {}

impl AstNode for CallableExpr {
    fn can_cast(kind: parser::SyntaxKind) -> bool
    where
        Self: Sized,
    {
        ast::CallExpr::can_cast(kind) || ast::MethodCallExpr::can_cast(kind)
    }

    fn cast(syntax: SyntaxNode) -> Option<Self>
    where
        Self: Sized,
    {
        if let Some(it) = ast::CallExpr::cast(syntax.clone()) {
            Some(Self::Call(it))
        } else {
            ast::MethodCallExpr::cast(syntax).map(Self::MethodCall)
        }
    }

    fn syntax(&self) -> &SyntaxNode {
        match self {
            Self::Call(it) => it.syntax(),
            Self::MethodCall(it) => it.syntax(),
        }
    }
}

impl MacroDef {
    fn tts(&self) -> (Option<ast::TokenTree>, Option<ast::TokenTree>) {
        let mut types = support::children(self.syntax());
        let first = types.next();
        let second = types.next();
        (first, second)
    }

    pub fn args(&self) -> Option<TokenTree> {
        match self.tts() {
            (Some(args), Some(_)) => Some(args),
            _ => None,
        }
    }

    pub fn body(&self) -> Option<TokenTree> {
        match self.tts() {
            (Some(body), None) | (_, Some(body)) => Some(body),
            _ => None,
        }
    }
}

impl ClosureExpr {
    pub fn body(&self) -> Option<Expr> {
        support::child(&self.syntax)
    }
}
impl Const {
    pub fn body(&self) -> Option<Expr> {
        support::child(&self.syntax)
    }
}
impl Fn {
    pub fn body(&self) -> Option<BlockExpr> {
        support::child(&self.syntax)
    }
}
impl Static {
    pub fn body(&self) -> Option<Expr> {
        support::child(&self.syntax)
    }
}
impl FormatArgsExpr {
    pub fn args(&self) -> AstChildren<FormatArgsArg> {
        support::children(&self.syntax)
    }
}
impl ArgList {
    pub fn args(&self) -> AstChildren<Expr> {
        support::children(&self.syntax)
    }
}