2022-02-07 15:35:28 +03:00
|
|
|
// Copyright (C) 2019-2022 Aleo Systems Inc.
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
use leo_errors::{emitter::Handler, Result};
|
|
|
|
use leo_span::symbol::create_session_if_not_set_then;
|
|
|
|
|
|
|
|
use std::{env, fs, path::Path};
|
|
|
|
|
2022-02-21 21:52:12 +03:00
|
|
|
fn to_leo_tree(input_filepath: &Path, state_filepath: &Path) -> Result<String, String> {
|
|
|
|
// Loads the inputs a string from the given file path.
|
|
|
|
let input_string = fs::read_to_string(&input_filepath.to_path_buf()).expect("failed to open an input file");
|
|
|
|
let state_string = fs::read_to_string(&state_filepath.to_path_buf()).expect("failed to open a state file");
|
2022-02-07 15:35:28 +03:00
|
|
|
|
|
|
|
// Parses the Leo file constructing an ast which is then serialized.
|
|
|
|
create_session_if_not_set_then(|_| {
|
2022-02-21 20:42:00 +03:00
|
|
|
Handler::with(|handler| {
|
2022-02-21 21:52:12 +03:00
|
|
|
let input = leo_parser::parse_program_inputs(
|
2022-02-21 20:42:00 +03:00
|
|
|
&handler,
|
2022-02-21 21:52:12 +03:00
|
|
|
input_string.clone(),
|
|
|
|
input_filepath.to_str().unwrap(),
|
|
|
|
state_string,
|
|
|
|
state_filepath.to_str().unwrap(),
|
2022-02-21 20:42:00 +03:00
|
|
|
)?;
|
2022-02-21 21:36:46 +03:00
|
|
|
|
|
|
|
let json = input.to_json_string()?;
|
|
|
|
Ok(json)
|
|
|
|
}).map_err(|e| e.to_string())
|
2022-02-07 15:35:28 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-21 21:36:46 +03:00
|
|
|
fn main() -> Result<(), String> {
|
2022-02-07 15:35:28 +03:00
|
|
|
// Parse the command-line arguments as strings.
|
|
|
|
let cli_arguments = env::args().collect::<Vec<String>>();
|
|
|
|
|
|
|
|
// Check that the correct number of command-line arguments were passed in.
|
2022-02-21 21:52:12 +03:00
|
|
|
if cli_arguments.len() < 3 || cli_arguments.len() > 4 {
|
2022-02-07 15:35:28 +03:00
|
|
|
eprintln!("Warning - an invalid number of command-line arguments were provided.");
|
|
|
|
println!(
|
2022-02-21 21:52:12 +03:00
|
|
|
"\nCommand-line usage:\n\n\tleo_ast {{PATH/TO/INPUT_FILENAME}}.in {{PATH/TO/STATE_FILENAME}}.in {{PATH/TO/OUTPUT_DIRECTORY (optional)}}\n"
|
2022-02-07 15:35:28 +03:00
|
|
|
);
|
|
|
|
return Ok(()); // Exit innocently
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the input filepath.
|
|
|
|
let input_filepath = Path::new(&cli_arguments[1]);
|
2022-02-21 21:52:12 +03:00
|
|
|
let state_filepath = Path::new(&cli_arguments[2]);
|
2022-02-07 15:35:28 +03:00
|
|
|
|
|
|
|
// Construct the serialized syntax tree.
|
2022-02-21 21:52:12 +03:00
|
|
|
let serialized_leo_tree = to_leo_tree(input_filepath, state_filepath)?;
|
2022-02-07 15:35:28 +03:00
|
|
|
println!("{}", serialized_leo_tree);
|
|
|
|
|
|
|
|
// Determine the output directory.
|
2022-02-21 21:52:12 +03:00
|
|
|
let output_directory = match cli_arguments.len() == 4 {
|
2022-02-07 15:35:28 +03:00
|
|
|
true => format!(
|
|
|
|
"{}/{}.json",
|
|
|
|
cli_arguments[2],
|
|
|
|
input_filepath.file_stem().unwrap().to_str().unwrap()
|
|
|
|
),
|
|
|
|
false => format!("./{}.json", input_filepath.file_stem().unwrap().to_str().unwrap()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Write the serialized syntax tree to the output directory.
|
|
|
|
fs::write(Path::new(&output_directory), serialized_leo_tree).expect("failed to write output");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|