Skip to content

Commit

Permalink
Bug fix in indentation group parsing and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Feb 6, 2024
1 parent d6781e5 commit 8c05744
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/h10/src/indentation_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ impl Parser {
// Skip to `where`.
self.next();
// Start a new group right after `where`.
let group_idx = match self.next() {
let group_idx = match self.peek() {
Some(nested_group_start) if nested_group_start.indentation() > 0 => {
self.next();
let nested_group_idx = match reuse(&nested_group_start, arena) {
Some(reused_group_idx) => reused_group_idx,
None => {
Expand Down
56 changes: 56 additions & 0 deletions crates/h10/src/indentation_groups/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,62 @@ fn parse_8() {
token.check_token_str(pgm);
}

#[test]
fn parse_9() {
#[rustfmt::skip]
let lines = [
"",
"class X a where",
"x = 1",
""
];
let pgm = lines.join("\n");

let mut arena = DeclArena::new();
let token = lex_full(&pgm, Pos::ZERO);
let groups = parse_indentation_groups(token.clone(), &mut arena);
assert_eq!(groups.len(), 2);

let group0 = arena.get(groups[0]);
assert_eq!(group0.span_start(&arena).line, 0);
assert_eq!(group0.span_end(&arena).line, 2);
assert_eq!(group0.span_end(&arena).char, 0);

let group1 = arena.get(groups[1]);
assert_eq!(group1.span_start(&arena).line, 2);
assert_eq!(group1.span_start(&arena).char, 0);
assert_eq!(group1.span_end(&arena).line, 3);
assert_eq!(group1.span_end(&arena).char, 0);
}

#[test]
fn parse_10() {
#[rustfmt::skip]
let lines = [
"",
"class X a",
"x = 1",
""
];
let pgm = lines.join("\n");

let mut arena = DeclArena::new();
let token = lex_full(&pgm, Pos::ZERO);
let groups = parse_indentation_groups(token.clone(), &mut arena);
assert_eq!(groups.len(), 2);

let group0 = arena.get(groups[0]);
assert_eq!(group0.span_start(&arena).line, 0);
assert_eq!(group0.span_end(&arena).line, 2);
assert_eq!(group0.span_end(&arena).char, 0);

let group1 = arena.get(groups[1]);
assert_eq!(group1.span_start(&arena).line, 2);
assert_eq!(group1.span_start(&arena).char, 0);
assert_eq!(group1.span_end(&arena).line, 3);
assert_eq!(group1.span_end(&arena).char, 0);
}

#[test]
fn trivia_nodes() {
// Check that the initial trivia is assigned to the first AST node and trailing trivia are
Expand Down

0 comments on commit 8c05744

Please sign in to comment.