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

feat: parse object type definition #2690

Merged
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
225 changes: 225 additions & 0 deletions crates/biome_graphql_parser/src/parser/definitions/field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
use crate::parser::{
directive::DirectiveList,
is_at_name, parse_description,
parse_error::{expected_field_definition, expected_name, expected_type},
parse_name,
r#type::parse_type,
value::{is_at_string, parse_default_value},
GraphqlParser,
};
use biome_graphql_syntax::{
GraphqlSyntaxKind::{self, *},
T,
};
use biome_parser::{
parse_lists::ParseNodeList, parse_recovery::ParseRecovery, parsed_syntax::ParsedSyntax,
prelude::ParsedSyntax::*, Parser,
};

use super::is_at_definition;

#[inline]
pub(super) fn parse_fields_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_fields(p) {
return Absent;
}
let m = p.start();

p.expect(T!['{']);
FieldDefinitionList.parse_list(p);

p.expect(T!['}']);
Present(m.complete(p, GRAPHQL_FIELDS_DEFINITION))
}

#[derive(Default)]
struct FieldDefinitionList;

impl ParseNodeList for FieldDefinitionList {
type Kind = GraphqlSyntaxKind;
type Parser<'source> = GraphqlParser<'source>;

const LIST_KIND: Self::Kind = GRAPHQL_FIELD_DEFINITION_LIST;

fn parse_element(&mut self, p: &mut Self::Parser<'_>) -> ParsedSyntax {
parse_field_definition(p)
}

fn is_at_list_end(&self, p: &mut Self::Parser<'_>) -> bool {
is_at_fields_end(p)
}

fn recover(
&mut self,
p: &mut Self::Parser<'_>,
parsed_element: ParsedSyntax,
) -> biome_parser::parse_recovery::RecoveryResult {
parsed_element.or_recover(
p,
&FieldDefinitionListParseRecovery,
expected_field_definition,
)
}
}

struct FieldDefinitionListParseRecovery;

impl ParseRecovery for FieldDefinitionListParseRecovery {
type Kind = GraphqlSyntaxKind;
type Parser<'source> = GraphqlParser<'source>;
const RECOVERED_KIND: Self::Kind = GRAPHQL_BOGUS;

fn is_at_recovered(&self, p: &mut Self::Parser<'_>) -> bool {
is_at_field(p) || is_at_fields_end(p)
}
}

#[inline]
fn parse_field_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_field(p) {
return Absent;
}
let m = p.start();

// description is optional
parse_description(p).ok();
parse_name(p).or_add_diagnostic(p, expected_name);

// arguments are optional
parse_arguments_definition(p).ok();
p.expect(T![:]);

parse_type(p).or_add_diagnostic(p, expected_type);

DirectiveList.parse_list(p);

Present(m.complete(p, GRAPHQL_FIELD_DEFINITION))
}

#[inline]
fn parse_arguments_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_arguments_definition(p) {
return Absent;
}
let m = p.start();

p.expect(T!['(']);
ArgumentDefinitionList.parse_list(p);
p.expect(T![')']);

Present(m.complete(p, GRAPHQL_ARGUMENTS_DEFINITION))
}

#[derive(Default)]
struct ArgumentDefinitionList;
impl ParseNodeList for ArgumentDefinitionList {
type Kind = GraphqlSyntaxKind;
type Parser<'source> = GraphqlParser<'source>;

const LIST_KIND: Self::Kind = GRAPHQL_ARGUMENT_DEFINITION_LIST;

fn parse_element(&mut self, p: &mut Self::Parser<'_>) -> ParsedSyntax {
parse_input_value_definition(p)
}

fn is_at_list_end(&self, p: &mut Self::Parser<'_>) -> bool {
is_at_arguments_definition_end(p)
}

fn recover(
&mut self,
p: &mut Self::Parser<'_>,
parsed_element: ParsedSyntax,
) -> biome_parser::parse_recovery::RecoveryResult {
parsed_element.or_recover(
p,
&ArgumentDefinitionListParseRecovery,
expected_field_definition,
)
}
}

