aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/utils.rs
blob: 5bef4a6393d4eddf8bf9f3221991ddca6b8f01d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::{File, SyntaxKind, SyntaxNodeRef, WalkEvent};
use std::fmt::Write;
use std::ops::Deref;
use std::str;

/// Parse a file and create a string representation of the resulting parse tree.
pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
    let mut errors: Vec<_> = syntax.root_data().to_vec();
    errors.sort_by_key(|e| e.offset());
    let mut err_pos = 0;
    let mut level = 0;
    let mut buf = String::new();
    macro_rules! indent {
        () => {
            for _ in 0..level {
                buf.push_str("  ");
            }
        };
    }

    for event in syntax.preorder() {
        match event {
            WalkEvent::Enter(node) => {
                indent!();
                writeln!(buf, "{:?}", node).unwrap();
                if node.first_child().is_none() {
                    let off = node.range().end();
                    while err_pos < errors.len() && errors[err_pos].offset() <= off {
                        indent!();
                        writeln!(buf, "err: `{}`", errors[err_pos]).unwrap();
                        err_pos += 1;
                    }
                }
                level += 1;
            }
            WalkEvent::Leave(_) => level -= 1,
        }
    }

    assert_eq!(level, 0);
    for err in errors[err_pos..].iter() {
        writeln!(buf, "err: `{}`", err).unwrap();
    }

    buf
}

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();
    for node in root.descendants() {
        match node.kind() {
            SyntaxKind::L_CURLY => stack.push(node),
            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(),
                    );
                }
            }
            _ => (),
        }
    }
}

#[derive(Debug)]
pub struct MutAsciiString<'a> {
    buf: &'a mut [u8],
    len: usize,
}

impl<'a> MutAsciiString<'a> {
    pub fn new(buf: &'a mut [u8]) -> MutAsciiString<'a> {
        MutAsciiString { buf, len: 0 }
    }

    pub fn as_str(&self) -> &str {
        str::from_utf8(&self.buf[..self.len]).unwrap()
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn push(&mut self, c: char) {
        assert!(self.len() < self.buf.len());
        assert!(c.is_ascii());

        self.buf[self.len] = c as u8;
        self.len += 1;
    }
}

impl<'a> Deref for MutAsciiString<'a> {
    type Target = str;
    fn deref(&self) -> &str {
        self.as_str()
    }
}