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

Abstract (start, end) from Spanning struct into new struct Span #1207

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions juniper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
- Removed lifetime parameter from `ParseError`, `GraphlQLError`, `GraphQLBatchRequest` and `GraphQLRequest`. ([#1081], [#528])
- Upgraded [GraphiQL] to 3.0.9 version (requires new [`graphql-transport-ws` GraphQL over WebSocket Protocol] integration on server, see `juniper_warp/examples/subscription.rs`). ([#1188], [#1193], [#1204])
- Made `LookAheadMethods::children()` method to return slice instead of `Vec`. ([#1200])
- Abstract `(start, end)` from the `Spanning<T>` struct into separate struct `Span`. ([#1208])

### Added

Expand Down Expand Up @@ -131,6 +132,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
[#1199]: /../../pull/1199
[#1200]: /../../pull/1200
[#1204]: /../../pull/1204
[#1208]: /../../pull/1208
[ba1ed85b]: /../../commit/ba1ed85b3c3dd77fbae7baf6bc4e693321a94083
[CVE-2022-31173]: /../../security/advisories/GHSA-4rx6-g5vg-5f3j

Expand Down
6 changes: 3 additions & 3 deletions juniper/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ where
schema: &root_node.schema,
context,
errors: &errors,
field_path: Arc::new(FieldPath::Root(operation.start)),
field_path: Arc::new(FieldPath::Root(operation.span.start)),
};

value = match operation.item.operation_type {
Expand Down Expand Up @@ -983,7 +983,7 @@ where
schema: &root_node.schema,
context,
errors: &errors,
field_path: Arc::new(FieldPath::Root(operation.start)),
field_path: Arc::new(FieldPath::Root(operation.span.start)),
};

value = match operation.item.operation_type {
Expand Down Expand Up @@ -1129,7 +1129,7 @@ where
schema: &root_node.schema,
context,
errors: &errors,
field_path: Arc::new(FieldPath::Root(operation.start)),
field_path: Arc::new(FieldPath::Root(operation.span.start)),
};

value = match operation.item.operation_type {
Expand Down
4 changes: 2 additions & 2 deletions juniper/src/integrations/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ impl Serialize for Spanning<ParseError> {
map.serialize_value(&msg)?;

let mut loc = IndexMap::new();
loc.insert("line".to_owned(), self.start.line() + 1);
loc.insert("column".to_owned(), self.start.column() + 1);
loc.insert("line".to_owned(), self.start().line() + 1);
loc.insert("column".to_owned(), self.start().column() + 1);

let locations = vec![loc];
map.serialize_key("locations")?;
Expand Down
2 changes: 1 addition & 1 deletion juniper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub use crate::{
},
introspection::IntrospectionFormat,
macros::helper::subscription::{ExtractTypeFromStream, IntoFieldResult},
parser::{ParseError, ScalarToken, Spanning},
parser::{ParseError, ScalarToken, Span, Spanning},
schema::{
meta,
model::{RootNode, SchemaType},
Expand Down
73 changes: 32 additions & 41 deletions juniper/src/parser/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ where
let fields = fields.as_ref().map(|c| c as &[_]);
let selection_set = parse_selection_set(parser, schema, fields)?;

Ok(Spanning::start_end(
&selection_set.start,
&selection_set.end,
Ok(Spanning::new(
selection_set.span,
Operation {
operation_type: OperationType::Query,
name: None,
Expand All @@ -94,7 +93,7 @@ where
},
))
} else {
let start_pos = parser.peek().start;
let start_pos = parser.peek().span.start;
let operation_type = parse_operation_type(parser)?;
let op = match operation_type.item {
OperationType::Query => Some(schema.concrete_query_type()),
Expand All @@ -114,7 +113,7 @@ where

Ok(Spanning::start_end(
&start_pos,
&selection_set.end,
&selection_set.span.end,
Operation {
operation_type: operation_type.item,
name,
Expand All @@ -133,9 +132,7 @@ fn parse_fragment_definition<'a, 'b, S>(
where
S: ScalarValue,
{
let Spanning {
start: start_pos, ..
} = parser.expect(&Token::Name("fragment"))?;
let start_pos = parser.expect(&Token::Name("fragment"))?.span.start;
let name = match parser.expect_name() {
Ok(n) => {
if n.item == "on" {
Expand All @@ -160,7 +157,7 @@ where

Ok(Spanning::start_end(
&start_pos,
&selection_set.end,
&selection_set.span.end,
Fragment {
name,
type_condition: type_cond,
Expand Down Expand Up @@ -222,10 +219,7 @@ fn parse_fragment<'a, 'b, S>(
where
S: ScalarValue,
{
let Spanning {
start: ref start_pos,
..
} = parser.expect(&Token::Ellipsis)?;
let start_pos = parser.expect(&Token::Ellipsis)?.span.start;

match parser.peek().item {
Token::Name("on") => {
Expand All @@ -240,8 +234,8 @@ where
let selection_set = parse_selection_set(parser, schema, fields)?;

Ok(Selection::InlineFragment(Spanning::start_end(
&start_pos.clone(),
&selection_set.end,
&start_pos,
&selection_set.span.end,
InlineFragment {
type_condition: Some(name),
directives: directives.map(|s| s.item),
Expand All @@ -253,8 +247,8 @@ where
let selection_set = parse_selection_set(parser, schema, fields)?;

Ok(Selection::InlineFragment(Spanning::start_end(
&start_pos.clone(),
&selection_set.end,
&start_pos,
&selection_set.span.end,
InlineFragment {
type_condition: None,
directives: None,
Expand All @@ -270,7 +264,7 @@ where
&start_pos.clone(),
&directives
.as_ref()
.map_or(&frag_name.end, |s| &s.end)
.map_or(frag_name.end(), Spanning::end)
.clone(),
FragmentSpread {
name: frag_name,
Expand All @@ -283,8 +277,8 @@ where
let selection_set = parse_selection_set(parser, schema, fields)?;

Ok(Selection::InlineFragment(Spanning::start_end(
&start_pos.clone(),
&selection_set.end,
&start_pos,
&selection_set.span.end,
InlineFragment {
type_condition: None,
directives: directives.map(|s| s.item),
Expand Down Expand Up @@ -329,13 +323,13 @@ where
let selection_set = parse_optional_selection_set(parser, schema, fields)?;

Ok(Spanning::start_end(
&alias.as_ref().unwrap_or(&name).start.clone(),
&alias.as_ref().unwrap_or(&name).span.start,
&selection_set
.as_ref()
.map(|s| &s.end)
.or_else(|| directives.as_ref().map(|s| &s.end))
.or_else(|| arguments.as_ref().map(|s| &s.end))
.unwrap_or(&name.end)
.map(Spanning::end)
.or_else(|| directives.as_ref().map(Spanning::end))
.or_else(|| arguments.as_ref().map(Spanning::end))
.unwrap_or(name.end())
.clone(),
Field {
alias,
Expand Down Expand Up @@ -389,8 +383,8 @@ where
let value = parse_value_literal(parser, false, schema, tpe)?;

Ok(Spanning::start_end(
&name.start.clone(),
&value.end.clone(),
&name.span.start,
&value.span.end.clone(),
(name, value),
))
}
Expand Down Expand Up @@ -437,9 +431,7 @@ fn parse_variable_definition<'a, 'b, S>(
where
S: ScalarValue,
{
let Spanning {
start: start_pos, ..
} = parser.expect(&Token::Dollar)?;
let start_pos = parser.expect(&Token::Dollar)?.span.start;
let var_name = parser.expect_name()?;
parser.expect(&Token::Colon)?;
let var_type = parse_type(parser)?;
Expand All @@ -457,10 +449,10 @@ where
&start_pos,
&default_value
.as_ref()
.map_or(&var_type.end, |s| &s.end)
.map_or(var_type.end(), Spanning::end)
.clone(),
(
Spanning::start_end(&start_pos, &var_name.end, var_name.item),
Spanning::start_end(&start_pos, var_name.end(), var_name.item),
VariableDefinition {
var_type,
default_value,
Expand Down Expand Up @@ -496,9 +488,7 @@ fn parse_directive<'a, 'b, S>(
where
S: ScalarValue,
{
let Spanning {
start: start_pos, ..
} = parser.expect(&Token::At)?;
let start_pos = parser.expect(&Token::At)?.span.start;
let name = parser.expect_name()?;

let directive = schema.directive_by_name(name.item);
Expand All @@ -511,20 +501,21 @@ where

Ok(Spanning::start_end(
&start_pos,
&arguments.as_ref().map_or(&name.end, |s| &s.end).clone(),
&arguments.as_ref().map_or(name.end(), Spanning::end).clone(),
Directive { name, arguments },
))
}

pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<Type<'a>> {
let parsed_type = if let Some(Spanning {
start: start_pos, ..
span: ref start_span,
..
}) = parser.skip(&Token::BracketOpen)?
{
let inner_type = parse_type(parser)?;
let Spanning { end: end_pos, .. } = parser.expect(&Token::BracketClose)?;
let end_pos = parser.expect(&Token::BracketClose)?.span.end;
Spanning::start_end(
&start_pos,
&start_span.start,
&end_pos,
Type::List(Box::new(inner_type.item), None),
)
Expand All @@ -542,13 +533,13 @@ pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<Type<'a>> {
}

fn wrap_non_null<'a>(parser: &mut Parser<'a>, inner: Spanning<Type<'a>>) -> ParseResult<Type<'a>> {
let Spanning { end: end_pos, .. } = parser.expect(&Token::ExclamationMark)?;
let end_pos = &parser.expect(&Token::ExclamationMark)?.span.end;

let wrapped = match inner.item {
Type::Named(name) => Type::NonNullNamed(name),
Type::List(l, expected_size) => Type::NonNullList(l, expected_size),
t => t,
};

Ok(Spanning::start_end(&inner.start, &end_pos, wrapped))
Ok(Spanning::start_end(&inner.span.start, end_pos, wrapped))
}
2 changes: 1 addition & 1 deletion juniper/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ pub use self::document::parse_document_source;
pub use self::{
lexer::{Lexer, LexerError, ScalarToken, Token},
parser::{OptionParseResult, ParseError, ParseResult, Parser, UnlocatedParseResult},
utils::{SourcePosition, Spanning},
utils::{SourcePosition, Span, Spanning},
};
36 changes: 14 additions & 22 deletions juniper/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ impl<'a> Parser<'a> {
#[doc(hidden)]
pub fn next_token(&mut self) -> ParseResult<Token<'a>> {
if self.tokens.len() == 1 {
Err(Spanning::start_end(
&self.peek().start,
&self.peek().end,
Err(Spanning::new(
self.peek().span,
ParseError::UnexpectedEndOfFile,
))
} else {
Expand All @@ -125,7 +124,7 @@ impl<'a> Parser<'a> {
Ok(Some(self.next_token()?))
} else if self.peek().item == Token::EndOfFile {
Err(Spanning::zero_width(
&self.peek().start,
self.peek().start(),
ParseError::UnexpectedEndOfFile,
))
} else {
Expand All @@ -144,14 +143,12 @@ impl<'a> Parser<'a> {
T: fmt::Debug,
F: Fn(&mut Parser<'a>) -> ParseResult<T>,
{
let Spanning {
start: start_pos, ..
} = self.expect(opening)?;
let start_pos = &self.expect(opening)?.span.start;
let mut items = Vec::new();

loop {
if let Some(Spanning { end: end_pos, .. }) = self.skip(closing)? {
return Ok(Spanning::start_end(&start_pos, &end_pos, items));
if let Some(Spanning { span, .. }) = self.skip(closing)? {
return Ok(Spanning::start_end(start_pos, &span.end, items));
}

items.push(parser(self)?);
Expand All @@ -169,16 +166,14 @@ impl<'a> Parser<'a> {
T: fmt::Debug,
F: Fn(&mut Parser<'a>) -> ParseResult<T>,
{
let Spanning {
start: start_pos, ..
} = self.expect(opening)?;
let start_pos = &self.expect(opening)?.span.start;
let mut items = Vec::new();

loop {
items.push(parser(self)?);

if let Some(Spanning { end: end_pos, .. }) = self.skip(closing)? {
return Ok(Spanning::start_end(&start_pos, &end_pos, items));
if let Some(end_spanning) = self.skip(closing)? {
return Ok(Spanning::start_end(start_pos, end_spanning.end(), items));
}
}
}
Expand All @@ -194,16 +189,14 @@ impl<'a> Parser<'a> {
T: fmt::Debug,
F: Fn(&mut Parser<'a>) -> UnlocatedParseResult<T>,
{
let Spanning {
start: start_pos, ..
} = self.expect(opening)?;
let start_pos = &self.expect(opening)?.span.start;
let mut items = Vec::new();

loop {
items.push(parser(self)?);

if let Some(Spanning { end: end_pos, .. }) = self.skip(closing)? {
return Ok(Spanning::start_end(&start_pos, &end_pos, items));
if let Some(end_spanning) = self.skip(closing)? {
return Ok(Spanning::start_end(start_pos, end_spanning.end(), items));
}
}
}
Expand All @@ -224,9 +217,8 @@ impl<'a> Parser<'a> {
Spanning {
item: Token::EndOfFile,
..
} => Err(Spanning::start_end(
&self.peek().start,
&self.peek().end,
} => Err(Spanning::new(
self.peek().span,
ParseError::UnexpectedEndOfFile,
)),
_ => Err(self.next_token()?.map(ParseError::unexpected_token)),
Expand Down
4 changes: 2 additions & 2 deletions juniper/src/parser/tests/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ fn numbers() {
expected: &str,
) {
let parsed = tokenize_single(source);
assert_eq!(parsed.start, start);
assert_eq!(parsed.end, end);
assert_eq!(parsed.span.start, start);
assert_eq!(parsed.span.end, end);

match parsed.item {
Token::Scalar(ScalarToken::Float(actual)) => {
Expand Down
Loading