Skip to main content

nu_protocol/engine/
argument.rs

1use std::sync::Arc;
2
3use crate::{Span, Value, ast::Expression, ir::DataSlice};
4
5/// Represents a fully evaluated argument to a call.
6#[derive(Debug, Clone)]
7pub enum Argument {
8    /// A positional argument
9    Positional { span: Span, val: Value },
10    /// A spread argument, e.g. `...$args`
11    Spread { span: Span, vals: Value },
12    /// A named argument with no value, e.g. `--flag`
13    Flag {
14        data: Arc<[u8]>,
15        name: DataSlice,
16        short: DataSlice,
17        span: Span,
18    },
19    /// A named argument with a value, e.g. `--flag value` or `--flag=`
20    Named {
21        data: Arc<[u8]>,
22        name: DataSlice,
23        short: DataSlice,
24        span: Span,
25        val: Value,
26    },
27    /// Information generated by the parser for use by certain keyword commands
28    ParserInfo {
29        data: Arc<[u8]>,
30        name: DataSlice,
31        // TODO: rather than `Expression`, this would probably be best served by a specific enum
32        // type for this purpose.
33        info: Box<Expression>,
34    },
35}
36
37impl Argument {
38    /// The span encompassing the argument's usage within the call, distinct from the span of the
39    /// actual value of the argument.
40    pub fn span(&self) -> Option<Span> {
41        match self {
42            Argument::Positional { span, .. } => Some(*span),
43            Argument::Spread { span, .. } => Some(*span),
44            Argument::Flag { span, .. } => Some(*span),
45            Argument::Named { span, .. } => Some(*span),
46            // Because `ParserInfo` is generated, its span shouldn't be used
47            Argument::ParserInfo { .. } => None,
48        }
49    }
50}
51
52/// Stores the argument context for calls in IR evaluation.
53#[derive(Debug, Clone, Default)]
54pub struct ArgumentStack {
55    arguments: Vec<Argument>,
56}
57
58impl ArgumentStack {
59    /// Create a new, empty argument stack.
60    pub const fn new() -> Self {
61        ArgumentStack { arguments: vec![] }
62    }
63
64    /// Returns the index of the end of the argument stack. Call and save this before adding
65    /// arguments.
66    pub fn get_base(&self) -> usize {
67        self.arguments.len()
68    }
69
70    /// Calculates the number of arguments past the given [previously retrieved](.get_base) base
71    /// pointer.
72    pub fn get_len(&self, base: usize) -> usize {
73        self.arguments.len().checked_sub(base).unwrap_or_else(|| {
74            panic!(
75                "base ({}) is beyond the end of the arguments stack ({})",
76                base,
77                self.arguments.len()
78            );
79        })
80    }
81
82    /// Push an argument onto the end of the argument stack.
83    pub fn push(&mut self, argument: Argument) {
84        self.arguments.push(argument);
85    }
86
87    /// Clear all of the arguments after the given base index, to prepare for the next frame.
88    pub fn leave_frame(&mut self, base: usize) {
89        self.arguments.truncate(base);
90    }
91
92    /// Get arguments for the frame based on the given [`base`](`.get_base()`) and
93    /// [`len`](`.get_len()`) parameters.
94    pub fn get_args(&self, base: usize, len: usize) -> &[Argument] {
95        &self.arguments[base..(base + len)]
96    }
97
98    /// Move arguments for the frame based on the given [`base`](`.get_base()`) and
99    /// [`len`](`.get_len()`) parameters.
100    pub fn drain_args(&mut self, base: usize, len: usize) -> impl Iterator<Item = Argument> + '_ {
101        self.arguments.drain(base..(base + len))
102    }
103}