Skip to content

Commit a84767c

Browse files
Restrict by grammar refactoring type_ref and named_type (#393)
## Usage and product changes Restrict by grammar refactoring type_ref and named_type ## Implementation We notice the two distinct uses of `type_ref` and `named_type` and decide to separate them, allowing for less inapplicable cases during translation to server IR.
1 parent 0a0cac5 commit a84767c

File tree

10 files changed

+142
-113
lines changed

10 files changed

+142
-113
lines changed

rust/parser/define/struct_.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
visit_identifier, IntoChildNodes, Node, Rule, RuleMatcher,
1212
},
1313
schema::definable::{struct_::Field, Struct},
14-
type_::TypeRefAny,
14+
type_::{NamedType, NamedTypeAny, TypeRefAny},
1515
TypeRef,
1616
};
1717

@@ -42,12 +42,12 @@ fn visit_definition_struct_field(node: Node<'_>) -> Field {
4242
Field::new(span, key, type_)
4343
}
4444

45-
fn visit_struct_field_value_type(node: Node<'_>) -> TypeRefAny {
45+
fn visit_struct_field_value_type(node: Node<'_>) -> NamedTypeAny {
4646
debug_assert_eq!(node.as_rule(), Rule::struct_field_value_type);
4747
let child = node.into_child();
4848
match child.as_rule() {
49-
Rule::value_type => TypeRefAny::Type(TypeRef::Named(visit_value_type(child))),
50-
Rule::value_type_optional => TypeRefAny::Optional(visit_value_type_optional(child)),
49+
Rule::value_type => NamedTypeAny::Simple(visit_value_type(child)),
50+
Rule::value_type_optional => NamedTypeAny::Optional(visit_value_type_optional(child)),
5151
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
5252
}
5353
}

rust/parser/define/type_.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn visit_owns_declaration(node: Node<'_>) -> Owns {
8989
let owned_label = children.consume_any();
9090
let owned = match owned_label.as_rule() {
9191
Rule::label_list => TypeRefAny::List(visit_label_list(owned_label)),
92-
Rule::label => TypeRefAny::Type(TypeRef::Named(NamedType::Label(visit_label(owned_label)))),
92+
Rule::label => TypeRefAny::Type(TypeRef::Label(visit_label(owned_label))),
9393
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: owned_label.to_string() }),
9494
};
9595

