Shorten some names ↔

This commit is contained in:
Laurenz 2020-10-04 20:22:11 +02:00
parent a41d7ab47d
commit ef8aa763fa
15 changed files with 69 additions and 84 deletions

View File

@ -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,
);
}
}

View File

@ -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)*) => {

View File

@ -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)
}

View File

@ -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<String>, 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<String>, function: ValueFunc) {
self.functions.insert(name.into(), function);
}
}

View File

@ -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())
}
}

View File

@ -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());
}

View File

@ -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",
);
}
}

View File

@ -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));
}
}

View File

@ -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,

View File

@ -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;

View File

@ -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.

View File

@ -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);
};
}

View File

@ -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};

View File

@ -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.

View File

@ -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,
);
}
}