1
Fork 0

refactor on span_look_ahead

This commit is contained in:
yukang 2023-08-06 22:28:14 +08:00
parent 026c4b6a65
commit eb0fcc5ad1
3 changed files with 28 additions and 33 deletions

View file

@ -973,24 +973,21 @@ impl SourceMap {
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)
}
/// Returns a new span to check next none-whitespace character or some specified expected character
/// If `expect` is none, the first span of non-whitespace character is returned.
/// If `expect` presented, the first span of the character `expect` is returned
/// Otherwise, the span reached to limit is returned.
pub fn span_look_ahead(&self, span: Span, expect: Option<&str>, limit: Option<usize>) -> Span {
/// Check whether span is followed by some specified expected string in limit scope
pub fn span_look_ahead(&self, span: Span, expect: &str, limit: Option<usize>) -> Option<Span> {
let mut sp = span;
for _ in 0..limit.unwrap_or(100_usize) {
sp = self.next_point(sp);
if let Ok(ref snippet) = self.span_to_snippet(sp) {
if expect.is_some_and(|es| snippet == es) {
break;
if snippet == expect {
return Some(sp);
}
if expect.is_none() && snippet.chars().any(|c| !c.is_whitespace()) {
if snippet.chars().any(|c| !c.is_whitespace()) {
break;
}
}
}
sp
None
}
/// Finds the width of the character, either before or after the end of provided span,