From dcd298d18fa72c7dd37085f3d5b93c518a6afab9 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Fri, 12 Apr 2024 17:19:22 +0200 Subject: matching on elements by relative role just a skeleton to see if this works --- src/pipeline.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/pipeline.rs b/src/pipeline.rs index cba046f..cce3e31 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -78,12 +78,33 @@ impl Root { } } +// address nodes by their role relative to their parent +enum NixSyntaxRole { + Argument, + Function, + Attribute, + // TODO +} + +impl NixSyntaxRole { + fn from_str(from: &str) -> Option { + use NixSyntaxRole::*; + Some(match from { + "Argument" => Argument, + "Function" => Function, + "Attribute" => Attribute, + _ => return None + }) + } +} + enum Op { Down, DownRecursive, Up, UpRecursive, NixSyntaxNode(rnix::SyntaxKind), + NixSyntaxRole(NixSyntaxRole), Named(String) } @@ -100,7 +121,11 @@ impl Atom { "Inherit" => Op::NixSyntaxNode(rnix::SyntaxKind::NODE_INHERIT), "String" => Op::NixSyntaxNode(rnix::SyntaxKind::NODE_STRING), // TODO other syntax nodes - name => Op::Named(name.to_owned()), + name => if let Some(role) = NixSyntaxRole::from_str(name) { + Op::NixSyntaxRole(role) + } else { + Op::Named(name.to_owned()) + }, }; Some(op) } @@ -129,6 +154,13 @@ impl Atom { Some(Op::UpRecursive) => Box::new(acc.map(|s| s.ancestors()).flatten()), // TODO: how to select roles relative to previous node? Some(Op::NixSyntaxNode(kind)) => Box::new(acc.filter(move |s| s.kind() == kind)), + Some(Op::NixSyntaxRole(role)) => {use NixSyntaxRole::*; match role { + Argument => Box::new(acc.filter_map(move |s| match_ast! { match s { + ast::Apply(value) => value.argument().map(|s| s.syntax().to_owned()), + _ => None + }})), + _ => todo!() + }} Some(Op::Named(name)) => Box::new(acc .filter(move |node| match_ast! { match node { -- cgit v1.2.3