Unnamed repository; edit this file 'description' to name the repository.
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
use super::*;

pub(super) const PATH_FIRST: TokenSet =
    TokenSet::new(&[IDENT, T![self], T![super], T![crate], T![Self], T![:], T![<]]);

pub(super) fn is_path_start(p: &Parser<'_>) -> bool {
    is_use_path_start(p) || p.at(T![<]) || p.at(T![Self])
}

pub(super) fn is_use_path_start(p: &Parser<'_>) -> bool {
    match p.current() {
        IDENT | T![self] | T![super] | T![crate] => true,
        T![:] if p.at(T![::]) => true,
        _ => false,
    }
}

pub(super) fn use_path(p: &mut Parser<'_>) {
    path(p, Mode::Use);
}

pub(crate) fn type_path(p: &mut Parser<'_>) {
    path(p, Mode::Type);
}

pub(super) fn expr_path(p: &mut Parser<'_>) {
    path(p, Mode::Expr);
}

pub(crate) fn type_path_for_qualifier(
    p: &mut Parser<'_>,
    qual: CompletedMarker,
) -> CompletedMarker {
    path_for_qualifier(p, Mode::Type, qual)
}

#[derive(Clone, Copy, Eq, PartialEq)]
enum Mode {
    Use,
    Type,
    Expr,
}

fn path(p: &mut Parser<'_>, mode: Mode) {
    let path = p.start();
    path_segment(p, mode, true);
    let qual = path.complete(p, PATH);
    path_for_qualifier(p, mode, qual);
}

fn path_for_qualifier(
    p: &mut Parser<'_>,
    mode: Mode,
    mut qual: CompletedMarker,
) -> CompletedMarker {
    loop {
        let use_tree = mode == Mode::Use && matches!(p.nth(2), T![*] | T!['{']);
        if p.at(T![::]) && !use_tree {
            let path = qual.precede(p);
            p.bump(T![::]);
            path_segment(p, mode, false);
            let path = path.complete(p, PATH);
            qual = path;
        } else {
            return qual;
        }
    }
}

const EXPR_PATH_SEGMENT_RECOVERY_SET: TokenSet =
    items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')'], T![,], T![let]]));
const TYPE_PATH_SEGMENT_RECOVERY_SET: TokenSet = types::TYPE_RECOVERY_SET;

fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) {
    let m = p.start();
    // test qual_paths
    // type X = <A as B>::Output;
    // fn foo() { <usize as Default>::default(); }
    if first && p.eat(T![<]) {
        // test_err angled_path_without_qual
        // type X = <()>;
        // type Y = <A as B>;
        types::type_(p);
        if p.eat(T![as]) {
            if is_use_path_start(p) {
                types::path_type(p);
            } else {
                p.error("expected a trait");
            }
        }
        p.expect(T![>]);
        if !p.at(T![::]) {
            p.error("expected `::`");
        }
    } else {
        let empty = if first {
            p.eat(T![::]);
            false
        } else {
            true
        };
        match p.current() {
            IDENT => {
                name_ref(p);
                opt_path_type_args(p, mode);
            }
            // test crate_path
            // use crate::foo;
            T![self] | T![super] | T![crate] | T![Self] => {
                let m = p.start();
                p.bump_any();
                m.complete(p, NAME_REF);
            }
            _ => {
                let recover_set = match mode {
                    Mode::Use => items::ITEM_RECOVERY_SET,
                    Mode::Type => TYPE_PATH_SEGMENT_RECOVERY_SET,
                    Mode::Expr => EXPR_PATH_SEGMENT_RECOVERY_SET,
                };
                p.err_recover("expected identifier", recover_set);
                if empty {
                    // test_err empty_segment
                    // use crate::;
                    m.abandon(p);
                    return;
                }
            }
        };
    }
    m.complete(p, PATH_SEGMENT);
}

