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

refactor(parser): Use peek_token() to streamline peek operations #900

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions crates/apollo-parser/src/parser/grammar/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ pub(crate) fn document(p: &mut Parser) {
}
}
TokenKind::Name => {
let def = p.peek_data().unwrap();
select_definition(def, p);
if let Some(def) = p.peek_data() {
select_definition(def, p);
} else {
p.err_and_pop("expected a definition after this Name");
}
}
TokenKind::LCurly => {
let def = p.peek_data().unwrap();
select_definition(def, p);
if let Some(def) = p.peek_data() {
select_definition(def, p);
} else {
p.err_and_pop("expected a definition");
}
}
TokenKind::Eof => return ControlFlow::Break(()),
_ => p.err_and_pop("expected a StringValue, Name or OperationDefinition"),
Expand Down
39 changes: 20 additions & 19 deletions crates/apollo-parser/src/parser/grammar/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ pub(crate) fn fragment_definition(p: &mut Parser) {
/// Name *but not* **on**
pub(crate) fn fragment_name(p: &mut Parser) {
let _g = p.start_node(SyntaxKind::FRAGMENT_NAME);
match p.peek() {
Some(TokenKind::Name) => {
if p.peek_data().unwrap() == "on" {
return p.err("Fragment Name cannot be 'on'");
if let Some(token) = p.peek_token() {
if token.kind() == TokenKind::Name {
if token.data() != "on" {
name::name(p);
} else {
p.err("Fragment Name cannot be 'on'");
}
name::name(p)
return;
}
_ => p.err("expected Fragment Name"),
}
p.err("expected Fragment Name");
}

/// See: https://spec.graphql.org/October2021/#TypeCondition
Expand All @@ -53,21 +55,20 @@ pub(crate) fn fragment_name(p: &mut Parser) {
/// **on** NamedType
pub(crate) fn type_condition(p: &mut Parser) {
let _g = p.start_node(SyntaxKind::TYPE_CONDITION);
match p.peek() {
Some(TokenKind::Name) => {
if p.peek_data().unwrap() == "on" {
p.bump(SyntaxKind::on_KW);
} else {
p.err("expected 'on'");
}
if let Some(token) = p.peek_token() {
if token.kind() == TokenKind::Name && token.data() == "on" {
p.bump(SyntaxKind::on_KW);
} else {
p.err("expected 'on'");
}

if let Some(TokenKind::Name) = p.peek() {
ty::named_type(p)
} else {
p.err("expected a Name in Type Condition")
}
if let Some(TokenKind::Name) = p.peek() {
ty::named_type(p)
} else {
p.err("expected a Name in Type Condition")
}
_ => p.err("expected Type Condition"),
} else {
p.err("expected Type Condition")
}
}

Expand Down
10 changes: 6 additions & 4 deletions crates/apollo-parser/src/parser/grammar/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ use crate::S;
/// *Name*:
/// [_A-Za-z][_0-9A-Za-z]
pub(crate) fn name(p: &mut Parser) {
match p.peek() {
Some(TokenKind::Name) => {
if let Some(token) = p.peek_token() {
if token.kind() == TokenKind::Name {
let name_data = token.data();
let _g = p.start_node(SyntaxKind::NAME);
validate_name(p.peek_data().unwrap(), p);
validate_name(name_data, p);
p.bump(SyntaxKind::IDENT);
return;
}
_ => p.err("expected a Name"),
}
p.err("expected a Name");
}

pub(crate) fn validate_name(name: &str, p: &mut Parser) {
Expand Down
6 changes: 3 additions & 3 deletions crates/apollo-parser/src/parser/grammar/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub(crate) fn object_type_definition(p: &mut Parser) {
_ => p.err("expected a name"),
}

if let Some(TokenKind::Name) = p.peek() {
if p.peek_data().unwrap() == "implements" {
if let Some(token) = p.peek_token() {
if token.kind() == TokenKind::Name && token.data() == "implements" {
implements_interfaces(p);
}
}
Expand All @@ -58,7 +58,7 @@ pub(crate) fn object_type_extension(p: &mut Parser) {
p.bump(SyntaxKind::extend_KW);
p.bump(SyntaxKind::type_KW);

// Use this variable to see if any of ImplementsInterfacs, Directives or
// Use this variable to see if any of ImplementsInterfaces, Directives or
// FieldsDefinitions is provided. If none are present, we push an error.
let mut meets_requirements = false;

Expand Down
29 changes: 15 additions & 14 deletions crates/apollo-parser/src/parser/grammar/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,22 @@ pub(crate) fn value(p: &mut Parser, constness: Constness, pop_on_error: bool) {
p.bump(SyntaxKind::STRING);
}
Some(TokenKind::Name) => {
let node = p.peek_data().unwrap();
match node {
"true" => {
let _g = p.start_node(SyntaxKind::BOOLEAN_VALUE);
p.bump(SyntaxKind::true_KW);
}
"false" => {
let _g = p.start_node(SyntaxKind::BOOLEAN_VALUE);
p.bump(SyntaxKind::false_KW);
}
"null" => {
let _g = p.start_node(SyntaxKind::NULL_VALUE);
p.bump(SyntaxKind::null_KW)
if let Some(token) = p.peek_token() {
match token.data() {
"true" => {
let _g = p.start_node(SyntaxKind::BOOLEAN_VALUE);
p.bump(SyntaxKind::true_KW);
}
"false" => {
let _g = p.start_node(SyntaxKind::BOOLEAN_VALUE);
p.bump(SyntaxKind::false_KW);
}
"null" => {
let _g = p.start_node(SyntaxKind::NULL_VALUE);
p.bump(SyntaxKind::null_KW)
}
_ => enum_value(p),
}
_ => enum_value(p),
}
}
Some(T!['[']) => list_value(p, constness),
Expand Down