struct ArgumentDefinitionListParseRecovery;
impl ParseRecovery for ArgumentDefinitionListParseRecovery {
type Kind = GraphqlSyntaxKind;
type Parser<'source> = GraphqlParser<'source>;
const RECOVERED_KIND: Self::Kind = GRAPHQL_BOGUS;

fn is_at_recovered(&self, p: &mut Self::Parser<'_>) -> bool {
is_at_field(p) || is_at_fields_end(p)
}
}

#[inline]
fn parse_input_value_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_input_value_definition(p) {
return Absent;
}
let m = p.start();

// description is optional
parse_description(p).ok();
parse_name(p).or_add_diagnostic(p, expected_name);

p.expect(T![:]);

parse_type(p).or_add_diagnostic(p, expected_type);

// default value is optional
parse_default_value(p).ok();

DirectiveList.parse_list(p);

Present(m.complete(p, GRAPHQL_INPUT_VALUE_DEFINITION))
}

#[inline]
pub(super) fn is_at_fields(p: &mut GraphqlParser<'_>) -> bool {
p.at(T!['{'])
// missing opening brace
|| is_at_field(p)
}

#[inline]
pub(super) fn is_at_fields_end(p: &mut GraphqlParser<'_>) -> bool {
p.at(T!['}']) || is_at_definition(p)
}

#[inline]
fn is_at_field(p: &mut GraphqlParser<'_>) -> bool {
(is_at_name(p) && p.nth_at(1, T![:]))
// with arguments
|| (is_at_name(p) && p.nth_at(1, T!['(']))
|| (is_at_string(p) && p.nth_at(1, GRAPHQL_NAME)) && p.nth_at(2, T![:])
// missing name
|| p.at(T![:])
}

#[inline]
fn is_at_input_value_definition(p: &mut GraphqlParser<'_>) -> bool {
(is_at_name(p) && p.nth_at(1, T![:]))
|| (is_at_string(p) && p.nth_at(1, GRAPHQL_NAME) && p.nth_at(2, T![:]))
// missing name
|| p.at(T![:])
|| (is_at_string(p) && p.nth_at(1, T![:]))
// missing colon: `name String`
|| (is_at_name(p) && p.nth_at(1, GRAPHQL_NAME))
}

/// We must enforce that the arguments definition is always opened with a `(` token.
/// Otherwise, we might end up parsing a field definition as an input value definition.
/// For example:
/// ```graphql
/// name : String = "") : String
/// ```
/// In this case, the opening parenthesis is missing, the name token of an input value definition
/// is also missing. It would be to complex to disambiguate input value definitions from field.
#[inline]
fn is_at_arguments_definition(p: &mut GraphqlParser<'_>) -> bool {
p.at(T!['('])
}

#[inline]
fn is_at_arguments_definition_end(p: &mut GraphqlParser<'_>) -> bool {
p.at(T![')']) || is_at_fields_end(p)
}
92 changes: 92 additions & 0 deletions crates/biome_graphql_parser/src/parser/definitions/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use crate::parser::{
directive::is_at_directive, is_at_name, parse_error::expected_named_type,
r#type::parse_named_type, GraphqlParser,
};
use biome_graphql_syntax::{
GraphqlSyntaxKind::{self, *},
T,
};
use biome_parser::{
parse_lists::ParseSeparatedList, parse_recovery::ParseRecovery, parsed_syntax::ParsedSyntax,
prelude::ParsedSyntax::*, Parser,
};

use super::field::{is_at_fields, is_at_fields_end};

#[inline]
pub(super) fn parse_implements_interface(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_implements_interface(p) {
return Absent;
}
let m = p.start();

p.bump(T![implements]);

if p.at(T![&]) {
p.bump(T![&]);
}

ImplementsInterfaceList.parse_list(p);

Present(m.complete(p, GRAPHQL_IMPLEMENTS_INTERFACES))
}

#[derive(Default)]
struct ImplementsInterfaceList;

