wasm4pm 26.6.13

High-performance process mining algorithms in WebAssembly for JavaScript/TypeScript
Documentation
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
//! Recursive-descent parser for POWL model strings.
//!
//! Mirrors `pm4py/objects/powl/parser.py:parse_powl_model_string()`.
//!
//! Grammar (informally):
//!   powl  ::= partial_order | decision_graph | xor | loop | tau | transition
//!   partial_order ::= "PO=(nodes={" nodes "}, order={" edges "})"
//!   decision_graph ::= "DG=(nodes={" nodes "}, order={" edges "}, starts=[" starts "], ends=[" ends "], empty=" bool ")"
//!   xor   ::= "X (" powl ("," powl)* ")"
//!   loop  ::= "* (" powl ("," powl)* ")"
//!   tau   ::= "tau"
//!   transition ::= label     (any string not matching above)

use crate::error::Wasm4pmError;
use crate::powl_arena::{BinaryRelation, Operator, PowlArena};
use wasm4pm_compat::powl::{ChoiceGraph, ChoiceGraphNode};

// ─── Tokeniser ────────────────────────────────────────────────────────────────

fn tokenize(s: &str) -> Vec<String> {
    let mut tokens: Vec<String> = Vec::new();
    let mut depth = 0usize;
    let mut cur = String::new();
    for ch in s.chars() {
        match ch {
            '(' | '{' => {
                depth += 1;
                cur.push(ch);
            }
            ')' | '}' => {
                depth = depth.saturating_sub(1);
                cur.push(ch);
            }
            ',' if depth == 0 => {
                let tok = cur.trim().to_string();
                if !tok.is_empty() {
                    tokens.push(tok);
                }
                cur.clear();
            }
            _ => {
                cur.push(ch);
            }
        }
    }
    let tok = cur.trim().to_string();
    if !tok.is_empty() {
        tokens.push(tok);
    }
    tokens
}

// ─── Parser ───────────────────────────────────────────────────────────────────

/// Parse a POWL model string and return the root index in the arena.
pub fn parse_powl_model_string(s: &str, arena: &mut PowlArena) -> Result<u32, Wasm4pmError> {
    let s = s.replace(['\n', '\r', '\t'], "").trim().to_string();

    if s.is_empty() {
        return Err(Wasm4pmError::Parse("empty POWL string".to_string()));
    }

    // Choice graph (POWL 2.0, Definition 1, arXiv:2505.07052)
    if s.starts_with("CG=") || s.starts_with("CG(") {
        return parse_choice_graph(&s, arena).map_err(Wasm4pmError::Parse);
    }

    // Decision graph
    if s.starts_with("DG=") || s.starts_with("DG(") {
        return parse_decision_graph(&s, arena).map_err(Wasm4pmError::Parse);
    }

    // Partial order
    if s.starts_with("PO=") || s.starts_with("PO(") {
        return parse_partial_order(&s, arena).map_err(Wasm4pmError::Parse);
    }

    // XOR
    if s.starts_with("X (") || s.starts_with("X(") {
        return parse_operator(&s, "X", Operator::Xor, arena).map_err(Wasm4pmError::Parse);
    }

    // Loop
    if s.starts_with("* (") || s.starts_with("*(") {
        return parse_operator(&s, "*", Operator::Loop, arena).map_err(Wasm4pmError::Parse);
    }

    // Sequence operator
    if s.starts_with("-> (") || s.starts_with("->(") {
        return parse_sequence_operator(&s, arena).map_err(Wasm4pmError::Parse);
    }

    // Parallel operator
    if s.starts_with("+ (") || s.starts_with("+(") {
        return parse_parallel_operator(&s, arena).map_err(Wasm4pmError::Parse);
    }

    // Silent transition
    if s == "tau" {
        let idx = arena.add_silent_transition();
        return Ok(idx);
    }

    // Labeled transition
    let label = s.trim_matches('\'').to_string();
    Ok(arena.add_transition(Some(label)))
}

// ─── Partial order parsing ────────────────────────────────────────────────────

fn parse_partial_order(s: &str, arena: &mut PowlArena) -> Result<u32, String> {
    // Extract nodes first; then search for order= AFTER the nodes section so
    // that `order={...}` patterns embedded inside child sub-models (which live
    // in the nodes section) are not mistakenly matched.
    let (nodes_str, after_nodes) = extract_braced_content_from(s, "nodes={", 0)?;
    let order_str = extract_braced_content_from(s, "order={", after_nodes)
        .map(|(c, _)| c)
        .unwrap_or("");

    let node_tokens: Vec<String> = if nodes_str.trim().is_empty() {
        Vec::new()
    } else {
        tokenize(nodes_str.trim())
    };

    let mut child_indices: Vec<u32> = Vec::new();
    let mut token_to_local: Vec<(String, u32)> = Vec::new();

    for tok in &node_tokens {
        let child_idx = parse_powl_model_string(tok, arena).map_err(|e| e.to_string())?;
        let local = child_indices.len() as u32;
        child_indices.push(child_idx);
        token_to_local.push((tok.clone(), local));
    }

    let spo_idx = arena.add_strict_partial_order(child_indices.clone());

    if !order_str.trim().is_empty() {
        let edge_tokens: Vec<String> = tokenize(order_str.trim());
        for edge_tok in &edge_tokens {
            if let Some(arrow_pos) = edge_tok.find("-->") {
                let src_str = edge_tok[..arrow_pos].trim();
                let tgt_str = edge_tok[arrow_pos + 3..].trim();

                let src_local = token_to_local
                    .iter()
                    .position(|(t, _)| node_label_matches(t, src_str))
                    .ok_or_else(|| format!("edge source '{}' not found in nodes", src_str))?;
                let tgt_local = token_to_local
                    .iter()
                    .position(|(t, _)| node_label_matches(t, tgt_str))
                    .ok_or_else(|| format!("edge target '{}' not found in nodes", tgt_str))?;

                arena.add_order_edge(spo_idx, src_local, tgt_local).ok();
            }
        }
    }

    Ok(spo_idx)
}

