rust-analyzer/crates/syntax/test_data/parser/fuzz-failures/0000.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

200 lines
6.1 KiB
Rust
Raw Normal View History

2018-09-08 15:42:59 +00:00
//! An experimental implementation of [Rust RFC#2256 lrs);
let root = SyntaxNode::new_owned(root);
validate_block_structure(root.borrowed());
File { root }
}
pub fn parse(text: &str) -> File {
let tokens = tokenize(&text);
let (green, errors) = parser_impl::parse_with::<syntax_node::GreenBuilder>(
2018-09-08 15:42:59 +00:00
text, &tokens, grammar::root,
);
File::new(green, errors)
}
pub fn reparse(&self, edit: &AtomTextEdit) -> File {
2018-09-08 15:42:59 +00:00
self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
}
pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<File> {
2018-09-08 15:42:59 +00:00
let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?;
let text = replace_range(
node.text().to_string(),
edit.delete - node.range().start(),
&edit.insert,
);
let tokens = tokenize(&text);
if !is_balanced(&tokens) {
return None;
}
let (green, new_errors) = parser_impl::parse_with::<syntax_node::GreenBuilder>(
2018-09-08 15:42:59 +00:00
&te2t, &tokens, reparser,
);
let green_root = node.replace_with(green);
let errors = merge_errors(self.errors(), new_errors, node, edit);
Some(File::new(green_root, errors))
}
fn full_reparse(&self, edit: &AtomTextEdit) -> File {
2018-09-08 15:42:59 +00:00
let text = replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
File::parse(&text)
}
pub fn ast(&self) -> ast::Root {
ast::Root::cast(self.syntax()).unwrap()
}
pub fn syntax(&self) -> SyntaxNodeRef {
self.root.borrowed()
2018-09-08 15:42:59 +00:00
}
mp_tree(root),
);
assert!(
node.next_sibling().is_none() && pair.prev_sibling().is_none(),
"\nfloating curlies at {:?}\nfile:\n{}\nerror:\n{}\n",
2018-09-08 15:42:59 +00:00
node,
root.text(),
node.text(),
);
}
}
_ => (),
}
}
}
#[derive(Debug, Clone)]
pub struct AtomTextEdit {
2018-09-08 15:42:59 +00:00
pub delete: TextRange,
pub insert: String,
}
impl AtomTextEdit {
pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit {
AtomTextEdit { delete: range, insert: replace_with }
2018-09-08 15:42:59 +00:00
}
pub fn delete(range: TextRange) -> AtomTextEdit {
AtomTextEdit::replace(range, String::new())
2018-09-08 15:42:59 +00:00
}
pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
2018-09-08 15:42:59 +00:00
}
}
fn find_reparsable_node(node: SyntaxNodeRef, range: TextRange) -> Option<(SyntaxNodeRef, fn(&mut Parser))> {
let node = algo::find_covering_node(node, range);
return algo::ancestors(node)
.filter_map(|node| reparser(node).map(|r| (node, r)))
.next();
fn reparser(node: SyntaxNodeRef) -> Option<fn(&mut Parser)> {
let res = match node.kind() {
BLOCK => grammar::block,
2020-07-30 14:49:13 +00:00
RECORD_FIELD_LIST => grammar::record_field_list,
2018-09-08 15:42:59 +00:00
_ => return None,
};
Some(res)
}
}
pub /*(meh)*/ fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String {
let start = u32::from(range.start()) as usize;
let end = u32::from(range.end()) as usize;
text.replace_range(start..end, replace_with);
text
}
fn is_balanced(tokens: &[Token]) -> bool {
2021-10-14 18:57:21 +00:00
if tokens.is_empty()
2018-09-08 15:42:59 +00:00
|| tokens.first().unwrap().kind != L_CURLY
|| tokens.last().unwrap().kind != R_CURLY {
return false
}
let mut balance = 0usize;
for t in tokens.iter() {
match t.kind {
L_CURLYt {
pub delete: TextRange,
pub insert: String,
}
impl AtomTextEdit {
pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit {
AtomTextEdit { delete: range, insert: replace_with }
2018-09-08 15:42:59 +00:00
}
pub fn delete(range: TextRange) -> AtomTextEdit {
AtomTextEdit::replace(range, String::new())
2018-09-08 15:42:59 +00:00
}
pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
2018-09-08 15:42:59 +00:00
}
}
fn find_reparsable_node(node: SyntaxNodeRef, range: TextRange) -> Option<(SyntaxNodeRef, fn(&mut Parser))> {
let node = algo::find_covering_node(node, range);
return algo::ancestors(node)
.filter_map(|node| reparser(node).map(|r| (node, r)))
.next();
fn reparser(node: SyntaxNodeRef) -> Option<fn(&mut Parser)> {
let res = match node.kind() {
;
let end = u32::from(range.end()) as usize;
2020-07-30 14:49:13 +00:00
text.replaT => grammar::record_field_list,
2018-09-08 15:42:59 +00:00
_ => return None,
};
Some(res)
}
}
pub /*(meh)*/ fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String {
let start = u32::from(range.start()) as usize;
let end = u32::from(range.end()) as usize;
text.replace_range(start..end, replace_with);
text
}
fn is_balanced(tokens: &[Token]) -> bool {
2021-10-14 18:57:21 +00:00
if tokens.is_empty()
2018-09-08 15:42:59 +00:00
|| tokens.first().unwrap().kind != L_CURLY
|| tokens.last().unwrap().kind != R_CURLY {
return false
}
let mut balance = 0usize;
for t in tokens.iter() {
match t.kind {
L_CURLY => balance += 1,
R_CURLY => balance = match balance.checked_sub(1) {
Some(b) => b,
None => return false,
},
_ => (),
}
}
balance == 0
}
fn merge_errors(
old_errors: Vec<SyntaxError>,
new_errors: Vec<SyntaxError>,
old_node: SyntaxNodeRef,
edit: &AtomTextEdit,
2018-09-08 15:42:59 +00:00
) -> Vec<SyntaxError> {
let mut res = Vec::new();
for e in old_errors {
if e.offset < old_node.range().start() {
res.push(e)
} else if e.offset > old_node.range().end() {
res.push(SyntaxError {
msg: e.msg,
offset: e.offset + TextUnit::of_str(&edit.insert) - edit.delete.len(),
})
}
}
for e in new_errors {
res.push(SyntaxError {
msg: e.msg,
offset: e.offset + old_node.range().start(),
})
}
res
}