diff --git a/benches/oneshot.rs b/benches/oneshot.rs index d3e2ff8e4..c088a93c8 100644 --- a/benches/oneshot.rs +++ b/benches/oneshot.rs @@ -49,6 +49,11 @@ fn bench_parse(iai: &mut Iai) { iai.run(|| parse(SRC)); } +fn bench_edit(iai: &mut Iai) { + let (mut ctx, id) = context(); + iai.run(|| black_box(ctx.sources.edit(id, 1168 .. 1171, "_Uhr_"))); +} + fn bench_eval(iai: &mut Iai) { let (mut ctx, id) = context(); iai.run(|| ctx.evaluate(id).unwrap()); @@ -66,6 +71,7 @@ main!( bench_scan, bench_tokenize, bench_parse, + bench_edit, bench_eval, bench_layout ); diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 1f1ac2660..f2fae5f28 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -26,14 +26,14 @@ pub fn parse(src: &str) -> Rc { } /// Parse an atomic primary. Returns `Some` if all of the input was consumed. -pub fn parse_atomic(src: &str, _: bool) -> Option> { +pub fn parse_atomic(src: &str, _: bool) -> Option<(Vec, bool)> { let mut p = Parser::new(src, TokenMode::Code); primary(&mut p, true).ok()?; p.eject_partial() } /// Parse some markup. Returns `Some` if all of the input was consumed. -pub fn parse_markup(src: &str, _: bool) -> Option> { +pub fn parse_markup(src: &str, _: bool) -> Option<(Vec, bool)> { let mut p = Parser::new(src, TokenMode::Markup); markup(&mut p); p.eject() @@ -41,7 +41,10 @@ pub fn parse_markup(src: &str, _: bool) -> Option> { /// Parse some markup without the topmost node. Returns `Some` if all of the /// input was consumed. -pub fn parse_markup_elements(src: &str, mut at_start: bool) -> Option> { +pub fn parse_markup_elements( + src: &str, + mut at_start: bool, +) -> Option<(Vec, bool)> { let mut p = Parser::new(src, TokenMode::Markup); while !p.eof() { markup_node(&mut p, &mut at_start); @@ -50,7 +53,7 @@ pub fn parse_markup_elements(src: &str, mut at_start: bool) -> Option } /// Parse a template literal. Returns `Some` if all of the input was consumed. -pub fn parse_template(source: &str, _: bool) -> Option> { +pub fn parse_template(source: &str, _: bool) -> Option<(Vec, bool)> { let mut p = Parser::new(source, TokenMode::Code); if !matches!(p.peek(), Some(NodeKind::LeftBracket)) { return None; @@ -61,7 +64,7 @@ pub fn parse_template(source: &str, _: bool) -> Option> { } /// Parse a code block. Returns `Some` if all of the input was consumed. -pub fn parse_block(source: &str, _: bool) -> Option> { +pub fn parse_block(source: &str, _: bool) -> Option<(Vec, bool)> { let mut p = Parser::new(source, TokenMode::Code); if !matches!(p.peek(), Some(NodeKind::LeftBrace)) { return None; @@ -72,7 +75,7 @@ pub fn parse_block(source: &str, _: bool) -> Option> { } /// Parse a comment. Returns `Some` if all of the input was consumed. -pub fn parse_comment(source: &str, _: bool) -> Option> { +pub fn parse_comment(source: &str, _: bool) -> Option<(Vec, bool)> { let mut p = Parser::new(source, TokenMode::Code); comment(&mut p).ok()?; p.eject() diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 31c918a8f..a37cb9c62 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -48,9 +48,9 @@ impl<'s> Parser<'s> { } /// End the parsing process and return multiple children. - pub fn eject(self) -> Option> { + pub fn eject(self) -> Option<(Vec, bool)>{ if self.eof() && self.group_success() { - Some(self.children) + Some((self.children, self.tokens.was_unterminated())) } else { None } @@ -97,8 +97,9 @@ impl<'s> Parser<'s> { /// End the parsing process and return multiple children, even if there /// remains stuff in the string. - pub fn eject_partial(self) -> Option> { - self.group_success().then(|| self.children) + pub fn eject_partial(self) -> Option<(Vec, bool)> { + self.group_success() + .then(|| (self.children, self.tokens.was_unterminated())) } /// Whether the end of the source string or group is reached. diff --git a/src/parse/tokens.rs b/src/parse/tokens.rs index 27ec046df..7be31fe18 100644 --- a/src/parse/tokens.rs +++ b/src/parse/tokens.rs @@ -13,6 +13,7 @@ use crate::util::EcoString; pub struct Tokens<'s> { s: Scanner<'s>, mode: TokenMode, + has_unterminated: bool, } /// What kind of tokens to emit. @@ -28,7 +29,11 @@ impl<'s> Tokens<'s> { /// Create a new token iterator with the given mode. #[inline] pub fn new(src: &'s str, mode: TokenMode) -> Self { - Self { s: Scanner::new(src), mode } + Self { + s: Scanner::new(src), + mode, + has_unterminated: false, + } } /// Get the current token mode. @@ -63,6 +68,12 @@ impl<'s> Tokens<'s> { pub fn scanner(&self) -> Scanner<'s> { self.s } + + /// Whether the last token was unterminated. + #[inline] + pub fn was_unterminated(&self) -> bool { + self.has_unterminated + } } impl<'s> Iterator for Tokens<'s> { @@ -248,6 +259,7 @@ impl<'s> Tokens<'s> { ) } } else { + self.has_unterminated = true; NodeKind::Error( ErrorPos::End, "expected closing brace".into(), @@ -346,6 +358,7 @@ impl<'s> Tokens<'s> { let remaining = backticks - found; let noun = if remaining == 1 { "backtick" } else { "backticks" }; + self.has_unterminated = true; NodeKind::Error( ErrorPos::End, if found == 0 { @@ -393,6 +406,7 @@ impl<'s> Tokens<'s> { display, })) } else { + self.has_unterminated = true; NodeKind::Error( ErrorPos::End, if !display || (!escaped && dollar) { @@ -481,18 +495,23 @@ impl<'s> Tokens<'s> { if self.s.eat_if('"') { NodeKind::Str(string) } else { + self.has_unterminated = true; NodeKind::Error(ErrorPos::End, "expected quote".into()) } } fn line_comment(&mut self) -> NodeKind { self.s.eat_until(is_newline); + if self.s.peek().is_none() { + self.has_unterminated = true; + } NodeKind::LineComment } fn block_comment(&mut self) -> NodeKind { let mut state = '_'; let mut depth = 1; + let mut terminated = false; // Find the first `*/` that does not correspond to a nested `/*`. while let Some(c) = self.s.eat() { @@ -500,6 +519,7 @@ impl<'s> Tokens<'s> { ('*', '/') => { depth -= 1; if depth == 0 { + terminated = true; break; } '_' @@ -512,6 +532,10 @@ impl<'s> Tokens<'s> { } } + if !terminated { + self.has_unterminated = true; + } + NodeKind::BlockComment } diff --git a/src/source.rs b/src/source.rs index 797e815b1..7eb1d3a7f 100644 --- a/src/source.rs +++ b/src/source.rs @@ -128,7 +128,6 @@ pub struct SourceFile { src: String, line_starts: Vec, root: Rc, - was_incremental: bool, } impl SourceFile { @@ -142,7 +141,6 @@ impl SourceFile { root: parse(&src), src, line_starts, - was_incremental: false, } } @@ -268,7 +266,7 @@ impl SourceFile { /// Edit the source file by replacing the given range. /// /// This panics if the `replace` range is out of bounds. - pub fn edit(&mut self, replace: Range, with: &str) { + pub fn edit(&mut self, replace: Range, with: &str) -> Range { let start = replace.start; self.src.replace_range(replace.clone(), with); @@ -287,11 +285,13 @@ impl SourceFile { // Update the root node. let span = Span::new(self.id, replace.start, replace.end); - if Rc::make_mut(&mut self.root).incremental(&self.src, span, with.len()) { - self.was_incremental = true; + if let Ok(range) = + Rc::make_mut(&mut self.root).incremental(&self.src, span, with.len()) + { + range } else { self.root = parse(&self.src); - self.was_incremental = false; + 0 .. self.src.len() } } @@ -485,93 +485,108 @@ mod tests { #[test] fn test_incremental_parse() { #[track_caller] - fn test(prev: &str, range: Range, with: &str, incr: bool) { + fn test(prev: &str, range: Range, with: &str, incr: Range) { let mut source = SourceFile::detached(prev); - source.edit(range, with); + let range = source.edit(range, with); + assert_eq!(range, incr); - if incr { - assert!(source.was_incremental); - let incr_tree = source.root.clone(); - assert_eq!(parse(source.src()), incr_tree); - } else { - assert!(!source.was_incremental); - } + let incr_tree = source.root.clone(); + assert_eq!(parse(source.src()), incr_tree); } // Test simple replacements. - test("hello world", 6 .. 11, "wankers", true); - test("a d e", 1 .. 3, " b c d", true); - test("a #f() e", 1 .. 6, " b c d", false); - test("{(0, 1, 2)}", 5 .. 6, "11pt", true); - test("= A heading", 3 .. 3, "n evocative", true); + test("hello world", 6 .. 11, "wankers", 5 .. 13); + test("a d e", 1 .. 3, " b c d", 0 .. 8); + test("a #f() e", 1 .. 6, " b c d", 0 .. 8); + test("{(0, 1, 2)}", 5 .. 6, "11pt", 5 .. 9); + test("= A heading", 3 .. 3, "n evocative", 2 .. 15); + test("your thing", 5 .. 5, "a", 4 .. 11); + test("a your thing a", 6 .. 7, "a", 2 .. 12); test( "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])", 16 .. 20, "none", - true, + 16 .. 20, ); test( "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])", 33 .. 42, "[_gronk_]", - true, + 33 .. 42, ); test( "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])", 34 .. 41, "_bar_", - true, + 34 .. 39, ); - test("{let i=1; for x in range(5) {i}}", 6 .. 6, " ", true); - test("{let i=1; for x in range(5) {i}}", 13 .. 14, " ", true); - test("hello {x}", 6 .. 9, "#f()", false); + test("{let i=1; for x in range(5) {i}}", 6 .. 6, " ", 1 .. 9); + test("{let i=1; for x in range(5) {i}}", 13 .. 14, " ", 13 .. 15); + test("hello {x}", 6 .. 9, "#f()", 5 .. 10); test( "this is -- in my opinion -- spectacular", 8 .. 10, "---", - true, + 7 .. 12, ); - test("understanding `code` is complicated", 15 .. 15, "C ", true); - test("{ let x = g() }", 10 .. 12, "f(54", true); test( - "#let rect with (fill: eastern)", - 14 .. 29, - " (stroke: conifer", - true, + "understanding `code` is complicated", + 15 .. 15, + "C ", + 14 .. 22, + ); + test("{ let x = g() }", 10 .. 12, "f(54", 2 .. 15); + test( + "a #let rect with (fill: eastern)\nb", + 16 .. 31, + " (stroke: conifer", + 2 .. 34, ); - test("a b c", 1 .. 1, " /* letters */", false); // Test the whitespace invariants. - test("hello \\ world", 7 .. 8, "a ", false); - test("hello \\ world", 7 .. 8, "\n\n", true); - test("x = y", 2 .. 2, "+ y ", true); - test("x = y", 2 .. 2, "+ y \n ", false); - test("abc\n= a heading", 3 .. 4, "\nsome more test\n\n", true); - test("abc\n= a heading", 3 .. 4, "\nnot ", false); - test("hey #myfriend", 4 .. 4, "\\", false); - test("hey #myfriend", 4 .. 4, "\\", true); + test("hello \\ world", 7 .. 8, "a ", 6 .. 14); + test("hello \\ world", 7 .. 8, " a", 6 .. 14); + test("x = y", 1 .. 1, " + y", 0 .. 6); + test("x = y", 1 .. 1, " + y\n", 0 .. 10); + test("abc\n= a heading\njoke", 3 .. 4, "\nmore\n\n", 0 .. 21); + test("abc\n= a heading\njoke", 3 .. 4, "\nnot ", 0 .. 19); + test("hey #myfriend", 4 .. 4, "\\", 0 .. 14); + test("hey #myfriend", 4 .. 4, "\\", 3 .. 6); // Test type invariants. - test("#for x in array {x}", 16 .. 19, "[#x]", true); - test("#let x = 1 {5}", 1 .. 4, "if", false); - test("{let x = 1 {5}}", 1 .. 4, "if", true); - test("#let x = 1 {5}", 4 .. 4, " if", false); - test("{let x = 1 {5}}", 4 .. 4, " if", true); - test("a // b c #f()", 3 .. 4, "", false); - test("{\nf()\n//g(a)\n}", 6 .. 8, "", true); - test("{(1, 2)}", 1 .. 1, "while ", true); - - // this appearantly works but the assertion fails. - test("a b c", 1 .. 1, "{[}", true); + test("a #for x in array {x}", 18 .. 21, "[#x]", 2 .. 22); + test("a #let x = 1 {5}", 3 .. 6, "if", 0 .. 15); + test("a {let x = 1 {5}} b", 3 .. 6, "if", 2 .. 16); + test("#let x = 1 {5}", 4 .. 4, " if", 0 .. 17); + test("{let x = 1 {5}}", 4 .. 4, " if", 0 .. 18); + test("a // b c #f()", 3 .. 4, "", 0 .. 12); + test("{\nf()\n//g(a)\n}", 6 .. 8, "", 0 .. 12); + test("a{\nf()\n//g(a)\n}b", 7 .. 9, "", 1 .. 13); + test("a #while x {\n g(x) \n} b", 11 .. 11, "//", 0 .. 26); + test("{(1, 2)}", 1 .. 1, "while ", 0 .. 14); + test("a b c", 1 .. 1, "{[}", 0 .. 5); // Test unclosed things. - test(r#"{"hi"}"#, 4 .. 5, "c", false); - test(r"this \u{abcd}", 8 .. 9, "", true); - test(r"this \u{abcd} that", 12 .. 13, "", false); - test(r"{{let x = z}; a = 1} b", 6 .. 6, "//", false); + test(r#"{"hi"}"#, 4 .. 5, "c", 0 .. 6); + test(r"this \u{abcd}", 8 .. 9, "", 5 .. 12); + test(r"this \u{abcd} that", 12 .. 13, "", 0 .. 17); + test(r"{{let x = z}; a = 1} b", 6 .. 6, "//", 0 .. 24); + test("a b c", 1 .. 1, " /* letters */", 0 .. 16); + test("a b c", 1 .. 1, " /* letters", 0 .. 16); + test( + "{if i==1 {a} else [b]; b()}", + 12 .. 12, + " /* letters */", + 1 .. 35, + ); + test( + "{if i==1 {a} else [b]; b()}", + 12 .. 12, + " /* letters", + 0 .. 38, + ); - // these appearantly works but the assertion fails. - test(r#"a ```typst hello``` b"#, 16 .. 17, "", false); - test(r#"a ```typst hello```"#, 16 .. 17, "", true); + test(r#"a ```typst hello``` b"#, 16 .. 17, "", 0 .. 20); + test(r#"a ```typst hello```"#, 16 .. 17, "", 2 .. 18); } } diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index d1ca36746..cfb443761 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -6,6 +6,7 @@ mod pretty; mod span; use std::fmt::{self, Debug, Display, Formatter}; +use std::ops::Range; use std::rc::Rc; pub use highlight::*; @@ -88,7 +89,7 @@ impl Green { } /// Find the innermost child that is incremental safe. - fn incremental_int( + fn incremental( &mut self, edit: &str, replace: Span, @@ -96,7 +97,7 @@ impl Green { offset: usize, parent_mode: TokenMode, outermost: bool, - ) -> bool { + ) -> Result, ()> { match self { Green::Node(n) => Rc::make_mut(n).incremental_int( edit, @@ -106,21 +107,7 @@ impl Green { parent_mode, outermost, ), - Green::Token(_) => false, - } - } - - /// The error messages for this node and its descendants. - pub fn errors(&self) -> Vec { - match self { - Green::Node(n) => n.errors(), - Green::Token(t) => { - if t.kind().is_error() { - vec![t.kind().clone()] - } else { - vec![] - } - } + Green::Token(_) => Err(()), } } } @@ -198,15 +185,23 @@ impl GreenNode { self.data().len() } - /// The error messages for this node and its descendants. - pub fn errors(&self) -> Vec { - let mut res = self.children.iter().flat_map(|c| c.errors()).collect::>(); + pub fn replace_child_range( + &mut self, + child_idx_range: Range, + replacement: Vec, + ) { + let old_len: usize = + self.children[child_idx_range.clone()].iter().map(Green::len).sum(); + let new_len: usize = replacement.iter().map(Green::len).sum(); - if self.kind().is_error() { - res.push(self.kind().clone()); - } + self.children.splice(child_idx_range, replacement); + self.erroneous = self.children.iter().any(|x| x.erroneous()); + self.data.set_len(self.data.len + new_len - old_len); + } - res + pub fn update_child_len(&mut self, new_len: usize, old_len: usize) { + self.data.len = self.data.len() + new_len - old_len; + self.erroneous = self.children.iter().any(|x| x.erroneous()); } /// Find the innermost child that is incremental safe. @@ -215,12 +210,7 @@ impl GreenNode { src: &str, replace: Span, replacement_len: usize, - ) -> bool { - let edit = &src[replace.inserted(replace, replacement_len).to_range()]; - if edit.contains("//") || edit.contains("/*") || edit.contains("*/") { - return false; - } - + ) -> Result, ()> { self.incremental_int(src, replace, replacement_len, 0, TokenMode::Markup, true) } @@ -232,10 +222,9 @@ impl GreenNode { mut offset: usize, parent_mode: TokenMode, outermost: bool, - ) -> bool { + ) -> Result, ()> { let kind = self.kind().clone(); let mode = kind.mode().contextualize(parent_mode); - eprintln!("in {:?} (mode {:?})", kind, mode); let mut loop_result = None; let mut child_at_start = true; @@ -243,13 +232,16 @@ impl GreenNode { let mut start = None; for (i, child) in self.children.iter_mut().enumerate() { let child_span = Span::new(replace.source, offset, offset + child.len()); - if child_span.surrounds(replace) { - eprintln!("found correct child"); - + if child_span.surrounds(replace) + && start.is_none() + && ((replace.start != child_span.end && replace.end != child_span.start) + || mode == TokenMode::Code + || i == last) + { let old_len = child.len(); // First, we try if the child has another, more specific applicable child. if !kind.incremental_safety().unsafe_interior() { - if child.incremental_int( + if let Ok(range) = child.incremental( src, replace, replacement_len, @@ -257,21 +249,17 @@ impl GreenNode { kind.mode().child_mode(), i == last && outermost, ) { - eprintln!("child success"); let new_len = child.len(); - self.data.set_len(self.data.len() + new_len - old_len); - self.erroneous = self.children.iter().any(|x| x.erroneous()); - return true; + self.update_child_len(new_len, old_len); + return Ok(range); } } // This didn't work, so we try to replace the child at this // level. let (function, policy) = - match child.kind().reparsing_function(kind.mode().child_mode()) { - Ok(p) => p, - _ => return false, - }; + child.kind().reparsing_function(kind.mode().child_mode()); + let function = function?; loop_result = Some(( i .. i + 1, child_span, @@ -280,23 +268,21 @@ impl GreenNode { policy, )); break; - } else if child_span.contains(replace.start) + } else if start.is_none() + && child_span.contains(replace.start) && mode == TokenMode::Markup && child.kind().incremental_safety().markup_safe() { - eprintln!("found safe start"); start = Some((i, offset)); } else if child_span.contains(replace.end) + && (replace.end != child_span.end || i == last) && mode == TokenMode::Markup && child.kind().incremental_safety().markup_safe() { - eprintln!("found safe end"); if let Some((start, start_offset)) = start { let (function, policy) = - match child.kind().reparsing_function(kind.mode().child_mode()) { - Ok(p) => p, - _ => return false, - }; + child.kind().reparsing_function(kind.mode().child_mode()); + let function = function?; loop_result = Some(( start .. i + 1, Span::new(replace.source, start_offset, offset + child.len()), @@ -310,7 +296,6 @@ impl GreenNode { && (mode != TokenMode::Markup || !child.kind().incremental_safety().markup_safe()) { - eprintln!("unsafe inbetweeen {:?}", child.kind()); break; } @@ -322,15 +307,7 @@ impl GreenNode { // We now have a child that we can replace and a function to do so if // the loop found any results at all. let (child_idx_range, child_span, child_outermost, func, policy) = - if let Some(loop_result) = loop_result { - loop_result - } else { - // No child fully contains the replacement. - eprintln!("no child match"); - return false; - }; - - eprintln!("aquired function, policy {:?}", policy); + loop_result.ok_or(())?; let src_span = child_span.inserted(replace, replacement_len); let recompile_range = if policy == IncrementalSafety::AtomicPrimary { @@ -339,123 +316,139 @@ impl GreenNode { src_span.to_range() }; - let new_children = if let Some(new_children) = - func(&src[recompile_range], child_at_start) - { - if policy != IncrementalSafety::AtomicPrimary - || new_children.iter().map(Green::len).sum::() == src_span.len() - { - new_children - } else { - eprintln!("wrong atomic len"); - return false; - } - } else { - eprintln!("function failed"); - return false; - }; - let child_mode = self.children[child_idx_range.start].kind().mode().child_mode(); - eprintln!("child mode {:?}", child_mode); + let (mut new_children, unterminated) = + func(&src[recompile_range], child_at_start).ok_or(())?; - // Check if the children / child has the right type. - let require_single = match policy { - IncrementalSafety::AtomicPrimary | IncrementalSafety::SameKind => true, - IncrementalSafety::SameKindInCode if child_mode == TokenMode::Code => true, - _ => false, - }; + let insertion = match check_invariants( + &new_children, + self.children(), + unterminated, + child_idx_range.clone(), + child_outermost, + child_at_start, + mode, + src_span, + policy, + ) { + InvariantResult::Ok => Ok(new_children), + InvariantResult::UseFirst => Ok(vec![std::mem::take(&mut new_children[0])]), + InvariantResult::Error => Err(()), + }?; - if require_single { - eprintln!("must be a single replacement"); - if new_children.len() != 1 { - eprintln!("not a single replacement"); - return false; - } + self.replace_child_range(child_idx_range, insertion); - if match policy { - IncrementalSafety::SameKind => true, - IncrementalSafety::SameKindInCode if child_mode == TokenMode::Code => { - true - } - _ => false, - } { - if self.children[child_idx_range.start].kind() != new_children[0].kind() { - eprintln!("not the same kind"); - return false; - } - } - } - - // Do not accept unclosed nodes if the old node did not use to be at the - // right edge of the tree. - if !child_outermost - && new_children - .iter() - .flat_map(|x| x.errors()) - .any(|x| matches!(x, NodeKind::Error(ErrorPos::End, _))) - { - eprintln!("unclosed node"); - return false; - } - - // Check if the neighbor invariants are still true. - if mode == TokenMode::Markup { - if child_idx_range.start > 0 { - if self.children[child_idx_range.start - 1].kind().incremental_safety() - == IncrementalSafety::EnsureRightWhitespace - && !new_children[0].kind().is_whitespace() - { - eprintln!("left whitespace missing"); - return false; - } - } - - let mut new_at_start = child_at_start; - for child in &new_children { - new_at_start = child.kind().is_at_start(new_at_start); - } - - for child in &self.children[child_idx_range.end ..] { - if child.kind().is_trivia() { - new_at_start = child.kind().is_at_start(new_at_start); - continue; - } - - match child.kind().incremental_safety() { - IncrementalSafety::EnsureAtStart if !new_at_start => { - return false; - } - IncrementalSafety::EnsureNotAtStart if new_at_start => { - return false; - } - _ => {} - } - break; - } - - if new_children.last().map(|x| x.kind().incremental_safety()) - == Some(IncrementalSafety::EnsureRightWhitespace) - && self.children.len() > child_idx_range.end - { - if !self.children[child_idx_range.end].kind().is_whitespace() { - eprintln!("right whitespace missing"); - return false; - } - } - } - - eprintln!("... replacing"); - - let old_len: usize = - self.children[child_idx_range.clone()].iter().map(Green::len).sum(); - let new_len: usize = new_children.iter().map(Green::len).sum(); - - self.children.splice(child_idx_range, new_children); - self.erroneous = self.children.iter().any(|x| x.erroneous()); - self.data.set_len(self.data.len + new_len - old_len); - true + Ok(src_span.to_range()) } } +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +enum InvariantResult { + Ok, + UseFirst, + Error, +} + +fn check_invariants( + use_children: &[Green], + old_children: &[Green], + unterminated: bool, + child_idx_range: Range, + outermost: bool, + child_at_start: bool, + mode: TokenMode, + src_span: Span, + policy: IncrementalSafety, +) -> InvariantResult { + let (new_children, ok) = if policy == IncrementalSafety::AtomicPrimary { + if use_children.iter().map(Green::len).sum::() == src_span.len() { + (use_children, InvariantResult::Ok) + } else if use_children[0].len() == src_span.len() { + (&use_children[0 .. 1], InvariantResult::UseFirst) + } else { + return InvariantResult::Error; + } + } else { + (use_children, InvariantResult::Ok) + }; + + let child_mode = old_children[child_idx_range.start].kind().mode().child_mode(); + + // Check if the children / child has the right type. + let require_single = match policy { + IncrementalSafety::AtomicPrimary | IncrementalSafety::SameKind => true, + IncrementalSafety::SameKindInCode if child_mode == TokenMode::Code => true, + _ => false, + }; + + if require_single { + if new_children.len() != 1 { + return InvariantResult::Error; + } + + if match policy { + IncrementalSafety::SameKind => true, + IncrementalSafety::SameKindInCode => child_mode == TokenMode::Code, + _ => false, + } { + if old_children[child_idx_range.start].kind() != new_children[0].kind() { + return InvariantResult::Error; + } + } + } + + // Do not accept unclosed nodes if the old node did not use to be at the + // right edge of the tree. + if !outermost && unterminated { + return InvariantResult::Error; + } + + // Check if the neighbor invariants are still true. + if mode == TokenMode::Markup { + if child_idx_range.start > 0 { + if old_children[child_idx_range.start - 1].kind().incremental_safety() + == IncrementalSafety::EnsureRightWhitespace + && !new_children[0].kind().is_whitespace() + { + return InvariantResult::Error; + } + } + + let mut new_at_start = child_at_start; + for child in new_children { + new_at_start = child.kind().is_at_start(new_at_start); + } + + for child in &old_children[child_idx_range.end ..] { + if child.kind().is_trivia() { + new_at_start = child.kind().is_at_start(new_at_start); + continue; + } + + match child.kind().incremental_safety() { + IncrementalSafety::EnsureAtStart if !new_at_start => { + return InvariantResult::Error; + } + IncrementalSafety::EnsureNotAtStart if new_at_start => { + return InvariantResult::Error; + } + _ => {} + } + break; + } + + if new_children.last().map(|x| x.kind().incremental_safety()) + == Some(IncrementalSafety::EnsureRightWhitespace) + && old_children.len() > child_idx_range.end + { + if !old_children[child_idx_range.end].kind().is_whitespace() { + return InvariantResult::Error; + } + } + } + + ok +} + impl From for Green { fn from(node: GreenNode) -> Self { Rc::new(node).into() @@ -1025,6 +1018,7 @@ impl NodeKind { match self { Self::Markup | Self::Space(_) + | Self::Linebreak | Self::Parbreak | Self::Text(_) | Self::TextInLine(_) @@ -1034,6 +1028,10 @@ impl NodeKind { | Self::Escape(_) | Self::Strong | Self::Emph + | Self::Heading + | Self::Enum + | Self::EnumNumbering(_) + | Self::List | Self::Raw(_) | Self::Math(_) => NodeMode::Markup, Self::Template @@ -1058,24 +1056,24 @@ impl NodeKind { pub fn reparsing_function( &self, parent_mode: TokenMode, - ) -> Result< - (fn(&str, bool) -> Option>, IncrementalSafety), + ) -> ( + Result Option<(Vec, bool)>, ()>, IncrementalSafety, - > { + ) { let policy = self.incremental_safety(); if policy.is_unsafe() { - return Err(policy); + return (Err(()), policy); } let contextualized = self.mode().contextualize(parent_mode); let is_code = contextualized == TokenMode::Code; if is_code && policy == IncrementalSafety::UnsafeLayer { - return Err(policy); + return (Err(()), policy); } if is_code && policy == IncrementalSafety::AtomicPrimary { - return Ok((parse_atomic, policy)); + return (Ok(parse_atomic), policy); } if policy == IncrementalSafety::SameKind @@ -1085,19 +1083,19 @@ impl NodeKind { NodeKind::Template => parse_template, NodeKind::Block => parse_block, NodeKind::LineComment | NodeKind::BlockComment => parse_comment, - _ => return Err(policy), + _ => return (Err(()), policy), }; - return Ok((parser, policy)); + return (Ok(parser), policy); } let parser: fn(&str, bool) -> _ = match contextualized { TokenMode::Markup if self == &Self::Markup => parse_markup, TokenMode::Markup => parse_markup_elements, - _ => return Err(policy), + _ => return (Err(()), policy), }; - Ok((parser, policy)) + (Ok(parser), policy) } /// Whether it is safe to do incremental parsing on this node. Never allow @@ -1434,6 +1432,8 @@ impl IncrementalSafety { Self::Safe | Self::SameKindInCode | Self::EnsureAtStart + | Self::EnsureNotAtStart + | Self::EnsureRightWhitespace | Self::UnsafeLayer => true, _ => false, } @@ -1458,8 +1458,7 @@ impl NodeMode { match self { Self::Markup => TokenMode::Markup, Self::Code => TokenMode::Code, - Self::Universal if old != TokenMode::Markup => TokenMode::Code, - Self::Universal => TokenMode::Markup, + Self::Universal => old, } }