Skip to content

Commit dbc1bcb

Browse files
Add --output-directory/-d, child index info
1 parent 15469be commit dbc1bcb

6 files changed

Lines changed: 34 additions & 16 deletions

File tree

treeedb/src/cli.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fs;
22
use std::io;
33
use std::io::Read;
4+
use std::path::{Path, PathBuf};
45
use std::process;
56

67
use anyhow::{Context, Result};
@@ -44,14 +45,15 @@ fn handle_parse_errors(path: &str, tree: &Tree, on_parse_error: &OnParseError) {
4445
/// Generate Datalog facts from source code
4546
#[derive(Parser, Debug)]
4647
#[command(author, version, about, long_about = None)]
47-
// TODO(lb): Output directory, default is current directory
48-
// #[arg(short, long, default_value = None)]
49-
// pub output: Option<String>,
5048
pub struct Args {
5149
/// Behavior on parse errors
5250
#[arg(long, default_value_t = OnParseError::Warn, value_name = "CHOICE")]
5351
on_parse_error: OnParseError,
5452

53+
/// Output directory
54+
#[arg(short, long, default_value = ".", value_name = "OUT_DIR")]
55+
pub output_directory: PathBuf,
56+
5557
/// Source code to consume; if empty, parse from stdin
5658
#[arg(value_name = "SRC_FILE")]
5759
pub source_files: Vec<String>,
@@ -75,13 +77,19 @@ fn stdin_string() -> Result<String> {
7577
Ok(stdin_str)
7678
}
7779

80+
fn create_consumer(output_directory: &Path) -> Result<super::wide::WideCsvConsumer> {
81+
// TODO(lb): Create consumer based on config
82+
// For now, just use the wide CSV consumer as the default
83+
Ok(super::wide::WideCsvConsumer::new(
84+
output_directory.join("node.csv"),
85+
output_directory.join("field.csv"),
86+
output_directory.join("child.csv"),
87+
)?)
88+
}
89+
7890
pub fn main(language: tree_sitter::Language) -> Result<()> {
7991
let args = Args::parse();
80-
let mut fc = super::wide::WideCsvConsumer::new(
81-
"node.csv".into(),
82-
"field.csv".into(),
83-
"child.csv".into(),
84-
)?;
92+
let mut fc = create_consumer(&args.output_directory)?;
8593
if args.source_files.is_empty() {
8694
let content = stdin_string()?;
8795
let tree = parse(language, &content)?;

treeedb/src/consumer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait FactConsumer {
2222
child: &Node<'_>,
2323
) -> Result<(), Self::Err>;
2424

25-
fn child(&mut self, parent: &Node<'_>, child: &Node<'_>) -> Result<(), Self::Err>;
25+
fn child(&mut self, parent: &Node<'_>, index: u32, child: &Node<'_>) -> Result<(), Self::Err>;
2626

2727
fn node(&mut self, node: &Node<'_>, source: &[u8]) -> Result<(), Self::Err>;
2828
}

treeedb/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub fn facts<E>(
2121
fc.field(&node, name, &child)?;
2222
}
2323
}
24-
for child in node.named_children(&mut cursor) {
25-
fc.child(&node, &child)?;
24+
for (i, child) in node.named_children(&mut cursor).enumerate() {
25+
fc.child(&node, i as u32, &child)?;
2626
nodes.push(child);
2727
}
2828
}

treeedb/src/narrow.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ impl FactConsumer for NarrowCsvConsumer {
3535
Ok(())
3636
}
3737

38-
fn child(&mut self, _parent: &Node<'_>, _child: &Node<'_>) -> Result<(), Self::Err> {
38+
fn child(
39+
&mut self,
40+
_parent: &Node<'_>,
41+
_index: u32,
42+
_child: &Node<'_>,
43+
) -> Result<(), Self::Err> {
3944
Ok(())
4045
}
4146

treeedb/src/wide.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ impl FactConsumer for WideCsvConsumer {
4141
Ok(())
4242
}
4343

44-
fn child(&mut self, parent: &Node<'_>, child: &Node<'_>) -> Result<(), Self::Err> {
45-
self.child
46-
.write_record([&parent.id().to_string(), &child.id().to_string()])?;
44+
fn child(&mut self, parent: &Node<'_>, index: u32, child: &Node<'_>) -> Result<(), Self::Err> {
45+
self.child.write_record([
46+
&parent.id().to_string(),
47+
&index.to_string(),
48+
&child.id().to_string(),
49+
])?;
4750
Ok(())
4851
}
4952

treeedbgen-souffle/src/gen.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn node_with_fields(
107107
)?;
108108
writeln!(
109109
w,
110-
"{}(x, as(y, {})) :- {}{}(x), {}child(x, y).",
110+
"{}(x, as(y, {})) :- {}{}(x), {}child(x, _, y).",
111111
child_relation_name,
112112
child_type_name,
113113
config.relation_prefix,
@@ -219,6 +219,7 @@ fn declare_node(config: &PrivGenConfig, w: &mut impl Write) -> Result<(), GenErr
219219
writeln!(w, ".type {}EndRow <: number", config.type_prefix)?;
220220
writeln!(w, ".type {}EndCol <: number", config.type_prefix)?;
221221
writeln!(w, ".type {}NodeText <: symbol", config.type_prefix)?;
222+
writeln!(w, ".type {}NodeIndex <: number", config.type_prefix)?;
222223
writeln!(
223224
w,
224225
".decl {}node({})",
@@ -294,6 +295,7 @@ fn declare_child(config: &PrivGenConfig, w: &mut impl Write) -> Result<(), GenEr
294295
config.relation_prefix,
295296
[
296297
format!("parent: {}Node", config.type_prefix),
298+
format!("index: {}NodeIndex", config.type_prefix),
297299
format!("child: {}Node", config.type_prefix),
298300
]
299301
.join(", ")

0 commit comments

Comments
 (0)