2018-10-31 20:41:43 +00:00
|
|
|
use crate::{File, SyntaxKind, SyntaxNodeRef, WalkEvent};
|
2018-10-15 21:44:23 +00:00
|
|
|
use std::fmt::Write;
|
2018-11-07 09:40:58 +00:00
|
|
|
use std::str;
|
2018-07-30 12:25:52 +00:00
|
|
|
|
|
|
|
/// Parse a file and create a string representation of the resulting parse tree.
|
2018-08-17 19:00:13 +00:00
|
|
|
pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
|
2018-10-17 23:25:37 +00:00
|
|
|
let mut errors: Vec<_> = syntax.root_data().to_vec();
|
2018-11-05 17:38:34 +00:00
|
|
|
errors.sort_by_key(|e| e.offset());
|
2018-07-31 19:14:56 +00:00
|
|
|
let mut err_pos = 0;
|
|
|
|
let mut level = 0;
|
|
|
|
let mut buf = String::new();
|
|
|
|
macro_rules! indent {
|
|
|
|
() => {
|
|
|
|
for _ in 0..level {
|
|
|
|
buf.push_str(" ");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-07-30 12:25:52 +00:00
|
|
|
|
2018-10-17 16:52:25 +00:00
|
|
|
for event in syntax.preorder() {
|
2018-07-31 19:14:56 +00:00
|
|
|
match event {
|
|
|
|
WalkEvent::Enter(node) => {
|
|
|
|
indent!();
|
|
|
|
writeln!(buf, "{:?}", node).unwrap();
|
|
|
|
if node.first_child().is_none() {
|
|
|
|
let off = node.range().end();
|
2018-11-05 17:38:34 +00:00
|
|
|
while err_pos < errors.len() && errors[err_pos].offset() <= off {
|
2018-07-31 19:14:56 +00:00
|
|
|
indent!();
|
2018-11-05 17:38:34 +00:00
|
|
|
writeln!(buf, "err: `{}`", errors[err_pos]).unwrap();
|
2018-07-31 19:14:56 +00:00
|
|
|
err_pos += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
level += 1;
|
2018-07-31 19:29:38 +00:00
|
|
|
}
|
2018-10-17 16:52:25 +00:00
|
|
|
WalkEvent::Leave(_) => level -= 1,
|
2018-07-30 12:25:52 +00:00
|
|
|
}
|
2018-07-31 19:14:56 +00:00
|
|
|
}
|
2018-07-30 12:25:52 +00:00
|
|
|
|
2018-07-31 19:14:56 +00:00
|
|
|
assert_eq!(level, 0);
|
2018-07-31 19:29:38 +00:00
|
|
|
for err in errors[err_pos..].iter() {
|
2018-11-05 17:38:34 +00:00
|
|
|
writeln!(buf, "err: `{}`", err).unwrap();
|
2018-07-30 12:25:52 +00:00
|
|
|
}
|
2018-07-31 19:14:56 +00:00
|
|
|
|
2018-10-16 15:51:58 +00:00
|
|
|
buf
|
2018-07-30 12:25:52 +00:00
|
|
|
}
|
2018-09-08 15:34:41 +00:00
|
|
|
|
|
|
|
pub fn check_fuzz_invariants(text: &str) {
|
|
|
|
let file = File::parse(text);
|
|
|
|
let root = file.syntax();
|
|
|
|
validate_block_structure(root);
|
|
|
|
let _ = file.ast();
|
|
|
|
let _ = file.errors();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn validate_block_structure(root: SyntaxNodeRef) {
|
|
|
|
let mut stack = Vec::new();
|
2018-10-02 15:02:57 +00:00
|
|
|
for node in root.descendants() {
|
2018-09-08 15:34:41 +00:00
|
|
|
match node.kind() {
|
2018-10-15 21:44:23 +00:00
|
|
|
SyntaxKind::L_CURLY => stack.push(node),
|
2018-09-08 15:34:41 +00:00
|
|
|
SyntaxKind::R_CURLY => {
|
|
|
|
if let Some(pair) = stack.pop() {
|
|
|
|
assert_eq!(
|
|
|
|
node.parent(),
|
|
|
|
pair.parent(),
|
|
|
|
"\nunpaired curleys:\n{}\n{}\n",
|
|
|
|
root.text(),
|
|
|
|
dump_tree(root),
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
node.next_sibling().is_none() && pair.prev_sibling().is_none(),
|
|
|
|
"\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
|
|
|
|
node,
|
|
|
|
root.text(),
|
|
|
|
node.text(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|