refactor: add Parser::directive_ident_list helper

This commit is contained in:
Erich Gubler 2024-10-18 17:19:19 -04:00
parent 7aa00a07e1
commit 54861b712c
2 changed files with 38 additions and 0 deletions

View File

@ -112,6 +112,8 @@ impl std::error::Error for ParseError {
pub enum ExpectedToken<'a> { pub enum ExpectedToken<'a> {
Token(Token<'a>), Token(Token<'a>),
Identifier, Identifier,
AfterIdentListComma,
AfterIdentListArg,
/// Expected: constant, parenthesized expression, identifier /// Expected: constant, parenthesized expression, identifier
PrimaryExpression, PrimaryExpression,
/// Expected: assignment, increment/decrement expression /// Expected: assignment, increment/decrement expression
@ -345,6 +347,12 @@ impl<'a> Error<'a> {
ExpectedToken::Type => "type".to_string(), ExpectedToken::Type => "type".to_string(),
ExpectedToken::Variable => "variable access".to_string(), ExpectedToken::Variable => "variable access".to_string(),
ExpectedToken::Function => "function name".to_string(), ExpectedToken::Function => "function name".to_string(),
ExpectedToken::AfterIdentListArg => {
"next argument, trailing comma, or end of list (',' or ';')".to_string()
}
ExpectedToken::AfterIdentListComma => {
"next argument or end of list (';')".to_string()
}
}; };
ParseError { ParseError {
message: format!( message: format!(

View File

@ -2258,6 +2258,36 @@ impl Parser {
Ok(fun) Ok(fun)
} }
#[allow(unused)]
fn directive_ident_list<'a>(
&self,
lexer: &mut Lexer<'a>,
handler: impl FnMut(&'a str, Span) -> Result<(), Error<'a>>,
) -> Result<(), Error<'a>> {
let mut handler = handler;
'next_arg: loop {
let (ident, span) = lexer.next_ident_with_span()?;
handler(ident, span)?;
let expected_token = match lexer.peek().0 {
Token::Separator(',') => {
let _ = lexer.next();
if matches!(lexer.peek().0, Token::Word(..)) {
continue 'next_arg;
}
ExpectedToken::AfterIdentListComma
}
_ => ExpectedToken::AfterIdentListArg,
};
if !matches!(lexer.next().0, Token::Separator(';')) {
return Err(Error::Unexpected(span, expected_token));
}
break Ok(());
}
}
fn global_decl<'a>( fn global_decl<'a>(
&mut self, &mut self,
lexer: &mut Lexer<'a>, lexer: &mut Lexer<'a>,