Skip to content

Commit

Permalink
refactor(biome_graphql_parser): use is_nth_at_*
Browse files Browse the repository at this point in the history
  • Loading branch information
vohoanglong0107 committed May 10, 2024
1 parent 5088667 commit 826223c
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 220 deletions.
6 changes: 3 additions & 3 deletions crates/biome_graphql_parser/src/parser/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use biome_parser::{
use super::{
definitions::is_at_selection_set_end,
directive::is_at_directive,
is_at_name,
is_nth_at_name,
parse_error::{expected_argument, expected_value},
value::parse_value,
};
Expand All @@ -24,7 +24,7 @@ impl ParseRecovery for ArgumentListParseRecovery {
const RECOVERED_KIND: Self::Kind = GRAPHQL_ARGUMENT;

fn is_at_recovered(&self, p: &mut Self::Parser<'_>) -> bool {
is_at_name(p) || is_at_argument_list_end(p)
is_nth_at_name(p, 0) || is_at_argument_list_end(p)
}
}

Expand Down Expand Up @@ -70,7 +70,7 @@ pub(crate) fn parse_arguments(p: &mut GraphqlParser) -> ParsedSyntax {

#[inline]
fn parse_argument(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_name(p) {
if !is_nth_at_name(p, 0) {
return Absent;
}

Expand Down
10 changes: 5 additions & 5 deletions crates/biome_graphql_parser/src/parser/definitions/enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parser::{
directive::{is_at_directive, DirectiveList},
is_at_name, parse_description,
is_nth_at_name, parse_description,
parse_error::expected_name,
parse_name,
value::{is_at_string, parse_enum_value},
Expand Down Expand Up @@ -90,7 +90,7 @@ impl ParseRecovery for EnumValueListParseRecovery {
// 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)
is_nth_at_name(p, 0) || is_at_enum_values_end(p)
}
}

Expand Down Expand Up @@ -122,13 +122,13 @@ fn is_at_enum_values(p: &mut GraphqlParser) -> bool {
// After an enum definition is a new type definition
// so it's safe to assume any name we see before a new type definition is
// an enum value
|| is_at_name(p)
|| (is_at_string(p) && p.nth_at(1, GRAPHQL_NAME))
|| is_nth_at_name(p, 0)
|| (is_at_string(p) && is_nth_at_name(p, 1))
}

#[inline]
fn is_at_enum_value(p: &mut GraphqlParser) -> bool {
is_at_name(p) || (is_at_string(p) && p.nth_at(1, GRAPHQL_NAME)) || is_at_directive(p)
is_nth_at_name(p, 0) || (is_at_string(p) && is_nth_at_name(p, 1)) || is_at_directive(p)
}

#[inline]
Expand Down
29 changes: 21 additions & 8 deletions crates/biome_graphql_parser/src/parser/definitions/field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parser::{
directive::DirectiveList,
is_at_name, parse_description,
is_nth_at_name, parse_description,
parse_error::{expected_field_definition, expected_name, expected_type},
parse_name,
r#type::parse_type,
Expand Down Expand Up @@ -185,25 +185,38 @@ pub(super) fn is_at_fields_end(p: &mut GraphqlParser<'_>) -> bool {
p.at(T!['}']) || is_at_definition(p)
}

/// Currently at a field definition, allowing some small errors.
#[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![:])
(is_nth_at_field(p, 0))
|| (is_at_string(p) && is_nth_at_field(p, 1))
// missing name
|| p.at(T![:])
}

/// At correctly formatted field definition at nth token.
#[inline]
fn is_nth_at_field(p: &mut GraphqlParser<'_>, n: usize) -> bool {
is_nth_at_name(p, n) && (p.nth_at(n + 1, T![:]) || p.nth_at(n + 1, T!['(']))
}

/// Currently at an input value definition, allowing some small errors.
#[inline]
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![:]))
is_nth_at_input_value_definition(p, 0)
|| (is_at_string(p) && is_nth_at_input_value_definition(p, 1))
// 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))
|| (is_nth_at_name(p, 0) && is_nth_at_name(p, 1))
|| (is_at_string(p) && is_nth_at_name(p, 1) && is_nth_at_name(p, 2))
}

/// At correctly formatted input value definition at nth token.
#[inline]
fn is_nth_at_input_value_definition(p: &mut GraphqlParser<'_>, n: usize) -> bool {
is_nth_at_name(p, n) && p.nth_at(n + 1, T![:])
}

/// We must enforce that the arguments definition is always opened with a `(` token.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parser::{
directive::{is_at_directive, DirectiveList},
is_at_name, parse_description,
is_nth_at_name, parse_description,
parse_error::{expected_name, expected_named_type},
parse_name,
r#type::parse_named_type,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl ParseRecovery for ImplementsInterfaceListParseRecovery {
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)
is_nth_at_name(p, 0) || p.at(T![&]) || is_at_implements_interface_end(p)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::parser::{
argument::parse_arguments,
directive::{is_at_directive, DirectiveList},
is_at_name,
is_nth_at_name,
parse_error::{
expected_any_selection, expected_name, expected_type, expected_variable,
expected_variable_definition,
Expand Down Expand Up @@ -196,7 +196,7 @@ fn parse_fragment(p: &mut GraphqlParser) -> ParsedSyntax {
}
let m = p.start();
p.expect(DOT3);
if is_at_name(p) {
if is_nth_at_name(p, 0) {
// name is checked for in `is_at_name`
parse_name(p).ok();
DirectiveList.parse_list(p);
Expand Down Expand Up @@ -272,7 +272,7 @@ fn is_at_variable_definitions_end(p: &GraphqlParser) -> bool {
fn is_at_variable_definition(p: &mut GraphqlParser) -> bool {
is_at_variable(p)
// malformed variable
|| is_at_name(p)
|| is_nth_at_name(p, 0)
// malformed variable,but not inside selection set
|| (p.nth_at(1, T![:]) && !p.at(T!['{']))
// missing entire variable
Expand Down Expand Up @@ -300,7 +300,7 @@ fn is_at_selection(p: &mut GraphqlParser) -> bool {

#[inline]
fn is_at_field(p: &mut GraphqlParser) -> bool {
is_at_name(p) || is_at_alias(p)
is_nth_at_name(p, 0) || is_at_alias(p)
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions crates/biome_graphql_parser/src/parser/definitions/union.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parser::{
directive::DirectiveList,
is_at_name, parse_description,
is_nth_at_name, parse_description,
parse_error::{expected_name, expected_named_type},
parse_name,
r#type::parse_named_type,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl ParseRecovery for UnionMemberListParseRecovery {
// 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
|| is_at_name(p)
|| is_nth_at_name(p, 0)
|| is_at_union_member_types_end(p)
}
}
Expand All @@ -130,7 +130,7 @@ fn is_at_union_member_types(p: &mut GraphqlParser<'_>) -> bool {
// missing both = and |. 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
|| is_at_name(p)
|| is_nth_at_name(p, 0)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_graphql_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ fn parse_description(p: &mut GraphqlParser) -> ParsedSyntax {
}

#[inline]
fn is_at_name(p: &GraphqlParser) -> bool {
p.at(GRAPHQL_NAME)
fn is_nth_at_name(p: &mut GraphqlParser, n: usize) -> bool {
p.nth_at(n, GRAPHQL_NAME)
}
4 changes: 2 additions & 2 deletions crates/biome_graphql_parser/src/parser/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use biome_parser::{
};

use super::{
is_at_name,
is_nth_at_name,
parse_error::{expected_named_or_list_type, expected_type},
};

Expand Down Expand Up @@ -42,7 +42,7 @@ fn parse_list_type(p: &mut GraphqlParser) -> CompletedMarker {

#[inline]
pub(crate) fn parse_named_type(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_name(p) {
if !is_nth_at_name(p, 0) {
return Absent;
}
let m = p.start();
Expand Down
14 changes: 7 additions & 7 deletions crates/biome_graphql_parser/src/parser/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use biome_parser::{

use super::{
argument::is_at_argument_list_end,
is_at_name,
is_nth_at_name,
parse_error::{expected_object_field, expected_value},
variable::{is_at_variable, parse_variable},
};
Expand Down Expand Up @@ -65,7 +65,7 @@ impl ParseRecovery for ObjectValueMemberListParseRecovery {
const RECOVERED_KIND: Self::Kind = GRAPHQL_OBJECT_FIELD;

fn is_at_recovered(&self, p: &mut Self::Parser<'_>) -> bool {
is_at_name(p) || is_at_object_end(p)
is_nth_at_name(p, 0) || is_at_object_end(p)
}
}

Expand Down Expand Up @@ -233,7 +233,7 @@ fn parse_object_field(p: &mut GraphqlParser) -> ParsedSyntax {
}

#[inline]
fn is_at_value(p: &GraphqlParser) -> bool {
fn is_at_value(p: &mut GraphqlParser) -> bool {
is_at_variable(p)
|| is_at_int(p)
|| is_at_float(p)
Expand Down Expand Up @@ -271,8 +271,8 @@ fn is_at_null(p: &GraphqlParser) -> bool {
}

#[inline]
fn is_at_enum(p: &GraphqlParser) -> bool {
is_at_name(p)
fn is_at_enum(p: &mut GraphqlParser) -> bool {
is_nth_at_name(p, 0)
}

#[inline]
Expand All @@ -295,8 +295,8 @@ fn is_at_object(p: &GraphqlParser) -> bool {
}

#[inline]
fn is_at_object_field(p: &GraphqlParser) -> bool {
is_at_name(p)
fn is_at_object_field(p: &mut GraphqlParser) -> bool {
is_nth_at_name(p, 0)
}

#[inline]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ type Person implements Character @deprecated

type Person implements Character & Character1 @deprecated

"This is a person"
type Person {
name(start_with: String): String
"filder by age" age: Int @deprecated
picture(size: Int = 0): Url
height("filter by height" greater_than: Int @deprecated): Int
weight("filter by weight" greater_than: Int = 0 @deprecated): Int
"filter by weight" weight("filter by weight" greater_than: Int = 0 @deprecated): Int
}

Loading

0 comments on commit 826223c

Please sign in to comment.