Skip to content

Commit

Permalink
parse implements on object
Browse files Browse the repository at this point in the history
  • Loading branch information
obmarg committed Jan 7, 2024
1 parent 63f72a7 commit 5f148aa
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions cynic-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct ObjectDefinition {
pub name: StringId,
pub fields: Vec<NodeId>,
pub directives: Vec<DirectiveId>,
pub implements: Vec<StringId>,
}

pub struct FieldDefinition {
Expand Down
8 changes: 8 additions & 0 deletions cynic-parser/src/ast/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ impl<'a> AstReader<'a, ObjectDefinitionId> {
self.ast.lookup(self.ast.lookup(self.id).name)
}

pub fn implements_interfaces(&self) -> impl Iterator<Item = &'a str> + 'a {
self.ast
.lookup(self.id)
.implements
.iter()
.map(|id| self.ast.lookup(*id))
}

pub fn fields(&self) -> impl Iterator<Item = AstReader<'a, FieldDefinitionId>> + 'a {
self.ast
.lookup(self.id)
Expand Down
4 changes: 4 additions & 0 deletions cynic-parser/src/lexer/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pub enum Token<'a> {
#[token("null")]
Null,

#[token("implements")]
Implements,

// IntegerPart: -?(0|[1-9][0-9]*)
// FractionalPart: \\.[0-9]+
// ExponentPart: [eE][+-]?[0-9]+
Expand Down Expand Up @@ -262,6 +265,7 @@ impl fmt::Display for Token<'_> {
Token::True => "true",
Token::False => "false",
Token::Null => "null,",
Token::Implements => "implements",
};
f.write_str(message)
}
Expand Down
4 changes: 2 additions & 2 deletions cynic-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ mod tests {
fn test_basic_object() {
insta::assert_snapshot!(
parse_type_system_document(r#"
type MyType @hello {
type MyType implements Blah & Bloo @hello {
field: Whatever @hello(name: ["string"]),
other: [[Int!]]!
}"#
).to_sdl(),
@r###"
type MyType @hello {
type MyType implements Blah & Bloo @hello {
field: Whatever@hello(name: ("string"))
other: [[Int!]]!
}
Expand Down
16 changes: 14 additions & 2 deletions cynic-parser/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,21 @@ impl<'a> Pretty<'a, BoxAllocator> for NodeDisplay<'a, SchemaDefinitionId> {

impl<'a> Pretty<'a, BoxAllocator> for NodeDisplay<'a, ObjectDefinitionId> {
fn pretty(self, allocator: &'a BoxAllocator) -> pretty::DocBuilder<'a, BoxAllocator, ()> {
allocator
let mut builder = allocator
.text(format!("type {}", self.0.name()))
.append(allocator.space())
.append(allocator.space());

let interfaces = self.0.implements_interfaces().collect::<Vec<_>>();

if !interfaces.is_empty() {
builder = builder
.append(allocator.text("implements"))
.append(allocator.space())
.append(allocator.intersperse(interfaces, " & "))
.append(allocator.space());
}

builder
.append(allocator.intersperse(self.0.directives().map(NodeDisplay), allocator.line()))
.append(allocator.space())
.append(allocator.text("{"))
Expand Down
21 changes: 20 additions & 1 deletion cynic-parser/src/schema.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,29 @@ pub RootOperationTypeDefinition: RootOperationTypeDefinition = {
}

pub ObjectDefinition: ObjectDefinition = {
ty <name:Name> <directives:Directive*> <fields:FieldsDefinition?> => ObjectDefinition {
ty <name:Name> <implements:ImplementsInterfaces?> <directives:Directive*> <fields:FieldsDefinition?> => ObjectDefinition {
name,
directives,
implements: implements.unwrap_or_default(),
fields: fields.unwrap_or_default ()
}
};

ImplementsInterfaces: Vec<StringId> = {
<interfaces:ImplementsInterfaces> "&" <name:NamedType> => {
let mut interfaces = interfaces;
interfaces.push(name);
interfaces
},
implements "&"? <name:NamedType> => {
vec![name]
}
}

ImplementItem: StringId = {
"&" <name:NamedType> => name,
}

FieldsDefinition: Vec<NodeId> = {
"{" <fields:FieldDefinition+> "}" => fields
};
Expand Down Expand Up @@ -160,6 +176,7 @@ Ident: &'input str = {
true => "true",
false => "false",
null => "null",
implements => "implements",
}

extern {
Expand All @@ -183,6 +200,7 @@ extern {
"!" => lexer::Token::Exclamation,
"=" => lexer::Token::Equals,
"@" => lexer::Token::At,
"&" => lexer::Token::Ampersand,

RawIdent => lexer::Token::Identifier(<&'input str>),

Expand All @@ -200,5 +218,6 @@ extern {
true => lexer::Token::True,
false => lexer::Token::False,
null => lexer::Token::Null,
implements => lexer::Token::Implements,
}
}

0 comments on commit 5f148aa

Please sign in to comment.