@@ -26,7 +26,7 @@ use miette::{Diagnostic, LabeledSpan, SourceSpan};
26
26
use smol_str:: SmolStr ;
27
27
use thiserror:: Error ;
28
28
29
- use crate :: ast:: { self , ExprConstructionError , InputInteger , PolicyID , RestrictedExprError , Var } ;
29
+ use crate :: ast:: { self , Expr , ExprConstructionError , InputInteger , PolicyID , Var } ;
30
30
use crate :: parser:: fmt:: join_with_conjunction;
31
31
use crate :: parser:: loc:: Loc ;
32
32
use crate :: parser:: node:: Node ;
@@ -43,55 +43,36 @@ pub(crate) type RawErrorRecovery<'a> = lalr::ErrorRecovery<RawLocation, RawToken
43
43
44
44
type OwnedRawParseError = lalr:: ParseError < RawLocation , String , RawUserError > ;
45
45
46
- /// For errors during parsing
46
+ /// Errors that can occur when parsing Cedar policies or expressions.
47
+ //
48
+ // CAUTION: this type is publicly exported in `cedar-policy`.
49
+ // Don't make fields `pub`, don't make breaking changes, and use caution when
50
+ // adding public methods.
47
51
#[ derive( Clone , Debug , Diagnostic , Error , PartialEq , Eq ) ]
48
52
pub enum ParseError {
49
- /// Error from the CST parser.
53
+ /// Error from the text -> CST parser
50
54
#[ error( transparent) ]
51
55
#[ diagnostic( transparent) ]
52
56
ToCST ( #[ from] ToCSTError ) ,
53
- /// Error in the CST -> AST transform, mostly well-formedness issues.
57
+ /// Error from the CST -> AST transform
54
58
#[ error( transparent) ]
55
59
#[ diagnostic( transparent) ]
56
60
ToAST ( #[ from] ToASTError ) ,
57
- /// Error concerning restricted expressions.
58
- #[ error( transparent) ]
59
- #[ diagnostic( transparent) ]
60
- RestrictedExpr ( #[ from] RestrictedExprError ) ,
61
- /// Errors concerning parsing literals on their own
62
- #[ error( transparent) ]
63
- #[ diagnostic( transparent) ]
64
- ParseLiteral ( #[ from] ParseLiteralError ) ,
65
61
}
66
62
67
- impl ParseError {
68
- /// Extract a primary source span locating the error, if one is available.
69
- pub fn primary_source_span ( & self ) -> Option < SourceSpan > {
70
- match self {
71
- ParseError :: ToCST ( to_cst_err) => Some ( to_cst_err. primary_source_span ( ) ) ,
72
- ParseError :: ToAST ( to_ast_err) => Some ( to_ast_err. source_loc ( ) . span ) ,
73
- ParseError :: RestrictedExpr ( restricted_expr_err) => match restricted_expr_err {
74
- RestrictedExprError :: InvalidRestrictedExpression { expr, .. } => {
75
- expr. source_loc ( ) . map ( |loc| loc. span )
76
- }
77
- } ,
78
- ParseError :: ParseLiteral ( parse_lit_err) => parse_lit_err
79
- . labels ( )
80
- . and_then ( |mut it| it. next ( ) . map ( |lspan| * lspan. inner ( ) ) ) ,
81
- }
82
- }
83
- }
84
-
85
- /// Errors in the top-level parse literal entrypoint
63
+ /// Errors possible from `Literal::from_str()`
86
64
#[ derive( Debug , Clone , PartialEq , Diagnostic , Error , Eq ) ]
87
- pub enum ParseLiteralError {
88
- /// The top-level parser endpoint for parsing a literal encountered a non-literal.
89
- /// Since this can be any possible other expression, we just return it as a string.
90
- #[ error( "`{0}` is not a literal" ) ]
91
- ParseLiteral ( String ) ,
65
+ pub enum LiteralParseError {
66
+ /// Failed to parse the input
67
+ #[ error( transparent) ]
68
+ #[ diagnostic( transparent) ]
69
+ Parse ( #[ from] ParseErrors ) ,
70
+ /// Parsed successfully as an expression, but failed to construct a literal
71
+ #[ error( "invalid literal: `{0}`" ) ]
72
+ InvalidLiteral ( Expr ) ,
92
73
}
93
74
94
- /// Errors in the CST -> AST transform, mostly well-formedness issues.
75
+ /// Error from the CST -> AST transform
95
76
#[ derive( Debug , Error , Clone , PartialEq , Eq ) ]
96
77
#[ error( "{kind}" ) ]
97
78
pub struct ToASTError {
@@ -487,7 +468,7 @@ pub enum InvalidIsError {
487
468
WrongOp ( cst:: RelOp ) ,
488
469
}
489
470
490
- /// Error from the CST parser.
471
+ /// Error from the text -> CST parser
491
472
#[ derive( Clone , Debug , Error , PartialEq , Eq ) ]
492
473
pub struct ToCSTError {
493
474
err : OwnedRawParseError ,
@@ -685,6 +666,10 @@ pub fn expected_to_string(expected: &[String], config: &ExpectedTokenConfig) ->
685
666
}
686
667
687
668
/// Multiple parse errors.
669
+ //
670
+ // CAUTION: this type is publicly exported in `cedar-policy`.
671
+ // Don't make fields `pub`, don't make breaking changes, and use caution when
672
+ // adding public methods.
688
673
#[ derive( Clone , Debug , Default , PartialEq , Eq ) ]
689
674
pub struct ParseErrors ( pub Vec < ParseError > ) ;
690
675
@@ -696,13 +681,8 @@ impl ParseErrors {
696
681
ParseErrors ( Vec :: new ( ) )
697
682
}
698
683
699
- /// Constructs a new, empty `ParseErrors` with the specified capacity.
700
- pub fn with_capacity ( capacity : usize ) -> Self {
701
- ParseErrors ( Vec :: with_capacity ( capacity) )
702
- }
703
-
704
684
/// Add an error to the `ParseErrors`
705
- pub ( super ) fn push ( & mut self , err : impl Into < ParseError > ) {
685
+ pub ( crate ) fn push ( & mut self , err : impl Into < ParseError > ) {
706
686
self . 0 . push ( err. into ( ) ) ;
707
687
}
708
688
}
0 commit comments