Skip to content

Commit 59adc6d

Browse files
authored
propagate source locations on cedar syntax types (#1406)
Signed-off-by: Craig Disselkoen <[email protected]>
1 parent ebc0785 commit 59adc6d

File tree

9 files changed

+669
-413
lines changed

9 files changed

+669
-413
lines changed

cedar-policy-validator/src/cedar_schema/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub struct EntityDecl {
258258
/// Entity Types this type is allowed to be related to via the `in` relation
259259
pub member_of_types: Vec<Path>,
260260
/// Attributes this entity has
261-
pub attrs: Vec<Node<Annotated<AttrDecl>>>,
261+
pub attrs: Node<Vec<Node<Annotated<AttrDecl>>>>,
262262
/// Tag type for this entity (`None` means no tags on this entity)
263263
pub tags: Option<Node<Type>>,
264264
}
@@ -340,7 +340,7 @@ pub enum AppDecl {
340340
/// Constraints on the `principal` or `resource`
341341
PR(PRAppDecl),
342342
/// Constraints on the `context`
343-
Context(Either<Path, Vec<Node<Annotated<AttrDecl>>>>),
343+
Context(Either<Path, Node<Vec<Node<Annotated<AttrDecl>>>>>),
344344
}
345345

346346
/// An action declaration

cedar-policy-validator/src/cedar_schema/fmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<N: Display> Display for json_schema::NamespaceDefinition<N> {
5757
impl<N: Display> Display for json_schema::Type<N> {
5858
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5959
match self {
60-
json_schema::Type::Type(ty) => match ty {
60+
json_schema::Type::Type { ty, .. } => match ty {
6161
json_schema::TypeVariant::Boolean => write!(f, "__cedar::Bool"),
6262
json_schema::TypeVariant::Entity { name } => write!(f, "{name}"),
6363
json_schema::TypeVariant::EntityOrCommon { type_name } => {
@@ -69,7 +69,7 @@ impl<N: Display> Display for json_schema::Type<N> {
6969
json_schema::TypeVariant::Set { element } => write!(f, "Set < {element} >"),
7070
json_schema::TypeVariant::String => write!(f, "__cedar::String"),
7171
},
72-
json_schema::Type::CommonTypeRef { type_name } => write!(f, "{type_name}"),
72+
json_schema::Type::CommonTypeRef { type_name, .. } => write!(f, "{type_name}"),
7373
}
7474
}
7575
}

cedar-policy-validator/src/cedar_schema/grammar.lalrpop

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,15 @@ Decl: Node<Declaration> = {
125125
<t:TypeDecl> => t,
126126
}
127127

128-
// Entity := 'entity' Idents ['in' EntOrTypes] [['='] RecType] ';'
128+
// Entity := 'entity' Idents ['in' EntOrTypes] [['='] RecType] ';' <r:@R>
129129
Entity: Node<Declaration> = {
130-
<l:@L> ENTITY <ets: Idents> <ps:(IN <EntTypes>)?> <ds:("="? "{" <AttrDecls?> "}")?> <ts:(TAGS <Type>)?> ";" <r:@R>
131-
=> 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))),
130+
<l1:@L> ENTITY <ets: Idents> <ps:(IN <EntTypes>)?> <l2:@L> <ds:("="? "{" <AttrDecls?> "}")?> <r2:@R> <ts:(TAGS <Type>)?> ";" <r1:@R>
131+
=> Node::with_source_loc(Declaration::Entity(EntityDecl {
132+
names: ets,
133+
member_of_types: ps.unwrap_or_default(),
134+
attrs: Node::with_source_loc(ds.map(|ds| ds.unwrap_or_default()).unwrap_or_default(), Loc::new(l2..r2, Arc::clone(src))),
135+
tags: ts,
136+
}), Loc::new(l1..r1, Arc::clone(src))),
132137
}
133138

134139
// Action := 'action' Names ['in' QualNameOrNames]
@@ -175,18 +180,18 @@ AppDecls: Node<NonEmpty<Node<AppDecl>>> = {
175180
ds,
176181
Loc::new(l..r, Arc::clone(src)))
177182
},
178-
<l:@L> CONTEXT ":" "{" <attrs:AttrDecls?> "}" ","? <r:@R>
183+
<l1:@L> CONTEXT ":" <l2:@L> "{" <attrs:AttrDecls?> "}" <r2:@R> ","? <r1:@R>
179184
=>
180185
Node::with_source_loc(
181-
nonempty![Node::with_source_loc(AppDecl::Context(Either::Right(attrs.unwrap_or_default())), Loc::new(l..r, Arc::clone(src)))],
182-
Loc::new(l..r, Arc::clone(src))),
183-
<l:@L> CONTEXT ":" "{" <attrs:AttrDecls?> "}" "," <r:@R> <mut ds: AppDecls>
186+
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)))],
187+
Loc::new(l1..r1, Arc::clone(src))),
188+
<l1:@L> CONTEXT ":" <l2:@L> "{" <attrs:AttrDecls?> "}" <r2:@R> "," <r1:@R> <mut ds: AppDecls>
184189
=> {
185190
let (mut ds, _) = ds.into_inner();
186-
ds.insert(0, Node::with_source_loc(AppDecl::Context(Either::Right(attrs.unwrap_or_default())), Loc::new(l..r, Arc::clone(src))));
191+
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))));
187192
Node::with_source_loc(
188193
ds,
189-
Loc::new(l..r, Arc::clone(src)))
194+
Loc::new(l1..r1, Arc::clone(src)))
190195
},
191196
}
192197

0 commit comments

Comments
 (0)