workshop-rs 0.3.7

Canonical multi-locale Overwatch Workshop semantic core: catalog, parser, WIR, validation, emitter.
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
use crate::frontend::parser::*;

impl ParseContext<'_> {
    pub(crate) fn conditions_section(&mut self) -> Result<Vec<wir::ValueId>> {
        self.expect_keyword("conditions")?;
        self.expect(TokenKind::LBrace, "expected '{' after 'conditions'")?;
        let mut conditions = Vec::new();
        loop {
            match self.peek() {
                Some(Token {
                    kind: TokenKind::RBrace,
                    ..
                }) => {
                    self.pos += 1;
                    break;
                }
                Some(Token {
                    kind: TokenKind::Semi,
                    ..
                }) => {
                    self.pos += 1;
                }
                Some(_) => {
                    if matches!(
                        self.peek(),
                        Some(Token {
                            kind: TokenKind::String(_),
                            ..
                        })
                    ) {
                        self.pos += 1;
                        continue;
                    }
                    if let Some(Token {
                        kind: TokenKind::Word(word),
                        ..
                    }) = self.peek()
                    {
                        if self.settings_name_matches("tokens", "disabled", &word) {
                            self.pos += 1;
                            let _disabled_condition = self.value()?;
                            self.expect(TokenKind::Semi, "expected ';' after condition")?;
                            continue;
                        }
                    }
                    let condition = self.value()?;
                    self.expect(TokenKind::Semi, "expected ';' after condition")?;
                    conditions.push(condition);
                }
                None => {
                    return Err(self.malformed("unexpected end of input in conditions", self.eof()));
                }
            }
        }
        Ok(conditions)
    }

    pub(crate) fn actions_section(&mut self) -> Result<Vec<wir::ActionId>> {
        self.expect_keyword("actions")?;
        self.expect(TokenKind::LBrace, "expected '{' after 'actions'")?;
        let mut all_actions = Vec::new();
        loop {
            let (actions, stop) = self.actions_until_end()?;
            all_actions.extend(actions);
            if stop == Stop::SectionClosed {
                return Ok(all_actions);
            }
            // Some exported Workshop artifacts retain an unmatched structural
            // marker after source-side pruning. Preserve it as an opaque raw
            // action so the source remains visible instead of silently
            // discarding it or rejecting the whole project.
            all_actions.push(self.opaque_action()?);
        }
    }

    /// Parse actions until a structural `else`/`elseIf`/`end` terminator
    /// (not consumed; the token position is preserved) or the enclosing `}`
    /// (consumed). Returns where the parse stopped.
    pub(crate) fn actions_until_end(&mut self) -> Result<(Vec<wir::ActionId>, Stop)> {
        let mut actions = Vec::new();
        loop {
            match self.peek() {
                Some(Token {
                    kind: TokenKind::RBrace,
                    ..
                }) => {
                    self.pos += 1;
                    return Ok((actions, Stop::SectionClosed));
                }
                Some(Token {
                    kind: TokenKind::Word(_),
                    ..
                }) => {
                    if let Some(action) = self.assignment_action()? {
                        actions.push(action);
                        continue;
                    }
                    let saved = self.pos;
                    let (phrase, start, end) = self.phrase()?;
                    if let Some(rest) = self.disabled_action_rest(&phrase) {
                        if let Some(structural) = self.resolve_entry(Kind::Structural, rest) {
                            match structural.id.as_str() {
                                "while" => {
                                    actions.push(self.while_group()?);
                                    continue;
                                }
                                "if" => {
                                    actions.push(self.if_group()?);
                                    continue;
                                }
                                _ => {}
                            }
                        }
                        if self.resolve_entry(Kind::Action, rest).is_some() {
                            let canonical = rest;
                            actions.push(self.action_call_from_phrase(
                                canonical.to_string(),
                                start,
                                end,
                            )?);
                            continue;
                        }
                    }
                    if canonical_keyword(&phrase) == "disabled"
                        && matches!(
                            self.peek(),
                            Some(Token {
                                kind: TokenKind::Word(word),
                                ..
                            }) if canonical_keyword(&word) == "While"
                        )
                    {
                        self.pos += 1;
                        actions.push(self.while_group()?);
                        continue;
                    }
                    match self.canonical_keyword(&phrase).as_str() {
                        "end" => {
                            self.pos = saved;
                            return Ok((actions, Stop::End));
                        }
                        "elseIf" => {
                            self.pos = saved;
                            return Ok((actions, Stop::ElseIf));
                        }
                        "else" => {
                            self.pos = saved;
                            return Ok((actions, Stop::Else));
                        }
                        "if" => actions.push(self.if_group()?),
                        "forGlobalVariable" => actions.push(self.for_group()?),
                        "forPlayerVariable" => actions.push(self.for_player_group()?),
                        "while" => actions.push(self.while_group()?),
                        "Loop" => actions.push(self.action_call_from_phrase(phrase, start, end)?),
                        "Loop If Condition Is True" => {
                            actions.push(self.action_call_from_phrase(phrase, start, end)?)
                        }
                        "Global" | "Event Player" => {
                            self.pos = saved;
                            actions.push(self.opaque_action()?);
                        }
                        _ => {
                            if self.line_has_assignment() {
                                self.pos = saved;
                                actions.push(self.opaque_action()?);
                            } else {
                                actions.push(self.action_call_from_phrase(phrase, start, end)?);
                            }
                        }
                    }
                }
                Some(Token {
                    kind: TokenKind::String(_),
                    ..
                }) => {
                    // Raw Workshop permits standalone quoted annotations in
                    // generated action blocks. They are inert source text,
                    // not executable actions.
                    self.pos += 1;
                }
                Some(token) => {
                    let saved = self.pos;
                    if let Some(action) = self.member_assignment_action(saved, token.start)? {
                        actions.push(action);
                    } else {
                        return Err(self.malformed("expected an action", &token));
                    }
                }
                None => {
                    return Err(self.malformed("unexpected end of input in actions", self.eof()));
                }
            }
        }
    }

    /// Return the action spelling after the locale-declared disabled
    /// modifier. The modifier itself is settings/catalog data, not a parser
    /// branch for a fixed pair of client locales.
    pub(crate) fn disabled_action_rest<'a>(&self, phrase: &'a str) -> Option<&'a str> {
        if let Some(rest) = phrase.strip_prefix("disabled ") {
            return Some(rest);
        }
        let localized = table::localized_name(self.locale.as_str(), "tokens", "disabled")?;
        phrase.strip_prefix(localized)?.strip_prefix(' ')
    }

    pub(crate) fn assignment_action(&mut self) -> Result<Option<wir::ActionId>> {
        let saved = self.pos;
        let Some(Token {
            kind: TokenKind::Word(first),
            start,
            ..
        }) = self.peek()
        else {
            return Ok(None);
        };

        if matches!(canonical_keyword(&first), "Global" | "global") {
            self.pos += 1;
            self.expect(TokenKind::Dot, "expected '.' after 'Global'")?;
            let (name, _, target_end) = self.phrase()?;
            if matches!(
                self.peek().map(|token| token.kind),
                Some(TokenKind::LBracket)
            ) {
                self.pos += 1;
                let index = self.value()?;
                self.expect(
                    TokenKind::RBracket,
                    "expected ']' after global variable index",
                )?;
                let operator = self.assignment_operator()?.ok_or_else(|| {
                    self.malformed(
                        "expected assignment after global variable index",
                        self.peek().as_ref().unwrap_or(self.eof()),
                    )
                })?;
                let variable = self.global_by_name(&name)?;
                let target = self.target.values.push(ValueNode::new(
                    Value::GlobalVariable(variable),
                    Some(Span::new(self.file(), start, target_end)),
                ));
                let value = self.value()?;
                self.expect(TokenKind::Semi, "expected ';' after indexed assignment")?;
                return Ok(Some(self.indexed_assignment_action(
                    true, target, index, operator, value, start,
                )));
            }
            let Some(operator) = self.assignment_operator()? else {
                return self.member_assignment_action(saved, start);
            };
            let variable = self.global_by_name(&name)?;
            let value = self.value()?;
            let value = if let AssignmentOperator::Modify(op) = &operator {
                self.normalize_modify_value(*op, value)
            } else {
                value
            };
            self.expect(TokenKind::Semi, "expected ';' after assignment")?;
            let span = Some(Span::new(self.file(), start, self.previous_span().1));
            let target_span = Some(Span::new(self.file(), start, target_end));
            return Ok(Some(self.target.actions.push(match operator {
                AssignmentOperator::Set => Action::SetGlobalVariable {
                    variable,
                    value,
                    span,
                    target_span,
                },
                AssignmentOperator::Modify(op) => Action::ModifyGlobalVariable {
                    variable,
                    op,
                    value,
                    span,
                    target_span,
                },
            })));
        }

        let first_canonical = canonical_keyword(&first);
        let is_event_player = first_canonical == "Event Player"
            || (matches!(first_canonical, "Event" | "event")
                && matches!(
                    self.peek_at(1).map(|token| token.kind),
                    Some(TokenKind::Word(word))
                        if matches!(canonical_keyword(&word), "Player" | "player")
                ));
        if !is_event_player {
            // Object/member assignments use the same value grammar as member
            // reads (`receiver.member` and `receiver.member[index]`). Keep
            // this native member-assignment form distinct from catalog actions: the
            // receiver and member are dynamic Workshop values, not a builtin
            // identity. Global and Event Player assignments are handled by
            // their dedicated variable paths above and below.
            if !matches!(first_canonical, "Event" | "event") {
                return self.member_assignment_action(saved, start);
            }
            return Ok(None);
        }

        if first_canonical == "Event Player" {
            self.pos += 1;
        } else {
            self.pos += 2;
        }
        let event_player = self.target.values.push(ValueNode::new(
            Value::EventPlayer,
            Some(Span::new(self.file(), start, self.previous_span().1)),
        ));
        self.expect(TokenKind::Dot, "expected '.' after 'Event Player'")?;
        let (name, target_start, target_end) = self.phrase()?;
        let variable = self.player_by_name(&name)?;
        if matches!(
            self.peek().map(|token| token.kind),
            Some(TokenKind::LBracket)
        ) {
            self.pos += 1;
            let index = self.value()?;
            self.expect(
                TokenKind::RBracket,
                "expected ']' after player variable index",
            )?;
            let operator = self.assignment_operator()?.ok_or_else(|| {
                self.malformed(
                    "expected assignment after player variable index",
                    self.peek().as_ref().unwrap_or(self.eof()),
                )
            })?;
            let value = self.value()?;
            self.expect(TokenKind::Semi, "expected ';' after indexed assignment")?;
            let variable_value = self.target.values.push(ValueNode::new(
                Value::PlayerVariable {
                    player: event_player,
                    variable,
                },
                Some(Span::new(self.file(), target_start, target_end)),
            ));
            return Ok(Some(self.indexed_assignment_action(
                false,
                variable_value,
                index,
                operator,
                value,
                start,
            )));
        }
        let Some(operator) = self.assignment_operator()? else {
            return self.member_assignment_action(saved, start);
        };
        let value = self.value()?;
        let value = if let AssignmentOperator::Modify(op) = &operator {
            self.normalize_modify_value(*op, value)
        } else {
            value
        };
        self.expect(TokenKind::Semi, "expected ';' after assignment")?;
        let span = Some(Span::new(self.file(), start, self.previous_span().1));
        let target_span = Some(Span::new(self.file(), target_start, target_end));
        Ok(Some(self.target.actions.push(match operator {
            AssignmentOperator::Set => Action::SetPlayerVariable {
                player: event_player,
                variable,
                value,
                span,
                target_span,
            },
            AssignmentOperator::Modify(op) => Action::ModifyPlayerVariable {
                player: event_player,
                variable,
                op,
                value,
                span,
                target_span,
            },
        })))
    }

    pub(crate) fn member_assignment_action(
        &mut self,
        saved: usize,
        start: Position,
    ) -> Result<Option<wir::ActionId>> {
        self.pos = saved;
        if !self.line_has_assignment() {
            return Ok(None);
        }
        let target = self.value()?;
        let Some(operator) = self.assignment_operator()? else {
            self.pos = saved;
            return Ok(None);
        };
        let value = self.value()?;
        self.expect(TokenKind::Semi, "expected ';' after member assignment")?;
        let op = match operator {
            AssignmentOperator::Set => None,
            AssignmentOperator::Modify(op) => Some(op),
        };
        Ok(Some(self.target.actions.push(Action::AssignMember {
            target,
            op,
            value,
            span: Some(Span::new(self.file(), start, self.previous_span().1)),
        })))
    }

    pub(crate) fn indexed_assignment_action(
        &mut self,
        global: bool,
        variable: wir::ValueId,
        index: wir::ValueId,
        operator: AssignmentOperator,
        value: wir::ValueId,
        start: Position,
    ) -> wir::ActionId {
        let name = match &operator {
            AssignmentOperator::Set => {
                if global {
                    "setGlobalVariableAtIndex"
                } else {
                    "setPlayerVariableAtIndex"
                }
            }
            AssignmentOperator::Modify(_) => {
                if global {
                    "modifyGlobalVariableAtIndex"
                } else {
                    "modifyPlayerVariableAtIndex"
                }
            }
        };
        let index = self.normalize_contextual_argument(name, 1, index);
        let (value, modify_op) = match operator {
            AssignmentOperator::Set => (value, None),
            AssignmentOperator::Modify(op) => (self.normalize_modify_value(op, value), Some(op)),
        };
        let args = match modify_op {
            None => vec![variable, index, value],
            Some(op) => vec![
                variable,
                index,
                self.target.values.push(ValueNode::new(
                    Value::Call {
                        name: op.catalog_id().to_string(),
                        args: Vec::new(),
                    },
                    None,
                )),
                value,
            ],
        };
        self.target.actions.push(Action::Call {
            name: name.to_string(),
            args,
            span: Some(Span::new(self.file(), start, self.previous_span().1)),
        })
    }

    pub(crate) fn assignment_operator(&mut self) -> Result<Option<AssignmentOperator>> {
        let Some(token) = self.peek() else {
            return Ok(None);
        };
        if let TokenKind::Word(word) = &token.kind {
            let op = match word.as_str() {
                "min" => ModifyOp::Min,
                "max" => ModifyOp::Max,
                _ => {
                    if matches!(self.peek_at(1).map(|token| token.kind), Some(TokenKind::Op(equal)) if equal == "=")
                    {
                        return Err(WorkshopError::Unsupported {
                            message: format!("unsupported assignment operator '{word}='"),
                            span: Some(Span::new(
                                self.file(),
                                token.start,
                                self.peek_at(1).unwrap().end,
                            )),
                        });
                    }
                    return Ok(None);
                }
            };
            if !matches!(self.peek_at(1).map(|token| token.kind), Some(TokenKind::Op(equal)) if equal == "=")
            {
                return Ok(None);
            }
            self.pos += 2;
            return Ok(Some(AssignmentOperator::Modify(op)));
        }
        let operator = match &token.kind {
            TokenKind::Op(operator) => operator.clone(),
            _ => return Ok(None),
        };
        if operator == "=" {
            self.pos += 1;
            return Ok(Some(AssignmentOperator::Set));
        }
        let op = match operator.as_str() {
            "+" => ModifyOp::Add,
            "-" => ModifyOp::Subtract,
            "*" => ModifyOp::Multiply,
            "/" => ModifyOp::Divide,
            "%" => ModifyOp::Modulo,
            _ => return Ok(None),
        };
        if !matches!(self.peek_at(1).map(|token| token.kind), Some(TokenKind::Op(equal)) if equal == "=")
        {
            return Ok(None);
        }
        self.pos += 2;
        Ok(Some(AssignmentOperator::Modify(op)))
    }

    pub(crate) fn opaque_action(&mut self) -> Result<wir::ActionId> {
        let start = self
            .peek()
            .map(|token| token.start)
            .unwrap_or(self.eof().start);
        while let Some(token) = self.peek() {
            self.pos += 1;
            if matches!(token.kind, TokenKind::Semi) {
                break;
            }
        }
        Ok(self.target.actions.push(Action::Call {
            name: "rawWorkshopAction".to_string(),
            args: Vec::new(),
            span: Some(Span::new(self.file(), start, self.previous_span().1)),
        }))
    }

    pub(crate) fn if_group(&mut self) -> Result<wir::ActionId> {
        let start = self.previous_span().0;
        self.expect(TokenKind::LParen, "expected '(' after 'If'")?;
        let condition = self.value()?;
        self.expect(TokenKind::RParen, "expected ')' after If condition")?;
        self.expect(TokenKind::Semi, "expected ';' after If condition")?;

        let mut branches = Vec::new();
        let mut stop = {
            let (body, stop) = self.actions_until_end()?;
            branches.push(wir::IfBranch { condition, body });
            stop
        };

        let mut else_body = None;
        loop {
            match stop {
                Stop::End => {
                    self.consume_phrase("End")?;
                    self.expect(TokenKind::Semi, "expected ';' after 'End'")?;
                    break;
                }
                Stop::ElseIf => {
                    self.consume_phrase("Else If")?;
                    self.expect(TokenKind::LParen, "expected '(' after 'Else If'")?;
                    let condition = self.value()?;
                    self.expect(TokenKind::RParen, "expected ')' after Else If condition")?;
                    self.expect(TokenKind::Semi, "expected ';' after Else If condition")?;
                    let (body, next) = self.actions_until_end()?;
                    branches.push(wir::IfBranch { condition, body });
                    stop = next;
                }
                Stop::Else => {
                    self.consume_phrase("Else")?;
                    self.expect(TokenKind::Semi, "expected ';' after 'Else'")?;
                    let (body, next) = self.actions_until_end()?;
                    else_body = Some(body);
                    stop = next;
                }
                Stop::SectionClosed => {
                    // The oracle closes a rule-final if/if-else with the
                    // enclosing actions-section `}` (no trailing `End;`,
                    // #87). Rewind so the enclosing actions section consumes
                    // that `}` and the rule's own `}` stays intact.
                    self.pos -= 1;
                    break;
                }
            }
        }
        let end_span = self.previous_span();
        let action = Action::If {
            branches,
            else_body,
            span: Some(Span::new(self.file(), start, end_span.1)),
        };
        Ok(self.target.actions.push(action))
    }

    pub(crate) fn for_group(&mut self) -> Result<wir::ActionId> {
        let start = self.previous_span().0;
        self.expect(
            TokenKind::LParen,
            "expected '(' after 'For Global Variable'",
        )?;
        let (name, _, _) = self.phrase()?;
        let variable = self.global_by_name(&name)?;
        self.expect(TokenKind::Comma, "expected ',' after loop variable")?;
        let start_value = self.value()?;
        self.expect(TokenKind::Comma, "expected ',' after start")?;
        let stop = self.value()?;
        self.expect(TokenKind::Comma, "expected ',' after stop")?;
        let step = self.value()?;
        self.expect(TokenKind::RParen, "expected ')' after For bounds")?;
        self.expect(TokenKind::Semi, "expected ';' after For Global Variable")?;
        let (body, loop_stop) = self.actions_until_end()?;
        if loop_stop != Stop::End {
            return Err(self.malformed(
                "'For Global Variable' requires a matching 'End'",
                self.previous(),
            ));
        }
        self.consume_phrase("End")?;
        self.expect(TokenKind::Semi, "expected ';' after 'End'")?;
        let end_span = self.previous_span();
        let action = Action::ForGlobalVariable {
            variable,
            start: start_value,
            stop,
            step,
            body,
            span: Some(Span::new(self.file(), start, end_span.1)),
            target_span: None,
        };
        Ok(self.target.actions.push(action))
    }

    pub(crate) fn while_group(&mut self) -> Result<wir::ActionId> {
        let start = self.previous_span().0;
        self.expect(TokenKind::LParen, "expected '(' after 'While'")?;
        let condition = self.value()?;
        self.expect(TokenKind::RParen, "expected ')' after While condition")?;
        self.expect(TokenKind::Semi, "expected ';' after While condition")?;
        let (body, stop) = self.actions_until_end()?;
        if stop != Stop::End {
            return Err(self.malformed("'While' requires a matching 'End'", self.previous()));
        }
        self.consume_phrase("End")?;
        self.expect(TokenKind::Semi, "expected ';' after 'End'")?;
        let end_span = self.previous_span();
        let action = Action::While {
            condition,
            body,
            span: Some(Span::new(self.file(), start, end_span.1)),
        };
        Ok(self.target.actions.push(action))
    }

    /// `For Player Variable(player, name, start, stop, step)` — the
    /// reference's per-player loop form (parsed from pinned reference
    /// evidence; the differential gate normalizes it to the declared global
    /// form, #119).
    pub(crate) fn for_player_group(&mut self) -> Result<wir::ActionId> {
        let start = self.previous_span().0;
        self.expect(
            TokenKind::LParen,
            "expected '(' after 'For Player Variable'",
        )?;
        let player = self.value()?;
        self.expect(TokenKind::Comma, "expected ',' after loop player")?;
        let (name, _, _) = self.phrase()?;
        let variable = self.player_by_name(&name)?;
        self.expect(TokenKind::Comma, "expected ',' after loop variable")?;
        let start_value = self.value()?;
        self.expect(TokenKind::Comma, "expected ',' after start")?;
        let stop = self.value()?;
        self.expect(TokenKind::Comma, "expected ',' after stop")?;
        let step = self.value()?;
        self.expect(TokenKind::RParen, "expected ')' after For bounds")?;
        self.expect(TokenKind::Semi, "expected ';' after For Player Variable")?;
        let (body, loop_stop) = self.actions_until_end()?;
        if loop_stop != Stop::End {
            return Err(self.malformed(
                "'For Player Variable' requires a matching 'End'",
                self.previous(),
            ));
        }
        self.consume_phrase("End")?;
        self.expect(TokenKind::Semi, "expected ';' after 'End'")?;
        let end_span = self.previous_span();
        let action = Action::ForPlayerVariable {
            player,
            variable,
            start: start_value,
            stop,
            step,
            body,
            span: Some(Span::new(self.file(), start, end_span.1)),
        };
        Ok(self.target.actions.push(action))
    }

    pub(crate) fn action_call_from_phrase(
        &mut self,
        phrase: String,
        start: Position,
        end: Position,
    ) -> Result<wir::ActionId> {
        match self
            .catalog
            .resolve(Kind::Structural, &self.locale, &phrase)
        {
            Some(entry) => match entry.id.as_str() {
                "setGlobalVariable" => {
                    self.expect(
                        TokenKind::LParen,
                        "expected '(' after 'Set Global Variable'",
                    )?;
                    let (name, _, _) = self.phrase()?;
                    let variable = self.global_by_name(&name)?;
                    self.expect(TokenKind::Comma, "expected ',' after variable")?;
                    let value = self.value()?;
                    self.expect(TokenKind::RParen, "expected ')'")?;
                    self.expect(TokenKind::Semi, "expected ';'")?;
                    Ok(self.target.actions.push(Action::SetGlobalVariable {
                        variable,
                        value,
                        span: Some(Span::new(self.file(), start, end)),
                        target_span: None,
                    }))
                }
                "modifyGlobalVariable" => {
                    self.expect(TokenKind::LParen, "expected '('")?;
                    let (name, _, _) = self.phrase()?;
                    let variable = self.global_by_name(&name)?;
                    self.expect(TokenKind::Comma, "expected ',' after variable")?;
                    let op = self.modify_op()?;
                    self.expect(TokenKind::Comma, "expected ',' after modify operator")?;
                    let value = self.value()?;
                    let value = self.normalize_modify_value(op, value);
                    self.expect(TokenKind::RParen, "expected ')'")?;
                    self.expect(TokenKind::Semi, "expected ';'")?;
                    Ok(self.target.actions.push(Action::ModifyGlobalVariable {
                        variable,
                        op,
                        value,
                        span: Some(Span::new(self.file(), start, end)),
                        target_span: None,
                    }))
                }
                "setPlayerVariable" => {
                    self.expect(TokenKind::LParen, "expected '('")?;
                    let player = self.value()?;
                    self.expect(TokenKind::Comma, "expected ',' after player")?;
                    let (name, _, _) = self.phrase()?;
                    let variable = self.player_by_name(&name)?;
                    self.expect(TokenKind::Comma, "expected ',' after variable")?;
                    let value = self.value()?;
                    self.expect(TokenKind::RParen, "expected ')'")?;
                    self.expect(TokenKind::Semi, "expected ';'")?;
                    Ok(self.target.actions.push(Action::SetPlayerVariable {
                        player,
                        variable,
                        value,
                        span: Some(Span::new(self.file(), start, end)),
                        target_span: None,
                    }))
                }
                "modifyPlayerVariable" => {
                    self.expect(TokenKind::LParen, "expected '('")?;
                    let player = self.value()?;
                    self.expect(TokenKind::Comma, "expected ',' after player")?;
                    let (name, _, _) = self.phrase()?;
                    let variable = self.player_by_name(&name)?;
                    self.expect(TokenKind::Comma, "expected ',' after variable")?;
                    let op = self.modify_op()?;
                    self.expect(TokenKind::Comma, "expected ',' after modify operator")?;
                    let value = self.value()?;
                    let value = self.normalize_modify_value(op, value);
                    self.expect(TokenKind::RParen, "expected ')'")?;
                    self.expect(TokenKind::Semi, "expected ';'")?;
                    Ok(self.target.actions.push(Action::ModifyPlayerVariable {
                        player,
                        variable,
                        op,
                        value,
                        span: Some(Span::new(self.file(), start, end)),
                        target_span: None,
                    }))
                }
                "forGlobalVariable" => self.for_group(),
                "forPlayerVariable" => self.for_player_group(),
                "callSubroutine" => {
                    self.expect(TokenKind::LParen, "expected '('")?;
                    let (name, _, _) = self.phrase()?;
                    let subroutine = self.subroutine_by_name(&name)?;
                    self.expect(TokenKind::RParen, "expected ')'")?;
                    self.expect(TokenKind::Semi, "expected ';'")?;
                    Ok(self.target.actions.push(Action::CallSubroutine {
                        subroutine,
                        span: Some(Span::new(self.file(), start, end)),
                        callee_span: None,
                    }))
                }
                other => Err(WorkshopError::Unsupported {
                    message: format!(
                        "structural action '{other}' is not supported in action position"
                    ),
                    span: Some(Span::new(self.file(), start, end)),
                }),
            },
            None => {
                // Generic action call; the argument list is optional.
                let Some(action) = self
                    .resolve_entry(Kind::Action, &phrase)
                    .or_else(|| self.resolve_entry(Kind::Action, &format!("{phrase} ")))
                    .or_else(|| {
                        let alias = match phrase.as_str() {
                            "Set Player Allowed Heroes" => "Set Allowed Heroes",
                            "设置技能充能" => "设置终极技能充能",
                            _ => return None,
                        };
                        self.resolve_entry(Kind::Action, alias)
                    })
                else {
                    return Err(WorkshopError::Unknown {
                        kind: "action",
                        spelling: phrase,
                        locale: self.locale.clone(),
                        span: Some(Span::new(self.file(), start, end)),
                    });
                };
                // The player-variable chase forms lay the variable out as
                // `player, name` leading arguments (the pinned oracle's
                // spelling, #110); the name is not a value, so the action is
                // parsed like `Set Player Variable` and reconstructed as the
                // canonical `chaseAtRate`/`chaseOverTime` call with a
                // player-variable first argument (the shape the emitter
                // dispatches on).
                match action.id.as_str() {
                    "chasePlayerVariableAtRate" | "chasePlayerVariableOverTime" => {
                        self.expect(TokenKind::LParen, "expected '('")?;
                        let player = self.value()?;
                        self.expect(TokenKind::Comma, "expected ',' after player")?;
                        let (name, _, _) = self.phrase()?;
                        let variable = self.player_by_name(&name)?;
                        let mut args = Vec::with_capacity(4);
                        args.push(self.target.values.push(wir::ValueNode::new(
                            wir::Value::PlayerVariable { player, variable },
                            None,
                        )));
                        // The remaining arguments sit at overall argument
                        // indexes 2.. (player and name consumed indexes 0-1),
                        // so the signature context resolves their expected
                        // domains at the shifted positions.
                        let mut arg_index = 2usize;
                        loop {
                            match self.peek() {
                                Some(Token {
                                    kind: TokenKind::RParen,
                                    ..
                                }) => break,
                                Some(Token {
                                    kind: TokenKind::Comma,
                                    ..
                                }) => {
                                    self.pos += 1;
                                }
                                _ => {}
                            }
                            let saved = self.expected_domain;
                            self.expected_domain =
                                self.context.expected_domain(action.id.as_str(), arg_index);
                            let arg = self.value()?;
                            self.expected_domain = saved;
                            args.push(arg);
                            arg_index += 1;
                        }
                        self.expect(TokenKind::RParen, "expected ')'")?;
                        self.expect(TokenKind::Semi, "expected ';' after action")?;
                        let canonical = if action.id == "chasePlayerVariableAtRate" {
                            "chaseAtRate"
                        } else {
                            "chaseOverTime"
                        };
                        return Ok(self.target.actions.push(Action::Call {
                            name: canonical.to_string(),
                            args,
                            span: Some(Span::new(self.file(), start, end)),
                        }));
                    }
                    "startRule" => {
                        self.expect(TokenKind::LParen, "expected '('")?;
                        let (name, _, _) = self.phrase()?;
                        let subroutine = self.subroutine_by_name(&name)?;
                        self.expect(TokenKind::Comma, "expected ',' after subroutine")?;
                        let saved = self.expected_domain;
                        self.expected_domain = self.context.expected_domain(action.id.as_str(), 1);
                        let behavior = self.value()?;
                        self.expected_domain = saved;
                        self.expect(TokenKind::RParen, "expected ')'")?;
                        self.expect(TokenKind::Semi, "expected ';' after action")?;
                        let subroutine_value = self
                            .target
                            .values
                            .push(ValueNode::new(Value::Subroutine(subroutine), None));
                        return Ok(self.target.actions.push(Action::Call {
                            name: action.id.clone(),
                            args: vec![subroutine_value, behavior],
                            span: Some(Span::new(self.file(), start, end)),
                        }));
                    }
                    "stopChasingPlayerVariable" => {
                        self.expect(TokenKind::LParen, "expected '('")?;
                        let player = self.value()?;
                        self.expect(TokenKind::Comma, "expected ',' after player")?;
                        let (name, _, _) = self.phrase()?;
                        let variable = self.player_by_name(&name)?;
                        self.expect(TokenKind::RParen, "expected ')'")?;
                        self.expect(TokenKind::Semi, "expected ';' after action")?;
                        let player_variable = self.target.values.push(ValueNode::new(
                            Value::PlayerVariable { player, variable },
                            None,
                        ));
                        return Ok(self.target.actions.push(Action::Call {
                            name: action.id.clone(),
                            args: vec![player_variable],
                            span: Some(Span::new(self.file(), start, end)),
                        }));
                    }
                    _ => {}
                }
                let args = if let Some(Token {
                    kind: TokenKind::LParen,
                    ..
                }) = self.peek()
                {
                    self.pos += 1;
                    let args = self.value_args(action.id.as_str())?;
                    self.expect(TokenKind::RParen, "expected ')'")?;
                    args
                } else {
                    Vec::new()
                };
                self.expect(TokenKind::Semi, "expected ';' after action")?;
                Ok(self.target.actions.push(Action::Call {
                    name: action.id.clone(),
                    args,
                    span: Some(Span::new(self.file(), start, end)),
                }))
            }
        }
    }

    pub(crate) fn modify_op(&mut self) -> Result<ModifyOp> {
        let (phrase, start, end) = self.phrase()?;
        if phrase == "根据值从数组中移除" {
            return Ok(ModifyOp::RemoveFromArray);
        }
        if phrase == "根据索引从数组中移除" {
            return Ok(ModifyOp::RemoveFromArrayByIndex);
        }
        let entry = self
            .catalog
            .resolve(Kind::Operator, &self.locale, &phrase)
            .ok_or_else(|| WorkshopError::Unknown {
                kind: "modify operator",
                spelling: phrase.clone(),
                locale: self.locale.clone(),
                span: Some(Span::new(self.file(), start, end)),
            })?;
        let op = match entry.id.as_str() {
            "add" => ModifyOp::Add,
            "subtract" => ModifyOp::Subtract,
            "multiply" => ModifyOp::Multiply,
            "divide" => ModifyOp::Divide,
            "modulo" => ModifyOp::Modulo,
            "min" => ModifyOp::Min,
            "max" => ModifyOp::Max,
            "raiseToPower" => ModifyOp::RaiseToPower,
            "appendToArray" => ModifyOp::AppendToArray,
            "removeFromArray" | "removeFromArrayByValue" => ModifyOp::RemoveFromArray,
            "removeFromArrayByIndex" => ModifyOp::RemoveFromArrayByIndex,
            other => {
                return Err(WorkshopError::Unsupported {
                    message: format!("unsupported modify operator '{other}'"),
                    span: Some(Span::new(self.file(), start, end)),
                });
            }
        };
        Ok(op)
    }
}