From 1f6cbfbd8adf50ba237235dc785620dcdd1380b2 Mon Sep 17 00:00:00 2001 From: joshua Date: Sat, 26 Apr 2025 17:44:28 -0400 Subject: [PATCH 1/3] Allow relates overrides to have lists --- rust/parser/statement/type_.rs | 8 +++++++- rust/parser/typeql.pest | 2 +- rust/statement/type_.rs | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/rust/parser/statement/type_.rs b/rust/parser/statement/type_.rs index 09f92e5e..f1e546ed 100644 --- a/rust/parser/statement/type_.rs +++ b/rust/parser/statement/type_.rs @@ -127,7 +127,13 @@ fn visit_relates_constraint(node: Node<'_>) -> Relates { }; let specialised = if children.try_consume_expected(Rule::AS).is_some() { - Some(visit_type_ref(children.consume_expected(Rule::type_ref))) + let next = children.consume_any(); + let specialised = match next.as_rule() { + Rule::type_ref => TypeRefAny::Type(visit_type_ref(next)), + Rule::type_ref_list => TypeRefAny::List(visit_type_ref_list(next)), + _ => unreachable!("{}", TypeQLError::IllegalGrammar { input: next.to_string() }), + }; + Some(specialised) } else { None }; diff --git a/rust/parser/typeql.pest b/rust/parser/typeql.pest index 653f55db..1355d150 100644 --- a/rust/parser/typeql.pest +++ b/rust/parser/typeql.pest @@ -91,7 +91,7 @@ label_constraint = { LABEL ~ ( label_scoped | label ) } owns_constraint = { OWNS ~ type_ref_list | OWNS ~ type_ref } -relates_constraint = { RELATES ~ type_ref_list +relates_constraint = { RELATES ~ type_ref_list ~ ( AS ~ type_ref_list )? | RELATES ~ type_ref ~ ( AS ~ type_ref )? } plays_constraint = { PLAYS ~ type_ref } diff --git a/rust/statement/type_.rs b/rust/statement/type_.rs index 516c3f7c..669c1db0 100644 --- a/rust/statement/type_.rs +++ b/rust/statement/type_.rs @@ -248,11 +248,11 @@ impl fmt::Display for Owns { pub struct Relates { pub span: Option, pub related: TypeRefAny, - pub specialised: Option, + pub specialised: Option, } impl Relates { - pub fn new(span: Option, related: TypeRefAny, specialised: Option) -> Self { + pub fn new(span: Option, related: TypeRefAny, specialised: Option) -> Self { Self { span, related, specialised } } } From 1a4a7b4d97e03ea94f6783f60253d535e8290e92 Mon Sep 17 00:00:00 2001 From: joshua Date: Sat, 26 Apr 2025 18:35:37 -0400 Subject: [PATCH 2/3] Fix grammar for relates declaration --- rust/parser/define/type_.rs | 8 +++++++- rust/parser/test/mod.rs | 15 +++++++++++++++ rust/parser/typeql.pest | 2 +- rust/schema/definable/type_/capability.rs | 4 ++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/rust/parser/define/type_.rs b/rust/parser/define/type_.rs index bb28deec..2988f341 100644 --- a/rust/parser/define/type_.rs +++ b/rust/parser/define/type_.rs @@ -110,7 +110,13 @@ pub(in crate::parser) fn visit_relates_declaration(node: Node<'_>) -> Relates { _ => unreachable!("{}", TypeQLError::IllegalGrammar { input: related_label.to_string() }), }; let specialised = if children.try_consume_expected(Rule::AS).is_some() { - Some(visit_label(children.consume_expected(Rule::label))) + let node = children.consume_any(); + let specialised = match node.as_rule() { + Rule::label_list => TypeRefAny::List(visit_label_list(node)), + Rule::label => TypeRefAny::Type(TypeRef::Label(visit_label(node))), + _ => unreachable!("{}", TypeQLError::IllegalGrammar { input: node.to_string() }), + }; + Some(specialised) } else { None }; diff --git a/rust/parser/test/mod.rs b/rust/parser/test/mod.rs index 6ecb3daf..ed131908 100644 --- a/rust/parser/test/mod.rs +++ b/rust/parser/test/mod.rs @@ -125,3 +125,18 @@ reduce $c1 = count($x);"#; // let expected = typeql_match!(var("x").isa("movie")).count(); assert_valid_eq_repr!(expected, parsed, uncommented); } + +#[test] +fn tmp() { + let query = r#"define + relation light, relates color[]; + relation traffic-light, relates tlight[] as color[];"#; + let parsed = match parse_query(query) { + Ok(parsed) => parsed, + Err(err) => { + println!("{}", err); + panic!("") + } + }; + // let expected = typeql_match!(var("x").isa("movie")).count(); +} diff --git a/rust/parser/typeql.pest b/rust/parser/typeql.pest index 1355d150..50ec900a 100644 --- a/rust/parser/typeql.pest +++ b/rust/parser/typeql.pest @@ -211,7 +211,7 @@ owns_declaration = { OWNS ~ label_list | OWNS ~ label } plays_declaration = { PLAYS ~ label_scoped } -relates_declaration = { RELATES ~ label_list +relates_declaration = { RELATES ~ label_list ~ ( AS ~ label_list )? | RELATES ~ label ~ ( AS ~ label )? } diff --git a/rust/schema/definable/type_/capability.rs b/rust/schema/definable/type_/capability.rs index 772ce62f..d3569ec6 100644 --- a/rust/schema/definable/type_/capability.rs +++ b/rust/schema/definable/type_/capability.rs @@ -126,11 +126,11 @@ impl fmt::Display for Owns { pub struct Relates { pub span: Option, pub related: TypeRefAny, - pub specialised: Option