ad-astra 1.0.0

Embeddable scripting language platform Ad Astra. Main Crate.
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
////////////////////////////////////////////////////////////////////////////////
// This file is part of "Ad Astra", an embeddable scripting programming       //
// language platform.                                                         //
//                                                                            //
// This work is proprietary software with source-available code.              //
//                                                                            //
// To copy, use, distribute, or contribute to this work, you must agree to    //
// the terms of the General License Agreement:                                //
//                                                                            //
// https://github.com/Eliah-Lakhin/ad-astra/blob/master/EULA.md               //
//                                                                            //
// The agreement grants a Basic Commercial License, allowing you to use       //
// this work in non-commercial and limited commercial products with a total   //
// gross revenue cap. To remove this commercial limit for one of your         //
// products, you must acquire a Full Commercial License.                      //
//                                                                            //
// If you contribute to the source code, documentation, or related materials, //
// you must grant me an exclusive license to these contributions.             //
// Contributions are governed by the "Contributions" section of the General   //
// License Agreement.                                                         //
//                                                                            //
// Copying the work in parts is strictly forbidden, except as permitted       //
// under the General License Agreement.                                       //
//                                                                            //
// If you do not or cannot agree to the terms of this Agreement,              //
// do not use this work.                                                      //
//                                                                            //
// This work is provided "as is", without any warranties, express or implied, //
// except where such disclaimers are legally invalid.                         //
//                                                                            //
// Copyright (c) 2024 Ilya Lakhin (Илья Александрович Лахин).                 //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

use std::{
    borrow::Cow,
    fmt::{Display, Formatter},
    ops::Range,
};

use compact_str::CompactString;
use lady_deirdre::{
    format::AnnotationPriority,
    syntax::{ErrorRef, NodeRef, NodeRule, RecoveryResult},
};

use crate::{
    analysis::DiagnosticsDepth,
    runtime::{ops::OperatorKind, ScriptOrigin, ScriptType, TypeFamily, TypeHint, TypeMeta},
    syntax::{PolyRefOrigin, ScriptDoc, ScriptNode, SpanBounds},
};

/// A classification of module diagnostic issues.
///
/// Each [ModuleIssue](crate::analysis::ModuleIssue) object belongs to a specific
/// class, as described by this enum type.
///
/// From an IssueCode, you can obtain additional metadata about the diagnostic
/// issue, such as a short description (via the Display implementation of
/// IssueCode) or the severity of the issue.
///
/// Additionally, you can convert an IssueCode into a numeric representation,
/// which can be used to categorize diagnostics by their numeric codes in a
/// code editor.
///
/// The numeric representation is in the XYY decimal digits format, where X
/// represents the issue's [diagnostics depth](DiagnosticsDepth), and YY
/// represents the issue's "sub-code" within that depth level.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[repr(u16)]
#[non_exhaustive]
pub enum IssueCode {
    /// Syntax Error.
    ///
    /// This error indicates that the source code is not well-formed from a
    /// syntactical point of view. These types of errors must be addressed
    /// first.
    Parse = 101,

    /// Semantics Error.
    ///
    /// The specified package in the `use <package>;` import statement is
    /// unknown to the script engine.
    UnresolvedPackage = 201,
    /// Semantics Error.
    ///
    /// The component in the `use foo.<component>;` import statement is not a
    /// valid package.
    NotAPackage = 202,
    /// Semantics Error.
    ///
    /// The `break` or `continue` statement is used outside of a loop.
    OrphanedBreak = 203,
    /// Semantics Error.
    ///
    /// The function already has a parameter with the same name. Function
    /// parameter names must be unique.
    DuplicateParam = 204,
    /// Semantics Error.
    ///
    /// An attempt to use a variable that is not initialized at this point in
    /// the control flow.
    ReadUninit = 205,
    /// Semantics Error.
    ///
    /// An attempt to use an identifier that does not correspond to any known
    /// variable within this scope or a package symbol.
    UnresolvedIdent = 206,
    /// Semantics Error.
    ///
    /// Invalid integer literal format.
    IntParse = 207,
    /// Semantics Error.
    ///
    /// Invalid floating-point literal format.
    FloatParse = 208,
    /// Semantics Warning.
    ///
    /// This statement is not reachable during the execution of the source code.
    /// It is considered dead code. Consider removing this statement or
    /// commenting it out.
    UnreachableStatement = 209,
    /// Semantics Warning.
    ///
    /// This match arm is not reachable during the execution of the source code.
    /// It is considered dead code. Consider removing this match arm or
    /// commenting it out.
    UnreachableArm = 210,
    /// Semantics Warning.
    ///
    /// The `struct` declaration already contains a field with the same name.
    DuplicateEntry = 211,
    /// Semantics Warning.
    ///
    /// An attempt to assign to an orphaned literal. This assignment is
    /// semantically meaningless.
    LiteralAssignment = 212,

