diff --git a/ast/src/input/definition.rs b/ast/src/input/definition.rs index 2068479161..8d638a2807 100644 --- a/ast/src/input/definition.rs +++ b/ast/src/input/definition.rs @@ -19,7 +19,7 @@ use crate::{Expression, Identifier, Type}; /// A single definition inside a section in a state or an input file. /// Structure of a definition would be: `: = ;` -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Definition { pub type_: Type, pub name: Identifier, diff --git a/ast/src/input/input.rs b/ast/src/input/input.rs index ceada94460..f5c6d9658f 100644 --- a/ast/src/input/input.rs +++ b/ast/src/input/input.rs @@ -15,9 +15,10 @@ // along with the Leo library. If not, see . use super::*; +use leo_errors::AstError; /// Input data which includes [`ProgramInput`] and [`ProgramState`]. -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct Input { pub program_input: ProgramInput, pub program_state: ProgramState, @@ -25,7 +26,14 @@ pub struct Input { /// A raw unprocessed input or state file data. Used for future conversion /// into [`ProgramInput`] or [`ProgramState`]. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct ParsedInputFile { pub sections: Vec
, } + +impl Input { + /// Serializes the ast into a JSON string. + pub fn to_json_string(&self) -> Result { + Ok(serde_json::to_string_pretty(&self).map_err(|e| AstError::failed_to_convert_ast_to_json_string(&e))?) + } +} diff --git a/ast/src/input/input_value.rs b/ast/src/input/input_value.rs index 3541852813..0dbd6bda5f 100644 --- a/ast/src/input/input_value.rs +++ b/ast/src/input/input_value.rs @@ -17,9 +17,10 @@ use crate::{CharValue, Expression, GroupValue, IntegerType, Node, SpreadOrExpression, Type, ValueExpression}; use leo_errors::{AstError, LeoError, ParserError, Result}; +use serde::{Deserialize, Serialize}; use std::fmt; -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum InputValue { Address(String), Boolean(bool), diff --git a/ast/src/input/mod.rs b/ast/src/input/mod.rs index 092ae170e1..0bfc8ce1a4 100644 --- a/ast/src/input/mod.rs +++ b/ast/src/input/mod.rs @@ -38,5 +38,7 @@ pub use section::*; use indexmap::IndexMap; use leo_errors::{LeoError, Result}; use leo_span::{sym, Span, Symbol}; +use serde::{Deserialize, Serialize}; +use std::fmt; type Definitions = IndexMap; diff --git a/ast/src/input/parameter.rs b/ast/src/input/parameter.rs index 3b13d3827d..cd235df4e5 100644 --- a/ast/src/input/parameter.rs +++ b/ast/src/input/parameter.rs @@ -19,7 +19,7 @@ use crate::{Identifier, Type}; /// A set of properties for a single definition in an input file. /// Used as a key in [`ProgramInput`] and [`ProgramState`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize)] pub struct Parameter { pub variable: Identifier, pub type_: Type, @@ -35,3 +35,20 @@ impl From for Parameter { } } } + +impl fmt::Display for Parameter { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}: {}", self.variable, self.type_) + } +} + +/// Parameter is a key, so for allowing its JSON representation, +/// we need to make a string. +impl Serialize for Parameter { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} diff --git a/ast/src/input/program_input.rs b/ast/src/input/program_input.rs index 4a6803aa95..a15ee4cf46 100644 --- a/ast/src/input/program_input.rs +++ b/ast/src/input/program_input.rs @@ -17,7 +17,7 @@ use super::*; /// Processed Program input. -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct ProgramInput { main: Definitions, registers: Definitions, diff --git a/ast/src/input/program_state.rs b/ast/src/input/program_state.rs index 9a75d5a070..14d9f75c9b 100644 --- a/ast/src/input/program_state.rs +++ b/ast/src/input/program_state.rs @@ -17,7 +17,7 @@ use super::*; /// Processed Program state. -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct ProgramState { state: Definitions, record: Definitions, diff --git a/ast/src/input/section.rs b/ast/src/input/section.rs index e90891e299..a17def65ec 100644 --- a/ast/src/input/section.rs +++ b/ast/src/input/section.rs @@ -18,7 +18,7 @@ use super::*; /// A single section in an input or a state file. /// Example of a section would be: `[main]`. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Section { pub name: Symbol, pub definitions: Vec, diff --git a/parser/examples/input_parser.rs b/parser/examples/input_parser.rs index a9475f4e75..a39ab3b590 100644 --- a/parser/examples/input_parser.rs +++ b/parser/examples/input_parser.rs @@ -19,7 +19,7 @@ use leo_span::symbol::create_session_if_not_set_then; use std::{env, fs, path::Path}; -fn to_leo_tree(filepath: &Path) -> Result { +fn to_leo_tree(filepath: &Path) -> Result { // Loads the Leo code as a string from the given file path. let program_filepath = filepath.to_path_buf(); let program_string = fs::read_to_string(&program_filepath).expect("failed to open input file"); @@ -27,20 +27,22 @@ fn to_leo_tree(filepath: &Path) -> Result { // Parses the Leo file constructing an ast which is then serialized. create_session_if_not_set_then(|_| { Handler::with(|handler| { - let _ast = leo_parser::parse_program_input( + let input = leo_parser::parse_program_input( &handler, program_string.clone(), filepath.to_str().unwrap(), program_string, filepath.to_str().unwrap(), )?; - // Ok(Input::to_json_string(&ast).expect("serialization failed")) - Ok("aa".to_string()) - }); + + let json = input.to_json_string()?; + println!("{}", json); + Ok(json) + }).map_err(|e| e.to_string()) }) } -fn main() -> Result<()> { +fn main() -> Result<(), String> { // Parse the command-line arguments as strings. let cli_arguments = env::args().collect::>(); diff --git a/span/src/symbol.rs b/span/src/symbol.rs index 0f74a3b7d8..74ee62d6cf 100644 --- a/span/src/symbol.rs +++ b/span/src/symbol.rs @@ -161,6 +161,9 @@ symbols! { record, state, state_leaf, + + public, + private, } /// An interned string.