Skip to content

Commit

Permalink
feat: impl Iterator for errors
Browse files Browse the repository at this point in the history
fixes #139

ast.errors() now return an `Iter<'_, Error>` which allows us to iterate, but also check for the `len()`. It should allow us to use `is_empty()` as well eventually, if / when rust-lang/rust#35428 lands.
  • Loading branch information
o0Ignition0o committed Nov 24, 2021
1 parent 29832e1 commit a816538
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 32 deletions.
8 changes: 4 additions & 4 deletions crates/apollo-parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ let input = "union SearchResult = Photo | Person | Cat | Dog";
let parser = Parser::new(input);
let ast = parser.parse();

// ast.errors() returns an errors slice encountered during lexing and parsing
assert!(ast.errors().is_empty());
// ast.errors() returns an iterator with the errors encountered during lexing and parsing
assert_eq!(0, ast.errors().len());

// ast.document() gets the Document, or root node, of the tree that you can
// start iterating on.
Expand Down Expand Up @@ -93,7 +93,7 @@ type ProductDimension {
";
let parser = Parser::new(input);
let ast = parser.parse();
assert!(ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down Expand Up @@ -124,7 +124,7 @@ let input = "

let parser = Parser::new(input);
let ast = parser.parse();
assert!(&ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down
2 changes: 1 addition & 1 deletion crates/apollo-parser/examples/unused_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn are_variables_unused() {
let parser = Parser::new(&src);
let ast = parser.parse();

assert!(&ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down
2 changes: 1 addition & 1 deletion crates/apollo-parser/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//! let parser = Parser::new(schema);
//! let ast = parser.parse();
//!
//! assert!(ast.errors().is_empty());
//! assert_eq!(0, ast.errors().len());
//! let document = ast.document();
//! for definition in document.definitions() {
//! match definition {
Expand Down
2 changes: 1 addition & 1 deletion crates/apollo-parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::fmt;
/// let parser = Parser::new(input);
/// let ast = parser.parse();
///
/// assert!(ast.errors().is_empty());
/// assert_eq!(0, ast.errors().len());
///
/// let doc = ast.document();
/// ```
Expand Down
6 changes: 4 additions & 2 deletions crates/apollo-parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod cursor;
mod token;
mod token_kind;

use std::slice::Iter;

use crate::{lexer::cursor::Cursor, Error};

pub use token::Token;
Expand Down Expand Up @@ -59,8 +61,8 @@ impl Lexer {
}

/// Get a reference to the lexer's tokens.
pub(crate) fn errors(&self) -> &[Error] {
self.errors.as_slice()
pub(crate) fn errors(&self) -> Iter<'_, Error> {
self.errors.iter()
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/apollo-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
//! let ast = parser.parse();
//!
//! // ast.errors() returns an errors slice encountered during lexing and parsing
//! assert!(ast.errors().is_empty());
//! assert_eq!(0, ast.errors().len());
//!
//! // ast.document() get the Document, or root node, of the tree that you can
//! // start iterating on.
Expand Down Expand Up @@ -94,7 +94,7 @@
//! ";
//! let parser = Parser::new(input);
//! let ast = parser.parse();
//! assert!(ast.errors().is_empty());
//! assert_eq!(0, ast.errors().len());
//!
//! let doc = ast.document();
//!
Expand Down Expand Up @@ -125,7 +125,7 @@
//!
//! let parser = Parser::new(input);
//! let ast = parser.parse();
//! assert!(&ast.errors().is_empty());
//! assert_eq!(0, ast.errors().len());
//!
//! let doc = ast.document();
//!
Expand Down
2 changes: 1 addition & 1 deletion crates/apollo-parser/src/parser/grammar/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ type Business implements NamedEntity & ValuedEntity & CatEntity {
}";
let parser = Parser::new(input);
let ast = parser.parse();
assert!(ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down
4 changes: 2 additions & 2 deletions crates/apollo-parser/src/parser/grammar/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ mod test {
";
let parser = Parser::new(input);
let ast = parser.parse();
assert!(&ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down Expand Up @@ -129,7 +129,7 @@ query GraphQuery($graph_id: ID!, $variant: String) {
";
let parser = Parser::new(input);
let ast = parser.parse();
assert!(&ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down
2 changes: 1 addition & 1 deletion crates/apollo-parser/src/parser/grammar/union_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod test {
let input = "union SearchResult = Photo | Person | Cat | Dog";
let parser = Parser::new(input);
let ast = parser.parse();
assert!(ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down
2 changes: 1 addition & 1 deletion crates/apollo-parser/src/parser/grammar/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ query GraphQuery($graph_id: ID!, $variant: String) {
";
let parser = Parser::new(input);
let ast = parser.parse();
assert!(&ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down
6 changes: 3 additions & 3 deletions crates/apollo-parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) use token_text::TokenText;
/// // Parse the query, and return a SyntaxTree.
/// let ast = parser.parse();
/// // Check that are no errors. These are not part of the AST.
/// assert!(&ast.errors().is_empty());
/// assert_eq!(0, ast.errors().len());
///
/// // Get the document root node
/// let doc = ast.document();
Expand All @@ -65,7 +65,7 @@ pub(crate) use token_text::TokenText;
/// let parser = Parser::new(core_schema);
/// let ast = parser.parse();
///
/// assert!(ast.errors().is_empty());
/// assert_eq!(0, ast.errors().len());
///
/// let document = ast.document();
/// ```
Expand All @@ -91,7 +91,7 @@ impl Parser {
tokens.push(s);
}

for e in lexer.errors().to_owned() {
for e in lexer.errors().cloned() {
errors.push(e);
}

Expand Down
8 changes: 4 additions & 4 deletions crates/apollo-parser/src/parser/syntax_tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{fmt, slice::Iter};

use rowan::GreenNodeBuilder;

Expand Down Expand Up @@ -44,8 +44,8 @@ pub struct SyntaxTree {

impl SyntaxTree {
/// Get a reference to the syntax tree's errors.
pub fn errors(&self) -> &[crate::Error] {
self.errors.as_ref()
pub fn errors(&self) -> Iter<'_, crate::Error> {
self.errors.iter()
}

/// Return the root typed `Document` node.
Expand Down Expand Up @@ -159,7 +159,7 @@ mod test {
";
let parser = Parser::new(input);
let ast = parser.parse();
assert!(ast.errors().is_empty());
assert_eq!(0, ast.errors().len());

let doc = ast.document();

Expand Down
16 changes: 8 additions & 8 deletions crates/apollo-parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
fmt::Write,
fs,
path::{Path, PathBuf},
slice::Iter,
};

use expect_test::expect_file;
Expand Down Expand Up @@ -52,25 +53,24 @@ fn parser_tests() {
});
}

fn assert_errors_are_present(errors: &[Error], path: &Path) {
fn assert_errors_are_present(errors: Iter<'_, Error>, path: &Path) {
assert!(
!errors.is_empty(),
errors.len() != 0,
"There should be errors in the file {:?}",
path.display()
);
}

fn assert_errors_are_absent(errors: &[Error], path: &Path) {
assert_eq!(
errors,
&[] as &[Error],
fn assert_errors_are_absent(errors: Iter<'_, Error>, path: &Path) {
assert!(
errors.len() == 0,
"There should be no errors in the file {:?}",
path.display(),
);
}

/// Concatenate tokens and erorrs.
fn dump_tokens_and_errors(tokens: &[Token], errors: &[Error]) -> String {
/// Concatenate tokens and errors.
fn dump_tokens_and_errors(tokens: &[Token], errors: Iter<'_, Error>) -> String {
let mut acc = String::new();
for token in tokens {
writeln!(acc, "{:?}", token).unwrap();
Expand Down

0 comments on commit a816538

Please sign in to comment.