Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function redefine, other small changes for functions #379

Open
wants to merge 4 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .factory/automation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions rust/parser/define/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions rust/parser/redefine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions rust/parser/typeql.pest
Original file line number Diff line number Diff line change
Expand Up @@ -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 <person> label <new_person_label>

// UNDEFINE QUERY ==============================================================
Expand Down
24 changes: 15 additions & 9 deletions rust/schema/definable/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use std::fmt::{self, Formatter, Write};
use std::{
fmt::{self, Formatter, Write},
ptr::write,
};

use crate::{
common::{identifier::Identifier, token, Span, Spanned},
Expand All @@ -20,11 +23,12 @@ pub struct Function {
span: Option<Span>,
pub signature: Signature,
pub block: FunctionBlock,
pub unparsed: String,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flag a hack.
I'm supposed to use the span for this. But then I have to pass the unparsed query in the core functions.
I'm not sure whether I want to do that today or leave this in as a hack.

}

impl Function {
pub fn new(span: Option<Span>, signature: Signature, block: FunctionBlock) -> Self {
Self { span, signature, block }
pub fn new(span: Option<Span>, signature: Signature, block: FunctionBlock, unparsed: String) -> Self {
Self { span, signature, block, unparsed }
}
}

Expand Down Expand Up @@ -66,7 +70,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(())
Expand All @@ -79,7 +83,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(())
}
Expand Down Expand Up @@ -157,8 +161,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, " }}")
}
}

Expand Down Expand Up @@ -187,8 +193,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)
}
}

Expand Down