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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::Pipeline;
use crate::{
OutDest, Signature, Span, Type, VarId,
engine::{BoundNames, ScopeBindings, StateWorkingSet},
ir::IrBlock,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
pub signature: Box<Signature>,
pub pipelines: Vec<Pipeline>,
pub captures: Vec<(VarId, Span)>,
pub redirect_env: bool,
/// The block compiled to IR instructions. Not available for subexpressions.
pub ir_block: Option<IrBlock>,
pub span: Option<Span>, // None option encodes no span to avoid using test_span()
/// Local command/module name bindings introduced while parsing this block.
///
/// Nested parse scopes discard their name maps on exit; this snapshot lets `scope`
/// subcommands report those locals while the block is being evaluated.
///
/// Not serialized: only meaningful within the process that parsed the block.
#[serde(skip)]
pub scope_bindings: Option<Arc<ScopeBindings>>,
/// Whether `parse_block` ran with `scoped = true` (`enter_scope`).
///
/// Distinct from `scope_bindings`: a scoped parse of a let-only file still
/// snapshots as `None`. Needed so `source` does not reuse a `source-env` parse.
#[serde(skip)]
pub parsed_scoped: bool,
/// For a file parsed without a scope of its own (`scoped = false`), the names the parse bound
/// in the caller's scope frame, or `None` if it changed that frame in other ways too.
///
/// The parser reuses a cached unscoped block only where these names are still bound, since
/// reusing it doesn't declare them again. See [`BoundNames`].
#[serde(skip)]
pub bound_names: Option<Arc<BoundNames>>,
}
impl Block {
pub fn len(&self) -> usize {
self.pipelines.len()
}
pub fn is_empty(&self) -> bool {
self.pipelines.is_empty()
}
pub fn pipe_redirection(
&self,
working_set: &StateWorkingSet,
) -> (Option<OutDest>, Option<OutDest>) {
if let Some(first) = self.pipelines.first() {
first.pipe_redirection(working_set)
} else {
(None, None)
}
}
}
impl Default for Block {
fn default() -> Self {
Self::new()
}
}
impl Block {
pub fn new() -> Self {
Self {
signature: Box::new(Signature::new("")),
pipelines: vec![],
captures: vec![],
redirect_env: false,
ir_block: None,
span: None,
scope_bindings: None,
parsed_scoped: false,
bound_names: None,
}
}
pub fn new_with_capacity(capacity: usize) -> Self {
Self {
signature: Box::new(Signature::new("")),
pipelines: Vec::with_capacity(capacity),
captures: vec![],
redirect_env: false,
ir_block: None,
span: None,
scope_bindings: None,
parsed_scoped: false,
bound_names: None,
}
}
pub fn output_type(&self) -> Type {
match self.pipelines.last().and_then(|pl| pl.elements.last()) {
Some(pe) if pe.redirection.is_none() => pe.expr.ty.clone(),
Some(_) => Type::Any,
None => Type::Nothing,
}
}
/// Replace any `$in` variables in the initial element of pipelines within the block
pub fn replace_in_variable(
&mut self,
working_set: &mut StateWorkingSet<'_>,
new_var_id: VarId,
) {
for pipeline in self.pipelines.iter_mut() {
if let Some(element) = pipeline.elements.first_mut() {
element.replace_in_variable(working_set, new_var_id);
}
}
}
}
impl<T> From<T> for Block
where
T: Iterator<Item = Pipeline>,
{
fn from(pipelines: T) -> Self {
Self {
signature: Box::new(Signature::new("")),
pipelines: pipelines.collect(),
captures: vec![],
redirect_env: false,
ir_block: None,
span: None,
scope_bindings: None,
parsed_scoped: false,
bound_names: None,
}
}
}