diff options
Diffstat (limited to '')
| -rw-r--r-- | askama_parser/Cargo.toml | 17 | ||||
| -rw-r--r-- | askama_parser/src/expr.rs (renamed from askama_derive/src/parser/expr.rs) | 2 | ||||
| -rw-r--r-- | askama_parser/src/lib.rs (renamed from askama_derive/src/parser/mod.rs) | 33 | ||||
| -rw-r--r-- | askama_parser/src/node.rs (renamed from askama_derive/src/parser/node.rs) | 46 | ||||
| -rw-r--r-- | askama_parser/src/tests.rs (renamed from askama_derive/src/parser/tests.rs) | 0 | 
5 files changed, 59 insertions, 39 deletions
| diff --git a/askama_parser/Cargo.toml b/askama_parser/Cargo.toml new file mode 100644 index 0000000..8af58df --- /dev/null +++ b/askama_parser/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "askama_parser" +version = "0.1.0" +description = "Parser for Askama templates" +documentation = "https://docs.rs/askama" +keywords = ["markup", "template", "jinja2", "html"] +categories = ["template-engine"] +homepage = "https://github.com/djc/askama" +repository = "https://github.com/djc/askama" +license = "MIT OR Apache-2.0" +workspace = ".." +readme = "README.md" +edition = "2021" +rust-version = "1.58" + +[dependencies] +nom = { version = "7", default-features = false, features = ["alloc"] } diff --git a/askama_derive/src/parser/expr.rs b/askama_parser/src/expr.rs index deefb48..03620e7 100644 --- a/askama_derive/src/parser/expr.rs +++ b/askama_parser/src/expr.rs @@ -14,7 +14,7 @@ use super::{  };  #[derive(Debug, PartialEq)] -pub(crate) enum Expr<'a> { +pub enum Expr<'a> {      BoolLit(&'a str),      NumLit(&'a str),      StrLit(&'a str), diff --git a/askama_derive/src/parser/mod.rs b/askama_parser/src/lib.rs index 8da96f5..336004e 100644 --- a/askama_derive/src/parser/mod.rs +++ b/askama_parser/src/lib.rs @@ -1,3 +1,6 @@ +#![deny(unreachable_pub)] +#![deny(elided_lifetimes_in_paths)] +  use std::cell::Cell;  use std::{fmt, str}; @@ -11,8 +14,8 @@ use nom::multi::separated_list1;  use nom::sequence::{delimited, pair, tuple};  use nom::{error_position, AsChar, IResult, InputTakeAtPosition}; -pub(crate) use self::expr::Expr; -pub(crate) use self::node::{Cond, CondTest, Loop, Macro, Node, Target, When, Whitespace, Ws}; +pub use self::expr::Expr; +pub use self::node::{Cond, CondTest, Loop, Macro, Node, Target, When, Whitespace, Ws};  mod expr;  mod node; @@ -61,14 +64,14 @@ mod _parsed {      use super::{parse, Node, ParseError, Syntax}; -    pub(crate) struct Parsed { +    pub struct Parsed {          #[allow(dead_code)]          source: String,          nodes: Vec<Node<'static>>,      }      impl Parsed { -        pub(crate) fn new(source: String, syntax: &Syntax<'_>) -> Result<Self, ParseError> { +        pub fn new(source: String, syntax: &Syntax<'_>) -> Result<Self, ParseError> {              // Self-referential borrowing: `self` will keep the source alive as `String`,              // internally we will transmute it to `&'static str` to satisfy the compiler.              // However, we only expose the nodes with a lifetime limited to `self`. @@ -82,15 +85,15 @@ mod _parsed {          }          // The return value's lifetime must be limited to `self` to uphold the unsafe invariant. -        pub(crate) fn nodes(&self) -> &[Node<'_>] { +        pub fn nodes(&self) -> &[Node<'_>] {              &self.nodes          }      }  } -pub(crate) use _parsed::Parsed; +pub use _parsed::Parsed; -pub(crate) fn parse<'a>(src: &'a str, syntax: &Syntax<'_>) -> Result<Vec<Node<'a>>, ParseError> { +pub fn parse<'a>(src: &'a str, syntax: &Syntax<'_>) -> Result<Vec<Node<'a>>, ParseError> {      match Node::parse(src, &State::new(syntax)) {          Ok((left, res)) => {              if !left.is_empty() { @@ -128,7 +131,7 @@ pub(crate) fn parse<'a>(src: &'a str, syntax: &Syntax<'_>) -> Result<Vec<Node<'a  }  #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct ParseError(String); +pub struct ParseError(String);  impl std::error::Error for ParseError {} @@ -358,13 +361,13 @@ fn tag_expr_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {  }  #[derive(Debug)] -pub(crate) struct Syntax<'a> { -    pub(crate) block_start: &'a str, -    pub(crate) block_end: &'a str, -    pub(crate) expr_start: &'a str, -    pub(crate) expr_end: &'a str, -    pub(crate) comment_start: &'a str, -    pub(crate) comment_end: &'a str, +pub struct Syntax<'a> { +    pub block_start: &'a str, +    pub block_end: &'a str, +    pub expr_start: &'a str, +    pub expr_end: &'a str, +    pub comment_start: &'a str, +    pub comment_end: &'a str,  }  impl Default for Syntax<'static> { diff --git a/askama_derive/src/parser/node.rs b/askama_parser/src/node.rs index ce303bc..8719d38 100644 --- a/askama_derive/src/parser/node.rs +++ b/askama_parser/src/node.rs @@ -16,7 +16,7 @@ use super::{  };  #[derive(Debug, PartialEq)] -pub(crate) enum Node<'a> { +pub enum Node<'a> {      Lit(&'a str, &'a str, &'a str),      Comment(Ws),      Expr(Ws, Expr<'a>), @@ -37,7 +37,7 @@ pub(crate) enum Node<'a> {  }  #[derive(Debug, PartialEq)] -pub(crate) enum Target<'a> { +pub enum Target<'a> {      Name(&'a str),      Tuple(Vec<&'a str>, Vec<Target<'a>>),      Struct(Vec<&'a str>, Vec<(&'a str, Target<'a>)>), @@ -49,46 +49,46 @@ pub(crate) enum Target<'a> {  }  #[derive(Clone, Copy, Debug, PartialEq)] -pub(crate) enum Whitespace { +pub enum Whitespace {      Preserve,      Suppress,      Minimize,  }  #[derive(Debug, PartialEq)] -pub(crate) struct Loop<'a> { -    pub(crate) ws1: Ws, -    pub(crate) var: Target<'a>, -    pub(crate) iter: Expr<'a>, -    pub(crate) cond: Option<Expr<'a>>, -    pub(crate) body: Vec<Node<'a>>, -    pub(crate) ws2: Ws, -    pub(crate) else_block: Vec<Node<'a>>, -    pub(crate) ws3: Ws, +pub struct Loop<'a> { +    pub ws1: Ws, +    pub var: Target<'a>, +    pub iter: Expr<'a>, +    pub cond: Option<Expr<'a>>, +    pub body: Vec<Node<'a>>, +    pub ws2: Ws, +    pub else_block: Vec<Node<'a>>, +    pub ws3: Ws,  } -pub(crate) type When<'a> = (Ws, Target<'a>, Vec<Node<'a>>); +pub type When<'a> = (Ws, Target<'a>, Vec<Node<'a>>);  #[derive(Debug, PartialEq)] -pub(crate) struct Macro<'a> { -    pub(crate) ws1: Ws, -    pub(crate) args: Vec<&'a str>, -    pub(crate) nodes: Vec<Node<'a>>, -    pub(crate) ws2: Ws, +pub struct Macro<'a> { +    pub ws1: Ws, +    pub args: Vec<&'a str>, +    pub nodes: Vec<Node<'a>>, +    pub ws2: Ws,  }  /// First field is "minus/plus sign was used on the left part of the item".  ///  /// Second field is "minus/plus sign was used on the right part of the item".  #[derive(Clone, Copy, Debug, PartialEq)] -pub(crate) struct Ws(pub(crate) Option<Whitespace>, pub(crate) Option<Whitespace>); +pub struct Ws(pub Option<Whitespace>, pub Option<Whitespace>); -pub(crate) type Cond<'a> = (Ws, Option<CondTest<'a>>, Vec<Node<'a>>); +pub type Cond<'a> = (Ws, Option<CondTest<'a>>, Vec<Node<'a>>);  #[derive(Debug, PartialEq)] -pub(crate) struct CondTest<'a> { -    pub(crate) target: Option<Target<'a>>, -    pub(crate) expr: Expr<'a>, +pub struct CondTest<'a> { +    pub target: Option<Target<'a>>, +    pub expr: Expr<'a>,  }  impl Node<'_> { diff --git a/askama_derive/src/parser/tests.rs b/askama_parser/src/tests.rs index 0e785eb..0e785eb 100644 --- a/askama_derive/src/parser/tests.rs +++ b/askama_parser/src/tests.rs | 
