Skip to content

Commit

Permalink
propagate source locations on cedar syntax types (#1406)
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Disselkoen <cdiss@amazon.com>
  • Loading branch information
cdisselkoen authored Jan 3, 2025
1 parent ebc0785 commit 59adc6d
Show file tree
Hide file tree
Showing 9 changed files with 669 additions and 413 deletions.
4 changes: 2 additions & 2 deletions cedar-policy-validator/src/cedar_schema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub struct EntityDecl {
/// Entity Types this type is allowed to be related to via the `in` relation
pub member_of_types: Vec<Path>,
/// Attributes this entity has
pub attrs: Vec<Node<Annotated<AttrDecl>>>,
pub attrs: Node<Vec<Node<Annotated<AttrDecl>>>>,
/// Tag type for this entity (`None` means no tags on this entity)
pub tags: Option<Node<Type>>,
}
Expand Down Expand Up @@ -340,7 +340,7 @@ pub enum AppDecl {
/// Constraints on the `principal` or `resource`
PR(PRAppDecl),
/// Constraints on the `context`
Context(Either<Path, Vec<Node<Annotated<AttrDecl>>>>),
Context(Either<Path, Node<Vec<Node<Annotated<AttrDecl>>>>>),
}

/// An action declaration
Expand Down
4 changes: 2 additions & 2 deletions cedar-policy-validator/src/cedar_schema/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<N: Display> Display for json_schema::NamespaceDefinition<N> {
impl<N: Display> Display for json_schema::Type<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
json_schema::Type::Type(ty) => match ty {
json_schema::Type::Type { ty, .. } => match ty {
json_schema::TypeVariant::Boolean => write!(f, "__cedar::Bool"),
json_schema::TypeVariant::Entity { name } => write!(f, "{name}"),
json_schema::TypeVariant::EntityOrCommon { type_name } => {
Expand All @@ -69,7 +69,7 @@ impl<N: Display> Display for json_schema::Type<N> {
json_schema::TypeVariant::Set { element } => write!(f, "Set < {element} >"),
json_schema::TypeVariant::String => write!(f, "__cedar::String"),
},
json_schema::Type::CommonTypeRef { type_name } => write!(f, "{type_name}"),
json_schema::Type::CommonTypeRef { type_name, .. } => write!(f, "{type_name}"),
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions cedar-policy-validator/src/cedar_schema/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ Decl: Node<Declaration> = {
<t:TypeDecl> => t,
}

// Entity := 'entity' Idents ['in' EntOrTypes] [['='] RecType] ';'
// Entity := 'entity' Idents ['in' EntOrTypes] [['='] RecType] ';' <r:@R>
Entity: Node<Declaration> = {
<l:@L> ENTITY <ets: Idents> <ps:(IN <EntTypes>)?> <ds:("="? "{" <AttrDecls?> "}")?> <ts:(TAGS <Type>)?> ";" <r:@R>
=> Node::with_source_loc(Declaration::Entity(EntityDecl { names: ets, member_of_types: ps.unwrap_or_default(), attrs: ds.map(|ds| ds.unwrap_or_default()).unwrap_or_default(), tags: ts }), Loc::new(l..r, Arc::clone(src))),
<l1:@L> ENTITY <ets: Idents> <ps:(IN <EntTypes>)?> <l2:@L> <ds:("="? "{" <AttrDecls?> "}")?> <r2:@R> <ts:(TAGS <Type>)?> ";" <r1:@R>
=> Node::with_source_loc(Declaration::Entity(EntityDecl {
names: ets,
member_of_types: ps.unwrap_or_default(),
attrs: Node::with_source_loc(ds.map(|ds| ds.unwrap_or_default()).unwrap_or_default(), Loc::new(l2..r2, Arc::clone(src))),
tags: ts,
}), Loc::new(l1..r1, Arc::clone(src))),
}

// Action := 'action' Names ['in' QualNameOrNames]
Expand Down Expand Up @@ -175,18 +180,18 @@ AppDecls: Node<NonEmpty<Node<AppDecl>>> = {
ds,
Loc::new(l..r, Arc::clone(src)))
},
<l:@L> CONTEXT ":" "{" <attrs:AttrDecls?> "}" ","? <r:@R>
<l1:@L> CONTEXT ":" <l2:@L> "{" <attrs:AttrDecls?> "}" <r2:@R> ","? <r1:@R>
=>
Node::with_source_loc(
nonempty![Node::with_source_loc(AppDecl::Context(Either::Right(attrs.unwrap_or_default())), Loc::new(l..r, Arc::clone(src)))],
Loc::new(l..r, Arc::clone(src))),
<l:@L> CONTEXT ":" "{" <attrs:AttrDecls?> "}" "," <r:@R> <mut ds: AppDecls>
nonempty![Node::with_source_loc(AppDecl::Context(Either::Right(Node::with_source_loc(attrs.unwrap_or_default(), Loc::new(l2..r2, Arc::clone(src))))), Loc::new(l1..r1, Arc::clone(src)))],
Loc::new(l1..r1, Arc::clone(src))),
<l1:@L> CONTEXT ":" <l2:@L> "{" <attrs:AttrDecls?> "}" <r2:@R> "," <r1:@R> <mut ds: AppDecls>
=> {
let (mut ds, _) = ds.into_inner();
ds.insert(0, Node::with_source_loc(AppDecl::Context(Either::Right(attrs.unwrap_or_default())), Loc::new(l..r, Arc::clone(src))));
ds.insert(0, Node::with_source_loc(AppDecl::Context(Either::Right(Node::with_source_loc(attrs.unwrap_or_default(), Loc::new(l2..r2, Arc::clone(src))))), Loc::new(l1..r1, Arc::clone(src))));
Node::with_source_loc(
ds,
Loc::new(l..r, Arc::clone(src)))
Loc::new(l1..r1, Arc::clone(src)))
},
}

Expand Down
Loading

0 comments on commit 59adc6d

Please sign in to comment.