// ─── Decision graph parsing ──────────────────────────────────────────────────────

fn parse_decision_graph(s: &str, arena: &mut PowlArena) -> Result<u32, String> {
    // Extract nodes first; search for order= only after the nodes section so
    // nested sub-model reprs that contain `order={` are not mistakenly matched.
    let (nodes_str, after_nodes) = extract_braced_content_from(s, "nodes={", 0)?;
    let order_str = extract_braced_content_from(s, "order={", after_nodes)
        .map(|(c, _)| c)
        .unwrap_or("");
    let starts_str = extract_bracketed_content(s, "starts=[")?;
    let ends_str = extract_bracketed_content(s, "ends=[")?;
    let empty_str = extract_bool_value(s, "empty=")?;

    let node_tokens: Vec<String> = if nodes_str.trim().is_empty() {
        Vec::new()
    } else {
        tokenize(nodes_str.trim())
    };

    let mut child_indices: Vec<u32> = Vec::new();
    let mut token_to_local: Vec<(String, u32)> = Vec::new();

    for tok in &node_tokens {
        let child_idx = parse_powl_model_string(tok, arena).map_err(|e| e.to_string())?;
        let local = child_indices.len() as u32;
        child_indices.push(child_idx);
        token_to_local.push((tok.clone(), local));
    }

    let n = child_indices.len();
    let mut order = BinaryRelation::new(n);

    if !order_str.trim().is_empty() {
        let edge_tokens: Vec<String> = tokenize(order_str.trim());
        for edge_tok in &edge_tokens {
            if let Some(arrow_pos) = edge_tok.find("-->") {
                let src_str = edge_tok[..arrow_pos].trim();
                let tgt_str = edge_tok[arrow_pos + 3..].trim();

                let src_local = token_to_local
                    .iter()
                    .position(|(t, _)| node_label_matches(t, src_str))
                    .ok_or_else(|| format!("edge source '{}' not found in nodes", src_str))?;
                let tgt_local = token_to_local
                    .iter()
                    .position(|(t, _)| node_label_matches(t, tgt_str))
                    .ok_or_else(|| format!("edge target '{}' not found in nodes", tgt_str))?;

                order.add_edge(src_local, tgt_local);
            }
        }
    }

    let start_nodes: Vec<usize> = if starts_str.trim().is_empty() {
        Vec::new()
    } else {
        parse_node_list(starts_str, &token_to_local)?
    };

    let end_nodes: Vec<usize> = if ends_str.trim().is_empty() {
        Vec::new()
    } else {
        parse_node_list(ends_str, &token_to_local)?
    };

    let empty_path: bool = empty_str == "true";

    Ok(arena.add_decision_graph(child_indices, order, start_nodes, end_nodes, empty_path))
}

fn parse_node_list(s: &str, token_to_local: &[(String, u32)]) -> Result<Vec<usize>, String> {
    let tokens: Vec<String> = tokenize(s.trim());
    let mut indices = Vec::new();
    for tok in &tokens {
        let idx = token_to_local
            .iter()
            .position(|(t, _)| node_label_matches(t, tok))
            .ok_or_else(|| format!("node '{}' not found in token list", tok))?;
        indices.push(idx);
    }
    Ok(indices)
}

