2022-03-07 21:08:56 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-09-02 20:21:37 +02:00
|
|
|
use super::Expression;
|
2021-10-11 23:17:45 +02:00
|
|
|
use crate::{DeclId, Span, Spanned};
|
2021-09-02 10:25:22 +02:00
|
|
|
|
2022-04-09 04:55:02 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum Argument {
|
|
|
|
Positional(Expression),
|
2022-04-09 07:17:48 +02:00
|
|
|
Named((Spanned<String>, Option<Spanned<String>>, Option<Expression>)),
|
2022-04-09 04:55:02 +02:00
|
|
|
}
|
|
|
|
|
2022-03-07 21:08:56 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2021-09-02 10:25:22 +02:00
|
|
|
pub struct Call {
|
|
|
|
/// identifier of the declaration to call
|
|
|
|
pub decl_id: DeclId,
|
|
|
|
pub head: Span,
|
2022-04-09 04:55:02 +02:00
|
|
|
pub arguments: Vec<Argument>,
|
2022-02-21 23:22:21 +01:00
|
|
|
pub redirect_stdout: bool,
|
|
|
|
pub redirect_stderr: bool,
|
2021-09-02 10:25:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Call {
|
2022-02-04 03:01:45 +01:00
|
|
|
pub fn new(head: Span) -> Call {
|
2021-09-02 10:25:22 +02:00
|
|
|
Self {
|
|
|
|
decl_id: 0,
|
2022-02-04 03:01:45 +01:00
|
|
|
head,
|
2022-04-09 04:55:02 +02:00
|
|
|
arguments: vec![],
|
2022-02-21 23:22:21 +01:00
|
|
|
redirect_stdout: true,
|
|
|
|
redirect_stderr: false,
|
2021-09-02 10:25:22 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-11 23:26:35 +02:00
|
|
|
|
2022-04-09 07:17:48 +02:00
|
|
|
pub fn named_iter(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = &(Spanned<String>, Option<Spanned<String>>, Option<Expression>)> {
|
2022-04-09 04:55:02 +02:00
|
|
|
self.arguments.iter().filter_map(|arg| match arg {
|
|
|
|
Argument::Named(named) => Some(named),
|
|
|
|
Argument::Positional(_) => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn named_iter_mut(
|
|
|
|
&mut self,
|
2022-04-09 07:17:48 +02:00
|
|
|
) -> impl Iterator<Item = &mut (Spanned<String>, Option<Spanned<String>>, Option<Expression>)>
|
|
|
|
{
|
2022-04-09 04:55:02 +02:00
|
|
|
self.arguments.iter_mut().filter_map(|arg| match arg {
|
|
|
|
Argument::Named(named) => Some(named),
|
|
|
|
Argument::Positional(_) => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn named_len(&self) -> usize {
|
|
|
|
self.named_iter().count()
|
|
|
|
}
|
|
|
|
|
2022-04-09 07:17:48 +02:00
|
|
|
pub fn add_named(
|
|
|
|
&mut self,
|
|
|
|
named: (Spanned<String>, Option<Spanned<String>>, Option<Expression>),
|
|
|
|
) {
|
2022-04-09 04:55:02 +02:00
|
|
|
self.arguments.push(Argument::Named(named));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_positional(&mut self, positional: Expression) {
|
|
|
|
self.arguments.push(Argument::Positional(positional));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn positional_iter(&self) -> impl Iterator<Item = &Expression> {
|
|
|
|
self.arguments.iter().filter_map(|arg| match arg {
|
|
|
|
Argument::Named(_) => None,
|
|
|
|
Argument::Positional(positional) => Some(positional),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn positional_iter_mut(&mut self) -> impl Iterator<Item = &mut Expression> {
|
|
|
|
self.arguments.iter_mut().filter_map(|arg| match arg {
|
|
|
|
Argument::Named(_) => None,
|
|
|
|
Argument::Positional(positional) => Some(positional),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn positional_nth(&self, i: usize) -> Option<&Expression> {
|
|
|
|
self.positional_iter().nth(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn positional_len(&self) -> usize {
|
|
|
|
self.positional_iter().count()
|
|
|
|
}
|
|
|
|
|
2021-09-11 23:26:35 +02:00
|
|
|
pub fn has_flag(&self, flag_name: &str) -> bool {
|
2022-04-09 04:55:02 +02:00
|
|
|
for name in self.named_iter() {
|
2021-10-11 23:17:45 +02:00
|
|
|
if flag_name == name.0.item {
|
2021-09-11 23:26:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
2021-10-01 23:53:13 +02:00
|
|
|
|
|
|
|
pub fn get_flag_expr(&self, flag_name: &str) -> Option<Expression> {
|
2022-04-09 04:55:02 +02:00
|
|
|
for name in self.named_iter() {
|
2021-10-11 23:17:45 +02:00
|
|
|
if flag_name == name.0.item {
|
2022-04-09 07:17:48 +02:00
|
|
|
return name.2.clone();
|
2021-10-01 23:53:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2021-10-02 04:59:11 +02:00
|
|
|
|
2021-11-29 19:21:55 +01:00
|
|
|
pub fn get_named_arg(&self, flag_name: &str) -> Option<Spanned<String>> {
|
2022-04-09 04:55:02 +02:00
|
|
|
for name in self.named_iter() {
|
2021-11-29 19:21:55 +01:00
|
|
|
if flag_name == name.0.item {
|
|
|
|
return Some(name.0.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-02-11 19:38:10 +01:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
let mut span = self.head;
|
|
|
|
|
2022-04-09 04:55:02 +02:00
|
|
|
for positional in self.positional_iter() {
|
2022-02-11 19:38:10 +01:00
|
|
|
if positional.span.end > span.end {
|
|
|
|
span.end = positional.span.end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 07:17:48 +02:00
|
|
|
for (named, _, val) in self.named_iter() {
|
2022-02-11 19:38:10 +01:00
|
|
|
if named.span.end > span.end {
|
|
|
|
span.end = named.span.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(val) = &val {
|
|
|
|
if val.span.end > span.end {
|
|
|
|
span.end = val.span.end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
span
|
|
|
|
}
|
2021-09-02 10:25:22 +02:00
|
|
|
}
|