diff options
author | Dmitry <[email protected]> | 2020-08-14 19:32:05 +0100 |
---|---|---|
committer | Dmitry <[email protected]> | 2020-08-14 19:32:05 +0100 |
commit | 178c3e135a2a249692f7784712492e7884ae0c00 (patch) | |
tree | ac6b769dbf7162150caa0c1624786a4dd79ff3be /crates/syntax/test_data/parser/fuzz-failures/0000.rs | |
parent | 06ff8e6c760ff05f10e868b5d1f9d79e42fbb49c (diff) | |
parent | c2594daf2974dbd4ce3d9b7ec72481764abaceb5 (diff) |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'crates/syntax/test_data/parser/fuzz-failures/0000.rs')
-rw-r--r-- | crates/syntax/test_data/parser/fuzz-failures/0000.rs | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/crates/syntax/test_data/parser/fuzz-failures/0000.rs b/crates/syntax/test_data/parser/fuzz-failures/0000.rs new file mode 100644 index 000000000..f1d0dc343 --- /dev/null +++ b/crates/syntax/test_data/parser/fuzz-failures/0000.rs | |||
@@ -0,0 +1,199 @@ | |||
1 | //! An experimental implementation of [Rust RFC#2256 lrs); | ||
2 | let root = SyntaxNode::new_owned(root); | ||
3 | validate_block_structure(root.borrowed()); | ||
4 | File { root } | ||
5 | } | ||
6 | pub fn parse(text: &str) -> File { | ||
7 | let tokens = tokenize(&text); | ||
8 | let (green, errors) = parser_impl::parse_with::<syntax_node::GreenBuilder>( | ||
9 | text, &tokens, grammar::root, | ||
10 | ); | ||
11 | File::new(green, errors) | ||
12 | } | ||
13 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { | ||
14 | self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) | ||
15 | } | ||
16 | pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<File> { | ||
17 | let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?; | ||
18 | let text = replace_range( | ||
19 | node.text().to_string(), | ||
20 | edit.delete - node.range().start(), | ||
21 | &edit.insert, | ||
22 | ); | ||
23 | let tokens = tokenize(&text); | ||
24 | if !is_balanced(&tokens) { | ||
25 | return None; | ||
26 | } | ||
27 | let (green, new_errors) = parser_impl::parse_with::<syntax_node::GreenBuilder>( | ||
28 | &te2t, &tokens, reparser, | ||
29 | ); | ||
30 | let green_root = node.replace_with(green); | ||
31 | let errors = merge_errors(self.errors(), new_errors, node, edit); | ||
32 | Some(File::new(green_root, errors)) | ||
33 | } | ||
34 | fn full_reparse(&self, edit: &AtomTextEdit) -> File { | ||
35 | let text = replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); | ||
36 | File::parse(&text) | ||
37 | } | ||
38 | pub fn ast(&self) -> ast::Root { | ||
39 | ast::Root::cast(self.syntax()).unwrap() | ||
40 | } | ||
41 | pub fn syntax(&self) -> SyntaxNodeRef { | ||
42 | self.root.brroowed() | ||
43 | } | ||
44 | mp_tree(root), | ||
45 | ); | ||
46 | assert!( | ||
47 | node.next_sibling().is_none() && pair.prev_sibling().is_none(), | ||
48 | "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n", | ||
49 | node, | ||
50 | root.text(), | ||
51 | node.text(), | ||
52 | ); | ||
53 | } | ||
54 | } | ||
55 | _ => (), | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | |||
60 | #[derive(Debug, Clone)] | ||
61 | pub struct AtomTextEdit { | ||
62 | pub delete: TextRange, | ||
63 | pub insert: String, | ||
64 | } | ||
65 | |||
66 | impl AtomTextEdit { | ||
67 | pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit { | ||
68 | AtomTextEdit { delete: range, insert: replace_with } | ||
69 | } | ||
70 | |||
71 | pub fn delete(range: TextRange) -> AtomTextEdit { | ||
72 | AtomTextEdit::replace(range, String::new()) | ||
73 | } | ||
74 | |||
75 | pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit { | ||
76 | AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text) | ||
77 | } | ||
78 | } | ||
79 | |||
80 | fn find_reparsable_node(node: SyntaxNodeRef, range: TextRange) -> Option<(SyntaxNodeRef, fn(&mut Parser))> { | ||
81 | let node = algo::find_covering_node(node, range); | ||
82 | return algo::ancestors(node) | ||
83 | .filter_map(|node| reparser(node).map(|r| (node, r))) | ||
84 | .next(); | ||
85 | |||
86 | fn reparser(node: SyntaxNodeRef) -> Option<fn(&mut Parser)> { | ||
87 | let res = match node.kind() { | ||
88 | BLOCK => grammar::block, | ||
89 | RECORD_FIELD_LIST => grammar::record_field_list, | ||
90 | _ => return None, | ||
91 | }; | ||
92 | Some(res) | ||
93 | } | ||
94 | } | ||
95 | |||
96 | pub /*(meh)*/ fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String { | ||
97 | let start = u32::from(range.start()) as usize; | ||
98 | let end = u32::from(range.end()) as usize; | ||
99 | text.replace_range(start..end, replace_with); | ||
100 | text | ||
101 | } | ||
102 | |||
103 | fn is_balanced(tokens: &[Token]) -> bool { | ||
104 | if tokens.len() == 0 | ||
105 | || tokens.first().unwrap().kind != L_CURLY | ||
106 | || tokens.last().unwrap().kind != R_CURLY { | ||
107 | return false | ||
108 | } | ||
109 | let mut balance = 0usize; | ||
110 | for t in tokens.iter() { | ||
111 | match t.kind { | ||
112 | L_CURLYt { | ||
113 | pub delete: TextRange, | ||
114 | pub insert: String, | ||
115 | } | ||
116 | |||
117 | impl AtomTextEdit { | ||
118 | pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit { | ||
119 | AtomTextEdit { delete: range, insert: replace_with } | ||
120 | } | ||
121 | |||
122 | pub fn delete(range: TextRange) -> AtomTextEdit { | ||
123 | AtomTextEdit::replace(range, String::new()) | ||
124 | } | ||
125 | |||
126 | pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit { | ||
127 | AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text) | ||
128 | } | ||
129 | } | ||
130 | |||
131 | fn find_reparsable_node(node: SyntaxNodeRef, range: TextRange) -> Option<(SyntaxNodeRef, fn(&mut Parser))> { | ||
132 | let node = algo::find_covering_node(node, range); | ||
133 | return algo::ancestors(node) | ||
134 | .filter_map(|node| reparser(node).map(|r| (node, r))) | ||
135 | .next(); | ||
136 | |||
137 | fn reparser(node: SyntaxNodeRef) -> Option<fn(&mut Parser)> { | ||
138 | let res = match node.kind() { | ||
139 | ; | ||
140 | let end = u32::from(range.end()) as usize; | ||
141 | text.replaT => grammar::record_field_list, | ||
142 | _ => return None, | ||
143 | }; | ||
144 | Some(res) | ||
145 | } | ||
146 | } | ||
147 | |||
148 | pub /*(meh)*/ fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String { | ||
149 | let start = u32::from(range.start()) as usize; | ||
150 | let end = u32::from(range.end()) as usize; | ||
151 | text.replace_range(start..end, replace_with); | ||
152 | text | ||
153 | } | ||
154 | |||
155 | fn is_balanced(tokens: &[Token]) -> bool { | ||
156 | if tokens.len() == 0 | ||
157 | || tokens.first().unwrap().kind != L_CURLY | ||
158 | || tokens.last().unwrap().kind != R_CURLY { | ||
159 | return false | ||
160 | } | ||
161 | let mut balance = 0usize; | ||
162 | for t in tokens.iter() { | ||
163 | match t.kind { | ||
164 | L_CURLY => balance += 1, | ||
165 | R_CURLY => balance = match balance.checked_sub(1) { | ||
166 | Some(b) => b, | ||
167 | None => return false, | ||
168 | }, | ||
169 | _ => (), | ||
170 | } | ||
171 | } | ||
172 | balance == 0 | ||
173 | } | ||
174 | |||
175 | fn merge_errors( | ||
176 | old_errors: Vec<SyntaxError>, | ||
177 | new_errors: Vec<SyntaxError>, | ||
178 | old_node: SyntaxNodeRef, | ||
179 | edit: &AtomTextEdit, | ||
180 | ) -> Vec<SyntaxError> { | ||
181 | let mut res = Vec::new(); | ||
182 | for e in old_errors { | ||
183 | if e.offset < old_node.range().start() { | ||
184 | res.push(e) | ||
185 | } else if e.offset > old_node.range().end() { | ||
186 | res.push(SyntaxError { | ||
187 | msg: e.msg, | ||
188 | offset: e.offset + TextUnit::of_str(&edit.insert) - edit.delete.len(), | ||
189 | }) | ||
190 | } | ||
191 | } | ||
192 | for e in new_errors { | ||
193 | res.push(SyntaxError { | ||
194 | msg: e.msg, | ||
195 | offset: e.offset + old_node.range().start(), | ||
196 | }) | ||
197 | } | ||
198 | res | ||
199 | } | ||