// ─── Choice graph parsing (CG=) ──────────────────────────────────────────────
//
// Grammar:
//   CG=(nodes={n0=Start, n1=Activity(a), n2=PO=(...), n3=End},
//       edges={n0->n1, n0->n2, n1->n3, n2->n3})
//
// `Start` and `End` are reserved literals. `Activity(label)` wraps a label.
// Any other expression is parsed as a nested POWL sub-model. Edges use `->`.
fn parse_choice_graph(s: &str, arena: &mut PowlArena) -> Result<u32, String> {
    let nodes_str = extract_braced_content(s, "nodes={")?;
    let edges_str = extract_braced_content(s, "edges={")?;

    let raw_node_tokens: Vec<String> = if nodes_str.trim().is_empty() {
        Vec::new()
    } else {
        tokenize(nodes_str.trim())
    };

    // Each node is `nID=<spec>`; record (id_str, ChoiceGraphNode).
    let mut id_to_idx: Vec<(String, usize)> = Vec::new();
    let mut nodes: Vec<ChoiceGraphNode> = Vec::new();

    for tok in &raw_node_tokens {
        let (id_str, spec) = match tok.find('=') {
            Some(eq) => (
                tok[..eq].trim().to_string(),
                tok[eq + 1..].trim().to_string(),
            ),
            None => return Err(format!("CG node '{}' missing '=spec'", tok)),
        };
        let cg_node = parse_choice_graph_node_spec(&spec, arena)?;
        id_to_idx.push((id_str, nodes.len()));
        nodes.push(cg_node);
    }

    let mut edges: Vec<(usize, usize)> = Vec::new();
    if !edges_str.trim().is_empty() {
        let edge_tokens: Vec<String> = tokenize(edges_str.trim());
        for edge_tok in &edge_tokens {
            // Find `->` but not `-->` (used in PO/DG grammar).
            let arrow_pos = match edge_tok.find("->") {
                Some(p) => p,
                None => continue,
            };
            // Reject `-->` ( PO arrow): must be `->` only.
            if edge_tok.as_bytes().get(arrow_pos + 2) == Some(&b'>') {
                return Err(format!(
                    "CG edge '{}': use `->` (not `-->`) — `-->` belongs to PO/DG grammar",
                    edge_tok
                ));
            }
            let src_id = edge_tok[..arrow_pos].trim();
            let tgt_id = edge_tok[arrow_pos + 2..].trim();
            let src = id_to_idx
                .iter()
                .find(|(id, _)| id == src_id)
                .map(|(_, i)| *i)
                .ok_or_else(|| format!("CG edge source '{}' not found", src_id))?;
            let tgt = id_to_idx
                .iter()
                .find(|(id, _)| id == tgt_id)
                .map(|(_, i)| *i)
                .ok_or_else(|| format!("CG edge target '{}' not found", tgt_id))?;
            edges.push((src, tgt));
        }
    }

    let cg = ChoiceGraph::new(nodes, edges);
    Ok(arena.add_choice_graph(&cg))
}

fn parse_choice_graph_node_spec(
    spec: &str,
    arena: &mut PowlArena,
) -> Result<ChoiceGraphNode, String> {
    let s = spec.trim();
    if s == "Start" {
        return Ok(ChoiceGraphNode::Start);
    }
    if s == "End" {
        return Ok(ChoiceGraphNode::End);
    }
    if let Some(rest) = s.strip_prefix("Activity(") {
        let inner = rest
            .strip_suffix(')')
            .ok_or_else(|| format!("Activity(...) missing ')': '{}'", s))?;
        return Ok(ChoiceGraphNode::Activity(inner.trim().to_string()));
    }
    // Fallback: parse as nested POWL sub-model.
    let sub_idx = parse_powl_model_string(s, arena).map_err(|e| e.to_string())?;
    Ok(ChoiceGraphNode::SubModel(sub_idx))
}

fn extract_bracketed_content<'a>(s: &'a str, key: &str) -> Result<&'a str, String> {
    let start = s
        .find(key)
        .ok_or_else(|| format!("'{}' not found in '{}'", key, s))?;
    let content_start = start + key.len();
    let rest = &s[content_start..];
    let mut depth = 1usize;
    let mut end = 0usize;
    for (i, ch) in rest.char_indices() {
        match ch {
            '[' | '(' | '{' => depth += 1,
            ']' | ')' | '}' => {
                depth -= 1;
                if depth == 0 {
                    end = i;
                    break;
                }
            }
            _ => {}
        }
    }
    Ok(&rest[..end])
}

fn extract_bool_value<'a>(s: &'a str, key: &str) -> Result<&'a str, String> {
    let start = s
        .find(key)
        .ok_or_else(|| format!("'{}' not found in '{}'", key, s))?;
    let content_start = start + key.len();
    let rest = &s[content_start..];

    // Find the end: either a comma, closing paren/brace, or end of string
    let mut end = 0usize;
    for (i, ch) in rest.char_indices() {
        match ch {
            ',' | ')' | '}' => {
                end = i;
                break;
            }
            _ => {}
        }
    }
    if end == 0 {
        end = rest.len();
    }
    Ok(rest[..end].trim())
}

fn node_label_matches(token: &str, label: &str) -> bool {
    let t = token.trim().trim_matches('\'');
    let l = label.trim().trim_matches('\'');
    if t == l {
        return true;
    }
    // Normalize internal whitespace around commas and parens so that
    // "X(pay, installment)" matches "X(pay,installment)" etc.
    let normalize = |s: &str| s.chars().filter(|c| !c.is_whitespace()).collect::<String>();
    normalize(t) == normalize(l)
}

fn extract_braced_content<'a>(s: &'a str, key: &str) -> Result<&'a str, String> {
    extract_braced_content_from(s, key, 0).map(|(content, _)| content)
}

