2023-02-03 01:27:06 +03:00
|
|
|
// Copyright (C) 2019-2023 Aleo Systems Inc.
|
2022-02-07 15:35:28 +03:00
|
|
|
// This file is part of the Leo library.
|
|
|
|
|
|
|
|
// The Leo library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// The Leo library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-08-05 01:36:01 +03:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
2023-08-16 01:07:04 +03:00
|
|
|
use leo_ast::NodeBuilder;
|
2022-02-07 15:35:28 +03:00
|
|
|
use leo_errors::{emitter::Handler, Result};
|
|
|
|
use leo_span::symbol::create_session_if_not_set_then;
|
|
|
|
|
2023-05-31 05:50:01 +03:00
|
|
|
use clap::Parser;
|
2022-02-23 22:53:21 +03:00
|
|
|
use std::{
|
|
|
|
fs,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2022-02-07 15:35:28 +03:00
|
|
|
|
2023-05-31 05:50:01 +03:00
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
#[clap(name = "input parser", about = "Parse an Input file and save its JSON representation")]
|
2022-02-22 12:56:01 +03:00
|
|
|
struct Opt {
|
|
|
|
/// Path to the input file.
|
|
|
|
input_path: PathBuf,
|
2022-02-23 22:53:21 +03:00
|
|
|
|
2022-02-22 12:56:01 +03:00
|
|
|
/// Optional path to the output directory.
|
|
|
|
out_dir_path: Option<PathBuf>,
|
2022-02-07 15:35:28 +03:00
|
|
|
|
2022-02-22 12:56:01 +03:00
|
|
|
/// Whether to print result to STDOUT.
|
2023-05-31 05:50:01 +03:00
|
|
|
#[clap(short, long)]
|
2022-02-23 22:53:21 +03:00
|
|
|
print_stdout: bool,
|
2022-02-22 12:56:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), String> {
|
2022-06-06 21:00:08 +03:00
|
|
|
let opt = Opt::parse();
|
2022-05-03 21:48:48 +03:00
|
|
|
let input_tree = create_session_if_not_set_then(|s| {
|
2023-03-27 09:18:03 +03:00
|
|
|
let input_string = s.source_map.load_file(&opt.input_path).expect("failed to open an input file");
|
2022-05-03 21:48:48 +03:00
|
|
|
|
2022-02-21 20:42:00 +03:00
|
|
|
Handler::with(|handler| {
|
2023-08-16 01:07:04 +03:00
|
|
|
let node_builder = NodeBuilder::default();
|
|
|
|
let input =
|
|
|
|
leo_parser::parse_program_inputs(handler, &node_builder, &input_string.src, input_string.start_pos)?;
|
2022-03-16 03:15:08 +03:00
|
|
|
input.to_json_string()
|
2022-02-21 23:01:32 +03:00
|
|
|
})
|
|
|
|
.map_err(|e| e.to_string())
|
2022-02-22 12:56:01 +03:00
|
|
|
})?;
|
2022-02-07 15:35:28 +03:00
|
|
|
|
2022-02-22 12:56:01 +03:00
|
|
|
if opt.print_stdout {
|
2022-10-25 08:41:10 +03:00
|
|
|
println!("{input_tree}");
|
2022-02-07 15:35:28 +03:00
|
|
|
}
|
|
|
|
|
2022-02-22 12:56:01 +03:00
|
|
|
let out_path = if let Some(out_dir) = opt.out_dir_path {
|
2023-03-27 09:18:03 +03:00
|
|
|
format!("{}/{}.json", out_dir.as_path().display(), opt.input_path.file_stem().unwrap().to_str().unwrap())
|
2022-02-22 12:56:01 +03:00
|
|
|
} else {
|
|
|
|
format!("./{}.json", opt.input_path.file_stem().unwrap().to_str().unwrap())
|
2022-02-07 15:35:28 +03:00
|
|
|
};
|
|
|
|
|
2022-02-22 12:56:01 +03:00
|
|
|
fs::write(Path::new(&out_path), input_tree).expect("failed to write output");
|
2022-02-23 22:53:21 +03:00
|
|
|
|
2022-02-07 15:35:28 +03:00
|
|
|
Ok(())
|
|
|
|
}
|