aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-01 16:00:06 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-31 10:27:15 +0200
commite40e93796f45d34d038c6a20c4b034eb3b384b12 (patch)
treeff717ff9781c943f87715d7c8385a9af01fde3a9
parentbdb6c0b89df5099fe6cf2e79e04e0efd21f64aa8 (diff)
downloadaskama-e40e93796f45d34d038c6a20c4b034eb3b384b12.tar.gz
askama-e40e93796f45d34d038c6a20c4b034eb3b384b12.tar.bz2
askama-e40e93796f45d34d038c6a20c4b034eb3b384b12.zip
Extract askama_parser crate
Diffstat (limited to '')
-rw-r--r--Cargo.toml2
-rw-r--r--askama_derive/Cargo.toml2
-rw-r--r--askama_derive/src/config.rs2
-rw-r--r--askama_derive/src/generator.rs2
-rw-r--r--askama_derive/src/heritage.rs2
-rw-r--r--askama_derive/src/input.rs2
-rw-r--r--askama_derive/src/lib.rs4
-rw-r--r--askama_parser/Cargo.toml17
-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
12 files changed, 68 insertions, 46 deletions
diff --git a/Cargo.toml b/Cargo.toml
index bd8b8da..60326cd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,7 @@ members = [
"askama_derive",
"askama_escape",
"askama_mendes",
+ "askama_parser",
"askama_rocket",
"askama_tide",
"askama_warp",
@@ -18,5 +19,6 @@ default-members = [
"askama",
"askama_derive",
"askama_escape",
+ "askama_parser",
"testing",
]
diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml
index 5b33e6d..cb4635f 100644
--- a/askama_derive/Cargo.toml
+++ b/askama_derive/Cargo.toml
@@ -31,9 +31,9 @@ with-tide = []
with-warp = []
[dependencies]
+parser = { package = "askama_parser", version = "0.1", path = "../askama_parser" }
mime = "0.3"
mime_guess = "2"
-nom = "7"
proc-macro2 = "1"
quote = "1"
serde = { version = "1.0", optional = true, features = ["derive"] }
diff --git a/askama_derive/src/config.rs b/askama_derive/src/config.rs
index e456c67..6533fdc 100644
--- a/askama_derive/src/config.rs
+++ b/askama_derive/src/config.rs
@@ -5,8 +5,8 @@ use std::{env, fs};
#[cfg(feature = "serde")]
use serde::Deserialize;
-use crate::parser::{Syntax, Whitespace};
use crate::CompileError;
+use parser::{Syntax, Whitespace};
#[derive(Debug)]
pub(crate) struct Config<'a> {
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index b6c6151..0c482b5 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -1,8 +1,8 @@
use crate::config::{get_template_source, read_config_file, Config, WhitespaceHandling};
use crate::heritage::{Context, Heritage};
use crate::input::{Print, Source, TemplateInput};
-use crate::parser::{Cond, CondTest, Expr, Loop, Node, Parsed, Target, When, Whitespace, Ws};
use crate::CompileError;
+use parser::{Cond, CondTest, Expr, Loop, Node, Parsed, Target, When, Whitespace, Ws};
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
diff --git a/askama_derive/src/heritage.rs b/askama_derive/src/heritage.rs
index dbb2b1f..38c2cc8 100644
--- a/askama_derive/src/heritage.rs
+++ b/askama_derive/src/heritage.rs
@@ -2,8 +2,8 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use crate::config::Config;
-use crate::parser::{Loop, Macro, Node};
use crate::CompileError;
+use parser::{Loop, Macro, Node};
pub(crate) struct Heritage<'a> {
pub(crate) root: &'a Context<'a>,
diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs
index be425a3..d4e8ad9 100644
--- a/askama_derive/src/input.rs
+++ b/askama_derive/src/input.rs
@@ -1,7 +1,7 @@
use crate::config::Config;
use crate::generator::TemplateArgs;
-use crate::parser::Syntax;
use crate::CompileError;
+use parser::Syntax;
use std::path::{Path, PathBuf};
use std::str::FromStr;
diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs
index 1483438..8a737aa 100644
--- a/askama_derive/src/lib.rs
+++ b/askama_derive/src/lib.rs
@@ -7,12 +7,12 @@ use std::fmt;
use proc_macro::TokenStream;
use proc_macro2::Span;
+use parser::ParseError;
+
mod config;
mod generator;
mod heritage;
mod input;
-mod parser;
-use parser::ParseError;
#[proc_macro_derive(Template, attributes(template))]
pub fn derive_template(input: TokenStream) -> TokenStream {
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