From 63f8528a9fb269b0e397e11d005ee078d7afee5d Mon Sep 17 00:00:00 2001 From: Krishnan Govindraj Date: Mon, 11 Nov 2024 19:36:16 +0000 Subject: [PATCH 1/4] Functions know their unparsed --- rust/parser/define/function.rs | 4 ++-- rust/schema/definable/function.rs | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/rust/parser/define/function.rs b/rust/parser/define/function.rs index 22ea8445..e7114246 100644 --- a/rust/parser/define/function.rs +++ b/rust/parser/define/function.rs @@ -25,13 +25,13 @@ use crate::{ pub(in crate::parser) fn visit_definition_function(node: Node<'_>) -> Function { debug_assert_eq!(node.as_rule(), Rule::definition_function); let span = node.span(); + let unparsed = node.as_span().as_str().to_owned(); let mut children = node.into_children(); - children.skip_expected(Rule::FUN); let signature = visit_function_signature(children.consume_expected(Rule::function_signature)); let block = visit_function_block(children.consume_expected(Rule::function_block)); debug_assert_eq!(children.try_consume_any(), None); - Function::new(span, signature, block) + Function::new(span, signature, block, unparsed) } pub fn visit_function_block(node: Node<'_>) -> FunctionBlock { diff --git a/rust/schema/definable/function.rs b/rust/schema/definable/function.rs index 05f44746..a51f5094 100644 --- a/rust/schema/definable/function.rs +++ b/rust/schema/definable/function.rs @@ -20,11 +20,12 @@ pub struct Function { span: Option, pub signature: Signature, pub block: FunctionBlock, + pub unparsed: String, } impl Function { - pub fn new(span: Option, signature: Signature, block: FunctionBlock) -> Self { - Self { span, signature, block } + pub fn new(span: Option, signature: Signature, block: FunctionBlock, unparsed: String) -> Self { + Self { span, signature, block, unparsed } } } From 54fc1713c1dfc72dcc1690ff62bbd2cec6c590b2 Mon Sep 17 00:00:00 2001 From: Krishnan Govindraj Date: Mon, 18 Nov 2024 17:48:43 +0000 Subject: [PATCH 2/4] Implement display for some of typeql --- rust/schema/definable/function.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rust/schema/definable/function.rs b/rust/schema/definable/function.rs index a51f5094..a26ed582 100644 --- a/rust/schema/definable/function.rs +++ b/rust/schema/definable/function.rs @@ -5,6 +5,7 @@ */ use std::fmt::{self, Formatter, Write}; +use std::ptr::write; use crate::{ common::{identifier::Identifier, token, Span, Spanned}, @@ -67,7 +68,7 @@ impl Signature { impl Pretty for Signature { fn fmt(&self, indent_level: usize, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}(", self.ident)?; - todo!("write args"); + self.args.iter().try_for_each(|arg| write!(f, "{}: {}", arg.var, arg.type_))?; write!(f, ") -> ")?; Pretty::fmt(&self.output, indent_level, f)?; Ok(()) @@ -80,7 +81,7 @@ impl fmt::Display for Signature { Pretty::fmt(self, 0, f) } else { write!(f, "{}(", self.ident)?; - todo!("write args"); + self.args.iter().try_for_each(|arg| write!(f, "{}: {}", arg.var, arg.type_))?; write!(f, ") -> {}", self.output)?; Ok(()) } @@ -158,8 +159,10 @@ impl Pretty for Stream { } impl fmt::Display for Stream { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!() + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{{ ")?; + write_joined!(f, ", ", self.types)?; + write!(f, " }}") } } @@ -188,8 +191,8 @@ impl Pretty for Single { } impl fmt::Display for Single { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!() + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write_joined!(f, ", ", self.types) } } From 7e91ed2f5bd81e57b9250fbe06ad70d7e0edc69c Mon Sep 17 00:00:00 2001 From: Krishnan Govindraj Date: Fri, 22 Nov 2024 14:08:13 +0000 Subject: [PATCH 3/4] Add function redefinable --- rust/parser/redefine.rs | 16 ++++++++++++++-- rust/parser/typeql.pest | 5 +++-- rust/schema/definable/function.rs | 6 ++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/rust/parser/redefine.rs b/rust/parser/redefine.rs index d91f454e..cbf8c5a6 100644 --- a/rust/parser/redefine.rs +++ b/rust/parser/redefine.rs @@ -8,8 +8,11 @@ use super::{ define::type_::visit_type_capability, type_::visit_label, visit_kind, IntoChildNodes, Node, Rule, RuleMatcher, }; use crate::{ - common::Spanned, - parser::annotation::visit_annotations, + common::{error::TypeQLError, Spanned}, + parser::{ + annotation::visit_annotations, + define::{function::visit_definition_function, struct_::visit_definition_struct}, + }, query::schema::Redefine, schema::definable::{Definable, Type}, }; @@ -23,6 +26,15 @@ pub(super) fn visit_query_redefine(node: Node<'_>) -> Redefine { fn visit_redefinable(node: Node<'_>) -> Definable { debug_assert_eq!(node.as_rule(), Rule::redefinable); + let child = node.into_child(); + match child.as_rule() { + Rule::redefinable_type => visit_redefinable_kind(child), + Rule::definition_function => Definable::Function(visit_definition_function(child)), + _ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }), + } +} + +fn visit_redefinable_kind(node: Node<'_>) -> Definable { let span = node.span(); let mut children = node.into_children(); let kind = children.try_consume_expected(Rule::kind).map(visit_kind); diff --git a/rust/parser/typeql.pest b/rust/parser/typeql.pest index 9713bad5..a4065e80 100644 --- a/rust/parser/typeql.pest +++ b/rust/parser/typeql.pest @@ -255,8 +255,9 @@ struct_destructor_value = { var | struct_destructor } // REDEFINE QUERY ============================================================== -query_redefine = { REDEFINE ~ ( redefinable ~ SEMICOLON )+ } -redefinable = { kind? ~ label ~ ( annotations | type_capability ) } +query_redefine = { REDEFINE ~ ( redefinable )+ } +redefinable = { redefinable_type | definition_function } +redefinable_type = { kind? ~ label ~ ( annotations | type_capability ) ~ SEMICOLON } // TODO: add `redefine label // UNDEFINE QUERY ============================================================== diff --git a/rust/schema/definable/function.rs b/rust/schema/definable/function.rs index a26ed582..ce08c4d4 100644 --- a/rust/schema/definable/function.rs +++ b/rust/schema/definable/function.rs @@ -4,8 +4,10 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use std::fmt::{self, Formatter, Write}; -use std::ptr::write; +use std::{ + fmt::{self, Formatter, Write}, + ptr::write, +}; use crate::{ common::{identifier::Identifier, token, Span, Spanned}, From f452b7b718be95b008ce76fc41b6f95027497927 Mon Sep 17 00:00:00 2001 From: Krishnan Govindraj Date: Fri, 22 Nov 2024 17:04:01 +0000 Subject: [PATCH 4/4] Split rust tests into unit & behaviour --- .factory/automation.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.factory/automation.yml b/.factory/automation.yml index 6d5bfb44..5aa559f5 100644 --- a/.factory/automation.yml +++ b/.factory/automation.yml @@ -43,11 +43,15 @@ build: bazel test //java/query/... --test_output=errors bazel test //java/test/deployment/... --test_output=errors bazel test //java/test/behaviour/... --test_output=errors - test-rust: + test-rust-unit: image: typedb-ubuntu-22.04 command: | bazel run @typedb_dependencies//tool/bazelinstall:remote_cache_setup.sh bazel test //rust:typeql_unit_tests --test_output=errors + test-rust-behaviour: + image: typedb-ubuntu-22.04 + command: | + bazel run @typedb_dependencies//tool/bazelinstall:remote_cache_setup.sh bazel test //rust/tests/behaviour:test_behaviour --test_output=errors deploy-crate-snapshot: filter: