summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-11-26 13:39:18 +0100
committerLaurenz <laurmaedje@gmail.com>2022-11-26 13:41:27 +0100
commit7af46fc025ee08eb78ae7f6898300083c886bf6f (patch)
tree5837d972961844650bc9668d8516d7b5239a8d18 /src/syntax
parent3cdd8bfa40fe5fdf0c676af905c3c2c1f614ef24 (diff)
Dynamic labels
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/ast.rs23
-rw-r--r--src/syntax/kind.rs2
-rw-r--r--src/syntax/parsing.rs5
-rw-r--r--src/syntax/tokens.rs18
4 files changed, 16 insertions, 32 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs
index 5b61af77..81ddd596 100644
--- a/src/syntax/ast.rs
+++ b/src/syntax/ast.rs
@@ -95,8 +95,6 @@ pub enum MarkupNode {
Raw(Raw),
/// A hyperlink: `https://typst.org`.
Link(Link),
- /// A label: `<label>`.
- Label(Label),
/// A reference: `@target`.
Ref(Ref),
/// A section heading: `= Introduction`.
@@ -126,7 +124,6 @@ impl AstNode for MarkupNode {
SyntaxKind::Emph => node.cast().map(Self::Emph),
SyntaxKind::Raw(_) => node.cast().map(Self::Raw),
SyntaxKind::Link(_) => node.cast().map(Self::Link),
- SyntaxKind::Label(_) => node.cast().map(Self::Label),
SyntaxKind::Ref(_) => node.cast().map(Self::Ref),
SyntaxKind::Heading => node.cast().map(Self::Heading),
SyntaxKind::ListItem => node.cast().map(Self::List),
@@ -149,7 +146,6 @@ impl AstNode for MarkupNode {
Self::Emph(v) => v.as_untyped(),
Self::Raw(v) => v.as_untyped(),
Self::Link(v) => v.as_untyped(),
- Self::Label(v) => v.as_untyped(),
Self::Ref(v) => v.as_untyped(),
Self::Heading(v) => v.as_untyped(),
Self::List(v) => v.as_untyped(),
@@ -314,21 +310,6 @@ impl Link {
}
node! {
- /// A label: `<label>`.
- Label
-}
-
-impl Label {
- /// Get the label.
- pub fn get(&self) -> &EcoString {
- match self.0.kind() {
- SyntaxKind::Label(v) => v,
- _ => panic!("label is of wrong kind"),
- }
- }
-}
-
-node! {
/// A reference: `@target`.
Ref
}
@@ -704,6 +685,7 @@ node! {
| SyntaxKind::Float(_)
| SyntaxKind::Numeric(_, _)
| SyntaxKind::Str(_)
+ | SyntaxKind::Label(_)
}
impl Lit {
@@ -717,6 +699,7 @@ impl Lit {
SyntaxKind::Float(v) => LitKind::Float(v),
SyntaxKind::Numeric(v, unit) => LitKind::Numeric(v, unit),
SyntaxKind::Str(ref v) => LitKind::Str(v.clone()),
+ SyntaxKind::Label(ref v) => LitKind::Label(v.clone()),
_ => panic!("literal is of wrong kind"),
}
}
@@ -739,6 +722,8 @@ pub enum LitKind {
Numeric(f64, Unit),
/// A quoted string: `"..."`.
Str(EcoString),
+ /// A label: `<intro>`.
+ Label(EcoString),
}
node! {
diff --git a/src/syntax/kind.rs b/src/syntax/kind.rs
index 58e2b915..b7ee6a79 100644
--- a/src/syntax/kind.rs
+++ b/src/syntax/kind.rs
@@ -153,7 +153,7 @@ pub enum SyntaxKind {
Raw(Arc<RawFields>),
/// A hyperlink: `https://typst.org`.
Link(EcoString),
- /// A label: `<label>`.
+ /// A label: `<intro>`.
Label(EcoString),
/// A reference: `@target`.
Ref(EcoString),
diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs
index 9529d1d1..1678eb01 100644
--- a/src/syntax/parsing.rs
+++ b/src/syntax/parsing.rs
@@ -232,7 +232,6 @@ fn markup_node(p: &mut Parser, at_start: &mut bool) {
| SyntaxKind::Shorthand(_)
| SyntaxKind::Link(_)
| SyntaxKind::Raw(_)
- | SyntaxKind::Label(_)
| SyntaxKind::Ref(_) => p.eat(),
// Math.
@@ -257,6 +256,7 @@ fn markup_node(p: &mut Parser, at_start: &mut bool) {
// Hashtag + keyword / identifier.
SyntaxKind::Ident(_)
+ | SyntaxKind::Label(_)
| SyntaxKind::Let
| SyntaxKind::Set
| SyntaxKind::Show
@@ -617,7 +617,8 @@ fn literal(p: &mut Parser) -> bool {
| SyntaxKind::Float(_)
| SyntaxKind::Bool(_)
| SyntaxKind::Numeric(_, _)
- | SyntaxKind::Str(_),
+ | SyntaxKind::Str(_)
+ | SyntaxKind::Label(_),
) => {
p.eat();
true
diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs
index 4b86c89b..e0ef2fa1 100644
--- a/src/syntax/tokens.rs
+++ b/src/syntax/tokens.rs
@@ -207,8 +207,8 @@ impl<'s> Tokens<'s> {
}
'`' => self.raw(),
c if c.is_ascii_digit() => self.numbering(start),
- '<' => self.label(),
- '@' => self.reference(start),
+ '<' if self.s.at(is_id_continue) => self.label(),
+ '@' if self.s.at(is_id_continue) => self.reference(),
// Escape sequences.
'\\' => self.backslash(),
@@ -417,13 +417,8 @@ impl<'s> Tokens<'s> {
}
}
- fn reference(&mut self, start: usize) -> SyntaxKind {
- let label = self.s.eat_while(is_id_continue);
- if !label.is_empty() {
- SyntaxKind::Ref(label.into())
- } else {
- self.text(start)
- }
+ fn reference(&mut self) -> SyntaxKind {
+ SyntaxKind::Ref(self.s.eat_while(is_id_continue).into())
}
fn math(&mut self, start: usize, c: char) -> SyntaxKind {
@@ -475,6 +470,9 @@ impl<'s> Tokens<'s> {
'(' => SyntaxKind::LeftParen,
')' => SyntaxKind::RightParen,
+ // Labels.
+ '<' if self.s.at(is_id_continue) => self.label(),
+
// Two-char operators.
'=' if self.s.eat_if('=') => SyntaxKind::EqEq,
'!' if self.s.eat_if('=') => SyntaxKind::ExclEq,
@@ -954,7 +952,7 @@ mod tests {
t!(Code: "=" => Eq);
t!(Code: "==" => EqEq);
t!(Code: "!=" => ExclEq);
- t!(Code: "<" => Lt);
+ t!(Code[" /"]: "<" => Lt);
t!(Code: "<=" => LtEq);
t!(Code: ">" => Gt);
t!(Code: ">=" => GtEq);