aboutsummaryrefslogtreecommitdiff
path: root/src/parser/mod.rs
blob: c23ed33495cefe9b0b14350f06d7ec1c97bb1cc0 (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
use {File, SyntaxKind, Token};

use syntax_kinds::*;

#[macro_use]
mod parser;
mod input;
mod event;
mod grammar;
use self::event::Event;

/// Parse a sequence of tokens into the representative node tree
pub fn parse(text: String, tokens: &[Token]) -> File {
    let events = {
        let input = input::ParserInput::new(&text, tokens);
        let mut parser = parser::Parser::new(&input);
        grammar::file(&mut parser);
        parser.into_events()
    };
    event::to_file(text, tokens, events)
}

fn is_insignificant(kind: SyntaxKind) -> bool {
    match kind {
        WHITESPACE | COMMENT => true,
        _ => false,
    }
}

impl<'p> parser::Parser<'p> {
    fn at(&self, kind: SyntaxKind) -> bool {
        self.current() == kind
    }

    fn err_and_bump(&mut self, message: &str) {
        let err = self.start();
        self.error(message);
        self.bump();
        err.complete(self, ERROR);
    }

    fn expect(&mut self, kind: SyntaxKind) -> bool {
        if self.at(kind) {
            self.bump();
            true
        } else {
            self.error(format!("expected {:?}", kind));
            false
        }
    }

    fn eat(&mut self, kind: SyntaxKind) -> bool {
        self.at(kind) && {
            self.bump();
            true
        }
    }
}