rust/crates/ra_syntax/src/validation.rs

65 lines
2.0 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;
2019-04-05 15:34:45 -05:00
mod field_expr;
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,
2019-03-30 05:25:53 -05:00
SyntaxKind::{L_CURLY, R_CURLY, BYTE, BYTE_STRING, STRING, CHAR},
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-03-30 05:25:53 -05:00
.visit::<ast::Literal, _>(validate_literal)
2019-02-21 06:51:22 -06:00
.visit::<ast::Block, _>(block::validate_block_node)
2019-04-05 15:34:45 -05:00
.visit::<ast::FieldExpr, _>(field_expr::validate_field_expr_node)
2018-11-08 08:42:00 -06:00
.accept(node);
}
errors
}
2019-02-21 06:51:22 -06:00
2019-03-30 05:25:53 -05:00
// FIXME: kill duplication
fn validate_literal(literal: &ast::Literal, acc: &mut Vec<SyntaxError>) {
match literal.token().kind() {
BYTE => byte::validate_byte_node(literal.token(), acc),
BYTE_STRING => byte_string::validate_byte_string_node(literal.token(), acc),
STRING => string::validate_string_node(literal.token(), acc),
CHAR => char::validate_char_node(literal.token(), acc),
_ => (),
}
}
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(),
);
}
}
_ => (),
}
}
}