summaryrefslogtreecommitdiff
path: root/crates/typst-syntax
diff options
context:
space:
mode:
authorMatt Fellenz <matt@felle.nz>2023-11-02 09:08:08 -0700
committerGitHub <noreply@github.com>2023-11-02 17:08:08 +0100
commit8fd546760c7c425398f0114997c8085a481d8d2a (patch)
tree9d0a58cd50494145af9d4b322f6477fddd917959 /crates/typst-syntax
parentb716700b61b229e2b703b892d3e047d84633d982 (diff)
Implement dict key interpolation (#2559)
Diffstat (limited to 'crates/typst-syntax')
-rw-r--r--crates/typst-syntax/src/ast.rs7
-rw-r--r--crates/typst-syntax/src/parser.rs22
2 files changed, 14 insertions, 15 deletions
diff --git a/crates/typst-syntax/src/ast.rs b/crates/typst-syntax/src/ast.rs
index f411326f..8da045e6 100644
--- a/crates/typst-syntax/src/ast.rs
+++ b/crates/typst-syntax/src/ast.rs
@@ -1270,11 +1270,8 @@ node! {
impl<'a> Keyed<'a> {
/// The key: `"spacy key"`.
- pub fn key(self) -> Str<'a> {
- self.0
- .children()
- .find_map(|node| node.cast::<Str>())
- .unwrap_or_default()
+ pub fn key(self) -> Expr<'a> {
+ self.0.cast_first_match().unwrap_or_default()
}
/// The right-hand side of the pair: `true`.
diff --git a/crates/typst-syntax/src/parser.rs b/crates/typst-syntax/src/parser.rs
index 0cc733e6..56fe3c9b 100644
--- a/crates/typst-syntax/src/parser.rs
+++ b/crates/typst-syntax/src/parser.rs
@@ -943,19 +943,18 @@ fn item(p: &mut Parser, keyed: bool) -> SyntaxKind {
let kind = match p.node(m).map(SyntaxNode::kind) {
Some(SyntaxKind::Ident) => SyntaxKind::Named,
- Some(SyntaxKind::Str) if keyed => SyntaxKind::Keyed,
+ Some(_) if keyed => SyntaxKind::Keyed,
_ => {
for child in p.post_process(m) {
if child.kind() == SyntaxKind::Colon {
break;
}
- let mut message = EcoString::from("expected identifier");
- if keyed {
- message.push_str(" or string");
- }
- message.push_str(", found ");
- message.push_str(child.kind().name());
+ let expected = if keyed { "expression" } else { "identifier" };
+ let message = eco_format!(
+ "expected {expected}, found {found}",
+ found = child.kind().name(),
+ );
child.convert_to_error(message);
}
SyntaxKind::Named
@@ -1235,9 +1234,12 @@ fn validate_dict<'a>(children: impl Iterator<Item = &'a mut SyntaxNode>) {
match child.kind() {
SyntaxKind::Named | SyntaxKind::Keyed => {
let Some(first) = child.children_mut().first_mut() else { continue };
- let key = match first.cast::<ast::Str>() {
- Some(str) => str.get(),
- None => first.text().clone(),
+ let key = if let Some(str) = first.cast::<ast::Str>() {
+ str.get()
+ } else if let Some(ident) = first.cast::<ast::Ident>() {
+ ident.get().clone()
+ } else {
+ continue;
};
if !used.insert(key.clone()) {