/// Extract the content inside `key{...}` starting the search from byte offset `from`.
/// Returns `(content_slice, byte_offset_after_closing_brace)`.
///
/// Searching from an offset allows callers to chain sequential extractions
/// (e.g. find `nodes={...}` first, then search for `order={` only in the
/// remaining suffix) so that keys nested inside sub-model reprs are not
/// mistakenly matched.
fn extract_braced_content_from<'a>(
    s: &'a str,
    key: &str,
    from: usize,
) -> Result<(&'a str, usize), String> {
    let search_start = s
        .get(from..)
        .ok_or_else(|| format!("offset {} out of range", from))?;
    let rel_start = search_start
        .find(key)
        .ok_or_else(|| format!("'{}' not found in '{}'", key, &s[from..]))?;
    let abs_start = from + rel_start;
    let content_start = abs_start + key.len();
    let rest = &s[content_start..];
    let mut depth = 1usize;
    let mut end = 0usize;
    for (i, ch) in rest.char_indices() {
        match ch {
            '{' | '(' => depth += 1,
            '}' | ')' => {
                depth -= 1;
                if depth == 0 {
                    end = i;
                    break;
                }
            }
            _ => {}
        }
    }
    // abs_end points to the byte just after the closing brace in `s`
    let abs_end = content_start + end + 1; // +1 skips the closing `}`
    Ok((&rest[..end], abs_end))
}

// ─── Operator parsing ─────────────────────────────────────────────────────────

fn parse_operator(
    s: &str,
    prefix: &str,
    op: Operator,
    arena: &mut PowlArena,
) -> Result<u32, String> {
    let after_prefix = s[prefix.len()..].trim();
    let inner = strip_outer_parens(after_prefix)
        .ok_or_else(|| format!("malformed operator expression: '{}'", s))?;

    let child_tokens = tokenize(inner.trim());
    if child_tokens.is_empty() {
        return Err(format!("operator '{}' has no children", prefix));
    }

    let mut children: Vec<u32> = Vec::new();
    for tok in &child_tokens {
        let child_idx = parse_powl_model_string(tok, arena).map_err(|e| e.to_string())?;
        children.push(child_idx);
    }

    match op {
        Operator::Xor if children.len() < 2 => {
            return Err("XOR requires at least 2 children".to_string());
        }
        Operator::Loop if children.len() != 2 => {
            return Err("LOOP requires exactly 2 children".to_string());
        }
        _ => {}
    }

    Ok(arena.add_operator(op, children))
}

fn strip_outer_parens(s: &str) -> Option<&str> {
    let s = s.trim_start();
    if !s.starts_with('(') {
        return None;
    }
    let inner = &s[1..];
    let mut depth = 1usize;
    for (i, ch) in inner.char_indices() {
        match ch {
            '(' | '{' => depth += 1,
            ')' | '}' => {
                depth -= 1;
                if depth == 0 {
                    return Some(&inner[..i]);
                }
            }
            _ => {}
        }
    }
    None
}

// ─── Sequence operator parsing ─────────────────────────────────────────────────

fn parse_sequence_operator(s: &str, arena: &mut PowlArena) -> Result<u32, String> {
    // Remove "-> (" or "->("
    let after_prefix = if s.starts_with("-> (") {
        &s[4..]
    } else if s.starts_with("->(") {
        &s[3..]
    } else {
        return Err("parse_sequence: invalid prefix".to_string());
    };

    // Find the matching closing parenthesis
    let mut depth = 1usize;
    let mut closing_idx = None;
    for (i, ch) in after_prefix.char_indices() {
        match ch {
            '(' | '{' => depth += 1,
            ')' | '}' => {
                depth -= 1;
                if depth == 0 {
                    closing_idx = Some(i);
                    break;
                }
            }
            _ => {}
        }
    }

    let closing_idx = closing_idx.ok_or_else(|| "parse_sequence: missing closing )".to_string())?;
    let content = &after_prefix[..closing_idx];

    // Tokenize children
    let tokens = tokenize(content);
    let mut children = Vec::new();
    for token in tokens {
        if !token.is_empty() && token != "," {
            let op = parse_powl_model_string(&token, arena).map_err(|e| e.to_string())?;
            children.push(op);
        }
    }

    if children.is_empty() {
        return Err("parse_sequence: no children".to_string());
    }

    // Create total-order sequence: all i < j edges
    Ok(arena.add_sequence(children))
}

// ─── Parallel operator parsing ─────────────────────────────────────────────────

