aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/validation.rs
blob: 03d98eff490894d83ce9cb73e4b52b8775a767ff (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
use crate::{
    ast::{self, AstNode},
    File,
    string_lexing,
    yellow::{
        SyntaxError,
    },
};

pub(crate) fn validate(file: &File) -> Vec<SyntaxError> {
    let mut errors = Vec::new();
    for d in file.root.borrowed().descendants() {
        if let Some(c) = ast::Char::cast(d) {
            let components = &mut string_lexing::parse_char_literal(c.text());
            let len = components.count();

            if !components.has_closing_quote {
                errors.push(SyntaxError {
                    msg: "Unclosed char literal".to_string(),
                    offset: d.range().start(),
                });
            }

            if len == 0 {
                errors.push(SyntaxError {
                    msg: "Empty char literal".to_string(),
                    offset: d.range().start(),
                });
            }

            if len > 1 {
                errors.push(SyntaxError {
                    msg: "Character literal should be only one character long".to_string(),
                    offset: d.range().start(),
                });
            }
        }
    }
    errors
}