Add doc comment new line parsing

This commit is contained in:
Pablo Hirafuji 2020-11-06 13:57:41 -03:00
parent 00130c6dc0
commit 0c20713f94
2 changed files with 27 additions and 8 deletions

View file

@ -317,12 +317,27 @@ fn spaces<'a>(
'\n' => {
state = state.newline()?;
// This was a newline, so end this line comment.
space_list.push(LineComment(comment_line_buf.into_bump_str()));
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(nonblank.len_utf8())?;

View file

@ -2345,18 +2345,20 @@ mod test_parse {
let arena = Bump::new();
let newlines = &[Newline, Newline];
let def = Def::Body(
arena.alloc(Located::new(4, 4, 0, 1, Identifier("x"))),
arena.alloc(Located::new(4, 4, 4, 5, Num("5"))),
arena.alloc(Located::new(6, 6, 0, 1, Identifier("x"))),
arena.alloc(Located::new(6, 6, 4, 5, Num("5"))),
);
let loc_def = &*arena.alloc(Located::new(4, 4, 0, 1, def));
let loc_def = &*arena.alloc(Located::new(6, 6, 0, 1, def));
let defs = &[loc_def];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines);
let loc_ret = Located::new(6, 6, 0, 2, ret);
let loc_ret = Located::new(8, 8, 0, 2, ret);
let reset_indentation = &[
DocComment("first line of docs"),
DocComment(" second line"),
DocComment(" third line"),
DocComment("fourth line"),
DocComment(""),
DocComment("sixth line after doc new line"),
];
let expected = Expr::SpaceBefore(
arena.alloc(Defs(defs, arena.alloc(loc_ret))),
@ -2370,6 +2372,8 @@ mod test_parse {
## second line
## third line
## fourth line
##
## sixth line after doc new line
x = 5
42