Skip to content

Commit

Permalink
Fix parsing comma separated lists
Browse files Browse the repository at this point in the history
List-like AST structures now require a comma (as intended) between
values, except if there's only one value. We also still allow trailing
commas. This ensures that code such as this now results in a parser
error:

    A { @A = 10 @b = 20 }

Instead, this should be written like so:

    A { @A = 10, @b = 20 }

This fixes #562.

Changelog: fixed
  • Loading branch information
yorickpeterse committed Jun 13, 2023
1 parent 97d195a commit 6c0c699
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion ast/src/parser.rs
Expand Up @@ -2865,7 +2865,9 @@ impl Parser {

values.push(func(self, token)?);

if self.peek().kind == TokenKind::Comma {
if values.len() >= 1 && self.peek().kind != close {
self.expect(TokenKind::Comma)?;
} else if self.peek().kind == TokenKind::Comma {
self.next();
}
}
Expand Down Expand Up @@ -4189,6 +4191,17 @@ mod tests {
location: cols(1, 1)
}))
);

assert_eq!(
expr("A\n{ @a = 10, }"),
Expression::Constant(Box::new(Constant {
source: None,
name: "A".to_string(),
location: cols(1, 1)
}))
);

assert_error_expr!("A { @a = 10 @b = 20 }", cols(13, 14));
}

#[test]
Expand Down

0 comments on commit 6c0c699

Please sign in to comment.