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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use super::*;
impl<'a> Parser<'a> {
/// Create a new bash-profile parser for the given input.
pub fn new(input: &'a str) -> Self {
Self::with_limits_and_profile(
input,
DEFAULT_MAX_AST_DEPTH,
DEFAULT_MAX_PARSER_OPERATIONS,
ShellProfile::native(ShellDialect::Bash),
)
}
/// Create a new parser for the given input and shell dialect.
///
/// This uses [`ShellProfile::native`] for the selected dialect. Use
/// [`Parser::with_profile`] when zsh option state is known.
pub fn with_dialect(input: &'a str, dialect: ShellDialect) -> Self {
Self::with_profile(input, ShellProfile::native(dialect))
}
/// Create a new parser for the given input and full shell profile.
///
/// Profiles allow callers to provide parser-visible zsh option state in
/// addition to the broad shell dialect.
pub fn with_profile(input: &'a str, shell_profile: ShellProfile) -> Self {
Self::with_limits_and_profile(
input,
DEFAULT_MAX_AST_DEPTH,
DEFAULT_MAX_PARSER_OPERATIONS,
shell_profile,
)
}
/// Create a new bash parser with a custom maximum AST depth.
///
/// The requested depth is clamped to the parser's hard safety cap. Hitting
/// the limit produces a non-clean [`ParseResult`] rather than panicking.
pub fn with_max_depth(input: &'a str, max_depth: usize) -> Self {
Self::with_limits_and_profile(
input,
max_depth,
DEFAULT_MAX_PARSER_OPERATIONS,
ShellProfile::native(ShellDialect::Bash),
)
}
/// Create a new bash parser with a custom fuel limit.
///
/// Fuel bounds the number of parser operations. Exhaustion produces a
/// terminal parse error in the returned [`ParseResult`].
pub fn with_fuel(input: &'a str, max_fuel: usize) -> Self {
Self::with_limits_and_profile(
input,
DEFAULT_MAX_AST_DEPTH,
max_fuel,
ShellProfile::native(ShellDialect::Bash),
)
}
/// Create a new bash parser with custom depth and fuel limits.
///
/// `max_depth` is clamped to the parser's hard safety cap to prevent stack
/// overflow from misconfiguration. `max_fuel` bounds parser operations.
/// Either limit can produce a non-clean [`ParseResult`].
pub fn with_limits(input: &'a str, max_depth: usize, max_fuel: usize) -> Self {
Self::with_limits_and_profile(
input,
max_depth,
max_fuel,
ShellProfile::native(ShellDialect::Bash),
)
}
/// Create a new parser with custom depth, fuel, and dialect settings.
///
/// This uses [`ShellProfile::native`] for `dialect`; use
/// [`Parser::with_limits_and_profile`] when explicit zsh option state is
/// available.
pub fn with_limits_and_dialect(
input: &'a str,
max_depth: usize,
max_fuel: usize,
dialect: ShellDialect,
) -> Self {
Self::with_limits_and_profile(input, max_depth, max_fuel, ShellProfile::native(dialect))
}
/// Create a new parser with custom depth, fuel, and shell-profile settings.
///
/// This is the most explicit constructor for embedders that need both
/// resource limits and parser-visible shell option state.
pub fn with_limits_and_profile(
input: &'a str,
max_depth: usize,
max_fuel: usize,
shell_profile: ShellProfile,
) -> Self {
Self::with_limits_and_profile_and_benchmarking(
input,
max_depth,
max_fuel,
shell_profile,
false,
)
}
pub(super) fn with_limits_and_profile_and_benchmarking(
input: &'a str,
max_depth: usize,
max_fuel: usize,
shell_profile: ShellProfile,
benchmark_counters_enabled: bool,
) -> Self {
#[cfg(not(feature = "benchmarking"))]
let _ = benchmark_counters_enabled;
let zsh_timeline = (shell_profile.dialect == ShellDialect::Zsh)
.then(|| ZshOptionTimeline::build(input, &shell_profile))
.flatten()
.map(Arc::new);
let mut lexer = Lexer::with_max_subst_depth_and_profile(
input,
max_depth.min(HARD_MAX_AST_DEPTH),
&shell_profile,
zsh_timeline.clone(),
);
#[cfg(feature = "benchmarking")]
if benchmark_counters_enabled {
lexer.enable_benchmark_counters();
}
let mut comments = Vec::new();
let (current_token, current_token_kind, current_keyword, current_span) = loop {
match lexer.next_lexed_token_with_comments() {
Some(st) if st.kind == TokenKind::Comment => {
comments.push(Comment {
range: st.span.to_range(),
});
}
Some(st) => {
break (
Some(st.clone()),
Some(st.kind),
Self::keyword_from_token(&st),
st.span,
);
}
None => break (None, None, None, Span::new()),
}
};
Self {
input,
lexer,
synthetic_tokens: VecDeque::new(),
alias_replays: Vec::new(),
current_token,
current_word_cache: None,
current_token_kind,
current_keyword,
current_span,
peeked_token: None,
max_depth: max_depth.min(HARD_MAX_AST_DEPTH),
current_depth: 0,
fuel: max_fuel,
max_fuel,
source_text_pattern_depth: 0,
comments,
aliases: HashMap::new(),
expand_aliases: false,
expand_next_word: false,
brace_group_depth: 0,
brace_body_stack: Vec::new(),
syntax_facts: SyntaxFacts::default(),
dialect: shell_profile.dialect,
shell_profile,
zsh_timeline,
#[cfg(feature = "benchmarking")]
benchmark_counters: benchmark_counters_enabled.then(ParserBenchmarkCounters::default),
}
}
#[cfg(feature = "benchmarking")]
pub(super) fn rebuild_with_benchmark_counters(&self) -> Self {
Self::with_limits_and_profile_and_benchmarking(
self.input,
self.max_depth,
self.max_fuel,
self.shell_profile.clone(),
true,
)
}
#[cfg(test)]
pub(super) fn current_span(&self) -> Span {
self.current_span
}
/// Parse a standalone shell word string.
///
/// This handles shell word constructs such as parameter expansion, command
/// substitution, arithmetic expansion, and quoting. The returned word is
/// positioned as if `input` started at the beginning of a file.
pub fn parse_word_string(input: &str) -> Word {
let mut parser = Parser::new(input);
let start = Position::new();
parser.parse_word_with_context(
input,
Span::from_positions(start, start.advanced_by(input)),
start,
true,
)
}
/// Classify a contiguous group of already-parsed words as a shell assignment.
///
/// Some shell syntax, such as process substitution inside an array subscript,
/// can produce multiple AST words while still occupying one contiguous
/// assignment operand in the source.
pub fn parse_assignment_word_group(
source: &str,
words: &[&Word],
explicit_array_kind: Option<ArrayKind>,
subscript_interpretation: SubscriptInterpretation,
) -> Option<Assignment> {
let first = words.first()?;
let last = words.last()?;
let span = Span::from_positions(first.span.start, last.span.end);
let raw = span.slice(source);
let mut parser = Parser::new(source);
parser.parse_assignment_from_text(raw, span, explicit_array_kind, subscript_interpretation)
}
/// Parse a word string with caller-configured limits and shell dialect.
pub(super) fn parse_word_string_with_limits_and_dialect(
input: &str,
max_depth: usize,
max_fuel: usize,
dialect: ShellDialect,
) -> Word {
let mut parser = Parser::with_limits_and_profile(
input,
max_depth,
max_fuel,
ShellProfile::native(dialect),
);
let start = Position::new();
parser.parse_word_with_context(
input,
Span::from_positions(start, start.advanced_by(input)),
start,
true,
)
}
/// Parse a fragment against the original source span so part offsets stay
/// aligned with the surrounding script.
#[cfg(test)]
pub(super) fn parse_word_fragment(source: &str, text: &str, span: Span) -> Word {
Self::parse_word_fragment_with_limits(
source,
text,
span,
DEFAULT_MAX_AST_DEPTH,
DEFAULT_MAX_PARSER_OPERATIONS,
ShellProfile::native(ShellDialect::Bash),
)
}
pub(super) fn parse_word_fragment_with_limits(
source: &str,
text: &str,
span: Span,
max_depth: usize,
max_fuel: usize,
shell_profile: ShellProfile,
) -> Word {
let mut parser = Parser::with_limits_and_profile(text, max_depth, max_fuel, shell_profile);
let source_backed = span.end.offset <= source.len() && span.slice(source) == text;
let start = Position::new();
let fragment_span = Span::from_positions(start, start.advanced_by(text));
let mut word = parser.parse_word_with_context(text, fragment_span, start, source_backed);
if !source_backed {
Self::materialize_word_source_backing(&mut word, text);
}
Self::rebase_word(&mut word, span.start);
word.span = span;
word
}
}