@@ -106,7 +106,7 @@ pub(in crate::parser) fn visit_relates_declaration(node: Node<'_>) -> Relates {
106106
let related_label = children.consume_any();
107107
let related = match related_label.as_rule() {
108108
Rule::label_list => TypeRefAny::List(visit_label_list(related_label)),
109-
Rule::label => TypeRefAny::Type(TypeRef::Named(NamedType::Label(visit_label(related_label)))),
109+
Rule::label => TypeRefAny::Type(TypeRef::Label(visit_label(related_label))),
110110
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: related_label.to_string() }),
111111
};
112112
let specialised = if children.try_consume_expected(Rule::AS).is_some() {

rust/parser/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ fn visit_fetch_attribute(node: Node<'_>) -> FetchAttribute {
275275
let child = children.consume_any();
276276
let attribute = match child.as_rule() {
277277
Rule::label_list => TypeRefAny::List(visit_label_list(child)),
278-
Rule::label => TypeRefAny::Type(TypeRef::Named(NamedType::Label(visit_label(child)))),
278+
Rule::label => TypeRefAny::Type(TypeRef::Label(visit_label(child))),
279279
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
280280
};
281281
debug_assert_eq!(children.try_consume_any(), None);

rust/parser/statement/type_.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use crate::{
88
common::{error::TypeQLError, Spanned},
99
parser::{
1010
annotation::visit_annotations,
11-
type_::{
12-
visit_label, visit_label_scoped, visit_type_ref, visit_type_ref_any, visit_type_ref_list, visit_value_type,
13-
},
11+
type_::{visit_label, visit_label_scoped, visit_type_ref, visit_type_ref_list, visit_value_type},
1412
visit_kind, IntoChildNodes, Node, Rule, RuleMatcher,
1513
},
1614
statement::{
@@ -25,7 +23,7 @@ pub(super) fn visit_statement_type(node: Node<'_>) -> Statement {
2523
let span = node.span();
2624
let mut children = node.into_children();
2725
let kind = children.try_consume_expected(Rule::kind).map(visit_kind);
28-
let type_ = visit_type_ref_any(children.consume_expected(Rule::type_ref_any));
26+
let type_ = visit_type_ref(children.consume_expected(Rule::type_ref));
2927
let constraints = children.map(visit_type_constraint).collect();
3028
Statement::Type(Type::new(span, kind, type_, constraints))
3129
}
@@ -59,7 +57,7 @@ fn visit_sub_constraint(node: Node<'_>) -> Sub {
5957
let span = node.span();
6058
let mut children = node.into_children();
6159
let kind = visit_sub_token(children.consume_expected(Rule::SUB_));
62-
let supertype = visit_type_ref_any(children.consume_expected(Rule::type_ref_any));
60+
let supertype = visit_type_ref(children.consume_expected(Rule::type_ref));
6361
debug_assert_eq!(children.try_consume_any(), None);
6462
Sub::new(span, kind, supertype)
6563
}

rust/parser/type_.rs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,35 @@
77
use super::{visit_identifier, visit_var, IntoChildNodes, Node, Rule, RuleMatcher};
88
use crate::{
99
common::{error::TypeQLError, token, Spanned},
10-
type_::{BuiltinValueType, Label, List, NamedType, Optional, TypeRef},
11-
ScopedLabel, TypeRefAny,
10+
type_::{BuiltinValueType, Label, NamedType, NamedTypeAny, NamedTypeList, NamedTypeOptional, TypeRef, TypeRefList},
11+
ScopedLabel,
1212
};
1313

14-
pub(super) fn visit_type_ref_any(node: Node<'_>) -> TypeRefAny {
15-
debug_assert_eq!(node.as_rule(), Rule::type_ref_any);
16-
let child = node.into_child();
17-
match child.as_rule() {
18-
Rule::type_ref => TypeRefAny::Type(visit_type_ref(child)),
19-
Rule::type_ref_optional => TypeRefAny::Optional(visit_type_ref_optional(child)),
20-
Rule::type_ref_list => TypeRefAny::List(visit_type_ref_list(child)),
21-
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
22-
}
23-
}
24-
2514
pub(super) fn visit_type_ref(node: Node<'_>) -> TypeRef {
2615
debug_assert_eq!(node.as_rule(), Rule::type_ref);
2716
let child = node.into_child();
2817
match child.as_rule() {
2918
Rule::var => TypeRef::Variable(visit_var(child)),
30-
Rule::named_type => TypeRef::Named(visit_named_type(child)),
19+
Rule::label => TypeRef::Label(visit_label(child)),
20+
Rule::label_scoped => TypeRef::Scoped(visit_label_scoped(child)),
3121
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
3222
}
3323
}
3424

35-
pub(super) fn visit_type_ref_optional(node: Node<'_>) -> Optional {
36-
debug_assert_eq!(node.as_rule(), Rule::type_ref_optional);
37-
let span = node.span();
38-
let inner = visit_type_ref(node.into_child());
39-
Optional::new(span, inner)
40-
}
41-
42-
pub(super) fn visit_type_ref_list(node: Node<'_>) -> List {
25+
pub(super) fn visit_type_ref_list(node: Node<'_>) -> TypeRefList {
4326
debug_assert_eq!(node.as_rule(), Rule::type_ref_list);
4427
let span = node.span();
4528
let inner = visit_type_ref(node.into_child());
46-
List::new(span, inner)
29+
TypeRefList::new(span, inner)
4730
}
4831

49-
pub(super) fn visit_named_type_any(node: Node<'_>) -> TypeRefAny {
32+
pub(super) fn visit_named_type_any(node: Node<'_>) -> NamedTypeAny {
5033
debug_assert_eq!(node.as_rule(), Rule::named_type_any);
5134
let child = node.into_child();
5235
match child.as_rule() {
53-
Rule::named_type => TypeRefAny::Type(TypeRef::Named(visit_named_type(child))),
54-
Rule::named_type_optional => TypeRefAny::Optional(visit_named_type_optional(child)),
55-
Rule::named_type_list => TypeRefAny::List(visit_named_type_list(child)),
36+
Rule::named_type => NamedTypeAny::Simple(visit_named_type(child)),
37+
Rule::named_type_optional => NamedTypeAny::Optional(visit_named_type_optional(child)),
38+
Rule::named_type_list => NamedTypeAny::List(visit_named_type_list(child)),
5639
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
5740
}
5841
}
@@ -62,24 +45,23 @@ pub(super) fn visit_named_type(node: Node<'_>) -> NamedType {
6245
let child = node.into_child();
6346
match child.as_rule() {
6447
Rule::label => NamedType::Label(visit_label(child)),
65-
Rule::label_scoped => NamedType::Role(visit_label_scoped(child)),
6648
Rule::value_type_primitive => NamedType::BuiltinValueType(visit_value_type_primitive(child)),
6749
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
6850
}
6951
}
7052

71-
pub(super) fn visit_named_type_optional(node: Node<'_>) -> Optional {
53+
pub(super) fn visit_named_type_optional(node: Node<'_>) -> NamedTypeOptional {
7254
debug_assert_eq!(node.as_rule(), Rule::named_type_optional);
7355
let span = node.span();
74-
let inner = TypeRef::Named(visit_named_type(node.into_child()));
75-
Optional::new(span, inner)
56+
let inner = visit_named_type(node.into_child());
57+
NamedTypeOptional::new(span, inner)
7658
}
7759

78-
pub(super) fn visit_named_type_list(node: Node<'_>) -> List {
60+
pub(super) fn visit_named_type_list(node: Node<'_>) -> NamedTypeList {
7961
debug_assert_eq!(node.as_rule(), Rule::named_type_list);
8062
let span = node.span();
81-
let inner = TypeRef::Named(visit_named_type(node.into_child()));
82-
List::new(span, inner)
63+
let inner = visit_named_type(node.into_child());
64+
NamedTypeList::new(span, inner)
8365
}
8466

8567
pub(super) fn visit_label(node: Node<'_>) -> Label {
@@ -89,11 +71,11 @@ pub(super) fn visit_label(node: Node<'_>) -> Label {
8971
Label::new(span, ident)
9072
}
9173

92-
pub(super) fn visit_label_list(node: Node<'_>) -> List {
74+
pub(super) fn visit_label_list(node: Node<'_>) -> TypeRefList {
9375
debug_assert_eq!(node.as_rule(), Rule::label_list);
9476
let span = node.span();
95-
let inner = TypeRef::Named(NamedType::Label(visit_label(node.into_child())));
96-
List::new(span, inner)
77+
let inner = TypeRef::Label(visit_label(node.into_child()));
78+
TypeRefList::new(span, inner)
9779
}
9880

9981
pub(super) fn visit_label_scoped(node: Node<'_>) -> ScopedLabel {
@@ -116,11 +98,11 @@ pub(super) fn visit_value_type(node: Node<'_>) -> NamedType {
11698
}
11799
}
118100

119-
pub(super) fn visit_value_type_optional(node: Node<'_>) -> Optional {
101+
pub(super) fn visit_value_type_optional(node: Node<'_>) -> NamedTypeOptional {
120102
debug_assert_eq!(node.as_rule(), Rule::value_type_optional);
121103
let span = node.span();
122104
let inner = visit_value_type(node.into_child());
123-
Optional::new(span, TypeRef::Named(inner))
105+
NamedTypeOptional::new(span, inner)
124106
}
125107

126108
pub(super) fn visit_value_type_primitive(node: Node<'_>) -> BuiltinValueType {

rust/parser/typeql.pest

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ statement = { statement_single | statement_type | statement_thing }
7474

7575
// TYPE STATEMENTS =============================================================
7676

77-
statement_type = { kind ~ type_ref_any ~ ( type_constraint ~ ( COMMA ~ type_constraint )* ~ COMMA? )?
78-
| type_ref_any ~ type_constraint ~ ( COMMA ~ type_constraint )* ~ COMMA?
77+
statement_type = { kind ~ type_ref ~ ( type_constraint ~ ( COMMA ~ type_constraint )* ~ COMMA? )?
78+
| type_ref ~ type_constraint ~ ( COMMA ~ type_constraint )* ~ COMMA?
7979
}
8080
type_constraint = { type_constraint_base ~ annotations? }
8181
type_constraint_base = { sub_constraint | value_type_constraint | label_constraint
8282
| owns_constraint | relates_constraint | plays_constraint
8383
}
8484

85-
sub_constraint = { SUB_ ~ type_ref_any }
85+
sub_constraint = { SUB_ ~ type_ref }
8686
value_type_constraint = { VALUE ~ value_type }
8787
label_constraint = { LABEL ~ ( label_scoped | label ) }
8888
owns_constraint = { OWNS ~ type_ref_list
@@ -277,12 +277,10 @@ undefine_struct = { STRUCT ~ identifier }
277277

278278
// TYPE, LABEL AND IDENTIFIER CONSTRUCTS =======================================
279279

280-
type_ref = { named_type | var }
281-
type_ref_optional = { type_ref ~ QUESTION }
280+
type_ref = { label_scoped | label | var }
282281
type_ref_list = { type_ref ~ SQ_BRACKET_OPEN ~ SQ_BRACKET_CLOSE }
283-
type_ref_any = { type_ref_optional | type_ref_list | type_ref }
284282

285-
named_type = { value_type_primitive | label_scoped | label }
283+
named_type = { value_type_primitive | label }
286284
named_type_optional = { named_type ~ QUESTION }
287285
named_type_list = { named_type ~ SQ_BRACKET_OPEN ~ SQ_BRACKET_CLOSE }
288286
named_type_any = { named_type_optional | named_type_list | named_type }

rust/schema/definable/function.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
common::{identifier::Identifier, token, Span, Spanned},
1111
pretty::{indent, Pretty},
1212
query::stage::{reduce::Reducer, Stage},
13-
type_::TypeRefAny,
13+
type_::{NamedTypeAny, TypeRefAny},
1414
util::write_joined,
1515
variable::Variable,
1616
};
@@ -108,11 +108,11 @@ impl fmt::Display for Signature {
108108
pub struct Argument {
109109
pub span: Option<Span>,
110110
pub var: Variable,
111-
pub type_: TypeRefAny,
111+
pub type_: NamedTypeAny,
112112
}
113113

114114
impl Argument {
115-
pub fn new(span: Option<Span>, var: Variable, type_: TypeRefAny) -> Self {
115+
pub fn new(span: Option<Span>, var: Variable, type_: NamedTypeAny) -> Self {
116116
Self { span, var, type_ }
117117
}
118118
}
@@ -160,11 +160,11 @@ impl fmt::Display for Output {
160160
#[derive(Debug, Clone, PartialEq, Eq)]
161161
pub struct Stream {
162162
pub span: Option<Span>,
163-
pub types: Vec<TypeRefAny>,
163+
pub types: Vec<NamedTypeAny>,
164164
}
165165

166166
impl Stream {
167-
pub fn new(span: Option<Span>, types: Vec<TypeRefAny>) -> Self {
167+
pub fn new(span: Option<Span>, types: Vec<NamedTypeAny>) -> Self {
168168
Self { span, types }
169169
}
170170
}
@@ -188,11 +188,11 @@ impl fmt::Display for Stream {
188188
#[derive(Debug, Clone, PartialEq, Eq)]
189189
pub struct Single {
190190
pub span: Option<Span>,
191-
pub types: Vec<TypeRefAny>,
191+
pub types: Vec<NamedTypeAny>,
192192
}
193193

194194
impl Single {
195-
pub fn new(span: Option<Span>, types: Vec<TypeRefAny>) -> Self {
195+
pub fn new(span: Option<Span>, types: Vec<NamedTypeAny>) -> Self {
196196
Self { span, types }
197197
}
198198
}

rust/schema/definable/struct_.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
common::{identifier::Identifier, Span, Spanned},
1111
pretty::{indent, Pretty},
1212
token,
13-
type_::TypeRefAny,
13+
type_::{NamedTypeAny, TypeRefAny},
1414
};
1515

1616
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -64,11 +64,11 @@ impl fmt::Display for Struct {
6464
pub struct Field {
6565
pub span: Option<Span>,
6666
pub key: Identifier,
67-
pub type_: TypeRefAny,
67+
pub type_: NamedTypeAny,
6868
}
6969

7070
impl Field {
71-
pub fn new(span: Option<Span>, key: Identifier, type_: TypeRefAny) -> Self {
71+
pub fn new(span: Option<Span>, key: Identifier, type_: NamedTypeAny) -> Self {
7272
Self { span, key, type_ }
7373
}
7474
}

rust/statement/type_.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ use crate::{
1717
pub struct Type {
1818
pub span: Option<Span>,
1919
pub kind: Option<token::Kind>,
20-
pub type_: TypeRefAny,
20+
pub type_: TypeRef,
2121
pub constraints: Vec<Constraint>,
2222
}
2323

2424
impl Type {
25-
pub fn new(span: Option<Span>, kind: Option<token::Kind>, type_: TypeRefAny, constraints: Vec<Constraint>) -> Self {
25+
pub fn new(span: Option<Span>, kind: Option<token::Kind>, type_: TypeRef, constraints: Vec<Constraint>) -> Self {
2626
Self { span, kind, type_, constraints }
2727
}
2828
}
@@ -168,11 +168,11 @@ impl fmt::Display for SubKind {
168168
pub struct Sub {
169169
pub span: Option<Span>,
170170
pub kind: SubKind,
171-
pub supertype: TypeRefAny,
171+
pub supertype: TypeRef,
172172
}
173173

174174
impl Sub {
175-
pub fn new(span: Option<Span>, kind: SubKind, supertype: TypeRefAny) -> Self {
175+
pub fn new(span: Option<Span>, kind: SubKind, supertype: TypeRef) -> Self {
176176
Self { span, kind, supertype }
177177
}
178178
}

0 commit comments

Comments
 (0)