summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main/src/main.rs15
-rw-r--r--src/diag.rs (renamed from src/diagnostic.rs)20
-rw-r--r--src/eval/mod.rs6
-rw-r--r--src/eval/scope.rs12
-rw-r--r--src/eval/state.rs2
-rw-r--r--src/eval/value.rs10
-rw-r--r--src/layout/tree.rs20
-rw-r--r--src/lib.rs20
-rw-r--r--src/library/mod.rs8
-rw-r--r--src/parse/mod.rs2
-rw-r--r--src/parse/parser.rs12
-rw-r--r--src/parse/tests.rs6
-rw-r--r--src/prelude.rs3
-rw-r--r--src/syntax/mod.rs2
-rw-r--r--tests/test_typeset.rs15
15 files changed, 69 insertions, 84 deletions
diff --git a/main/src/main.rs b/main/src/main.rs
index 21b96a25..126dbf8b 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
index 8d56f401..38f3d0a2 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<String>, level: Level) -> Self {
- Self { message: message.into(), level }
+ pub fn new(level: Level, message: impl Into<String>) -> 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 dc35ce71..3796669b 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 7d69e1fc..fc530bbb 100644
--- a/src/eval/scope.rs
+++ b/src/eval/scope.rs
@@ -18,15 +18,15 @@ impl Scope {
Self { functions: HashMap::new() }
}
- /// Associate the given name with the function.
- pub fn insert(&mut self, name: impl Into<String>, function: ValueFunc) {
- self.functions.insert(name.into(), function);
- }
-
/// Return the function with the given name if there is one.
- pub fn func(&self, name: &str) -> Option<&ValueFunc> {
+ pub fn get(&self, name: &str) -> Option<&ValueFunc> {
self.functions.get(name)
}
+
+ /// Associate the given name with the function.
+ pub fn set(&mut self, name: impl Into<String>, function: ValueFunc) {
+ self.functions.insert(name.into(), function);
+ }
}
impl Debug for Scope {
diff --git a/src/eval/state.rs b/src/eval/state.rs
index d6e0b195..e4e9f793 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 f1ffc6de..16c3dca8 100644
--- a/src/eval/value.rs
+++ b/src/eval/value.rs
@@ -530,10 +530,7 @@ mod tests {
dict.expect::<String>("", 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::<bool>(), Some(false));
assert_eq!(dict.take_key::<f64>("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 4dbf716e..5468a7cd 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<SynNode>) {
- 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 1cd948d7..d2bd076d 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<T> Pass<T> {
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Feedback {
/// Diagnostics about the source code.
- pub diagnostics: SpanVec<Diagnostic>,
+ pub diags: SpanVec<Diag>,
/// Decorations of the source code for semantic syntax highlighting.
- pub decorations: SpanVec<Decoration>,
+ pub decos: SpanVec<Deco>,
}
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 c6dfb758..66fc5d59 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 916f2fdc..ca2375f2 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 041a61bc..83e9b096 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<Diagnostic>) {
- self.f.diagnostics.push(diag);
+ pub fn diag(&mut self, diag: Spanned<Diag>) {
+ 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<Decoration>) {
- self.f.decorations.push(deco);
+ pub fn deco(&mut self, deco: Spanned<Deco>) {
+ 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 21375a0f..6738998a 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::<Vec<_>>();
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 a7a0fd00..618d1537 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 3ac5176b..a6ae9267 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 3d3b2b8f..1ac06f5e 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,
);
}
}