sway_ast/
path.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
use crate::priv_prelude::*;

#[derive(Clone, Debug, Serialize)]
pub struct PathExpr {
    pub root_opt: Option<(Option<AngleBrackets<QualifiedPathRoot>>, DoubleColonToken)>,
    pub prefix: PathExprSegment,
    pub suffix: Vec<(DoubleColonToken, PathExprSegment)>,
    // path expression with incomplete suffix are needed to do
    // parser recovery on inputs like foo::
    #[serde(skip_serializing)]
    pub incomplete_suffix: bool,
}

#[derive(Clone, Debug, Serialize)]
pub struct PathExprSegment {
    pub name: Ident,
    pub generics_opt: Option<(DoubleColonToken, GenericArgs)>,
}

impl Spanned for PathExpr {
    fn span(&self) -> Span {
        let start = match &self.root_opt {
            Some((qualified_path_root_opt, double_colon_token)) => match qualified_path_root_opt {
                Some(qualified_path_root) => qualified_path_root.span(),
                None => double_colon_token.span(),
            },
            None => self.prefix.span(),
        };
        let end = match self.suffix.last() {
            Some((_, path_expr_segment)) => path_expr_segment.span(),
            None => self.prefix.span(),
        };
        Span::join(start, &end)
    }
}

impl PathExpr {
    pub fn try_into_ident(self) -> Result<Ident, PathExpr> {
        if self.root_opt.is_none()
            && self.suffix.is_empty()
            && self.prefix.generics_opt.is_none()
            && !self.incomplete_suffix
        {
            return Ok(self.prefix.name);
        }
        Err(self)
    }
}

impl Spanned for PathExprSegment {
    fn span(&self) -> Span {
        let start = self.name.span();
        match &self.generics_opt {
            Some((_, generic_args)) => Span::join(start, &generic_args.span()),
            None => start,
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct PathType {
    pub root_opt: Option<(Option<AngleBrackets<QualifiedPathRoot>>, DoubleColonToken)>,
    pub prefix: PathTypeSegment,
    pub suffix: Vec<(DoubleColonToken, PathTypeSegment)>,
}

impl PathType {
    pub fn last_segment(&self) -> &PathTypeSegment {
        self.suffix
            .iter()
            .map(|s| &s.1)
            .last()
            .unwrap_or(&self.prefix)
    }
}

impl Spanned for PathType {
    fn span(&self) -> Span {
        let start = match &self.root_opt {
            Some((qualified_path_root_opt, double_colon_token)) => match qualified_path_root_opt {
                Some(qualified_path_root) => qualified_path_root.span(),
                None => double_colon_token.span(),
            },
            None => self.prefix.span(),
        };
        let end = match self.suffix.last() {
            Some((_, path_type_segment)) => path_type_segment.span(),
            None => self.prefix.span(),
        };
        Span::join(start, &end)
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct PathTypeSegment {
    pub name: Ident,
    pub generics_opt: Option<(Option<DoubleColonToken>, GenericArgs)>,
}

impl Spanned for PathTypeSegment {
    fn span(&self) -> Span {
        let start = self.name.span();
        match &self.generics_opt {
            Some((_, generic_args)) => Span::join(start, &generic_args.span()),
            None => start,
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct QualifiedPathRoot {
    pub ty: Box<Ty>,
    pub as_trait: Option<(AsToken, Box<PathType>)>,
}