aboutsummaryrefslogtreecommitdiff
path: root/src/parser/event_parser/parser.rs
blob: 04ef4fb2843e4afb2408d845abf7da2aee831866 (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
use {Token, SyntaxKind, TextUnit};
use super::{Event};
use super::super::is_insignificant;
use syntax_kinds::{L_CURLY, R_CURLY, ERROR};

pub struct Parser<'t> {
    text: &'t str,
    raw_tokens: &'t [Token],
    non_ws_tokens: Vec<(usize, TextUnit)>,

    pos: usize,
    events: Vec<Event>,
    curly_level: i32,
}

impl<'t> Parser<'t> {
    pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> {
        let mut non_ws_tokens = Vec::new();
        let mut len = TextUnit::new(0);
        for (idx, &token) in raw_tokens.iter().enumerate() {
            if !is_insignificant(token.kind) {
                non_ws_tokens.push((idx, len))
            }
            len += token.len;
        }

        Parser {
            text,
            raw_tokens,
            non_ws_tokens,

            pos: 0,
            events: Vec::new(),
            curly_level: 0,
        }
    }

    pub(crate) fn into_events(self) -> Vec<Event> {
        assert!(self.is_eof());
        self.events
    }

    pub(crate) fn is_eof(&self) -> bool {
        self.pos == self.non_ws_tokens.len()
    }

    pub(crate) fn start(&mut self, kind: SyntaxKind) {
        self.event(Event::Start { kind });
    }

    pub(crate) fn finish(&mut self) {
        self.event(Event::Finish);
    }

    pub(crate) fn current(&self) -> Option<SyntaxKind> {
        if self.is_eof() {
            return None;
        }
        let idx = self.non_ws_tokens[self.pos].0;
        Some(self.raw_tokens[idx].kind)
    }

    pub(crate) fn current_is(&self, kind: SyntaxKind) -> bool {
        self.current() == Some(kind)
    }

    pub(crate) fn bump(&mut self) -> Option<SyntaxKind> {
        let kind = self.current()?;
        match kind {
            L_CURLY => self.curly_level += 1,
            R_CURLY => self.curly_level -= 1,
            _ => (),
        }
        self.pos += 1;
        self.event(Event::Token { kind, n_raw_tokens: 1 });
        Some(kind)
    }

    pub(crate) fn expect(&mut self, kind: SyntaxKind) -> Result<(), ()> {
        if kind == self.current().ok_or(())? {
            self.bump();
            Ok(())
        } else {
            Err(())
        }
    }

    pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> Result<(), ()> {
        let level = self.curly_level;
        self.expect(L_CURLY)?;
        f(self);
        assert!(self.curly_level > level);
        if self.expect(R_CURLY).is_ok() {
            return Ok(());
        }
        self.start(ERROR);
        while self.curly_level > level {
            if self.bump().is_none() {
                break;
            }
        }
        self.finish();
        Ok(()) //???
    }

    fn event(&mut self, event: Event) {
        self.events.push(event)
    }
}