diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 219 |
1 files changed, 177 insertions, 42 deletions
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 @@ | |||
1 | use trawk::{Context, Program}; | 1 | /// TBSP: tree-based source processor |
2 | #[derive(argh::FromArgs)] | ||
3 | struct Cli { | ||
4 | /// read the TBSP program source from a file | ||
5 | #[argh(option, short = 'f')] | ||
6 | program_file: std::path::PathBuf, | ||
7 | |||
8 | /// set the language that the file is written in | ||
9 | #[argh(option, short = 'l')] | ||
10 | language: String, | ||
11 | |||
12 | /// input file to process | ||
13 | #[argh(positional)] | ||
14 | file: Option<std::path::PathBuf>, | ||
15 | } | ||
2 | 16 | ||
3 | fn main() { | 17 | fn main() { |
4 | let src = r#" | 18 | let cli: Cli = argh::from_env(); |
5 | bar = 0 | 19 | |
6 | def foo(): | 20 | let program = std::fs::read_to_string(&cli.program_file).unwrap_or_else(|e| { |
7 | baz = 5 | 21 | eprintln!( |
8 | "# | 22 | "failed to read program-file from `{}`: {e}", |
9 | .to_owned(); | 23 | cli.program_file.display() |
10 | 24 | ); | |
11 | let program = Program::new() | 25 | std::process::exit(-1); |
12 | .from_str( | 26 | }); |
13 | r#" | 27 | |
14 | BEGIN { | 28 | let language = match cli.language.as_str() { |
15 | bool in_def = false; | 29 | "md" => tree_sitter_md::language(), |
16 | } | 30 | "typescript" => tree_sitter_typescript::language_typescript(), |
17 | pre function_definition { | 31 | "javascript" => tree_sitter_javascript::language(), |
18 | in_def = true; | 32 | "python" => tree_sitter_python::language(), |
33 | "rust" => tree_sitter_rust::language(), | ||
34 | lang => { | ||
35 | eprintln!("unknown language `{lang}`"); | ||
36 | std::process::exit(-1); | ||
37 | } | ||
38 | }; | ||
39 | |||
40 | let file = cli | ||
41 | .file | ||
42 | .map(std::fs::read_to_string) | ||
43 | .unwrap_or_else(try_consume_stdin) | ||
44 | .unwrap_or_else(|e| { | ||
45 | eprintln!("{e}"); | ||
46 | std::process::exit(-1) | ||
47 | }); | ||
48 | |||
49 | trawk::evaluate(&file, &program, language).unwrap_or_else(|e| { | ||
50 | eprintln!("{e:?}"); | ||
51 | std::process::exit(-1); | ||
52 | }); | ||
53 | } | ||
54 | |||
55 | fn try_consume_stdin() -> std::io::Result<String> { | ||
56 | let mut buffer = String::new(); | ||
57 | let mut lock = std::io::stdin().lock(); | ||
58 | |||
59 | while let Ok(n) = std::io::Read::read_to_string(&mut lock, &mut buffer) { | ||
60 | if n == 0 { | ||
61 | break; | ||
62 | } | ||
19 | } | 63 | } |
20 | post function_definition { | 64 | |
21 | in_def = false; | 65 | if buffer.is_empty() { |
66 | Err(std::io::Error::other("empty stdin")) | ||
67 | } else { | ||
68 | Ok(buffer) | ||
22 | } | 69 | } |
23 | pre identifier { | ||
24 | if (in_def) { | ||
25 | print(text(node)); | ||
26 | print(" "); | ||
27 | print("in def\n"); | ||
28 | } else { | ||
29 | }; | ||
30 | }"#, | ||
31 | ) | ||
32 | .unwrap(); | ||
33 | |||
34 | let mut parser = tree_sitter::Parser::new(); | ||
35 | let _ = parser.set_language(tree_sitter_python::language()); | ||
36 | |||
37 | let tree = parser.parse(&src, None).unwrap(); | ||
38 | let cursor = tree.walk(); | ||
39 | |||
40 | let mut ctx = Context::new(tree_sitter_python::language()) | ||
41 | .with_input(src) | ||
42 | .with_cursor(cursor) | ||
43 | .with_program(program) | ||
44 | .unwrap(); | ||
45 | |||
46 | let _ = ctx.eval(); | ||
47 | } | 70 | } |
71 | |||
72 | // fn main() { | ||
73 | // let src = r#" | ||
74 | // # foo1 | ||
75 | // | ||
76 | // bar | ||
77 | // | ||
78 | // ## foo1.1 | ||
79 | // | ||
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 | // } | ||