This commit is contained in:
Folkert 2021-02-08 22:47:22 +01:00
parent 0ccf17007e
commit 8f83bb4518
6 changed files with 26 additions and 407 deletions

View file

@ -301,7 +301,9 @@ pub fn space0_e<'a, E>(
where
E: 'a,
{
spaces0_help(min_indent, space_problem, indent_problem)
spaces_help(false, min_indent, space_problem, indent_problem, |_, _| {
unreachable!("no spaces are required, so this is unreachable")
})
}
/// One or more (spaces/comments/newlines).
@ -422,253 +424,6 @@ fn spaces<'a>(
)
}
#[inline(always)]
fn spaces0_help<'a, E>(
min_indent: u16,
space_problem: fn(BadInputError, Row, Col) -> E,
indent_problem: fn(Row, Col) -> E,
) -> impl Parser<'a, &'a [CommentOrNewline<'a>], E>
where
E: 'a,
{
move |arena: &'a Bump, state: State<'a>| {
let original_state = state.clone();
let mut space_list = Vec::new_in(arena);
let mut bytes_parsed = 0;
let mut comment_line_buf = String::new_in(arena);
let mut line_state = LineState::Normal;
let mut state = state;
let mut any_newlines = false;
let start_bytes_len = state.bytes.len();
while !state.bytes.is_empty() {
match peek_utf8_char(&state) {
Ok((ch, utf8_len)) => {
bytes_parsed += utf8_len;
match line_state {
LineState::Normal => {
match ch {
' ' => {
// Don't check indentation here; it might not be enough
// indentation yet, but maybe it will be after more spaces happen!
state =
state.advance_spaces_e(arena, 1, space_problem.clone())?;
}
'\r' => {
// Ignore carriage returns.
state = state.advance_spaces_e(arena, 1, space_problem)?;
}
'\n' => {
// don't need to check the indent here since we'll reset it
// anyway
state = state.newline_e(arena, space_problem)?;
// Newlines only get added to the list when they're outside comments.
space_list.push(Newline);
any_newlines = true;
}
'#' => {
// Check indentation to make sure we were indented enough
// before this comment began.
let progress =
Progress::from_lengths(start_bytes_len, state.bytes.len());
state = state
.check_indent_e(arena, min_indent, indent_problem)
.map_err(|(fail, _)| {
(progress, fail, original_state.clone())
})?
.advance_without_indenting_e(arena, 1, space_problem)?;
// We're now parsing a line comment!
line_state = LineState::Comment;
}
_ => {
return {
// First make sure we were indented enough!
//
// (We only do this if we've encountered any newlines.
// Otherwise, we assume indentation is already correct.
// It's actively important for correctness that we skip
// this check if there are no newlines, because otherwise
// we would have false positives for single-line defs.)
let progress = Progress::from_lengths(
start_bytes_len,
state.bytes.len(),
);
if any_newlines {
state = state
.check_indent_e(arena, min_indent, indent_problem)
.map_err(|(fail, _)| {
(progress, fail, original_state.clone())
})?;
}
Ok((progress, space_list.into_bump_slice(), state))
};
}
}
}
LineState::Comment => {
match ch {
' ' => {
// If we're in a line comment, this won't affect indentation anyway.
state = state.advance_without_indenting_e(
arena,
1,
space_problem,
)?;
if comment_line_buf.len() == 1 {
match comment_line_buf.chars().next() {
Some('#') => {
// This is a comment begining with `## ` - that is,
// a doc comment.
//
// (The space is important; otherwise, this is not
// a doc comment, but rather something like a
// big separator block, e.g. ############)
line_state = LineState::DocComment;
// This is now the beginning of the doc comment.
comment_line_buf.clear();
}
_ => {
comment_line_buf.push(ch);
}
}
} else {
comment_line_buf.push(ch);
}
}
'\n' => {
state = state.newline_e(arena, space_problem)?;
match (comment_line_buf.len(), comment_line_buf.chars().next())
{
(1, Some('#')) => {
// This is a line with `##` - that is,
// a doc comment new line.
space_list.push(DocComment(""));
comment_line_buf = String::new_in(arena);
line_state = LineState::Normal;
}
_ => {
// This was a newline, so end this line comment.
space_list.push(LineComment(
comment_line_buf.into_bump_str(),
));
comment_line_buf = String::new_in(arena);
line_state = LineState::Normal;
}
}
}
nonblank => {
// Chars can have btye lengths of more than 1!
state = state.advance_without_indenting_e(
arena,
nonblank.len_utf8(),
space_problem,
)?;
comment_line_buf.push(nonblank);
}
}
}
LineState::DocComment => {
match ch {
' ' => {
// If we're in a doc comment, this won't affect indentation anyway.
state = state.advance_without_indenting_e(
arena,
1,
space_problem,
)?;
comment_line_buf.push(ch);
}
'\n' => {
state = state.newline_e(arena, space_problem)?;
// This was a newline, so end this doc comment.
space_list.push(DocComment(comment_line_buf.into_bump_str()));
comment_line_buf = String::new_in(arena);
line_state = LineState::Normal;
}
nonblank => {
state = state.advance_without_indenting_e(
arena,
utf8_len,
space_problem,
)?;
comment_line_buf.push(nonblank);
}
}
}
}
}
Err(SyntaxError::BadUtf8) => {
// If we hit an invalid UTF-8 character, bail out immediately.
let progress = Progress::from_lengths(start_bytes_len, state.bytes.len());
let row = state.line;
let col = state.column;
return state.fail(
arena,
progress,
space_problem(BadInputError::BadUtf8, row, col),
);
}
Err(_) => {
let space_slice = space_list.into_bump_slice();
// First make sure we were indented enough!
//
// (We only do this if we've encountered any newlines.
// Otherwise, we assume indentation is already correct.
// It's actively important for correctness that we skip
// this check if there are no newlines, because otherwise
// we would have false positives for single-line defs.)
let progress = Progress::from_lengths(start_bytes_len, state.bytes.len());
if any_newlines {
return Ok((
progress,
space_slice,
state
.check_indent_e(arena, min_indent, indent_problem)
.map_err(|(fail, _)| (progress, fail, original_state))?,
));
}
return Ok((progress, space_slice, state));
}
};
}
// First make sure we were indented enough!
//
// (We only do this if we've encountered any newlines.
// Otherwise, we assume indentation is already correct.
// It's actively important for correctness that we skip
// this check if there are no newlines, because otherwise
// we would have false positives for single-line defs.)
let progress = Progress::from_lengths(start_bytes_len, state.bytes.len());
if any_newlines {
state = state
.check_indent_e(arena, min_indent, indent_problem)
.map_err(|(fail, _)| (progress, fail, original_state))?;
}
Ok((progress, space_list.into_bump_slice(), state))
}
}
#[inline(always)]
fn spaces_help<'a, E>(
require_at_least_one: bool,