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
fn test_arithmetic_expansion_in_loop() {
let script = r#"
count=3
while [ "$count" -gt "0" ]; do
echo "Iteration $count"
count=$((count - 1))
done
"#;
let mut parser = BashParser::new(script).unwrap();
let result = parser.parse();
assert!(
result.is_ok(),
"While loop with arithmetic decrement should parse: {:?}",
result.err()
);
let ast = result.unwrap();
let has_while = ast
.statements
.iter()
.any(|s| matches!(s, BashStmt::While { .. }));
assert!(has_while, "AST should contain a while loop");
}
#[test]
fn test_arithmetic_expansion_complex_expressions() {
let script = r#"
a=10
b=20
sum=$((a + b))
diff=$((a - b))
prod=$((a * b))
quot=$((a / b))
mod=$((a % b))
"#;
let mut parser = BashParser::new(script).unwrap();
let result = parser.parse();
assert!(
result.is_ok(),
"Complex arithmetic expressions should parse: {:?}",
result.err()
);
}
// ============================================================================
// ISSUE #4: Benchmark Parser Gaps - STOP THE LINE (P0 BLOCKER)
// ============================================================================
// Issue: docs/known-limitations/issue-004-benchmark-parser-gaps.md
//
// All benchmark fixture files (small.sh, medium.sh, large.sh) fail to parse
// due to missing parser support for common bash constructs:
// 1. $RANDOM - Special bash variable (0-32767 random integer)
// 2. $$ - Process ID variable
// 3. $(command) - Command substitution
// 4. function keyword - Function definition syntax
//
// These tests verify parser ACCEPTS these constructs (LEXER/PARSER ONLY).
// Purification transformation is separate (handled by purifier).
//
// Architecture: bash → PARSE (accept) → AST → PURIFY (transform) → POSIX sh
// Cannot purify what cannot be parsed!
// ============================================================================
#[test]
fn test_ISSUE_004_001_parse_random_special_variable() {
// RED PHASE: Write failing test for $RANDOM parsing
//
// CRITICAL: Parser MUST accept $RANDOM to enable purification
// Purifier will later reject/transform it, but parser must accept first
//
// INPUT: bash with $RANDOM
// EXPECTED: Parser accepts, returns AST with Variable("RANDOM")
// PURIFIER (later): Rejects or transforms to deterministic alternative
let bash = r#"
#!/bin/bash
ID=$RANDOM
echo "Random ID: $ID"
"#;
// ARRANGE: Lexer should tokenize $RANDOM
let lexer_result = BashParser::new(bash);
assert!(
lexer_result.is_ok(),
"Lexer should tokenize $RANDOM: {:?}",
lexer_result.err()
);
// ACT: Parser should accept $RANDOM
let mut parser = lexer_result.unwrap();
let parse_result = parser.parse();
// ASSERT: Parser must accept $RANDOM (for purification to work)
assert!(
parse_result.is_ok(),
"Parser MUST accept $RANDOM to enable purification: {:?}",
parse_result.err()
);
// VERIFY: AST contains assignment with Variable("RANDOM")
let ast = parse_result.unwrap();
assert!(
!ast.statements.is_empty(),
"$RANDOM should produce non-empty AST"
);
}
#[test]
fn test_ISSUE_004_002_parse_process_id_variable() {
// RED PHASE: Write failing test for $$ parsing
//
// CRITICAL: Parser MUST accept $$ to enable purification
// $$ is process ID (non-deterministic, needs purification)
//
// INPUT: bash with $$
// EXPECTED: Parser accepts, returns AST with special PID variable
// PURIFIER (later): Transforms to deterministic alternative
let bash = r#"
#!/bin/bash
PID=$$
TEMP_DIR="/tmp/build-$PID"
echo "Process ID: $PID"
"#;
// ARRANGE: Lexer should tokenize $$
let lexer_result = BashParser::new(bash);
assert!(
lexer_result.is_ok(),
"Lexer should tokenize $$: {:?}",
lexer_result.err()
);
// ACT: Parser should accept $$
let mut parser = lexer_result.unwrap();
let parse_result = parser.parse();
// ASSERT: Parser must accept $$ (for purification to work)
assert!(
parse_result.is_ok(),
"Parser MUST accept $$ to enable purification: {:?}",
parse_result.err()
);
// VERIFY: AST contains assignment with PID variable
let ast = parse_result.unwrap();
assert!(
!ast.statements.is_empty(),
"$$ should produce non-empty AST"
);
}