fn parse_parallel_operator(s: &str, arena: &mut PowlArena) -> Result<u32, String> {
    // Remove "+ (" or "+("
    let after_prefix = if s.starts_with("+ (") {
        &s[3..]
    } else if s.starts_with("+(") {
        &s[2..]
    } else {
        return Err("parse_parallel: invalid prefix".to_string());
    };

    // Find the matching closing parenthesis
    let mut depth = 1usize;
    let mut closing_idx = None;
    for (i, ch) in after_prefix.char_indices() {
        match ch {
            '(' | '{' => depth += 1,
            ')' | '}' => {
                depth -= 1;
                if depth == 0 {
                    closing_idx = Some(i);
                    break;
                }
            }
            _ => {}
        }
    }

    let closing_idx = closing_idx.ok_or_else(|| "parse_parallel: missing closing )".to_string())?;
    let content = &after_prefix[..closing_idx];

    // Tokenize children
    let tokens = tokenize(content);
    let mut children = Vec::new();
    for token in tokens {
        if !token.is_empty() && token != "," {
            let op = parse_powl_model_string(&token, arena).map_err(|e| e.to_string())?;
            children.push(op);
        }
    }

    if children.is_empty() {
        return Err("parse_parallel: no children".to_string());
    }

    // Create unordered parallel: no edges, pure partial order
    Ok(arena.add_strict_partial_order(children))
}

// ─── WASM entry points ───────────────────────────────────────────────────────

use crate::state::{get_or_init_state, StoredObject};
use crate::utilities::to_js_str;
use wasm_bindgen::prelude::*;

/// Load a POWL v1 model string and return a JSON object with handle, node_count, repr, version.
#[wasm_bindgen]
pub fn load_powl_from_string(powl_str: &str) -> Result<JsValue, JsValue> {
    let mut arena = PowlArena::new();
    let root_idx = parse_powl_model_string(powl_str, &mut arena)
        .map_err(|e| crate::error::js_val(&format!("POWL parse error: {}", e)))?;
    let node_count = arena.len();
    let repr = arena.to_repr(root_idx);
    let handle = get_or_init_state()
        .store_object(StoredObject::PowlModel {
            arena,
            root: root_idx,
        })
        .map_err(|_| crate::error::js_val("Failed to store POWL model"))?;
    to_js_str(&serde_json::json!({
        "handle": handle,
        "node_count": node_count,
        "repr": repr,
        "version": "v1"
    }))
}

/// Load a POWL v2 DSL string and return a JSON object with handle, node_count, repr, version.
#[wasm_bindgen]
pub fn load_powl_v2_from_string(dsl: &str) -> Result<JsValue, JsValue> {
    let mut arena = PowlArena::new();
    let root_idx = parse_powl_v2_string(dsl, &mut arena)
        .map_err(|e| crate::error::js_val(&format!("POWL v2 parse error: {}", e)))?;
    let node_count = arena.len();
    let repr = arena.to_repr(root_idx);
    let handle = get_or_init_state()
        .store_object(StoredObject::PowlModel {
            arena,
            root: root_idx,
        })
        .map_err(|_| crate::error::js_val("Failed to store POWL v2 model"))?;
    to_js_str(&serde_json::json!({
        "handle": handle,
        "node_count": node_count,
        "repr": repr,
        "version": "v2"
    }))
}

// ─── POWL v2 DSL parser ───────────────────────────────────────────────────────
//
// Grammar (POWL v2 DSL from powlv2lsp):
//   Activity(id, "label"?, silent?, related=[...], divergent=[...], convergent=[...], deficient=[...])
//   PartialOrder(id) { nodes: [...], edges: [(id1,id2),...] }
//   ChoiceGraph(id) { nodes: [...], edges: [...], start: id1, end: id2 }
//   Loop(id) { do: ..., redo: ... }
//
// Metadata fields (related/divergent/convergent/deficient) are intentionally ignored.

/// Parse a single POWL v2 DSL construct and return the root arena index.
pub fn parse_powl_v2_string(s: &str, arena: &mut PowlArena) -> Result<u32, Wasm4pmError> {
    let s = s.trim();
    if s.is_empty() {
        return Err(Wasm4pmError::Parse("empty POWL v2 string".to_string()));
    }

    if s.starts_with("Activity(") {
        return parse_v2_activity(s, arena);
    }
    if s.starts_with("PartialOrder(") {
        return parse_v2_partial_order(s, arena);
    }
    if s.starts_with("ChoiceGraph(") {
        return parse_v2_choice_graph(s, arena);
    }
    if s.starts_with("Loop(") {
        return parse_v2_loop(s, arena);
    }

    Err(Wasm4pmError::Parse(format!(
        "unrecognised POWL v2 construct: '{}'",
        &s[..s.len().min(40)]
    )))
}

/// Parse: Activity(id, "label"?, silent?, ...)
/// Only id and optional quoted label are stored; everything else is ignored.
fn parse_v2_activity(s: &str, arena: &mut PowlArena) -> Result<u32, Wasm4pmError> {
    let inner = v2_strip_outer(s, "Activity(", ")")
        .ok_or_else(|| Wasm4pmError::Parse(format!("malformed Activity: '{}'", s)))?;
    // Split by comma at depth 0 to get positional/named args.
    let parts = v2_split_depth0(inner);
    // First part is the id (ignored for storage), second optional is quoted label.
    let label: Option<String> = parts
        .get(1)
        .and_then(|p| v2_extract_quoted_string(p.trim()));
    // Check for silent flag: any part that is exactly "true" or "silent: true"
    let is_silent = parts.iter().any(|p| {
        let t = p.trim();
        t == "true" || t == "silent: true" || t == "silent:true"
    });
    if is_silent {
        Ok(arena.add_silent_transition())
    } else {
        Ok(arena.add_transition(label))
    }
}

