summaryrefslogtreecommitdiff
path: root/crates/typst-macros
diff options
context:
space:
mode:
authorMalo <57839069+MDLC01@users.noreply.github.com>2024-11-17 20:08:23 +0100
committerGitHub <noreply@github.com>2024-11-17 19:08:23 +0000
commit5c37a1cfea2a08b08f4af34ba867748fd90c687e (patch)
treed5dec4ae7ff0b3238d84224a325eddc1a1427dee /crates/typst-macros
parent5672cc2a29379f7743f66c46a7192d698a733047 (diff)
Use `codex` for symbols (#5421)
Diffstat (limited to 'crates/typst-macros')
-rw-r--r--crates/typst-macros/src/lib.rs47
-rw-r--r--crates/typst-macros/src/symbols.rs129
2 files changed, 0 insertions, 176 deletions
diff --git a/crates/typst-macros/src/lib.rs b/crates/typst-macros/src/lib.rs
index acc5e603..e1c3c13a 100644
--- a/crates/typst-macros/src/lib.rs
+++ b/crates/typst-macros/src/lib.rs
@@ -9,7 +9,6 @@ mod category;
mod elem;
mod func;
mod scope;
-mod symbols;
mod time;
mod ty;
@@ -338,52 +337,6 @@ pub fn derive_cast(item: BoundaryStream) -> BoundaryStream {
.into()
}
-/// Defines a list of `Symbol`s.
-///
-/// The `#[call(path)]` attribute can be used to specify a function to call when
-/// the symbol is invoked. The function must be `NativeFunc`.
-///
-/// ```ignore
-/// const EMOJI: &[(&str, Symbol)] = symbols! {
-/// // A plain symbol without modifiers.
-/// abacus: '๐Ÿงฎ',
-///
-/// // A symbol with a modifierless default and one modifier.
-/// alien: ['๐Ÿ‘ฝ', monster: '๐Ÿ‘พ'],
-///
-/// // A symbol where each variant has a modifier. The first one will be
-/// // the default.
-/// clock: [one: '๐Ÿ•', two: '๐Ÿ•‘', ...],
-///
-/// // A callable symbol without modifiers.
-/// breve: #[call(crate::math::breve)] 'ห˜',
-///
-/// // A callable symbol with a modifierless default and one modifier.
-/// acute: [
-/// #[call(crate::math::acute)] 'ยด',
-/// double: 'ห',
-/// ],
-///
-/// // A callable symbol where each variant has a modifier.
-/// arrow: [
-/// #[call(crate::math::arrow)] r: 'โ†’',
-/// r.long.bar: 'โŸผ',
-/// #[call(crate::math::arrow_l)] l: 'โ†',
-/// l.long.bar: 'โŸป',
-/// ],
-/// }
-/// ```
-///
-/// _Note:_ While this could use `macro_rules!` instead of a proc-macro, it was
-/// horribly slow in rust-analyzer. The underlying cause might be
-/// [this issue](https://github.com/rust-lang/rust-analyzer/issues/11108).
-#[proc_macro]
-pub fn symbols(stream: BoundaryStream) -> BoundaryStream {
- symbols::symbols(stream.into())
- .unwrap_or_else(|err| err.to_compile_error())
- .into()
-}
-
/// Times function invocations.
///
/// When tracing is enabled in the typst-cli, this macro will record the
diff --git a/crates/typst-macros/src/symbols.rs b/crates/typst-macros/src/symbols.rs
deleted file mode 100644
index 9917f436..00000000
--- a/crates/typst-macros/src/symbols.rs
+++ /dev/null
@@ -1,129 +0,0 @@
-use proc_macro2::TokenStream;
-use quote::quote;
-use syn::ext::IdentExt;
-use syn::parse::{Parse, ParseStream, Parser};
-use syn::punctuated::Punctuated;
-use syn::{Ident, LitChar, Path, Result, Token};
-
-use crate::util::foundations;
-
-/// Expand the `symbols!` macro.
-pub fn symbols(stream: TokenStream) -> Result<TokenStream> {
- let list: Punctuated<Symbol, Token![,]> =
- Punctuated::parse_terminated.parse2(stream)?;
- let pairs = list.iter().map(|symbol| {
- let name = symbol.name.to_string();
- let kind = match &symbol.kind {
- Kind::Single(c, h) => {
- let symbol = construct_sym_char(c, h);
- quote! { #foundations::Symbol::single(#symbol), }
- }
- Kind::Multiple(variants) => {
- let variants = variants.iter().map(|variant| {
- let name = &variant.name;
- let c = &variant.c;
- let symbol = construct_sym_char(c, &variant.handler);
- quote! { (#name, #symbol) }
- });
- quote! {
- #foundations::Symbol::list(&[#(#variants),*])
- }
- }
- };
- quote! { (#name, #kind) }
- });
- Ok(quote! { &[#(#pairs),*] })
-}
-
-fn construct_sym_char(ch: &LitChar, handler: &Handler) -> TokenStream {
- match &handler.0 {
- None => quote! { #foundations::SymChar::pure(#ch), },
- Some(path) => quote! {
- #foundations::SymChar::with_func(
- #ch,
- <#path as ::typst_library::foundations::NativeFunc>::func,
- ),
- },
- }
-}
-
-struct Symbol {
- name: syn::Ident,
- kind: Kind,
-}
-
-enum Kind {
- Single(syn::LitChar, Handler),
- Multiple(Punctuated<Variant, Token![,]>),
-}
-
-struct Variant {
- name: String,
- c: syn::LitChar,
- handler: Handler,
-}
-
-struct Handler(Option<Path>);
-
-impl Parse for Symbol {
- fn parse(input: ParseStream) -> Result<Self> {
- let name = input.call(Ident::parse_any)?;
- input.parse::<Token![:]>()?;
- let kind = input.parse()?;
- Ok(Self { name, kind })
- }
-}
-
-impl Parse for Kind {
- fn parse(input: ParseStream) -> Result<Self> {
- let handler = input.parse::<Handler>()?;
- if input.peek(syn::LitChar) {
- Ok(Self::Single(input.parse()?, handler))
- } else {
- if handler.0.is_some() {
- return Err(input.error("unexpected handler"));
- }
- let content;
- syn::bracketed!(content in input);
- Ok(Self::Multiple(Punctuated::parse_terminated(&content)?))
- }
- }
-}
-
-impl Parse for Variant {
- fn parse(input: ParseStream) -> Result<Self> {
- let mut name = String::new();
- let handler = input.parse::<Handler>()?;
- if input.peek(syn::Ident::peek_any) {
- name.push_str(&input.call(Ident::parse_any)?.to_string());
- while input.peek(Token![.]) {
- input.parse::<Token![.]>()?;
- name.push('.');
- name.push_str(&input.call(Ident::parse_any)?.to_string());
- }
- input.parse::<Token![:]>()?;
- }
- let c = input.parse()?;
- Ok(Self { name, c, handler })
- }
-}
-
-impl Parse for Handler {
- fn parse(input: ParseStream) -> Result<Self> {
- let Ok(attrs) = input.call(syn::Attribute::parse_outer) else {
- return Ok(Self(None));
- };
- let handler = attrs
- .iter()
- .find_map(|attr| {
- if attr.path().is_ident("call") {
- if let Ok(path) = attr.parse_args::<Path>() {
- return Some(Self(Some(path)));
- }
- }
- None
- })
- .unwrap_or(Self(None));
- Ok(handler)
- }
-}