diff --git a/main/src/main.rs b/main/src/main.rs index 21b96a256..126dbf8b1 100644 --- a/main/src/main.rs +++ b/main/src/main.rs @@ -45,25 +45,24 @@ fn main() { let state = State::default(); let Pass { output: layouts, - feedback: Feedback { mut diagnostics, .. }, + feedback: Feedback { mut diags, .. }, } = block_on(typeset(&src, state, Rc::clone(&loader))); - if !diagnostics.is_empty() { - diagnostics.sort(); + if !diags.is_empty() { + diags.sort(); let map = LineMap::new(&src); - for diagnostic in diagnostics { - let span = diagnostic.span; + for diag in diags { + let span = diag.span; let start = map.location(span.start); let end = map.location(span.end); - println!( " {}: {}:{}-{}: {}", - diagnostic.v.level, + diag.v.level, src_path.display(), start, end, - diagnostic.v.message, + diag.v.message, ); } } diff --git a/src/diagnostic.rs b/src/diag.rs similarity index 81% rename from src/diagnostic.rs rename to src/diag.rs index 8d56f4011..38f3d0a27 100644 --- a/src/diagnostic.rs +++ b/src/diag.rs @@ -1,15 +1,15 @@ //! Diagnostics for source code. //! //! There are no fatal errors. The document will always compile and yield a -//! layout on a best effort process, generating diagnostics for incorrect -//! things. +//! layout on a best effort process, but diagnostics are nevertheless generated +//! for incorrect things. use std::fmt::{self, Display, Formatter}; /// A diagnostic that arose in parsing or layouting. #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] #[cfg_attr(feature = "serialize", derive(serde::Serialize))] -pub struct Diagnostic { +pub struct Diag { /// How severe / important the diagnostic is. pub level: Level, /// A message describing the diagnostic. @@ -25,10 +25,10 @@ pub enum Level { Error, } -impl Diagnostic { +impl Diag { /// Create a new diagnostic from message and level. - pub fn new(message: impl Into, level: Level) -> Self { - Self { message: message.into(), level } + pub fn new(level: Level, message: impl Into) -> Self { + Self { level, message: message.into() } } } @@ -64,7 +64,7 @@ impl Display for Level { #[macro_export] macro_rules! error { ($($tts:tt)*) => { - $crate::__impl_diagnostic!($crate::diagnostic::Level::Error; $($tts)*) + $crate::__impl_diagnostic!($crate::diag::Level::Error; $($tts)*) }; } @@ -77,7 +77,7 @@ macro_rules! error { #[macro_export] macro_rules! warning { ($($tts:tt)*) => { - $crate::__impl_diagnostic!($crate::diagnostic::Level::Warning; $($tts)*) + $crate::__impl_diagnostic!($crate::diag::Level::Warning; $($tts)*) }; } @@ -86,11 +86,11 @@ macro_rules! warning { #[doc(hidden)] macro_rules! __impl_diagnostic { ($level:expr; @$feedback:expr, $($tts:tt)*) => { - $feedback.diagnostics.push($crate::__impl_diagnostic!($level; $($tts)*)); + $feedback.diags.push($crate::__impl_diagnostic!($level; $($tts)*)); }; ($level:expr; $fmt:literal $($tts:tt)*) => { - $crate::diagnostic::Diagnostic::new(format!($fmt $($tts)*), $level) + $crate::diag::Diag::new($level, format!($fmt $($tts)*)) }; ($level:expr; $span:expr, $fmt:literal $($tts:tt)*) => { diff --git a/src/eval/mod.rs b/src/eval/mod.rs index dc35ce714..3796669b8 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -90,13 +90,13 @@ impl Eval for ExprCall { let span = self.name.span; let args = self.args.eval(ctx).await; - if let Some(func) = ctx.state.scope.func(name) { - ctx.f.decorations.push(Decoration::Resolved.span_with(span)); + if let Some(func) = ctx.state.scope.get(name) { + ctx.f.decos.push(Deco::Resolved.span_with(span)); (func.clone())(args, ctx).await } else { if !name.is_empty() { error!(@ctx.f, span, "unknown function"); - ctx.f.decorations.push(Decoration::Unresolved.span_with(span)); + ctx.f.decos.push(Deco::Unresolved.span_with(span)); } Value::Dict(args) } diff --git a/src/eval/scope.rs b/src/eval/scope.rs index 7d69e1fcb..fc530bbb9 100644 --- a/src/eval/scope.rs +++ b/src/eval/scope.rs @@ -18,14 +18,14 @@ impl Scope { Self { functions: HashMap::new() } } - /// Associate the given name with the function. - pub fn insert(&mut self, name: impl Into, function: ValueFunc) { - self.functions.insert(name.into(), function); + /// Return the function with the given name if there is one. + pub fn get(&self, name: &str) -> Option<&ValueFunc> { + self.functions.get(name) } - /// Return the function with the given name if there is one. - pub fn func(&self, name: &str) -> Option<&ValueFunc> { - self.functions.get(name) + /// Associate the given name with the function. + pub fn set(&mut self, name: impl Into, function: ValueFunc) { + self.functions.insert(name.into(), function); } } diff --git a/src/eval/state.rs b/src/eval/state.rs index d6e0b1952..e4e9f7937 100644 --- a/src/eval/state.rs +++ b/src/eval/state.rs @@ -75,7 +75,7 @@ impl TextState { } /// The absolute paragraph spacing. - pub fn paragraph_spacing(&self) -> f64 { + pub fn par_spacing(&self) -> f64 { self.par_spacing.eval(self.font_size()) } } diff --git a/src/eval/value.rs b/src/eval/value.rs index f1ffc6de3..16c3dca8b 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -530,10 +530,7 @@ mod tests { dict.expect::("", Span::ZERO, &mut f), Some("hi".to_string()) ); - assert_eq!(f.diagnostics, [error!( - Span::ZERO, - "expected string, found bool" - )]); + assert_eq!(f.diags, [error!(Span::ZERO, "expected string, found bool")]); assert_eq!(dict.len(), 1); } @@ -545,10 +542,7 @@ mod tests { dict.insert("hi", entry(Value::Bool(true))); assert_eq!(dict.take::(), Some(false)); assert_eq!(dict.take_key::("hi", &mut f), None); - assert_eq!(f.diagnostics, [error!( - Span::ZERO, - "expected float, found bool" - )]); + assert_eq!(f.diags, [error!(Span::ZERO, "expected float, found bool")]); assert!(dict.is_empty()); } diff --git a/src/layout/tree.rs b/src/layout/tree.rs index 4dbf716ef..5468a7cd6 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -4,7 +4,7 @@ use super::*; use crate::eval::Eval; use crate::shaping; use crate::syntax::{ - Decoration, Expr, NodeHeading, NodeRaw, Span, SpanWith, Spanned, SynNode, SynTree, + Deco, Expr, NodeHeading, NodeRaw, Span, SpanWith, Spanned, SynNode, SynTree, }; use crate::DynFuture; @@ -52,18 +52,18 @@ impl<'a> TreeLayouter<'a> { } async fn layout_node(&mut self, node: &Spanned) { - let decorate = |this: &mut Self, deco: Decoration| { - this.ctx.f.decorations.push(deco.span_with(node.span)); + let decorate = |this: &mut Self, deco: Deco| { + this.ctx.f.decos.push(deco.span_with(node.span)); }; match &node.v { SynNode::Space => self.layout_space(), SynNode::Text(text) => { if self.ctx.state.text.emph { - decorate(self, Decoration::Emph); + decorate(self, Deco::Emph); } if self.ctx.state.text.strong { - decorate(self, Decoration::Strong); + decorate(self, Deco::Strong); } self.layout_text(text).await; } @@ -72,11 +72,11 @@ impl<'a> TreeLayouter<'a> { SynNode::Parbreak => self.layout_parbreak(), SynNode::Emph => { self.ctx.state.text.emph ^= true; - decorate(self, Decoration::Emph); + decorate(self, Deco::Emph); } SynNode::Strong => { self.ctx.state.text.strong ^= true; - decorate(self, Decoration::Strong); + decorate(self, Deco::Strong); } SynNode::Heading(heading) => self.layout_heading(heading).await, @@ -95,7 +95,7 @@ impl<'a> TreeLayouter<'a> { fn layout_parbreak(&mut self) { self.layouter.add_secondary_spacing( - self.ctx.state.text.paragraph_spacing(), + self.ctx.state.text.par_spacing(), SpacingKind::PARAGRAPH, ); } @@ -188,7 +188,7 @@ impl<'a> TreeLayouter<'a> { } else { error!( @self.ctx.f, span, - "page break cannot only be issued from root context", + "page break can only be issued from root context", ); } } @@ -214,7 +214,7 @@ impl<'a> TreeLayouter<'a> { } else { error!( @self.ctx.f, span, - "page style cannot only be changed from root context", + "page style can only be changed from root context", ); } } diff --git a/src/lib.rs b/src/lib.rs index 1cd948d71..d2bd076d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ //! [`MultiLayout`]: layout/type.MultiLayout.html #[macro_use] -pub mod diagnostic; +pub mod diag; pub mod color; pub mod eval; @@ -42,11 +42,11 @@ use std::fmt::Debug; use std::future::Future; use std::pin::Pin; -use crate::diagnostic::Diagnostic; +use crate::diag::Diag; use crate::eval::State; use crate::font::SharedFontLoader; use crate::layout::BoxLayout; -use crate::syntax::{Decoration, Offset, Pos, SpanVec}; +use crate::syntax::{Deco, Offset, Pos, SpanVec}; /// Process source code directly into a collection of layouts. pub async fn typeset( @@ -97,15 +97,15 @@ impl Pass { #[derive(Debug, Default, Clone, Eq, PartialEq)] pub struct Feedback { /// Diagnostics about the source code. - pub diagnostics: SpanVec, + pub diags: SpanVec, /// Decorations of the source code for semantic syntax highlighting. - pub decorations: SpanVec, + pub decos: SpanVec, } impl Feedback { /// Create a new feedback instance without errors and decos. pub fn new() -> Self { - Self { diagnostics: vec![], decorations: vec![] } + Self { diags: vec![], decos: vec![] } } /// Merged two feedbacks into one. @@ -116,14 +116,14 @@ impl Feedback { /// Add other feedback data to this feedback. pub fn extend(&mut self, more: Self) { - self.diagnostics.extend(more.diagnostics); - self.decorations.extend(more.decorations); + self.diags.extend(more.diags); + self.decos.extend(more.decos); } /// Add more feedback whose spans are local and need to be offset by an /// `offset` to be correct in this feedback's context. pub fn extend_offset(&mut self, more: Self, offset: Pos) { - self.diagnostics.extend(more.diagnostics.offset(offset)); - self.decorations.extend(more.decorations.offset(offset)); + self.diags.extend(more.diags.offset(offset)); + self.decos.extend(more.decos.offset(offset)); } } diff --git a/src/library/mod.rs b/src/library/mod.rs index c6dfb758f..66fc5d597 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -22,18 +22,12 @@ macro_rules! std { /// Create a scope with all standard library functions. pub fn _std() -> Scope { let mut std = Scope::new(); - $(std.insert($name, wrap!($func));)* + $(std.set($name, ValueFunc::new(|args, ctx| Box::pin($func(args, ctx))));)* std } }; } -macro_rules! wrap { - ($func:expr) => { - ValueFunc::new(|args, ctx| Box::pin($func(args, ctx))) - }; -} - std! { "align" => align, "box" => boxed, diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 916f2fdc3..ca2375f29 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -246,7 +246,7 @@ fn dict_contents(p: &mut Parser) -> (LitDict, bool) { if let Some(key) = &entry.key { comma_and_keyless = false; - p.deco(Decoration::DictKey.span_with(key.span)); + p.deco(Deco::DictKey.span_with(key.span)); } let behind = entry.expr.span.end; diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 041a61bc1..83e9b0960 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -1,8 +1,8 @@ use std::fmt::{self, Debug, Formatter}; use super::{Scanner, TokenMode, Tokens}; -use crate::diagnostic::Diagnostic; -use crate::syntax::{Decoration, Pos, Span, SpanWith, Spanned, Token}; +use crate::diag::Diag; +use crate::syntax::{Deco, Pos, Span, SpanWith, Spanned, Token}; use crate::Feedback; /// A convenient token-based parser. @@ -34,8 +34,8 @@ impl<'s> Parser<'s> { } /// Add a diagnostic to the feedback. - pub fn diag(&mut self, diag: Spanned) { - self.f.diagnostics.push(diag); + pub fn diag(&mut self, diag: Spanned) { + self.f.diags.push(diag); } /// Eat the next token and add a diagnostic that it was not the expected @@ -66,8 +66,8 @@ impl<'s> Parser<'s> { } /// Add a decoration to the feedback. - pub fn deco(&mut self, deco: Spanned) { - self.f.decorations.push(deco); + pub fn deco(&mut self, deco: Spanned) { + self.f.decos.push(deco); } /// Update the token mode and push the previous mode onto a stack. diff --git a/src/parse/tests.rs b/src/parse/tests.rs index 21375a0f2..6738998a3 100644 --- a/src/parse/tests.rs +++ b/src/parse/tests.rs @@ -12,7 +12,7 @@ use crate::syntax::*; // ------------------------------ Construct Syntax Nodes ------------------------------ // -use Decoration::*; +use Deco::*; use SynNode::{Emph as E, Linebreak as L, Parbreak as P, Space as S, Strong as B}; fn T(text: &str) -> SynNode { @@ -160,7 +160,7 @@ macro_rules! e { ($src:expr => $($tts:tt)*) => { let exp = vec![$($tts)*]; let pass = parse($src); - let found = pass.feedback.diagnostics.iter() + let found = pass.feedback.diags.iter() .map(|s| s.as_ref().map(|e| e.message.as_str())) .collect::>(); check($src, exp, found, true); @@ -172,7 +172,7 @@ macro_rules! d { ($src:expr => $($tts:tt)*) => { let exp = vec![$($tts)*]; let pass = parse($src); - check($src, exp, pass.feedback.decorations, true); + check($src, exp, pass.feedback.decos, true); }; } diff --git a/src/prelude.rs b/src/prelude.rs index a7a0fd008..618d1537e 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -2,9 +2,8 @@ #[doc(no_inline)] pub use crate::eval::{Dict, Value, ValueDict}; -pub use crate::layout::primitive::*; #[doc(no_inline)] -pub use crate::layout::{layout_tree, Command, LayoutContext}; +pub use crate::layout::{layout_tree, primitive::*, Command, LayoutContext}; #[doc(no_inline)] pub use crate::syntax::{Span, Spanned, SynTree}; pub use crate::{Feedback, Pass}; diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 3ac5176b6..a6ae92670 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -15,7 +15,7 @@ pub use token::*; #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[cfg_attr(feature = "serialize", derive(serde::Serialize))] #[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))] -pub enum Decoration { +pub enum Deco { /// Emphasized text. Emph, /// Strong text. diff --git a/tests/test_typeset.rs b/tests/test_typeset.rs index 3d3b2b8f1..1ac06f5ec 100644 --- a/tests/test_typeset.rs +++ b/tests/test_typeset.rs @@ -74,25 +74,24 @@ fn test(name: &str, src: &str, src_path: &Path, loader: &SharedFontLoader) { let state = State::default(); let Pass { output: layouts, - feedback: Feedback { mut diagnostics, .. }, + feedback: Feedback { mut diags, .. }, } = block_on(typeset(&src, state, Rc::clone(loader))); - if !diagnostics.is_empty() { - diagnostics.sort(); + if !diags.is_empty() { + diags.sort(); let map = LineMap::new(&src); - for diagnostic in diagnostics { - let span = diagnostic.span; + for diag in diags { + let span = diag.span; let start = map.location(span.start); let end = map.location(span.end); - println!( " {}: {}:{}-{}: {}", - diagnostic.v.level, + diag.v.level, src_path.display(), start, end, - diagnostic.v.message, + diag.v.message, ); } }