Comments and neighbors

This commit is contained in:
Martin Haug 2021-11-10 20:41:10 +01:00
parent 91f2f97572
commit 3162c6a83a
6 changed files with 303 additions and 255 deletions

View File

@ -49,6 +49,11 @@ fn bench_parse(iai: &mut Iai) {
iai.run(|| parse(SRC)); 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) { fn bench_eval(iai: &mut Iai) {
let (mut ctx, id) = context(); let (mut ctx, id) = context();
iai.run(|| ctx.evaluate(id).unwrap()); iai.run(|| ctx.evaluate(id).unwrap());
@ -66,6 +71,7 @@ main!(
bench_scan, bench_scan,
bench_tokenize, bench_tokenize,
bench_parse, bench_parse,
bench_edit,
bench_eval, bench_eval,
bench_layout bench_layout
); );

View File

@ -26,14 +26,14 @@ pub fn parse(src: &str) -> Rc<GreenNode> {
} }
/// Parse an atomic primary. Returns `Some` if all of the input was consumed. /// Parse an atomic primary. Returns `Some` if all of the input was consumed.
pub fn parse_atomic(src: &str, _: bool) -> Option<Vec<Green>> { pub fn parse_atomic(src: &str, _: bool) -> Option<(Vec<Green>, bool)> {
let mut p = Parser::new(src, TokenMode::Code); let mut p = Parser::new(src, TokenMode::Code);
primary(&mut p, true).ok()?; primary(&mut p, true).ok()?;
p.eject_partial() p.eject_partial()
} }
/// Parse some markup. Returns `Some` if all of the input was consumed. /// Parse some markup. Returns `Some` if all of the input was consumed.
pub fn parse_markup(src: &str, _: bool) -> Option<Vec<Green>> { pub fn parse_markup(src: &str, _: bool) -> Option<(Vec<Green>, bool)> {
let mut p = Parser::new(src, TokenMode::Markup); let mut p = Parser::new(src, TokenMode::Markup);
markup(&mut p); markup(&mut p);
p.eject() p.eject()
@ -41,7 +41,10 @@ pub fn parse_markup(src: &str, _: bool) -> Option<Vec<Green>> {
/// Parse some markup without the topmost node. Returns `Some` if all of the /// Parse some markup without the topmost node. Returns `Some` if all of the
/// input was consumed. /// input was consumed.
pub fn parse_markup_elements(src: &str, mut at_start: bool) -> Option<Vec<Green>> { pub fn parse_markup_elements(
src: &str,
mut at_start: bool,
) -> Option<(Vec<Green>, bool)> {
let mut p = Parser::new(src, TokenMode::Markup); let mut p = Parser::new(src, TokenMode::Markup);
while !p.eof() { while !p.eof() {
markup_node(&mut p, &mut at_start); markup_node(&mut p, &mut at_start);
@ -50,7 +53,7 @@ pub fn parse_markup_elements(src: &str, mut at_start: bool) -> Option<Vec<Green>
} }
/// Parse a template literal. Returns `Some` if all of the input was consumed. /// Parse a template literal. Returns `Some` if all of the input was consumed.
pub fn parse_template(source: &str, _: bool) -> Option<Vec<Green>> { pub fn parse_template(source: &str, _: bool) -> Option<(Vec<Green>, bool)> {
let mut p = Parser::new(source, TokenMode::Code); let mut p = Parser::new(source, TokenMode::Code);
if !matches!(p.peek(), Some(NodeKind::LeftBracket)) { if !matches!(p.peek(), Some(NodeKind::LeftBracket)) {
return None; return None;
@ -61,7 +64,7 @@ pub fn parse_template(source: &str, _: bool) -> Option<Vec<Green>> {
} }
/// Parse a code block. Returns `Some` if all of the input was consumed. /// Parse a code block. Returns `Some` if all of the input was consumed.
pub fn parse_block(source: &str, _: bool) -> Option<Vec<Green>> { pub fn parse_block(source: &str, _: bool) -> Option<(Vec<Green>, bool)> {
let mut p = Parser::new(source, TokenMode::Code); let mut p = Parser::new(source, TokenMode::Code);
if !matches!(p.peek(), Some(NodeKind::LeftBrace)) { if !matches!(p.peek(), Some(NodeKind::LeftBrace)) {
return None; return None;
@ -72,7 +75,7 @@ pub fn parse_block(source: &str, _: bool) -> Option<Vec<Green>> {
} }
/// Parse a comment. Returns `Some` if all of the input was consumed. /// Parse a comment. Returns `Some` if all of the input was consumed.
pub fn parse_comment(source: &str, _: bool) -> Option<Vec<Green>> { pub fn parse_comment(source: &str, _: bool) -> Option<(Vec<Green>, bool)> {
let mut p = Parser::new(source, TokenMode::Code); let mut p = Parser::new(source, TokenMode::Code);
comment(&mut p).ok()?; comment(&mut p).ok()?;
p.eject() p.eject()

View File

@ -48,9 +48,9 @@ impl<'s> Parser<'s> {
} }
/// End the parsing process and return multiple children. /// End the parsing process and return multiple children.
pub fn eject(self) -> Option<Vec<Green>> { pub fn eject(self) -> Option<(Vec<Green>, bool)>{
if self.eof() && self.group_success() { if self.eof() && self.group_success() {
Some(self.children) Some((self.children, self.tokens.was_unterminated()))
} else { } else {
None None
} }
@ -97,8 +97,9 @@ impl<'s> Parser<'s> {
/// End the parsing process and return multiple children, even if there /// End the parsing process and return multiple children, even if there
/// remains stuff in the string. /// remains stuff in the string.
pub fn eject_partial(self) -> Option<Vec<Green>> { pub fn eject_partial(self) -> Option<(Vec<Green>, bool)> {
self.group_success().then(|| self.children) self.group_success()
.then(|| (self.children, self.tokens.was_unterminated()))
} }
/// Whether the end of the source string or group is reached. /// Whether the end of the source string or group is reached.

View File

@ -13,6 +13,7 @@ use crate::util::EcoString;
pub struct Tokens<'s> { pub struct Tokens<'s> {
s: Scanner<'s>, s: Scanner<'s>,
mode: TokenMode, mode: TokenMode,
has_unterminated: bool,
} }
/// What kind of tokens to emit. /// What kind of tokens to emit.
@ -28,7 +29,11 @@ impl<'s> Tokens<'s> {
/// Create a new token iterator with the given mode. /// Create a new token iterator with the given mode.
#[inline] #[inline]
pub fn new(src: &'s str, mode: TokenMode) -> Self { 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. /// Get the current token mode.
@ -63,6 +68,12 @@ impl<'s> Tokens<'s> {
pub fn scanner(&self) -> Scanner<'s> { pub fn scanner(&self) -> Scanner<'s> {
self.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> { impl<'s> Iterator for Tokens<'s> {
@ -248,6 +259,7 @@ impl<'s> Tokens<'s> {
) )
} }
} else { } else {
self.has_unterminated = true;
NodeKind::Error( NodeKind::Error(
ErrorPos::End, ErrorPos::End,
"expected closing brace".into(), "expected closing brace".into(),
@ -346,6 +358,7 @@ impl<'s> Tokens<'s> {
let remaining = backticks - found; let remaining = backticks - found;
let noun = if remaining == 1 { "backtick" } else { "backticks" }; let noun = if remaining == 1 { "backtick" } else { "backticks" };
self.has_unterminated = true;
NodeKind::Error( NodeKind::Error(
ErrorPos::End, ErrorPos::End,
if found == 0 { if found == 0 {
@ -393,6 +406,7 @@ impl<'s> Tokens<'s> {
display, display,
})) }))
} else { } else {
self.has_unterminated = true;
NodeKind::Error( NodeKind::Error(
ErrorPos::End, ErrorPos::End,
if !display || (!escaped && dollar) { if !display || (!escaped && dollar) {
@ -481,18 +495,23 @@ impl<'s> Tokens<'s> {
if self.s.eat_if('"') { if self.s.eat_if('"') {
NodeKind::Str(string) NodeKind::Str(string)
} else { } else {
self.has_unterminated = true;
NodeKind::Error(ErrorPos::End, "expected quote".into()) NodeKind::Error(ErrorPos::End, "expected quote".into())
} }
} }
fn line_comment(&mut self) -> NodeKind { fn line_comment(&mut self) -> NodeKind {
self.s.eat_until(is_newline); self.s.eat_until(is_newline);
if self.s.peek().is_none() {
self.has_unterminated = true;
}
NodeKind::LineComment NodeKind::LineComment
} }
fn block_comment(&mut self) -> NodeKind { fn block_comment(&mut self) -> NodeKind {
let mut state = '_'; let mut state = '_';
let mut depth = 1; let mut depth = 1;
let mut terminated = false;
// Find the first `*/` that does not correspond to a nested `/*`. // Find the first `*/` that does not correspond to a nested `/*`.
while let Some(c) = self.s.eat() { while let Some(c) = self.s.eat() {
@ -500,6 +519,7 @@ impl<'s> Tokens<'s> {
('*', '/') => { ('*', '/') => {
depth -= 1; depth -= 1;
if depth == 0 { if depth == 0 {
terminated = true;
break; break;
} }
'_' '_'
@ -512,6 +532,10 @@ impl<'s> Tokens<'s> {
} }
} }
if !terminated {
self.has_unterminated = true;
}
NodeKind::BlockComment NodeKind::BlockComment
} }

View File

@ -128,7 +128,6 @@ pub struct SourceFile {
src: String, src: String,
line_starts: Vec<usize>, line_starts: Vec<usize>,
root: Rc<GreenNode>, root: Rc<GreenNode>,
was_incremental: bool,
} }
impl SourceFile { impl SourceFile {
@ -142,7 +141,6 @@ impl SourceFile {
root: parse(&src), root: parse(&src),
src, src,
line_starts, line_starts,
was_incremental: false,
} }
} }
@ -268,7 +266,7 @@ impl SourceFile {
/// Edit the source file by replacing the given range. /// Edit the source file by replacing the given range.
/// ///
/// This panics if the `replace` range is out of bounds. /// This panics if the `replace` range is out of bounds.
pub fn edit(&mut self, replace: Range<usize>, with: &str) { pub fn edit(&mut self, replace: Range<usize>, with: &str) -> Range<usize> {
let start = replace.start; let start = replace.start;
self.src.replace_range(replace.clone(), with); self.src.replace_range(replace.clone(), with);
@ -287,11 +285,13 @@ impl SourceFile {
// Update the root node. // Update the root node.
let span = Span::new(self.id, replace.start, replace.end); let span = Span::new(self.id, replace.start, replace.end);
if Rc::make_mut(&mut self.root).incremental(&self.src, span, with.len()) { if let Ok(range) =
self.was_incremental = true; Rc::make_mut(&mut self.root).incremental(&self.src, span, with.len())
{
range
} else { } else {
self.root = parse(&self.src); self.root = parse(&self.src);
self.was_incremental = false; 0 .. self.src.len()
} }
} }
@ -485,93 +485,108 @@ mod tests {
#[test] #[test]
fn test_incremental_parse() { fn test_incremental_parse() {
#[track_caller] #[track_caller]
fn test(prev: &str, range: Range<usize>, with: &str, incr: bool) { fn test(prev: &str, range: Range<usize>, with: &str, incr: Range<usize>) {
let mut source = SourceFile::detached(prev); let mut source = SourceFile::detached(prev);
source.edit(range, with); let range = source.edit(range, with);
assert_eq!(range, incr);
if incr { let incr_tree = source.root.clone();
assert!(source.was_incremental); assert_eq!(parse(source.src()), incr_tree);
let incr_tree = source.root.clone();
assert_eq!(parse(source.src()), incr_tree);
} else {
assert!(!source.was_incremental);
}
} }
// Test simple replacements. // Test simple replacements.
test("hello world", 6 .. 11, "wankers", true); test("hello world", 6 .. 11, "wankers", 5 .. 13);
test("a d e", 1 .. 3, " b c d", true); test("a d e", 1 .. 3, " b c d", 0 .. 8);
test("a #f() e", 1 .. 6, " b c d", false); test("a #f() e", 1 .. 6, " b c d", 0 .. 8);
test("{(0, 1, 2)}", 5 .. 6, "11pt", true); test("{(0, 1, 2)}", 5 .. 6, "11pt", 5 .. 9);
test("= A heading", 3 .. 3, "n evocative", true); 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( test(
"#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])", "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])",
16 .. 20, 16 .. 20,
"none", "none",
true, 16 .. 20,
); );
test( test(
"#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])", "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])",
33 .. 42, 33 .. 42,
"[_gronk_]", "[_gronk_]",
true, 33 .. 42,
); );
test( test(
"#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])", "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])",
34 .. 41, 34 .. 41,
"_bar_", "_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}}", 6 .. 6, " ", 1 .. 9);
test("{let i=1; for x in range(5) {i}}", 13 .. 14, " ", true); test("{let i=1; for x in range(5) {i}}", 13 .. 14, " ", 13 .. 15);
test("hello {x}", 6 .. 9, "#f()", false); test("hello {x}", 6 .. 9, "#f()", 5 .. 10);
test( test(
"this is -- in my opinion -- spectacular", "this is -- in my opinion -- spectacular",
8 .. 10, 8 .. 10,
"---", "---",
true, 7 .. 12,
); );
test("understanding `code` is complicated", 15 .. 15, "C ", true);
test("{ let x = g() }", 10 .. 12, "f(54", true);
test( test(
"#let rect with (fill: eastern)", "understanding `code` is complicated",
14 .. 29, 15 .. 15,
" (stroke: conifer", "C ",
true, 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 the whitespace invariants.
test("hello \\ world", 7 .. 8, "a ", false); test("hello \\ world", 7 .. 8, "a ", 6 .. 14);
test("hello \\ world", 7 .. 8, "\n\n", true); test("hello \\ world", 7 .. 8, " a", 6 .. 14);
test("x = y", 2 .. 2, "+ y ", true); test("x = y", 1 .. 1, " + y", 0 .. 6);
test("x = y", 2 .. 2, "+ y \n ", false); test("x = y", 1 .. 1, " + y\n", 0 .. 10);
test("abc\n= a heading", 3 .. 4, "\nsome more test\n\n", true); test("abc\n= a heading\njoke", 3 .. 4, "\nmore\n\n", 0 .. 21);
test("abc\n= a heading", 3 .. 4, "\nnot ", false); test("abc\n= a heading\njoke", 3 .. 4, "\nnot ", 0 .. 19);
test("hey #myfriend", 4 .. 4, "\\", false); test("hey #myfriend", 4 .. 4, "\\", 0 .. 14);
test("hey #myfriend", 4 .. 4, "\\", true); test("hey #myfriend", 4 .. 4, "\\", 3 .. 6);
// Test type invariants. // Test type invariants.
test("#for x in array {x}", 16 .. 19, "[#x]", true); test("a #for x in array {x}", 18 .. 21, "[#x]", 2 .. 22);
test("#let x = 1 {5}", 1 .. 4, "if", false); test("a #let x = 1 {5}", 3 .. 6, "if", 0 .. 15);
test("{let x = 1 {5}}", 1 .. 4, "if", true); test("a {let x = 1 {5}} b", 3 .. 6, "if", 2 .. 16);
test("#let x = 1 {5}", 4 .. 4, " if", false); test("#let x = 1 {5}", 4 .. 4, " if", 0 .. 17);
test("{let x = 1 {5}}", 4 .. 4, " if", true); test("{let x = 1 {5}}", 4 .. 4, " if", 0 .. 18);
test("a // b c #f()", 3 .. 4, "", false); test("a // b c #f()", 3 .. 4, "", 0 .. 12);
test("{\nf()\n//g(a)\n}", 6 .. 8, "", true); test("{\nf()\n//g(a)\n}", 6 .. 8, "", 0 .. 12);
test("{(1, 2)}", 1 .. 1, "while ", true); 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);
// this appearantly works but the assertion fails. test("{(1, 2)}", 1 .. 1, "while ", 0 .. 14);
test("a b c", 1 .. 1, "{[}", true); test("a b c", 1 .. 1, "{[}", 0 .. 5);
// Test unclosed things. // Test unclosed things.
test(r#"{"hi"}"#, 4 .. 5, "c", false); test(r#"{"hi"}"#, 4 .. 5, "c", 0 .. 6);
test(r"this \u{abcd}", 8 .. 9, "", true); test(r"this \u{abcd}", 8 .. 9, "", 5 .. 12);
test(r"this \u{abcd} that", 12 .. 13, "", false); test(r"this \u{abcd} that", 12 .. 13, "", 0 .. 17);
test(r"{{let x = z}; a = 1} b", 6 .. 6, "//", false); 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, "", 0 .. 20);
test(r#"a ```typst hello``` b"#, 16 .. 17, "", false); test(r#"a ```typst hello```"#, 16 .. 17, "", 2 .. 18);
test(r#"a ```typst hello```"#, 16 .. 17, "", true);
} }
} }

View File

@ -6,6 +6,7 @@ mod pretty;
mod span; mod span;
use std::fmt::{self, Debug, Display, Formatter}; use std::fmt::{self, Debug, Display, Formatter};
use std::ops::Range;
use std::rc::Rc; use std::rc::Rc;
pub use highlight::*; pub use highlight::*;
@ -88,7 +89,7 @@ impl Green {
} }
/// Find the innermost child that is incremental safe. /// Find the innermost child that is incremental safe.
fn incremental_int( fn incremental(
&mut self, &mut self,
edit: &str, edit: &str,
replace: Span, replace: Span,
@ -96,7 +97,7 @@ impl Green {
offset: usize, offset: usize,
parent_mode: TokenMode, parent_mode: TokenMode,
outermost: bool, outermost: bool,
) -> bool { ) -> Result<Range<usize>, ()> {
match self { match self {
Green::Node(n) => Rc::make_mut(n).incremental_int( Green::Node(n) => Rc::make_mut(n).incremental_int(
edit, edit,
@ -106,21 +107,7 @@ impl Green {
parent_mode, parent_mode,
outermost, outermost,
), ),
Green::Token(_) => false, Green::Token(_) => Err(()),
}
}
/// The error messages for this node and its descendants.
pub fn errors(&self) -> Vec<NodeKind> {
match self {
Green::Node(n) => n.errors(),
Green::Token(t) => {
if t.kind().is_error() {
vec![t.kind().clone()]
} else {
vec![]
}
}
} }
} }
} }
@ -198,15 +185,23 @@ impl GreenNode {
self.data().len() self.data().len()
} }
/// The error messages for this node and its descendants. pub fn replace_child_range(
pub fn errors(&self) -> Vec<NodeKind> { &mut self,
let mut res = self.children.iter().flat_map(|c| c.errors()).collect::<Vec<_>>(); child_idx_range: Range<usize>,
replacement: Vec<Green>,
) {
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() { self.children.splice(child_idx_range, replacement);
res.push(self.kind().clone()); 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. /// Find the innermost child that is incremental safe.
@ -215,12 +210,7 @@ impl GreenNode {
src: &str, src: &str,
replace: Span, replace: Span,
replacement_len: usize, replacement_len: usize,
) -> bool { ) -> Result<Range<usize>, ()> {
let edit = &src[replace.inserted(replace, replacement_len).to_range()];
if edit.contains("//") || edit.contains("/*") || edit.contains("*/") {
return false;
}
self.incremental_int(src, replace, replacement_len, 0, TokenMode::Markup, true) self.incremental_int(src, replace, replacement_len, 0, TokenMode::Markup, true)
} }
@ -232,10 +222,9 @@ impl GreenNode {
mut offset: usize, mut offset: usize,
parent_mode: TokenMode, parent_mode: TokenMode,
outermost: bool, outermost: bool,
) -> bool { ) -> Result<Range<usize>, ()> {
let kind = self.kind().clone(); let kind = self.kind().clone();
let mode = kind.mode().contextualize(parent_mode); let mode = kind.mode().contextualize(parent_mode);
eprintln!("in {:?} (mode {:?})", kind, mode);
let mut loop_result = None; let mut loop_result = None;
let mut child_at_start = true; let mut child_at_start = true;
@ -243,13 +232,16 @@ impl GreenNode {
let mut start = None; let mut start = None;
for (i, child) in self.children.iter_mut().enumerate() { for (i, child) in self.children.iter_mut().enumerate() {
let child_span = Span::new(replace.source, offset, offset + child.len()); let child_span = Span::new(replace.source, offset, offset + child.len());
if child_span.surrounds(replace) { if child_span.surrounds(replace)
eprintln!("found correct child"); && start.is_none()
&& ((replace.start != child_span.end && replace.end != child_span.start)
|| mode == TokenMode::Code
|| i == last)
{
let old_len = child.len(); let old_len = child.len();
// First, we try if the child has another, more specific applicable child. // First, we try if the child has another, more specific applicable child.
if !kind.incremental_safety().unsafe_interior() { if !kind.incremental_safety().unsafe_interior() {
if child.incremental_int( if let Ok(range) = child.incremental(
src, src,
replace, replace,
replacement_len, replacement_len,
@ -257,21 +249,17 @@ impl GreenNode {
kind.mode().child_mode(), kind.mode().child_mode(),
i == last && outermost, i == last && outermost,
) { ) {
eprintln!("child success");
let new_len = child.len(); let new_len = child.len();
self.data.set_len(self.data.len() + new_len - old_len); self.update_child_len(new_len, old_len);
self.erroneous = self.children.iter().any(|x| x.erroneous()); return Ok(range);
return true;
} }
} }
// This didn't work, so we try to replace the child at this // This didn't work, so we try to replace the child at this
// level. // level.
let (function, policy) = let (function, policy) =
match child.kind().reparsing_function(kind.mode().child_mode()) { child.kind().reparsing_function(kind.mode().child_mode());
Ok(p) => p, let function = function?;
_ => return false,
};
loop_result = Some(( loop_result = Some((
i .. i + 1, i .. i + 1,
child_span, child_span,
@ -280,23 +268,21 @@ impl GreenNode {
policy, policy,
)); ));
break; break;
} else if child_span.contains(replace.start) } else if start.is_none()
&& child_span.contains(replace.start)
&& mode == TokenMode::Markup && mode == TokenMode::Markup
&& child.kind().incremental_safety().markup_safe() && child.kind().incremental_safety().markup_safe()
{ {
eprintln!("found safe start");
start = Some((i, offset)); start = Some((i, offset));
} else if child_span.contains(replace.end) } else if child_span.contains(replace.end)
&& (replace.end != child_span.end || i == last)
&& mode == TokenMode::Markup && mode == TokenMode::Markup
&& child.kind().incremental_safety().markup_safe() && child.kind().incremental_safety().markup_safe()
{ {
eprintln!("found safe end");
if let Some((start, start_offset)) = start { if let Some((start, start_offset)) = start {
let (function, policy) = let (function, policy) =
match child.kind().reparsing_function(kind.mode().child_mode()) { child.kind().reparsing_function(kind.mode().child_mode());
Ok(p) => p, let function = function?;
_ => return false,
};
loop_result = Some(( loop_result = Some((
start .. i + 1, start .. i + 1,
Span::new(replace.source, start_offset, offset + child.len()), Span::new(replace.source, start_offset, offset + child.len()),
@ -310,7 +296,6 @@ impl GreenNode {
&& (mode != TokenMode::Markup && (mode != TokenMode::Markup
|| !child.kind().incremental_safety().markup_safe()) || !child.kind().incremental_safety().markup_safe())
{ {
eprintln!("unsafe inbetweeen {:?}", child.kind());
break; break;
} }
@ -322,15 +307,7 @@ impl GreenNode {
// We now have a child that we can replace and a function to do so if // We now have a child that we can replace and a function to do so if
// the loop found any results at all. // the loop found any results at all.
let (child_idx_range, child_span, child_outermost, func, policy) = let (child_idx_range, child_span, child_outermost, func, policy) =
if let Some(loop_result) = loop_result { loop_result.ok_or(())?;
loop_result
} else {
// No child fully contains the replacement.
eprintln!("no child match");
return false;
};
eprintln!("aquired function, policy {:?}", policy);
let src_span = child_span.inserted(replace, replacement_len); let src_span = child_span.inserted(replace, replacement_len);
let recompile_range = if policy == IncrementalSafety::AtomicPrimary { let recompile_range = if policy == IncrementalSafety::AtomicPrimary {
@ -339,123 +316,139 @@ impl GreenNode {
src_span.to_range() src_span.to_range()
}; };
let new_children = if let Some(new_children) = let (mut new_children, unterminated) =
func(&src[recompile_range], child_at_start) func(&src[recompile_range], child_at_start).ok_or(())?;
{
if policy != IncrementalSafety::AtomicPrimary
|| new_children.iter().map(Green::len).sum::<usize>() == 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);
// Check if the children / child has the right type. let insertion = match check_invariants(
let require_single = match policy { &new_children,
IncrementalSafety::AtomicPrimary | IncrementalSafety::SameKind => true, self.children(),
IncrementalSafety::SameKindInCode if child_mode == TokenMode::Code => true, unterminated,
_ => false, 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 { self.replace_child_range(child_idx_range, insertion);
eprintln!("must be a single replacement");
if new_children.len() != 1 {
eprintln!("not a single replacement");
return false;
}
if match policy { Ok(src_span.to_range())
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
} }
} }
#[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<usize>,
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::<usize>() == 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<GreenNode> for Green { impl From<GreenNode> for Green {
fn from(node: GreenNode) -> Self { fn from(node: GreenNode) -> Self {
Rc::new(node).into() Rc::new(node).into()
@ -1025,6 +1018,7 @@ impl NodeKind {
match self { match self {
Self::Markup Self::Markup
| Self::Space(_) | Self::Space(_)
| Self::Linebreak
| Self::Parbreak | Self::Parbreak
| Self::Text(_) | Self::Text(_)
| Self::TextInLine(_) | Self::TextInLine(_)
@ -1034,6 +1028,10 @@ impl NodeKind {
| Self::Escape(_) | Self::Escape(_)
| Self::Strong | Self::Strong
| Self::Emph | Self::Emph
| Self::Heading
| Self::Enum
| Self::EnumNumbering(_)
| Self::List
| Self::Raw(_) | Self::Raw(_)
| Self::Math(_) => NodeMode::Markup, | Self::Math(_) => NodeMode::Markup,
Self::Template Self::Template
@ -1058,24 +1056,24 @@ impl NodeKind {
pub fn reparsing_function( pub fn reparsing_function(
&self, &self,
parent_mode: TokenMode, parent_mode: TokenMode,
) -> Result< ) -> (
(fn(&str, bool) -> Option<Vec<Green>>, IncrementalSafety), Result<fn(&str, bool) -> Option<(Vec<Green>, bool)>, ()>,
IncrementalSafety, IncrementalSafety,
> { ) {
let policy = self.incremental_safety(); let policy = self.incremental_safety();
if policy.is_unsafe() { if policy.is_unsafe() {
return Err(policy); return (Err(()), policy);
} }
let contextualized = self.mode().contextualize(parent_mode); let contextualized = self.mode().contextualize(parent_mode);
let is_code = contextualized == TokenMode::Code; let is_code = contextualized == TokenMode::Code;
if is_code && policy == IncrementalSafety::UnsafeLayer { if is_code && policy == IncrementalSafety::UnsafeLayer {
return Err(policy); return (Err(()), policy);
} }
if is_code && policy == IncrementalSafety::AtomicPrimary { if is_code && policy == IncrementalSafety::AtomicPrimary {
return Ok((parse_atomic, policy)); return (Ok(parse_atomic), policy);
} }
if policy == IncrementalSafety::SameKind if policy == IncrementalSafety::SameKind
@ -1085,19 +1083,19 @@ impl NodeKind {
NodeKind::Template => parse_template, NodeKind::Template => parse_template,
NodeKind::Block => parse_block, NodeKind::Block => parse_block,
NodeKind::LineComment | NodeKind::BlockComment => parse_comment, 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 { let parser: fn(&str, bool) -> _ = match contextualized {
TokenMode::Markup if self == &Self::Markup => parse_markup, TokenMode::Markup if self == &Self::Markup => parse_markup,
TokenMode::Markup => parse_markup_elements, 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 /// Whether it is safe to do incremental parsing on this node. Never allow
@ -1434,6 +1432,8 @@ impl IncrementalSafety {
Self::Safe Self::Safe
| Self::SameKindInCode | Self::SameKindInCode
| Self::EnsureAtStart | Self::EnsureAtStart
| Self::EnsureNotAtStart
| Self::EnsureRightWhitespace
| Self::UnsafeLayer => true, | Self::UnsafeLayer => true,
_ => false, _ => false,
} }
@ -1458,8 +1458,7 @@ impl NodeMode {
match self { match self {
Self::Markup => TokenMode::Markup, Self::Markup => TokenMode::Markup,
Self::Code => TokenMode::Code, Self::Code => TokenMode::Code,
Self::Universal if old != TokenMode::Markup => TokenMode::Code, Self::Universal => old,
Self::Universal => TokenMode::Markup,
} }
} }