aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libsyntax2/src')
-rw-r--r--crates/libsyntax2/src/lib.rs38
-rw-r--r--crates/libsyntax2/src/utils.rs42
2 files changed, 43 insertions, 37 deletions
diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs
index 7a30f5d38..d955c01e7 100644
--- a/crates/libsyntax2/src/lib.rs
+++ b/crates/libsyntax2/src/lib.rs
@@ -66,7 +66,9 @@ impl File {
66 fn new(green: GreenNode, errors: Vec<SyntaxError>) -> File { 66 fn new(green: GreenNode, errors: Vec<SyntaxError>) -> File {
67 let root = SyntaxRoot::new(green, errors); 67 let root = SyntaxRoot::new(green, errors);
68 let root = SyntaxNode::new_owned(root); 68 let root = SyntaxNode::new_owned(root);
69 validate_block_structure(root.borrowed()); 69 if cfg!(debug_assertions) {
70 utils::validate_block_structure(root.borrowed());
71 }
70 File { root } 72 File { root }
71 } 73 }
72 pub fn parse(text: &str) -> File { 74 pub fn parse(text: &str) -> File {
@@ -112,40 +114,6 @@ impl File {
112 } 114 }
113} 115}
114 116
115#[cfg(not(debug_assertions))]
116fn validate_block_structure(_: SyntaxNodeRef) {}
117
118#[cfg(debug_assertions)]
119fn validate_block_structure(root: SyntaxNodeRef) {
120 let mut stack = Vec::new();
121 for node in algo::walk::preorder(root) {
122 match node.kind() {
123 SyntaxKind::L_CURLY => {
124 stack.push(node)
125 }
126 SyntaxKind::R_CURLY => {
127 if let Some(pair) = stack.pop() {
128 assert_eq!(
129 node.parent(),
130 pair.parent(),
131 "\nunpaired curleys:\n{}\n{}\n",
132 root.text(),
133 utils::dump_tree(root),
134 );
135 assert!(
136 node.next_sibling().is_none() && pair.prev_sibling().is_none(),
137 "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
138 node,
139 root.text(),
140 node.text(),
141 );
142 }
143 }
144 _ => (),
145 }
146 }
147}
148
149#[derive(Debug, Clone)] 117#[derive(Debug, Clone)]
150pub struct AtomEdit { 118pub struct AtomEdit {
151 pub delete: TextRange, 119 pub delete: TextRange,
diff --git a/crates/libsyntax2/src/utils.rs b/crates/libsyntax2/src/utils.rs
index fbe48dd71..671dd7afa 100644
--- a/crates/libsyntax2/src/utils.rs
+++ b/crates/libsyntax2/src/utils.rs
@@ -1,7 +1,7 @@
1use std::fmt::Write; 1use std::fmt::Write;
2use { 2use {
3 algo::walk::{walk, WalkEvent}, 3 algo::walk::{preorder, walk, WalkEvent},
4 SyntaxNodeRef, TreeRoot, 4 SyntaxKind, File, SyntaxNodeRef, TreeRoot,
5}; 5};
6 6
7/// Parse a file and create a string representation of the resulting parse tree. 7/// Parse a file and create a string representation of the resulting parse tree.
@@ -45,3 +45,41 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
45 45
46 return buf; 46 return buf;
47} 47}
48
49pub fn check_fuzz_invariants(text: &str) {
50 let file = File::parse(text);
51 let root = file.syntax();
52 validate_block_structure(root);
53 let _ = file.ast();
54 let _ = file.errors();
55}
56
57pub(crate) fn validate_block_structure(root: SyntaxNodeRef) {
58 let mut stack = Vec::new();
59 for node in preorder(root) {
60 match node.kind() {
61 SyntaxKind::L_CURLY => {
62 stack.push(node)
63 }
64 SyntaxKind::R_CURLY => {
65 if let Some(pair) = stack.pop() {
66 assert_eq!(
67 node.parent(),
68 pair.parent(),
69 "\nunpaired curleys:\n{}\n{}\n",
70 root.text(),
71 dump_tree(root),
72 );
73 assert!(
74 node.next_sibling().is_none() && pair.prev_sibling().is_none(),
75 "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
76 node,
77 root.text(),
78 node.text(),
79 );
80 }
81 }
82 _ => (),
83 }
84 }
85}