cedar_policy_core/parser/
err.rs

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
/*
 * Copyright Cedar Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt::{self, Display, Write};
use std::iter;
use std::ops::{Deref, DerefMut};

use either::Either;
use lalrpop_util as lalr;
use lazy_static::lazy_static;
use miette::{Diagnostic, LabeledSpan, SourceSpan};
use nonempty::NonEmpty;
use smol_str::SmolStr;
use thiserror::Error;

use crate::ast::{self, ReservedNameError};
use crate::parser::fmt::join_with_conjunction;
use crate::parser::loc::Loc;
use crate::parser::node::Node;
use crate::parser::unescape::UnescapeError;

use super::cst;

pub(crate) type RawLocation = usize;
pub(crate) type RawToken<'a> = lalr::lexer::Token<'a>;
pub(crate) type RawUserError = Node<String>;

pub(crate) type RawParseError<'a> = lalr::ParseError<RawLocation, RawToken<'a>, RawUserError>;
pub(crate) type RawErrorRecovery<'a> = lalr::ErrorRecovery<RawLocation, RawToken<'a>, RawUserError>;

type OwnedRawParseError = lalr::ParseError<RawLocation, String, RawUserError>;

/// Errors that can occur when parsing Cedar policies or expressions.
#[derive(Clone, Debug, Diagnostic, Error, PartialEq, Eq)]
pub enum ParseError {
    /// Error from the text -> CST parser
    #[error(transparent)]
    #[diagnostic(transparent)]
    ToCST(#[from] ToCSTError),
    /// Error from the CST -> AST transform
    #[error(transparent)]
    #[diagnostic(transparent)]
    ToAST(#[from] ToASTError),
}

/// Errors possible from `Literal::from_str()`
#[derive(Debug, Clone, PartialEq, Diagnostic, Error, Eq)]
pub enum LiteralParseError {
    /// Failed to parse the input
    #[error(transparent)]
    #[diagnostic(transparent)]
    Parse(#[from] ParseErrors),
    /// Parsed successfully as an expression, but failed to construct a literal
    #[error("invalid literal: {0}")]
    InvalidLiteral(ast::Expr),
}

/// Error from the CST -> AST transform
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[error("{kind}")]
pub struct ToASTError {
    kind: ToASTErrorKind,
    loc: Loc,
}

// Construct `labels` and `source_code` based on the `loc` in this
// struct; and everything else forwarded directly to `kind`.
impl Diagnostic for ToASTError {
    impl_diagnostic_from_source_loc_field!(loc);

    fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.kind.code()
    }

    fn severity(&self) -> Option<miette::Severity> {
        self.kind.severity()
    }

    fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.kind.help()
    }

    fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.kind.url()
    }

    fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
        self.kind.diagnostic_source()
    }
}

impl ToASTError {
    /// Construct a new `ToASTError`.
    pub fn new(kind: ToASTErrorKind, loc: Loc) -> Self {
        Self { kind, loc }
    }

    /// Get the error kind.
    pub fn kind(&self) -> &ToASTErrorKind {
        &self.kind
    }

    pub(crate) fn source_loc(&self) -> &Loc {
        &self.loc
    }
}

const POLICY_SCOPE_HELP: &str =
    "policy scopes must contain a `principal`, `action`, and `resource` element in that order";

/// Details about a particular kind of `ToASTError`.
//
// This is NOT a publicly exported error type.
#[derive(Debug, Diagnostic, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ToASTErrorKind {
    /// Returned when we attempt to parse a template with a conflicting id
    #[error("a template with id `{0}` already exists in the policy set")]
    DuplicateTemplateId(ast::PolicyID),
    /// Returned when we attempt to parse a policy with a conflicting id
    #[error("a policy with id `{0}` already exists in the policy set")]
    DuplicatePolicyId(ast::PolicyID),
    /// Returned when a template is encountered but a static policy is expected
    #[error(transparent)]
    #[diagnostic(transparent)]
    ExpectedStaticPolicy(#[from] parse_errors::ExpectedStaticPolicy),
    /// Returned when a static policy is encountered but a template is expected
    #[error(transparent)]
    #[diagnostic(transparent)]
    ExpectedTemplate(#[from] parse_errors::ExpectedTemplate),
    /// Returned when we attempt to parse a policy or template with duplicate or
    /// conflicting annotations
    #[error("duplicate annotation: @{0}")]
    DuplicateAnnotation(ast::AnyId),
    /// Returned when a policy contains template slots in a when/unless clause.
    /// This is not currently supported; see [RFC 3](https://github.com/cedar-policy/rfcs/pull/3).
    #[error(transparent)]
    #[diagnostic(transparent)]
    SlotsInConditionClause(#[from] parse_errors::SlotsInConditionClause),
    /// Returned when a policy is missing one of the three required scope elements
    /// (`principal`, `action`, and `resource`)
    #[error("this policy is missing the `{0}` variable in the scope")]
    #[diagnostic(help("{POLICY_SCOPE_HELP}"))]
    MissingScopeVariable(ast::Var),
    /// Returned when a policy has an extra scope element
    #[error("this policy has an extra element in the scope: {0}")]
    #[diagnostic(help("{POLICY_SCOPE_HELP}"))]
    ExtraScopeElement(cst::VariableDef),
    /// Returned when a policy uses a reserved keyword as an identifier.
    #[error("this identifier is reserved and cannot be used: {0}")]
    ReservedIdentifier(cst::Ident),
    /// Returned when a policy contains an invalid identifier.
    /// This error is not currently returned, but is here for future-proofing;
    /// see [`cst::Ident::Invalid`].
    #[error("invalid identifier: {0}")]
    InvalidIdentifier(String),
    /// Returned when a policy uses '=' as a binary operator.
    /// '=' is not an operator in Cedar; we can suggest '==' instead.
    #[error("'=' is not a valid operator in Cedar")]
    #[diagnostic(help("try using '==' instead"))]
    InvalidSingleEq,
    /// Returned when a policy uses an effect keyword beyond `permit` or `forbid`
    #[error("invalid policy effect: {0}")]
    #[diagnostic(help("effect must be either `permit` or `forbid`"))]
    InvalidEffect(cst::Ident),
    /// Returned when a policy uses a condition keyword beyond `when` or `unless`
    #[error("invalid policy condition: {0}")]
    #[diagnostic(help("condition must be either `when` or `unless`"))]
    InvalidCondition(cst::Ident),
    /// Returned when a policy uses a variable in the scope beyond `principal`,
    /// `action`, or `resource`
    #[error("found an invalid variable in the policy scope: {0}")]
    #[diagnostic(help("{POLICY_SCOPE_HELP}"))]
    InvalidScopeVariable(cst::Ident),
    /// Returned when a policy scope clause contains the wrong variable.
    /// (`principal` must be in the first clause, etc...)
    #[error("found the variable `{got}` where the variable `{expected}` must be used")]
    #[diagnostic(help("{POLICY_SCOPE_HELP}"))]
    IncorrectVariable {
        /// The variable that is expected
        expected: ast::Var,
        /// The variable that was present
        got: ast::Var,
    },
    /// Returned when a policy scope uses an operator not allowed in scopes
    #[error("invalid operator in the policy scope: {0}")]
    #[diagnostic(help("policy scope clauses can only use `==`, `in`, `is`, or `_ is _ in _`"))]
    InvalidScopeOperator(cst::RelOp),
    /// Returned when an action scope uses an operator not allowed in action scopes
    /// (special case of `InvalidScopeOperator`)
    #[error("invalid operator in the action scope: {0}")]
    #[diagnostic(help("action scope clauses can only use `==` or `in`"))]
    InvalidActionScopeOperator(cst::RelOp),
    /// Returned when the action scope clause contains an `is`
    #[error("`is` cannot appear in the action scope")]
    #[diagnostic(help("try moving `action is ..` into a `when` condition"))]
    IsInActionScope,
    /// Returned when an `is` operator is used together with `==`
    #[error("`is` cannot be used together with `==`")]
    #[diagnostic(help("try using `_ is _ in _`"))]
    IsWithEq,
    /// Returned when an entity uid used as an action does not have the type `Action`
    #[error(transparent)]
    #[diagnostic(transparent)]
    InvalidActionType(#[from] parse_errors::InvalidActionType),
    /// Returned when a condition clause is empty
    #[error("{}condition clause cannot be empty", match .0 { Some(ident) => format!("`{}` ", ident), None => "".to_string() })]
    EmptyClause(Option<cst::Ident>),
    /// Returned when membership chains do not resolve to an expression,
    /// violating an internal invariant
    #[error("internal invariant violated. Membership chain did not resolve to an expression")]
    #[diagnostic(help("please file an issue at <https://github.com/cedar-policy/cedar/issues> including the text that failed to parse"))]
    MembershipInvariantViolation,
    /// Returned for a non-parse-able string literal
    #[error("invalid string literal: {0}")]
    InvalidString(String),
    /// Returned when attempting to use an arbitrary variable name.
    /// Cedar does not support arbitrary variables.
    #[error("invalid variable: {0}")]
    #[diagnostic(help("the valid Cedar variables are `principal`, `action`, `resource`, and `context`; did you mean to enclose `{0}` in quotes to make a string?"))]
    ArbitraryVariable(SmolStr),
    /// Returned when attempting to use an invalid attribute name
    #[error("invalid attribute name: {0}")]
    #[diagnostic(help("attribute names can either be identifiers or string literals"))]
    InvalidAttribute(SmolStr),
    /// Returned when attempting to use an attribute with a namespace
    #[error("`{0}` cannot be used as an attribute as it contains a namespace")]
    PathAsAttribute(String),
    /// Returned when a policy attempts to call a method function-style
    #[error("`{0}` is a method, not a function")]
    #[diagnostic(help("use a method-style call `e.{0}(..)`"))]
    FunctionCallOnMethod(ast::UnreservedId),
    /// Returned when a policy attempts to call a function in the method style
    #[error("`{0}` is a function, not a method")]
    #[diagnostic(help("use a function-style call `{0}(..)`"))]
    MethodCallOnFunction(ast::UnreservedId),
    /// Returned when the right hand side of a `like` expression is not a constant pattern literal
    #[error("right hand side of a `like` expression must be a pattern literal, but got `{0}`")]
    InvalidPattern(String),
    /// Returned when the right hand side of a `is` expression is not an entity type name
    #[error("right hand side of an `is` expression must be an entity type name, but got `{0}`")]
    #[diagnostic(help("try using `==` to test for equality"))]
    InvalidIsType(String),
    /// Returned when an unexpected node is in the policy scope
    #[error("expected {expected}, found {got}")]
    WrongNode {
        /// What the expected AST node kind was
        expected: &'static str,
        /// What AST node was present in the policy source
        got: String,
        /// Optional free-form text with a suggestion for how to fix the problem
        #[help]
        suggestion: Option<String>,
    },
    /// Returned when a policy contains ambiguous ordering of operators.
    /// This can be resolved by using parenthesis to make order explicit
    #[error("multiple relational operators (>, ==, in, etc.) must be used with parentheses to make ordering explicit")]
    AmbiguousOperators,
    /// Returned when a policy uses the division operator (`/`), which is not supported
    #[error("division is not supported")]
    UnsupportedDivision,
    /// Returned when a policy uses the remainder/modulo operator (`%`), which is not supported
    #[error("remainder/modulo is not supported")]
    UnsupportedModulo,
    /// Any `ExpressionConstructionError` can also happen while converting CST to AST
    #[error(transparent)]
    #[diagnostic(transparent)]
    ExpressionConstructionError(#[from] ast::ExpressionConstructionError),
    /// Returned when a policy contains an integer literal that is out of range
    #[error("integer literal `{0}` is too large")]
    #[diagnostic(help("maximum allowed integer literal is `{}`", ast::InputInteger::MAX))]
    IntegerLiteralTooLarge(u64),
    /// Returned when a unary operator is chained more than 4 times in a row
    #[error("too many occurrences of `{0}`")]
    #[diagnostic(help("cannot chain more the 4 applications of a unary operator"))]
    UnaryOpLimit(ast::UnaryOp),
    /// Returned when a variable is called as a function, which is not allowed.
    /// Functions are not first class values in Cedar
    #[error("`{0}(...)` is not a valid function call")]
    #[diagnostic(help("variables cannot be called as functions"))]
    VariableCall(ast::Var),
    /// Returned when a policy attempts to call a method on a value that has no methods
    #[error("attempted to call `{0}.{1}(...)`, but `{0}` does not have any methods")]
    NoMethods(ast::Name, ast::UnreservedId),
    /// Returned when a policy attempts to call a method that does not exist
    #[error("`{id}` is not a valid method")]
    UnknownMethod {
        /// The user-provided method id
        id: ast::UnreservedId,
        /// The hint to resolve the error
        #[help]
        hint: Option<String>,
    },
    /// Returned when a policy attempts to call a function that does not exist
    #[error("`{id}` is not a valid function")]
    UnknownFunction {
        /// The user-provided function id
        id: ast::Name,
        /// The hint to resolve the error
        #[help]
        hint: Option<String>,
    },
    /// Returned when a policy attempts to write an entity literal
    #[error("invalid entity literal: {0}")]
    #[diagnostic(help("entity literals should have a form like `Namespace::User::\"alice\"`"))]
    InvalidEntityLiteral(String),
    /// Returned when an expression is the target of a function call.
    /// Functions are not first class values in Cedar
    #[error("function calls must be of the form `<name>(arg1, arg2, ...)`")]
    ExpressionCall,
    /// Returned when a policy attempts to access the fields of a value with no fields
    #[error("invalid member access `{0}.{1}`, `{0}` has no fields or methods")]
    InvalidAccess(ast::Name, SmolStr),
    /// Returned when a policy attempts to index on a fields of a value with no fields
    #[error("invalid indexing expression `{0}[\"{}\"]`, `{0}` has no fields", .1.escape_debug())]
    InvalidIndex(ast::Name, SmolStr),
    /// Returned when the contents of an indexing expression is not a string literal
    #[error("the contents of an index expression must be a string literal")]
    NonStringIndex,
    /// Returned when a user attempts to use type-constraint `:` syntax. This
    /// syntax was not adopted, but `is` can be used to write type constraints
    /// in the policy scope.
    #[error("type constraints using `:` are not supported")]
    #[diagnostic(help("try using `is` instead"))]
    TypeConstraints,
    /// Returned when a string needs to be fully normalized
    #[error("`{kind}` needs to be normalized (e.g., whitespace removed): {src}")]
    #[diagnostic(help("the normalized form is `{normalized_src}`"))]
    NonNormalizedString {
        /// The kind of string we are expecting
        kind: &'static str,
        /// The source string passed in
        src: String,
        /// The normalized form of the string
        normalized_src: String,
    },
    /// Returned when a CST node is empty during CST to AST/EST conversion.
    /// This should have resulted in an error during the text to CST
    /// conversion, which will terminate parsing. So it should be unreachable
    /// in later stages.
    #[error("internal invariant violated. Parsed data node should not be empty")]
    #[diagnostic(help("please file an issue at <https://github.com/cedar-policy/cedar/issues> including the text that failed to parse"))]
    EmptyNodeInvariantViolation,
    /// Returned when a function or method is called with the wrong arity
    #[error("call to `{name}` requires exactly {expected} argument{}, but got {got} argument{}", if .expected == &1 { "" } else { "s" }, if .got == &1 { "" } else { "s" })]
    WrongArity {
        /// Name of the function or method being called
        name: &'static str,
        /// The expected number of arguments
        expected: usize,
        /// The number of arguments present in source
        got: usize,
    },
    /// Returned when a string contains invalid escapes
    #[error(transparent)]
    #[diagnostic(transparent)]
    Unescape(#[from] UnescapeError),
    /// Returned when a policy scope has incorrect entity uids or template slots
    #[error(transparent)]
    #[diagnostic(transparent)]
    WrongEntityArgument(#[from] parse_errors::WrongEntityArgument),
    /// Returned when a policy contains a template slot other than `?principal` or `?resource`
    #[error("`{0}` is not a valid template slot")]
    #[diagnostic(help("a template slot may only be `?principal` or `?resource`"))]
    InvalidSlot(SmolStr),
    /// Returned when an entity type contains a reserved namespace or typename (as of this writing, just `__cedar`)
    #[error(transparent)]
    #[diagnostic(transparent)]
    ReservedNamespace(#[from] ReservedNameError),
    /// Returned when a policy uses `_ in _ is _` instead of `_ is _ in _` in the policy scope
    #[error("when `is` and `in` are used together, `is` must come first")]
    #[diagnostic(help("try `_ is _ in _`"))]
    InvertedIsIn,
}

impl ToASTErrorKind {
    /// Constructor for the [`ToASTErrorKind::WrongNode`] error
    pub fn wrong_node(
        expected: &'static str,
        got: impl Into<String>,
        suggestion: Option<impl Into<String>>,
    ) -> Self {
        Self::WrongNode {
            expected,
            got: got.into(),
            suggestion: suggestion.map(Into::into),
        }
    }

    /// Constructor for the [`ToASTErrorKind::WrongArity`] error
    pub fn wrong_arity(name: &'static str, expected: usize, got: usize) -> Self {
        Self::WrongArity {
            name,
            expected,
            got,
        }
    }

    /// Constructor for the [`ToASTErrorKind::SlotsInConditionClause`] error
    pub fn slots_in_condition_clause(slot: ast::Slot, clause_type: &'static str) -> Self {
        parse_errors::SlotsInConditionClause { slot, clause_type }.into()
    }

    /// Constructor for the [`ToASTErrorKind::ExpectedStaticPolicy`] error
    pub fn expected_static_policy(slot: ast::Slot) -> Self {
        parse_errors::ExpectedStaticPolicy { slot }.into()
    }

    /// Constructor for the [`ToASTErrorKind::ExpectedTemplate`] error
    pub fn expected_template() -> Self {
        parse_errors::ExpectedTemplate::new().into()
    }

    /// Constructor for the [`ToASTErrorKind::WrongEntityArgument`] error when
    /// one kind of entity argument was expected
    pub fn wrong_entity_argument_one_expected(
        expected: parse_errors::Ref,
        got: parse_errors::Ref,
    ) -> Self {
        parse_errors::WrongEntityArgument {
            expected: Either::Left(expected),
            got,
        }
        .into()
    }

    /// Constructor for the [`ToASTErrorKind::WrongEntityArgument`] error when
    /// one of two kinds of entity argument was expected
    pub fn wrong_entity_argument_two_expected(
        r1: parse_errors::Ref,
        r2: parse_errors::Ref,
        got: parse_errors::Ref,
    ) -> Self {
        let expected = Either::Right((r1, r2));
        parse_errors::WrongEntityArgument { expected, got }.into()
    }
}

/// Error subtypes for [`ToASTErrorKind`]
pub mod parse_errors {

    use std::sync::Arc;

    use super::*;

    /// Details about a `ExpectedStaticPolicy` error.
    #[derive(Debug, Clone, Diagnostic, Error, PartialEq, Eq)]
    #[error("expected a static policy, got a template containing the slot {}", slot.id)]
    #[diagnostic(help("try removing the template slot(s) from this policy"))]
    pub struct ExpectedStaticPolicy {
        /// Slot that was found (which is not valid in a static policy)
        pub(crate) slot: ast::Slot,
    }

    impl From<ast::UnexpectedSlotError> for ExpectedStaticPolicy {
        fn from(err: ast::UnexpectedSlotError) -> Self {
            match err {
                ast::UnexpectedSlotError::FoundSlot(slot) => Self { slot },
            }
        }
    }

    /// Details about a `ExpectedTemplate` error.
    #[derive(Debug, Clone, Diagnostic, Error, PartialEq, Eq)]
    #[error("expected a template, got a static policy")]
    #[diagnostic(help("a template should include slot(s) `?principal` or `?resource`"))]
    pub struct ExpectedTemplate {
        /// A private field, just so the public interface notes this as a
        /// private-fields struct and not a empty-fields struct for semver
        /// purposes (e.g., consumers cannot construct this type with
        /// `ExpectedTemplate {}`)
        _dummy: (),
    }

    impl ExpectedTemplate {
        pub(crate) fn new() -> Self {
            Self { _dummy: () }
        }
    }

    /// Details about a `SlotsInConditionClause` error.
    #[derive(Debug, Clone, Diagnostic, Error, PartialEq, Eq)]
    #[error("found template slot {} in a `{clause_type}` clause", slot.id)]
    #[diagnostic(help("slots are currently unsupported in `{clause_type}` clauses"))]
    pub struct SlotsInConditionClause {
        /// Slot that was found in a when/unless clause
        pub(crate) slot: ast::Slot,
        /// Clause type, e.g. "when" or "unless"
        pub(crate) clause_type: &'static str,
    }

    /// Details about an `InvalidActionType` error.
    #[derive(Debug, Clone, Diagnostic, Error, PartialEq, Eq)]
    #[diagnostic(help("action entities must have type `Action`, optionally in a namespace"))]
    pub struct InvalidActionType {
        pub(crate) euids: NonEmpty<Arc<ast::EntityUID>>,
    }

    impl std::fmt::Display for InvalidActionType {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            let subject = if self.euids.len() > 1 {
                "entity uids"
            } else {
                "an entity uid"
            };
            write!(f, "expected {subject} with type `Action` but got ")?;
            join_with_conjunction(f, "and", self.euids.iter(), |f, e| write!(f, "`{e}`"))
        }
    }

    /// Details about an `WrongEntityArgument` error.
    #[derive(Debug, Clone, Diagnostic, Error, PartialEq, Eq)]
    #[error("expected {}, found {got}", match .expected { Either::Left(r) => r.to_string(), Either::Right((r1, r2)) => format!("{r1} or {r2}") })]
    pub struct WrongEntityArgument {
        /// What kinds of references the given scope clause required.
        /// Some scope clauses require exactly one kind of reference, some require one of two
        pub(crate) expected: Either<Ref, (Ref, Ref)>,
        /// The kind of reference that was present in the policy
        pub(crate) got: Ref,
    }

    /// The 3 kinds of literals that can be in a policy scope
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub enum Ref {
        /// A single entity uids
        Single,
        /// A list of entity uids
        Set,
        /// A template slot
        Template,
    }

    impl std::fmt::Display for Ref {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                Ref::Single => write!(f, "single entity uid"),
                Ref::Template => write!(f, "template slot"),
                Ref::Set => write!(f, "set of entity uids"),
            }
        }
    }
}

/// Error from the text -> CST parser
#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub struct ToCSTError {
    err: OwnedRawParseError,
}

impl ToCSTError {
    /// Extract a primary source span locating the error.
    pub fn primary_source_span(&self) -> SourceSpan {
        match &self.err {
            OwnedRawParseError::InvalidToken { location } => SourceSpan::from(*location),
            OwnedRawParseError::UnrecognizedEof { location, .. } => SourceSpan::from(*location),
            OwnedRawParseError::UnrecognizedToken {
                token: (token_start, _, token_end),
                ..
            } => SourceSpan::from(*token_start..*token_end),
            OwnedRawParseError::ExtraToken {
                token: (token_start, _, token_end),
            } => SourceSpan::from(*token_start..*token_end),
            OwnedRawParseError::User { error } => error.loc.span,
        }
    }

    pub(crate) fn from_raw_parse_err(err: RawParseError<'_>) -> Self {
        Self {
            err: err.map_token(|token| token.to_string()),
        }
    }

    pub(crate) fn from_raw_err_recovery(recovery: RawErrorRecovery<'_>) -> Self {
        Self::from_raw_parse_err(recovery.error)
    }
}

impl Display for ToCSTError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.err {
            OwnedRawParseError::InvalidToken { .. } => write!(f, "invalid token"),
            OwnedRawParseError::UnrecognizedEof { .. } => write!(f, "unexpected end of input"),
            OwnedRawParseError::UnrecognizedToken {
                token: (_, token, _),
                ..
            } => write!(f, "unexpected token `{token}`"),
            OwnedRawParseError::ExtraToken {
                token: (_, token, _),
                ..
            } => write!(f, "extra token `{token}`"),
            OwnedRawParseError::User { error } => write!(f, "{error}"),
        }
    }
}

impl Diagnostic for ToCSTError {
    fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
        let primary_source_span = self.primary_source_span();
        let labeled_span = match &self.err {
            OwnedRawParseError::InvalidToken { .. } => LabeledSpan::underline(primary_source_span),
            OwnedRawParseError::UnrecognizedEof { expected, .. } => LabeledSpan::new_with_span(
                expected_to_string(expected, &POLICY_TOKEN_CONFIG),
                primary_source_span,
            ),
            OwnedRawParseError::UnrecognizedToken { expected, .. } => LabeledSpan::new_with_span(
                expected_to_string(expected, &POLICY_TOKEN_CONFIG),
                primary_source_span,
            ),
            OwnedRawParseError::ExtraToken { .. } => LabeledSpan::underline(primary_source_span),
            OwnedRawParseError::User { .. } => LabeledSpan::underline(primary_source_span),
        };
        Some(Box::new(iter::once(labeled_span)))
    }
}

/// Defines configurable rules for how tokens in an `UnrecognizedToken` or
/// `UnrecognizedEof` error should be displayed to users.
#[derive(Debug)]
pub struct ExpectedTokenConfig {
    /// Defines user-friendly names for tokens used by our parser. Keys are the
    /// names of tokens as defined in the `.lalrpop` grammar file. A token may
    /// be omitted from this map if the name is already friendly enough.
    pub friendly_token_names: HashMap<&'static str, &'static str>,

    /// Some tokens defined in our grammar always cause later processing to fail.
    /// Our policy grammar defines a token for the mod operator `%`, but we
    /// reject any CST that uses the operator. To reduce confusion we filter
    /// these from the list of expected tokens in an error message.
    pub impossible_tokens: HashSet<&'static str>,

    /// Both our policy and schema grammar have a generic identifier token
    /// and some more specific identifier tokens that we use to parse specific
    /// constructs. It is very often not useful to explicitly list out all of
    /// these special identifier because the parser really just wants any
    /// generic identifier. That it would accept these does not give any
    /// useful information.
    pub special_identifier_tokens: HashSet<&'static str>,

    /// If this token is expected, then the parser expected a generic identifier, so
    /// we omit the specific identifiers favor of saying we expect an "identifier".
    pub identifier_sentinel: &'static str,

    /// Special identifiers that may be worth displaying even if the parser
    /// wants a generic identifier. These can tokens will be parsed as something
    /// other than an identifier when they occur as the first token in an
    /// expression (or a type, in the case of the schema grammar).
    pub first_set_identifier_tokens: HashSet<&'static str>,

    /// If this token is expected, then the parser was looking to start parsing
    /// an expression (or type, in the schema). We know that we should report the
    /// tokens that aren't parsed as identifiers at the start of an expression.
    pub first_set_sentinel: &'static str,
}

lazy_static! {
    static ref POLICY_TOKEN_CONFIG: ExpectedTokenConfig = ExpectedTokenConfig {
        friendly_token_names: HashMap::from([
            ("TRUE", "`true`"),
            ("FALSE", "`false`"),
            ("IF", "`if`"),
            ("PERMIT", "`permit`"),
            ("FORBID", "`forbid`"),
            ("WHEN", "`when`"),
            ("UNLESS", "`unless`"),
            ("IN", "`in`"),
            ("HAS", "`has`"),
            ("LIKE", "`like`"),
            ("IS", "`is`"),
            ("THEN", "`then`"),
            ("ELSE", "`else`"),
            ("PRINCIPAL", "`principal`"),
            ("ACTION", "`action`"),
            ("RESOURCE", "`resource`"),
            ("CONTEXT", "`context`"),
            ("PRINCIPAL_SLOT", "`?principal`"),
            ("RESOURCE_SLOT", "`?resource`"),
            ("IDENTIFIER", "identifier"),
            ("NUMBER", "number"),
            ("STRINGLIT", "string literal"),
        ]),
        impossible_tokens: HashSet::from(["\"=\"", "\"%\"", "\"/\"", "OTHER_SLOT"]),
        special_identifier_tokens: HashSet::from([
            "PERMIT",
            "FORBID",
            "WHEN",
            "UNLESS",
            "IN",
            "HAS",
            "LIKE",
            "IS",
            "THEN",
            "ELSE",
            "PRINCIPAL",
            "ACTION",
            "RESOURCE",
            "CONTEXT",
        ]),
        identifier_sentinel: "IDENTIFIER",
        first_set_identifier_tokens: HashSet::from(["TRUE", "FALSE", "IF"]),
        first_set_sentinel: "\"!\"",
    };
}

/// Format lalrpop expected error messages
pub fn expected_to_string(expected: &[String], config: &ExpectedTokenConfig) -> Option<String> {
    let mut expected = expected
        .iter()
        .filter(|e| !config.impossible_tokens.contains(e.as_str()))
        .map(|e| e.as_str())
        .collect::<BTreeSet<_>>();
    if expected.contains(config.identifier_sentinel) {
        for token in config.special_identifier_tokens.iter() {
            expected.remove(*token);
        }
        if !expected.contains(config.first_set_sentinel) {
            for token in config.first_set_identifier_tokens.iter() {
                expected.remove(*token);
            }
        }
    }
    if expected.is_empty() {
        return None;
    }

    let mut expected_string = "expected ".to_owned();
    // PANIC SAFETY Shouldn't be `Err` since we're writing strings to a string
    #[allow(clippy::expect_used)]
    join_with_conjunction(
        &mut expected_string,
        "or",
        expected,
        |f, token| match config.friendly_token_names.get(token) {
            Some(friendly_token_name) => write!(f, "{}", friendly_token_name),
            None => write!(f, "{}", token.replace('"', "`")),
        },
    )
    .expect("failed to format expected tokens");
    Some(expected_string)
}

/// Represents one or more [`ParseError`]s encountered when parsing a policy or
/// template.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParseErrors(NonEmpty<ParseError>);

impl ParseErrors {
    /// Construct a `ParseErrors` with a single element
    pub(crate) fn singleton(err: impl Into<ParseError>) -> Self {
        Self(NonEmpty::singleton(err.into()))
    }

    /// Construct a new `ParseErrors` with at least one element
    pub(crate) fn new(first: ParseError, rest: impl IntoIterator<Item = ParseError>) -> Self {
        Self(NonEmpty {
            head: first,
            tail: rest.into_iter().collect::<Vec<_>>(),
        })
    }

    /// Construct a new `ParseErrors` from another `NonEmpty` type
    pub(crate) fn new_from_nonempty(errs: NonEmpty<ParseError>) -> Self {
        Self(errs)
    }

    pub(crate) fn from_iter(i: impl IntoIterator<Item = ParseError>) -> Option<Self> {
        NonEmpty::collect(i).map(Self::new_from_nonempty)
    }

    /// Flatten a `Vec<ParseErrors>` into a single `ParseErrors`, returning
    /// `None` if the input vector is empty.
    pub(crate) fn flatten(v: Vec<ParseErrors>) -> Option<Self> {
        let (first, rest) = v.split_first()?;
        let mut first = first.clone();
        rest.iter()
            .for_each(|errs| first.extend(errs.iter().cloned()));
        Some(first)
    }

    /// If there are any `Err`s in the input, this function will return a
    /// combined version of all errors. Otherwise, it will return a vector of
    /// all the `Ok` values.
    pub(crate) fn transpose<T>(
        i: impl IntoIterator<Item = Result<T, ParseErrors>>,
    ) -> Result<Vec<T>, Self> {
        let mut errs = vec![];
        let oks: Vec<_> = i
            .into_iter()
            .filter_map(|r| r.map_err(|e| errs.push(e)).ok())
            .collect();
        if let Some(combined_errs) = Self::flatten(errs) {
            Err(combined_errs)
        } else {
            Ok(oks)
        }
    }
}

impl Display for ParseErrors {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.first()) // intentionally showing only the first error; see #326
    }
}

impl std::error::Error for ParseErrors {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.first().source()
    }

    #[allow(deprecated)]
    fn description(&self) -> &str {
        self.first().description()
    }

    #[allow(deprecated)]
    fn cause(&self) -> Option<&dyn std::error::Error> {
        self.first().cause()
    }
}

// Except for `.related()`, everything else is forwarded to the first error.
// This ensures that users who only use `Display`, `.code()`, `.labels()` etc, still get rich
// information for the first error, even if they don't realize there are multiple errors here.
// See #326.
impl Diagnostic for ParseErrors {
    fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
        // the .related() on the first error, and then the 2nd through Nth errors (but not their own .related())
        let mut errs = self.iter().map(|err| err as &dyn Diagnostic);
        errs.next().map(move |first_err| match first_err.related() {
            Some(first_err_related) => Box::new(first_err_related.chain(errs)),
            None => Box::new(errs) as Box<dyn Iterator<Item = _>>,
        })
    }

    fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.first().code()
    }

    fn severity(&self) -> Option<miette::Severity> {
        self.first().severity()
    }

    fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.first().help()
    }

    fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.first().url()
    }

    fn source_code(&self) -> Option<&dyn miette::SourceCode> {
        self.first().source_code()
    }

    fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
        self.first().labels()
    }

    fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
        self.first().diagnostic_source()
    }
}

impl AsRef<NonEmpty<ParseError>> for ParseErrors {
    fn as_ref(&self) -> &NonEmpty<ParseError> {
        &self.0
    }
}

impl AsMut<NonEmpty<ParseError>> for ParseErrors {
    fn as_mut(&mut self) -> &mut NonEmpty<ParseError> {
        &mut self.0
    }
}

impl Deref for ParseErrors {
    type Target = NonEmpty<ParseError>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for ParseErrors {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Into<ParseError>> From<T> for ParseErrors {
    fn from(err: T) -> Self {
        ParseErrors::singleton(err.into())
    }
}

impl<T: Into<ParseError>> Extend<T> for ParseErrors {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        self.0.extend(iter.into_iter().map(Into::into))
    }
}

impl IntoIterator for ParseErrors {
    type Item = ParseError;
    type IntoIter = iter::Chain<iter::Once<Self::Item>, std::vec::IntoIter<Self::Item>>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<'a> IntoIterator for &'a ParseErrors {
    type Item = &'a ParseError;
    type IntoIter = iter::Chain<iter::Once<Self::Item>, std::slice::Iter<'a, ParseError>>;

    fn into_iter(self) -> Self::IntoIter {
        iter::once(&self.head).chain(self.tail.iter())
    }
}