impl ParseSeparatedList for ImplementsInterfaceList {
type Kind = GraphqlSyntaxKind;
type Parser<'source> = GraphqlParser<'source>;

const LIST_KIND: Self::Kind = GRAPHQL_IMPLEMENTS_INTERFACE_LIST;

fn parse_element(&mut self, p: &mut Self::Parser<'_>) -> ParsedSyntax {
parse_named_type(p)
}

fn is_at_list_end(&self, p: &mut Self::Parser<'_>) -> bool {
is_at_implements_interface_end(p)
}

fn recover(
&mut self,
p: &mut Self::Parser<'_>,
parsed_element: ParsedSyntax,
) -> biome_parser::parse_recovery::RecoveryResult {
parsed_element.or_recover(
p,
&ImplementsInterfaceListParseRecovery,
expected_named_type,
)
}

fn separating_element_kind(&mut self) -> Self::Kind {
T![&]
}

fn allow_trailing_separating_element(&self) -> bool {
false
}
}

struct ImplementsInterfaceListParseRecovery;

impl ParseRecovery for ImplementsInterfaceListParseRecovery {
type Kind = GraphqlSyntaxKind;
type Parser<'source> = GraphqlParser<'source>;
const RECOVERED_KIND: Self::Kind = GRAPHQL_BOGUS;

fn is_at_recovered(&self, p: &mut Self::Parser<'_>) -> bool {
is_at_name(p) || p.at(T![&]) || is_at_implements_interface_end(p)
}
}

#[inline]
fn is_at_implements_interface(p: &mut GraphqlParser<'_>) -> bool {
p.at(T![implements])
}

#[inline]
fn is_at_implements_interface_end(p: &mut GraphqlParser<'_>) -> bool {
is_at_directive(p) || is_at_fields(p) || is_at_fields_end(p)
}
7 changes: 7 additions & 0 deletions crates/biome_graphql_parser/src/parser/definitions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
mod field;
mod fragment;
mod interface;
mod object;
mod operation;
mod scalar;
mod schema;
Expand All @@ -12,6 +15,7 @@ use biome_parser::{

use self::{
fragment::{is_at_fragment_definition, parse_fragment_definition},
object::{is_at_object_type_definition, parse_object_type_definition},
operation::{is_at_operation, parse_operation_definition},
scalar::{is_at_scalar_type_definition, parse_scalar_type_definition},
schema::{is_at_schema_definition, parse_schema_definition},
Expand Down Expand Up @@ -67,6 +71,8 @@ fn parse_definition(p: &mut GraphqlParser) -> ParsedSyntax {
parse_schema_definition(p)
} else if is_at_scalar_type_definition(p) {
parse_scalar_type_definition(p)
} else if is_at_object_type_definition(p) {
parse_object_type_definition(p)
} else {
Absent
}
Expand All @@ -79,4 +85,5 @@ fn is_at_definition(p: &mut GraphqlParser<'_>) -> bool {
|| is_at_fragment_definition(p)
|| is_at_schema_definition(p)
|| is_at_scalar_type_definition(p)
|| is_at_object_type_definition(p)
}
39 changes: 39 additions & 0 deletions crates/biome_graphql_parser/src/parser/definitions/object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::parser::{
directive::DirectiveList, parse_description, parse_error::expected_name, parse_name,
value::is_at_string, GraphqlParser,
};
use biome_graphql_syntax::{GraphqlSyntaxKind::*, T};
use biome_parser::{
parse_lists::ParseNodeList, parsed_syntax::ParsedSyntax, prelude::ParsedSyntax::*, Parser,
};

use super::{field::parse_fields_definition, interface::parse_implements_interface};

#[inline]
pub(crate) fn parse_object_type_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_object_type_definition(p) {
return Absent;
}
let m = p.start();

// description is optional
parse_description(p).ok();

p.bump(T![type]);

parse_name(p).or_add_diagnostic(p, expected_name);

// implements interface is optional
parse_implements_interface(p).ok();
DirectiveList.parse_list(p);

// fields definition is optional
parse_fields_definition(p).ok();

Present(m.complete(p, GRAPHQL_OBJECT_TYPE_DEFINITION))
}

#[inline]
pub(crate) fn is_at_object_type_definition(p: &mut GraphqlParser<'_>) -> bool {
p.at(T![type]) || (is_at_string(p) && p.nth_at(1, T![type]))
}
Loading