From 83b537bb860643ebdabc43ab47cb8645da8a2e6d Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 14 Jul 2024 13:00:51 +0100 Subject: rename: trawk -> tbsp --- Cargo.lock | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 14 ++-- src/eval.rs | 8 +- src/main.rs | 188 +++++++++++---------------------------------- 4 files changed, 307 insertions(+), 155 deletions(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b853519 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,252 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "argh" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7af5ba06967ff7214ce4c7419c7d185be7ecd6cc4965a8f6e1d8ce0398aad219" +dependencies = [ + "argh_derive", + "argh_shared", +] + +[[package]] +name = "argh_derive" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56df0aeedf6b7a2fc67d06db35b09684c3e8da0c95f8f27685cb17e08413d87a" +dependencies = [ + "argh_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "argh_shared" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5693f39141bda5760ecc4111ab08da40565d1771038c4a0250f03457ec707531" +dependencies = [ + "serde", +] + +[[package]] +name = "cc" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "066fce287b1d4eafef758e89e09d724a24808a9196fe9756b8ca90e86d0719a2" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "serde" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tbsp" +version = "0.1.0" +dependencies = [ + "argh", + "nom", + "regex", + "serde", + "thiserror", + "tree-sitter", + "tree-sitter-javascript", + "tree-sitter-md", + "tree-sitter-python", + "tree-sitter-rust", + "tree-sitter-typescript", +] + +[[package]] +name = "thiserror" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2675633b1499176c2dff06b0856a27976a8f9d436737b4cf4f312d4d91d8bbb" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d20468752b09f49e909e55a5d338caa8bedf615594e9d80bc4c565d30faf798c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tree-sitter" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "705bf7c0958d0171dd7d3a6542f2f4f21d87ed5f1dc8db52919d3a6bed9a359a" +dependencies = [ + "cc", + "regex", +] + +[[package]] +name = "tree-sitter-javascript" +version = "0.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8710a71bc6779e33811a8067bdda3ed08bed1733296ff915e44faf60f8c533d7" +dependencies = [ + "cc", + "tree-sitter", +] + +[[package]] +name = "tree-sitter-md" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c3cfd068f2527250bbd8ff407431164e12b17863e7eafb76e311dd3f96965a" +dependencies = [ + "cc", + "tree-sitter", +] + +[[package]] +name = "tree-sitter-python" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4066c6cf678f962f8c2c4561f205945c84834cce73d981e71392624fdc390a9" +dependencies = [ + "cc", + "tree-sitter", +] + +[[package]] +name = "tree-sitter-rust" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "277690f420bf90741dea984f3da038ace46c4fe6047cba57a66822226cde1c93" +dependencies = [ + "cc", + "tree-sitter", +] + +[[package]] +name = "tree-sitter-typescript" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecb35d98a688378e56c18c9c159824fd16f730ccbea19aacf4f206e5d5438ed9" +dependencies = [ + "cc", + "tree-sitter", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/Cargo.toml b/Cargo.toml index 570ec22..429338f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,22 +1,22 @@ [package] -name = "trawk" +name = "tbsp" version = "0.1.0" edition = "2021" [lib] [dependencies] -tree-sitter = "0.20" +tree-sitter = "0.21" regex = "1.3" thiserror = "1.0.61" serde = "1.0.204" nom = "7.1.3" -tree-sitter-md = {version = "0.1", optional = true} -tree-sitter-typescript = {version = "0.20.1", optional = true} -tree-sitter-javascript = {version = "0.20.0", optional = true} -tree-sitter-python = {version = "0.20.4", optional = true} -tree-sitter-rust = {version = "0.20.3", optional = true} +tree-sitter-md = {version = "0.2", optional = true} +tree-sitter-typescript = {version = "0.21", optional = true} +tree-sitter-javascript = {version = "0.21", optional = true} +tree-sitter-python = {version = "0.21", optional = true} +tree-sitter-rust = {version = "0.21", optional = true} argh = "0.1.12" [features] diff --git a/src/eval.rs b/src/eval.rs index e13cec3..029cffe 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -1,4 +1,4 @@ -//! tree walking interpreter for trawk +//! tree walking interpreter for tbsp use crate::ast; use std::{collections::HashMap, fmt}; @@ -112,6 +112,8 @@ impl Value { fn mul(&self, other: &Self) -> Result { match (self, other) { (Self::Integer(s), Self::Integer(o)) => Ok(Self::Integer(*s * *o)), + (Self::Integer(s), Self::String(o)) => Ok(Self::String(o.repeat(*s as usize))), + (Self::String(_), Self::Integer(_)) => other.mul(self), _ => Err(Error::UndefinedBinOp( ast::BinOp::Arith(ast::ArithOp::Mul), self.ty(), @@ -656,13 +658,13 @@ impl<'a> Context<'a> { pub fn evaluate(file: &str, program: &str, language: tree_sitter::Language) -> Result { let mut parser = tree_sitter::Parser::new(); - let _ = parser.set_language(language); + let _ = parser.set_language(&language); let tree = parser.parse(file, None).unwrap(); let cursor = tree.walk(); let program = ast::Program::new().from_str(program).unwrap(); - let mut ctx = Context::new(tree_sitter_md::language()) + let mut ctx = Context::new(language) .with_input(file.to_owned()) .with_cursor(cursor) .with_program(program)?; diff --git a/src/main.rs b/src/main.rs index 6196a32..ed024fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -/// TBSP: tree-based source processor +/// tree-based source processor #[derive(argh::FromArgs)] struct Cli { /// read the TBSP program source from a file @@ -14,42 +14,43 @@ struct Cli { file: Option, } -fn main() { - 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}`"); +impl Cli { + fn program(&self) -> String { + std::fs::read_to_string(&self.program_file).unwrap_or_else(|e| { + eprintln!( + "failed to read program-file from `{}`: {e}", + self.program_file.display() + ); 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) - }); + fn language(&self) -> tree_sitter::Language { + match self.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); + } + } + } - trawk::evaluate(&file, &program, language).unwrap_or_else(|e| { - eprintln!("{e:?}"); - std::process::exit(-1); - }); + fn file(&self) -> String { + match self.file.as_ref() { + Some(f) => std::fs::read_to_string(f).unwrap_or_else(|e| { + eprintln!("failed to read input-file from `{}`: {e}", f.display()); + std::process::exit(-1); + }), + None => try_consume_stdin().unwrap_or_else(|e| { + eprintln!("failed to read input-file from stdin: {e}"); + std::process::exit(-1); + }), + } + } } fn try_consume_stdin() -> std::io::Result { @@ -69,114 +70,11 @@ fn try_consume_stdin() -> std::io::Result { } } -// 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(); -// } +fn main() { + let cli: Cli = argh::from_env(); + + tbsp::evaluate(&cli.file(), &cli.program(), cli.language()).unwrap_or_else(|e| { + eprintln!("{e:?}"); + std::process::exit(-1); + }); +} -- cgit v1.2.3