    /// Semantics Warning.
    ///
    /// The provided type does not meet the formal requirements of the
    /// specification.
    TypeMismatch = 301,
    /// Semantics Warning.
    ///
    /// An attempt to index into a nil object.
    NilIndex = 302,
    /// Semantics Warning.
    ///
    /// An attempt to index by an expression that is neither a number nor a
    /// range of integers.
    IndexTypeMismatch = 303,
    /// Semantics Warning.
    ///
    /// The object does not implement the specified operator.
    UndefinedOperator = 304,
    /// Semantics Warning.
    ///
    /// The object does not implement the Display operator.
    UndefinedDisplay = 305,
    /// Semantics Warning.
    ///
    /// The function requires either more or fewer arguments.
    CallArityMismatch = 306,
    /// Semantics Warning.
    ///
    /// Expected a function with a different number of parameters.
    FnArityMismatch = 307,
    /// Semantics Warning.
    ///
    /// The function's result type is different from the expected type.
    ResultMismatch = 308,
    /// Semantics Warning.
    ///
    /// The object does not have the specified field.
    UnknownComponent = 309,
    /// Semantics Warning.
    ///
    /// The function has inconsistent return points. Some branches return
    /// non-nil values, while others return nil values. This issue likely
    /// indicates that a trailing `return <expr>;` statement is missing.
    InconsistentReturns = 310,
}

impl Display for IssueCode {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        let message = match self {
            Self::Parse => "Parse error.",

            Self::UnresolvedPackage => "Unresolved import.",
            Self::NotAPackage => "Importing a component that is not a package.",
            Self::OrphanedBreak => "Break outside of a loop.",
            Self::DuplicateParam => "Duplicate function parameter name.",
            Self::ReadUninit => "Use of possibly uninitialized variable.",
            Self::UnresolvedIdent => "Unresolved reference.",
            Self::IntParse => "Invalid integer literal.",
            Self::FloatParse => "Invalid float literal.",
            Self::UnreachableStatement => "Unreachable statement.",
            Self::UnreachableArm => "Unreachable match arm.",
            Self::DuplicateEntry => "Duplicate struct entry.",
            Self::LiteralAssignment => "Assignment to literal is meaningless.",

            Self::TypeMismatch => "Type mismatch.",
            Self::NilIndex => "Index operator is not applicable to nil type.",
            Self::IndexTypeMismatch => "Index type must be an integer or an integer range.",
            Self::UndefinedOperator => "Unresolved operator.",
            Self::UndefinedDisplay => "Type does not implement the Display operator.",
            Self::CallArityMismatch => "Call arity mismatch.",
            Self::FnArityMismatch => "Function arity mismatch.",
            Self::ResultMismatch => "Function result type mismatch.",
            Self::UnknownComponent => "Unknown field.",
            Self::InconsistentReturns => "Missing trailing return statement.",
        };

        formatter.write_str(message)
    }
}

impl IssueCode {
    /// Returns the issue's [severity](IssueSeverity), which is either an error
    /// or a warning.
    #[inline(always)]
    pub fn severity(self) -> IssueSeverity {
        match self {
            Self::Parse => IssueSeverity::Error,

            Self::UnresolvedPackage => IssueSeverity::Error,
            Self::NotAPackage => IssueSeverity::Error,
            Self::OrphanedBreak => IssueSeverity::Error,
            Self::DuplicateParam => IssueSeverity::Error,
            Self::ReadUninit => IssueSeverity::Error,
            Self::UnresolvedIdent => IssueSeverity::Error,
            Self::IntParse => IssueSeverity::Error,
            Self::FloatParse => IssueSeverity::Error,
            Self::UnreachableStatement => IssueSeverity::Warning,
            Self::UnreachableArm => IssueSeverity::Warning,
            Self::DuplicateEntry => IssueSeverity::Warning,
            Self::LiteralAssignment => IssueSeverity::Warning,

            Self::TypeMismatch => IssueSeverity::Warning,
            Self::NilIndex => IssueSeverity::Warning,
            Self::IndexTypeMismatch => IssueSeverity::Warning,
            Self::UndefinedOperator => IssueSeverity::Warning,
            Self::UndefinedDisplay => IssueSeverity::Warning,
            Self::CallArityMismatch => IssueSeverity::Warning,
            Self::FnArityMismatch => IssueSeverity::Warning,
            Self::ResultMismatch => IssueSeverity::Warning,
            Self::UnknownComponent => IssueSeverity::Warning,
            Self::InconsistentReturns => IssueSeverity::Warning,
        }
    }

