parser/grammar/
paths.rs

1use super::*;
2
3pub(super) const PATH_FIRST: TokenSet =
4    TokenSet::new(&[IDENT, T![self], T![super], T![crate], T![Self], T![:], T![<]]);
5
6pub(super) fn is_path_start(p: &Parser<'_>) -> bool {
7    is_use_path_start(p) || p.at(T![<]) || p.at(T![Self])
8}
9
10pub(super) fn is_use_path_start(p: &Parser<'_>) -> bool {
11    match p.current() {
12        IDENT | T![self] | T![super] | T![crate] => true,
13        T![:] if p.at(T![::]) => true,
14        _ => false,
15    }
16}
17
18pub(super) fn use_path(p: &mut Parser<'_>) {
19    path(p, Mode::Use);
20}
21
22pub(super) fn vis_path(p: &mut Parser<'_>) {
23    path(p, Mode::Vis);
24}
25
26pub(super) fn attr_path(p: &mut Parser<'_>) {
27    path(p, Mode::Attr);
28}
29
30pub(crate) fn type_path(p: &mut Parser<'_>) {
31    path(p, Mode::Type);
32}
33
34pub(super) fn expr_path(p: &mut Parser<'_>) {
35    path(p, Mode::Expr);
36}
37
38pub(crate) fn type_path_for_qualifier(
39    p: &mut Parser<'_>,
40    qual: CompletedMarker,
41) -> CompletedMarker {
42    path_for_qualifier(p, Mode::Type, qual)
43}
44
45#[derive(Clone, Copy, Eq, PartialEq)]
46enum Mode {
47    Use,
48    Attr,
49    Type,
50    Expr,
51    Vis,
52}
53
54fn path(p: &mut Parser<'_>, mode: Mode) -> Option<CompletedMarker> {
55    let path = p.start();
56    if path_segment(p, mode, true).is_none() {
57        path.abandon(p);
58        return None;
59    }
60    let qual = path.complete(p, PATH);
61    Some(path_for_qualifier(p, mode, qual))
62}
63
64fn path_for_qualifier(
65    p: &mut Parser<'_>,
66    mode: Mode,
67    mut qual: CompletedMarker,
68) -> CompletedMarker {
69    loop {
70        let use_tree = mode == Mode::Use && matches!(p.nth(2), T![*] | T!['{']);
71        if p.at(T![::]) && !use_tree {
72            let path = qual.precede(p);
73            p.bump(T![::]);
74            path_segment(p, mode, false);
75            let path = path.complete(p, PATH);
76            qual = path;
77        } else {
78            return qual;
79        }
80    }
81}
82
83const EXPR_PATH_SEGMENT_RECOVERY_SET: TokenSet =
84    items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')'], T![,], T![let]]));
85const TYPE_PATH_SEGMENT_RECOVERY_SET: TokenSet = types::TYPE_RECOVERY_SET;
86
87fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) -> Option<CompletedMarker> {
88    let m = p.start();
89    // test qual_paths
90    // type X = <A as B>::Output;
91    // fn foo() { <usize as Default>::default(); }
92    if first && p.eat(T![<]) {
93        // test_err angled_path_without_qual
94        // type X = <()>;
95        // type Y = <A as B>;
96        types::type_(p);
97        if p.eat(T![as]) {
98            if is_use_path_start(p) {
99                types::path_type(p);
100            } else {
101                p.error("expected a trait");
102            }
103        }
104        p.expect(T![>]);
105        if !p.at(T![::]) {
106            p.error("expected `::`");
107        }
108    } else {
109        let mut empty = if first { !p.eat(T![::]) } else { true };
110        if p.at_ts(PATH_NAME_REF_KINDS) {
111            // test crate_path
112            // use crate::foo;
113            name_ref_mod_path(p);
114            opt_path_args(p, mode);
115        } else {
116            let recover_set = match mode {
117                Mode::Use => items::ITEM_RECOVERY_SET,
118                Mode::Attr => {
119                    items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![']'], T![=], T![#]]))
120                }
121                Mode::Vis => items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')']])),
122                Mode::Type => TYPE_PATH_SEGMENT_RECOVERY_SET,
123                Mode::Expr => EXPR_PATH_SEGMENT_RECOVERY_SET,
124            };
125            empty &= p.err_recover(
126                "expected identifier, `self`, `super`, `crate`, or `Self`",
127                recover_set,
128            );
129            if empty {
130                // test_err empty_segment
131                // use crate::;
132                m.abandon(p);
133                return None;
134            }
135        }
136    }
137    Some(m.complete(p, PATH_SEGMENT))
138}
139
140pub(crate) fn opt_path_type_args(p: &mut Parser<'_>) {
141    // test typepathfn_with_coloncolon
142    // type F = Start::(Middle) -> (Middle)::End;
143    // type GenericArg = S<Start(Middle)::End>;
144    let m;
145    if p.at(T![::]) && matches!(p.nth(2), T![<] | T!['(']) {
146        m = p.start();
147        p.bump(T![::]);
148    } else if (p.current() == T![<] && p.nth(1) != T![=]) || p.current() == T!['('] {
149        m = p.start();
150    } else {
151        return;
152    }
153    let current = p.current();
154    if current == T![<] {
155        // test_err generic_arg_list_recover
156        // type T = T<0, ,T>;
157        // type T = T::<0, ,T>;
158        delimited(
159            p,
160            T![<],
161            T![>],
162            T![,],
163            || "expected generic argument".into(),
164            generic_args::GENERIC_ARG_FIRST,
165            generic_args::generic_arg,
166        );
167        m.complete(p, GENERIC_ARG_LIST);
168    } else if p.nth_at(1, T![..]) {
169        // test return_type_syntax_in_path
170        // fn foo<T>()
171        // where
172        //     T::method(..): Send,
173        //     method(..): Send,
174        //     method::(..): Send,
175        // {}
176        p.bump(T!['(']);
177        p.bump(T![..]);
178        p.expect(T![')']);
179        m.complete(p, RETURN_TYPE_SYNTAX);
180    } else {
181        // test path_fn_trait_args
182        // type F = Box<Fn(i32) -> ()>;
183        // type F = Box<::Fn(i32) -> ()>;
184        // type F = Box<Fn::(i32) -> ()>;
185        // type F = Box<::Fn::(i32) -> ()>;
186        delimited(
187            p,
188            T!['('],
189            T![')'],
190            T![,],
191            || "expected type".into(),
192            types::TYPE_FIRST,
193            |p| {
194                let progress = types::TYPE_FIRST.contains(p.current());
195                generic_args::type_arg(p);
196                progress
197            },
198        );
199        m.complete(p, PARENTHESIZED_ARG_LIST);
200        opt_ret_type(p);
201    }
202}
203
204fn opt_path_args(p: &mut Parser<'_>, mode: Mode) {
205    match mode {
206        Mode::Use | Mode::Attr | Mode::Vis => {}
207        Mode::Type => opt_path_type_args(p),
208        Mode::Expr => generic_args::opt_generic_arg_list_expr(p),
209    }
210}