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

Create EOF token with empty data #591

Merged
merged 1 commit into from
Jul 13, 2023
Merged
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
21 changes: 19 additions & 2 deletions crates/apollo-parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'a> Iterator for Lexer<'a> {
}

if self.input.is_empty() {
let mut eof = Token::new(TokenKind::Eof, "EOF");
let mut eof = Token::new(TokenKind::Eof, "");
eof.index = self.index;

self.finished = true;
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'a> Cursor<'a> {
let mut state = State::Start;
let mut token = Token {
kind: TokenKind::Eof,
data: "EOF",
data: "",
index: self.index(),
};

Expand Down Expand Up @@ -654,4 +654,21 @@ type Query {
],
);
}

#[test]
fn stream_produces_original_input() {
let schema = r#"
type Query {
name: String
format: String = "Y-m-d\\TH:i:sP"
}
"#;

let lexer = Lexer::new(schema);
let processed_schema = lexer
.into_iter()
.fold(String::new(), |acc, token| acc + token.unwrap().data());

assert_eq!(schema, processed_schema);
}
}
3 changes: 2 additions & 1 deletion crates/apollo-parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,11 @@ impl<'a> Parser<'a> {
return;
}

let message = format!("expected {kind:?}, got {data}");
let err = if is_eof {
let message = format!("expected {kind:?}, got EOF");
Error::eof(message, index)
} else {
let message = format!("expected {kind:?}, got {data}");
Error::with_loc(message, data, index)
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
- DOCUMENT@0..19
- OPERATION_DEFINITION@0..19
- DOCUMENT@0..16
- OPERATION_DEFINITION@0..16
- [email protected]
- [email protected] "query"
- [email protected] " "
- [email protected]
- [email protected] "__typename"
- ERROR@16..19 "EOF"
- ERROR@16..16 ""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what to make of this here, the OPERATION_DEFINITION is now showing the correct character span (the source is 16 characters long) but I don't know why this error message is empty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an error token, not an error message, so IMO it's correct like this

- ERROR@16:16 "expected a Selection Set" EOF
recursion limit: 4096, high: 0