From 5ae81971f299688b05d77af208d7bb44ffce5e2d Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 23 Nov 2022 15:58:59 +0100 Subject: Introduce `Library` --- src/model/eval.rs | 7 ++- src/model/items.rs | 130 ----------------------------------------------- src/model/library.rs | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/model/mod.rs | 4 +- src/model/vm.rs | 4 +- 5 files changed, 150 insertions(+), 136 deletions(-) delete mode 100644 src/model/items.rs create mode 100644 src/model/library.rs (limited to 'src/model') diff --git a/src/model/eval.rs b/src/model/eval.rs index 7f2cea63..fb1bd121 100644 --- a/src/model/eval.rs +++ b/src/model/eval.rs @@ -35,10 +35,13 @@ pub fn eval( panic!("Tried to cyclicly evaluate {}", path); } + // Hook up the lang items. + let library = world.library(); + super::set_lang_items(library.items.clone()); + // Evaluate the module. let route = unsafe { Route::insert(route, id) }; - let std = &world.config().scope; - let scopes = Scopes::new(Some(std)); + let scopes = Scopes::new(Some(&library.scope)); let mut vm = Vm::new(world, route.track(), id, scopes); let result = source.ast()?.eval(&mut vm); diff --git a/src/model/items.rs b/src/model/items.rs deleted file mode 100644 index 50ea8b60..00000000 --- a/src/model/items.rs +++ /dev/null @@ -1,130 +0,0 @@ -use std::fmt::{self, Debug, Formatter}; -use std::hash::{Hash, Hasher}; -use std::num::NonZeroUsize; - -use comemo::Tracked; -use once_cell::sync::OnceCell; - -use super::{Content, NodeId, StyleChain}; -use crate::diag::SourceResult; -use crate::frame::Frame; -use crate::geom::{Abs, Dir}; -use crate::util::{hash128, EcoString}; -use crate::World; - -/// Global storage for lang items. -#[doc(hidden)] -pub static LANG_ITEMS: OnceCell = OnceCell::new(); - -/// Set the lang items. This is a hack :( -/// -/// Passing the lang items everywhere they are needed (especially the text node -/// related things) is very painful. By storing them globally, in theory, we -/// break incremental, but only when different sets of lang items are used in -/// the same program. For this reason, if this function is called multiple -/// times, the items must be the same. -pub(crate) fn set_lang_items(items: LangItems) { - if LANG_ITEMS.set(items).is_err() { - let first = hash128(LANG_ITEMS.get().unwrap()); - let second = hash128(&items); - assert_eq!(first, second, "set differing lang items"); - } -} - -/// Access a lang item. -macro_rules! item { - ($name:ident) => { - $crate::model::LANG_ITEMS.get().unwrap().$name - }; -} - -/// Definition of certain standard library items the language is aware of. -#[derive(Copy, Clone)] -pub struct LangItems { - /// The root layout function. - pub layout: fn( - content: &Content, - world: Tracked, - styles: StyleChain, - ) -> SourceResult>, - /// Access the em size. - pub em: fn(StyleChain) -> Abs, - /// Access the text direction. - pub dir: fn(StyleChain) -> Dir, - /// Whitespace. - pub space: fn() -> Content, - /// A forced line break: `\`. - pub linebreak: fn(justify: bool) -> Content, - /// Plain text without markup. - pub text: fn(text: EcoString) -> Content, - /// The id of the text node. - pub text_id: NodeId, - /// Get the string if this is a text node. - pub text_str: fn(&Content) -> Option<&str>, - /// A smart quote: `'` or `"`. - pub smart_quote: fn(double: bool) -> Content, - /// A paragraph break. - pub parbreak: fn() -> Content, - /// Strong content: `*Strong*`. - pub strong: fn(body: Content) -> Content, - /// Emphasized content: `_Emphasized_`. - pub emph: fn(body: Content) -> Content, - /// Raw text with optional syntax highlighting: `` `...` ``. - pub raw: fn(text: EcoString, tag: Option, block: bool) -> Content, - /// A hyperlink: `https://typst.org`. - pub link: fn(url: EcoString) -> Content, - /// A reference: `@target`. - pub ref_: fn(target: EcoString) -> Content, - /// A section heading: `= Introduction`. - pub heading: fn(level: NonZeroUsize, body: Content) -> Content, - /// An item in an unordered list: `- ...`. - pub list_item: fn(body: Content) -> Content, - /// An item in an enumeration (ordered list): `+ ...` or `1. ...`. - pub enum_item: fn(number: Option, body: Content) -> Content, - /// An item in a description list: `/ Term: Details`. - pub desc_item: fn(term: Content, body: Content) -> Content, - /// A mathematical formula: `$x$`, `$ x^2 $`. - pub math: fn(children: Vec, display: bool) -> Content, - /// An atom in a formula: `x`, `+`, `12`. - pub math_atom: fn(atom: EcoString) -> Content, - /// A base with optional sub- and superscripts in a formula: `a_1^2`. - pub math_script: - fn(base: Content, sub: Option, sup: Option) -> Content, - /// A fraction in a formula: `x/2`. - pub math_frac: fn(num: Content, denom: Content) -> Content, - /// An alignment indicator in a formula: `&`, `&&`. - pub math_align: fn(count: usize) -> Content, -} - -impl Debug for LangItems { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.pad("LangItems { .. }") - } -} - -impl Hash for LangItems { - fn hash(&self, state: &mut H) { - (self.layout as usize).hash(state); - (self.em as usize).hash(state); - (self.dir as usize).hash(state); - self.space.hash(state); - self.linebreak.hash(state); - self.text.hash(state); - self.smart_quote.hash(state); - self.parbreak.hash(state); - self.strong.hash(state); - self.emph.hash(state); - self.raw.hash(state); - self.link.hash(state); - self.ref_.hash(state); - self.heading.hash(state); - self.list_item.hash(state); - self.enum_item.hash(state); - self.desc_item.hash(state); - self.math.hash(state); - self.math_atom.hash(state); - self.math_script.hash(state); - self.math_frac.hash(state); - self.math_align.hash(state); - } -} diff --git a/src/model/library.rs b/src/model/library.rs new file mode 100644 index 00000000..8433e514 --- /dev/null +++ b/src/model/library.rs @@ -0,0 +1,141 @@ +use std::fmt::{self, Debug, Formatter}; +use std::hash::{Hash, Hasher}; +use std::num::NonZeroUsize; + +use comemo::Tracked; +use once_cell::sync::OnceCell; + +use super::{Content, NodeId, Scope, StyleChain, StyleMap}; +use crate::diag::SourceResult; +use crate::frame::Frame; +use crate::geom::{Abs, Dir}; +use crate::util::{hash128, EcoString}; +use crate::World; + +/// A Typst standard library. +#[derive(Debug, Clone, Hash)] +pub struct Library { + /// The scope containing definitions that are available everywhere. + pub scope: Scope, + /// The default properties for page size, font selection and so on. + pub styles: StyleMap, + /// Defines which standard library items fulfill which syntactical roles. + pub items: LangItems, +} + +/// Definition of certain standard library items the language is aware of. +#[derive(Clone)] +pub struct LangItems { + /// The root layout function. + pub layout: fn( + content: &Content, + world: Tracked, + styles: StyleChain, + ) -> SourceResult>, + /// Access the em size. + pub em: fn(StyleChain) -> Abs, + /// Access the text direction. + pub dir: fn(StyleChain) -> Dir, + /// Whitespace. + pub space: fn() -> Content, + /// A forced line break: `\`. + pub linebreak: fn(justify: bool) -> Content, + /// Plain text without markup. + pub text: fn(text: EcoString) -> Content, + /// The id of the text node. + pub text_id: NodeId, + /// Get the string if this is a text node. + pub text_str: fn(&Content) -> Option<&str>, + /// A smart quote: `'` or `"`. + pub smart_quote: fn(double: bool) -> Content, + /// A paragraph break. + pub parbreak: fn() -> Content, + /// Strong content: `*Strong*`. + pub strong: fn(body: Content) -> Content, + /// Emphasized content: `_Emphasized_`. + pub emph: fn(body: Content) -> Content, + /// Raw text with optional syntax highlighting: `` `...` ``. + pub raw: fn(text: EcoString, tag: Option, block: bool) -> Content, + /// A hyperlink: `https://typst.org`. + pub link: fn(url: EcoString) -> Content, + /// A reference: `@target`. + pub ref_: fn(target: EcoString) -> Content, + /// A section heading: `= Introduction`. + pub heading: fn(level: NonZeroUsize, body: Content) -> Content, + /// An item in an unordered list: `- ...`. + pub list_item: fn(body: Content) -> Content, + /// An item in an enumeration (ordered list): `+ ...` or `1. ...`. + pub enum_item: fn(number: Option, body: Content) -> Content, + /// An item in a description list: `/ Term: Details`. + pub desc_item: fn(term: Content, body: Content) -> Content, + /// A mathematical formula: `$x$`, `$ x^2 $`. + pub math: fn(children: Vec, display: bool) -> Content, + /// An atom in a formula: `x`, `+`, `12`. + pub math_atom: fn(atom: EcoString) -> Content, + /// A base with optional sub- and superscripts in a formula: `a_1^2`. + pub math_script: + fn(base: Content, sub: Option, sup: Option) -> Content, + /// A fraction in a formula: `x/2`. + pub math_frac: fn(num: Content, denom: Content) -> Content, + /// An alignment indicator in a formula: `&`, `&&`. + pub math_align: fn(count: usize) -> Content, +} + +impl Debug for LangItems { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.pad("LangItems { .. }") + } +} + +impl Hash for LangItems { + fn hash(&self, state: &mut H) { + (self.layout as usize).hash(state); + (self.em as usize).hash(state); + (self.dir as usize).hash(state); + self.space.hash(state); + self.linebreak.hash(state); + self.text.hash(state); + self.smart_quote.hash(state); + self.parbreak.hash(state); + self.strong.hash(state); + self.emph.hash(state); + self.raw.hash(state); + self.link.hash(state); + self.ref_.hash(state); + self.heading.hash(state); + self.list_item.hash(state); + self.enum_item.hash(state); + self.desc_item.hash(state); + self.math.hash(state); + self.math_atom.hash(state); + self.math_script.hash(state); + self.math_frac.hash(state); + self.math_align.hash(state); + } +} + +/// Global storage for lang items. +#[doc(hidden)] +pub static LANG_ITEMS: OnceCell = OnceCell::new(); + +/// Set the lang items. This is a hack :( +/// +/// Passing the lang items everywhere they are needed (especially the text node +/// related things) is very painful. By storing them globally, in theory, we +/// break incremental, but only when different sets of lang items are used in +/// the same program. For this reason, if this function is called multiple +/// times, the items must be the same. +pub(crate) fn set_lang_items(items: LangItems) { + if let Err(items) = LANG_ITEMS.set(items) { + let first = hash128(LANG_ITEMS.get().unwrap()); + let second = hash128(&items); + assert_eq!(first, second, "set differing lang items"); + } +} + +/// Access a lang item. +macro_rules! item { + ($name:ident) => { + $crate::model::LANG_ITEMS.get().unwrap().$name + }; +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 0f946a40..93e33d5c 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1,7 +1,7 @@ //! Document and computation model. #[macro_use] -mod items; +mod library; #[macro_use] mod cast; #[macro_use] @@ -34,7 +34,7 @@ pub use self::content::*; pub use self::dict::*; pub use self::eval::*; pub use self::func::*; -pub use self::items::*; +pub use self::library::*; pub use self::scope::*; pub use self::str::*; pub use self::styles::*; diff --git a/src/model/vm.rs b/src/model/vm.rs index 28885f29..d13be29c 100644 --- a/src/model/vm.rs +++ b/src/model/vm.rs @@ -38,7 +38,7 @@ impl<'a> Vm<'a> { location, scopes, flow: None, - items: world.config().items, + items: world.library().items.clone(), } } @@ -47,7 +47,7 @@ impl<'a> Vm<'a> { pub fn locate(&self, path: &str) -> StrResult { if !self.location.is_detached() { if let Some(path) = path.strip_prefix('/') { - return Ok(self.world.config().root.join(path).normalize()); + return Ok(self.world.root().join(path).normalize()); } if let Some(dir) = self.world.source(self.location).path().parent() { -- cgit v1.2.3