From 9b5bbab104d8ba445143f6f3a9e4149b40c29ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 4 Nov 2018 15:06:38 +0100 Subject: Add character literal parsing and validation --- crates/ra_syntax/src/validation.rs | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 crates/ra_syntax/src/validation.rs (limited to 'crates/ra_syntax/src/validation.rs') diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs new file mode 100644 index 000000000..03d98eff4 --- /dev/null +++ b/crates/ra_syntax/src/validation.rs @@ -0,0 +1,40 @@ +use crate::{ + ast::{self, AstNode}, + File, + string_lexing, + yellow::{ + SyntaxError, + }, +}; + +pub(crate) fn validate(file: &File) -> Vec { + 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 +} -- cgit v1.2.3