aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/subtree_parser.rs
blob: 528aa0f8a0c8edb71e73752e48be95d05362b2e3 (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
92
use crate::subtree_source::SubtreeTokenSource;

use ra_parser::{TokenSource, TreeSink};
use ra_syntax::{SyntaxKind};

struct OffsetTokenSink {
    token_pos: usize,
}

impl TreeSink for OffsetTokenSink {
    fn token(&mut self, _kind: SyntaxKind, n_tokens: u8) {
        self.token_pos += n_tokens as usize;
    }
    fn start_node(&mut self, _kind: SyntaxKind) {}
    fn finish_node(&mut self) {}
    fn error(&mut self, _error: ra_parser::ParseError) {}
}

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_path(self) -> Option<tt::TokenTree> {
        self.parse(ra_parser::parse_path)
    }

    pub fn parse_expr(self) -> Option<tt::TokenTree> {
        self.parse(ra_parser::parse_expr)
    }

    pub fn parse_ty(self) -> Option<tt::TokenTree> {
        self.parse(ra_parser::parse_ty)
    }

    pub fn parse_pat(self) -> Option<tt::TokenTree> {
        self.parse(ra_parser::parse_pat)
    }

    pub fn parse_stmt(self) -> Option<tt::TokenTree> {
        self.parse(|src, sink| ra_parser::parse_stmt(src, sink, false))
    }

    pub fn parse_block(self) -> Option<tt::TokenTree> {
        self.parse(ra_parser::parse_block)
    }

    pub fn parse_meta(self) -> Option<tt::TokenTree> {
        self.parse(ra_parser::parse_meta)
    }

    pub fn parse_item(self) -> Option<tt::TokenTree> {
        self.parse(ra_parser::parse_item)
    }

    pub fn parse_vis(self) -> Option<tt::TokenTree> {
        self.parse(ra_parser::parse_vis)
    }

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

        f(&src, &mut sink);

        self.finish(sink.token_pos, &mut src)
    }

    fn finish(self, parsed_token: usize, src: &mut SubtreeTokenSource) -> Option<tt::TokenTree> {
        let res = src.bump_n(parsed_token);
        *self.cur_pos += res.len();

        let res: Vec<_> = res.into_iter().cloned().collect();

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