From 6ab7760822ccd24b4ef126d4737d41f1be15fe19 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 1 Mar 2023 16:30:58 +0100 Subject: [PATCH] Split up `model` module --- cli/src/main.rs | 2 +- docs/src/lib.rs | 6 ++-- library/src/compute/calc.rs | 2 +- library/src/compute/construct.rs | 2 +- library/src/compute/foundations.rs | 2 +- library/src/lib.rs | 3 +- library/src/math/accent.rs | 2 +- library/src/math/mod.rs | 6 ++-- library/src/math/op.rs | 2 +- library/src/prelude.rs | 12 +++++--- library/src/symbols/emoji.rs | 2 +- library/src/symbols/sym.rs | 2 +- macros/src/castable.rs | 34 ++++++++++----------- macros/src/func.rs | 20 ++++++------- macros/src/node.rs | 16 +++++----- macros/src/symbols.rs | 4 +-- src/doc.rs | 5 ++-- src/{model => eval}/args.rs | 0 src/{model => eval}/array.rs | 4 +-- src/{model => eval}/cast.rs | 5 ++-- src/{model => eval}/dict.rs | 2 +- src/{model => eval}/func.rs | 6 ++-- src/{model => eval}/library.rs | 5 ++-- src/{model => eval}/methods.rs | 0 src/{model/eval.rs => eval/mod.rs} | 43 +++++++++++++++++++++++---- src/{model => eval}/module.rs | 0 src/{model => eval}/ops.rs | 0 src/{model => eval}/scope.rs | 0 src/{model => eval}/str.rs | 2 +- src/{model => eval}/symbol.rs | 0 src/{model => eval}/value.rs | 2 +- src/ide/analyze.rs | 2 +- src/ide/complete.rs | 2 +- src/ide/tooltip.rs | 2 +- src/lib.rs | 12 ++++---- src/model/content.rs | 8 ++--- src/model/mod.rs | 47 +++++------------------------- src/model/styles.rs | 3 +- src/model/typeset.rs | 5 ++-- tests/src/benches.rs | 21 +++++-------- tests/src/tests.rs | 12 ++++---- 41 files changed, 150 insertions(+), 155 deletions(-) rename src/{model => eval}/args.rs (100%) rename src/{model => eval}/array.rs (98%) rename src/{model => eval}/cast.rs (99%) rename src/{model => eval}/dict.rs (99%) rename src/{model => eval}/func.rs (99%) rename src/{model => eval}/library.rs (97%) rename src/{model => eval}/methods.rs (100%) rename src/{model/eval.rs => eval/mod.rs} (98%) rename src/{model => eval}/module.rs (100%) rename src/{model => eval}/ops.rs (100%) rename src/{model => eval}/scope.rs (100%) rename src/{model => eval}/str.rs (99%) rename src/{model => eval}/symbol.rs (100%) rename src/{model => eval}/value.rs (99%) diff --git a/cli/src/main.rs b/cli/src/main.rs index 397699684..65b21e228 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -18,8 +18,8 @@ use same_file::{is_same_file, Handle}; use siphasher::sip128::{Hasher128, SipHasher}; use termcolor::{ColorChoice, StandardStream, WriteColor}; use typst::diag::{FileError, FileResult, SourceError, StrResult}; +use typst::eval::Library; use typst::font::{Font, FontBook, FontInfo, FontVariant}; -use typst::model::Library; use typst::syntax::{Source, SourceId}; use typst::util::{Buffer, PathExt}; use typst::World; diff --git a/docs/src/lib.rs b/docs/src/lib.rs index 1989970e7..c32f9ff97 100644 --- a/docs/src/lib.rs +++ b/docs/src/lib.rs @@ -15,9 +15,9 @@ use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use serde_yaml as yaml; use typst::doc::Frame; +use typst::eval::{CastInfo, Func, FuncInfo, Library, Module, ParamInfo, Value}; use typst::font::{Font, FontBook}; use typst::geom::{Abs, Sides, Smart}; -use typst::model::{CastInfo, Func, FuncInfo, Library, Module, ParamInfo, Value}; use typst_library::layout::PageNode; use unscanny::Scanner; @@ -43,7 +43,7 @@ static LIBRARY: Lazy> = Lazy::new(|| { lib.styles.set(PageNode::HEIGHT, Smart::Auto); lib.styles .set(PageNode::MARGIN, Sides::splat(Some(Smart::Custom(Abs::pt(15.0).into())))); - typst::model::set_lang_items(lib.items.clone()); + typst::eval::set_lang_items(lib.items.clone()); Prehashed::new(lib) }); @@ -630,7 +630,7 @@ fn symbol_page(resolver: &dyn Resolver, parent: &str, name: &str) -> PageModel { .find(|&(_, x)| x == c) .map(|(s, _)| s), codepoint: c as u32, - accent: typst::model::combining_accent(c).is_some(), + accent: typst::eval::combining_accent(c).is_some(), unicode_name: unicode_names2::name(c) .map(|s| s.to_string().to_title_case()), alternates: symbol diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs index 8eae11852..9ebce84cb 100644 --- a/library/src/compute/calc.rs +++ b/library/src/compute/calc.rs @@ -1,7 +1,7 @@ use std::cmp::Ordering; use std::ops::Rem; -use typst::model::{Module, Scope}; +use typst::eval::{Module, Scope}; use crate::prelude::*; diff --git a/library/src/compute/construct.rs b/library/src/compute/construct.rs index b6f2de8ff..8355e20fd 100644 --- a/library/src/compute/construct.rs +++ b/library/src/compute/construct.rs @@ -1,7 +1,7 @@ use std::str::FromStr; use ecow::EcoVec; -use typst::model::Regex; +use typst::eval::Regex; use crate::prelude::*; diff --git a/library/src/compute/foundations.rs b/library/src/compute/foundations.rs index ccf7a165b..1619fb60f 100644 --- a/library/src/compute/foundations.rs +++ b/library/src/compute/foundations.rs @@ -137,5 +137,5 @@ pub fn assert(args: &mut Args) -> SourceResult { #[func] pub fn eval(vm: &Vm, args: &mut Args) -> SourceResult { let Spanned { v: text, span } = args.expect::>("source")?; - typst::model::eval_code_str(vm.world(), &text, span) + typst::eval::eval_code_str(vm.world(), &text, span) } diff --git a/library/src/lib.rs b/library/src/lib.rs index 31da5b71f..0759f73fd 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -10,8 +10,9 @@ pub mod symbols; pub mod text; pub mod visualize; +use typst::eval::{LangItems, Library, Module, Scope}; use typst::geom::{Align, Color, Dir, GenAlign}; -use typst::model::{LangItems, Library, Module, Node, NodeId, Scope, StyleMap}; +use typst::model::{Node, NodeId, StyleMap}; use self::layout::LayoutRoot; diff --git a/library/src/math/accent.rs b/library/src/math/accent.rs index aa6d0523a..9c474eee7 100644 --- a/library/src/math/accent.rs +++ b/library/src/math/accent.rs @@ -1,4 +1,4 @@ -use typst::model::combining_accent; +use typst::eval::combining_accent; use super::*; diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index c455d106b..d73b17690 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -29,9 +29,9 @@ pub use self::style::*; pub use self::underover::*; use ttf_parser::{GlyphId, Rect}; -use typst::font::Font; -use typst::font::FontWeight; -use typst::model::{Guard, Module, Scope, SequenceNode, StyledNode}; +use typst::eval::{Module, Scope}; +use typst::font::{Font, FontWeight}; +use typst::model::{Guard, SequenceNode, StyledNode}; use unicode_math_class::MathClass; use self::ctx::*; diff --git a/library/src/math/op.rs b/library/src/math/op.rs index 772013bdb..4eb9c48c3 100644 --- a/library/src/math/op.rs +++ b/library/src/math/op.rs @@ -1,4 +1,4 @@ -use typst::model::Scope; +use typst::eval::Scope; use super::*; diff --git a/library/src/prelude.rs b/library/src/prelude.rs index 22458a553..a406e5867 100644 --- a/library/src/prelude.rs +++ b/library/src/prelude.rs @@ -14,13 +14,17 @@ pub use typst::diag::{bail, error, At, SourceResult, StrResult}; #[doc(no_inline)] pub use typst::doc::*; #[doc(no_inline)] +pub use typst::eval::{ + array, castable, dict, format_str, func, Args, Array, AutoValue, Cast, CastInfo, + Dict, Func, NoneValue, Str, Symbol, Value, Vm, +}; +#[doc(no_inline)] pub use typst::geom::*; #[doc(no_inline)] pub use typst::model::{ - array, capability, capable, castable, dict, format_str, func, node, Args, Array, - AutoValue, Cast, CastInfo, Content, Dict, Finalize, Fold, Func, Introspector, Label, - Node, NodeId, NoneValue, Prepare, Resolve, Selector, Show, StabilityProvider, Str, - StyleChain, StyleMap, StyleVec, Symbol, Unlabellable, Value, Vm, Vt, + capability, capable, node, Content, Finalize, Fold, Introspector, Label, Node, + NodeId, Prepare, Resolve, Selector, Show, StabilityProvider, StyleChain, StyleMap, + StyleVec, Unlabellable, Vt, }; #[doc(no_inline)] pub use typst::syntax::{Span, Spanned}; diff --git a/library/src/symbols/emoji.rs b/library/src/symbols/emoji.rs index efde0e764..07c7004b5 100644 --- a/library/src/symbols/emoji.rs +++ b/library/src/symbols/emoji.rs @@ -1,4 +1,4 @@ -use typst::model::{symbols, Module, Scope, Symbol}; +use typst::eval::{symbols, Module, Scope, Symbol}; /// A module with all emoji. pub fn emoji() -> Module { diff --git a/library/src/symbols/sym.rs b/library/src/symbols/sym.rs index 6a6ca758c..90c89fc34 100644 --- a/library/src/symbols/sym.rs +++ b/library/src/symbols/sym.rs @@ -1,4 +1,4 @@ -use typst::model::{symbols, Module, Scope, Symbol}; +use typst::eval::{symbols, Module, Scope, Symbol}; /// A module with all general symbols. pub fn sym() -> Module { diff --git a/macros/src/castable.rs b/macros/src/castable.rs index 6738ee1d8..c39df90af 100644 --- a/macros/src/castable.rs +++ b/macros/src/castable.rs @@ -18,20 +18,20 @@ pub fn castable(stream: TokenStream) -> Result { let describe_func = create_describe_func(&castable); let dynamic_impls = castable.name.as_ref().map(|name| { quote! { - impl ::typst::model::Type for #ty { + impl ::typst::eval::Type for #ty { const TYPE_NAME: &'static str = #name; } - impl From<#ty> for ::typst::model::Value { + impl From<#ty> for ::typst::eval::Value { fn from(v: #ty) -> Self { - ::typst::model::Value::Dyn(::typst::model::Dynamic::new(v)) + ::typst::eval::Value::Dyn(::typst::eval::Dynamic::new(v)) } } } }); Ok(quote! { - impl ::typst::model::Cast for #ty { + impl ::typst::eval::Cast for #ty { #is_func #cast_func #describe_func @@ -53,7 +53,7 @@ fn create_is_func(castable: &Castable) -> TokenStream { } Pattern::Ty(_, ty) => { cast_checks.push(quote! { - if <#ty as ::typst::model::Cast>::is(value) { + if <#ty as ::typst::eval::Cast>::is(value) { return true; } }); @@ -63,7 +63,7 @@ fn create_is_func(castable: &Castable) -> TokenStream { let dynamic_check = castable.name.is_some().then(|| { quote! { - if let ::typst::model::Value::Dyn(dynamic) = &value { + if let ::typst::eval::Value::Dyn(dynamic) = &value { if dynamic.is::() { return true; } @@ -73,7 +73,7 @@ fn create_is_func(castable: &Castable) -> TokenStream { let str_check = (!string_arms.is_empty()).then(|| { quote! { - if let ::typst::model::Value::Str(string) = &value { + if let ::typst::eval::Value::Str(string) = &value { match string.as_str() { #(#string_arms,)* _ => {} @@ -83,7 +83,7 @@ fn create_is_func(castable: &Castable) -> TokenStream { }); quote! { - fn is(value: &typst::model::Value) -> bool { + fn is(value: &::typst::eval::Value) -> bool { #dynamic_check #str_check #(#cast_checks)* @@ -105,8 +105,8 @@ fn create_cast_func(castable: &Castable) -> TokenStream { } Pattern::Ty(binding, ty) => { cast_checks.push(quote! { - if <#ty as ::typst::model::Cast>::is(&value) { - let #binding = <#ty as ::typst::model::Cast>::cast(value)?; + if <#ty as ::typst::eval::Cast>::is(&value) { + let #binding = <#ty as ::typst::eval::Cast>::cast(value)?; return Ok(#expr); } }); @@ -116,7 +116,7 @@ fn create_cast_func(castable: &Castable) -> TokenStream { let dynamic_check = castable.name.is_some().then(|| { quote! { - if let ::typst::model::Value::Dyn(dynamic) = &value { + if let ::typst::eval::Value::Dyn(dynamic) = &value { if let Some(concrete) = dynamic.downcast::() { return Ok(concrete.clone()); } @@ -126,7 +126,7 @@ fn create_cast_func(castable: &Castable) -> TokenStream { let str_check = (!string_arms.is_empty()).then(|| { quote! { - if let ::typst::model::Value::Str(string) = &value { + if let ::typst::eval::Value::Str(string) = &value { match string.as_str() { #(#string_arms,)* _ => {} @@ -136,11 +136,11 @@ fn create_cast_func(castable: &Castable) -> TokenStream { }); quote! { - fn cast(value: ::typst::model::Value) -> ::typst::diag::StrResult { + fn cast(value: ::typst::eval::Value) -> ::typst::diag::StrResult { #dynamic_check #str_check #(#cast_checks)* - ::error(value) + ::error(value) } } } @@ -153,10 +153,10 @@ fn create_describe_func(castable: &Castable) -> TokenStream { let docs = documentation(&cast.attrs); infos.push(match &cast.pattern { Pattern::Str(lit) => { - quote! { ::typst::model::CastInfo::Value(#lit.into(), #docs) } + quote! { ::typst::eval::CastInfo::Value(#lit.into(), #docs) } } Pattern::Ty(_, ty) => { - quote! { <#ty as ::typst::model::Cast>::describe() } + quote! { <#ty as ::typst::eval::Cast>::describe() } } }); } @@ -168,7 +168,7 @@ fn create_describe_func(castable: &Castable) -> TokenStream { } quote! { - fn describe() -> ::typst::model::CastInfo { + fn describe() -> ::typst::eval::CastInfo { #(#infos)+* } } diff --git a/macros/src/func.rs b/macros/src/func.rs index 41504b208..f65c135ed 100644 --- a/macros/src/func.rs +++ b/macros/src/func.rs @@ -21,7 +21,7 @@ pub fn func(item: syn::Item) -> Result { let docs = docs.trim(); let info = quote! { - ::typst::model::FuncInfo { + ::typst::eval::FuncInfo { name, display: #display, category: #category, @@ -54,9 +54,9 @@ pub fn func(item: syn::Item) -> Result { #[doc(hidden)] #vis enum #ty {} - impl::typst::model::FuncType for #ty { - fn create_func(name: &'static str) -> ::typst::model::Func { - ::typst::model::Func::from_fn(#full, #info) + impl::typst::eval::FuncType for #ty { + fn create_func(name: &'static str) -> ::typst::eval::Func { + ::typst::eval::Func::from_fn(#full, #info) } } }) @@ -72,9 +72,9 @@ pub fn func(item: syn::Item) -> Result { Ok(quote! { #item - impl #params ::typst::model::FuncType for #ident #args #clause { - fn create_func(name: &'static str) -> ::typst::model::Func { - ::typst::model::Func::from_node::(#info) + impl #params ::typst::eval::FuncType for #ident #args #clause { + fn create_func(name: &'static str) -> ::typst::eval::Func { + ::typst::eval::Func::from_node::(#info) } } }) @@ -159,11 +159,11 @@ fn params(docs: &mut String) -> Result<(Vec, Vec)> { let docs = docs.trim(); infos.push(quote! { - ::typst::model::ParamInfo { + ::typst::eval::ParamInfo { name: #name, docs: #docs, - cast: <#ty as ::typst::model::Cast< - ::typst::syntax::Spanned<::typst::model::Value> + cast: <#ty as ::typst::eval::Cast< + ::typst::syntax::Spanned<::typst::eval::Value> >>::describe(), named: #named, positional: #positional, diff --git a/macros/src/node.rs b/macros/src/node.rs index b551182f3..0d59a4029 100644 --- a/macros/src/node.rs +++ b/macros/src/node.rs @@ -270,8 +270,8 @@ fn create_node_construct_func(node: &Node) -> syn::ImplItemMethod { node.construct.clone().unwrap_or_else(|| { parse_quote! { fn construct( - _: &::typst::model::Vm, - args: &mut ::typst::model::Args, + _: &::typst::eval::Vm, + args: &mut ::typst::eval::Args, ) -> ::typst::diag::SourceResult<::typst::model::Content> { ::typst::diag::bail!(args.span, "cannot be constructed manually"); } @@ -317,7 +317,7 @@ fn create_node_set_func(node: &Node) -> syn::ImplItemMethod { parse_quote! { fn set( - args: &mut ::typst::model::Args, + args: &mut ::typst::eval::Args, constructor: bool, ) -> ::typst::diag::SourceResult<::typst::model::StyleMap> { let mut styles = ::typst::model::StyleMap::new(); @@ -340,11 +340,11 @@ fn create_node_properties_func(node: &Node) -> syn::ImplItemMethod { let docs = docs.trim(); quote! { - ::typst::model::ParamInfo { + ::typst::eval::ParamInfo { name: #name, docs: #docs, - cast: <#value_ty as ::typst::model::Cast< - ::typst::syntax::Spanned<::typst::model::Value> + cast: <#value_ty as ::typst::eval::Cast< + ::typst::syntax::Spanned<::typst::eval::Value> >>::describe(), named: true, positional: #shorthand, @@ -356,7 +356,7 @@ fn create_node_properties_func(node: &Node) -> syn::ImplItemMethod { }); parse_quote! { - fn properties() -> ::std::vec::Vec<::typst::model::ParamInfo> + fn properties() -> ::std::vec::Vec<::typst::eval::ParamInfo> where Self: Sized { @@ -372,7 +372,7 @@ fn create_node_field_method(node: &Node) -> syn::ImplItemMethod { fn field( &self, _: &str, - ) -> ::std::option::Option<::typst::model::Value> { + ) -> ::std::option::Option<::typst::eval::Value> { None } } diff --git a/macros/src/symbols.rs b/macros/src/symbols.rs index fb120883c..efa4834d3 100644 --- a/macros/src/symbols.rs +++ b/macros/src/symbols.rs @@ -62,11 +62,11 @@ impl Parse for Kind { impl Kind { fn expand(&self) -> TokenStream { match self { - Self::Single(c) => quote! { typst::model::Symbol::new(#c), }, + Self::Single(c) => quote! { typst::eval::Symbol::new(#c), }, Self::Multiple(variants) => { let variants = variants.iter().map(Variant::expand); quote! { - typst::model::Symbol::list(&[#(#variants),*]) + typst::eval::Symbol::list(&[#(#variants),*]) } } } diff --git a/src/doc.rs b/src/doc.rs index 55e2f467a..cba3ca99c 100644 --- a/src/doc.rs +++ b/src/doc.rs @@ -7,15 +7,14 @@ use std::sync::Arc; use ecow::EcoString; +use crate::eval::{dict, Dict, Value}; use crate::font::Font; use crate::geom::{ self, rounded_rect, Abs, Align, Axes, Color, Corners, Dir, Em, Geometry, Numeric, Paint, Point, Rel, RgbaColor, Shape, Sides, Size, Stroke, Transform, }; use crate::image::Image; -use crate::model::{ - capable, dict, node, Content, Dict, Fold, StableId, StyleChain, Value, -}; +use crate::model::{capable, node, Content, Fold, StableId, StyleChain}; /// A finished document with metadata and page frames. #[derive(Debug, Default, Clone, Hash)] diff --git a/src/model/args.rs b/src/eval/args.rs similarity index 100% rename from src/model/args.rs rename to src/eval/args.rs diff --git a/src/model/array.rs b/src/eval/array.rs similarity index 98% rename from src/model/array.rs rename to src/eval/array.rs index 746763ab2..53bae06fb 100644 --- a/src/model/array.rs +++ b/src/eval/array.rs @@ -12,11 +12,11 @@ use crate::diag::{bail, At, SourceResult, StrResult}; #[doc(hidden)] macro_rules! __array { ($value:expr; $count:expr) => { - $crate::model::Array::from_vec($crate::model::eco_vec![$value.into(); $count]) + $crate::eval::Array::from_vec($crate::eval::eco_vec![$value.into(); $count]) }; ($($value:expr),* $(,)?) => { - $crate::model::Array::from_vec($crate::model::eco_vec![$($value.into()),*]) + $crate::eval::Array::from_vec($crate::eval::eco_vec![$($value.into()),*]) }; } diff --git a/src/model/cast.rs b/src/eval/cast.rs similarity index 99% rename from src/model/cast.rs rename to src/eval/cast.rs index 4c3005504..77521f7f5 100644 --- a/src/model/cast.rs +++ b/src/eval/cast.rs @@ -4,9 +4,7 @@ use std::str::FromStr; use ecow::EcoString; -use super::{ - castable, Array, Content, Dict, Func, Label, Regex, Selector, Str, Transform, Value, -}; +use super::{castable, Array, Dict, Func, Regex, Str, Value}; use crate::diag::StrResult; use crate::doc::{Destination, Lang, Location, Region}; use crate::font::{FontStretch, FontStyle, FontWeight}; @@ -14,6 +12,7 @@ use crate::geom::{ Axes, Color, Corners, Dir, GenAlign, Get, Length, Paint, PartialStroke, Point, Ratio, Rel, Sides, Smart, }; +use crate::model::{Content, Label, Selector, Transform}; use crate::syntax::Spanned; /// Cast from a value to a specific type. diff --git a/src/model/dict.rs b/src/eval/dict.rs similarity index 99% rename from src/model/dict.rs rename to src/eval/dict.rs index 50a2275f5..6c1934c9c 100644 --- a/src/model/dict.rs +++ b/src/eval/dict.rs @@ -18,7 +18,7 @@ macro_rules! __dict { #[allow(unused_mut)] let mut map = std::collections::BTreeMap::new(); $(map.insert($key.into(), $value.into());)* - $crate::model::Dict::from_map(map) + $crate::eval::Dict::from_map(map) }}; } diff --git a/src/model/func.rs b/src/eval/func.rs similarity index 99% rename from src/model/func.rs rename to src/eval/func.rs index 2ba462d33..e5280932a 100644 --- a/src/model/func.rs +++ b/src/eval/func.rs @@ -5,11 +5,9 @@ use std::sync::Arc; use comemo::{Prehashed, Track, Tracked, TrackedMut}; use ecow::EcoString; -use super::{ - Args, CastInfo, Dict, Eval, Flow, Node, NodeId, Route, Scope, Scopes, Selector, - StyleMap, Tracer, Value, Vm, -}; +use super::{Args, CastInfo, Dict, Eval, Flow, Route, Scope, Scopes, Tracer, Value, Vm}; use crate::diag::{bail, SourceResult, StrResult}; +use crate::model::{Node, NodeId, Selector, StyleMap}; use crate::syntax::ast::{self, AstNode, Expr}; use crate::syntax::{SourceId, Span, SyntaxNode}; use crate::util::hash128; diff --git a/src/model/library.rs b/src/eval/library.rs similarity index 97% rename from src/model/library.rs rename to src/eval/library.rs index 8ef22f10c..adfcc6e7c 100644 --- a/src/model/library.rs +++ b/src/eval/library.rs @@ -5,10 +5,11 @@ use std::num::NonZeroUsize; use ecow::EcoString; use once_cell::sync::OnceCell; -use super::{Content, Module, NodeId, StyleChain, StyleMap, Vt}; +use super::Module; use crate::diag::SourceResult; use crate::doc::Document; use crate::geom::{Abs, Dir}; +use crate::model::{Content, NodeId, StyleChain, StyleMap, Vt}; use crate::util::hash128; /// Definition of Typst's standard library. @@ -139,6 +140,6 @@ pub fn set_lang_items(items: LangItems) { /// Access a lang item. macro_rules! item { ($name:ident) => { - $crate::model::LANG_ITEMS.get().unwrap().$name + $crate::eval::LANG_ITEMS.get().unwrap().$name }; } diff --git a/src/model/methods.rs b/src/eval/methods.rs similarity index 100% rename from src/model/methods.rs rename to src/eval/methods.rs diff --git a/src/model/eval.rs b/src/eval/mod.rs similarity index 98% rename from src/model/eval.rs rename to src/eval/mod.rs index 225c5e7ab..2cf6f4d19 100644 --- a/src/model/eval.rs +++ b/src/eval/mod.rs @@ -1,5 +1,40 @@ //! Evaluation of markup into modules. +#[macro_use] +mod library; +#[macro_use] +mod cast; +#[macro_use] +mod array; +#[macro_use] +mod dict; +#[macro_use] +mod str; +#[macro_use] +mod value; +mod args; +mod func; +mod methods; +mod module; +mod ops; +mod scope; +mod symbol; + +pub use typst_macros::{castable, func}; + +pub use self::args::*; +pub use self::array::*; +pub use self::cast::*; +pub use self::dict::*; +pub use self::func::*; +pub use self::library::*; +pub use self::methods::*; +pub use self::module::*; +pub use self::scope::*; +pub use self::str::*; +pub use self::symbol::*; +pub use self::value::*; + use std::collections::BTreeMap; use std::mem; use std::path::{Path, PathBuf}; @@ -8,14 +43,10 @@ use comemo::{Track, Tracked, TrackedMut}; use ecow::EcoVec; use unicode_segmentation::UnicodeSegmentation; -use super::{ - combining_accent, methods, ops, Arg, Args, Array, CapturesVisitor, Closure, Content, - Dict, Func, Label, LangItems, Module, Recipe, Scopes, Selector, StyleMap, Symbol, - Transform, Value, -}; use crate::diag::{ bail, error, At, SourceError, SourceResult, StrResult, Trace, Tracepoint, }; +use crate::model::{Content, Label, Recipe, Selector, StyleMap, Transform}; use crate::syntax::ast::AstNode; use crate::syntax::{ ast, parse_code, Source, SourceId, Span, Spanned, SyntaxKind, SyntaxNode, @@ -43,7 +74,7 @@ pub fn eval( // Hook up the lang items. let library = world.library(); - super::set_lang_items(library.items.clone()); + set_lang_items(library.items.clone()); // Evaluate the module. let route = unsafe { Route::insert(route, id) }; diff --git a/src/model/module.rs b/src/eval/module.rs similarity index 100% rename from src/model/module.rs rename to src/eval/module.rs diff --git a/src/model/ops.rs b/src/eval/ops.rs similarity index 100% rename from src/model/ops.rs rename to src/eval/ops.rs diff --git a/src/model/scope.rs b/src/eval/scope.rs similarity index 100% rename from src/model/scope.rs rename to src/eval/scope.rs diff --git a/src/model/str.rs b/src/eval/str.rs similarity index 99% rename from src/model/str.rs rename to src/eval/str.rs index 5fcc1d059..63ea5dc8b 100644 --- a/src/model/str.rs +++ b/src/eval/str.rs @@ -15,7 +15,7 @@ use crate::geom::GenAlign; #[doc(hidden)] macro_rules! __format_str { ($($tts:tt)*) => {{ - $crate::model::Str::from($crate::model::eco_format!($($tts)*)) + $crate::eval::Str::from($crate::eval::eco_format!($($tts)*)) }}; } diff --git a/src/model/symbol.rs b/src/eval/symbol.rs similarity index 100% rename from src/model/symbol.rs rename to src/eval/symbol.rs diff --git a/src/model/value.rs b/src/eval/value.rs similarity index 99% rename from src/model/value.rs rename to src/eval/value.rs index f6ab95de2..5e06da76d 100644 --- a/src/model/value.rs +++ b/src/eval/value.rs @@ -461,7 +461,7 @@ primitive! { Args: "arguments", Args } #[cfg(test)] mod tests { use super::*; - use crate::model::{array, dict}; + use crate::eval::{array, dict}; #[track_caller] fn test(value: impl Into, exp: &str) { diff --git a/src/ide/analyze.rs b/src/ide/analyze.rs index 357e98f8a..3c46cca15 100644 --- a/src/ide/analyze.rs +++ b/src/ide/analyze.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use comemo::Track; -use crate::model::{eval, Module, Route, Tracer, Value}; +use crate::eval::{eval, Module, Route, Tracer, Value}; use crate::syntax::{ast, LinkedNode, Source, SyntaxKind}; use crate::util::PathExt; use crate::World; diff --git a/src/ide/complete.rs b/src/ide/complete.rs index fe1afe66b..06ab53a13 100644 --- a/src/ide/complete.rs +++ b/src/ide/complete.rs @@ -4,7 +4,7 @@ use ecow::{eco_format, EcoString}; use if_chain::if_chain; use super::{analyze_expr, analyze_import, plain_docs_sentence, summarize_font_family}; -use crate::model::{methods_on, CastInfo, Scope, Value}; +use crate::eval::{methods_on, CastInfo, Scope, Value}; use crate::syntax::{ast, LinkedNode, Source, SyntaxKind}; use crate::World; diff --git a/src/ide/tooltip.rs b/src/ide/tooltip.rs index 0796c09ef..ac0cb60b3 100644 --- a/src/ide/tooltip.rs +++ b/src/ide/tooltip.rs @@ -2,8 +2,8 @@ use if_chain::if_chain; use unicode_segmentation::UnicodeSegmentation; use super::{analyze_expr, plain_docs_sentence, summarize_font_family}; +use crate::eval::{CastInfo, Tracer, Value}; use crate::geom::{round_2, Length, Numeric}; -use crate::model::{CastInfo, Tracer, Value}; use crate::syntax::ast; use crate::syntax::{LinkedNode, Source, SyntaxKind}; use crate::World; diff --git a/src/lib.rs b/src/lib.rs index 1c3998640..d73055d14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,8 +23,8 @@ //! [parsed]: syntax::parse //! [syntax tree]: syntax::SyntaxNode //! [AST]: syntax::ast -//! [evaluate]: model::eval -//! [module]: model::Module +//! [evaluate]: eval::eval +//! [module]: eval::Module //! [content]: model::Content //! [typeset]: model::typeset //! [document]: doc::Document @@ -43,23 +43,23 @@ pub mod geom; #[macro_use] pub mod diag; #[macro_use] -pub mod model; +pub mod eval; pub mod doc; pub mod export; pub mod font; pub mod ide; pub mod image; +pub mod model; pub mod syntax; use std::path::Path; use comemo::{Prehashed, Track}; -use model::Tracer; use crate::diag::{FileResult, SourceResult}; use crate::doc::Document; +use crate::eval::{Library, Route, Tracer}; use crate::font::{Font, FontBook}; -use crate::model::{Library, Route}; use crate::syntax::{Source, SourceId}; use crate::util::Buffer; @@ -68,7 +68,7 @@ pub fn compile(world: &(dyn World + 'static), source: &Source) -> SourceResult bool { + pub(crate) fn labellable(&self) -> bool { !self.has::() } diff --git a/src/model/mod.rs b/src/model/mod.rs index 32b0a0033..692d18d54 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1,49 +1,16 @@ -//! Content and computation model. +//! The document model. -#[macro_use] -mod library; -#[macro_use] -mod cast; -#[macro_use] -mod array; -#[macro_use] -mod dict; -#[macro_use] -mod str; -#[macro_use] -mod value; #[macro_use] mod styles; -mod args; mod content; -mod eval; -mod func; -mod methods; -mod module; -mod ops; mod realize; -mod scope; -mod symbol; mod typeset; +pub use self::content::*; +pub use self::realize::*; +pub use self::styles::*; +pub use self::typeset::*; + #[doc(hidden)] pub use once_cell; -pub use typst_macros::{capability, capable, castable, func, node}; - -pub use self::args::*; -pub use self::array::*; -pub use self::cast::*; -pub use self::content::*; -pub use self::dict::*; -pub use self::eval::*; -pub use self::func::*; -pub use self::library::*; -pub use self::methods::*; -pub use self::module::*; -pub use self::realize::*; -pub use self::scope::*; -pub use self::str::*; -pub use self::styles::*; -pub use self::symbol::*; -pub use self::typeset::*; -pub use self::value::*; +pub use typst_macros::{capability, capable, node}; diff --git a/src/model/styles.rs b/src/model/styles.rs index 27c403097..185074917 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -7,8 +7,9 @@ use std::sync::Arc; use comemo::{Prehashed, Tracked}; -use super::{Args, Content, Dict, Func, Label, NodeId, Regex, Value}; +use super::{Content, Label, NodeId}; use crate::diag::{SourceResult, Trace, Tracepoint}; +use crate::eval::{Args, Dict, Func, Regex, Value}; use crate::geom::{ Abs, Align, Axes, Corners, Em, GenAlign, Length, Numeric, PartialStroke, Rel, Sides, Smart, diff --git a/src/model/typeset.rs b/src/model/typeset.rs index 7af8094c9..f8b5e0121 100644 --- a/src/model/typeset.rs +++ b/src/model/typeset.rs @@ -5,9 +5,10 @@ use std::num::NonZeroUsize; use comemo::{Track, Tracked, TrackedMut}; -use super::{Content, Selector, StyleChain, Value}; +use super::{Content, Selector, StyleChain}; use crate::diag::SourceResult; use crate::doc::{Document, Element, Frame, Location, Meta}; +use crate::eval::Value; use crate::geom::Transform; use crate::util::hash128; use crate::World; @@ -46,7 +47,7 @@ pub fn typeset(world: Tracked, content: &Content) -> SourceResult { /// The compilation environment. #[doc(hidden)] diff --git a/tests/src/benches.rs b/tests/src/benches.rs index 1628f32dc..ea53f6029 100644 --- a/tests/src/benches.rs +++ b/tests/src/benches.rs @@ -3,8 +3,8 @@ use std::path::Path; use comemo::{Prehashed, Track, Tracked}; use iai::{black_box, main, Iai}; use typst::diag::{FileError, FileResult}; +use typst::eval::Library; use typst::font::{Font, FontBook}; -use typst::model::Library; use typst::syntax::{Source, SourceId}; use typst::util::Buffer; use typst::World; @@ -59,24 +59,19 @@ fn bench_edit(iai: &mut Iai) { fn bench_eval(iai: &mut Iai) { let world = BenchWorld::new(); - let route = typst::model::Route::default(); - let mut tracer = typst::model::Tracer::default(); + let route = typst::eval::Route::default(); + let mut tracer = typst::eval::Tracer::default(); iai.run(|| { - typst::model::eval( - world.track(), - route.track(), - tracer.track_mut(), - &world.source, - ) - .unwrap() + typst::eval::eval(world.track(), route.track(), tracer.track_mut(), &world.source) + .unwrap() }); } fn bench_typeset(iai: &mut Iai) { let world = BenchWorld::new(); - let route = typst::model::Route::default(); - let mut tracer = typst::model::Tracer::default(); - let module = typst::model::eval( + let route = typst::eval::Route::default(); + let mut tracer = typst::eval::Tracer::default(); + let module = typst::eval::eval( world.track(), route.track(), tracer.track_mut(), diff --git a/tests/src/tests.rs b/tests/src/tests.rs index 62e6f3f52..0c2b0490f 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -13,9 +13,9 @@ use once_cell::unsync::OnceCell; use tiny_skia as sk; use typst::diag::{bail, FileError, FileResult, SourceResult}; use typst::doc::{Document, Element, Frame, Meta}; +use typst::eval::{func, Library, Value}; use typst::font::{Font, FontBook}; use typst::geom::{Abs, RgbaColor, Sides, Smart}; -use typst::model::{func, Library, Value}; use typst::syntax::{Source, SourceId, Span, SyntaxNode}; use typst::util::{Buffer, PathExt}; use typst::World; @@ -150,7 +150,7 @@ fn library() -> Library { /// ## Category /// test #[func] - fn test(args: &mut typst::model::Args) -> SourceResult { + fn test(args: &mut typst::eval::Args) -> SourceResult { let lhs = args.expect::("left-hand side")?; let rhs = args.expect::("right-hand side")?; if lhs != rhs { @@ -163,7 +163,7 @@ fn library() -> Library { /// ## Category /// test #[func] - fn print(args: &mut typst::model::Args) -> SourceResult { + fn print(args: &mut typst::eval::Args) -> SourceResult { print!("> "); for (i, value) in args.all::()?.into_iter().enumerate() { if i > 0 { @@ -441,10 +441,10 @@ fn test_part( if world.print.model { let world = (world as &dyn World).track(); - let route = typst::model::Route::default(); - let mut tracer = typst::model::Tracer::default(); + let route = typst::eval::Route::default(); + let mut tracer = typst::eval::Tracer::default(); let module = - typst::model::eval(world, route.track(), tracer.track_mut(), source).unwrap(); + typst::eval::eval(world, route.track(), tracer.track_mut(), source).unwrap(); println!("Model:\n{:#?}\n", module.content()); }