Add Parser::forbid (#712)
This commit is contained in:
parent
bdf1c92251
commit
3643a0dff0
@ -230,6 +230,20 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return an error if the next token is of kind `forbidden`
|
||||||
|
fn forbid<F>(&self, forbidden: TokenKind, error: F) -> CompilationResult<'src, ()>
|
||||||
|
where
|
||||||
|
F: FnOnce(Token) -> CompilationError,
|
||||||
|
{
|
||||||
|
let next = self.next()?;
|
||||||
|
|
||||||
|
if next.kind == forbidden {
|
||||||
|
Err(error(next))
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Accept a token of kind `Identifier` and parse into a `Name`
|
/// Accept a token of kind `Identifier` and parse into a `Name`
|
||||||
fn accept_name(&mut self) -> CompilationResult<'src, Option<Name<'src>>> {
|
fn accept_name(&mut self) -> CompilationResult<'src, Option<Name<'src>>> {
|
||||||
if self.next_is(Identifier) {
|
if self.next_is(Identifier) {
|
||||||
@ -511,15 +525,11 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {
|
|||||||
let variadic = if kind.is_variadic() {
|
let variadic = if kind.is_variadic() {
|
||||||
let variadic = self.parse_parameter(kind)?;
|
let variadic = self.parse_parameter(kind)?;
|
||||||
|
|
||||||
let next = self.next()?;
|
self.forbid(Identifier, |token| {
|
||||||
|
token.error(CompilationErrorKind::ParameterFollowsVariadicParameter {
|
||||||
if next.kind == Identifier {
|
parameter: token.lexeme(),
|
||||||
return Err(
|
})
|
||||||
next.error(CompilationErrorKind::ParameterFollowsVariadicParameter {
|
})?;
|
||||||
parameter: next.lexeme(),
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(variadic)
|
Some(variadic)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user