Skip to content

Symbol Aliases #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::fmt::Write;
use std::iter::Peekable;
use std::path::Path;
Expand Down Expand Up @@ -30,6 +31,7 @@ enum Def<'a> {
enum Symbol<'a> {
Single(char),
Multi(Vec<(&'a str, char)>),
MultiAlias(Vec<(Cow<'a, str>, char)>),
}

/// A single line during parsing.
Expand All @@ -41,6 +43,7 @@ enum Line<'a> {
ModuleEnd,
Symbol(&'a str, Option<char>),
Variant(&'a str, char),
Alias(&'a str, &'a str, &'a str, bool),
}

fn main() {
Expand Down Expand Up @@ -111,6 +114,23 @@ fn tokenize(line: &str) -> StrResult<Line> {
}
let c = decode_char(tail.ok_or("missing char")?)?;
Line::Variant(rest, c)
} else if let Some(mut value) = tail.and_then(|tail| tail.strip_prefix("@= ")) {
let alias = head;
validate_ident(alias)?;
let mut deep = false;
if let Some(v) = value.strip_suffix(".*") {
deep = true;
value = v;
}
let (head, rest) = value.split_once('.').unwrap_or((value, ""));
validate_ident(head)?;
if !rest.is_empty() {
for part in rest.split('.') {
validate_ident(part)?;
}
}

Line::Alias(alias, head, rest, deep)
} else {
validate_ident(head)?;
let c = tail.map(decode_char).transpose()?;
Expand Down Expand Up @@ -149,6 +169,7 @@ fn parse<'a>(
) -> StrResult<Vec<(&'a str, Binding<'a>)>> {
let mut defs = vec![];
let mut deprecation = None;
let mut aliases = vec![];
loop {
match p.next().transpose()? {
None | Some(Line::ModuleEnd) => {
Expand All @@ -158,6 +179,10 @@ fn parse<'a>(
break;
}
Some(Line::Deprecated(message)) => deprecation = Some(message),
Some(Line::Alias(alias, name, variant, deep)) => {
aliases.push((alias, name, variant, deep, deprecation));
deprecation = None;
}
Some(Line::Symbol(name, c)) => {
let mut variants = vec![];
while let Some(Line::Variant(name, c)) = p.peek().cloned().transpose()? {
Expand Down Expand Up @@ -192,6 +217,95 @@ fn parse<'a>(
other => return Err(format!("expected definition, found {other:?}")),
}
}
for (alias, name, variant, deep, deprecation) in aliases {
let aliased_symbol: &Symbol<'a> = defs
.iter()
.filter(|(n, _)| *n == name)
.find_map(|(_, b)| match &b.def {
Def::Symbol(s) => Some(s),
_ => None,
})
.ok_or_else(|| format!("alias to nonexistent symbol: {name}"))?;

match aliased_symbol {
&Symbol::Single(c) => {
if variant != "" {
return Err(format!(
"alias to nonexistent variant: {name}.{variant}"
));
}
defs.push((
alias,
Binding { def: Def::Symbol(Symbol::Single(c)), deprecation },
));
}
Symbol::MultiAlias(_) => {
return Err(format!("alias to alias: {name}.{variant}"));
}
Symbol::Multi(variants) => {
let variants: Vec<(Cow<'a, str>, char)> = variants
.iter()
.filter_map(|&(var, c)| {
if var == variant {
Some((Cow::Borrowed(""), c))
} else if deep {
var.strip_prefix(variant)?
.strip_prefix('.')
.map(|v| (Cow::Borrowed(v), c))
.or_else(|| {
// might just be in a different order

let mut alias_modifs = if variant.is_empty() {
vec![]
} else {
variant.split('.').collect()
};

let mut new_variant = Cow::Borrowed("");
for modif in var.split('.') {
if let Some(i) =
alias_modifs.iter().position(|m| *m == modif)
{
alias_modifs.swap_remove(i);
} else if new_variant.is_empty() {
new_variant = Cow::Borrowed(modif);
} else {
new_variant = Cow::Owned(format!(
"{new_variant}.{modif}"
));
}
}
alias_modifs.is_empty().then_some((new_variant, c))
})
} else {
None
}
})
.collect();
if variants.is_empty() {
return Err(format!(
"alias to nonexistent variant: {name}.{variant}"
));
}
if let [(ref s, c)] = variants[..] {
if s.is_empty() {
defs.push((
alias,
Binding { def: Def::Symbol(Symbol::Single(c)), deprecation },
));
continue;
}
}
defs.push((
alias,
Binding {
def: Def::Symbol(Symbol::MultiAlias(variants)),
deprecation,
},
));
}
}
}
Ok(defs)
}

Expand All @@ -211,6 +325,7 @@ fn encode(buf: &mut String, module: &Module) {
match symbol {
Symbol::Single(c) => write!(buf, "Single({c:?})").unwrap(),
Symbol::Multi(list) => write!(buf, "Multi(&{list:?})").unwrap(),
Symbol::MultiAlias(list) => write!(buf, "Multi(&{list:?})").unwrap(),
}
buf.push_str(")");
}
Expand Down
40 changes: 16 additions & 24 deletions src/modules/sym.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ breve ˘
caret ‸
caron ˇ
hat ^
diaer ¨
diaer @= dot.double
grave `
macron ¯
quote
Expand Down Expand Up @@ -365,8 +365,7 @@ succ ≻
.not ⊁
.ntilde ⋩
.tilde ≿
equiv ≡
.not ≢
equiv @= eq.triple.*
smt ⪪
.eq ⪬
lat ⪫
Expand All @@ -384,12 +383,7 @@ emptyset ∅
.bar ⦱
.circle ⦲
.rev ⦰
nothing ∅
.arrow.r ⦳
.arrow.l ⦴
.bar ⦱
.circle ⦲
.rev ⦰
nothing @= emptyset.*
without ∖
complement ∁
in ∈
Expand Down Expand Up @@ -447,11 +441,11 @@ infinity ∞
.bar ⧞
.incomplete ⧜
.tie ⧝
oo
diff
oo @= infinity
diff @= partial.*
partial ∂
gradient ∇
nabla
nabla @= gradient.*
sum ∑
.integral ⨋
product ∏
Expand Down Expand Up @@ -481,8 +475,8 @@ laplace ∆
forall ∀
exists ∃
.not ∄
top
bot
top @= tack.b
bot @= tack.t
not ¬
and ∧
.big ⋀
Expand All @@ -494,8 +488,7 @@ or ∨
.curly ⋎
.dot ⟇
.double ⩔
xor ⊕
.big ⨁
xor @= plus.circle.*
models ⊧
forces ⊩
.not ⊮
Expand All @@ -504,10 +497,9 @@ because ∵
qed ∎

// Function and category theory.
mapsto ↦
.long ⟼
compose ∘
convolve ∗
mapsto @= arrow.r.bar.*
compose @= circle.stroked.tiny
convolve @= ast.op
multimap ⊸
.double ⧟

Expand Down Expand Up @@ -999,13 +991,13 @@ Zeta Ζ
// from Letterlike Symbols.
// See https://github.com/typst/typst/pull/3375.
aleph א
alef א
alef @= aleph
beth ב
bet ב
bet @= beth
gimmel ג
gimel ג
gimel @= gimmel
daleth ד
dalet ד
dalet @= daleth
shin ש

// Double-struck.
Expand Down