-
Notifications
You must be signed in to change notification settings - Fork 6
/
print_ast.rs
47 lines (40 loc) · 1.29 KB
/
print_ast.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#[macro_use]
extern crate rust_to_ocaml_attr;
use std::path::PathBuf;
use crate::printers::parse_module_print_ast_code;
use crate::printers::read_stdin_or_file;
use crate::printers::PrintingMode;
pub mod ast;
pub mod constants;
pub mod cst_to_ast;
pub mod errors;
pub mod node_wrapper;
pub mod parser_post_process;
pub mod parser_pre_process;
pub mod printers;
pub mod sitter;
pub mod string_helpers;
/// Python Parser which will output AST pretty printed.
/// Usage: `print_ast file.py` or `echo "print('hello')" | print_ast`.
/// With a build system such as buck, `buck run //path/to/errpy:print_ast -- file.py` and `echo "print('test')" | buck run //path/to/errpy:print_ast -`
#[derive(clap::Parser)]
struct Args {
/// Python file to generate AST for
input_file: PathBuf,
}
fn main() {
let args = <Args as clap::Parser>::parse();
let input_file = read_stdin_or_file(args.input_file);
let (ast, errors) = parse_module_print_ast_code(input_file, PrintingMode::ASTAndPrettyPrintAST);
if errors.is_empty() {
println!("{}", ast);
} else {
print!("{}\n{}", ast, errors);
}
}