logic base

This commit is contained in:
Folkert 2022-05-15 00:00:32 +02:00
parent dc4f8289ad
commit 8f97c217a5
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C

View file

@ -277,10 +277,15 @@ fn eat_line_comment<'a>(
) -> SpaceState<'a> { ) -> SpaceState<'a> {
use SpaceState::*; use SpaceState::*;
let is_doc_comment = if let Some(b'#') = state.bytes().get(0) { let mut index = 0;
match state.bytes().get(1) { let bytes = state.bytes();
'outer: loop {
let is_doc_comment = if let Some(b'#') = bytes.get(index) {
match bytes.get(index + 1) {
Some(b' ') => { Some(b' ') => {
state = state.advance(2); state = state.advance(2);
index += 2;
true true
} }
@ -288,6 +293,7 @@ fn eat_line_comment<'a>(
// consume the second # and the \n // consume the second # and the \n
state = state.advance(1); state = state.advance(1);
state = state.advance_newline(); state = state.advance_newline();
index += 2;
comments_and_newlines.push(CommentOrNewline::DocComment("")); comments_and_newlines.push(CommentOrNewline::DocComment(""));
multiline = true; multiline = true;
@ -299,6 +305,7 @@ fn eat_line_comment<'a>(
} }
b'\n' => { b'\n' => {
state = state.advance_newline(); state = state.advance_newline();
index += 1;
multiline = true; multiline = true;
comments_and_newlines.push(CommentOrNewline::Newline); comments_and_newlines.push(CommentOrNewline::Newline);
} }
@ -310,10 +317,13 @@ fn eat_line_comment<'a>(
} }
b'#' => { b'#' => {
state = state.advance(1); state = state.advance(1);
return eat_line_comment(state, multiline, comments_and_newlines); index += 1;
continue 'outer;
} }
_ => break, _ => break,
} }
index += 1;
} }
return Good { return Good {
@ -339,6 +349,8 @@ fn eat_line_comment<'a>(
false false
}; };
let loop_start = index;
let initial = state.bytes(); let initial = state.bytes();
let mut it = initial.iter(); let mut it = initial.iter();
@ -357,6 +369,7 @@ fn eat_line_comment<'a>(
state = state.advance_newline(); state = state.advance_newline();
multiline = true; multiline = true;
index += 1;
while let Some(c) = it.next() { while let Some(c) = it.next() {
match c { match c {
b' ' => { b' ' => {
@ -375,10 +388,13 @@ fn eat_line_comment<'a>(
} }
b'#' => { b'#' => {
state = state.advance(1); state = state.advance(1);
return eat_line_comment(state, multiline, comments_and_newlines); index += 1;
continue 'outer;
} }
_ => break, _ => break,
} }
index += 1;
} }
return Good { return Good {
@ -394,11 +410,13 @@ fn eat_line_comment<'a>(
state = state.advance(1); state = state.advance(1);
} }
} }
index += 1;
} }
// We made it to the end of the bytes. This means there's a comment without a trailing newline. // We made it to the end of the bytes. This means there's a comment without a trailing newline.
let delta = initial.len() - state.bytes().len(); let delta = initial.len() - state.bytes().len();
let comment = unsafe { std::str::from_utf8_unchecked(&initial[..delta]) }; let comment = unsafe { std::str::from_utf8_unchecked(&bytes[loop_start..delta]) };
if is_doc_comment { if is_doc_comment {
comments_and_newlines.push(CommentOrNewline::DocComment(comment)); comments_and_newlines.push(CommentOrNewline::DocComment(comment));
@ -406,9 +424,10 @@ fn eat_line_comment<'a>(
comments_and_newlines.push(CommentOrNewline::LineComment(comment)); comments_and_newlines.push(CommentOrNewline::LineComment(comment));
} }
Good { return Good {
state, state,
multiline, multiline,
comments_and_newlines, comments_and_newlines,
};
} }
} }