diff options
Diffstat (limited to 'crates/ra_syntax/src/validation.rs')
-rw-r--r-- | crates/ra_syntax/src/validation.rs | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 69958f0d7..69f344d65 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs | |||
@@ -5,7 +5,8 @@ mod string; | |||
5 | mod block; | 5 | mod block; |
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
8 | SourceFile, SyntaxError, AstNode, | 8 | SourceFile, SyntaxError, AstNode, SyntaxNode, |
9 | SyntaxKind::{L_CURLY, R_CURLY}, | ||
9 | ast, | 10 | ast, |
10 | algo::visit::{visitor_ctx, VisitorCtx}, | 11 | algo::visit::{visitor_ctx, VisitorCtx}, |
11 | }; | 12 | }; |
@@ -14,12 +15,40 @@ pub(crate) fn validate(file: &SourceFile) -> Vec<SyntaxError> { | |||
14 | let mut errors = Vec::new(); | 15 | let mut errors = Vec::new(); |
15 | for node in file.syntax().descendants() { | 16 | for node in file.syntax().descendants() { |
16 | let _ = visitor_ctx(&mut errors) | 17 | let _ = visitor_ctx(&mut errors) |
17 | .visit::<ast::Byte, _>(self::byte::validate_byte_node) | 18 | .visit::<ast::Byte, _>(byte::validate_byte_node) |
18 | .visit::<ast::ByteString, _>(self::byte_string::validate_byte_string_node) | 19 | .visit::<ast::ByteString, _>(byte_string::validate_byte_string_node) |
19 | .visit::<ast::Char, _>(self::char::validate_char_node) | 20 | .visit::<ast::Char, _>(char::validate_char_node) |
20 | .visit::<ast::String, _>(self::string::validate_string_node) | 21 | .visit::<ast::String, _>(string::validate_string_node) |
21 | .visit::<ast::Block, _>(self::block::validate_block_node) | 22 | .visit::<ast::Block, _>(block::validate_block_node) |
22 | .accept(node); | 23 | .accept(node); |
23 | } | 24 | } |
24 | errors | 25 | errors |
25 | } | 26 | } |
27 | |||
28 | pub(crate) fn validate_block_structure(root: &SyntaxNode) { | ||
29 | let mut stack = Vec::new(); | ||
30 | for node in root.descendants() { | ||
31 | match node.kind() { | ||
32 | L_CURLY => stack.push(node), | ||
33 | R_CURLY => { | ||
34 | if let Some(pair) = stack.pop() { | ||
35 | assert_eq!( | ||
36 | node.parent(), | ||
37 | pair.parent(), | ||
38 | "\nunpaired curleys:\n{}\n{}\n", | ||
39 | root.text(), | ||
40 | root.debug_dump(), | ||
41 | ); | ||
42 | assert!( | ||
43 | node.next_sibling().is_none() && pair.prev_sibling().is_none(), | ||
44 | "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n", | ||
45 | node, | ||
46 | root.text(), | ||
47 | node.text(), | ||
48 | ); | ||
49 | } | ||
50 | } | ||
51 | _ => (), | ||
52 | } | ||
53 | } | ||
54 | } | ||