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(biome_graphql_parser): parse directive definition #2774

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
140 changes: 140 additions & 0 deletions crates/biome_graphql_parser/src/parser/definitions/directive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use crate::parser::{
parse_description,
parse_error::{expected_directive_location, expected_name},
parse_name,
value::is_at_string,
GraphqlParser,
};
use biome_graphql_syntax::{
GraphqlSyntaxKind::{self, *},
T,
};
use biome_parser::prelude::TokenSource;
use biome_parser::{
parse_lists::ParseSeparatedList, parse_recovery::ParseRecovery, parsed_syntax::ParsedSyntax,
prelude::ParsedSyntax::*, token_set, Parser, TokenSet,
};

use super::{field::parse_arguments_definition, is_at_definition};

const DIRECTIVE_LOCATION_SET: TokenSet<GraphqlSyntaxKind> = token_set!(
T![UPPER_QUERY],
T![UPPER_MUTATION],
T![UPPER_SUBSCRIPTION],
T![UPPER_FIELD],
T![FRAGMENT_DEFINITION],
T![FRAGMENT_SPREAD],
T![INLINE_FRAGMENT],
T![VARIABLE_DEFINITION],
T![UPPER_SCHEMA],
T![UPPER_SCALAR],
T![UPPER_OBJECT],
T![FIELD_DEFINITION],
T![ARGUMENT_DEFINITION],
T![UPPER_INTERFACE],
T![UPPER_UNION],
T![UPPER_ENUM],
T![ENUM_VALUE],
T![INPUT_OBJECT],
T![INPUT_FIELD_DEFINITION]
);

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

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

p.bump(T![directive]);
p.expect(T![@]);
parse_name(p).or_add_diagnostic(p, expected_name);

// arguments are optional
parse_arguments_definition(p).ok();

// repeatable is optional
p.eat(T![repeatable]);
p.expect(T![on]);

// | is optional
p.eat(T![|]);

let position = p.source().position();
DirectiveLocationList.parse_list(p);

// has not progressed, meaning no directive locations were parsed
if position == p.source().position() {
p.error(expected_directive_location(p, p.cur_range()));
}

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

#[derive(Default)]
struct DirectiveLocationList;

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

const LIST_KIND: Self::Kind = GRAPHQL_DIRECTIVE_LOCATION_LIST;

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

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

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

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

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

struct DirectiveLocationListParseRecovery;

impl ParseRecovery for DirectiveLocationListParseRecovery {
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 {
p.at_ts(DIRECTIVE_LOCATION_SET) || is_at_definition(p)
}
}

#[inline]
fn parse_directive_location(p: &mut GraphqlParser) -> ParsedSyntax {
if !p.at_ts(DIRECTIVE_LOCATION_SET) {
return Absent;
}
let m = p.start();
p.bump_ts(DIRECTIVE_LOCATION_SET);
Present(m.complete(p, GRAPHQL_DIRECTIVE_LOCATION))
}

#[inline]
pub(crate) fn is_at_directive_definition(p: &mut GraphqlParser) -> bool {
p.at(T![directive]) || (is_at_string(p) && p.nth_at(1, T![directive]))
}
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 @@ -97,7 +97,7 @@ fn parse_field_definition(p: &mut GraphqlParser) -> ParsedSyntax {
}

#[inline]
fn parse_arguments_definition(p: &mut GraphqlParser) -> ParsedSyntax {
pub(super) fn parse_arguments_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_arguments_definition(p) {
return Absent;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ pub(super) fn is_at_input_value_definition(p: &mut GraphqlParser<'_>) -> bool {
/// 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 {
pub(super) fn is_at_arguments_definition(p: &mut GraphqlParser<'_>) -> bool {
p.at(T!['('])
}

Expand Down
7 changes: 5 additions & 2 deletions crates/biome_graphql_parser/src/parser/definitions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod directive;
mod r#enum;
mod field;
mod fragment;
Expand All @@ -17,6 +18,7 @@ use biome_parser::{
};

use self::{
directive::{is_at_directive_definition, parse_directive_definition},
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},
Expand Down Expand Up @@ -69,7 +71,6 @@ impl ParseNodeList for DefinitionList {

#[inline]
fn parse_definition(p: &mut GraphqlParser) -> ParsedSyntax {
// TODO: add directive definition
if is_at_operation(p) {
parse_operation_definition(p)
} else if is_at_fragment_definition(p) {
Expand All @@ -88,14 +89,15 @@ fn parse_definition(p: &mut GraphqlParser) -> ParsedSyntax {
parse_enum_type_definition(p)
} else if is_at_input_object_type_definition(p) {
parse_input_object_type_definition(p)
} else if is_at_directive_definition(p) {
parse_directive_definition(p)
} else {
Absent
}
}

#[inline]
fn is_at_definition(p: &mut GraphqlParser<'_>) -> bool {
// TODO: add directive definition
is_at_operation(p)
|| is_at_fragment_definition(p)
|| is_at_schema_definition(p)
Expand All @@ -105,4 +107,5 @@ fn is_at_definition(p: &mut GraphqlParser<'_>) -> bool {
|| is_at_union_type_definition(p)
|| is_at_enum_type_definition(p)
|| is_at_input_object_type_definition(p)
|| is_at_directive_definition(p)
}
28 changes: 28 additions & 0 deletions crates/biome_graphql_parser/src/parser/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,31 @@ pub(crate) fn expected_root_operation_type_definition(
pub(crate) fn expected_operation_type(p: &GraphqlParser, range: TextRange) -> ParseDiagnostic {
expected_any(&["query", "mutation", "subscription"], range, p)
}

pub(crate) fn expected_directive_location(p: &GraphqlParser, range: TextRange) -> ParseDiagnostic {
expected_any(
&[
"QUERY",
"MUTATION",
"SUBSCRIPTION",
"FIELD",
"FRAGMENT_DEFINITION",
"FRAGMENT_SPREAD",
"INLINE_FRAGMENT",
"VARIABLE_DEFINITION",
"SCHEMA",
"SCALAR",
"OBJECT",
"FIELD_DEFINITION",
"ARGUMENT_DEFINITION",
"INTERFACE",
"UNION",
"ENUM",
"ENUM_VALUE",
"INPUT_OBJECT",
"INPUT_FIELD_DEFINITION",
],
range,
p,
)
ematipico marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
directive @example on |

directive @ on | ARGUMENT_DEFINITION

directive example on | ARGUMENT_DEFINITION
directive example ARGUMENT_DEFINITION

directive @example on
| FIELD
| FRAGMENT_SPREAD
|

directive @delegateField(: !) repeatable on OBJECT | INTERFACE

directve @example on

directive @example on
| #
| 123
| name
Loading