fn opt_path_type_args(p: &mut Parser<'_>, mode: Mode) {
    match mode {
        Mode::Use => {}
        Mode::Type => {
            // test typepathfn_with_coloncolon
            // type F = Start::(Middle) -> (Middle)::End;
            // type GenericArg = S<Start(Middle)::End>;
            if p.at(T![::]) && p.nth_at(2, T!['(']) {
                p.bump(T![::]);
            }
            // test path_fn_trait_args
            // type F = Box<Fn(i32) -> ()>;
            if p.at(T!['(']) {
                params::param_list_fn_trait(p);
                opt_ret_type(p);
            } else {
                generic_args::opt_generic_arg_list(p, false);
            }
        }
        Mode::Expr => generic_args::opt_generic_arg_list(p, true),
    }
}
t additional more elements to be inserted in the map. pub fn reserve(&mut self, additional: usize) { self.v.reserve(additional); } /// Clears the map, removing all elements. pub fn clear(&mut self) { self.v.clear(); } /// Shrinks the capacity of the map as much as possible. pub fn shrink_to_fit(&mut self) { let min_len = self.v.iter().rposition(|slot| slot.is_some()).map_or(0, |i| i + 1); self.v.truncate(min_len); self.v.shrink_to_fit(); } /// Returns whether the map contains a value for the specified index. pub fn contains_idx(&self, idx: Idx<T>) -> bool { matches!(self.v.get(Self::to_idx(idx)), Some(Some(_))) } /// Removes an index from the map, returning the value at the index if the index was previously in the map. pub fn remove(&mut self, idx: Idx<T>) -> Option<V> { self.v.get_mut(Self::to_idx(idx))?.take() } /// Inserts a value associated with a given arena index into the map. /// /// If the map did not have this index present, None is returned. /// Otherwise, the value is updated, and the old value is returned. pub fn insert(&mut self, idx: Idx<T>, t: V) -> Option<V> { let idx = Self::to_idx(idx); self.v.resize_with((idx + 1).max(self.v.len()), || None); self.v[idx].replace(t) } /// Returns a reference to the value associated with the provided index /// if it is present. pub fn get(&self, idx: Idx<T>) -> Option<&V> { self.v.get(Self::to_idx(idx)).and_then(|it| it.as_ref()) } /// Returns a mutable reference to the value associated with the provided index /// if it is present. pub fn get_mut(&mut self, idx: Idx<T>) -> Option<&mut V> { self.v.get_mut(Self::to_idx(idx)).and_then(|it| it.as_mut()) } /// Returns an iterator over the values in the map. pub fn values(&self) -> impl DoubleEndedIterator<Item = &V> { self.v.iter().filter_map(|o| o.as_ref()) } /// Returns an iterator over mutable references to the values in the map. pub fn values_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut V> { self.v.iter_mut().filter_map(|o| o.as_mut()) } /// Returns an iterator over the arena indexes and values in the map. pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Idx<T>, &V)> { self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?))) } /// Returns an iterator over the arena indexes and values in the map. pub fn iter_mut(&mut self) -> impl Iterator<Item = (Idx<T>, &mut V)> { self.v .iter_mut() .enumerate() .filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_mut()?))) } /// Gets the given key's corresponding entry in the map for in-place manipulation. pub fn entry(&mut self, idx: Idx<T>) -> Entry<'_, Idx<T>, V> { let idx = Self::to_idx(idx); self.v.resize_with((idx + 1).max(self.v.len()), || None); match &mut self.v[idx] { slot @ Some(_) => Entry::Occupied(OccupiedEntry { slot, _ty: PhantomData }), slot @ None => Entry::Vacant(VacantEntry { slot, _ty: PhantomData }), } } fn to_idx(idx: Idx<T>) -> usize { u32::from(idx.into_raw()) as usize } fn from_idx(idx: usize) -> Idx<T> { Idx::from_raw((idx as u32).into()) } } impl<T, V> std::ops::Index<Idx<V>> for ArenaMap<Idx<V>, T> { type Output = T; fn index(&self, idx: Idx<V>) -> &T { self.v[Self::to_idx(idx)].as_ref().unwrap() } } impl<T, V> std::ops::IndexMut<Idx<V>> for ArenaMap<Idx<V>, T> { fn index_mut(&mut self, idx: Idx<V>) -> &mut T { self.v[Self::to_idx(idx)].as_mut().unwrap() } } impl<T, V> Default for ArenaMap<Idx<V>, T> { fn default() -> Self { Self::new() } } impl<T, V> Extend<(Idx<V>, T)> for ArenaMap<Idx<V>, T> { fn extend<I: IntoIterator<Item = (Idx<V>, T)>>(&mut self, iter: I) { iter.into_iter().for_each(move |(k, v)| { self.insert(k, v); }); } } impl<T, V> FromIterator<(Idx<V>, T)> for ArenaMap<Idx<V>, T> { fn from_iter<I: IntoIterator<Item = (Idx<V>, T)>>(iter: I) -> Self { let mut this = Self::new(); this.extend(iter); this } } pub struct ArenaMapIter<IDX, V> { iter: Enumerate<std::vec::IntoIter<Option<V>>>, _ty: PhantomData<IDX>, } impl<T, V> IntoIterator for ArenaMap<Idx<T>, V> { type Item = (Idx<T>, V); type IntoIter = ArenaMapIter<Idx<T>, V>; fn into_iter(self) -> Self::IntoIter { let iter = self.v.into_iter().enumerate(); Self::IntoIter { iter, _ty: PhantomData } } } impl<T, V> ArenaMapIter<Idx<T>, V> { fn mapper((idx, o): (usize, Option<V>)) -> Option<(Idx<T>, V)> { Some((ArenaMap::<Idx<T>, V>::from_idx(idx), o?)) } } impl<T, V> Iterator for ArenaMapIter<Idx<T>, V> { type Item = (Idx<T>, V); #[inline] fn next(&mut self) -> Option<Self::Item> { for next in self.iter.by_ref() { match Self::mapper(next) { Some(r) => return Some(r), None => continue, } } None } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } } impl<T, V> DoubleEndedIterator for ArenaMapIter<Idx<T>, V> { #[inline] fn next_back(&mut self) -> Option<Self::Item> { while let Some(next_back) = self.iter.next_back() { match Self::mapper(next_back) { Some(r) => return Some(r), None => continue, } } None } } /// A view into a single entry in a map, which may either be vacant or occupied. /// /// This `enum` is constructed from the [`entry`] method on [`ArenaMap`]. /// /// [`entry`]: ArenaMap::entry pub enum Entry<'a, IDX, V> { /// A vacant entry. Vacant(VacantEntry<'a, IDX, V>), /// An occupied entry. Occupied(OccupiedEntry<'a, IDX, V>), } impl<'a, IDX, V> Entry<'a, IDX, V> { /// Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to /// the value in the entry. pub fn or_insert(self, default: V) -> &'a mut V { match self { Self::Vacant(ent) => ent.insert(default), Self::Occupied(ent) => ent.into_mut(), } } /// Ensures a value is in the entry by inserting the result of the default function if empty, and returns /// a mutable reference to the value in the entry. pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V { match self { Self::Vacant(ent) => ent.insert(default()), Self::Occupied(ent) => ent.into_mut(), } } /// Provides in-place mutable access to an occupied entry before any potential inserts into the map. pub fn and_modify<F: FnOnce(&mut V)>(mut self, f: F) -> Self { if let Self::Occupied(ent) = &mut self { f(ent.get_mut()); } self } } impl<'a, IDX, V> Entry<'a, IDX, V> where V: Default, { /// Ensures a value is in the entry by inserting the default value if empty, and returns a mutable reference /// to the value in the entry. // BUG this clippy lint is a false positive #[allow(clippy::unwrap_or_default)] pub fn or_default(self) -> &'a mut V { self.or_insert_with(Default::default) } } /// A view into an vacant entry in a [`ArenaMap`]. It is part of the [`Entry`] enum. pub struct VacantEntry<'a, IDX, V> { slot: &'a mut Option<V>, _ty: PhantomData<IDX>, } impl<'a, IDX, V> VacantEntry<'a, IDX, V> { /// Sets the value of the entry with the `VacantEntry`’s key, and returns a mutable reference to it. pub fn insert(self, value: V) -> &'a mut V { self.slot.insert(value) } } /// A view into an occupied entry in a [`ArenaMap`]. It is part of the [`Entry`] enum. pub struct OccupiedEntry<'a, IDX, V> { slot: &'a mut Option<V>, _ty: PhantomData<IDX>, } impl<'a, IDX, V> OccupiedEntry<'a, IDX, V> { /// Gets a reference to the value in the entry. pub fn get(&self) -> &V { self.slot.as_ref().expect("Occupied") } /// Gets a mutable reference to the value in the entry. pub fn get_mut(&mut self) -> &mut V { self.slot.as_mut().expect("Occupied") } /// Converts the entry into a mutable reference to its value. pub fn into_mut(self) -> &'a mut V { self.slot.as_mut().expect("Occupied") } /// Sets the value of the entry with the `OccupiedEntry`’s key, and returns the entry’s old value. pub fn insert(&mut self, value: V) -> V { self.slot.replace(value).expect("Occupied") } /// Takes the value of the entry out of the map, and returns it. pub fn remove(self) -> V { self.slot.take().expect("Occupied") } }