Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add ability to have trailing commas #32

Merged
merged 2 commits into from
Feb 20, 2021
Merged

feat(config): add ability to have trailing commas #32

merged 2 commits into from
Feb 20, 2021

Conversation

virchau13
Copy link
Member

@virchau13 virchau13 commented Feb 19, 2021

This closes #23.

Comment on lines 29 to 34
if iter.peek().map(|x| x.toktype == ty).unwrap_or(false) {
iter.next();
true
} else {
false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A match statement would probably be more readable:

Suggested change
if iter.peek().map(|x| x.toktype == ty).unwrap_or(false) {
iter.next();
true
} else {
false
}
match iter.peek().map(|x| x.toktype == ty) {
Some(true) => {
iter.next();
true
}
_ => false,
}

@@ -182,48 +171,60 @@ impl VariantExpr {
.unwrap_or(false)
{
return Err(ParseError::from(ParseErrorType::Custom(
"Must have at least one option",
"Variant expression have at least one option",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably read "- expression must have -".

}
}
expect(iter, &[TokType::RBracket])?;
Ok(VariantExpr { specs })
expect(iter, &[TokType::LBrace])?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to call expect(iter, &[TokType::LBrace]) as the top of the parse function (first line).

impl<T: SimpleParse> CommaList<T> {
pub fn parse<I: Iterator<Item = Token>>(
iter: &mut Peekable<I>,
end: TokType,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify what end is and if applicable, what TokType it should be.

loop {
// Allow trailing commas
if eat!(iter, RBrace) {
// Allow trailing comma
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain how this permits a trailing comma.

There is also a repetitive if statement in this loop:

            if eat(iter, end) {
                break;
            }

This should be refactored if possible.

Comment on lines +464 to +507
#[test]
fn variant_trailing_comma() {
success(
&toklist![
TokType::LBracket,
"a",
TokType::Comma,
TokType::RBracket,
TokType::Semicolon
],
&[Entry {
left: Spec {
string: None,
spectype: SpecType::variant_expr(vec![Spec::from("a")], None),
},
right: None,
}],
)
}

#[test]
fn match_trailing_comma() {
success(
&toklist![
TokType::LBrace,
"linux",
TokType::Colon,
"a",
TokType::Comma,
TokType::RBrace,
TokType::Semicolon
],
&[Entry {
left: Spec {
string: None,
spectype: SpecType::match_expr(
vec![(ExprType::Linux.into(), Spec::from("a"))],
None,
),
},
right: None,
}],
)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment to each of these test functions explaining that trailing commas are valid syntax and should be successfully parsed.

@claby2 claby2 merged commit ceb6f89 into plamorg:master Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to have extra trailing comma at the end of variant expressions
2 participants