forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.rs
More file actions
36 lines (32 loc) · 1.07 KB
/
function.rs
File metadata and controls
36 lines (32 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::ast;
use crate::error::{LexicalError, LexicalErrorType};
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![];
for (name, value) in func_args {
match name {
Some(n) => {
keywords.push(ast::Keyword { name: n, value });
}
None => {
// Allow starred args after keyword arguments.
if !keywords.is_empty() && !is_starred(&value) {
return Err(LexicalError {
error: LexicalErrorType::PositionalArgumentError,
location: value.location.clone(),
});
}
args.push(value);
}
}
}
Ok(ast::ArgumentList { args, keywords })
}
fn is_starred(exp: &ast::Expression) -> bool {
if let ast::ExpressionType::Starred { .. } = exp.node {
true
} else {
false
}
}