Add arguments value 🏓
197
src/eval/call.rs
@ -1,197 +0,0 @@
|
|||||||
use super::*;
|
|
||||||
|
|
||||||
impl Eval for ExprCall {
|
|
||||||
type Output = Value;
|
|
||||||
|
|
||||||
fn eval(&self, ctx: &mut EvalContext) -> Self::Output {
|
|
||||||
let callee = self.callee.eval(ctx);
|
|
||||||
|
|
||||||
if let Value::Func(func) = callee {
|
|
||||||
let func = func.clone();
|
|
||||||
let mut args = self.args.eval(ctx);
|
|
||||||
let returned = func(ctx, &mut args);
|
|
||||||
args.finish(ctx);
|
|
||||||
|
|
||||||
return returned;
|
|
||||||
} else if callee != Value::Error {
|
|
||||||
ctx.diag(error!(
|
|
||||||
self.callee.span(),
|
|
||||||
"expected function, found {}",
|
|
||||||
callee.type_name(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
Value::Error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eval for ExprArgs {
|
|
||||||
type Output = Args;
|
|
||||||
|
|
||||||
fn eval(&self, ctx: &mut EvalContext) -> Self::Output {
|
|
||||||
let mut pos = vec![];
|
|
||||||
let mut named = vec![];
|
|
||||||
|
|
||||||
for arg in &self.items {
|
|
||||||
match arg {
|
|
||||||
Argument::Pos(expr) => {
|
|
||||||
pos.push(expr.eval(ctx).with_span(expr.span()));
|
|
||||||
}
|
|
||||||
Argument::Named(Named { name, expr }) => {
|
|
||||||
named.push((
|
|
||||||
name.string.clone().with_span(name.span),
|
|
||||||
expr.eval(ctx).with_span(expr.span()),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Args { span: self.span, pos, named }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Evaluated arguments to a function.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Args {
|
|
||||||
/// The span of the whole argument list.
|
|
||||||
pub span: Span,
|
|
||||||
/// The positional arguments.
|
|
||||||
pub pos: SpanVec<Value>,
|
|
||||||
/// The named arguments.
|
|
||||||
pub named: Vec<(Spanned<String>, Spanned<Value>)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Args {
|
|
||||||
/// Find and remove the first convertible positional argument.
|
|
||||||
pub fn find<T>(&mut self, ctx: &mut EvalContext) -> Option<T>
|
|
||||||
where
|
|
||||||
T: Cast<Spanned<Value>>,
|
|
||||||
{
|
|
||||||
(0 .. self.pos.len()).find_map(move |i| try_cast(ctx, &mut self.pos, i))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Find and remove the first convertible positional argument, producing an
|
|
||||||
/// error if no match was found.
|
|
||||||
pub fn require<T>(&mut self, ctx: &mut EvalContext, what: &str) -> Option<T>
|
|
||||||
where
|
|
||||||
T: Cast<Spanned<Value>>,
|
|
||||||
{
|
|
||||||
let found = self.find(ctx);
|
|
||||||
if found.is_none() {
|
|
||||||
ctx.diag(error!(self.span, "missing argument: {}", what));
|
|
||||||
}
|
|
||||||
found
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Filter out and remove all convertible positional arguments.
|
|
||||||
pub fn filter<'a, 'b: 'a, T>(
|
|
||||||
&'a mut self,
|
|
||||||
ctx: &'a mut EvalContext<'b>,
|
|
||||||
) -> impl Iterator<Item = T> + Captures<'a> + Captures<'b>
|
|
||||||
where
|
|
||||||
T: Cast<Spanned<Value>>,
|
|
||||||
{
|
|
||||||
let mut i = 0;
|
|
||||||
std::iter::from_fn(move || {
|
|
||||||
while i < self.pos.len() {
|
|
||||||
if let Some(val) = try_cast(ctx, &mut self.pos, i) {
|
|
||||||
return Some(val);
|
|
||||||
}
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
None
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convert and remove the value for the given named argument, producing an
|
|
||||||
/// error if the conversion fails.
|
|
||||||
pub fn get<T>(&mut self, ctx: &mut EvalContext, name: &str) -> Option<T>
|
|
||||||
where
|
|
||||||
T: Cast<Spanned<Value>>,
|
|
||||||
{
|
|
||||||
let index = self.named.iter().position(|(k, _)| k.v.as_str() == name)?;
|
|
||||||
let value = self.named.remove(index).1;
|
|
||||||
cast(ctx, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Drain all remainings arguments into an array and a dictionary.
|
|
||||||
pub fn drain(&mut self) -> (ValueArray, ValueDict) {
|
|
||||||
let array = self.pos.drain(..).map(|s| s.v).collect();
|
|
||||||
let dict = self.named.drain(..).map(|(k, v)| (k.v, v.v)).collect();
|
|
||||||
(array, dict)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Produce "unexpected argument" errors for all remaining arguments.
|
|
||||||
pub fn finish(self, ctx: &mut EvalContext) {
|
|
||||||
let a = self.pos.iter().map(|v| v.as_ref());
|
|
||||||
let b = self.named.iter().map(|(k, v)| (&v.v).with_span(k.span.join(v.span)));
|
|
||||||
for value in a.chain(b) {
|
|
||||||
if value.v != &Value::Error {
|
|
||||||
ctx.diag(error!(value.span, "unexpected argument"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is a workaround because `-> impl Trait + 'a + 'b` does not work.
|
|
||||||
//
|
|
||||||
// See also: https://github.com/rust-lang/rust/issues/49431
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub trait Captures<'a> {}
|
|
||||||
impl<'a, T: ?Sized> Captures<'a> for T {}
|
|
||||||
|
|
||||||
/// Cast the value into `T`, generating an error if the conversion fails.
|
|
||||||
fn cast<T>(ctx: &mut EvalContext, value: Spanned<Value>) -> Option<T>
|
|
||||||
where
|
|
||||||
T: Cast<Spanned<Value>>,
|
|
||||||
{
|
|
||||||
let span = value.span;
|
|
||||||
match T::cast(value) {
|
|
||||||
CastResult::Ok(t) => Some(t),
|
|
||||||
CastResult::Warn(t, m) => {
|
|
||||||
ctx.diag(warning!(span, "{}", m));
|
|
||||||
Some(t)
|
|
||||||
}
|
|
||||||
CastResult::Err(value) => {
|
|
||||||
ctx.diag(error!(
|
|
||||||
span,
|
|
||||||
"expected {}, found {}",
|
|
||||||
T::TYPE_NAME,
|
|
||||||
value.v.type_name()
|
|
||||||
));
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Try to cast the value in the slot into `T`, putting it back if the
|
|
||||||
/// conversion fails.
|
|
||||||
fn try_cast<T>(
|
|
||||||
ctx: &mut EvalContext,
|
|
||||||
vec: &mut Vec<Spanned<Value>>,
|
|
||||||
i: usize,
|
|
||||||
) -> Option<T>
|
|
||||||
where
|
|
||||||
T: Cast<Spanned<Value>>,
|
|
||||||
{
|
|
||||||
// Replace with error placeholder when conversion works since error values
|
|
||||||
// are ignored when generating "unexpected argument" errors.
|
|
||||||
let slot = &mut vec[i];
|
|
||||||
let value = std::mem::replace(slot, Spanned::zero(Value::None));
|
|
||||||
let span = value.span;
|
|
||||||
match T::cast(value) {
|
|
||||||
CastResult::Ok(t) => {
|
|
||||||
vec.remove(i);
|
|
||||||
Some(t)
|
|
||||||
}
|
|
||||||
CastResult::Warn(t, m) => {
|
|
||||||
vec.remove(i);
|
|
||||||
ctx.diag(warning!(span, "{}", m));
|
|
||||||
Some(t)
|
|
||||||
}
|
|
||||||
CastResult::Err(value) => {
|
|
||||||
*slot = value;
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,12 +2,10 @@
|
|||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod value;
|
mod value;
|
||||||
mod call;
|
|
||||||
mod capture;
|
mod capture;
|
||||||
mod ops;
|
mod ops;
|
||||||
mod scope;
|
mod scope;
|
||||||
|
|
||||||
pub use call::*;
|
|
||||||
pub use capture::*;
|
pub use capture::*;
|
||||||
pub use scope::*;
|
pub use scope::*;
|
||||||
pub use value::*;
|
pub use value::*;
|
||||||
@ -344,6 +342,54 @@ impl ExprBinary {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Eval for ExprCall {
|
||||||
|
type Output = Value;
|
||||||
|
|
||||||
|
fn eval(&self, ctx: &mut EvalContext) -> Self::Output {
|
||||||
|
let callee = self.callee.eval(ctx);
|
||||||
|
|
||||||
|
if let Value::Func(func) = callee {
|
||||||
|
let func = func.clone();
|
||||||
|
let mut args = self.args.eval(ctx);
|
||||||
|
let returned = func(ctx, &mut args);
|
||||||
|
args.finish(ctx);
|
||||||
|
|
||||||
|
return returned;
|
||||||
|
} else if callee != Value::Error {
|
||||||
|
ctx.diag(error!(
|
||||||
|
self.callee.span(),
|
||||||
|
"expected function, found {}",
|
||||||
|
callee.type_name(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value::Error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eval for ExprArgs {
|
||||||
|
type Output = ValueArgs;
|
||||||
|
|
||||||
|
fn eval(&self, ctx: &mut EvalContext) -> Self::Output {
|
||||||
|
let items = self
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.map(|arg| match arg {
|
||||||
|
ExprArg::Pos(expr) => ValueArg {
|
||||||
|
name: None,
|
||||||
|
value: expr.eval(ctx).with_span(expr.span()),
|
||||||
|
},
|
||||||
|
ExprArg::Named(Named { name, expr }) => ValueArg {
|
||||||
|
name: Some(name.string.clone().with_span(name.span)),
|
||||||
|
value: expr.eval(ctx).with_span(expr.span()),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
ValueArgs { span: self.span, items }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Eval for ExprLet {
|
impl Eval for ExprLet {
|
||||||
type Output = Value;
|
type Output = Value;
|
||||||
|
|
||||||
|
@ -42,6 +42,8 @@ pub enum Value {
|
|||||||
Template(ValueTemplate),
|
Template(ValueTemplate),
|
||||||
/// An executable function.
|
/// An executable function.
|
||||||
Func(ValueFunc),
|
Func(ValueFunc),
|
||||||
|
/// Arguments to a function.
|
||||||
|
Args(ValueArgs),
|
||||||
/// Any object.
|
/// Any object.
|
||||||
Any(ValueAny),
|
Any(ValueAny),
|
||||||
/// The result of invalid operations.
|
/// The result of invalid operations.
|
||||||
@ -50,11 +52,11 @@ pub enum Value {
|
|||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
/// Create a new template value consisting of a single dynamic node.
|
/// Create a new template value consisting of a single dynamic node.
|
||||||
pub fn template<F>(f: F) -> Self
|
pub fn template<F>(name: impl Into<String>, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn(&mut ExecContext) + 'static,
|
F: Fn(&mut ExecContext) + 'static,
|
||||||
{
|
{
|
||||||
Self::Template(vec![TemplateNode::Any(TemplateAny::new(f))])
|
Self::Template(vec![TemplateNode::Any(TemplateAny::new(name, f))])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The name of the stored value's type.
|
/// The name of the stored value's type.
|
||||||
@ -74,6 +76,7 @@ impl Value {
|
|||||||
Self::Dict(_) => ValueDict::TYPE_NAME,
|
Self::Dict(_) => ValueDict::TYPE_NAME,
|
||||||
Self::Template(_) => ValueTemplate::TYPE_NAME,
|
Self::Template(_) => ValueTemplate::TYPE_NAME,
|
||||||
Self::Func(_) => ValueFunc::TYPE_NAME,
|
Self::Func(_) => ValueFunc::TYPE_NAME,
|
||||||
|
Self::Args(_) => ValueArgs::TYPE_NAME,
|
||||||
Self::Any(v) => v.type_name(),
|
Self::Any(v) => v.type_name(),
|
||||||
Self::Error => "error",
|
Self::Error => "error",
|
||||||
}
|
}
|
||||||
@ -111,8 +114,9 @@ impl Pretty for Value {
|
|||||||
Value::Dict(v) => v.pretty(p),
|
Value::Dict(v) => v.pretty(p),
|
||||||
Value::Template(v) => v.pretty(p),
|
Value::Template(v) => v.pretty(p),
|
||||||
Value::Func(v) => v.pretty(p),
|
Value::Func(v) => v.pretty(p),
|
||||||
|
Value::Args(v) => v.pretty(p),
|
||||||
Value::Any(v) => v.pretty(p),
|
Value::Any(v) => v.pretty(p),
|
||||||
Value::Error => p.push_str("(error)"),
|
Value::Error => p.push_str("<error>"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -194,16 +198,17 @@ impl Pretty for TemplateNode {
|
|||||||
/// A reference-counted dynamic template node (can implement custom behaviour).
|
/// A reference-counted dynamic template node (can implement custom behaviour).
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TemplateAny {
|
pub struct TemplateAny {
|
||||||
|
name: String,
|
||||||
f: Rc<dyn Fn(&mut ExecContext)>,
|
f: Rc<dyn Fn(&mut ExecContext)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TemplateAny {
|
impl TemplateAny {
|
||||||
/// Create a new dynamic template value from a rust function or closure.
|
/// Create a new dynamic template value from a rust function or closure.
|
||||||
pub fn new<F>(f: F) -> Self
|
pub fn new<F>(name: impl Into<String>, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn(&mut ExecContext) + 'static,
|
F: Fn(&mut ExecContext) + 'static,
|
||||||
{
|
{
|
||||||
Self { f: Rc::new(f) }
|
Self { name: name.into(), f: Rc::new(f) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +229,9 @@ impl Deref for TemplateAny {
|
|||||||
|
|
||||||
impl Pretty for TemplateAny {
|
impl Pretty for TemplateAny {
|
||||||
fn pretty(&self, p: &mut Printer) {
|
fn pretty(&self, p: &mut Printer) {
|
||||||
p.push_str("<any>");
|
p.push('<');
|
||||||
|
p.push_str(&self.name);
|
||||||
|
p.push('>');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,14 +245,14 @@ impl Debug for TemplateAny {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ValueFunc {
|
pub struct ValueFunc {
|
||||||
name: String,
|
name: String,
|
||||||
f: Rc<dyn Fn(&mut EvalContext, &mut Args) -> Value>,
|
f: Rc<dyn Fn(&mut EvalContext, &mut ValueArgs) -> Value>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ValueFunc {
|
impl ValueFunc {
|
||||||
/// Create a new function value from a rust function or closure.
|
/// Create a new function value from a rust function or closure.
|
||||||
pub fn new<F>(name: impl Into<String>, f: F) -> Self
|
pub fn new<F>(name: impl Into<String>, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn(&mut EvalContext, &mut Args) -> Value + 'static,
|
F: Fn(&mut EvalContext, &mut ValueArgs) -> Value + 'static,
|
||||||
{
|
{
|
||||||
Self { name: name.into(), f: Rc::new(f) }
|
Self { name: name.into(), f: Rc::new(f) }
|
||||||
}
|
}
|
||||||
@ -259,7 +266,7 @@ impl PartialEq for ValueFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for ValueFunc {
|
impl Deref for ValueFunc {
|
||||||
type Target = dyn Fn(&mut EvalContext, &mut Args) -> Value;
|
type Target = dyn Fn(&mut EvalContext, &mut ValueArgs) -> Value;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
self.f.as_ref()
|
self.f.as_ref()
|
||||||
@ -268,7 +275,9 @@ impl Deref for ValueFunc {
|
|||||||
|
|
||||||
impl Pretty for ValueFunc {
|
impl Pretty for ValueFunc {
|
||||||
fn pretty(&self, p: &mut Printer) {
|
fn pretty(&self, p: &mut Printer) {
|
||||||
|
p.push('<');
|
||||||
p.push_str(&self.name);
|
p.push_str(&self.name);
|
||||||
|
p.push('>');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,6 +287,180 @@ impl Debug for ValueFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Evaluated arguments to a function.
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct ValueArgs {
|
||||||
|
/// The span of the whole argument list.
|
||||||
|
pub span: Span,
|
||||||
|
/// The arguments.
|
||||||
|
pub items: Vec<ValueArg>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ValueArgs {
|
||||||
|
/// Find and remove the first convertible positional argument.
|
||||||
|
pub fn find<T>(&mut self, ctx: &mut EvalContext) -> Option<T>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
|
(0 .. self.items.len()).find_map(move |i| self.try_take(ctx, i))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Find and remove the first convertible positional argument, producing an
|
||||||
|
/// error if no match was found.
|
||||||
|
pub fn require<T>(&mut self, ctx: &mut EvalContext, what: &str) -> Option<T>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
|
let found = self.find(ctx);
|
||||||
|
if found.is_none() {
|
||||||
|
ctx.diag(error!(self.span, "missing argument: {}", what));
|
||||||
|
}
|
||||||
|
found
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Filter out and remove all convertible positional arguments.
|
||||||
|
pub fn filter<'a, 'b: 'a, T>(
|
||||||
|
&'a mut self,
|
||||||
|
ctx: &'a mut EvalContext<'b>,
|
||||||
|
) -> impl Iterator<Item = T> + Captures<'a> + Captures<'b>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
|
let mut i = 0;
|
||||||
|
std::iter::from_fn(move || {
|
||||||
|
while i < self.items.len() {
|
||||||
|
if let Some(val) = self.try_take(ctx, i) {
|
||||||
|
return Some(val);
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
None
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert and remove the value for the given named argument, producing an
|
||||||
|
/// error if the conversion fails.
|
||||||
|
pub fn get<T>(&mut self, ctx: &mut EvalContext, name: &str) -> Option<T>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
|
let index = self
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.position(|arg| arg.name.as_ref().map(|s| s.v.as_str()) == Some(name))?;
|
||||||
|
|
||||||
|
let value = self.items.remove(index).value;
|
||||||
|
self.cast(ctx, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Produce "unexpected argument" errors for all remaining arguments.
|
||||||
|
pub fn finish(self, ctx: &mut EvalContext) {
|
||||||
|
for arg in &self.items {
|
||||||
|
if arg.value.v != Value::Error {
|
||||||
|
ctx.diag(error!(arg.span(), "unexpected argument"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cast the value into `T`, generating an error if the conversion fails.
|
||||||
|
fn cast<T>(&self, ctx: &mut EvalContext, value: Spanned<Value>) -> Option<T>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
|
let span = value.span;
|
||||||
|
match T::cast(value) {
|
||||||
|
CastResult::Ok(t) => Some(t),
|
||||||
|
CastResult::Warn(t, m) => {
|
||||||
|
ctx.diag(warning!(span, "{}", m));
|
||||||
|
Some(t)
|
||||||
|
}
|
||||||
|
CastResult::Err(value) => {
|
||||||
|
ctx.diag(error!(
|
||||||
|
span,
|
||||||
|
"expected {}, found {}",
|
||||||
|
T::TYPE_NAME,
|
||||||
|
value.v.type_name()
|
||||||
|
));
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to take and cast a positional argument in the i'th slot into `T`,
|
||||||
|
/// putting it back if the conversion fails.
|
||||||
|
fn try_take<T>(&mut self, ctx: &mut EvalContext, i: usize) -> Option<T>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
|
let slot = &mut self.items[i];
|
||||||
|
if slot.name.is_some() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let value = std::mem::replace(&mut slot.value, Spanned::zero(Value::None));
|
||||||
|
let span = value.span;
|
||||||
|
match T::cast(value) {
|
||||||
|
CastResult::Ok(t) => {
|
||||||
|
self.items.remove(i);
|
||||||
|
Some(t)
|
||||||
|
}
|
||||||
|
CastResult::Warn(t, m) => {
|
||||||
|
self.items.remove(i);
|
||||||
|
ctx.diag(warning!(span, "{}", m));
|
||||||
|
Some(t)
|
||||||
|
}
|
||||||
|
CastResult::Err(value) => {
|
||||||
|
slot.value = value;
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pretty for ValueArgs {
|
||||||
|
fn pretty(&self, p: &mut Printer) {
|
||||||
|
p.push('<');
|
||||||
|
p.join(&self.items, ", ", |item, p| item.pretty(p));
|
||||||
|
p.push('>');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a workaround because `-> impl Trait + 'a + 'b` does not work.
|
||||||
|
//
|
||||||
|
// See also: https://github.com/rust-lang/rust/issues/49431
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub trait Captures<'a> {}
|
||||||
|
impl<'a, T: ?Sized> Captures<'a> for T {}
|
||||||
|
|
||||||
|
/// An argument to a function call: `12` or `draw: false`.
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct ValueArg {
|
||||||
|
/// The name of the argument (`None` for positional arguments).
|
||||||
|
pub name: Option<Spanned<String>>,
|
||||||
|
/// The value of the argument.
|
||||||
|
pub value: Spanned<Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ValueArg {
|
||||||
|
/// The source code location.
|
||||||
|
pub fn span(&self) -> Span {
|
||||||
|
match &self.name {
|
||||||
|
Some(name) => name.span.join(self.value.span),
|
||||||
|
None => self.value.span,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pretty for ValueArg {
|
||||||
|
fn pretty(&self, p: &mut Printer) {
|
||||||
|
if let Some(name) = &self.name {
|
||||||
|
p.push_str(&name.v);
|
||||||
|
p.push_str(": ");
|
||||||
|
}
|
||||||
|
self.value.v.pretty(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A wrapper around a dynamic value.
|
/// A wrapper around a dynamic value.
|
||||||
pub struct ValueAny(Box<dyn Bounds>);
|
pub struct ValueAny(Box<dyn Bounds>);
|
||||||
|
|
||||||
@ -454,7 +637,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_primitive {
|
macro_rules! primitive {
|
||||||
($type:ty:
|
($type:ty:
|
||||||
$type_name:literal,
|
$type_name:literal,
|
||||||
$variant:path
|
$variant:path
|
||||||
@ -482,28 +665,29 @@ macro_rules! impl_primitive {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_primitive! { bool: "boolean", Value::Bool }
|
primitive! { bool: "boolean", Value::Bool }
|
||||||
impl_primitive! { i64: "integer", Value::Int }
|
primitive! { i64: "integer", Value::Int }
|
||||||
impl_primitive! {
|
primitive! {
|
||||||
f64: "float",
|
f64: "float",
|
||||||
Value::Float,
|
Value::Float,
|
||||||
Value::Int(v) => v as f64,
|
Value::Int(v) => v as f64,
|
||||||
}
|
}
|
||||||
impl_primitive! { Length: "length", Value::Length }
|
primitive! { Length: "length", Value::Length }
|
||||||
impl_primitive! { Angle: "angle", Value::Angle }
|
primitive! { Angle: "angle", Value::Angle }
|
||||||
impl_primitive! { Relative: "relative", Value::Relative }
|
primitive! { Relative: "relative", Value::Relative }
|
||||||
impl_primitive! {
|
primitive! {
|
||||||
Linear: "linear",
|
Linear: "linear",
|
||||||
Value::Linear,
|
Value::Linear,
|
||||||
Value::Length(v) => v.into(),
|
Value::Length(v) => v.into(),
|
||||||
Value::Relative(v) => v.into(),
|
Value::Relative(v) => v.into(),
|
||||||
}
|
}
|
||||||
impl_primitive! { Color: "color", Value::Color }
|
primitive! { Color: "color", Value::Color }
|
||||||
impl_primitive! { String: "string", Value::Str }
|
primitive! { String: "string", Value::Str }
|
||||||
impl_primitive! { ValueArray: "array", Value::Array }
|
primitive! { ValueArray: "array", Value::Array }
|
||||||
impl_primitive! { ValueDict: "dictionary", Value::Dict }
|
primitive! { ValueDict: "dictionary", Value::Dict }
|
||||||
impl_primitive! { ValueTemplate: "template", Value::Template }
|
primitive! { ValueTemplate: "template", Value::Template }
|
||||||
impl_primitive! { ValueFunc: "function", Value::Func }
|
primitive! { ValueFunc: "function", Value::Func }
|
||||||
|
primitive! { ValueArgs: "arguments", Value::Args }
|
||||||
|
|
||||||
impl From<&str> for Value {
|
impl From<&str> for Value {
|
||||||
fn from(v: &str) -> Self {
|
fn from(v: &str) -> Self {
|
||||||
@ -519,11 +703,23 @@ impl From<ValueAny> for Value {
|
|||||||
|
|
||||||
/// Make a type usable as a [`Value`].
|
/// Make a type usable as a [`Value`].
|
||||||
///
|
///
|
||||||
/// Given a type `T`, this always implements the following traits:
|
/// Given a type `T`, this implements the following traits:
|
||||||
/// - [`Type`] for `T`,
|
/// - [`Type`] for `T`,
|
||||||
/// - [`Cast<Value>`](Cast) for `T`.
|
/// - [`Cast<Value>`](Cast) for `T`.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// Make a type `FontFamily` that can be cast from a [`Value::Any`] variant
|
||||||
|
/// containing a `FontFamily` or from a string.
|
||||||
|
/// ```
|
||||||
|
/// # use typst::typify;
|
||||||
|
/// # enum FontFamily { Named(String) }
|
||||||
|
/// typify! {
|
||||||
|
/// FontFamily: "font family",
|
||||||
|
/// Value::Str(string) => Self::Named(string.to_lowercase())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! impl_type {
|
macro_rules! typify {
|
||||||
($type:ty:
|
($type:ty:
|
||||||
$type_name:literal
|
$type_name:literal
|
||||||
$(, $pattern:pat => $out:expr)*
|
$(, $pattern:pat => $out:expr)*
|
||||||
@ -575,23 +771,19 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pretty_print_simple_values() {
|
fn test_pretty_print_value() {
|
||||||
|
// Simple values.
|
||||||
test_pretty(Value::None, "none");
|
test_pretty(Value::None, "none");
|
||||||
test_pretty(false, "false");
|
test_pretty(false, "false");
|
||||||
test_pretty(12.4, "12.4");
|
test_pretty(12, "12");
|
||||||
|
test_pretty(3.14, "3.14");
|
||||||
test_pretty(Length::pt(5.5), "5.5pt");
|
test_pretty(Length::pt(5.5), "5.5pt");
|
||||||
test_pretty(Angle::deg(90.0), "90.0deg");
|
test_pretty(Angle::deg(90.0), "90.0deg");
|
||||||
test_pretty(Relative::ONE / 2.0, "50.0%");
|
test_pretty(Relative::ONE / 2.0, "50.0%");
|
||||||
test_pretty(Relative::new(0.3) + Length::cm(2.0), "30.0% + 2.0cm");
|
test_pretty(Relative::new(0.3) + Length::cm(2.0), "30.0% + 2.0cm");
|
||||||
test_pretty(Color::Rgba(RgbaColor::new(1, 1, 1, 0xff)), "#010101");
|
test_pretty(Color::Rgba(RgbaColor::new(1, 1, 1, 0xff)), "#010101");
|
||||||
test_pretty("hello", r#""hello""#);
|
test_pretty("hello", r#""hello""#);
|
||||||
test_pretty(ValueFunc::new("nil", |_, _| Value::None), "nil");
|
|
||||||
test_pretty(ValueAny::new(1), "1");
|
|
||||||
test_pretty(Value::Error, "(error)");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_pretty_print_collections() {
|
|
||||||
// Array.
|
// Array.
|
||||||
test_pretty(Value::Array(vec![]), "()");
|
test_pretty(Value::Array(vec![]), "()");
|
||||||
test_pretty(vec![Value::None], "(none,)");
|
test_pretty(vec![Value::None], "(none,)");
|
||||||
@ -599,9 +791,47 @@ mod tests {
|
|||||||
|
|
||||||
// Dictionary.
|
// Dictionary.
|
||||||
let mut dict = BTreeMap::new();
|
let mut dict = BTreeMap::new();
|
||||||
|
test_pretty(dict.clone(), "(:)");
|
||||||
dict.insert("one".into(), Value::Int(1));
|
dict.insert("one".into(), Value::Int(1));
|
||||||
|
test_pretty(dict.clone(), "(one: 1)");
|
||||||
dict.insert("two".into(), Value::Bool(false));
|
dict.insert("two".into(), Value::Bool(false));
|
||||||
test_pretty(BTreeMap::new(), "(:)");
|
|
||||||
test_pretty(dict, "(one: 1, two: false)");
|
test_pretty(dict, "(one: 1, two: false)");
|
||||||
|
|
||||||
|
// Template.
|
||||||
|
test_pretty(
|
||||||
|
vec![
|
||||||
|
TemplateNode::Tree {
|
||||||
|
tree: Rc::new(vec![Node::Strong]),
|
||||||
|
map: HashMap::new(),
|
||||||
|
},
|
||||||
|
TemplateNode::Any(TemplateAny::new("example", |_| {})),
|
||||||
|
],
|
||||||
|
"[*<example>]",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Function and arguments.
|
||||||
|
test_pretty(ValueFunc::new("nil", |_, _| Value::None), "<nil>");
|
||||||
|
test_pretty(
|
||||||
|
ValueArgs {
|
||||||
|
span: Span::ZERO,
|
||||||
|
items: vec![
|
||||||
|
ValueArg {
|
||||||
|
name: Some(Spanned::zero("a".into())),
|
||||||
|
value: Spanned::zero(Value::Int(1)),
|
||||||
|
},
|
||||||
|
ValueArg {
|
||||||
|
name: None,
|
||||||
|
value: Spanned::zero(Value::Int(2)),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"<a: 1, 2>",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Any.
|
||||||
|
test_pretty(ValueAny::new(1), "1");
|
||||||
|
|
||||||
|
// Error.
|
||||||
|
test_pretty(Value::Error, "<error>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,17 +59,23 @@ pub trait ExecWith {
|
|||||||
impl ExecWith for Tree {
|
impl ExecWith for Tree {
|
||||||
fn exec_with(&self, ctx: &mut ExecContext, map: &ExprMap) {
|
fn exec_with(&self, ctx: &mut ExecContext, map: &ExprMap) {
|
||||||
for node in self {
|
for node in self {
|
||||||
match node {
|
node.exec_with(ctx, map);
|
||||||
Node::Text(text) => ctx.push_text(text),
|
}
|
||||||
Node::Space => ctx.push_space(),
|
}
|
||||||
Node::Linebreak => ctx.apply_linebreak(),
|
}
|
||||||
Node::Parbreak => ctx.apply_parbreak(),
|
|
||||||
Node::Strong => ctx.state.font.strong ^= true,
|
impl ExecWith for Node {
|
||||||
Node::Emph => ctx.state.font.emph ^= true,
|
fn exec_with(&self, ctx: &mut ExecContext, map: &ExprMap) {
|
||||||
Node::Heading(heading) => heading.exec_with(ctx, map),
|
match self {
|
||||||
Node::Raw(raw) => raw.exec(ctx),
|
Node::Text(text) => ctx.push_text(text),
|
||||||
Node::Expr(expr) => map[&(expr as *const _)].exec(ctx),
|
Node::Space => ctx.push_space(),
|
||||||
}
|
Node::Linebreak => ctx.apply_linebreak(),
|
||||||
|
Node::Parbreak => ctx.apply_parbreak(),
|
||||||
|
Node::Strong => ctx.state.font.strong ^= true,
|
||||||
|
Node::Emph => ctx.state.font.emph ^= true,
|
||||||
|
Node::Heading(heading) => heading.exec_with(ctx, map),
|
||||||
|
Node::Raw(raw) => raw.exec(ctx),
|
||||||
|
Node::Expr(expr) => map[&(expr as *const _)].exec(ctx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use crate::prelude::*;
|
|||||||
///
|
///
|
||||||
/// # Return value
|
/// # Return value
|
||||||
/// The name of the value's type as a string.
|
/// The name of the value's type as a string.
|
||||||
pub fn type_(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn type_(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
if let Some(value) = args.require::<Value>(ctx, "value") {
|
if let Some(value) = args.require::<Value>(ctx, "value") {
|
||||||
value.type_name().into()
|
value.type_name().into()
|
||||||
} else {
|
} else {
|
||||||
|
@ -10,12 +10,12 @@ use crate::prelude::*;
|
|||||||
///
|
///
|
||||||
/// # Positional arguments
|
/// # Positional arguments
|
||||||
/// - Path to image file: of type `string`.
|
/// - Path to image file: of type `string`.
|
||||||
pub fn image(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn image(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
let path = args.require::<Spanned<String>>(ctx, "path to image file");
|
let path = args.require::<Spanned<String>>(ctx, "path to image file");
|
||||||
let width = args.get(ctx, "width");
|
let width = args.get(ctx, "width");
|
||||||
let height = args.get(ctx, "height");
|
let height = args.get(ctx, "height");
|
||||||
|
|
||||||
Value::template(move |ctx| {
|
Value::template("image", move |ctx| {
|
||||||
if let Some(path) = &path {
|
if let Some(path) = &path {
|
||||||
let loaded = ctx.env.resources.load(&path.v, ImageResource::parse);
|
let loaded = ctx.env.resources.load(&path.v, ImageResource::parse);
|
||||||
if let Some((res, img)) = loaded {
|
if let Some((res, img)) = loaded {
|
||||||
|
@ -26,14 +26,14 @@ use crate::prelude::*;
|
|||||||
/// - `top`
|
/// - `top`
|
||||||
/// - `bottom`
|
/// - `bottom`
|
||||||
/// - `center`
|
/// - `center`
|
||||||
pub fn align(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
let first = args.find(ctx);
|
let first = args.find(ctx);
|
||||||
let second = args.find(ctx);
|
let second = args.find(ctx);
|
||||||
let hor = args.get(ctx, "horizontal");
|
let hor = args.get(ctx, "horizontal");
|
||||||
let ver = args.get(ctx, "vertical");
|
let ver = args.get(ctx, "vertical");
|
||||||
let body = args.find::<ValueTemplate>(ctx);
|
let body = args.find::<ValueTemplate>(ctx);
|
||||||
|
|
||||||
Value::template(move |ctx| {
|
Value::template("align", move |ctx| {
|
||||||
let snapshot = ctx.state.clone();
|
let snapshot = ctx.state.clone();
|
||||||
|
|
||||||
let mut had = Gen::uniform(false);
|
let mut had = Gen::uniform(false);
|
||||||
@ -157,7 +157,7 @@ impl Switch for Alignment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_type! {
|
typify! {
|
||||||
Alignment: "alignment",
|
Alignment: "alignment",
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,7 +188,7 @@ impl Display for Alignment {
|
|||||||
/// - `rtl` (right to left)
|
/// - `rtl` (right to left)
|
||||||
/// - `ttb` (top to bottom)
|
/// - `ttb` (top to bottom)
|
||||||
/// - `btt` (bottom to top)
|
/// - `btt` (bottom to top)
|
||||||
pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn box_(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
let width = args.get(ctx, "width");
|
let width = args.get(ctx, "width");
|
||||||
let height = args.get(ctx, "height");
|
let height = args.get(ctx, "height");
|
||||||
let main = args.get(ctx, "main-dir");
|
let main = args.get(ctx, "main-dir");
|
||||||
@ -196,7 +196,7 @@ pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
|||||||
let color = args.get(ctx, "color");
|
let color = args.get(ctx, "color");
|
||||||
let body = args.find::<ValueTemplate>(ctx);
|
let body = args.find::<ValueTemplate>(ctx);
|
||||||
|
|
||||||
Value::template(move |ctx| {
|
Value::template("box", move |ctx| {
|
||||||
let snapshot = ctx.state.clone();
|
let snapshot = ctx.state.clone();
|
||||||
|
|
||||||
ctx.set_dirs(Gen::new(main, cross));
|
ctx.set_dirs(Gen::new(main, cross));
|
||||||
@ -230,7 +230,7 @@ pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_type! {
|
typify! {
|
||||||
Dir: "direction"
|
Dir: "direction"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,7 +238,7 @@ impl_type! {
|
|||||||
///
|
///
|
||||||
/// # Positional arguments
|
/// # Positional arguments
|
||||||
/// - Amount of spacing: of type `linear` relative to current font size.
|
/// - Amount of spacing: of type `linear` relative to current font size.
|
||||||
pub fn h(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn h(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
spacing(ctx, args, SpecAxis::Horizontal)
|
spacing(ctx, args, SpecAxis::Horizontal)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,15 +246,15 @@ pub fn h(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
|||||||
///
|
///
|
||||||
/// # Positional arguments
|
/// # Positional arguments
|
||||||
/// - Amount of spacing: of type `linear` relative to current font size.
|
/// - Amount of spacing: of type `linear` relative to current font size.
|
||||||
pub fn v(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn v(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
spacing(ctx, args, SpecAxis::Vertical)
|
spacing(ctx, args, SpecAxis::Vertical)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Apply spacing along a specific axis.
|
/// Apply spacing along a specific axis.
|
||||||
fn spacing(ctx: &mut EvalContext, args: &mut Args, axis: SpecAxis) -> Value {
|
fn spacing(ctx: &mut EvalContext, args: &mut ValueArgs, axis: SpecAxis) -> Value {
|
||||||
let spacing: Option<Linear> = args.require(ctx, "spacing");
|
let spacing: Option<Linear> = args.require(ctx, "spacing");
|
||||||
|
|
||||||
Value::template(move |ctx| {
|
Value::template("spacing", move |ctx| {
|
||||||
if let Some(linear) = spacing {
|
if let Some(linear) = spacing {
|
||||||
let amount = linear.resolve(ctx.state.font.font_size());
|
let amount = linear.resolve(ctx.state.font.font_size());
|
||||||
let spacing = NodeSpacing { amount, softness: Softness::Hard };
|
let spacing = NodeSpacing { amount, softness: Softness::Hard };
|
||||||
@ -286,7 +286,7 @@ fn spacing(ctx: &mut EvalContext, args: &mut Args, axis: SpecAxis) -> Value {
|
|||||||
/// - Flip width and height: `flip`, of type `bool`.
|
/// - Flip width and height: `flip`, of type `bool`.
|
||||||
/// - Main layouting direction: `main-dir`, of type `direction`.
|
/// - Main layouting direction: `main-dir`, of type `direction`.
|
||||||
/// - Cross layouting direction: `cross-dir`, of type `direction`.
|
/// - Cross layouting direction: `cross-dir`, of type `direction`.
|
||||||
pub fn page(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn page(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
let paper = args.find::<Spanned<String>>(ctx).and_then(|name| {
|
let paper = args.find::<Spanned<String>>(ctx).and_then(|name| {
|
||||||
Paper::from_name(&name.v).or_else(|| {
|
Paper::from_name(&name.v).or_else(|| {
|
||||||
ctx.diag(error!(name.span, "invalid paper name"));
|
ctx.diag(error!(name.span, "invalid paper name"));
|
||||||
@ -305,7 +305,7 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
|||||||
let cross = args.get(ctx, "cross-dir");
|
let cross = args.get(ctx, "cross-dir");
|
||||||
let body = args.find::<ValueTemplate>(ctx);
|
let body = args.find::<ValueTemplate>(ctx);
|
||||||
|
|
||||||
Value::template(move |ctx| {
|
Value::template("page", move |ctx| {
|
||||||
let snapshot = ctx.state.clone();
|
let snapshot = ctx.state.clone();
|
||||||
|
|
||||||
if let Some(paper) = paper {
|
if let Some(paper) = paper {
|
||||||
@ -370,8 +370,8 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `pagebreak`: Start a new page.
|
/// `pagebreak`: Start a new page.
|
||||||
pub fn pagebreak(_: &mut EvalContext, _: &mut Args) -> Value {
|
pub fn pagebreak(_: &mut EvalContext, _: &mut ValueArgs) -> Value {
|
||||||
Value::template(move |ctx| {
|
Value::template("pagebreak", move |ctx| {
|
||||||
ctx.end_page_group(|_| true);
|
ctx.end_page_group(|_| true);
|
||||||
ctx.start_page_group(Softness::Hard);
|
ctx.start_page_group(Softness::Hard);
|
||||||
})
|
})
|
||||||
|
@ -54,7 +54,7 @@ use crate::prelude::*;
|
|||||||
/// - `expanded`
|
/// - `expanded`
|
||||||
/// - `extra-expanded`
|
/// - `extra-expanded`
|
||||||
/// - `ultra-expanded`
|
/// - `ultra-expanded`
|
||||||
pub fn font(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn font(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
let size = args.find::<Linear>(ctx);
|
let size = args.find::<Linear>(ctx);
|
||||||
let list: Vec<_> = args.filter::<FontFamily>(ctx).map(|f| f.to_string()).collect();
|
let list: Vec<_> = args.filter::<FontFamily>(ctx).map(|f| f.to_string()).collect();
|
||||||
let style = args.get(ctx, "style");
|
let style = args.get(ctx, "style");
|
||||||
@ -65,7 +65,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
|||||||
let monospace = args.get(ctx, "monospace");
|
let monospace = args.get(ctx, "monospace");
|
||||||
let body = args.find::<ValueTemplate>(ctx);
|
let body = args.find::<ValueTemplate>(ctx);
|
||||||
|
|
||||||
Value::template(move |ctx| {
|
Value::template("font", move |ctx| {
|
||||||
let snapshot = ctx.state.clone();
|
let snapshot = ctx.state.clone();
|
||||||
|
|
||||||
if let Some(linear) = size {
|
if let Some(linear) = size {
|
||||||
@ -145,7 +145,7 @@ impl Display for FontFamily {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_type! {
|
typify! {
|
||||||
FontFamilies: "font family or array of font families",
|
FontFamilies: "font family or array of font families",
|
||||||
Value::Str(string) => Self(vec![FontFamily::Named(string.to_lowercase())]),
|
Value::Str(string) => Self(vec![FontFamily::Named(string.to_lowercase())]),
|
||||||
Value::Array(values) => Self(values
|
Value::Array(values) => Self(values
|
||||||
@ -156,16 +156,16 @@ impl_type! {
|
|||||||
#(family: FontFamily) => Self(vec![family]),
|
#(family: FontFamily) => Self(vec![family]),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_type! {
|
typify! {
|
||||||
FontFamily: "font family",
|
FontFamily: "font family",
|
||||||
Value::Str(string) => Self::Named(string.to_lowercase())
|
Value::Str(string) => Self::Named(string.to_lowercase())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_type! {
|
typify! {
|
||||||
FontStyle: "font style"
|
FontStyle: "font style"
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_type! {
|
typify! {
|
||||||
FontWeight: "font weight",
|
FontWeight: "font weight",
|
||||||
Value::Int(number) => {
|
Value::Int(number) => {
|
||||||
let [min, max] = [Self::THIN, Self::BLACK];
|
let [min, max] = [Self::THIN, Self::BLACK];
|
||||||
@ -180,7 +180,7 @@ impl_type! {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_type! {
|
typify! {
|
||||||
FontStretch: "font stretch"
|
FontStretch: "font stretch"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,7 +191,7 @@ impl_type! {
|
|||||||
/// - Green component: of type `float`, between 0.0 and 1.0.
|
/// - Green component: of type `float`, between 0.0 and 1.0.
|
||||||
/// - Blue component: of type `float`, between 0.0 and 1.0.
|
/// - Blue component: of type `float`, between 0.0 and 1.0.
|
||||||
/// - Alpha component: optional, of type `float`, between 0.0 and 1.0.
|
/// - Alpha component: optional, of type `float`, between 0.0 and 1.0.
|
||||||
pub fn rgb(ctx: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn rgb(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
let r = args.require(ctx, "red component");
|
let r = args.require(ctx, "red component");
|
||||||
let g = args.require(ctx, "green component");
|
let g = args.require(ctx, "green component");
|
||||||
let b = args.require(ctx, "blue component");
|
let b = args.require(ctx, "blue component");
|
||||||
|
@ -51,29 +51,29 @@ fn collection<T: Collection>(p: &mut Parser, mut collection: T) -> T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an expression or a named pair.
|
/// Parse an expression or a named pair.
|
||||||
fn argument(p: &mut Parser) -> Option<Argument> {
|
fn argument(p: &mut Parser) -> Option<ExprArg> {
|
||||||
let first = expr(p)?;
|
let first = expr(p)?;
|
||||||
if p.eat_if(Token::Colon) {
|
if p.eat_if(Token::Colon) {
|
||||||
if let Expr::Ident(name) = first {
|
if let Expr::Ident(name) = first {
|
||||||
Some(Argument::Named(Named { name, expr: expr(p)? }))
|
Some(ExprArg::Named(Named { name, expr: expr(p)? }))
|
||||||
} else {
|
} else {
|
||||||
p.diag(error!(first.span(), "expected identifier"));
|
p.diag(error!(first.span(), "expected identifier"));
|
||||||
expr(p);
|
expr(p);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Some(Argument::Pos(first))
|
Some(ExprArg::Pos(first))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Abstraction for comma-separated list of expression / named pairs.
|
/// Abstraction for comma-separated list of expression / named pairs.
|
||||||
trait Collection {
|
trait Collection {
|
||||||
fn push_arg(&mut self, p: &mut Parser, arg: Argument);
|
fn push_arg(&mut self, p: &mut Parser, arg: ExprArg);
|
||||||
fn push_comma(&mut self) {}
|
fn push_comma(&mut self) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Collection for Vec<Argument> {
|
impl Collection for Vec<ExprArg> {
|
||||||
fn push_arg(&mut self, _: &mut Parser, arg: Argument) {
|
fn push_arg(&mut self, _: &mut Parser, arg: ExprArg) {
|
||||||
self.push(arg);
|
self.push(arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,23 +99,23 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Collection for State {
|
impl Collection for State {
|
||||||
fn push_arg(&mut self, p: &mut Parser, arg: Argument) {
|
fn push_arg(&mut self, p: &mut Parser, arg: ExprArg) {
|
||||||
match self {
|
match self {
|
||||||
Self::Unknown => match arg {
|
Self::Unknown => match arg {
|
||||||
Argument::Pos(expr) => *self = Self::Expr(expr),
|
ExprArg::Pos(expr) => *self = Self::Expr(expr),
|
||||||
Argument::Named(named) => *self = Self::Dict(vec![named]),
|
ExprArg::Named(named) => *self = Self::Dict(vec![named]),
|
||||||
},
|
},
|
||||||
Self::Expr(prev) => match arg {
|
Self::Expr(prev) => match arg {
|
||||||
Argument::Pos(expr) => *self = Self::Array(vec![take(prev), expr]),
|
ExprArg::Pos(expr) => *self = Self::Array(vec![take(prev), expr]),
|
||||||
Argument::Named(_) => diag(p, arg),
|
ExprArg::Named(_) => diag(p, arg),
|
||||||
},
|
},
|
||||||
Self::Array(array) => match arg {
|
Self::Array(array) => match arg {
|
||||||
Argument::Pos(expr) => array.push(expr),
|
ExprArg::Pos(expr) => array.push(expr),
|
||||||
Argument::Named(_) => diag(p, arg),
|
ExprArg::Named(_) => diag(p, arg),
|
||||||
},
|
},
|
||||||
Self::Dict(dict) => match arg {
|
Self::Dict(dict) => match arg {
|
||||||
Argument::Pos(_) => diag(p, arg),
|
ExprArg::Pos(_) => diag(p, arg),
|
||||||
Argument::Named(named) => dict.push(named),
|
ExprArg::Named(named) => dict.push(named),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -135,9 +135,9 @@ fn take(expr: &mut Expr) -> Expr {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn diag(p: &mut Parser, arg: Argument) {
|
fn diag(p: &mut Parser, arg: ExprArg) {
|
||||||
p.diag(error!(arg.span(), "{}", match arg {
|
p.diag(error!(arg.span(), "{}", match arg {
|
||||||
Argument::Pos(_) => "expected named pair, found expression",
|
ExprArg::Pos(_) => "expected named pair, found expression",
|
||||||
Argument::Named(_) => "expected expression, found named pair",
|
ExprArg::Named(_) => "expected expression, found named pair",
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
@ -198,11 +198,11 @@ fn bracket_call(p: &mut Parser) -> Option<Expr> {
|
|||||||
let mut inner = inner?;
|
let mut inner = inner?;
|
||||||
if let Some(body) = body {
|
if let Some(body) = body {
|
||||||
inner.span.expand(body.span());
|
inner.span.expand(body.span());
|
||||||
inner.args.items.push(Argument::Pos(body));
|
inner.args.items.push(ExprArg::Pos(body));
|
||||||
}
|
}
|
||||||
|
|
||||||
while let Some(mut top) = outer.pop() {
|
while let Some(mut top) = outer.pop() {
|
||||||
top.args.items.push(Argument::Pos(Expr::Call(inner)));
|
top.args.items.push(ExprArg::Pos(Expr::Call(inner)));
|
||||||
inner = top;
|
inner = top;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
pub use crate::diag::{Feedback, Pass};
|
pub use crate::diag::{Feedback, Pass};
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::eval::{
|
pub use crate::eval::{
|
||||||
Args, CastResult, Eval, EvalContext, TemplateAny, TemplateNode, Value, ValueAny,
|
CastResult, Eval, EvalContext, TemplateAny, TemplateNode, Value, ValueAny, ValueArgs,
|
||||||
ValueArray, ValueDict, ValueTemplate,
|
ValueArray, ValueDict, ValueTemplate,
|
||||||
};
|
};
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
@ -13,4 +13,4 @@ pub use crate::geom::*;
|
|||||||
pub use crate::layout::Node;
|
pub use crate::layout::Node;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::syntax::{Span, Spanned, WithSpan};
|
pub use crate::syntax::{Span, Spanned, WithSpan};
|
||||||
pub use crate::{error, impl_type, warning};
|
pub use crate::{error, typify, warning};
|
||||||
|
@ -105,7 +105,7 @@ impl Pretty for str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_pretty_display {
|
macro_rules! pretty_display {
|
||||||
($($type:ty),* $(,)?) => {
|
($($type:ty),* $(,)?) => {
|
||||||
$(impl Pretty for $type {
|
$(impl Pretty for $type {
|
||||||
fn pretty(&self, p: &mut Printer) {
|
fn pretty(&self, p: &mut Printer) {
|
||||||
@ -115,7 +115,7 @@ macro_rules! impl_pretty_display {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_pretty_display! {
|
pretty_display! {
|
||||||
bool,
|
bool,
|
||||||
Length,
|
Length,
|
||||||
Angle,
|
Angle,
|
||||||
|
@ -531,7 +531,7 @@ impl ExprCall {
|
|||||||
// Function name.
|
// Function name.
|
||||||
self.callee.pretty(p);
|
self.callee.pretty(p);
|
||||||
|
|
||||||
let mut write_args = |items: &[Argument]| {
|
let mut write_args = |items: &[ExprArg]| {
|
||||||
if !items.is_empty() {
|
if !items.is_empty() {
|
||||||
p.push(' ');
|
p.push(' ');
|
||||||
p.join(items, ", ", |item, p| item.pretty(p));
|
p.join(items, ", ", |item, p| item.pretty(p));
|
||||||
@ -542,7 +542,7 @@ impl ExprCall {
|
|||||||
// This can written as a chain.
|
// This can written as a chain.
|
||||||
//
|
//
|
||||||
// Example: Transforms "#[v][[f]]" => "#[v | f]".
|
// Example: Transforms "#[v][[f]]" => "#[v | f]".
|
||||||
[head @ .., Argument::Pos(Expr::Call(call))] => {
|
[head @ .., ExprArg::Pos(Expr::Call(call))] => {
|
||||||
write_args(head);
|
write_args(head);
|
||||||
call.pretty_bracketed(p, true);
|
call.pretty_bracketed(p, true);
|
||||||
}
|
}
|
||||||
@ -550,7 +550,7 @@ impl ExprCall {
|
|||||||
// This can be written with a body.
|
// This can be written with a body.
|
||||||
//
|
//
|
||||||
// Example: Transforms "#[v [Hi]]" => "#[v][Hi]".
|
// Example: Transforms "#[v [Hi]]" => "#[v][Hi]".
|
||||||
[head @ .., Argument::Pos(Expr::Template(template))] => {
|
[head @ .., ExprArg::Pos(Expr::Template(template))] => {
|
||||||
write_args(head);
|
write_args(head);
|
||||||
p.push(']');
|
p.push(']');
|
||||||
template.pretty(p);
|
template.pretty(p);
|
||||||
@ -573,7 +573,7 @@ pub struct ExprArgs {
|
|||||||
/// The source code location.
|
/// The source code location.
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
/// The positional and named arguments.
|
/// The positional and named arguments.
|
||||||
pub items: Vec<Argument>,
|
pub items: Vec<ExprArg>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pretty for ExprArgs {
|
impl Pretty for ExprArgs {
|
||||||
@ -584,14 +584,14 @@ impl Pretty for ExprArgs {
|
|||||||
|
|
||||||
/// An argument to a function call: `12` or `draw: false`.
|
/// An argument to a function call: `12` or `draw: false`.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Argument {
|
pub enum ExprArg {
|
||||||
/// A positional arguments.
|
/// A positional argument.
|
||||||
Pos(Expr),
|
Pos(Expr),
|
||||||
/// A named argument.
|
/// A named argument.
|
||||||
Named(Named),
|
Named(Named),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Argument {
|
impl ExprArg {
|
||||||
/// The source code location.
|
/// The source code location.
|
||||||
pub fn span(&self) -> Span {
|
pub fn span(&self) -> Span {
|
||||||
match self {
|
match self {
|
||||||
@ -601,7 +601,7 @@ impl Argument {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pretty for Argument {
|
impl Pretty for ExprArg {
|
||||||
fn pretty(&self, p: &mut Printer) {
|
fn pretty(&self, p: &mut Printer) {
|
||||||
match self {
|
match self {
|
||||||
Self::Pos(expr) => expr.pretty(p),
|
Self::Pos(expr) => expr.pretty(p),
|
||||||
|
@ -31,6 +31,7 @@ impl Pretty for Node {
|
|||||||
Self::Space => p.push(' '),
|
Self::Space => p.push(' '),
|
||||||
Self::Linebreak => p.push_str(r"\"),
|
Self::Linebreak => p.push_str(r"\"),
|
||||||
Self::Parbreak => p.push_str("\n\n"),
|
Self::Parbreak => p.push_str("\n\n"),
|
||||||
|
// TODO: Handle escaping.
|
||||||
Self::Text(text) => p.push_str(&text),
|
Self::Text(text) => p.push_str(&text),
|
||||||
Self::Heading(heading) => heading.pretty(p),
|
Self::Heading(heading) => heading.pretty(p),
|
||||||
Self::Raw(raw) => raw.pretty(p),
|
Self::Raw(raw) => raw.pretty(p),
|
||||||
|
@ -111,10 +111,10 @@ visit! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_arg(v, node: &Argument) {
|
fn visit_arg(v, node: &ExprArg) {
|
||||||
match node {
|
match node {
|
||||||
Argument::Pos(expr) => v.visit_expr(&expr),
|
ExprArg::Pos(expr) => v.visit_expr(&expr),
|
||||||
Argument::Named(named) => v.visit_expr(&named.expr),
|
ExprArg::Named(named) => v.visit_expr(&named.expr),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 9.9 KiB After Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 8.2 KiB After Width: | Height: | Size: 8.2 KiB |
@ -16,7 +16,7 @@ use walkdir::WalkDir;
|
|||||||
|
|
||||||
use typst::diag::{Diag, Feedback, Level, Pass};
|
use typst::diag::{Diag, Feedback, Level, Pass};
|
||||||
use typst::env::{Env, ImageResource, ResourceLoader};
|
use typst::env::{Env, ImageResource, ResourceLoader};
|
||||||
use typst::eval::{Args, EvalContext, Scope, Value, ValueFunc};
|
use typst::eval::{EvalContext, Scope, Value, ValueArgs, ValueFunc};
|
||||||
use typst::exec::State;
|
use typst::exec::State;
|
||||||
use typst::export::pdf;
|
use typst::export::pdf;
|
||||||
use typst::font::FsIndexExt;
|
use typst::font::FsIndexExt;
|
||||||
@ -24,7 +24,6 @@ use typst::geom::{Length, Point, Sides, Size, Spec};
|
|||||||
use typst::layout::{Element, Expansion, Fill, Frame, Geometry, Image, Shape};
|
use typst::layout::{Element, Expansion, Fill, Frame, Geometry, Image, Shape};
|
||||||
use typst::library;
|
use typst::library;
|
||||||
use typst::parse::{LineMap, Scanner};
|
use typst::parse::{LineMap, Scanner};
|
||||||
use typst::pretty::{Pretty, Printer};
|
|
||||||
use typst::shaping::Shaped;
|
use typst::shaping::Shaped;
|
||||||
use typst::syntax::{Location, Pos, SpanVec, Spanned, WithSpan};
|
use typst::syntax::{Location, Pos, SpanVec, Spanned, WithSpan};
|
||||||
use typst::typeset;
|
use typst::typeset;
|
||||||
@ -320,28 +319,13 @@ struct Panic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn register_helpers(scope: &mut Scope, panics: Rc<RefCell<Vec<Panic>>>) {
|
fn register_helpers(scope: &mut Scope, panics: Rc<RefCell<Vec<Panic>>>) {
|
||||||
pub fn f(_: &mut EvalContext, args: &mut Args) -> Value {
|
pub fn f(_: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||||
let (array, dict) = args.drain();
|
let value = args.clone().into();
|
||||||
let iter = array
|
args.items.clear();
|
||||||
.into_iter()
|
value
|
||||||
.map(|v| (None, v))
|
|
||||||
.chain(dict.into_iter().map(|(k, v)| (Some(k), v)));
|
|
||||||
|
|
||||||
let mut p = Printer::new();
|
|
||||||
p.push_str("f(");
|
|
||||||
p.join(iter, ", ", |(key, value), p| {
|
|
||||||
if let Some(key) = key {
|
|
||||||
p.push_str(&key);
|
|
||||||
p.push_str(": ");
|
|
||||||
}
|
|
||||||
value.pretty(p);
|
|
||||||
});
|
|
||||||
p.push(')');
|
|
||||||
|
|
||||||
Value::Str(p.finish())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let test = move |ctx: &mut EvalContext, args: &mut Args| -> Value {
|
let test = move |ctx: &mut EvalContext, args: &mut ValueArgs| -> Value {
|
||||||
let lhs = args.require::<Value>(ctx, "left-hand side");
|
let lhs = args.require::<Value>(ctx, "left-hand side");
|
||||||
let rhs = args.require::<Value>(ctx, "right-hand side");
|
let rhs = args.require::<Value>(ctx, "right-hand side");
|
||||||
if lhs != rhs {
|
if lhs != rhs {
|
||||||
|