Skip to content

Commit

Permalink
feat(biome_graphql_parser): parse input object definition
Browse files Browse the repository at this point in the history
  • Loading branch information
vohoanglong0107 committed May 8, 2024
1 parent ed7509c commit 88f9435
Show file tree
Hide file tree
Showing 8 changed files with 1,502 additions and 7 deletions.
6 changes: 3 additions & 3 deletions crates/biome_graphql_parser/src/parser/definitions/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ impl ParseRecovery for EnumValueListParseRecovery {
const RECOVERED_KIND: Self::Kind = GRAPHQL_BOGUS;

fn is_at_recovered(&self, p: &mut Self::Parser<'_>) -> bool {
// After a union definition is a new type definition so it's safe to
// assume any name we see before a new type definition is a union
// member type
// After a enum definition is a new type definition so it's safe to
// assume any name we see before a new type definition is a enum
// value
is_at_name(p) || is_at_enum_values_end(p)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_graphql_parser/src/parser/definitions/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl ParseRecovery for ArgumentDefinitionListParseRecovery {
}

#[inline]
fn parse_input_value_definition(p: &mut GraphqlParser) -> ParsedSyntax {
pub(super) fn parse_input_value_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_input_value_definition(p) {
return Absent;
}
Expand Down Expand Up @@ -196,7 +196,7 @@ fn is_at_field(p: &mut GraphqlParser<'_>) -> bool {
}

#[inline]
fn is_at_input_value_definition(p: &mut GraphqlParser<'_>) -> bool {
pub(super) 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
Expand Down
108 changes: 108 additions & 0 deletions crates/biome_graphql_parser/src/parser/definitions/input_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use crate::parser::{
directive::DirectiveList, parse_description, parse_error::expected_name, parse_name,
value::is_at_string, 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::{
field::{is_at_input_value_definition, parse_input_value_definition},
is_at_definition,
};

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

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

p.bump(T![input]);

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

DirectiveList.parse_list(p);

// input fields are optional
parse_input_fields_definition(p).ok();

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

#[inline]
fn parse_input_fields_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_input_fields_definition(p) {
return Absent;
}
let m = p.start();
p.expect(T!['{']);

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

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

#[derive(Default)]
struct InputFieldList;

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

const LIST_KIND: Self::Kind = GRAPHQL_INPUT_FIELD_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_input_fields_end(p)
}

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

struct InputFieldListParseRecovery;

impl ParseRecovery for InputFieldListParseRecovery {
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_input_value_definition(p) || is_input_fields_end(p)
}
}

#[inline]
pub(crate) fn is_at_input_object_type_definition(p: &mut GraphqlParser) -> bool {
p.at(T![input]) || (is_at_string(p) && p.nth_at(1, T![input]))
}

#[inline]
fn is_at_input_fields_definition(p: &mut GraphqlParser) -> bool {
p.at(T!['{'])
// missing opening brace
|| is_at_input_value_definition(p)
}

#[inline]
fn is_input_fields_end(p: &mut GraphqlParser) -> bool {
p.at(T!['}']) || is_at_definition(p)
}
9 changes: 7 additions & 2 deletions crates/biome_graphql_parser/src/parser/definitions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod r#enum;
mod field;
mod fragment;
mod input_object;
mod interface;
mod object;
mod operation;
Expand All @@ -17,6 +18,7 @@ use biome_parser::{

use self::{
fragment::{is_at_fragment_definition, parse_fragment_definition},
input_object::{is_at_input_object_type_definition, parse_input_object_type_definition},
interface::{is_at_interface_type_definition, parse_interface_type_definition},
object::{is_at_object_type_definition, parse_object_type_definition},
operation::{is_at_operation, parse_operation_definition},
Expand Down Expand Up @@ -67,7 +69,7 @@ impl ParseNodeList for DefinitionList {

#[inline]
fn parse_definition(p: &mut GraphqlParser) -> ParsedSyntax {
// TODO: parse any definition
// TODO: add directive definition
if is_at_operation(p) {
parse_operation_definition(p)
} else if is_at_fragment_definition(p) {
Expand All @@ -84,14 +86,16 @@ fn parse_definition(p: &mut GraphqlParser) -> ParsedSyntax {
parse_union_type_definition(p)
} else if is_at_enum_type_definition(p) {
parse_enum_type_definition(p)
} else if is_at_input_object_type_definition(p) {
parse_input_object_type_definition(p)
} else {
Absent
}
}

#[inline]
fn is_at_definition(p: &mut GraphqlParser<'_>) -> bool {
// TODO: recover at any definition
// TODO: add directive definition
is_at_operation(p)
|| is_at_fragment_definition(p)
|| is_at_schema_definition(p)
Expand All @@ -100,4 +104,5 @@ fn is_at_definition(p: &mut GraphqlParser<'_>) -> bool {
|| is_at_interface_type_definition(p)
|| is_at_union_type_definition(p)
|| is_at_enum_type_definition(p)
|| is_at_input_object_type_definition(p)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
input Point2D
x: Float
y: Float
}

input Point2D
x: Float
y: Float

input Point2D {

input Point2D @deprecated {
x: Float
}

input Point2D {
x: = 0
y: @deprecated
}

input Point2D {
x = 0
y: @deprecated
}

input Point2D {
= 0
: @
}

input Point2D {
x
y
}

input Point2D {
=
: Float
}

iput Point2D {
x: Float
y: Float
}
Loading

0 comments on commit 88f9435

Please sign in to comment.