Skip to content

Commit

Permalink
Disallowing Identifier [
Browse files Browse the repository at this point in the history
This gives a better error message when this pattern is used on accident.
  • Loading branch information
ecton committed May 16, 2023
1 parent 6098749 commit 9b62025
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ impl<'s> Parser<'s> {
kind,
},
))
} else if matches!(
self.peek(),
Some(Token {
kind: TokenKind::Open(Balanced::Bracket),
..
})
) {
let location = self.peek().expect("just matched").location.clone();
return Err(Error::new(location, ErrorKind::ExpectedMapOrTuple));
} else {
Ok(Event::new(
token.location,
Expand Down Expand Up @@ -562,6 +571,7 @@ pub enum ErrorKind {
ExpectedColon,
ExpectedValue,
ExpectedCommaOrEnd(Nested),
ExpectedMapOrTuple,
TrailingData,
}

Expand All @@ -585,6 +595,9 @@ impl Display for ErrorKind {
ErrorKind::TrailingData => f.write_str(
"source contained extra trailing data after a value was completely read",
),
ErrorKind::ExpectedMapOrTuple => {
f.write_str("[ is not valid for a named value, expected { or (")
}
}
}
}
Expand Down Expand Up @@ -944,4 +957,13 @@ mod tests {
]
);
}

#[test]
fn array_named_error() {
let err = Parser::new("Foo[]", Config::default())
.next()
.unwrap()
.unwrap_err();
assert_eq!(err, Error::new(3..4, ErrorKind::ExpectedMapOrTuple));
}
}

0 comments on commit 9b62025

Please sign in to comment.