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
//! Native localized Workshop parser.
//!
//! Parses vanilla Workshop text directly into validated, locale-independent
//! the canonical public [`Program`]. Localized actions, values, events, enums, and structural
//! keywords resolve through the canonical catalog; malformed input,
//! unknown spellings, and recognized-but-unsupported constructs are reported
//! as distinct structured diagnostics with source spans.

pub(crate) use std::collections::HashMap;
use std::sync::OnceLock;

pub(crate) use crate::core::signatures::ExpectedDomain;
pub(crate) use crate::core::source::{Position, SourceFile, Span};
use crate::program::Program;
pub(crate) use crate::settings::table::{self, KeyKind, PathPart};
pub(crate) use crate::settings::{Settings, SettingsListElement, SettingsNode};
pub(crate) use crate::wir::{
    self, Action, Event, EventTarget, EventTeam, ModifyOp, PlayerEventKind, Value, ValueNode,
};

pub(crate) use super::lexer::{Token, TokenKind, tokenize};
pub(crate) use crate::catalog::{Catalog, Kind, Locale, ParamCoercions};
pub(crate) use crate::core::error::{Result, WorkshopError};

/// Where action parsing stopped.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Stop {
    /// The enclosing `}` was consumed.
    SectionClosed,
    /// The `End` keyword is next (not consumed).
    End,
    /// The `Else If` keyword is next (not consumed).
    ElseIf,
    /// The `Else` keyword is next (not consumed).
    Else,
}

pub(crate) enum AssignmentOperator {
    Set,
    Modify(ModifyOp),
}

/// Parse localized Workshop text into Workshop IR using the catalog's
/// canonical call-signature context. Ambiguous bare enum members resolve when
/// their enclosing call pins one matching domain; unpinned ambiguity remains a
/// structured unsupported diagnostic. See [`parse_with_context`] when a
/// consumer needs to provide additional signature context.
pub fn parse(input: &str, catalog: &Catalog, locale: &Locale) -> Result<Program> {
    parse_with_context(input, catalog, locale, catalog)
}

/// Compatibility entry point for crate-internal storage tests and migration
/// checks. Ordinary consumers should use [`parse`], which returns [`Program`].
#[doc(hidden)]
pub fn parse_wir(input: &str, catalog: &Catalog, locale: &Locale) -> Result<wir::Program> {
    parse_wir_with_context(input, catalog, locale, catalog)
}

/// Parse localized Workshop text into Workshop IR, resolving ambiguous bare
/// enum members from the enclosing call's canonical signature context (#111).
///
/// When a bare member spelling matches several enum domains, the parser asks
/// [`ExpectedDomain::expected_domain`] for the domain the enclosing call's
/// signature expects at that argument position; the member resolves only when
/// that expected domain is one of the matching domains (i.e. the signature
/// pins exactly one). Without a pin the ambiguity diagnostic is unchanged.
pub fn parse_with_context(
    input: &str,
    catalog: &Catalog,
    locale: &Locale,
    context: &dyn ExpectedDomain,
) -> Result<Program> {
    parse_wir_with_context(input, catalog, locale, context).and_then(Program::from_wir)
}

#[doc(hidden)]
pub fn parse_wir_with_context(
    input: &str,
    catalog: &Catalog,
    locale: &Locale,
    context: &dyn ExpectedDomain,
) -> Result<wir::Program> {
    let tokens = tokenize(input).map_err(|error| WorkshopError::Malformed {
        message: error.message,
        span: Some(synthetic_span(error.position)),
    })?;
    ParseContext {
        tokens,
        pos: 0,
        source: input,
        catalog,
        locale: locale.clone(),
        context,
        expected_domain: None,
        call_stack: Vec::new(),
        target: wir::Program::default(),
        globals: HashMap::new(),
        players: HashMap::new(),
        subroutines: HashMap::new(),
    }
    .program()
}

/// A synthetic single-position span (used before a file registry exists).
pub(crate) fn synthetic_span(position: Position) -> Span {
    Span::new(crate::core::ids::Id::from_index(0), position, position)
}