    /// Returns the level of semantic analysis depth at which this issue was inferred.
    ///
    /// See [DiagnosticsDepth] for details.
    #[inline(always)]
    pub fn depth(self) -> DiagnosticsDepth {
        (self as u16 / 100) as DiagnosticsDepth
    }
}

/// The severity level of the source code diagnostics.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[repr(u8)]
pub enum IssueSeverity {
    /// Hard errors. The analyzer is quite confident that these kinds of
    /// diagnostic issues would lead to runtime errors.
    Error = 1 << 0,

    /// These types of diagnostic issues are likely to lead to runtime problems.
    /// However, the analyzer is not confident enough to classify them as hard
    /// errors due to the dynamic nature of the script execution model.
    Warning = 1 << 1,
}

impl Display for IssueSeverity {
    #[inline(always)]
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            IssueSeverity::Error => formatter.write_str("error"),
            IssueSeverity::Warning => formatter.write_str("warning"),
        }
    }
}

impl IssueSeverity {
    #[inline(always)]
    pub(crate) fn priority(self) -> AnnotationPriority {
        match self {
            Self::Error => AnnotationPriority::Primary,
            Self::Warning => AnnotationPriority::Note,
        }
    }
}

#[derive(Clone, PartialEq, Eq, Hash)]
pub(crate) enum ScriptIssue {
    Parse {
        error_ref: ErrorRef,
    },

    UnresolvedPackage {
        base: &'static TypeMeta,
        package_ref: NodeRef,
        quickfix: CompactString,
    },

    NotAPackage {
        ty: TypeHint,
        package_ref: NodeRef,
    },

    OrphanedBreak {
        break_ref: NodeRef,
    },

    DuplicateParam {
        var_ref: NodeRef,
    },

    ReadUninit {
        ident_ref: NodeRef,
    },

    UnresolvedIdent {
        ident_ref: NodeRef,
        quickfix: CompactString,
        import: CompactString,
    },

    IntParse {
        number_ref: NodeRef,
    },

    FloatParse {
        number_ref: NodeRef,
    },

    UnreachableStatement {
        st_ref: NodeRef,
    },

    UnreachableArm {
        arm_ref: NodeRef,
    },

    DuplicateEntry {
        entry_key_ref: NodeRef,
    },

    LiteralAssignment {
        op_ref: NodeRef,
    },

    TypeMismatch {
        expr_ref: NodeRef,
        expected: &'static TypeFamily,
        provided: &'static TypeFamily,
    },

    NilIndex {
        op_ref: NodeRef,
    },

    IndexTypeMismatch {
        arg_ref: NodeRef,
        provided: &'static TypeFamily,
    },

    UndefinedOperator {
        op_ref: NodeRef,
        op: OperatorKind,
        receiver: &'static TypeMeta,
    },

    CallArityMismatch {
        args_ref: NodeRef,
        expected: usize,
        provided: usize,
    },

    FnArityMismatch {
        arg_ref: NodeRef,
        expected: usize,
        provided: usize,
    },

    ResultMismatch {
        arg_ref: NodeRef,
        expected: &'static TypeFamily,
        provided: &'static TypeFamily,
    },

    UnknownComponent {
        field_ref: NodeRef,
        receiver: &'static TypeMeta,
        quickfix: CompactString,
    },

    InconsistentReturns {
        fn_ref: NodeRef,
    },
}

impl ScriptIssue {
    pub(crate) fn code(&self) -> IssueCode {
        match self {
            Self::Parse { .. } => IssueCode::Parse,
            Self::UnresolvedPackage { .. } => IssueCode::UnresolvedPackage,
            Self::NotAPackage { .. } => IssueCode::NotAPackage,
            Self::OrphanedBreak { .. } => IssueCode::OrphanedBreak,
            Self::DuplicateParam { .. } => IssueCode::DuplicateParam,
            Self::ReadUninit { .. } => IssueCode::ReadUninit,
            Self::UnresolvedIdent { .. } => IssueCode::UnresolvedIdent,
            Self::IntParse { .. } => IssueCode::IntParse,
            Self::FloatParse { .. } => IssueCode::FloatParse,
            Self::UnreachableStatement { .. } => IssueCode::UnreachableStatement,
            Self::UnreachableArm { .. } => IssueCode::UnreachableArm,
            Self::DuplicateEntry { .. } => IssueCode::DuplicateEntry,
            Self::LiteralAssignment { .. } => IssueCode::LiteralAssignment,
            Self::TypeMismatch { .. } => IssueCode::TypeMismatch,
            Self::NilIndex { .. } => IssueCode::NilIndex,
            Self::IndexTypeMismatch { .. } => IssueCode::IndexTypeMismatch,
            Self::UndefinedOperator { .. } => IssueCode::UndefinedOperator,
            Self::CallArityMismatch { .. } => IssueCode::CallArityMismatch,
            Self::FnArityMismatch { .. } => IssueCode::FnArityMismatch,
            Self::ResultMismatch { .. } => IssueCode::ResultMismatch,
            Self::UnknownComponent { .. } => IssueCode::UnknownComponent,
            Self::InconsistentReturns { .. } => IssueCode::InconsistentReturns,
        }
    }

