Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions minijinja/src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub enum Expr<'a> {
GetItem(Spanned<GetItem<'a>>),
Call(Spanned<Call<'a>>),
List(Spanned<List<'a>>),
Tuple(Spanned<Tuple<'a>>),
Map(Spanned<Map<'a>>),
}

Expand All @@ -161,6 +162,7 @@ impl fmt::Debug for Expr<'_> {
Expr::GetItem(s) => fmt::Debug::fmt(s, f),
Expr::Call(s) => fmt::Debug::fmt(s, f),
Expr::List(s) => fmt::Debug::fmt(s, f),
Expr::Tuple(s) => fmt::Debug::fmt(s, f),
Expr::Map(s) => fmt::Debug::fmt(s, f),
}
}
Expand All @@ -179,6 +181,7 @@ impl Expr<'_> {
| Expr::GetItem(_) => "expression",
Expr::Call(_) => "call",
Expr::List(_) => "list literal",
Expr::Tuple(_) => "tuple literal",
Expr::Map(_) => "map literal",
Expr::Test(_) => "test expression",
Expr::Filter(_) => "filter expression",
Expand All @@ -199,6 +202,7 @@ impl Expr<'_> {
Expr::GetItem(s) => s.span(),
Expr::Call(s) => s.span(),
Expr::List(s) => s.span(),
Expr::Tuple(s) => s.span(),
Expr::Map(s) => s.span(),
}
}
Expand All @@ -207,6 +211,7 @@ impl Expr<'_> {
match self {
Expr::Const(c) => Some(c.value.clone()),
Expr::List(l) => l.as_const(),
Expr::Tuple(t) => t.as_const(),
Expr::Map(m) => m.as_const(),
Expr::UnaryOp(c) => match c.op {
UnaryOpKind::Not => c.expr.as_const().map(|value| Value::from(!value.is_true())),
Expand Down Expand Up @@ -567,6 +572,33 @@ impl List<'_> {
}
}

/// Creates a tuple of values.
#[cfg_attr(feature = "internal_debug", derive(Debug))]
#[cfg_attr(feature = "unstable_machinery_serde", derive(serde::Serialize))]
pub struct Tuple<'a> {
pub items: Vec<Expr<'a>>,
}

impl Tuple<'_> {
pub fn as_const(&self) -> Option<Value> {
use crate::value::Tuple as ValueTuple;

if !self.items.iter().all(|x| matches!(x, Expr::Const(_))) {
return None;
}

let items = self.items.iter();
let sequence = items.filter_map(|expr| match expr {
Expr::Const(v) => Some(v.value.clone()),
_ => None,
});

Some(Value::from_object(ValueTuple::from(
sequence.collect::<Vec<_>>(),
)))
}
}

