diff --git a/crates/apollo-parser/src/parser/grammar/document.rs b/crates/apollo-parser/src/parser/grammar/document.rs index 32c203c60..9935f6ce4 100644 --- a/crates/apollo-parser/src/parser/grammar/document.rs +++ b/crates/apollo-parser/src/parser/grammar/document.rs @@ -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"), diff --git a/crates/apollo-parser/src/parser/grammar/fragment.rs b/crates/apollo-parser/src/parser/grammar/fragment.rs index bfc532984..f1a86dd41 100644 --- a/crates/apollo-parser/src/parser/grammar/fragment.rs +++ b/crates/apollo-parser/src/parser/grammar/fragment.rs @@ -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 @@ -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") } } diff --git a/crates/apollo-parser/src/parser/grammar/name.rs b/crates/apollo-parser/src/parser/grammar/name.rs index a478387fe..b9d158881 100644 --- a/crates/apollo-parser/src/parser/grammar/name.rs +++ b/crates/apollo-parser/src/parser/grammar/name.rs @@ -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) { diff --git a/crates/apollo-parser/src/parser/grammar/object.rs b/crates/apollo-parser/src/parser/grammar/object.rs index e4f029f04..28333ac7b 100644 --- a/crates/apollo-parser/src/parser/grammar/object.rs +++ b/crates/apollo-parser/src/parser/grammar/object.rs @@ -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); } } @@ -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; diff --git a/crates/apollo-parser/src/parser/grammar/value.rs b/crates/apollo-parser/src/parser/grammar/value.rs index 595951931..30936feaf 100644 --- a/crates/apollo-parser/src/parser/grammar/value.rs +++ b/crates/apollo-parser/src/parser/grammar/value.rs @@ -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),