Implement parsing trait for str ♻

This commit is contained in:
Laurenz 2019-02-20 17:36:34 +01:00
parent caf08ea5fa
commit 29229c4002
4 changed files with 25 additions and 20 deletions

View File

@ -172,16 +172,16 @@ impl<'s> Generator<'s> {
#[cfg(test)]
mod generator_tests {
use super::*;
use crate::parsing::{Tokenize, Parse};
use crate::parsing::ParseTree;
/// Test if the source gets generated into the document.
fn test(src: &str, doc: Document) {
assert_eq!(src.tokenize().parse().unwrap().generate(), Ok(doc));
assert_eq!(src.parse_tree().unwrap().generate(), Ok(doc));
}
/// Test if generation gives this error for the source code.
fn test_err(src: &str, err: GenerationError) {
assert_eq!(src.tokenize().parse().unwrap().generate(), Err(err));
assert_eq!(src.parse_tree().unwrap().generate(), Err(err));
}
#[test]

View File

@ -5,17 +5,15 @@
//! # Example
//! This is an example of compiling a _really_ simple document into _PDF_.
//! ```
//! use typeset::{parsing::{Tokenize, Parse}, doc::Generate, export::WritePdf};
//! use typeset::{parsing::ParseTree, doc::Generate, export::WritePdf};
//!
//! let path = "hello-typeset.pdf";
//! # let path = "../target/hello-typeset.pdf";
//! let mut file = std::fs::File::create(path).unwrap();
//!
//! // Tokenize, parse and then generate the document.
//! // Parse the source and then generate the document.
//! let src = "Hello World from Typeset!";
//! let doc = src.tokenize()
//! .parse().unwrap()
//! .generate().unwrap();
//! let doc = src.parse_tree().unwrap().generate().unwrap();
//!
//! file.write_pdf(&doc).unwrap();
//! ```

View File

@ -298,19 +298,28 @@ pub struct Function<'s> {
/// A type that is parsable into a syntax tree.
pub trait Parse<'s> {
pub trait ParseTree<'s> {
/// Parse self into a syntax tree.
fn parse(self) -> ParseResult<SyntaxTree<'s>>;
fn parse_tree(self) -> ParseResult<SyntaxTree<'s>>;
}
impl<'s> Parse<'s> for Tokens<'s> {
fn parse(self) -> ParseResult<SyntaxTree<'s>> {
impl<'s> ParseTree<'s> for &'s str {
#[inline]
fn parse_tree(self) -> ParseResult<SyntaxTree<'s>> {
self.tokenize().parse_tree()
}
}
impl<'s> ParseTree<'s> for Tokens<'s> {
#[inline]
fn parse_tree(self) -> ParseResult<SyntaxTree<'s>> {
Parser::new(self).parse()
}
}
impl<'s> Parse<'s> for Vec<Token<'s>> {
fn parse(self) -> ParseResult<SyntaxTree<'s>> {
impl<'s> ParseTree<'s> for Vec<Token<'s>> {
#[inline]
fn parse_tree(self) -> ParseResult<SyntaxTree<'s>> {
Parser::new(self.into_iter()).parse()
}
}
@ -594,12 +603,12 @@ mod parse_tests {
/// Test if the source code parses into the syntax tree.
fn test(src: &str, tree: SyntaxTree) {
assert_eq!(src.tokenize().parse(), Ok(tree));
assert_eq!(src.parse_tree(), Ok(tree));
}
/// Test if the source parses into the error.
fn test_err(src: &str, err: ParseError) {
assert_eq!(src.tokenize().parse(), Err(err));
assert_eq!(src.parse_tree(), Err(err));
}
/// Short cut macro to create a syntax tree.

View File

@ -101,15 +101,13 @@ impl<W: Write> WritePdf<Document> for W {
#[cfg(test)]
mod pdf_tests {
use super::*;
use crate::parsing::{Tokenize, Parse};
use crate::parsing::ParseTree;
use crate::doc::Generate;
/// Create a pdf with a name from the source code.
fn test(name: &str, src: &str) {
let mut file = std::fs::File::create(format!("../target/{}", name)).unwrap();
let doc = src.tokenize()
.parse().unwrap()
.generate().unwrap();
let doc = src.parse_tree().unwrap().generate().unwrap();
file.write_pdf(&doc).unwrap();
}