/// Parse: PartialOrder(id) { nodes: [...], edges: [(a,b),...] }
fn parse_v2_partial_order(s: &str, arena: &mut PowlArena) -> Result<u32, Wasm4pmError> {
    // Find the `{` block
    let brace_start = s
        .find('{')
        .ok_or_else(|| Wasm4pmError::Parse(format!("PartialOrder missing '{{': '{}'", s)))?;
    let block = v2_extract_brace_block(&s[brace_start..])
        .ok_or_else(|| Wasm4pmError::Parse("PartialOrder: unmatched braces".to_string()))?;

    // Extract nodes: [...]
    let nodes_list = v2_extract_bracket_field(block, "nodes").unwrap_or("");
    // Extract edges: [(a,b),...]
    let edges_list = v2_extract_bracket_field(block, "edges").unwrap_or("");

    // Parse node IDs and build child nodes (each id → transition)
    let node_ids: Vec<String> = if nodes_list.trim().is_empty() {
        vec![]
    } else {
        v2_split_depth0(nodes_list.trim())
            .into_iter()
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect()
    };

    let mut id_to_arena: Vec<(String, u32)> = Vec::new();
    for nid in &node_ids {
        // Each node in PartialOrder nodes list is either an inline construct or a bare id.
        let arena_idx = if nid.starts_with("Activity(")
            || nid.starts_with("PartialOrder(")
            || nid.starts_with("ChoiceGraph(")
            || nid.starts_with("Loop(")
        {
            parse_powl_v2_string(nid, arena)?
        } else {
            arena.add_transition(Some(nid.clone()))
        };
        id_to_arena.push((nid.clone(), arena_idx));
    }

    let children: Vec<u32> = id_to_arena.iter().map(|(_, idx)| *idx).collect();
    let spo_idx = arena.add_strict_partial_order(children);

    // Parse edges: [(a,b),...]
    if !edges_list.trim().is_empty() {
        let edge_tokens = v2_split_depth0(edges_list.trim());
        for etok in &edge_tokens {
            let etok = etok.trim().trim_matches('(').trim_end_matches(')');
            let comma = etok.find(',').ok_or_else(|| {
                Wasm4pmError::Parse(format!("PartialOrder edge '{}' missing comma", etok))
            })?;
            let src_id = etok[..comma].trim();
            let tgt_id = etok[comma + 1..].trim();
            let src_local = id_to_arena
                .iter()
                .position(|(id, _)| id == src_id)
                .ok_or_else(|| Wasm4pmError::Parse(format!("edge src '{}' not found", src_id)))?;
            let tgt_local = id_to_arena
                .iter()
                .position(|(id, _)| id == tgt_id)
                .ok_or_else(|| Wasm4pmError::Parse(format!("edge tgt '{}' not found", tgt_id)))?;
            arena.add_order_edge(spo_idx, src_local, tgt_local).ok();
        }
    }

    Ok(spo_idx)
}

