summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorstuebinm2024-04-16 18:51:24 +0200
committerstuebinm2024-04-16 18:51:24 +0200
commit238dabec513eac8af699756281d5aec12720686c (patch)
tree91db54c21027c96d73add8e4702327ac1a812293 /src/main.rs
parenta0459645638fe1397aa4f5e01a8f9093911ccf6c (diff)
add an actual AST for the query language
this incidentally also moves a lot of the parsing logic out of piplines.rs and instead keeps it in queries.rs, where it should probably be anyways, so the pipeline module can focus on just … well, applying the filter pipeline
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index 1b0ce7d..54f17a5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,6 @@
use std::{path::PathBuf, fs, process::exit};
-use pipeline::{Change, ChangeKind};
-use rnix::SyntaxNode;
-use rowan::{ast::AstNode, TextSize, NodeOrToken, TextRange};
+use pipeline::Change;
+use rowan::{ast::AstNode, NodeOrToken};
use clap::{arg, Parser};
#[allow(dead_code)]
@@ -50,10 +49,18 @@ fn main() {
exit(1);
}
+ let query = match parse.to_query() {
+ Err(es) => {
+ eprintln!("{}", es.join("\n"));
+ exit(1);
+ },
+ Ok(query) => query
+ };
+
// println!("{nexp:#?}");
if args.batchmode {
- batchmode::batchmode(args.path, parse, args.debug);
+ batchmode::batchmode(args.path, query, args.debug);
} else {
let (content, nexp) = match parse_nexp(&args.path[0]) {
Err(e) => {
@@ -63,7 +70,7 @@ fn main() {
Ok(exp) => exp
};
- let (changes, results) = parse.apply(&content, nexp.syntax().clone()).unwrap();
+ let (changes, results) = query.apply(&content, nexp.syntax().clone()).unwrap();
if args.debug {
println!("{changes:?}");
@@ -89,8 +96,9 @@ fn apply_changes(content: &str, mut changes: Vec<Change>, debug: bool) -> String
changes.sort_by_key(|change| change.node.text_range().start());
for change in changes {
+ use queries::Operator::*;
match change.kind {
- ChangeKind::Remove => {
+ Remove => {
let (before, after) = remove_node(&change.node);
if last_pos > before { continue }
ncontent += &content[last_pos..before];
@@ -99,7 +107,7 @@ fn apply_changes(content: &str, mut changes: Vec<Change>, debug: bool) -> String
}
last_pos = after;
}
- ChangeKind::Keep => {
+ Keep => {
let (before, after) = surrounding_noise(&change.node);
let (before, after) = eat_whitespace(&before, &after);
// let (before, after) = (change.node.text_range().start().into(), change.node.text_range().end().into());