Skip to content

Commit

Permalink
Extract askama_parser from askama_derive
Browse files Browse the repository at this point in the history
  • Loading branch information
couchand committed Mar 7, 2023
1 parent 51af19f commit 26e5eff
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 74 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"askama_derive",
"askama_escape",
"askama_mendes",
"askama_parser",
"askama_rocket",
"askama_tide",
"askama_warp",
Expand All @@ -18,5 +19,6 @@ default-members = [
"askama",
"askama_derive",
"askama_escape",
"askama_parser",
"testing",
]
1 change: 1 addition & 0 deletions askama_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ with-tide = []
with-warp = []

[dependencies]
askama_parser = { version = "0.1", path = "../askama_parser" }
mime = "0.3"
mime_guess = "2"
proc-macro2 = "1"
Expand Down
2 changes: 1 addition & 1 deletion askama_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::{env, fs};
#[cfg(feature = "serde")]
use serde::Deserialize;

use crate::parser::Syntax;
use crate::CompileError;
use askama_parser::Syntax;

#[derive(Debug)]
pub(crate) struct Config<'a> {
Expand Down
4 changes: 2 additions & 2 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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::{
use crate::CompileError;
use askama_parser::{
parse, Block, BlockDef, Call, Cond, CondTest, Expr, Lit, Loop, Match, Node, Raw, Tag, Target,
When, Whitespace, Ws,
};
use crate::CompileError;

use proc_macro::TokenStream;
use quote::{quote, ToTokens};
Expand Down
2 changes: 1 addition & 1 deletion askama_derive/src/heritage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::config::Config;
use crate::parser::{Block, BlockDef, Cond, Loop, Macro, Match, Node, Tag, When};
use crate::CompileError;
use askama_parser::{Block, BlockDef, Cond, Loop, Macro, Match, Node, Tag, When};

pub(crate) struct Heritage<'a> {
pub(crate) root: &'a Context<'a>,
Expand Down
2 changes: 1 addition & 1 deletion askama_derive/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::Config;
use crate::generator::TemplateArgs;
use crate::parser::Syntax;
use crate::CompileError;
use askama_parser::Syntax;

use std::path::{Path, PathBuf};
use std::str::FromStr;
Expand Down
5 changes: 2 additions & 3 deletions askama_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod config;
mod generator;
mod heritage;
mod input;
mod parser;

#[proc_macro_derive(Template, attributes(template))]
pub fn derive_template(input: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -52,8 +51,8 @@ impl From<String> for CompileError {
}
}

impl From<parser::ParseError> for CompileError {
fn from(e: parser::ParseError) -> Self {
impl From<askama_parser::ParseError> for CompileError {
fn from(e: askama_parser::ParseError) -> Self {
Self::new(e.to_string())
}
}
Expand Down
14 changes: 14 additions & 0 deletions askama_parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "askama_parser"
version = "0.1.0"
description = "Askama template syntax parser"
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 = "7"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{

/// An Askama expression.
#[derive(Debug, PartialEq)]
pub(crate) enum Expr<'a> {
pub enum Expr<'a> {
/// A boolean literal.
BoolLit(&'a str),
/// A numeric literal.
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Expr<'_> {

/// Returns `true` if enough assumptions can be made,
/// to determine that `self` is copyable.
pub(crate) fn is_copyable(&self) -> bool {
pub fn is_copyable(&self) -> bool {
self.is_copyable_within_op(false)
}

Expand Down Expand Up @@ -94,7 +94,7 @@ impl Expr<'_> {
}

/// Returns `true` if this is an `Attr` where the `obj` is `"self"`.
pub(crate) fn is_attr_self(&self) -> bool {
pub fn is_attr_self(&self) -> bool {
match self {
Expr::Attr(obj, _) if matches!(obj.as_ref(), Expr::Var("self")) => true,
Expr::Attr(obj, _) if matches!(obj.as_ref(), Expr::Attr(..)) => obj.is_attr_self(),
Expand All @@ -105,7 +105,7 @@ impl Expr<'_> {
/// Returns `true` if the outcome of this expression may be used multiple times in the same
/// `write!()` call, without evaluating the expression again, i.e. the expression should be
/// side-effect free.
pub(crate) fn is_cacheable(&self) -> bool {
pub fn is_cacheable(&self) -> bool {
match self {
// Literals are the definition of pure:
Expr::BoolLit(_) => true,
Expand Down
35 changes: 17 additions & 18 deletions askama_derive/src/parser/mod.rs → askama_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,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::{
pub use self::expr::Expr;
pub use self::node::{
Block, BlockDef, Call, Cond, CondTest, Lit, Loop, Macro, Match, Node, Raw, Tag, Target, When,
};

Expand All @@ -35,19 +35,19 @@ mod tests;

/// Askama template syntax configuration.
#[derive(Debug)]
pub(crate) struct Syntax<'a> {
pub struct Syntax<'a> {
/// Defaults to `"{%"`.
pub(crate) block_start: &'a str,
pub block_start: &'a str,
/// Defaults to `"%}"`.
pub(crate) block_end: &'a str,
pub block_end: &'a str,
/// Defaults to `"{{"`.
pub(crate) expr_start: &'a str,
pub expr_start: &'a str,
/// Defaults to `"}}"`.
pub(crate) expr_end: &'a str,
pub expr_end: &'a str,
/// Defaults to `"{#"`.
pub(crate) comment_start: &'a str,
pub comment_start: &'a str,
/// Defaults to `"#}"`.
pub(crate) comment_end: &'a str,
pub comment_end: &'a str,
}

impl Default for Syntax<'static> {
Expand All @@ -65,19 +65,19 @@ impl Default for Syntax<'static> {

/// Whitespace preservation or suppression.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum Whitespace {
pub enum Whitespace {
Preserve,
Suppress,
Minimize,
}

/// Whitespace suppression for a `Tag` or `Block`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Ws {
pub struct Ws {
/// Handling of trailing whitespace on literal text at a transition in to Askama.
pub(crate) flush: Option<Whitespace>,
pub flush: Option<Whitespace>,
/// Handling of leading whitespace on literal text at a transition out of Askama.
pub(crate) prepare: Option<Whitespace>,
pub prepare: Option<Whitespace>,
}

impl Ws {
Expand Down Expand Up @@ -127,7 +127,7 @@ impl From<char> for Whitespace {
/// Parse template source to an abstract syntax tree.
///
/// Tries to parse the provided template string using the given syntax.
pub(crate) fn parse<'a>(src: &'a str, syntax: &'a Syntax<'_>) -> Result<Block<'a>, ParseError> {
pub fn parse<'a>(src: &'a str, syntax: &'a Syntax<'_>) -> Result<Block<'a>, ParseError> {
let state = State::new(syntax);
let mut p = all_consuming(complete(|i| Node::parse(i, &state)));
match p(src) {
Expand Down Expand Up @@ -164,21 +164,20 @@ pub(crate) fn parse<'a>(src: &'a str, syntax: &'a Syntax<'_>) -> Result<Block<'a

/// An error encountered when parsing template source.
#[derive(Debug)]
pub(crate) struct ParseError {
pub struct ParseError {
row: usize,
column: usize,
snippet: String,
}

#[cfg(test)]
impl ParseError {
/// The line number in the source where the error was identified.
pub(crate) fn line(&self) -> usize {
pub fn line(&self) -> usize {
self.row
}

/// The column number in the source where the error was identified.
pub(crate) fn column(&self) -> usize {
pub fn column(&self) -> usize {
self.column
}
}
Expand Down
Loading

0 comments on commit 26e5eff

Please sign in to comment.