summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstuebinm2024-04-12 17:19:22 +0200
committerstuebinm2024-04-12 17:19:22 +0200
commitdcd298d18fa72c7dd37085f3d5b93c518a6afab9 (patch)
treecb78982edf90cf84f3c26792cc183d94d18eba4a
parent9e32c201789e4d0a119c4d486138f2f5edd1e168 (diff)
matching on elements by relative role
just a skeleton to see if this works
-rw-r--r--src/pipeline.rs34
1 files changed, 33 insertions, 1 deletions
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<NixSyntaxRole> {
+ 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 {