Merge pull request #1407 from vazrupe/dup-kargs-error

Add duplicate keyword argument error
This commit is contained in:
Windel Bouwman
2019-09-27 10:06:35 +02:00
committed by GitHub
3 changed files with 26 additions and 0 deletions

View File

@@ -21,6 +21,7 @@ pub enum LexicalErrorType {
UnicodeError,
NestingError,
PositionalArgumentError,
DuplicateKeywordArgumentError,
UnrecognizedToken { tok: char },
FStringError(FStringErrorType),
OtherError(String),
@@ -33,6 +34,9 @@ impl fmt::Display for LexicalErrorType {
LexicalErrorType::FStringError(error) => write!(f, "Got error in f-string: {}", error),
LexicalErrorType::UnicodeError => write!(f, "Got unexpected unicode"),
LexicalErrorType::NestingError => write!(f, "Got unexpected nesting"),
LexicalErrorType::DuplicateKeywordArgumentError => {
write!(f, "keyword argument repeated")
}
LexicalErrorType::PositionalArgumentError => {
write!(f, "positional argument follows keyword argument")
}

View File

@@ -1,3 +1,5 @@
use std::collections::HashSet;
use crate::ast;
use crate::error::{LexicalError, LexicalErrorType};
@@ -6,9 +8,22 @@ type FunctionArgument = (Option<Option<String>>, ast::Expression);
pub fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ast::ArgumentList, LexicalError> {
let mut args = vec![];
let mut keywords = vec![];
let mut keyword_names = HashSet::with_capacity(func_args.len());
for (name, value) in func_args {
match name {
Some(n) => {
if let Some(keyword_name) = n.clone() {
if keyword_names.contains(&keyword_name) {
return Err(LexicalError {
error: LexicalErrorType::DuplicateKeywordArgumentError,
location: value.location.clone(),
});
}
keyword_names.insert(keyword_name.clone());
}
keywords.push(ast::Keyword { name: n, value });
}
None => {

View File

@@ -95,3 +95,10 @@ assert kwargs == [('a', 1), ('b', 2)]
kwargs = func(a=1, b=2, c=3)
assert kwargs == [('a', 1), ('b', 2), ('c', 3)]
def inc(n):
return n + 1
with assert_raises(SyntaxError):
exec("inc(n=1, n=2)")