From bbcdeb128cce04cd95714b7bc7af5a23a7e38bd2 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 29 Jul 2020 18:09:51 +0200 Subject: =?UTF-8?q?Move,=20rename=20and=20switch=20some=20things=20(boring?= =?UTF-8?q?)=20=F0=9F=9A=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Problems -> Diagnostics - Position -> Pos - offset_spans -> Offset trait - Size -> Length (and some more size types renamed) - Paper into its own module - scope::Parser -> parsing::CallParser - Create `Decorations` alias - Remove lots of double newlines - Switch from f32 to f64 --- src/syntax/func/keys.rs | 1 - src/syntax/func/maps.rs | 67 +++++++++++++++++++++++------------------------ src/syntax/func/mod.rs | 8 +++--- src/syntax/func/values.rs | 43 +++++++++++++++--------------- 4 files changed, 58 insertions(+), 61 deletions(-) (limited to 'src/syntax/func') diff --git a/src/syntax/func/keys.rs b/src/syntax/func/keys.rs index b8f142ee..558667dd 100644 --- a/src/syntax/func/keys.rs +++ b/src/syntax/func/keys.rs @@ -7,7 +7,6 @@ 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. diff --git a/src/syntax/func/maps.rs b/src/syntax/func/maps.rs index 44d8e1aa..2ac70223 100644 --- a/src/syntax/func/maps.rs +++ b/src/syntax/func/maps.rs @@ -1,18 +1,17 @@ //! Deduplicating maps and keys for argument parsing. -use crate::problem::Problems; +use crate::diagnostic::Diagnostics; use crate::layout::prelude::*; -use crate::size::{PSize, ValueBox}; +use crate::length::{ScaleLength, Value4}; 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 problems is added to the error +/// 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. @@ -28,27 +27,27 @@ impl DedupMap where K: Eq { } /// Create a new map from an iterator of spanned keys and values. - pub fn from_iter(problems: &mut Problems, iter: I) -> DedupMap + pub fn from_iter(diagnostics: &mut Diagnostics, iter: I) -> DedupMap where I: IntoIterator> { let mut map = DedupMap::new(); - map.extend(problems, iter); + map.extend(diagnostics, iter); map } /// Add a spanned key-value pair. - pub fn insert(&mut self, problems: &mut Problems, entry: Spanned<(K, V)>) { + pub fn insert(&mut self, diagnostics: &mut Diagnostics, entry: Spanned<(K, V)>) { if self.map.iter().any(|e| e.v.0 == entry.v.0) { - problems.push(error!(entry.span, "duplicate argument")); + diagnostics.push(error!(entry.span, "duplicate argument")); } else { self.map.push(entry); } } /// Add multiple spanned key-value pairs. - pub fn extend(&mut self, problems: &mut Problems, items: I) + pub fn extend(&mut self, diagnostics: &mut Diagnostics, items: I) where I: IntoIterator> { for item in items.into_iter() { - self.insert(problems, item); + self.insert(diagnostics, item); } } @@ -71,15 +70,15 @@ impl DedupMap where K: Eq { } /// Create a new map where keys and values are mapped to new keys and - /// values. When the mapping introduces new duplicates, problems are + /// values. When the mapping introduces new duplicates, diagnostics are /// generated. - pub fn dedup(&self, problems: &mut Problems, mut f: F) -> DedupMap + 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(problems, Spanned { v: (key, value), span: *span }); + map.insert(diagnostics, Spanned { v: (key, value), span: *span }); } map @@ -98,21 +97,21 @@ pub struct AxisMap(DedupMap); impl AxisMap { /// Parse an axis map from the object. pub fn parse( - problems: &mut Problems, + diagnostics: &mut Diagnostics, object: &mut Object, ) -> AxisMap where K: Key + Into { let values: Vec<_> = object - .get_all_spanned::(problems) + .get_all_spanned::(diagnostics) .map(|s| s.map(|(k, v)| (k.into(), v))) .collect(); - AxisMap(DedupMap::from_iter(problems, values)) + AxisMap(DedupMap::from_iter(diagnostics, values)) } /// Deduplicate from specific or generic to just specific axes. - pub fn dedup(&self, problems: &mut Problems, axes: LayoutAxes) -> DedupMap + pub fn dedup(&self, diagnostics: &mut Diagnostics, axes: LayoutAxes) -> DedupMap where V: Clone { - self.0.dedup(problems, |key, val| (key.to_specific(axes), val.clone())) + self.0.dedup(diagnostics, |key, val| (key.to_specific(axes), val.clone())) } } @@ -124,23 +123,23 @@ pub struct PosAxisMap(DedupMap); impl PosAxisMap { /// Parse a positional/axis map from the function arguments. pub fn parse( - problems: &mut Problems, + 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::>(problems) { - map.insert(problems, Spanned { v: (key, v), span }) + 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::(problems) + .get_all_spanned::(diagnostics) .map(|s| s.map(|(k, v)| (PosAxisKey::Keyword(k.into()), v))) .collect(); - map.extend(problems, keywords); + map.extend(diagnostics, keywords); PosAxisMap(map) } @@ -149,7 +148,7 @@ impl PosAxisMap { /// or specific axes to just generic axes. pub fn dedup( &self, - problems: &mut Problems, + diagnostics: &mut Diagnostics, axes: LayoutAxes, mut f: F, ) -> DedupMap @@ -157,7 +156,7 @@ impl PosAxisMap { F: FnMut(&V) -> Option, V: Clone, { - self.0.dedup(problems, |key, val| { + self.0.dedup(diagnostics, |key, val| { (match key { PosAxisKey::First => f(val).unwrap_or(GenericAxis::Primary), PosAxisKey::Second => f(val).unwrap_or(GenericAxis::Secondary), @@ -171,24 +170,24 @@ impl PosAxisMap { /// 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>); +pub struct PaddingMap(DedupMap, Option>); impl PaddingMap { /// Parse a padding map from the function arguments. - pub fn parse(problems: &mut Problems, args: &mut FuncArgs) -> PaddingMap { + pub fn parse(diagnostics: &mut Diagnostics, args: &mut FuncArgs) -> PaddingMap { let mut map = DedupMap::new(); - let all = args.pos.get::>>(problems); + let all = args.pos.get::>>(diagnostics); if let Some(Spanned { v, span }) = all { - map.insert(problems, Spanned { v: (PaddingKey::All, v.into()), span }); + map.insert(diagnostics, Spanned { v: (PaddingKey::All, v.into()), span }); } let paddings: Vec<_> = args.key - .get_all_spanned::, Defaultable>(problems) + .get_all_spanned::, Defaultable>(diagnostics) .map(|s| s.map(|(k, v)| (k, v.into()))) .collect(); - map.extend(problems, paddings); + map.extend(diagnostics, paddings); PaddingMap(map) } @@ -196,13 +195,13 @@ impl PaddingMap { /// Apply the specified padding on a value box of optional, scalable sizes. pub fn apply( &self, - problems: &mut Problems, + diagnostics: &mut Diagnostics, axes: LayoutAxes, - padding: &mut ValueBox> + padding: &mut Value4> ) { use PaddingKey::*; - let map = self.0.dedup(problems, |key, &val| { + let map = self.0.dedup(diagnostics, |key, &val| { (match key { All => All, Both(axis) => Both(axis.to_specific(axes)), diff --git a/src/syntax/func/mod.rs b/src/syntax/func/mod.rs index 4228488d..c2631727 100644 --- a/src/syntax/func/mod.rs +++ b/src/syntax/func/mod.rs @@ -1,7 +1,7 @@ //! Primitives for argument parsing in library functions. use std::iter::FromIterator; -use crate::problem::{Problem, Problems}; +use crate::diagnostic::{Diagnostic, Diagnostics}; use super::expr::{Expr, Ident, Tuple, Object, Pair}; use super::span::{Span, Spanned}; @@ -85,13 +85,13 @@ pub enum FuncArg { pub trait OptionExt: Sized { /// Add an error about a missing argument `arg` with the given span if the /// option is `None`. - fn or_missing(self, problems: &mut Problems, span: Span, arg: &str) -> Self; + fn or_missing(self, diagnostics: &mut Diagnostics, span: Span, arg: &str) -> Self; } impl OptionExt for Option { - fn or_missing(self, problems: &mut Problems, span: Span, arg: &str) -> Self { + fn or_missing(self, diagnostics: &mut Diagnostics, span: Span, arg: &str) -> Self { if self.is_none() { - problems.push(error!(span, "missing argument: {}", arg)); + diagnostics.push(error!(span, "missing argument: {}", arg)); } self } diff --git a/src/syntax/func/values.rs b/src/syntax/func/values.rs index 7a1aa912..3269f8e9 100644 --- a/src/syntax/func/values.rs +++ b/src/syntax/func/values.rs @@ -4,13 +4,12 @@ use std::fmt::{self, Display, Formatter}; use toddle::query::{FontStyle, FontWeight}; use crate::layout::prelude::*; -use crate::size::{Size, ScaleSize}; -use crate::style::Paper; +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 @@ -24,14 +23,14 @@ use self::AlignmentValue::*; /// An implementation for `bool` might look as follows: /// ``` /// # use typstc::error; -/// # use typstc::problem::Problem; +/// # 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 { +/// fn parse(expr: Spanned) -> Result { /// match expr.v { /// # /* /// Expr::Bool(b) => Ok(b), @@ -44,11 +43,11 @@ use self::AlignmentValue::*; 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; + fn parse(expr: Spanned) -> Result; } impl Value for Spanned { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { let span = expr.span; V::parse(expr).map(|v| Spanned { v, span }) } @@ -58,7 +57,7 @@ impl Value for Spanned { macro_rules! value { ($type:ty, $name:expr, $($p:pat => $r:expr),* $(,)?) => { impl Value for $type { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { #[allow(unreachable_patterns)] match expr.v { $($p => Ok($r)),*, @@ -77,13 +76,13 @@ 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!(Size, "size", Expr::Size(s) => s); +value!(Length, "length", Expr::Length(s) => s); value!(Tuple, "tuple", Expr::Tuple(t) => t); value!(Object, "object", Expr::Object(o) => o); -value!(ScaleSize, "number or size", - Expr::Size(size) => ScaleSize::Absolute(size), - Expr::Number(scale) => ScaleSize::Scaled(scale as f32), +value!(ScaleLength, "number or length", + Expr::Length(length) => ScaleLength::Absolute(length), + Expr::Number(scale) => ScaleLength::Scaled(scale as f64), ); /// A value type that matches [`Expr::Ident`] and [`Expr::Str`] and implements @@ -108,20 +107,20 @@ impl From for String { /// # Example /// ``` /// # use typstc::syntax::func::{FuncArgs, Defaultable}; -/// # use typstc::size::Size; +/// # use typstc::length::Length; /// # let mut args = FuncArgs::new(); /// # let mut errors = vec![]; -/// args.key.get::>(&mut errors, "size"); +/// args.key.get::>(&mut errors, "length"); /// ``` /// This will yield. /// ```typst -/// [func: size=default] => None -/// [func: size=2cm] => Some(Size::cm(2.0)) +/// [func: length=default] => None +/// [func: length=2cm] => Some(Length::cm(2.0)) /// ``` pub struct Defaultable(pub Option); impl Value for Defaultable { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { Ok(Defaultable(match expr.v { Expr::Ident(ident) if ident.as_str() == "default" => None, _ => Some(V::parse(expr)?) @@ -136,7 +135,7 @@ impl From> for Option { } impl Value for FontStyle { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { FontStyle::from_name(Ident::parse(expr)?.as_str()) .ok_or_else(|| error!("invalid font style")) } @@ -145,7 +144,7 @@ impl Value for FontStyle { /// The additional boolean specifies whether a number was clamped into the range /// 100 - 900 to make it a valid font weight. impl Value for (FontWeight, bool) { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { match expr.v { Expr::Number(weight) => { let weight = weight.round(); @@ -170,14 +169,14 @@ impl Value for (FontWeight, bool) { } impl Value for Paper { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { Paper::from_name(Ident::parse(expr)?.as_str()) .ok_or_else(|| error!("invalid paper type")) } } impl Value for Direction { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { Ok(match Ident::parse(expr)?.as_str() { "left-to-right" | "ltr" | "LTR" => LeftToRight, "right-to-left" | "rtl" | "RTL" => RightToLeft, @@ -250,7 +249,7 @@ impl AlignmentValue { } impl Value for AlignmentValue { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { Ok(match Ident::parse(expr)?.as_str() { "origin" => Align(Origin), "center" => Align(Center), -- cgit v1.2.3