diff options
| author | +merlan #flirora <uruwi@protonmail.com> | 2024-06-11 05:08:30 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-11 09:08:30 +0000 |
| commit | 20475ab0bf4f28511064195b71890a3cf5d188a1 (patch) | |
| tree | 6d6e4e69f306e6df5779b5ab38d031a700e27784 /crates/typst-syntax | |
| parent | 7fa86eed0eca9b529d71d4006f389a753467e54a (diff) | |
Add hint when string is used in place of label (#4330)
Diffstat (limited to 'crates/typst-syntax')
| -rw-r--r-- | crates/typst-syntax/src/lexer.rs | 15 | ||||
| -rw-r--r-- | crates/typst-syntax/src/lib.rs | 3 |
2 files changed, 15 insertions, 3 deletions
diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs index 6719deaf..3f409b2d 100644 --- a/crates/typst-syntax/src/lexer.rs +++ b/crates/typst-syntax/src/lexer.rs @@ -408,7 +408,7 @@ impl Lexer<'_> { } fn ref_marker(&mut self) -> SyntaxKind { - self.s.eat_while(|c| is_id_continue(c) || matches!(c, ':' | '.')); + self.s.eat_while(is_valid_in_label_literal); // Don't include the trailing characters likely to be part of text. while matches!(self.s.scout(-1), Some('.' | ':')) { @@ -419,7 +419,7 @@ impl Lexer<'_> { } fn label(&mut self) -> SyntaxKind { - let label = self.s.eat_while(|c| is_id_continue(c) || matches!(c, ':' | '.')); + let label = self.s.eat_while(is_valid_in_label_literal); if label.is_empty() { return self.error("label cannot be empty"); } @@ -918,3 +918,14 @@ fn is_math_id_start(c: char) -> bool { fn is_math_id_continue(c: char) -> bool { is_xid_continue(c) && c != '_' } + +/// Whether a character can be part of a label literal's name. +#[inline] +fn is_valid_in_label_literal(c: char) -> bool { + is_id_continue(c) || matches!(c, ':' | '.') +} + +/// Returns true if this string is valid in a label literal. +pub fn is_valid_label_literal_id(id: &str) -> bool { + !id.is_empty() && id.chars().all(is_valid_in_label_literal) +} diff --git a/crates/typst-syntax/src/lib.rs b/crates/typst-syntax/src/lib.rs index 6a6063d2..5e7b710f 100644 --- a/crates/typst-syntax/src/lib.rs +++ b/crates/typst-syntax/src/lib.rs @@ -19,7 +19,8 @@ pub use self::file::FileId; pub use self::highlight::{highlight, highlight_html, Tag}; pub use self::kind::SyntaxKind; pub use self::lexer::{ - is_id_continue, is_id_start, is_ident, is_newline, link_prefix, split_newlines, + is_id_continue, is_id_start, is_ident, is_newline, is_valid_label_literal_id, + link_prefix, split_newlines, }; pub use self::node::{LinkedChildren, LinkedNode, Side, SyntaxError, SyntaxNode}; pub use self::parser::{parse, parse_code, parse_math}; |