/// Parse: ChoiceGraph(id) { nodes: [...], edges: [...], start: id1, end: id2 }
fn parse_v2_choice_graph(s: &str, arena: &mut PowlArena) -> Result<u32, Wasm4pmError> {
    let brace_start = s
        .find('{')
        .ok_or_else(|| Wasm4pmError::Parse(format!("ChoiceGraph missing '{{': '{}'", s)))?;
    let block = v2_extract_brace_block(&s[brace_start..])
        .ok_or_else(|| Wasm4pmError::Parse("ChoiceGraph: unmatched braces".to_string()))?;

    let nodes_list = v2_extract_bracket_field(block, "nodes").unwrap_or("");
    let edges_list = v2_extract_bracket_field(block, "edges").unwrap_or("");
    let start_id = v2_extract_scalar_field(block, "start")
        .unwrap_or("")
        .trim()
        .to_string();
    let end_id = v2_extract_scalar_field(block, "end")
        .unwrap_or("")
        .trim()
        .to_string();

    let node_ids: Vec<String> = if nodes_list.trim().is_empty() {
        vec![]
    } else {
        v2_split_depth0(nodes_list.trim())
            .into_iter()
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect()
    };

    // Build ChoiceGraph nodes: Start, End, or SubModel(arena_idx)
    let mut id_to_cg_idx: Vec<(String, usize)> = Vec::new();
    let mut cg_nodes: Vec<wasm4pm_compat::powl::ChoiceGraphNode> = Vec::new();

    for nid in &node_ids {
        let cg_node = if nid == "Start" || nid == &start_id {
            wasm4pm_compat::powl::ChoiceGraphNode::Start
        } else if nid == "End" || nid == &end_id {
            wasm4pm_compat::powl::ChoiceGraphNode::End
        } else {
            let sub_idx = if nid.starts_with("Activity(")
                || nid.starts_with("PartialOrder(")
                || nid.starts_with("ChoiceGraph(")
                || nid.starts_with("Loop(")
            {
                parse_powl_v2_string(nid, arena)?
            } else {
                arena.add_transition(Some(nid.clone()))
            };
            wasm4pm_compat::powl::ChoiceGraphNode::SubModel(sub_idx)
        };
        id_to_cg_idx.push((nid.clone(), cg_nodes.len()));
        cg_nodes.push(cg_node);
    }

    // Find start/end sentinel indices
    let start_idx = id_to_cg_idx
        .iter()
        .find(|(id, _)| id == "Start" || id == &start_id)
        .map(|(_, i)| *i)
        .unwrap_or(0);
    let end_idx = id_to_cg_idx
        .iter()
        .find(|(id, _)| id == "End" || id == &end_id)
        .map(|(_, i)| *i)
        .unwrap_or(cg_nodes.len().saturating_sub(1));

    // Parse edges
    let mut edges: Vec<(usize, usize)> = Vec::new();
    if !edges_list.trim().is_empty() {
        let edge_tokens = v2_split_depth0(edges_list.trim());
        for etok in &edge_tokens {
            let etok = etok.trim().trim_matches('(').trim_end_matches(')');
            let comma = etok.find(',').ok_or_else(|| {
                Wasm4pmError::Parse(format!("ChoiceGraph edge '{}' missing comma", etok))
            })?;
            let src_id = etok[..comma].trim();
            let tgt_id = etok[comma + 1..].trim();
            let src = id_to_cg_idx
                .iter()
                .find(|(id, _)| id == src_id)
                .map(|(_, i)| *i)
                .ok_or_else(|| Wasm4pmError::Parse(format!("CG src '{}' not found", src_id)))?;
            let tgt = id_to_cg_idx
                .iter()
                .find(|(id, _)| id == tgt_id)
                .map(|(_, i)| *i)
                .ok_or_else(|| Wasm4pmError::Parse(format!("CG tgt '{}' not found", tgt_id)))?;
            edges.push((src, tgt));
        }
    }

    let cg = wasm4pm_compat::powl::ChoiceGraph {
        nodes: cg_nodes,
        edges,
        start_idx,
        end_idx,
    };
    Ok(arena.add_choice_graph(&cg))
}

/// Parse: Loop(id) { do: ..., redo: ... }
fn parse_v2_loop(s: &str, arena: &mut PowlArena) -> Result<u32, Wasm4pmError> {
    let brace_start = s
        .find('{')
        .ok_or_else(|| Wasm4pmError::Parse(format!("Loop missing '{{': '{}'", s)))?;
    let block = v2_extract_brace_block(&s[brace_start..])
        .ok_or_else(|| Wasm4pmError::Parse("Loop: unmatched braces".to_string()))?;

    let do_str = v2_extract_scalar_field(block, "do")
        .ok_or_else(|| Wasm4pmError::Parse("Loop missing 'do' field".to_string()))?
        .trim()
        .to_string();
    let redo_str = v2_extract_scalar_field(block, "redo")
        .ok_or_else(|| Wasm4pmError::Parse("Loop missing 'redo' field".to_string()))?
        .trim()
        .to_string();

    let do_idx = parse_powl_v2_string(&do_str, arena)?;
    let redo_idx = parse_powl_v2_string(&redo_str, arena)?;

    Ok(arena.add_operator(crate::powl_arena::Operator::Loop, vec![do_idx, redo_idx]))
}

// ─── v2 parsing helpers ───────────────────────────────────────────────────────

/// Strip `prefix` from start and `suffix` from end of `s`, returning the inner slice.
fn v2_strip_outer<'a>(s: &'a str, prefix: &str, suffix: &str) -> Option<&'a str> {
    let s = s.trim();
    if s.starts_with(prefix) && s.ends_with(suffix) {
        Some(&s[prefix.len()..s.len() - suffix.len()])
    } else {
        None
    }
}

/// Split `s` by commas at bracket depth 0.
fn v2_split_depth0(s: &str) -> Vec<String> {
    let mut parts: Vec<String> = Vec::new();
    let mut cur = String::new();
    let mut depth = 0usize;
    for ch in s.chars() {
        match ch {
            '(' | '{' | '[' => {
                depth += 1;
                cur.push(ch);
            }
            ')' | '}' | ']' => {
                depth = depth.saturating_sub(1);
                cur.push(ch);
            }
            ',' if depth == 0 => {
                let tok = cur.trim().to_string();
                if !tok.is_empty() {
                    parts.push(tok);
                }
                cur.clear();
            }
            _ => {
                cur.push(ch);
            }
        }
    }
    let tok = cur.trim().to_string();
    if !tok.is_empty() {
        parts.push(tok);
    }
    parts
}

/// Extract the content of the first `{...}` block in `s`.
fn v2_extract_brace_block(s: &str) -> Option<&str> {
    let start = s.find('{')?;
    let rest = &s[start + 1..];
    let mut depth = 1usize;
    for (i, ch) in rest.char_indices() {
        match ch {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    return Some(&rest[..i]);
                }
            }
            _ => {}
        }
    }
    None
}

