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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Preprocessing utilities for multiline statement merging
/// Preprocess source to merge ALL multiline statements with braces
/// Handles: synth, bind, pattern, let with map/array, emit, etc.
/// Example:
/// bind myMidi -> myBassline {
/// velocity: 80,
/// bpm: 150
/// }
/// becomes:
/// bind myMidi -> myBassline { velocity: 80, bpm: 150 }
pub fn preprocess_multiline_braces(source: &str) -> String {
let lines: Vec<&str> = source.lines().collect();
let mut result = Vec::new();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
let trimmed = line.trim();
// Skip empty lines and comments
if trimmed.is_empty() || trimmed.starts_with("//") || trimmed.starts_with("#") {
result.push(line.to_string());
i += 1;
continue;
}
// Check if this line contains an opening brace
if line.contains('{') {
// Count braces to see if it's complete on one line
let open_braces = line.matches('{').count();
let close_braces = line.matches('}').count();
if open_braces > close_braces {
// Multiline detected - collect all lines until braces are balanced
let mut merged = line.to_string();
let mut brace_depth = open_braces - close_braces;
i += 1;
while i < lines.len() && brace_depth > 0 {
let next_line = lines[i];
let next_trimmed = next_line.trim();
// Skip comments inside multiline blocks
if next_trimmed.starts_with("//") || next_trimmed.starts_with("#") {
i += 1;
continue;
}
// Remove inline comments before merging
let clean_line = if let Some(comment_pos) = next_line.find("//") {
&next_line[..comment_pos]
} else if let Some(comment_pos) = next_line.find("#") {
&next_line[..comment_pos]
} else {
next_line
};
let clean_trimmed = clean_line.trim();
if !clean_trimmed.is_empty() {
// Add space before appending (unless it's a closing brace)
if !clean_trimmed.starts_with('}') && !merged.ends_with('{') {
merged.push(' ');
} else if clean_trimmed.starts_with('}') {
merged.push(' ');
}
merged.push_str(clean_trimmed);
}
// Update brace depth (use original line for brace counting)
brace_depth += next_line.matches('{').count();
brace_depth -= next_line.matches('}').count();
i += 1;
}
result.push(merged);
continue; // Don't increment i again
}
}
result.push(line.to_string());
i += 1;
}
result.join("\n")
}
/// Preprocess source to merge multiline statements with brackets
/// Handles: timeline, etc.
/// Example:
/// timeline [
/// section1 during 4 bars,
/// section2 during 8 bars
/// ]
/// becomes:
/// timeline [section1 during 4 bars, section2 during 8 bars]
pub fn preprocess_multiline_brackets(source: &str) -> String {
let lines: Vec<&str> = source.lines().collect();
let mut result = Vec::new();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
let trimmed = line.trim();
// Skip empty lines and comments
if trimmed.is_empty() || trimmed.starts_with("//") || trimmed.starts_with("#") {
result.push(line.to_string());
i += 1;
continue;
}
// Check if this line contains an opening bracket
if line.contains('[') {
// Count brackets to see if it's complete on one line
let open_brackets = line.matches('[').count();
let close_brackets = line.matches(']').count();
if open_brackets > close_brackets {
// Multiline detected - collect all lines until brackets are balanced
let mut merged = line.to_string();
let mut bracket_depth = open_brackets - close_brackets;
i += 1;
while i < lines.len() && bracket_depth > 0 {
let next_line = lines[i];
let next_trimmed = next_line.trim();
// Skip comments inside multiline blocks
if next_trimmed.starts_with("//") || next_trimmed.starts_with("#") {
i += 1;
continue;
}
// Remove inline comments before merging
let clean_line = if let Some(comment_pos) = next_line.find("//") {
&next_line[..comment_pos]
} else if let Some(comment_pos) = next_line.find("#") {
&next_line[..comment_pos]
} else {
next_line
};
let clean_trimmed = clean_line.trim();
if !clean_trimmed.is_empty() {
// Add space before appending
if !merged.ends_with('[') && !merged.ends_with(',') {
merged.push(' ');
} else if merged.ends_with(',') {
merged.push(' ');
}
merged.push_str(clean_trimmed);
}
// Update bracket depth
bracket_depth += next_line.matches('[').count();
bracket_depth -= next_line.matches(']').count();
i += 1;
}
result.push(merged);
continue; // Don't increment i again
}
}
result.push(line.to_string());
i += 1;
}
result.join("\n")
}
//
/// Preprocess source to merge multiline arrow calls
/// Example:
/// target -> method1(arg)
/// -> method2(arg)
/// becomes:
/// target -> method1(arg) -> method2(arg)
pub fn preprocess_multiline_arrow_calls(source: &str) -> String {
let lines: Vec<&str> = source.lines().collect();
let mut result = Vec::new();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
let trimmed = line.trim();
// Case A: line contains an arrow call and is not itself a continuation (doesn't start with ->)
if line.contains("->") && !trimmed.starts_with("->") {
// This is the start of a potential multiline arrow call
let mut merged = line.to_string();
i += 1;
// Merge all following lines that start with -> (allow interleaved comments/blank lines)
while i < lines.len() {
let next_line = lines[i];
let next_trimmed = next_line.trim();
// Skip comment or empty lines between arrow continuations
if next_trimmed.is_empty()
|| next_trimmed.starts_with("//")
|| next_trimmed.starts_with("#")
{
i += 1;
continue;
}
if next_trimmed.starts_with("->") {
// Append this line to the merged line
merged.push(' ');
merged.push_str(next_trimmed);
i += 1;
} else {
// Not a continuation line, stop merging
break;
}
}
result.push(merged);
continue; // Don't increment i again
}
// Case B: support definitions that set a trigger (e.g. `let t = .bank.kick`) followed by
// continuation lines that start with `-> effect(...)`. In this case the initial line does
// not contain '->' but we should treat following '->' lines as part of the same statement.
// Detect a trigger assignment by checking for '=' and a '.' beginning the RHS.
if !trimmed.starts_with("->") && line.contains('=') {
if let Some(rhs) = line.splitn(2, '=').nth(1) {
if rhs.trim_start().starts_with('.') {
// This is a trigger assignment; merge following -> lines
let mut merged = line.to_string();
let mut j = i + 1;
let mut merged_any = false;
while j < lines.len() {
let next_line = lines[j];
let next_trimmed = next_line.trim();
// Skip comments/blank lines
if next_trimmed.is_empty()
|| next_trimmed.starts_with("//")
|| next_trimmed.starts_with("#")
{
j += 1;
continue;
}
if next_trimmed.starts_with("->") {
merged.push(' ');
merged.push_str(next_trimmed);
merged_any = true;
j += 1;
} else {
break;
}
}
if merged_any {
result.push(merged);
i = j;
continue;
}
// If no continuation lines, fall through to default handling
}
}
}
// Case B2: support bare trigger lines like `.bank.kick` followed by indented `->` lines
// Example:
// .myBank.kick
// -> speed(1.0)
// -> reverse(true)
// In this case merge the following `->` continuation lines into the trigger line so
// parse_trigger_line can pick up effects in a single statement.
if !trimmed.starts_with("->") && trimmed.starts_with('.') && !line.contains("->") {
let mut merged = line.to_string();
let mut j = i + 1;
let mut merged_any = false;
while j < lines.len() {
let next_line = lines[j];
let next_trimmed = next_line.trim();
// Skip comments/blank lines
if next_trimmed.is_empty()
|| next_trimmed.starts_with("//")
|| next_trimmed.starts_with("#")
{
j += 1;
continue;
}
if next_trimmed.starts_with("->") {
merged.push(' ');
merged.push_str(next_trimmed);
merged_any = true;
j += 1;
} else {
break;
}
}
if merged_any {
result.push(merged);
i = j;
continue;
}
// If no continuation lines, fall through
}
// Case C: support synth declarations split across lines, e.g.
// let mySynth = synth saw
// -> lfo(...)
// If the RHS of an assignment starts with 'synth', merge following '->' continuation lines
if !trimmed.starts_with("->") && line.contains('=') {
if let Some(rhs) = line.splitn(2, '=').nth(1) {
if rhs.trim_start().starts_with("synth") {
// This is a synth assignment; merge following -> lines
let mut merged = line.to_string();
let mut j = i + 1;
let mut merged_any = false;
while j < lines.len() {
let next_line = lines[j];
let next_trimmed = next_line.trim();
// Skip comments/blank lines
if next_trimmed.is_empty()
|| next_trimmed.starts_with("//")
|| next_trimmed.starts_with("#")
{
j += 1;
continue;
}
if next_trimmed.starts_with("->") {
merged.push(' ');
merged.push_str(next_trimmed);
merged_any = true;
j += 1;
} else {
break;
}
}
if merged_any {
result.push(merged);
i = j;
continue;
}
// If no continuation lines, fall through to default handling
}
}
}
// Case D: support bare identifier lines followed by indented '->' continuations
// Example:
// mySynth
// -> note(C4)
// -> duration(2000)
// Merge into: `mySynth -> note(C4) -> duration(2000)`
if !trimmed.starts_with("->") && !line.contains("->") {
// If the line is a single identifier-like token, merge following -> lines
// Skip reserved keywords (let, var, const, for, if, group, etc.) to avoid
// accidentally merging declarations with continuations.
let token = trimmed.split_whitespace().next().unwrap_or("");
let reserved = [
"let", "var", "const", "for", "if", "group", "spawn", "on", "automate", "bind",
"call", "emit", "bank",
];
if !token.is_empty()
&& !reserved.contains(&token)
&& token
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '.')
{
let mut merged = line.to_string();
let mut j = i + 1;
let mut merged_any = false;
while j < lines.len() {
let next_line = lines[j];
let next_trimmed = next_line.trim();
if next_trimmed.is_empty()
|| next_trimmed.starts_with("//")
|| next_trimmed.starts_with("#")
{
j += 1;
continue;
}
if next_trimmed.starts_with("->") {
merged.push(' ');
merged.push_str(next_trimmed);
merged_any = true;
j += 1;
} else {
break;
}
}
if merged_any {
// If the previous pushed line was a `let <name> = synth ...` declaration for
// the same identifier, attach the chain to that let line instead of
// leaving a separate bare target line.
// Look backwards for the nearest non-empty previous pushed line and
// attempt to attach the chain to a `let <token> =` synth declaration.
// Determine the first method name in the chain (the call after the first '->')
let mut first_method_name = String::new();
if let Some(second_part) = merged.split("->").nth(1) {
let second_part = second_part.trim();
if let Some(paren_idx) = second_part.find('(') {
first_method_name = second_part[..paren_idx].trim().to_string();
} else {
first_method_name = second_part.to_string();
}
}
// Allowed synth-parameter method names which should be attached to the synth
// declaration when used under a bare identifier. Other methods (like `note` or
// `duration`) are runtime actions and should remain as separate ArrowCall
// statements targeting the identifier.
let synth_param_methods =
["type", "adsr", "lfo", "filter", "filters", "options"];
let _let_pattern1 = format!("let {} =", token);
let _let_pattern2 = format!("let {}=", token);
let mut attached = false;
// Only attempt to attach the chain to a previous `let` if the chain
// begins with a synth-parameter method.
if synth_param_methods.contains(&first_method_name.as_str()) {
if !result.is_empty() {
// Find index of last non-empty previous line
let mut k = result.len();
while k > 0 {
k -= 1;
let candidate = result[k].trim();
if candidate.is_empty() {
// skip empty/comment lines
continue;
}
// Be more permissive: accept any previous line that looks like a let assignment
// for this token (contains 'let', the token, and an '='). This is robust to
// spacing variations like 'let name =', 'let name=' or additional text.
let candidate_line = result[k].to_lowercase();
if candidate_line.contains("let")
&& candidate_line.contains(&token.to_lowercase())
&& candidate_line.contains('=')
{
// Attach chain part to this candidate line
if let Some(pos) = merged.find(token) {
let chain_part = merged[pos + token.len()..].trim_start();
if !chain_part.is_empty() {
result[k].push(' ');
result[k].push_str(chain_part);
}
i = j;
attached = true;
}
}
break; // stop after first non-empty line
}
}
}
if attached {
continue;
}
// If the first method is a synth-param, we attempted to attach above. If not,
// keep the merged bare identifier arrow call as its own statement.
if !synth_param_methods.contains(&first_method_name.as_str()) {
result.push(merged);
i = j;
continue;
}
// If we reach here but not attached (e.g., no matching let found), fall through
// and push the merged chain as its own statement.
result.push(merged);
i = j;
continue;
}
}
}
// Fallback: if this is a let assignment (synth or otherwise) and the subsequent
// non-empty lines start with '->', merge them. This is a more permissive rule to
// handle cases where continuation lines were not merged earlier (indentation/formatting).
if trimmed.starts_with("let ") && !trimmed.starts_with("->") && line.contains('=') {
// Peek ahead to see if next meaningful line starts with '->'
let mut j = i + 1;
let mut found = false;
while j < lines.len() {
let nl = lines[j];
let nt = nl.trim();
if nt.is_empty() || nt.starts_with("//") || nt.starts_with("#") {
j += 1;
continue;
}
if nt.starts_with("->") {
found = true;
}
break;
}
if found {
let mut merged = line.to_string();
i += 1;
let mut merged_any = false;
while i < lines.len() {
let next_line = lines[i];
let next_trimmed = next_line.trim();
if next_trimmed.is_empty()
|| next_trimmed.starts_with("//")
|| next_trimmed.starts_with("#")
{
i += 1;
continue;
}
if next_trimmed.starts_with("->") {
merged.push(' ');
merged.push_str(next_trimmed);
merged_any = true;
i += 1;
} else {
break;
}
}
if merged_any {
result.push(merged);
continue;
}
}
}
result.push(line.to_string());
i += 1;
}
result.join("\n")
}