pub(crate) struct ParseContext<'a> {
    pub(crate) tokens: Vec<Token>,
    pub(crate) pos: usize,
    pub(crate) source: &'a str,
    pub(crate) catalog: &'a Catalog,
    pub(crate) locale: Locale,
    /// Canonical signature context (#111): supplies the expected enum domain
    /// for the call argument currently being parsed.
    pub(crate) context: &'a dyn ExpectedDomain,
    /// The expected enum domain for the value currently being parsed, set by
    /// [`ParseContext::value_args`] from the enclosing call's signature.
    pub(crate) expected_domain: Option<&'a str>,
    pub(crate) call_stack: Vec<String>,
    pub(crate) target: wir::Program,
    pub(crate) globals: HashMap<String, wir::GlobalVarId>,
    pub(crate) players: HashMap<String, wir::PlayerVarId>,
    pub(crate) subroutines: HashMap<String, wir::SubroutineId>,
}

impl ParseContext<'_> {
    pub(crate) fn resolve_entry(
        &self,
        kind: Kind,
        spelling: &str,
    ) -> Option<crate::catalog::CatalogEntry> {
        self.catalog
            .resolve(kind, &self.locale, spelling)
            .cloned()
            .or_else(|| {
                if self.locale != *self.catalog.primary_locale() {
                    self.catalog
                        .resolve(kind, self.catalog.primary_locale(), spelling)
                        .cloned()
                } else {
                    None
                }
            })
    }

    pub(crate) fn canonical_keyword(&self, spelling: &str) -> String {
        self.catalog
            .resolve(Kind::Structural, &self.locale, spelling)
            .or_else(|| {
                (self.locale != *self.catalog.primary_locale()).then(|| {
                    self.catalog
                        .resolve(Kind::Structural, self.catalog.primary_locale(), spelling)
                })?
            })
            .map(|entry| entry.id.clone())
            .unwrap_or_else(|| canonical_keyword(spelling).to_string())
    }

    pub(crate) fn line_has_assignment(&self) -> bool {
        let tokens: Vec<_> = self.tokens[self.pos..]
            .iter()
            .take_while(|token| !matches!(token.kind, TokenKind::Semi | TokenKind::RBrace))
            .collect();
        tokens.iter().any(|token| {
            matches!(&token.kind, TokenKind::Op(op) if matches!(op.as_str(), "=" | "+=" | "-=" | "*=" | "/=" | "%="))
        }) || tokens.windows(2).any(|window| {
            matches!(&window[0].kind, TokenKind::Word(_))
                && matches!(&window[1].kind, TokenKind::Op(op) if op == "=")
        })
    }

    /// Read the maximal phrase of consecutive words (space-joined). Phrases
    /// may span lines because long Workshop action arguments wrap mid-phrase.
    pub(crate) fn phrase(&mut self) -> Result<(String, Position, Position)> {
        let mut words = Vec::new();
        let (start, mut end) = match self.peek() {
            Some(Token {
                kind: TokenKind::Word(word),
                start,
                end,
            }) => {
                words.push(word.clone());
                (start, end)
            }
            Some(Token {
                kind: TokenKind::Number { text, .. },
                start,
                end,
            }) => {
                words.push(text.clone());
                (start, end)
            }
            Some(token) => return Err(self.malformed("expected an identifier", &token)),
            None => return Err(self.malformed("expected an identifier", self.eof())),
        };
        self.pos += 1;
        while let Some(token) = self.peek() {
            match token {
                Token {
                    kind: TokenKind::Word(word),
                    end: word_end,
                    ..
                } => {
                    if matches!(
                        self.peek_at(1).map(|token| token.kind),
                        Some(TokenKind::Op(equal)) if equal == "="
                    ) {
                        break;
                    }
                    words.push(word.clone());
                    end = word_end;
                    self.pos += 1;
                }
                // Enum members embed numbers (`Team 2`, `Ability 2`); the
                // lexer splits them from the word, so phrases join Number
                // tokens too (#119 reference evidence).
                Token {
                    kind: TokenKind::Number { text, .. },
                    end: number_end,
                    ..
                } => {
                    words.push(text.clone());
                    end = number_end;
                    self.pos += 1;
                }
                _ => break,
            }
        }
        Ok((words.join(" "), start, end))
    }

    /// Read a single-line phrase (stops at a line boundary). Used for names
    /// that are structurally one per line, such as variable declarations.
    pub(crate) fn phrase_on_line(&mut self) -> Result<(String, Position, Position)> {
        let mut words = Vec::new();
        let (start, mut end, line) = match self.peek() {
            Some(Token {
                kind: TokenKind::Word(word),
                start,
                end,
            }) => {
                words.push(word.clone());
                (start, end, start.line)
            }
            Some(Token {
                kind: TokenKind::Number { text, .. },
                start,
                end,
            }) => {
                words.push(text.clone());
                (start, end, start.line)
            }
            Some(token) => return Err(self.malformed("expected an identifier", &token)),
            None => return Err(self.malformed("expected an identifier", self.eof())),
        };
        self.pos += 1;
        while let Some(token) = self.peek() {
            let (word, word_start, word_end) = match token {
                Token {
                    kind: TokenKind::Word(word),
                    start,
                    end,
                } => (word, start, end),
                Token {
                    kind: TokenKind::Number { text, .. },
                    start,
                    end,
                } => (text, start, end),
                Token {
                    kind: TokenKind::Dot,
                    start,
                    end,
                } => (".".to_string(), start, end),
                Token {
                    kind: TokenKind::Op(op),
                    start,
                    end,
                } if matches!(op.as_str(), "-" | "%") => (op.clone(), start, end),
                _ => break,
            };
            if word_start.line != line {
                break;
            }
            words.push(word);
            end = word_end;
            self.pos += 1;
        }
        Ok((
            words
                .join(" ")
                .replace(" .", ".")
                .replace(". ", ".")
                .replace(" : ", ":")
                .replace(" %", "%"),
            start,
            end,
        ))
    }

    pub(crate) fn phrase_on_line_with_colon(&mut self) -> Result<(String, Position, Position)> {
        let (mut phrase, start, mut end) = self.phrase_on_line()?;
        if matches!(
            self.peek(),
            Some(Token {
                kind: TokenKind::Colon,
                ..
            })
        ) {
            let colon_pos = self.pos;
            self.next();
            let (rest, _, rest_end) = self.phrase_on_line()?;
            if matches!(
                self.peek(),
                Some(Token {
                    kind: TokenKind::LBrace,
                    ..
                })
            ) {
                phrase.push(':');
                phrase.push(' ');
                phrase.push_str(&rest);
                end = rest_end;
            } else {
                self.pos = colon_pos;
            }
        }
        Ok((phrase, start, end))
    }

    pub(crate) fn enum_member_phrase(&mut self) -> Result<(String, Position, Position)> {
        let first = self
            .peek()
            .ok_or_else(|| self.malformed("expected an enum member", self.eof()))?;
        let start = first.start;
        let line = first.start.line;
        let mut end = first.end;
        let mut parts = Vec::new();
        while let Some(token) = self.peek() {
            if token.start.line != line
                || matches!(token.kind, TokenKind::RParen | TokenKind::Comma)
            {
                break;
            }
            self.pos += 1;
            end = token.end;
            parts.push(raw_token_text(&token.kind));
        }
        if parts.is_empty() {
            return Err(self.malformed("expected an enum member", &first));
        }
        Ok((
            parts
                .join(" ")
                .replace(" : ", ":")
                .replace(" .", ".")
                .replace(". ", "."),
            start,
            end,
        ))
    }

    /// Read a text line (tokens until `;`), joining words and dashes into
    /// the literal text, and consume the terminating `;`.
    pub(crate) fn line_text(&mut self) -> Result<String> {
        let mut parts = Vec::new();
        loop {
            match self.peek() {
                Some(Token {
                    kind: TokenKind::Semi,
                    ..
                }) => {
                    self.pos += 1;
                    break;
                }
                Some(Token {
                    kind: TokenKind::Word(word),
                    ..
                }) => {
                    parts.push(word.clone());
                    self.pos += 1;
                }
                Some(Token {
                    kind: TokenKind::Op(op),
                    ..
                }) if op == "-" => {
                    parts.push("-".to_string());
                    self.pos += 1;
                }
                Some(Token {
                    kind: TokenKind::Number { value, .. },
                    ..
                }) => {
                    parts.push(value.to_string());
                    self.pos += 1;
                }
                Some(Token {
                    kind: TokenKind::Dot,
                    ..
                }) => {
                    parts.push(".".to_string());
                    self.pos += 1;
                }
                Some(Token {
                    kind: TokenKind::Colon,
                    ..
                }) => {
                    parts.push(":".to_string());
                    self.pos += 1;
                }
                Some(token) => return Err(self.malformed("expected a text line", &token)),
                None => return Err(self.malformed("unexpected end of input in line", self.eof())),
            }
        }
        Ok(parts
            .join(" ")
            .replace(" .", ".")
            .replace(". ", ".")
            .replace(" : ", ":"))
    }

    /// Consume a known keyword phrase, verifying its spelling.
    pub(crate) fn consume_phrase(&mut self, expected: &str) -> Result<()> {
        let (phrase, _, _) = self.phrase()?;
        if phrase != expected {
            return Err(self.malformed(&format!("expected '{expected}'"), self.previous()));
        }
        Ok(())
    }

    pub(crate) fn expect_keyword(&mut self, expected: &str) -> Result<Position> {
        match self.next() {
            Some(Token {
                kind: TokenKind::Word(word),
                start,
                ..
            }) if self.canonical_keyword(&word) == expected => Ok(start),
            Some(token) => Err(self.malformed(&format!("expected '{expected}'"), &token)),
            None => Err(self.malformed(&format!("expected '{expected}'"), self.eof())),
        }
    }

    pub(crate) fn expect(&mut self, kind: TokenKind, message: &str) -> Result<()> {
        match self.next() {
            Some(token) if token.kind == kind => Ok(()),
            Some(token) => Err(self.malformed(message, &token)),
            None => Err(self.malformed(message, self.eof())),
        }
    }

    pub(crate) fn expect_string(&mut self, message: &str) -> Result<String> {
        match self.next() {
            Some(Token {
                kind: TokenKind::String(content),
                ..
            }) => Ok(content),
            Some(token) => Err(self.malformed(message, &token)),
            None => Err(self.malformed(message, self.eof())),
        }
    }

    pub(crate) fn malformed(&self, message: &str, token: &Token) -> WorkshopError {
        WorkshopError::Malformed {
            message: message.to_string(),
            span: Some(Span::new(self.file(), token.start, token.end)),
        }
    }

    pub(crate) fn unknown(&self, kind: &'static str, spelling: &str) -> WorkshopError {
        WorkshopError::Unknown {
            kind,
            spelling: spelling.to_string(),
            locale: self.locale.clone(),
            span: None,
        }
    }

    pub(crate) fn peek(&self) -> Option<Token> {
        self.tokens.get(self.pos).cloned()
    }

    pub(crate) fn peek_at(&self, offset: usize) -> Option<Token> {
        self.tokens.get(self.pos + offset).cloned()
    }

    pub(crate) fn next(&mut self) -> Option<Token> {
        let token = self.tokens.get(self.pos).cloned();
        if token.is_some() {
            self.pos += 1;
        }
        token
    }

    pub(crate) fn previous(&self) -> &Token {
        self.tokens
            .get(self.pos.saturating_sub(1))
            .unwrap_or_else(|| self.tokens.last().unwrap())
    }

    pub(crate) fn previous_span(&self) -> (Position, Position) {
        let token = self.previous();
        (token.start, token.end)
    }

    pub(crate) fn span_here(&self) -> (Position, Position) {
        let token = self
            .peek()
            .unwrap_or_else(|| self.tokens.last().unwrap().clone());
        (token.start, token.end)
    }

    pub(crate) fn eof(&self) -> &Token {
        self.tokens.last().unwrap()
    }

    pub(crate) fn file(&self) -> crate::core::ids::Id<SourceFile> {
        crate::core::ids::Id::from_index(0)
    }
}