    pub(crate) fn span(&self, doc: &ScriptDoc) -> ScriptOrigin {
        match self {
            Self::Parse { error_ref, .. } => match error_ref.deref(doc) {
                Some(issue) => ScriptOrigin::from(issue.aligned_span(doc)),
                None => ScriptOrigin::invalid(error_ref.id),
            },

            Self::UnresolvedPackage { package_ref, .. } => {
                package_ref.script_origin(doc, SpanBounds::Cover)
            }

            Self::NotAPackage { package_ref, .. } => {
                package_ref.script_origin(doc, SpanBounds::Cover)
            }

            Self::OrphanedBreak { break_ref, .. } => {
                break_ref.script_origin(doc, SpanBounds::Header)
            }

            Self::DuplicateParam { var_ref, .. } => var_ref.script_origin(doc, SpanBounds::Header),

            Self::ReadUninit { ident_ref, .. } => ident_ref.script_origin(doc, SpanBounds::Cover),

            Self::UnresolvedIdent { ident_ref, .. } => {
                ident_ref.script_origin(doc, SpanBounds::Cover)
            }

            Self::IntParse { number_ref, .. } => number_ref.script_origin(doc, SpanBounds::Cover),

            Self::FloatParse { number_ref, .. } => number_ref.script_origin(doc, SpanBounds::Cover),

            Self::UnreachableStatement { st_ref, .. } => {
                st_ref.script_origin(doc, SpanBounds::Cover)
            }

            Self::UnreachableArm { arm_ref, .. } => arm_ref.script_origin(doc, SpanBounds::Cover),

            Self::DuplicateEntry { entry_key_ref, .. } => {
                entry_key_ref.script_origin(doc, SpanBounds::Cover)
            }

            Self::LiteralAssignment { op_ref, .. } => op_ref.script_origin(doc, SpanBounds::Cover),

            Self::TypeMismatch { expr_ref, .. } => expr_ref.script_origin(doc, SpanBounds::Cover),

            Self::NilIndex { op_ref, .. } => op_ref.script_origin(doc, SpanBounds::Cover),

            Self::IndexTypeMismatch { arg_ref, .. } => {
                arg_ref.script_origin(doc, SpanBounds::Cover)
            }

            Self::UndefinedOperator { op_ref, .. } => op_ref.script_origin(doc, SpanBounds::Cover),

            Self::CallArityMismatch { args_ref, .. } => {
                args_ref.script_origin(doc, SpanBounds::Cover)
            }

            Self::FnArityMismatch { arg_ref, .. } => arg_ref.script_origin(doc, SpanBounds::Header),

            Self::ResultMismatch { arg_ref, .. } => arg_ref.script_origin(doc, SpanBounds::Header),

            Self::UnknownComponent { field_ref, .. } => {
                field_ref.script_origin(doc, SpanBounds::Cover)
            }

            Self::InconsistentReturns { fn_ref, .. } => {
                fn_ref.script_origin(doc, SpanBounds::Header)
            }
        }
    }

