From 533374db14087ac54fdc86afa5f009487ac1b850 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 2 Aug 2020 16:31:34 +0200 Subject: =?UTF-8?q?Refactor=20argument=20parsing=20=F0=9F=94=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/func/keys.rs | 169 -------------------------- src/syntax/func/maps.rs | 233 ----------------------------------- src/syntax/func/mod.rs | 107 ---------------- src/syntax/func/values.rs | 301 ---------------------------------------------- 4 files changed, 810 deletions(-) delete mode 100644 src/syntax/func/keys.rs delete mode 100644 src/syntax/func/maps.rs delete mode 100644 src/syntax/func/mod.rs delete mode 100644 src/syntax/func/values.rs (limited to 'src/syntax/func') diff --git a/src/syntax/func/keys.rs b/src/syntax/func/keys.rs deleted file mode 100644 index 558667dd..00000000 --- a/src/syntax/func/keys.rs +++ /dev/null @@ -1,169 +0,0 @@ -//! Key types for identifying keyword arguments. - -use crate::layout::prelude::*; -use super::values::AlignmentValue::{self, *}; -use super::*; - -use self::AxisKey::*; -use self::PaddingKey::*; - -/// Key types are used to extract keyword arguments from -/// [`Objects`](crate::syntax::expr::Object). They represent the key part of a -/// keyword argument. -/// ```typst -/// [func: key=value] -/// ^^^ -/// ``` -/// -/// # Example implementation -/// An implementation for the `AxisKey` that identifies layouting axes might -/// look as follows: -/// ``` -/// # use typstc::syntax::func::Key; -/// # use typstc::syntax::span::Spanned; -/// # #[derive(Eq, PartialEq)] enum Axis { Horizontal, Vertical, Primary, Secondary } -/// # #[derive(Eq, PartialEq)] enum AxisKey { Specific(Axis), Generic(Axis) } -/// # use Axis::*; -/// # use AxisKey::*; -/// impl Key for AxisKey { -/// fn parse(key: Spanned<&str>) -> Option { -/// match key.v { -/// "horizontal" | "h" => Some(Specific(Horizontal)), -/// "vertical" | "v" => Some(Specific(Vertical)), -/// "primary" | "p" => Some(Generic(Primary)), -/// "secondary" | "s" => Some(Generic(Secondary)), -/// _ => None, -/// } -/// } -/// } -/// ``` -pub trait Key: Sized + Eq { - /// Parse a key string into this type if it is valid for it. - fn parse(key: Spanned<&str>) -> Option; -} - -impl Key for String { - fn parse(key: Spanned<&str>) -> Option { - Some(key.v.to_string()) - } -} - -impl Key for Spanned { - fn parse(key: Spanned<&str>) -> Option { - K::parse(key).map(|v| Spanned { v, span: key.span }) - } -} - -/// Implements [`Key`] for types that just need to match on strings. -macro_rules! key { - ($type:ty, $($($p:pat)|* => $r:expr),* $(,)?) => { - impl Key for $type { - fn parse(key: Spanned<&str>) -> Option { - match key.v { - $($($p)|* => Some($r)),*, - _ => None, - } - } - } - }; -} - -/// A key which identifies a layouting axis. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -#[allow(missing_docs)] -pub enum AxisKey { - Generic(GenericAxis), - Specific(SpecificAxis), -} - -impl AxisKey { - /// The generic version of this axis key in the given system of axes. - pub fn to_generic(self, axes: LayoutAxes) -> GenericAxis { - match self { - Generic(axis) => axis, - Specific(axis) => axis.to_generic(axes), - } - } - - /// The specific version of this axis key in the given system of axes. - pub fn to_specific(self, axes: LayoutAxes) -> SpecificAxis { - match self { - Generic(axis) => axis.to_specific(axes), - Specific(axis) => axis, - } - } -} - -key!(AxisKey, - "horizontal" | "h" => Specific(Horizontal), - "vertical" | "v" => Specific(Vertical), - "primary" | "p" => Generic(Primary), - "secondary" | "s" => Generic(Secondary), -); - -/// A key which is equivalent to a [`AxisKey`] but uses typical extent keywords -/// instead of axis keywords, e.g. `width` instead of `horizontal`. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub struct ExtentKey(pub AxisKey); - -key!(ExtentKey, - "width" | "w" => ExtentKey(Specific(Horizontal)), - "height" | "h" => ExtentKey(Specific(Vertical)), - "primary-size" | "ps" => ExtentKey(Generic(Primary)), - "secondary-size" | "ss" => ExtentKey(Generic(Secondary)), -); - -impl From for AxisKey { - fn from(key: ExtentKey) -> AxisKey { - key.0 - } -} - -/// A key which identifies an axis, but alternatively allows for two positional -/// arguments with unspecified axes. -/// -/// This type does not implement `Key` in itself since it cannot be parsed from -/// a string. Rather, [`AxisKeys`](AxisKey) and positional arguments should be -/// parsed separately and mapped onto this key, as happening in the -/// [`PosAxisMap`](super::maps::PosAxisMap). -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum PosAxisKey { - /// The first positional argument. - First, - /// The second positional argument. - Second, - /// An axis keyword argument. - Keyword(AxisKey), -} - -/// An argument key which identifies a margin or padding target. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum PaddingKey { - /// All four sides should have the specified padding. - All, - /// Both sides of the given axis should have the specified padding. - Both(Axis), - /// Only the given side of the given axis should have the specified padding. - Side(Axis, AlignmentValue), -} - -key!(PaddingKey, - "horizontal" | "h" => Both(Specific(Horizontal)), - "vertical" | "v" => Both(Specific(Vertical)), - "primary" | "p" => Both(Generic(Primary)), - "secondary" | "s" => Both(Generic(Secondary)), - - "left" => Side(Specific(Horizontal), Left), - "right" => Side(Specific(Horizontal), Right), - "top" => Side(Specific(Vertical), Top), - "bottom" => Side(Specific(Vertical), Bottom), - - "primary-origin" => Side(Generic(Primary), Align(Origin)), - "primary-end" => Side(Generic(Primary), Align(End)), - "secondary-origin" => Side(Generic(Secondary), Align(Origin)), - "secondary-end" => Side(Generic(Secondary), Align(End)), - "horizontal-origin" => Side(Specific(Horizontal), Align(Origin)), - "horizontal-end" => Side(Specific(Horizontal), Align(End)), - "vertical-origin" => Side(Specific(Vertical), Align(Origin)), - "vertical-end" => Side(Specific(Vertical), Align(End)), -); diff --git a/src/syntax/func/maps.rs b/src/syntax/func/maps.rs deleted file mode 100644 index bc290a9e..00000000 --- a/src/syntax/func/maps.rs +++ /dev/null @@ -1,233 +0,0 @@ -//! Deduplicating maps and keys for argument parsing. - -use crate::diagnostic::Diagnostics; -use crate::geom::Value4; -use crate::layout::prelude::*; -use crate::length::ScaleLength; -use crate::syntax::span::Spanned; -use super::keys::*; -use super::values::*; -use super::*; - -/// A map which deduplicates redundant arguments. -/// -/// Whenever a duplicate argument is inserted into the map, through the -/// functions `from_iter`, `insert` or `extend` an diagnostics is added to the error -/// list that needs to be passed to those functions. -/// -/// All entries need to have span information to enable the error reporting. -#[derive(Debug, Default, Clone, Eq, PartialEq)] -pub struct DedupMap where K: Eq { - map: Vec>, -} - -impl DedupMap where K: Eq { - /// Create a new deduplicating map. - pub fn new() -> DedupMap { - DedupMap { map: vec![] } - } - - /// Create a new map from an iterator of spanned keys and values. - pub fn from_iter(diagnostics: &mut Diagnostics, iter: I) -> DedupMap - where I: IntoIterator> { - let mut map = DedupMap::new(); - map.extend(diagnostics, iter); - map - } - - /// Add a spanned key-value pair. - pub fn insert(&mut self, diagnostics: &mut Diagnostics, entry: Spanned<(K, V)>) { - if self.map.iter().any(|e| e.v.0 == entry.v.0) { - diagnostics.push(error!(entry.span, "duplicate argument")); - } else { - self.map.push(entry); - } - } - - /// Add multiple spanned key-value pairs. - pub fn extend(&mut self, diagnostics: &mut Diagnostics, items: I) - where I: IntoIterator> { - for item in items.into_iter() { - self.insert(diagnostics, item); - } - } - - /// Get the value corresponding to a key if it is present. - pub fn get(&self, key: K) -> Option<&V> { - self.map.iter().find(|e| e.v.0 == key).map(|e| &e.v.1) - } - - /// Get the value and its span corresponding to a key if it is present. - pub fn get_spanned(&self, key: K) -> Option> { - self.map.iter().find(|e| e.v.0 == key) - .map(|e| Spanned { v: &e.v.1, span: e.span }) - } - - /// Call a function with the value if the key is present. - pub fn with(&self, key: K, callback: F) where F: FnOnce(&V) { - if let Some(value) = self.get(key) { - callback(value); - } - } - - /// Create a new map where keys and values are mapped to new keys and - /// values. When the mapping introduces new duplicates, diagnostics are - /// generated. - pub fn dedup(&self, diagnostics: &mut Diagnostics, mut f: F) -> DedupMap - where F: FnMut(&K, &V) -> (K2, V2), K2: Eq { - let mut map = DedupMap::new(); - - for Spanned { v: (key, value), span } in self.map.iter() { - let (key, value) = f(key, value); - map.insert(diagnostics, Spanned { v: (key, value), span: *span }); - } - - map - } - - /// Iterate over the (key, value) pairs. - pub fn iter(&self) -> impl Iterator { - self.map.iter().map(|e| &e.v) - } -} - -/// A map for storing a value for axes given by keyword arguments. -#[derive(Debug, Clone, PartialEq)] -pub struct AxisMap(DedupMap); - -impl AxisMap { - /// Parse an axis map from the object. - pub fn parse( - diagnostics: &mut Diagnostics, - object: &mut Object, - ) -> AxisMap where K: Key + Into { - let values: Vec<_> = object - .get_all_spanned::(diagnostics) - .map(|s| s.map(|(k, v)| (k.into(), v))) - .collect(); - - AxisMap(DedupMap::from_iter(diagnostics, values)) - } - - /// Deduplicate from specific or generic to just specific axes. - pub fn dedup(&self, diagnostics: &mut Diagnostics, axes: LayoutAxes) -> DedupMap - where V: Clone { - self.0.dedup(diagnostics, |key, val| (key.to_specific(axes), val.clone())) - } -} - -/// A map for storing values for axes that are given through a combination of -/// (two) positional and keyword arguments. -#[derive(Debug, Clone, PartialEq)] -pub struct PosAxisMap(DedupMap); - -impl PosAxisMap { - /// Parse a positional/axis map from the function arguments. - pub fn parse( - diagnostics: &mut Diagnostics, - args: &mut FuncArgs, - ) -> PosAxisMap where K: Key + Into { - let mut map = DedupMap::new(); - - for &key in &[PosAxisKey::First, PosAxisKey::Second] { - if let Some(Spanned { v, span }) = args.pos.get::>(diagnostics) { - map.insert(diagnostics, Spanned { v: (key, v), span }) - } - } - - let keywords: Vec<_> = args.key - .get_all_spanned::(diagnostics) - .map(|s| s.map(|(k, v)| (PosAxisKey::Keyword(k.into()), v))) - .collect(); - - map.extend(diagnostics, keywords); - - PosAxisMap(map) - } - - /// Deduplicate from positional arguments and keyword arguments for generic - /// or specific axes to just generic axes. - pub fn dedup( - &self, - diagnostics: &mut Diagnostics, - axes: LayoutAxes, - mut f: F, - ) -> DedupMap - where - F: FnMut(&V) -> Option, - V: Clone, - { - self.0.dedup(diagnostics, |key, val| { - (match key { - PosAxisKey::First => f(val).unwrap_or(GenericAxis::Primary), - PosAxisKey::Second => f(val).unwrap_or(GenericAxis::Secondary), - PosAxisKey::Keyword(AxisKey::Specific(axis)) => axis.to_generic(axes), - PosAxisKey::Keyword(AxisKey::Generic(axis)) => *axis, - }, val.clone()) - }) - } -} - -/// A map for storing padding given for a combination of all sides, opposing -/// sides or single sides. -#[derive(Debug, Clone, PartialEq)] -pub struct PaddingMap(DedupMap, Option>); - -impl PaddingMap { - /// Parse a padding map from the function arguments. - pub fn parse(diagnostics: &mut Diagnostics, args: &mut FuncArgs) -> PaddingMap { - let mut map = DedupMap::new(); - - let all = args.key.get::>>(diagnostics, "margins"); - if let Some(Spanned { v, span }) = all { - map.insert(diagnostics, Spanned { v: (PaddingKey::All, v.into()), span }); - } - - let paddings: Vec<_> = args.key - .get_all_spanned::, Defaultable>(diagnostics) - .map(|s| s.map(|(k, v)| (k, v.into()))) - .collect(); - - map.extend(diagnostics, paddings); - - PaddingMap(map) - } - - /// Apply the specified padding on a value box of optional, scalable sizes. - pub fn apply( - &self, - diagnostics: &mut Diagnostics, - axes: LayoutAxes, - padding: &mut Value4> - ) { - use PaddingKey::*; - - let map = self.0.dedup(diagnostics, |key, &val| { - (match key { - All => All, - Both(axis) => Both(axis.to_specific(axes)), - Side(axis, alignment) => { - let generic = axis.to_generic(axes); - let axis = axis.to_specific(axes); - Side(axis, alignment.to_specific(axes, generic)) - } - }, val) - }); - - map.with(All, |&val| padding.set_all(val)); - map.with(Both(Horizontal), |&val| padding.set_horizontal(val)); - map.with(Both(Vertical), |&val| padding.set_vertical(val)); - - for &(key, val) in map.iter() { - if let Side(_, alignment) = key { - match alignment { - AlignmentValue::Left => padding.left = val, - AlignmentValue::Right => padding.right = val, - AlignmentValue::Top => padding.top = val, - AlignmentValue::Bottom => padding.bottom = val, - _ => {}, - } - } - } - } -} diff --git a/src/syntax/func/mod.rs b/src/syntax/func/mod.rs deleted file mode 100644 index 37dccc3d..00000000 --- a/src/syntax/func/mod.rs +++ /dev/null @@ -1,107 +0,0 @@ -//! Primitives for argument parsing in library functions. - -use std::iter::FromIterator; -use crate::diagnostic::{Diagnostic, Diagnostics}; -use super::expr::{Expr, Ident, Tuple, Object, Pair}; -use super::span::{Span, Spanned}; - -pub_use_mod!(maps); -pub_use_mod!(keys); -pub_use_mod!(values); - -/// An invocation of a function. -#[derive(Debug, Clone, PartialEq)] -pub struct FuncCall<'s> { - pub header: FuncHeader, - /// The body as a raw string containing what's inside of the brackets. - pub body: Option>, -} - -/// The parsed header of a function (everything in the first set of brackets). -#[derive(Debug, Clone, PartialEq)] -pub struct FuncHeader { - pub name: Spanned, - pub args: FuncArgs, -} - -/// The positional and keyword arguments passed to a function. -#[derive(Debug, Default, Clone, PartialEq)] -pub struct FuncArgs { - /// The positional arguments. - pub pos: Tuple, - /// They keyword arguments. - pub key: Object, -} - -impl FuncArgs { - /// Create new empty function arguments. - pub fn new() -> FuncArgs { - FuncArgs { - pos: Tuple::new(), - key: Object::new(), - } - } - - /// Add an argument. - pub fn add(&mut self, arg: Spanned) { - match arg.v { - FuncArg::Pos(item) => self.pos.add(Spanned::new(item, arg.span)), - FuncArg::Key(pair) => self.key.add(Spanned::new(pair, arg.span)), - } - } - - /// Iterate over all arguments. - pub fn into_iter(self) -> impl Iterator> { - let pos = self.pos.items.into_iter() - .map(|spanned| spanned.map(|item| FuncArg::Pos(item))); - - let key = self.key.pairs.into_iter() - .map(|spanned| spanned.map(|pair| FuncArg::Key(pair))); - - pos.chain(key) - } -} - -impl FromIterator> for FuncArgs { - fn from_iter>>(iter: I) -> Self { - let mut args = FuncArgs::new(); - for item in iter.into_iter() { - args.add(item); - } - args - } -} - -/// Either a positional or keyword argument. -#[derive(Debug, Clone, PartialEq)] -pub enum FuncArg { - /// A positional argument. - Pos(Expr), - /// A keyword argument. - Key(Pair), -} - -/// Extra methods on [`Options`](Option) used for argument parsing. -pub trait OptionExt: Sized { - /// Calls `f` with `val` if this is `Some(val)`. - fn with(self, f: impl FnOnce(T)); - - /// Add an error about a missing argument `arg` with the given span if the - /// option is `None`. - fn or_missing(self, diagnostics: &mut Diagnostics, span: Span, arg: &str) -> Self; -} - -impl OptionExt for Option { - fn with(self, f: impl FnOnce(T)) { - if let Some(val) = self { - f(val); - } - } - - fn or_missing(self, diagnostics: &mut Diagnostics, span: Span, arg: &str) -> Self { - if self.is_none() { - diagnostics.push(error!(span, "missing argument: {}", arg)); - } - self - } -} diff --git a/src/syntax/func/values.rs b/src/syntax/func/values.rs deleted file mode 100644 index d5e9c6e8..00000000 --- a/src/syntax/func/values.rs +++ /dev/null @@ -1,301 +0,0 @@ -//! Value types for extracting function arguments. - -use std::fmt::{self, Display, Formatter}; -use fontdock::{FontStyle, FontWeight, FontWidth}; - -use crate::layout::prelude::*; -use crate::length::{Length, ScaleLength}; -use crate::paper::Paper; -use super::*; - -use self::AlignmentValue::*; - -/// Value types are used to extract the values of positional and keyword -/// arguments from [`Tuples`](crate::syntax::expr::Tuple) and -/// [`Objects`](crate::syntax::expr::Object). They represent the value part of -/// an argument. -/// ```typst -/// [func: value, key=value] -/// ^^^^^ ^^^^^ -/// ``` -/// -/// # Example implementation -/// An implementation for `bool` might look as follows: -/// ``` -/// # use typstc::error; -/// # use typstc::diagnostic::Diagnostic; -/// # use typstc::syntax::expr::Expr; -/// # use typstc::syntax::func::Value; -/// # use typstc::syntax::span::Spanned; -/// # struct Bool; /* -/// impl Value for bool { -/// # */ impl Value for Bool { -/// fn parse(expr: Spanned) -> Result { -/// match expr.v { -/// # /* -/// Expr::Bool(b) => Ok(b), -/// # */ Expr::Bool(_) => Ok(Bool), -/// other => Err(error!("expected bool, found {}", other.name())), -/// } -/// } -/// } -/// ``` -pub trait Value: Sized { - /// Parse an expression into this value or return an error if the expression - /// is valid for this value type. - fn parse(expr: Spanned) -> Result; -} - -impl Value for Spanned { - fn parse(expr: Spanned) -> Result { - let span = expr.span; - V::parse(expr).map(|v| Spanned { v, span }) - } -} - -/// Implements [`Value`] for types that just need to match on expressions. -macro_rules! value { - ($type:ty, $name:expr, $($p:pat => $r:expr),* $(,)?) => { - impl Value for $type { - fn parse(expr: Spanned) -> Result { - #[allow(unreachable_patterns)] - match expr.v { - $($p => Ok($r)),*, - other => Err( - error!("expected {}, found {}", $name, other.name()) - ), - } - } - } - }; -} - -value!(Expr, "expression", e => e); - -value!(Ident, "identifier", Expr::Ident(i) => i); -value!(String, "string", Expr::Str(s) => s); -value!(f64, "number", Expr::Number(n) => n); -value!(bool, "bool", Expr::Bool(b) => b); -value!(Length, "length", Expr::Length(l) => l); -value!(Tuple, "tuple", Expr::Tuple(t) => t); -value!(Object, "object", Expr::Object(o) => o); - -value!(ScaleLength, "number or length", - Expr::Length(length) => ScaleLength::Absolute(length), - Expr::Number(scale) => ScaleLength::Scaled(scale), -); - -/// A value type that matches [`Expr::Ident`] and [`Expr::Str`] and implements -/// `Into`. -pub struct StringLike(pub String); - -value!(StringLike, "identifier or string", - Expr::Ident(Ident(s)) => StringLike(s), - Expr::Str(s) => StringLike(s), -); - -impl From for String { - fn from(like: StringLike) -> String { - like.0 - } -} - -/// A value type that matches the identifier `default` or a value type `V` and -/// implements `Into