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

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

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

impl<'a> OffsetTokenSink<'a> {
    pub fn collect(&self, begin: Cursor<'a>) -> Vec<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_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(&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,
            })),
        }
    }
}