/// Creates a map of values.
#[cfg_attr(feature = "internal_debug", derive(Debug))]
#[cfg_attr(feature = "unstable_machinery_serde", derive(serde::Serialize))]
Expand Down
7 changes: 7 additions & 0 deletions minijinja/src/compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,13 @@ impl<'source> CodeGenerator<'source> {
}
self.add(Instruction::BuildList(Some(l.items.len())));
}
ast::Expr::Tuple(t) => {
self.set_line_from_span(t.span());
for item in &t.items {
self.compile_expr(item);
}
self.add(Instruction::BuildTuple(Some(t.items.len())));
}
ast::Expr::Map(m) => {
self.set_line_from_span(m.span());
assert_eq!(m.keys.len(), m.values.len());
Expand Down
3 changes: 3 additions & 0 deletions minijinja/src/compiler/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub enum Instruction<'source> {
/// Builds a list of the last n pairs on the stack.
BuildList(Option<usize>),

/// Builds a tuple of the last n pairs on the stack.
BuildTuple(Option<usize>),

/// Unpacks a list into N stack items.
UnpackList(usize),

Expand Down
2 changes: 2 additions & 0 deletions minijinja/src/compiler/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ fn tracker_visit_expr<'a>(expr: &ast::Expr<'a>, state: &mut AssignmentTracker<'a
.for_each(|x| tracker_visit_callarg(x, state));
}
ast::Expr::List(expr) => expr.items.iter().for_each(|x| tracker_visit_expr(x, state)),
ast::Expr::Tuple(expr) => expr.items.iter().for_each(|x| tracker_visit_expr(x, state)),
ast::Expr::Map(expr) => expr.keys.iter().zip(expr.values.iter()).for_each(|(k, v)| {
tracker_visit_expr(k, state);
tracker_visit_expr(v, state);
Expand All @@ -197,6 +198,7 @@ fn track_assign<'a>(expr: &ast::Expr<'a>, state: &mut AssignmentTracker<'a>) {
match expr {
ast::Expr::Var(var) => state.assign(var.id),
ast::Expr::List(list) => list.items.iter().for_each(|x| track_assign(x, state)),
ast::Expr::Tuple(tuple) => tuple.items.iter().for_each(|x| track_assign(x, state)),
_ => {}
}
}
Expand Down
11 changes: 5 additions & 6 deletions minijinja/src/compiler/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,10 @@ impl<'a> Parser<'a> {
}

fn parse_tuple_or_expression(&mut self, span: Span) -> Result<ast::Expr<'a>, Error> {
// MiniJinja does not really have tuples, but it treats the tuple
// syntax the same as lists.
// Parse tuple expressions as actual tuples now
if skip_token!(self, Token::ParenClose) {
return Ok(ast::Expr::List(Spanned::new(
ast::List { items: vec![] },
return Ok(ast::Expr::Tuple(Spanned::new(
ast::Tuple { items: vec![] },
self.stream.expand_span(span),
)));
}
Expand All @@ -737,8 +736,8 @@ impl<'a> Parser<'a> {
}
items.push(ok!(self.parse_expr()));
}
expr = ast::Expr::List(Spanned::new(
ast::List { items },
expr = ast::Expr::Tuple(Spanned::new(
ast::Tuple { items },
self.stream.expand_span(span),
));
} else {
Expand Down
75 changes: 75 additions & 0 deletions minijinja/src/value/argtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,81 @@ impl<'a, T: ArgType<'a, Output = T>> ArgType<'a> for Rest<T> {
}
}

/// A tuple wrapper that renders as Python-style tuples.
///
/// This type works exactly like `Vec<T>` functionally but when
/// rendered it displays as `(item1, item2, ...)` instead of
/// `[item1, item2, ...]`. It's used internally to represent
/// tuple expressions from templates.
///
/// ```
/// # use minijinja::value::{Value, Tuple};
/// let tuple = Tuple::from(vec![Value::from(1), Value::from(2)]);
/// let value = Value::from_object(tuple);
/// assert_eq!(value.to_string(), "(1, 2)");
/// ```
#[derive(Debug, Clone)]
pub struct Tuple(pub Vec<Value>);

impl From<Vec<Value>> for Tuple {
fn from(vec: Vec<Value>) -> Self {
Tuple(vec)
}
}

impl From<&[Value]> for Tuple {
fn from(slice: &[Value]) -> Self {
Tuple(slice.to_vec())
}
}

impl Deref for Tuple {
type Target = Vec<Value>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Tuple {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl Object for Tuple {
fn repr(self: &Arc<Self>) -> ObjectRepr {
ObjectRepr::Seq
}

fn get_value(self: &Arc<Self>, key: &Value) -> Option<Value> {
key.as_usize().and_then(|i| self.0.get(i).cloned())
}

fn enumerate(self: &Arc<Self>) -> Enumerator {
Enumerator::Seq(self.0.len())
}

fn enumerator_len(self: &Arc<Self>) -> Option<usize> {
Some(self.0.len())
}

fn render(self: &Arc<Self>, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("(")?;
for (idx, value) in self.0.iter().enumerate() {
if idx > 0 {
f.write_str(", ")?;
}
std::fmt::Display::fmt(value, f)?;
}
// Special case: single item tuple needs trailing comma
if self.0.len() == 1 {
f.write_str(",")?;
}
f.write_str(")")
}
}

/// Utility to accept keyword arguments.
///
/// Keyword arguments are represented as regular values as the last argument
Expand Down
4 changes: 3 additions & 1 deletion minijinja/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ use crate::value::ops::as_f64;
use crate::value::serialize::transform;
use crate::vm::State;

pub use crate::value::argtypes::{from_args, ArgType, FunctionArgs, FunctionResult, Kwargs, Rest};
pub use crate::value::argtypes::{
from_args, ArgType, FunctionArgs, FunctionResult, Kwargs, Rest, Tuple,
};
pub use crate::value::merge_object::merge_maps;
pub use crate::value::object::{DynObject, Enumerator, Object, ObjectExt, ObjectRepr};

Expand Down
10 changes: 10 additions & 0 deletions minijinja/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,16 @@ impl<'env> Vm<'env> {
v.reverse();
stack.push(Value::from_object(v))
}
Instruction::BuildTuple(n) => {
use crate::value::Tuple;
let count = n.unwrap_or_else(|| stack.pop().try_into().unwrap());
let mut v = Vec::with_capacity(untrusted_size_hint(count));
for _ in 0..count {
v.push(stack.pop());
}
v.reverse();
stack.push(Value::from_object(Tuple::from(v)))
}
Instruction::UnpackList(count) => {
ctx_ok!(self.unpack_list(&mut stack, *count));
}
Expand Down
2 changes: 1 addition & 1 deletion minijinja/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Ok(
} @ 2:11-2:12,
],
} @ 2:8-2:12,
expr: List {
expr: Tuple {
items: [
Const {
value: 1,
Expand Down
6 changes: 3 additions & 3 deletions minijinja/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Ok(
raw: "\n",
} @ 1:15-2:0,
EmitExpr {
expr: List {
expr: Tuple {
items: [
Const {
value: 1,
Expand All @@ -43,7 +43,7 @@ Ok(
raw: "\n",
} @ 2:15-3:0,
EmitExpr {
expr: List {
expr: Tuple {
items: [
Const {
value: 1,
Expand All @@ -55,7 +55,7 @@ Ok(
raw: "\n",
} @ 3:10-4:0,
EmitExpr {
expr: List {
expr: Tuple {
items: [],
} @ 4:3-4:5,
} @ 4:0-4:5,
Expand Down
Loading