//! OpenCypher parser

use nom::Finish;

pub mod ast;
mod error;
mod lexer;
mod parser;

pub use error::{Error, ErrorKind};

/// Result type used by this crate
pub type Result<T, E = error::Error> = std::result::Result<T, E>;

/// Parse an OpenCypher into an AST.
pub fn parse_oc_query(source: &str) -> Result<ast::Queries>
{
  let context = parser::Context::new(source);
  let tokens = lexer::tokenize(source)?;
  let input = parser::Input(&tokens, &context);
  // Parse schema
  let (rem, parse_tree) = parser::queries(input.clone()).finish()?;

  if !rem.0.is_empty()
  {
    return Err(Error::new(
      error::ErrorKind::UnexpectedToken,
      rem.first().map_or(Default::default(), |x| x.span.clone()),
    ));
  }

  Ok(parse_tree)
}
