aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2024-07-14 13:00:51 +0100
committerAkshay <[email protected]>2024-07-14 13:00:51 +0100
commit83b537bb860643ebdabc43ab47cb8645da8a2e6d (patch)
treebf695240d2f82c998ed727812fbe5f200b85dbdd /src/main.rs
parent7ae7e42eb1eb981483cc4183368bec4932b8f1c2 (diff)
rename: trawk -> tbsp
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs188
1 files changed, 43 insertions, 145 deletions
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 @@
1/// TBSP: tree-based source processor 1/// tree-based source processor
2#[derive(argh::FromArgs)] 2#[derive(argh::FromArgs)]
3struct Cli { 3struct Cli {
4 /// read the TBSP program source from a file 4 /// read the TBSP program source from a file
@@ -14,42 +14,43 @@ struct Cli {
14 file: Option<std::path::PathBuf>, 14 file: Option<std::path::PathBuf>,
15} 15}
16 16
17fn main() { 17impl Cli {
18 let cli: Cli = argh::from_env(); 18 fn program(&self) -> String {
19 19 std::fs::read_to_string(&self.program_file).unwrap_or_else(|e| {
20 let program = std::fs::read_to_string(&cli.program_file).unwrap_or_else(|e| { 20 eprintln!(
21 eprintln!( 21 "failed to read program-file from `{}`: {e}",
22 "failed to read program-file from `{}`: {e}", 22 self.program_file.display()
23 cli.program_file.display() 23 );
24 );
25 std::process::exit(-1);
26 });
27
28 let language = match cli.language.as_str() {
29 "md" => tree_sitter_md::language(),
30 "typescript" => tree_sitter_typescript::language_typescript(),
31 "javascript" => tree_sitter_javascript::language(),
32 "python" => tree_sitter_python::language(),
33 "rust" => tree_sitter_rust::language(),
34 lang => {
35 eprintln!("unknown language `{lang}`");
36 std::process::exit(-1); 24 std::process::exit(-1);
37 } 25 })
38 }; 26 }
39 27
40 let file = cli 28 fn language(&self) -> tree_sitter::Language {
41 .file 29 match self.language.as_str() {
42 .map(std::fs::read_to_string) 30 "md" => tree_sitter_md::language(),
43 .unwrap_or_else(try_consume_stdin) 31 "typescript" => tree_sitter_typescript::language_typescript(),
44 .unwrap_or_else(|e| { 32 "javascript" => tree_sitter_javascript::language(),
45 eprintln!("{e}"); 33 "python" => tree_sitter_python::language(),
46 std::process::exit(-1) 34 "rust" => tree_sitter_rust::language(),
47 }); 35 lang => {
36 eprintln!("unknown language `{lang}`");
37 std::process::exit(-1);
38 }
39 }
40 }
48 41
49 trawk::evaluate(&file, &program, language).unwrap_or_else(|e| { 42 fn file(&self) -> String {
50 eprintln!("{e:?}"); 43 match self.file.as_ref() {
51 std::process::exit(-1); 44 Some(f) => std::fs::read_to_string(f).unwrap_or_else(|e| {
52 }); 45 eprintln!("failed to read input-file from `{}`: {e}", f.display());
46 std::process::exit(-1);
47 }),
48 None => try_consume_stdin().unwrap_or_else(|e| {
49 eprintln!("failed to read input-file from stdin: {e}");
50 std::process::exit(-1);
51 }),
52 }
53 }
53} 54}
54 55
55fn try_consume_stdin() -> std::io::Result<String> { 56fn try_consume_stdin() -> std::io::Result<String> {
@@ -69,114 +70,11 @@ fn try_consume_stdin() -> std::io::Result<String> {
69 } 70 }
70} 71}
71 72
72// fn main() { 73fn main() {
73// let src = r#" 74 let cli: Cli = argh::from_env();
74// # foo1 75
75// 76 tbsp::evaluate(&cli.file(), &cli.program(), cli.language()).unwrap_or_else(|e| {
76// bar 77 eprintln!("{e:?}");
77// 78 std::process::exit(-1);
78// ## foo1.1 79 });
79// 80}
80// bar baz
81//
82// # foo2
83//
84// bar baz
85//
86// ```
87// fn main() {
88// }
89// ```
90//
91// - foo
92// - bar
93// - baz
94//
95// "#
96// .to_owned();
97//
98// let program = Program::new()
99// .from_str(
100// r#"
101// BEGIN {
102// int depth = 0;
103//
104// print("<html>\n");
105// print("<body>\n");
106// }
107//
108// enter section {
109// depth += 1;
110// }
111// leave section {
112// depth -= 1;
113// }
114//
115// enter atx_heading {
116// print("<h");
117// print(depth);
118// print(">");
119// }
120// leave atx_heading {
121// print("</h");
122// print(depth);
123// print(">\n");
124// }
125//
126// enter paragraph {
127// print("<p>");
128// }
129// leave paragraph {
130// print("</p>\n");
131// }
132//
133// enter list {
134// print("<ol>");
135// }
136// leave list {
137// print("</ol>\n");
138// }
139//
140// enter list_item {
141// print("<li>");
142// }
143// leave list_item {
144// print("</li>\n");
145// }
146//
147// enter fenced_code_block {
148// print("<pre>");
149// }
150// leave fenced_code_block {
151// print("</pre>\n");
152// }
153//
154// enter inline {
155// print(text(node));
156// }
157// enter code_fence_content {
158// print(text(node));
159// }
160//
161// END {
162// print("</body>\n");
163// print("</html>\n");
164// }
165// "#,
166// )
167// .unwrap();
168//
169// let mut parser = tree_sitter::Parser::new();
170// let _ = parser.set_language(&tree_sitter_md::language());
171//
172// let tree = parser.parse(&src, None).unwrap();
173// let cursor = tree.walk();
174//
175// let mut ctx = Context::new(tree_sitter_md::language())
176// .with_input(src)
177// .with_cursor(cursor)
178// .with_program(program)
179// .unwrap();
180//
181// let _ = ctx.eval();
182// }