    pub(crate) fn message(&self, doc: &ScriptDoc) -> Cow<'static, str> {
        match self {
            Self::Parse { error_ref, .. } => Self::message_parse(doc, error_ref),

            Self::UnresolvedPackage { base, quickfix, .. } => {
                match (base.is_nil(), quickfix.is_empty()) {
                    (true, true) => Cow::from("unresolved import"),
                    (false, true) => Cow::from(format!("unresolved import from {base}")),
                    (true, false) => {
                        Cow::from(format!("unresolved import. did you mean {quickfix:?}?"))
                    }
                    (false, false) => Cow::from(format!(
                        "unresolved import from {base}. did you mean {quickfix:?}?"
                    )),
                }
            }

            Self::NotAPackage { ty, .. } => Cow::from(format!("type '{ty}' is not a package")),

            Self::OrphanedBreak { break_ref, .. } => match break_ref.deref(doc) {
                Some(ScriptNode::Continue { .. }) => {
                    Cow::from("continue statement outside of a loop")
                }
                _ => Cow::from("break statement outside of a loop"),
            },

            Self::DuplicateParam { .. } => Cow::from("duplicate fn parameter name"),

            Self::ReadUninit { .. } => Cow::from("use of possibly uninitialized variable"),

            Self::UnresolvedIdent {
                quickfix, import, ..
            } => match (quickfix.is_empty(), import.is_empty()) {
                (true, true) => Cow::from("unresolved reference"),

                (false, true) => Cow::from(format!(
                    "unresolved reference. did you mean \"{quickfix}\"?"
                )),

                _ => Cow::from(format!(
                    "unresolved reference. did you mean \"{import}.{quickfix}\"?"
                )),
            },

            Self::IntParse { .. } => Cow::from("invalid integer literal"),

            Self::FloatParse { .. } => Cow::from("invalid float literal"),

            Self::UnreachableStatement { .. } => Cow::from("unreachable statement"),

            Self::UnreachableArm { .. } => Cow::from("unreachable match arm"),

            Self::DuplicateEntry { .. } => Cow::from("duplicate struct entry"),

            Self::LiteralAssignment { .. } => Cow::from("assignment to literal is meaningless"),

            Self::TypeMismatch {
                expected, provided, ..
            } => {
                let expected = TypeHint::from(*expected);
                let provided = TypeHint::from(*provided);

                Cow::from(format!(
                    "expected '{expected}' type, but '{provided}' found"
                ))
            }

            Self::NilIndex { .. } => Cow::from("nil type cannot be indexed"),

            Self::IndexTypeMismatch { provided, .. } => {
                let numeric_family = <usize>::type_meta().family();
                let range_family = <Range<usize>>::type_meta().family();

                Cow::from(format!(
                    "expected '{numeric_family}' or '{range_family}' type, but '{provided}' found",
                ))
            }

            Self::UndefinedOperator { receiver, op, .. } => {
                let receiver = TypeHint::from(*receiver);

                Cow::from(format!("'{receiver}' does not implement {op}"))
            }

            Self::CallArityMismatch {
                expected, provided, ..
            } => match *expected {
                1 => Cow::from(format!("expected 1 argument, but {provided} provided",)),

                _ => Cow::from(format!(
                    "expected {expected} arguments, but {provided} provided",
                )),
            },

            Self::FnArityMismatch {
                expected, provided, ..
            } => Cow::from(format!(
                "expected fn({expected}) function, but fn({provided}) provided",
            )),

            Self::ResultMismatch {
                expected, provided, ..
            } => {
                let expected = TypeHint::from(*expected);
                let provided = TypeHint::from(*provided);

                Cow::from(format!(
                    "expected a function with '{expected}' return type, but the function returns '{provided}'"
                ))
            }

            Self::UnknownComponent {
                receiver, quickfix, ..
            } => {
                let receiver = TypeHint::from(*receiver);

                match quickfix.is_empty() {
                    true => Cow::from(format!("unknown '{receiver}' field",)),

                    false => Cow::from(format!(
                        "unknown '{receiver}' field. did you mean {quickfix:?}?",
                    )),
                }
            }

            Self::InconsistentReturns { .. } => Cow::from("missing trailing return expression"),
        }
    }

    fn message_parse(doc: &ScriptDoc, error_ref: &ErrorRef) -> Cow<'static, str> {
        let Some(issue) = error_ref.deref(doc) else {
            return Cow::from("parse error");
        };

        match issue.context {
            ScriptNode::STRING => Cow::from("unenclosed string literal"),
            ScriptNode::MULTILINE_COMMENT => Cow::from("unenclosed comment"),
            ScriptNode::BLOCK if issue.recovery == RecoveryResult::UnexpectedEOI => {
                Cow::from("unenclosed code block")
            }
            ScriptNode::EXPR if issue.recovery == RecoveryResult::PanicRecover => {
                Cow::from("unexpected operator")
            }

            _ => {
                if Self::is_operator_rule(issue.context) {
                    return Cow::from("missing operand");
                }

                Cow::from(issue.message::<ScriptNode>(doc).to_string())
            }
        }
    }

    #[inline(always)]
    fn is_operator_rule(rule: NodeRule) -> bool {
        match rule {
            ScriptNode::UNARY_LEFT | ScriptNode::BINARY | ScriptNode::QUERY => true,
            _ => false,
        }
    }
}