pub(crate) fn is_comparison(op: &str) -> bool {
    matches!(op, "==" | "!=" | "<" | "<=" | ">" | ">=")
}

pub(crate) fn canonical_keyword(keyword: &str) -> &str {
    static KEYWORDS: OnceLock<HashMap<String, String>> = OnceLock::new();
    KEYWORDS
        .get_or_init(|| {
            serde_json::from_str(include_str!("structural_keywords.json"))
                .expect("structural keyword data is valid JSON")
        })
        .get(keyword)
        .map(String::as_str)
        .unwrap_or(keyword)
}

pub(crate) fn raw_token_text(kind: &TokenKind) -> String {
    match kind {
        TokenKind::Word(value) => value.clone(),
        TokenKind::Number { text, .. } => text.clone(),
        TokenKind::String(value) => format!("\"{}\"", value.replace('"', "\\\"")),
        TokenKind::Op(value) => value.clone(),
        TokenKind::LParen => "(".to_string(),
        TokenKind::RParen => ")".to_string(),
        TokenKind::Comma => ",".to_string(),
        TokenKind::Semi => ";".to_string(),
        TokenKind::LBrace => "{".to_string(),
        TokenKind::RBrace => "}".to_string(),
        TokenKind::Colon => ":".to_string(),
        TokenKind::Dot => ".".to_string(),
        TokenKind::LBracket => "[".to_string(),
        TokenKind::RBracket => "]".to_string(),
        TokenKind::Eof => String::new(),
    }
}