mirror of
https://github.com/typst/typst
synced 2025-05-13 12:36:23 +08:00
Implement parsing trait for str ♻
This commit is contained in:
parent
caf08ea5fa
commit
29229c4002
@ -172,16 +172,16 @@ impl<'s> Generator<'s> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod generator_tests {
|
mod generator_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::parsing::{Tokenize, Parse};
|
use crate::parsing::ParseTree;
|
||||||
|
|
||||||
/// Test if the source gets generated into the document.
|
/// Test if the source gets generated into the document.
|
||||||
fn test(src: &str, doc: 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.
|
/// Test if generation gives this error for the source code.
|
||||||
fn test_err(src: &str, err: GenerationError) {
|
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]
|
#[test]
|
||||||
|
@ -5,17 +5,15 @@
|
|||||||
//! # Example
|
//! # Example
|
||||||
//! This is an example of compiling a _really_ simple document into _PDF_.
|
//! 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 = "hello-typeset.pdf";
|
||||||
//! # let path = "../target/hello-typeset.pdf";
|
//! # let path = "../target/hello-typeset.pdf";
|
||||||
//! let mut file = std::fs::File::create(path).unwrap();
|
//! 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 src = "Hello World from Typeset!";
|
||||||
//! let doc = src.tokenize()
|
//! let doc = src.parse_tree().unwrap().generate().unwrap();
|
||||||
//! .parse().unwrap()
|
|
||||||
//! .generate().unwrap();
|
|
||||||
//!
|
//!
|
||||||
//! file.write_pdf(&doc).unwrap();
|
//! file.write_pdf(&doc).unwrap();
|
||||||
//! ```
|
//! ```
|
||||||
|
@ -298,19 +298,28 @@ pub struct Function<'s> {
|
|||||||
|
|
||||||
|
|
||||||
/// A type that is parsable into a syntax tree.
|
/// A type that is parsable into a syntax tree.
|
||||||
pub trait Parse<'s> {
|
pub trait ParseTree<'s> {
|
||||||
/// Parse self into a syntax tree.
|
/// 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> {
|
impl<'s> ParseTree<'s> for &'s str {
|
||||||
fn parse(self) -> ParseResult<SyntaxTree<'s>> {
|
#[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()
|
Parser::new(self).parse()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s> Parse<'s> for Vec<Token<'s>> {
|
impl<'s> ParseTree<'s> for Vec<Token<'s>> {
|
||||||
fn parse(self) -> ParseResult<SyntaxTree<'s>> {
|
#[inline]
|
||||||
|
fn parse_tree(self) -> ParseResult<SyntaxTree<'s>> {
|
||||||
Parser::new(self.into_iter()).parse()
|
Parser::new(self.into_iter()).parse()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -594,12 +603,12 @@ mod parse_tests {
|
|||||||
|
|
||||||
/// Test if the source code parses into the syntax tree.
|
/// Test if the source code parses into the syntax tree.
|
||||||
fn test(src: &str, tree: SyntaxTree) {
|
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.
|
/// Test if the source parses into the error.
|
||||||
fn test_err(src: &str, err: ParseError) {
|
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.
|
/// Short cut macro to create a syntax tree.
|
||||||
|
@ -101,15 +101,13 @@ impl<W: Write> WritePdf<Document> for W {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod pdf_tests {
|
mod pdf_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::parsing::{Tokenize, Parse};
|
use crate::parsing::ParseTree;
|
||||||
use crate::doc::Generate;
|
use crate::doc::Generate;
|
||||||
|
|
||||||
/// Create a pdf with a name from the source code.
|
/// Create a pdf with a name from the source code.
|
||||||
fn test(name: &str, src: &str) {
|
fn test(name: &str, src: &str) {
|
||||||
let mut file = std::fs::File::create(format!("../target/{}", name)).unwrap();
|
let mut file = std::fs::File::create(format!("../target/{}", name)).unwrap();
|
||||||
let doc = src.tokenize()
|
let doc = src.parse_tree().unwrap().generate().unwrap();
|
||||||
.parse().unwrap()
|
|
||||||
.generate().unwrap();
|
|
||||||
file.write_pdf(&doc).unwrap();
|
file.write_pdf(&doc).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user