diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-04-11 15:52:57 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-04-11 15:52:57 +0200 |
| commit | 938b0af889b8fbe6265695b1b8e54aee338ba87f (patch) | |
| tree | 0121d733a556576d147742d460fe4df0d3c8cb1d /src/syntax | |
| parent | 790bd536eba76a2a48d61ea6b1bde78cde3d31f3 (diff) | |
Spreading into arrays and dictionaries
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/ast.rs | 78 |
1 files changed, 59 insertions, 19 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index 087b8d77..1318852d 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -447,11 +447,36 @@ node! { impl ArrayExpr { /// The array items. - pub fn items(&self) -> impl Iterator<Item = Expr> + '_ { + pub fn items(&self) -> impl Iterator<Item = ArrayItem> + '_ { self.0.children().filter_map(RedRef::cast) } } +/// An item in an array expresssion. +#[derive(Debug, Clone, PartialEq, Hash)] +pub enum ArrayItem { + /// A simple value: `12`. + Pos(Expr), + /// A spreaded value: `..things`. + Spread(Expr), +} + +impl TypedNode for ArrayItem { + fn from_red(node: RedRef) -> Option<Self> { + match node.kind() { + NodeKind::Spread => node.cast_first_child().map(Self::Spread), + _ => node.cast().map(Self::Pos), + } + } + + fn as_red(&self) -> RedRef<'_> { + match self { + Self::Pos(v) => v.as_red(), + Self::Spread(v) => v.as_red(), + } + } +} + node! { /// A dictionary expression: `(thickness: 3pt, pattern: dashed)`. DictExpr: DictExpr @@ -459,11 +484,37 @@ node! { impl DictExpr { /// The named dictionary items. - pub fn items(&self) -> impl Iterator<Item = Named> + '_ { + pub fn items(&self) -> impl Iterator<Item = DictItem> + '_ { self.0.children().filter_map(RedRef::cast) } } +/// An item in an dictionary expresssion. +#[derive(Debug, Clone, PartialEq, Hash)] +pub enum DictItem { + /// A simple named pair: `12`. + Named(Named), + /// A spreaded value: `..things`. + Spread(Expr), +} + +impl TypedNode for DictItem { + fn from_red(node: RedRef) -> Option<Self> { + match node.kind() { + NodeKind::Named => node.cast().map(Self::Named), + NodeKind::Spread => node.cast_first_child().map(Self::Spread), + _ => None, + } + } + + fn as_red(&self) -> RedRef<'_> { + match self { + Self::Named(v) => v.as_red(), + Self::Spread(v) => v.as_red(), + } + } +} + node! { /// A pair of a name and an expression: `pattern: dashed`. Named @@ -801,9 +852,9 @@ pub enum CallArg { impl TypedNode for CallArg { fn from_red(node: RedRef) -> Option<Self> { match node.kind() { - NodeKind::Named => node.cast().map(CallArg::Named), - NodeKind::Spread => node.cast_first_child().map(CallArg::Spread), - _ => node.cast().map(CallArg::Pos), + NodeKind::Named => node.cast().map(Self::Named), + NodeKind::Spread => node.cast_first_child().map(Self::Spread), + _ => node.cast().map(Self::Pos), } } @@ -816,17 +867,6 @@ impl TypedNode for CallArg { } } -impl CallArg { - /// The name of this argument. - pub fn span(&self) -> Span { - match self { - Self::Pos(expr) => expr.span(), - Self::Named(named) => named.span(), - Self::Spread(expr) => expr.span(), - } - } -} - node! { /// A closure expression: `(x, y) => z`. ClosureExpr: ClosureExpr @@ -870,9 +910,9 @@ pub enum ClosureParam { impl TypedNode for ClosureParam { fn from_red(node: RedRef) -> Option<Self> { match node.kind() { - NodeKind::Ident(_) => node.cast().map(ClosureParam::Pos), - NodeKind::Named => node.cast().map(ClosureParam::Named), - NodeKind::Spread => node.cast_first_child().map(ClosureParam::Sink), + NodeKind::Ident(_) => node.cast().map(Self::Pos), + NodeKind::Named => node.cast().map(Self::Named), + NodeKind::Spread => node.cast_first_child().map(Self::Sink), _ => None, } } |
