From 7ae7e42eb1eb981483cc4183368bec4932b8f1c2 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 14 Jul 2024 10:16:15 +0100 Subject: add trawk cli --- src/main.rs | 219 ++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 177 insertions(+), 42 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 09a15ef..6196a32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,47 +1,182 @@ -use trawk::{Context, Program}; +/// TBSP: tree-based source processor +#[derive(argh::FromArgs)] +struct Cli { + /// read the TBSP program source from a file + #[argh(option, short = 'f')] + program_file: std::path::PathBuf, + + /// set the language that the file is written in + #[argh(option, short = 'l')] + language: String, + + /// input file to process + #[argh(positional)] + file: Option, +} fn main() { - let src = r#" -bar = 0 -def foo(): - baz = 5 - "# - .to_owned(); - - let program = Program::new() - .from_str( - r#" - BEGIN { - bool in_def = false; - } - pre function_definition { - in_def = true; + let cli: Cli = argh::from_env(); + + let program = std::fs::read_to_string(&cli.program_file).unwrap_or_else(|e| { + eprintln!( + "failed to read program-file from `{}`: {e}", + cli.program_file.display() + ); + std::process::exit(-1); + }); + + let language = match cli.language.as_str() { + "md" => tree_sitter_md::language(), + "typescript" => tree_sitter_typescript::language_typescript(), + "javascript" => tree_sitter_javascript::language(), + "python" => tree_sitter_python::language(), + "rust" => tree_sitter_rust::language(), + lang => { + eprintln!("unknown language `{lang}`"); + std::process::exit(-1); + } + }; + + let file = cli + .file + .map(std::fs::read_to_string) + .unwrap_or_else(try_consume_stdin) + .unwrap_or_else(|e| { + eprintln!("{e}"); + std::process::exit(-1) + }); + + trawk::evaluate(&file, &program, language).unwrap_or_else(|e| { + eprintln!("{e:?}"); + std::process::exit(-1); + }); +} + +fn try_consume_stdin() -> std::io::Result { + let mut buffer = String::new(); + let mut lock = std::io::stdin().lock(); + + while let Ok(n) = std::io::Read::read_to_string(&mut lock, &mut buffer) { + if n == 0 { + break; + } } - post function_definition { - in_def = false; + + if buffer.is_empty() { + Err(std::io::Error::other("empty stdin")) + } else { + Ok(buffer) } - pre identifier { - if (in_def) { - print(text(node)); - print(" "); - print("in def\n"); - } else { - }; - }"#, - ) - .unwrap(); - - let mut parser = tree_sitter::Parser::new(); - let _ = parser.set_language(tree_sitter_python::language()); - - let tree = parser.parse(&src, None).unwrap(); - let cursor = tree.walk(); - - let mut ctx = Context::new(tree_sitter_python::language()) - .with_input(src) - .with_cursor(cursor) - .with_program(program) - .unwrap(); - - let _ = ctx.eval(); } + +// fn main() { +// let src = r#" +// # foo1 +// +// bar +// +// ## foo1.1 +// +// bar baz +// +// # foo2 +// +// bar baz +// +// ``` +// fn main() { +// } +// ``` +// +// - foo +// - bar +// - baz +// +// "# +// .to_owned(); +// +// let program = Program::new() +// .from_str( +// r#" +// BEGIN { +// int depth = 0; +// +// print("\n"); +// print("\n"); +// } +// +// enter section { +// depth += 1; +// } +// leave section { +// depth -= 1; +// } +// +// enter atx_heading { +// print(""); +// } +// leave atx_heading { +// print("\n"); +// } +// +// enter paragraph { +// print("

"); +// } +// leave paragraph { +// print("

\n"); +// } +// +// enter list { +// print("
    "); +// } +// leave list { +// print("
\n"); +// } +// +// enter list_item { +// print("
  • "); +// } +// leave list_item { +// print("
  • \n"); +// } +// +// enter fenced_code_block { +// print("
    ");
    +//     }
    +//     leave fenced_code_block {
    +//         print("
    \n"); +// } +// +// enter inline { +// print(text(node)); +// } +// enter code_fence_content { +// print(text(node)); +// } +// +// END { +// print("\n"); +// print("\n"); +// } +// "#, +// ) +// .unwrap(); +// +// let mut parser = tree_sitter::Parser::new(); +// let _ = parser.set_language(&tree_sitter_md::language()); +// +// let tree = parser.parse(&src, None).unwrap(); +// let cursor = tree.walk(); +// +// let mut ctx = Context::new(tree_sitter_md::language()) +// .with_input(src) +// .with_cursor(cursor) +// .with_program(program) +// .unwrap(); +// +// let _ = ctx.eval(); +// } -- cgit v1.2.3