aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/subtree_parser.rs
blob: 4440c69ff558faf3452cb3022c2889872149c29c (plain)
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
use crate::subtree_source::SubtreeTokenSource;

use ra_parser::{FragmentKind, TokenSource, TreeSink};
use ra_syntax::SyntaxKind;
use tt::buffer::{Cursor, TokenBuffer};

struct OffsetTokenSink<'a> {
    cursor: Cursor<'a>,
    error: bool,
}

impl<'a> OffsetTokenSink<'a> {
    pub fn collect(&self, begin: Cursor<'a>) -> Vec<&'a tt::TokenTree> {
        if !self.cursor.is_root() {
            return vec![];
        }

        let mut curr = begin;
        let mut res = vec![];

        while self.cursor != curr {
            if let Some(token) = curr.token_tree() {
                res.push(token);
            }
            curr = curr.bump();
        }

        res
    }
}

impl<'a> TreeSink for OffsetTokenSink<'a> {
    fn token(&mut self, _kind: SyntaxKind, n_tokens: u8) {
        for _ in 0..n_tokens {
            self.cursor = self.cursor.bump_subtree();
        }
    }
    fn start_node(&mut self, _kind: SyntaxKind) {}
    fn finish_node(&mut self) {}
    fn error(&mut self, _error: ra_parser::ParseError) {
        self.error = true;
    }
}

pub(crate) struct Parser<'a> {
    subtree: &'a tt::Subtree,
    cur_pos: &'a mut usize,
}

impl<'a> Parser<'a> {
    pub fn new(cur_pos: &'a mut usize, subtree: &'a tt::Subtree) -> Parser<'a> {
        Parser { cur_pos, subtree }
    }

    pub fn parse_fragment(self, fragment_kind: FragmentKind) -> Option<tt::TokenTree> {
        self.parse(|token_source, tree_skink| {
            ra_parser::parse_fragment(token_source, tree_skink, fragment_kind)
        })
    }

    fn parse<F>(self, f: F) -> Option<tt::TokenTree>
    where
        F: FnOnce(&mut dyn TokenSource, &mut dyn TreeSink),
    {
        let buffer = TokenBuffer::new(&self.subtree.token_trees[*self.cur_pos..]);
        let mut src = SubtreeTokenSource::new(&buffer);
        let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false };

        f(&mut src, &mut sink);

        let r = self.finish(buffer.begin(), &mut sink);
        if sink.error {
            return None;
        }
        r
    }

    fn finish(self, begin: Cursor, sink: &mut OffsetTokenSink) -> Option<tt::TokenTree> {
        let res = sink.collect(begin);
        *self.cur_pos += res.len();

        match res.len() {
            0 => None,
            1 => Some(res[0].clone()),
            _ => Some(tt::TokenTree::Subtree(tt::Subtree {
                delimiter: tt::Delimiter::None,
                token_trees: res.into_iter().cloned().collect(),
            })),
        }
    }
}