Parser - Detect duplicate argument names

This commit is contained in:
Emmanuel BENOîT 2023-01-02 21:49:16 +01:00
parent db781ed00f
commit 29d3677b2a

View file

@ -1,3 +1,5 @@
use std::collections::HashSet;
use crate::{ use crate::{
ast, ast,
errors::{ErrorHandler, ParserError}, errors::{ErrorHandler, ParserError},
@ -251,6 +253,7 @@ impl Parser {
let mut params = Vec::new(); let mut params = Vec::new();
if self.expect(&[TokenType::RightParen]).is_none() { if self.expect(&[TokenType::RightParen]).is_none() {
let mut names: HashSet<String> = HashSet::new();
loop { loop {
if params.len() >= kind.max_params() { if params.len() >= kind.max_params() {
return Err(ParserError::new( return Err(ParserError::new(
@ -262,7 +265,14 @@ impl Parser {
), ),
)); ));
} }
if let TokenType::Identifier(_) = self.peek().token_type { if let TokenType::Identifier(name) = &self.peek().token_type {
if names.contains(name) {
return Err(ParserError::new(
self.peek(),
&format!("duplicate {} parameter", kind.name()),
));
}
names.insert(name.to_owned());
params.push(self.advance().clone()); params.push(self.advance().clone());
} else { } else {
return Err(ParserError::new(self.peek(), "parameter name expected")); return Err(ParserError::new(self.peek(), "parameter name expected"));