rust/crates/ra_syntax/src/validation.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

2018-11-11 13:27:00 -06:00
mod byte;
2018-11-11 13:41:43 -06:00
mod byte_string;
2018-11-08 08:42:00 -06:00
mod char;
mod string;
mod block;
2018-11-08 08:42:00 -06:00
2019-01-07 07:15:47 -06:00
use crate::{
2019-02-21 06:51:22 -06:00
SourceFile, SyntaxError, AstNode, SyntaxNode,
SyntaxKind::{L_CURLY, R_CURLY},
2019-01-07 07:15:47 -06:00
ast,
algo::visit::{visitor_ctx, VisitorCtx},
};
pub(crate) fn validate(file: &SourceFile) -> Vec<SyntaxError> {
2018-11-08 08:42:00 -06:00
let mut errors = Vec::new();
for node in file.syntax().descendants() {
let _ = visitor_ctx(&mut errors)
2019-02-21 06:51:22 -06:00
.visit::<ast::Byte, _>(byte::validate_byte_node)
.visit::<ast::ByteString, _>(byte_string::validate_byte_string_node)
.visit::<ast::Char, _>(char::validate_char_node)
.visit::<ast::String, _>(string::validate_string_node)
.visit::<ast::Block, _>(block::validate_block_node)
2018-11-08 08:42:00 -06:00
.accept(node);
}
errors
}
2019-02-21 06:51:22 -06:00
pub(crate) fn validate_block_structure(root: &SyntaxNode) {
let mut stack = Vec::new();
for node in root.descendants() {
match node.kind() {
L_CURLY => stack.push(node),
R_CURLY => {
if let Some(pair) = stack.pop() {
assert_eq!(
node.parent(),
pair.parent(),
"\nunpaired curleys:\n{}\n{}\n",
root.text(),
root.debug_dump(),
);
assert!(
node.next_sibling().is_none() && pair.prev_sibling().is_none(),
"\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
node,
root.text(),
node.text(),
);
}
}
_ => (),
}
}
}