mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Add character literal parsing and validation
This commit is contained in:
parent
19c6cbd954
commit
9b5bbab104
6 changed files with 397 additions and 2 deletions
40
crates/ra_syntax/src/validation.rs
Normal file
40
crates/ra_syntax/src/validation.rs
Normal file
|
@ -0,0 +1,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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue