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

TypeQL 3.0 #342

Merged
merged 62 commits into from
Jul 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
061db6a
rough undefine
dmitrii-ubskii Jun 14, 2024
6954e3a
clean up type decl module structure + fmt
dmitrii-ubskii Jun 14, 2024
ae222ca
uncomment undifine tests
dmitrii-ubskii Jun 14, 2024
63c0c61
extract schame query tests
dmitrii-ubskii Jun 14, 2024
decfa25
fix function return type parsing order
dmitrii-ubskii Jun 17, 2024
a9f00c3
higher quality generation
dmitrii-ubskii Jun 17, 2024
722801f
disambiguate PEG
dmitrii-ubskii Jun 17, 2024
f5ddb3d
visit_eof_queries
dmitrii-ubskii Jun 17, 2024
5fc6e76
future proof (Iterator::intersperse in future release)
dmitrii-ubskii Jun 17, 2024
b027d39
extract schema visitors + data stub
dmitrii-ubskii Jun 17, 2024
183f61a
relates_annos
dmitrii-ubskii Jun 17, 2024
f130050
extract data query parser mod
dmitrii-ubskii Jun 17, 2024
3a853b8
refactor and repackage modules
dmitrii-ubskii Jun 17, 2024
ec53865
statement_is + VAR
dmitrii-ubskii Jun 17, 2024
b56e8b7
(most of) expression parsing + misc cleanup
dmitrii-ubskii Jun 18, 2024
1ea021d
rest of single statements
dmitrii-ubskii Jun 18, 2024
58d53c6
expression list
dmitrii-ubskii Jun 19, 2024
65ecff0
more statements (venturing into things)
dmitrii-ubskii Jun 19, 2024
7db4364
rest of thing statements + insert & delete
dmitrii-ubskii Jun 20, 2024
fda2fb3
type statement
dmitrii-ubskii Jun 20, 2024
04f6691
lowercase non-literal rules
dmitrii-ubskii Jun 20, 2024
a71d320
stage_put
dmitrii-ubskii Jun 20, 2024
97e4429
restore top-level parsers
dmitrii-ubskii Jun 20, 2024
61a1a61
fix put
dmitrii-ubskii Jun 20, 2024
eebcf5f
stage reduce + modifier
dmitrii-ubskii Jun 21, 2024
0ae752b
restore old match tests
dmitrii-ubskii Jun 21, 2024
b0e89b0
restructure + fndefs
dmitrii-ubskii Jun 25, 2024
b3aa7f5
fetch
dmitrii-ubskii Jun 25, 2024
5420788
undefine, redefine WIP
dmitrii-ubskii Jun 27, 2024
e6223eb
Freeze grammar, parse define
dmitrii-ubskii Jul 1, 2024
44a1438
split parser/schema into define & undefine
dmitrii-ubskii Jul 1, 2024
e8a54a2
type_::{List, Optional}
dmitrii-ubskii Jul 1, 2024
6e3ccbd
undefine
dmitrii-ubskii Jul 1, 2024
9ddd409
redefine + subkey
dmitrii-ubskii Jul 1, 2024
50afb2e
restore data queries + function def
dmitrii-ubskii Jul 1, 2024
cb834c0
update
dmitrii-ubskii Jul 1, 2024
a43f317
schema test: parsing & formatting (no builders yet)
dmitrii-ubskii Jul 1, 2024
8251e1a
match query tests
dmitrii-ubskii Jul 2, 2024
02c8233
rest of handwritten test + refactor
dmitrii-ubskii Jul 2, 2024
5b52a34
more test refactoring
dmitrii-ubskii Jul 3, 2024
4c06b91
merged dependencies
dmitrii-ubskii Jul 4, 2024
ac4f885
refactor + WIP for HIR
dmitrii-ubskii Jul 5, 2024
d9db7d5
rename type_::TypeConstraintBase => type_::ConstraintBase
dmitrii-ubskii Jul 8, 2024
3e7dab5
reintroduce enum_getter!
dmitrii-ubskii Jul 8, 2024
496162a
remove temp allowances in crate root
dmitrii-ubskii Jul 8, 2024
bd935f1
cleanup eof_FOO rules (query, label, fun, and struct only)
dmitrii-ubskii Jul 8, 2024
4eeca3d
Result in parse_string
dmitrii-ubskii Jul 8, 2024
612215b
unmerge label, identifer, variable
dmitrii-ubskii Jul 8, 2024
26459bd
dissolve reduce_limited + fmt
dmitrii-ubskii Jul 8, 2024
3c19088
reduce_all => reduce_value
dmitrii-ubskii Jul 8, 2024
19019bd
statement_anon_relation => statement_relation_anonymous
dmitrii-ubskii Jul 8, 2024
b0c71f7
Make HAS and LINKS optional in delete
dmitrii-ubskii Jul 8, 2024
1357427
unreserve CHECK & FIRST
dmitrii-ubskii Jul 8, 2024
e2d09b5
annotation_kind => annotation_category
dmitrii-ubskii Jul 8, 2024
9241a06
query_stage_final => query_stage_terminal
dmitrii-ubskii Jul 8, 2024
c0734d2
(query_)data => pipeline
dmitrii-ubskii Jul 8, 2024
10d61fb
make stage available at query::stage
dmitrii-ubskii Jul 8, 2024
316947c
value category => value tag
dmitrii-ubskii Jul 8, 2024
f6f2ed9
fix list range syntax (a:b => a..b)
dmitrii-ubskii Jul 8, 2024
bfe1b1e
fill in expr formatting
dmitrii-ubskii Jul 8, 2024
08ec0d9
maven update
dmitrii-ubskii Jul 8, 2024
3fca1d1
checkstyle + fmt
dmitrii-ubskii Jul 8, 2024
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
Prev Previous commit
Next Next commit
fetch
dmitrii-ubskii committed Jun 25, 2024
commit b3aa7f5f2fbc932c9a726a548e118b0615482e51
File renamed without changes.
21 changes: 17 additions & 4 deletions rust/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -4,16 +4,29 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use crate::identifier::{Identifier, Variable};

#[macro_export]
macro_rules! typeql_define {
macro_rules! define {
($($def:expr),* $(,)?) => {
$crate::query::TypeQLDefine::build(vec![$($def.into()),*])
$crate::query::Define::build(vec![$($def.into()),*])
}
}

#[macro_export]
macro_rules! typeql_undefine {
macro_rules! undefine {
($($def:expr),* $(,)?) => {
$crate::query::TypeQLUndefine::build(vec![$($def.into()),*])
$crate::query::Undefine::build(vec![$($def.into()),*])
}
}

#[macro_export]
macro_rules! match_ {
($($pattern:expr),* $(,)?) => {
$crate::query::data::stage::Match::build(vec![$($pattern.into()),*])
}
}

pub fn var(name: impl Into<Identifier>) -> Variable {
Variable::Named(None, name.into())
}
4 changes: 2 additions & 2 deletions rust/common/mod.rs
Original file line number Diff line number Diff line change
@@ -16,8 +16,8 @@ pub type Result<T = ()> = std::result::Result<T, Error>;

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LineColumn {
pub line: usize,
pub column: usize,
pub line: u32,
pub column: u32,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
18 changes: 18 additions & 0 deletions rust/identifier.rs
Original file line number Diff line number Diff line change
@@ -32,13 +32,31 @@ impl Spanned for Identifier {
}
}

impl From<&str> for Identifier {
fn from(value: &str) -> Self {
Self::new(None, value.to_owned())
}
}

impl From<String> for Identifier {
fn from(value: String) -> Self {
Self::new(None, value)
}
}

// FIXME move
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Variable {
Anonymous(Option<Span>),
Named(Option<Span>, Identifier),
}

impl fmt::Display for Variable {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
todo!()
}
}

impl Spanned for Variable {
fn span(&self) -> Option<Span> {
match self {
99 changes: 91 additions & 8 deletions rust/parser/data.rs
Original file line number Diff line number Diff line change
@@ -15,15 +15,16 @@ use crate::{
},
parser::{
schema::function::visit_definition_function, statement::thing::visit_statement_things, visit_integer_literal,
visit_var, visit_vars, RuleMatcher,
visit_label, visit_var, visit_vars, RuleMatcher,
},
pattern::{Conjunction, Disjunction, Negation, Pattern, Try},
pattern::{statement::Type, Conjunction, Disjunction, Negation, Pattern, Try},
query::{
data::{
stage::{
fetch::{Projection, ProjectionAttribute, ProjectionKeyLabel, ProjectionKeyVar},
modifier::{Filter, Limit, Offset, OrderedVariable, Sort},
reduce::{Check, Count, First, ReduceAll, Stat},
Delete, Insert, Match, Modifier, Put, Reduce, Stage,
Delete, Fetch, Insert, Match, Modifier, Put, Reduce, Stage,
},
Preamble,
},
@@ -76,7 +77,7 @@ fn visit_query_stage_final(node: Node<'_>) -> Stage {
let child = node.into_child();
match child.as_rule() {
Rule::stage_delete => Stage::Delete(visit_stage_delete(child)),
Rule::stage_fetch => visit_stage_fetch(child),
Rule::stage_fetch => Stage::Fetch(visit_stage_fetch(child)),
Rule::stage_reduce => Stage::Reduce(visit_stage_reduce(child)),
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
}
@@ -168,15 +169,97 @@ fn visit_stage_put(node: Node<'_>) -> Put {
debug_assert_eq!(node.as_rule(), Rule::stage_put);
let span = node.span();
let mut children = node.into_children();
let statement_things = children.skip_expected(Rule::PUT).consume_expected(Rule::statement_things);
let statement_things =
visit_statement_things(children.skip_expected(Rule::PUT).consume_expected(Rule::statement_things));
debug_assert_eq!(children.try_consume_any(), None);
Put::new(span, visit_statement_things(statement_things))
Put::new(span, statement_things)
}

fn visit_stage_fetch(node: Node<'_>) -> Stage {
todo!()
fn visit_stage_fetch(node: Node<'_>) -> Fetch {
debug_assert_eq!(node.as_rule(), Rule::stage_fetch);
let span = node.span();
let mut children = node.into_children();
let projections = visit_projections(children.skip_expected(Rule::FETCH).consume_expected(Rule::projections));
debug_assert_eq!(children.try_consume_any(), None);
Fetch::new(span, projections)
}

fn visit_projections(node: Node<'_>) -> Vec<Projection> {
debug_assert_eq!(node.as_rule(), Rule::projections);
node.into_children().map(visit_projection).collect()
}

fn visit_projection(node: Node<'_>) -> Projection {
debug_assert_eq!(node.as_rule(), Rule::projection);
let mut children = node.into_children();
let child = children.consume_any();
match child.as_rule() {
Rule::projection_key_var => {
let key_var = visit_projection_key_var(child);
let node = children.try_consume_any();
if let Some(n) = node {
let attrs = visit_projection_attributes(n);
Projection::Attribute(key_var, attrs)
} else {
Projection::Variable(key_var)
}
}
Rule::projection_key_label => {
let key_label = visit_projection_key_label(child);
let subquery = visit_projection_subquery(children.consume_expected(Rule::projection_subquery));
Projection::Subquery(key_label, subquery)
}
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
}
}

fn visit_projection_key_label(node: Node<'_>) -> ProjectionKeyLabel {
debug_assert_eq!(node.as_rule(), Rule::projection_key_label);
let mut children = node.into_children();
let child = children.consume_any();
match child.as_rule() {
Rule::quoted_string_literal => ProjectionKeyLabel { label: child.as_str().to_owned() },
Rule::label => ProjectionKeyLabel { label: child.as_str().to_owned() },
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
}
}

fn visit_projection_subquery(node: Node<'_>) -> DataQuery {
debug_assert_eq!(node.as_rule(), Rule::projection_subquery);
visit_query_data(node.into_child())
}

fn visit_projection_attributes(node: Node<'_>) -> Vec<ProjectionAttribute> {
debug_assert_eq!(node.as_rule(), Rule::projection_attributes);
node.into_children().map(visit_projection_attribute).collect()
}

fn visit_projection_attribute(node: Node<'_>) -> ProjectionAttribute {
debug_assert_eq!(node.as_rule(), Rule::projection_attribute);
let mut children = node.into_children();
let attribute_label = Type::Label(visit_label(children.consume_expected(Rule::label)));
let label = children.try_consume_expected(Rule::projection_key_as_label).map(visit_projection_as_label);
debug_assert!(children.try_consume_any().is_none());
ProjectionAttribute::new(attribute_label, label)
}

fn visit_projection_key_var(node: Node<'_>) -> ProjectionKeyVar {
debug_assert_eq!(node.as_rule(), Rule::projection_key_var);
let mut children = node.into_children();
let variable = visit_var(children.consume_expected(Rule::var));
let label = children.try_consume_expected(Rule::projection_key_as_label).map(visit_projection_as_label);
debug_assert!(children.try_consume_any().is_none());
ProjectionKeyVar::new(variable, label)
}

fn visit_projection_as_label(node: Node<'_>) -> ProjectionKeyLabel {
debug_assert_eq!(node.as_rule(), Rule::projection_key_as_label);
let mut children = node.into_children();
children.skip_expected(Rule::AS);
let label = visit_projection_key_label(children.consume_expected(Rule::projection_key_label));
debug_assert!(children.try_consume_any().is_none());
label
}
fn visit_stage_reduce(node: Node<'_>) -> Reduce {
debug_assert_eq!(node.as_rule(), Rule::stage_reduce);
visit_reduce(node.into_child())
4 changes: 2 additions & 2 deletions rust/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -44,8 +44,8 @@ impl Spanned for Node<'_> {
let (begin_line, begin_col) = self.as_span().start_pos().line_col();
let (end_line, end_col) = self.as_span().end_pos().line_col();
Some(Span {
begin: LineColumn { line: begin_line, column: begin_col },
end: LineColumn { line: end_line, column: end_col },
begin: LineColumn { line: begin_line as u32, column: begin_col as u32 },
end: LineColumn { line: end_line as u32, column: end_col as u32 },
})
}
}
10 changes: 5 additions & 5 deletions rust/parser/schema/mod.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ use crate::{
common::{error::TypeQLError, Spanned},
parser::schema::{function::visit_definition_function, type_::visit_definition_type},
pattern::Definable,
query::{SchemaQuery, TypeQLDefine, TypeQLUndefine},
query::{SchemaQuery, Define, Undefine},
};

pub(super) fn visit_query_schema(node: Node<'_>) -> SchemaQuery {
@@ -28,22 +28,22 @@ pub(super) fn visit_query_schema(node: Node<'_>) -> SchemaQuery {
query
}

fn visit_query_define(node: Node<'_>) -> TypeQLDefine {
fn visit_query_define(node: Node<'_>) -> Define {
debug_assert_eq!(node.as_rule(), Rule::query_define);
let span = node.span();
let mut children = node.into_children();
children.skip_expected(Rule::DEFINE);
let query = TypeQLDefine::new(visit_definables(children.consume_expected(Rule::definables)), span);
let query = Define::new(visit_definables(children.consume_expected(Rule::definables)), span);
debug_assert_eq!(children.try_consume_any(), None);
query
}

fn visit_query_undefine(node: Node<'_>) -> TypeQLUndefine {
fn visit_query_undefine(node: Node<'_>) -> Undefine {
debug_assert_eq!(node.as_rule(), Rule::query_undefine);
let span = node.span();
let mut children = node.into_children();
children.skip_expected(Rule::UNDEFINE);
let query = TypeQLUndefine::new(visit_definables(children.consume_expected(Rule::definables)), span);
let query = Undefine::new(visit_definables(children.consume_expected(Rule::definables)), span);
debug_assert_eq!(children.try_consume_any(), None);
query
}
2 changes: 1 addition & 1 deletion rust/parser/statement/mod.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ use crate::{
statement::type_::visit_statement_type, visit_label, visit_label_scoped, visit_list_label, visit_list_var,
visit_var,
},
pattern::statement::{Comparison, Statement, Type},
pattern::statement::{comparison::Comparison, Statement, Type},
};

mod single;
13 changes: 8 additions & 5 deletions rust/parser/statement/single.rs
Original file line number Diff line number Diff line change
@@ -7,14 +7,17 @@
use itertools::Itertools;

use crate::{
common::{error::TypeQLError, Spanned}, identifier::Label, parser::{
common::{error::TypeQLError, Spanned},
identifier::Label,
parser::{
expression::{visit_expression, visit_expression_function, visit_expression_value},
statement::visit_comparison,
visit_label, visit_var, visit_vars, IntoChildNodes, Node, Rule, RuleMatcher,
}, pattern::statement::{
Assignment, AssignmentPattern, ComparisonStatement, DeconstructField, InStream, Is, Statement,
StructDeconstruct,
}
},
pattern::statement::{
comparison::ComparisonStatement, Assignment, AssignmentPattern, DeconstructField, InStream, Is, Statement,
StructDeconstruct,
},
};

pub(super) fn visit_statement_single(node: Node<'_>) -> Statement {
8 changes: 6 additions & 2 deletions rust/parser/statement/thing.rs
Original file line number Diff line number Diff line change
@@ -14,8 +14,12 @@ use crate::{
visit_label, visit_var, IntoChildNodes, Node, Rule, RuleMatcher,
},
pattern::statement::{
AttributeComparisonStatement, AttributeValueStatement, Has, HasValue, Iid, Isa, IsaKind, Links, Relation,
RelationStatement, RolePlayer, Statement, ThingConstraint, ThingStatement, Type,
thing::{
isa::{Isa, IsaKind},
AttributeComparisonStatement, AttributeValueStatement, Has, HasValue, Iid, Links, Relation,
RelationStatement, RolePlayer, ThingConstraint, ThingStatement,
},
Statement, Type,
},
};

20 changes: 10 additions & 10 deletions rust/parser/test/schema_queries.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
*/

use super::assert_valid_eq_repr;
use crate::{parse_query, typeql_define, typeql_undefine};
use crate::{parse_query, define, undefine};

#[test]
fn test_define_query_with_owns_overrides() {
@@ -14,7 +14,7 @@ triangle owns side-length;
triangle-right-angled sub triangle;
triangle-right-angled owns hypotenuse-length as side-length;"#;
let parsed = parse_query(query).unwrap().into_schema().into_define();
let expected = typeql_define!(
let expected = define!(
// type_("triangle").sub("entity"),
// type_("triangle").owns("side-length"),
// type_("triangle-right-angled").sub("triangle"),
@@ -34,7 +34,7 @@ evolves-final sub evolves;
evolves-final relates from-final as from;"#;

let parsed = parse_query(query).unwrap().into_schema().into_define();
let expected = typeql_define!(
let expected = define!(
// type_("pokemon").sub("entity"),
// type_("evolves").sub("relation"),
// type_("evolves").relates("from").relates("to"),
@@ -57,7 +57,7 @@ evolves-final relates from-final as from;
pokemon plays evolves-final:from-final as from;"#;

let parsed = parse_query(query).unwrap().into_schema().into_define();
let expected = typeql_define!(
let expected = define!(
// type_("pokemon").sub("entity"),
// type_("evolves").sub("relation"),
// type_("evolves").relates("from").relates("to"),
@@ -83,7 +83,7 @@ pokemon plays evolves:from,
owns name;"#;

let parsed = parse_query(query).unwrap().into_schema().into_define();
let expected = typeql_define!(
let expected = define!(
// type_("pokemon").sub("entity"),
// type_("evolution").sub("relation"),
// type_("evolves-from").sub("role"),
@@ -109,7 +109,7 @@ pokemon plays evolves:from,
owns name;"#;

let parsed = parse_query(query).unwrap().into_schema().into_undefine();
let expected = typeql_undefine!(
let expected = undefine!(
// type_("pokemon").sub("entity"),
// type_("evolution").sub("relation"),
// type_("evolves-from").sub("role"),
@@ -128,7 +128,7 @@ concrete-type sub entity;
abstract-type sub entity @abstract;"#;
let parsed = parse_query(query).unwrap().into_schema().into_define();
let expected =
typeql_define!(/*type_("concrete-type").sub("entity"), type_("abstract-type").sub("entity").abstract_()*/);
define!(/*type_("concrete-type").sub("entity"), type_("abstract-type").sub("entity").abstract_()*/);
assert_valid_eq_repr!(expected, parsed, query);
}

@@ -138,7 +138,7 @@ fn test_define_value_type_query() {
my-type sub attribute,
value long;"#;
let parsed = parse_query(query).unwrap().into_schema().into_define();
let expected = typeql_define!(/*type_("my-type").sub("attribute").value(ValueType::Long)*/);
let expected = define!(/*type_("my-type").sub("attribute").value(ValueType::Long)*/);
assert_valid_eq_repr!(expected, parsed, query);
}

@@ -148,7 +148,7 @@ fn define_attribute_type_regex() {
digit sub attribute,
value string @regex("\d");"#;
let parsed = parse_query(query).unwrap().into_schema().into_define();
let expected = typeql_define!(/*type_("digit").sub("attribute").regex(r"\d")*/);
let expected = define!(/*type_("digit").sub("attribute").regex(r"\d")*/);
assert_valid_eq_repr!(expected, parsed, query);
}

@@ -157,6 +157,6 @@ fn undefine_attribute_type_regex() {
let query = r#"undefine
digit value string @regex("\d");"#;
let parsed = parse_query(query).unwrap().into_schema().into_undefine();
let expected = typeql_undefine!(/*type_("digit").regex(r"\d")*/);
let expected = undefine!(/*type_("digit").regex(r"\d")*/);
assert_valid_eq_repr!(expected, parsed, query);
}
Loading