mirror of
https://github.com/typst/typst
synced 2025-08-12 14:17:55 +08:00
Math shorthands
This commit is contained in:
parent
3ecb0c754b
commit
b2572f9d48
@ -424,6 +424,7 @@ impl Eval for ast::MathNode {
|
|||||||
Self::Space(_) => (vm.items.space)(),
|
Self::Space(_) => (vm.items.space)(),
|
||||||
Self::Linebreak(v) => v.eval(vm)?,
|
Self::Linebreak(v) => v.eval(vm)?,
|
||||||
Self::Escape(v) => (vm.items.math_atom)(v.get().into()),
|
Self::Escape(v) => (vm.items.math_atom)(v.get().into()),
|
||||||
|
Self::Shorthand(v) => (vm.items.math_atom)(v.get().into()),
|
||||||
Self::Atom(v) => v.eval(vm)?,
|
Self::Atom(v) => v.eval(vm)?,
|
||||||
Self::Symbol(v) => (vm.items.symbol)(v.get().clone()),
|
Self::Symbol(v) => (vm.items.symbol)(v.get().clone()),
|
||||||
Self::Script(v) => v.eval(vm)?,
|
Self::Script(v) => v.eval(vm)?,
|
||||||
|
@ -441,6 +441,9 @@ pub enum MathNode {
|
|||||||
Linebreak(Linebreak),
|
Linebreak(Linebreak),
|
||||||
/// An escape sequence: `\#`, `\u{1F5FA}`.
|
/// An escape sequence: `\#`, `\u{1F5FA}`.
|
||||||
Escape(Escape),
|
Escape(Escape),
|
||||||
|
/// A shorthand for a unicode codepoint. For example, `->` for a right
|
||||||
|
/// arrow.
|
||||||
|
Shorthand(Shorthand),
|
||||||
/// An atom: `x`, `+`, `12`.
|
/// An atom: `x`, `+`, `12`.
|
||||||
Atom(Atom),
|
Atom(Atom),
|
||||||
/// Symbol notation: `:arrow:l:` or `arrow:l`. Notations without any colons
|
/// Symbol notation: `:arrow:l:` or `arrow:l`. Notations without any colons
|
||||||
@ -464,6 +467,7 @@ impl AstNode for MathNode {
|
|||||||
SyntaxKind::Space { .. } => node.cast().map(Self::Space),
|
SyntaxKind::Space { .. } => node.cast().map(Self::Space),
|
||||||
SyntaxKind::Linebreak => node.cast().map(Self::Linebreak),
|
SyntaxKind::Linebreak => node.cast().map(Self::Linebreak),
|
||||||
SyntaxKind::Escape(_) => node.cast().map(Self::Escape),
|
SyntaxKind::Escape(_) => node.cast().map(Self::Escape),
|
||||||
|
SyntaxKind::Shorthand(_) => node.cast().map(Self::Shorthand),
|
||||||
SyntaxKind::Atom(_) => node.cast().map(Self::Atom),
|
SyntaxKind::Atom(_) => node.cast().map(Self::Atom),
|
||||||
SyntaxKind::Symbol(_) => node.cast().map(Self::Symbol),
|
SyntaxKind::Symbol(_) => node.cast().map(Self::Symbol),
|
||||||
SyntaxKind::Script => node.cast().map(Self::Script),
|
SyntaxKind::Script => node.cast().map(Self::Script),
|
||||||
@ -479,6 +483,7 @@ impl AstNode for MathNode {
|
|||||||
Self::Space(v) => v.as_untyped(),
|
Self::Space(v) => v.as_untyped(),
|
||||||
Self::Linebreak(v) => v.as_untyped(),
|
Self::Linebreak(v) => v.as_untyped(),
|
||||||
Self::Escape(v) => v.as_untyped(),
|
Self::Escape(v) => v.as_untyped(),
|
||||||
|
Self::Shorthand(v) => v.as_untyped(),
|
||||||
Self::Atom(v) => v.as_untyped(),
|
Self::Atom(v) => v.as_untyped(),
|
||||||
Self::Symbol(v) => v.as_untyped(),
|
Self::Symbol(v) => v.as_untyped(),
|
||||||
Self::Script(v) => v.as_untyped(),
|
Self::Script(v) => v.as_untyped(),
|
||||||
|
@ -442,6 +442,7 @@ fn math_primary(p: &mut Parser) {
|
|||||||
| SyntaxKind::Linebreak
|
| SyntaxKind::Linebreak
|
||||||
| SyntaxKind::Escape(_)
|
| SyntaxKind::Escape(_)
|
||||||
| SyntaxKind::Str(_)
|
| SyntaxKind::Str(_)
|
||||||
|
| SyntaxKind::Shorthand(_)
|
||||||
| SyntaxKind::Symbol(_) => p.eat(),
|
| SyntaxKind::Symbol(_) => p.eat(),
|
||||||
|
|
||||||
// Atoms.
|
// Atoms.
|
||||||
|
@ -439,6 +439,18 @@ impl Tokens<'_> {
|
|||||||
impl Tokens<'_> {
|
impl Tokens<'_> {
|
||||||
fn math(&mut self, start: usize, c: char) -> SyntaxKind {
|
fn math(&mut self, start: usize, c: char) -> SyntaxKind {
|
||||||
match c {
|
match c {
|
||||||
|
// Symbol shorthands.
|
||||||
|
'|' if self.s.eat_if("->") => SyntaxKind::Shorthand('\u{21A6}'),
|
||||||
|
'<' if self.s.eat_if("->") => SyntaxKind::Shorthand('\u{2194}'),
|
||||||
|
'<' if self.s.eat_if("=>") => SyntaxKind::Shorthand('\u{21D4}'),
|
||||||
|
'!' if self.s.eat_if('=') => SyntaxKind::Shorthand('\u{2260}'),
|
||||||
|
'<' if self.s.eat_if('=') => SyntaxKind::Shorthand('\u{2264}'),
|
||||||
|
'>' if self.s.eat_if('=') => SyntaxKind::Shorthand('\u{2265}'),
|
||||||
|
'<' if self.s.eat_if('-') => SyntaxKind::Shorthand('\u{2190}'),
|
||||||
|
'-' if self.s.eat_if('>') => SyntaxKind::Shorthand('\u{2192}'),
|
||||||
|
'=' if self.s.eat_if('>') => SyntaxKind::Shorthand('\u{21D2}'),
|
||||||
|
':' if self.s.eat_if('=') => SyntaxKind::Shorthand('\u{2254}'),
|
||||||
|
|
||||||
// Multi-char things.
|
// Multi-char things.
|
||||||
'#' => self.hash(start),
|
'#' => self.hash(start),
|
||||||
|
|
||||||
|
BIN
tests/ref/math/shorthand.png
Normal file
BIN
tests/ref/math/shorthand.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
4
tests/typ/math/shorthand.typ
Normal file
4
tests/typ/math/shorthand.typ
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
// Test math shorthands.
|
||||||
|
|
||||||
|
---
|
||||||
|
$ f : NN <=> RR, n |-> sqrt(n) $
|
Loading…
x
Reference in New Issue
Block a user