Debian Codegraph @

src/bin/parser.rs

raw symbols SCIP
1 //! This is a simple CLI utility to take in an FTL file and output the AST.
2 //!
3 //! ## View the `Debug` representation:
4 //!
5 //! From the root directory of the `fluent-rs` repo:
6 //!
7 //! ```sh
8 //! cargo run --bin parser -- ./fluent-syntax/tests/fixtures/literal_expressions.ftl
9 //! ```
10 //!
11 //! ## View the `json` representation:
12 //!
13 //! ```sh
14 //! cargo run --bin parser --features json -- ./fluent-syntax/tests/fixtures/literal_expressions.ftl
15 //! ```
16
17 use fluent_syntax::parser::parse;
18 use std::env;
19 use std::fs::File;
20 use std::io;
21 use std::io::Read;
22
23 fn read_file(path: &str) -> Result<String, io::Error> {
24 let mut f = File::open(path)?;
25 let mut s = String::new();
26 f.read_to_string(&mut s)?;
27 Ok(s)
28 }
29
30 fn main() {
31 let args: Vec<String> = env::args().collect();
32 let source = read_file(args.get(1).expect("Pass a file path as the first argument"))
33 .expect("Failed to fetch file");
34
35 let (ast, errors) = match parse(source.as_str()) {
36 Ok(ast) => (ast, None),
37 Err((ast, err)) => (ast, Some(err)),
38 };
39
40 #[cfg(feature = "json")]
41 {
42 let target_json = serde_json::to_string_pretty(&ast).unwrap();
43 println!("{}", target_json);
44 }
45 #[cfg(not(feature = "json"))]
46 {
47 use std::fmt::Write;
48 let mut result = String::new();
49 write!(result, "{:#?}", ast).unwrap();
50 println!("{}", result);
51 }
52
53 if let Some(errors) = errors {
54 println!("\n======== Errors ========== \n");
55 for err in errors {
56 println!("Err: {:#?}", err);
57 }
58 }
59 }