diff options
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/ast.rs | 37 | ||||
| -rw-r--r-- | src/syntax/highlight.rs | 1 | ||||
| -rw-r--r-- | src/syntax/mod.rs | 6 |
3 files changed, 38 insertions, 6 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index fa00fe4b..1add2fd6 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -472,7 +472,7 @@ impl ArrayExpr { /// An item in an array expresssion. #[derive(Debug, Clone, PartialEq, Hash)] pub enum ArrayItem { - /// A simple value: `12`. + /// A bare value: `12`. Pos(Expr), /// A spreaded value: `..things`. Spread(Expr), @@ -509,8 +509,10 @@ impl DictExpr { /// An item in an dictionary expresssion. #[derive(Debug, Clone, PartialEq, Hash)] pub enum DictItem { - /// A simple named pair: `12`. + /// A named pair: `thickness: 3pt`. Named(Named), + /// A keyed pair: `"spaced key": true`. + Keyed(Keyed), /// A spreaded value: `..things`. Spread(Expr), } @@ -519,6 +521,7 @@ impl TypedNode for DictItem { fn from_red(node: RedRef) -> Option<Self> { match node.kind() { NodeKind::Named => node.cast().map(Self::Named), + NodeKind::Keyed => node.cast().map(Self::Keyed), NodeKind::Spread => node.cast_first_child().map(Self::Spread), _ => None, } @@ -527,29 +530,53 @@ impl TypedNode for DictItem { fn as_red(&self) -> RedRef<'_> { match self { Self::Named(v) => v.as_red(), + Self::Keyed(v) => v.as_red(), Self::Spread(v) => v.as_red(), } } } node! { - /// A pair of a name and an expression: `pattern: dashed`. + /// A pair of a name and an expression: `thickness: 3pt`. Named } impl Named { - /// The name: `pattern`. + /// The name: `thickness`. pub fn name(&self) -> Ident { self.0.cast_first_child().expect("named pair is missing name") } - /// The right-hand side of the pair: `dashed`. + /// The right-hand side of the pair: `3pt`. pub fn expr(&self) -> Expr { self.0.cast_last_child().expect("named pair is missing expression") } } node! { + /// A pair of a string key and an expression: `"spaced key": true`. + Keyed +} + +impl Keyed { + /// The key: `"spaced key"`. + pub fn key(&self) -> EcoString { + self.0 + .children() + .find_map(|node| match node.kind() { + NodeKind::Str(key) => Some(key.clone()), + _ => None, + }) + .expect("keyed pair is missing key") + } + + /// The right-hand side of the pair: `true`. + pub fn expr(&self) -> Expr { + self.0.cast_last_child().expect("keyed pair is missing expression") + } +} + +node! { /// A unary operation: `-x`. UnaryExpr: UnaryExpr } diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs index dae379ac..06e88691 100644 --- a/src/syntax/highlight.rs +++ b/src/syntax/highlight.rs @@ -215,6 +215,7 @@ impl Category { NodeKind::ArrayExpr => None, NodeKind::DictExpr => None, NodeKind::Named => None, + NodeKind::Keyed => None, NodeKind::UnaryExpr => None, NodeKind::BinaryExpr => None, NodeKind::FieldAccess => None, diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index d21597ff..09d7265d 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -648,6 +648,8 @@ pub enum NodeKind { DictExpr, /// A named pair: `thickness: 3pt`. Named, + /// A keyed pair: `"spaced key": true`. + Keyed, /// A unary operation: `-x`. UnaryExpr, /// A binary operation: `a + b`. @@ -896,7 +898,8 @@ impl NodeKind { Self::GroupExpr => "group", Self::ArrayExpr => "array", Self::DictExpr => "dictionary", - Self::Named => "named argument", + Self::Named => "named pair", + Self::Keyed => "keyed pair", Self::UnaryExpr => "unary expression", Self::BinaryExpr => "binary expression", Self::FieldAccess => "field access", @@ -1021,6 +1024,7 @@ impl Hash for NodeKind { Self::ArrayExpr => {} Self::DictExpr => {} Self::Named => {} + Self::Keyed => {} Self::UnaryExpr => {} Self::BinaryExpr => {} Self::FieldAccess => {} |