/// Extract field with bracket value: `key: [...]` → return content inside `[...]`.
fn v2_extract_bracket_field<'a>(block: &'a str, key: &str) -> Option<&'a str> {
    let search = format!("{}:", key);
    let pos = block.find(&search)?;
    let rest = block[pos + search.len()..].trim_start();
    if !rest.starts_with('[') {
        return None;
    }
    let inner = &rest[1..];
    let mut depth = 1usize;
    for (i, ch) in inner.char_indices() {
        match ch {
            '[' | '{' | '(' => depth += 1,
            ']' | '}' | ')' => {
                depth -= 1;
                if depth == 0 {
                    return Some(&inner[..i]);
                }
            }
            _ => {}
        }
    }
    None
}

/// Extract scalar field value: `key: value` (up to next `,` or `}`).
fn v2_extract_scalar_field<'a>(block: &'a str, key: &str) -> Option<&'a str> {
    let search = format!("{}:", key);
    let pos = block.find(&search)?;
    let rest = block[pos + search.len()..].trim_start();
    // Skip past bracket/brace blocks for values that start with them
    if rest.starts_with('[') || rest.starts_with('{') || rest.starts_with('(') {
        return None; // use v2_extract_bracket_field for these
    }
    let end = rest
        .char_indices()
        .find(|(_, c)| *c == ',' || *c == '}')
        .map(|(i, _)| i)
        .unwrap_or(rest.len());
    Some(rest[..end].trim())
}

/// Extract a quoted string value (`"..."`) from a token.
fn v2_extract_quoted_string(s: &str) -> Option<String> {
    let s = s.trim();
    if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
        Some(s[1..s.len() - 1].to_string())
    } else if s.starts_with('\'') && s.ends_with('\'') && s.len() >= 2 {
        Some(s[1..s.len() - 1].to_string())
    } else {
        None
    }
}

// ─── tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(s: &str) -> (PowlArena, u32) {
        let mut arena = PowlArena::new();
        let root = parse_powl_model_string(s, &mut arena).expect("parse failed");
        (arena, root)
    }

    #[test]
    fn parse_transition() {
        let (arena, root) = parse("A");
        assert_eq!(arena.to_repr(root), "A");
    }

    #[test]
    fn parse_silent() {
        let (arena, root) = parse("tau");
        assert_eq!(arena.to_repr(root), "tau");
    }

    #[test]
    fn parse_xor() {
        let (arena, root) = parse("X ( A, B )");
        assert_eq!(arena.to_repr(root), "X ( A, B )");
    }

    #[test]
    fn parse_loop() {
        let (arena, root) = parse("* ( A, tau )");
        assert_eq!(arena.to_repr(root), "* ( A, tau )");
    }

    #[test]
    fn parse_partial_order_no_edges() {
        let (arena, root) = parse("PO=(nodes={A, B}, order={})");
        assert_eq!(arena.to_repr(root), "PO=(nodes={A, B}, order={})");
    }

    #[test]
    fn parse_partial_order_with_edge() {
        let (arena, root) = parse("PO=(nodes={NODE1, NODE2}, order={NODE1-->NODE2})");
        assert_eq!(
            arena.to_repr(root),
            "PO=(nodes={NODE1, NODE2}, order={NODE1-->NODE2})"
        );
    }

    #[test]
    fn parse_nested() {
        let s = "PO=(nodes={A, X ( B, C )}, order={A-->X ( B, C )})";
        let (arena, root) = parse(s);
        let repr = arena.to_repr(root);
        assert!(repr.contains("A-->"));
        assert!(repr.contains("X ( B, C )"));
    }

    #[test]
    fn docstring_example() {
        let s = "PO=(nodes={ NODE1, NODE2, NODE3 }, order={ NODE1-->NODE2 })";
        let (arena, root) = parse(s);
        assert!(arena.validate_partial_orders(root).is_ok());
        let repr = arena.to_repr(root);
        assert!(repr.contains("NODE1-->NODE2"));
    }

    #[test]
    fn parse_quoted_label() {
        let (arena, root) = parse("'Register Request'");
        assert_eq!(arena.to_repr(root), "Register Request");
    }

    #[test]
    fn parse_sequence_operator() {
        let (arena, root) = parse("-> (a b c)");
        assert!(arena.get(root).is_some());
    }

    #[test]
    fn parse_sequence_operator_nested() {
        let (arena, root) = parse("-> (-> (a b) c)");
        assert!(arena.get(root).is_some());
    }

    #[test]
    fn parse_sequence_operator_with_tau() {
        let (arena, root) = parse("-> (a tau b)");
        assert!(arena.get(root).is_some());
    }

    #[test]
    fn parse_parallel_operator() {
        let (arena, root) = parse("+ (a b c)");
        assert!(arena.get(root).is_some());
    }

    #[test]
    fn parse_parallel_operator_three_children() {
        let (arena, root) = parse("+ (x y z)");
        assert!(arena.get(root).is_some());
    }

    #[test]
    fn parse_parallel_operator_nested() {
        let (arena, root) = parse("+ (+ (a b) c)");
        assert!(arena.get(root).is_some());
    }
}