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
//! Standalone recursive-descent parser for bash arithmetic expressions.
//!
//! Parses the body of `$((...))` or `((...))` into a typed AST using the
//! existing `NodeKind::Arith*` variants. The public entry point,
//! [`parse_arith_expression`], takes the inner expression text (without the
//! surrounding delimiters) and returns a single [`Node`] representing the
//! parsed tree.
//!
//! Precedence follows the bash manual (lowest → highest): comma, assignment,
//! ternary, logical OR/AND, bitwise OR/XOR/AND, equality, comparison, shift,
//! additive, multiplicative, exponentiation, unary, pre/post increment and
//! decrement, primary.
//!
//! # Layout
//!
//! | file | responsibility |
//! |---------------|-------------------------------------------------------|
//! | `tokenizer.rs`| lexes the expression text into `Tok` values |
//! | `parser.rs` | `ArithParser` + precedence-climbing cascade |
//! | `tests.rs` | unit tests for tokenizer and parser combined |
use crate;
use crate;
use ArithParser;
use tokenize;
/// Maximum recursion depth for `parse_expression`, which is re-entered for
/// parenthesized groups, array subscripts, and ternary `if_true` branches.
/// Bounds the stack on pathological inputs like `((((((x))))))`.
///
/// Each recursive `parse_expression` call cascades through ~15 precedence
/// levels before reaching `parse_primary`, so keep the limit low enough
/// that even debug builds on small test-thread stacks stay safe. Real
/// bash arithmetic expressions never nest deeper than a handful of levels.
pub const MAX_ARITH_DEPTH: usize = 32;
/// Parses a bash arithmetic expression from its inner text.
///
/// # Errors
///
/// Returns [`RableError::Parse`] on malformed input. Callers that want
/// best-effort behavior (e.g. `Word.parts` decomposition) can ignore the
/// error and store `None` in the resulting `ArithmeticExpansion` node.
pub
/// Shared `Node` constructor used across `mod.rs` and `parser.rs`.
pub const
/// Shared error constructor used by both the tokenizer and the parser.
/// Positions are zero because the arithmetic subparser operates on an
/// already-extracted substring; the outer parser attaches real spans when
/// wrapping the returned node into an `ArithmeticExpansion`.
pub