///|
fn block_comment_at(text : String, pos : Int32) -> Bool {
text.get(pos) == Some('/') && text.get(pos + 1) == Some('*')
}
///|
fn block_comment_chain(
line : String,
from : Int32,
close : (Int32) -> Int32?,
memo : Map[Int32, Int32]?,
offset~ : Int32 = 0,
) -> Int32 {
let mut i = from
let pending = []
while block_comment_at(line, i) {
if memo is Some(table) {
if table.get(offset + i) is Some(end) {
i = end - offset
break
}
pending.push(offset + i)
}
guard close(i + 2) is Some(end) else {
i = line.length()
break
}
i = whitespace_end(line, end + 2)
}
if memo is Some(table) {
for start in pending {
table[start] = offset + i
}
}
i
}
///|
/// Skip ECMAScript whitespace and consecutive C-style block comments.
/// An incomplete comment extends to the end of the input.
pub fn block_trivia_end(line : String, from : Int32) -> Int32 {
let i = whitespace_end(line, from.clamp(min=0, max=line.length()))
if block_comment_at(line, i) {
block_comment_chain(
line,
i,
start => {
line
.unsafe_view(start~, end=line.length())
.find("*/")
.map(n => start + n)
},
None,
)
} else {
i
}
}
///|
/// Skip whitespace and C-style block comments inside this visible fragment.
/// Siblings ending at the same physical cutoff can reuse the continuation
/// at each opener. A new right edge invalidates the old results.
pub fn Line::block_trivia_end(self : Line, from : Int32) -> Int32 {
let text = self.text
let i = whitespace_end(text, from.clamp(min=0, max=text.length()))
guard block_comment_at(text, i) else { return i }
let limit = self.offset + text.length()
let memo = match self.columns.block_comments {
Some((end, table)) if end == limit => table
_ => {
let table = Map([])
self.columns.block_comments = Some((limit, table))
table
}
}
block_comment_chain(
text,
i,
start => self.find_literal("*/", start),
Some(memo),
offset=self.offset,
)
}