leo/ast/src/lib.rs

107 lines
2.8 KiB
Rust
Raw Normal View History

2021-02-02 07:26:56 +03:00
// Copyright (C) 2019-2021 Aleo Systems Inc.
2020-08-18 13:50:26 +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/>.
//! The abstract syntax tree (ast) for a Leo program.
2020-10-31 02:23:18 +03:00
//!
2020-11-12 23:00:27 +03:00
//! This module contains the [`Ast`] type, a wrapper around the [`Program`] type.
//! The [`Ast`] type is intended to be parsed and modified by different passes
//! of the Leo compiler. The Leo compiler can generate a set of R1CS constraints from any [`Ast`].
2020-08-16 05:20:41 +03:00
pub mod annotation;
pub use self::annotation::*;
2020-06-08 06:45:19 +03:00
pub mod circuits;
pub use self::circuits::*;
2020-06-08 05:47:35 +03:00
pub mod common;
pub use self::common::*;
2020-06-08 05:47:35 +03:00
pub mod errors;
pub use self::errors::*;
pub mod expression;
pub use self::expression::*;
2020-06-08 06:35:50 +03:00
pub mod functions;
pub use self::functions::*;
2020-06-08 06:35:50 +03:00
2020-08-16 01:30:19 +03:00
pub mod groups;
pub use self::groups::*;
2020-06-08 06:50:37 +03:00
pub mod imports;
pub use self::imports::*;
2020-06-08 06:50:37 +03:00
2020-08-01 05:39:30 +03:00
pub mod input;
pub use self::input::*;
2020-06-08 06:57:22 +03:00
pub mod program;
pub use self::program::*;
2020-06-08 06:57:22 +03:00
2021-03-09 23:56:35 +03:00
pub mod reducer;
pub use self::reducer::*;
2020-06-08 06:24:27 +03:00
pub mod statements;
pub use self::statements::*;
2020-06-08 06:24:27 +03:00
2020-06-08 05:47:35 +03:00
pub mod types;
pub use self::types::*;
2020-12-07 18:47:55 +03:00
mod node;
pub use node::*;
2021-02-05 04:26:52 +03:00
/// The abstract syntax tree (AST) for a Leo program.
2020-10-31 02:23:18 +03:00
///
2020-11-12 23:00:27 +03:00
/// The [`Ast`] type represents a Leo program as a series of recursive data types.
2020-10-31 02:23:18 +03:00
/// These data types form a tree that begins from a [`Program`] type root.
///
2020-11-12 23:00:27 +03:00
/// A new [`Ast`] can be created from a [`Grammar`] generated by the pest parser in the `grammar` module.
#[derive(Debug, Eq, PartialEq)]
2020-11-12 23:00:27 +03:00
pub struct Ast {
2020-10-31 03:31:09 +03:00
ast: Program,
}
2020-11-12 23:00:27 +03:00
impl Ast {
2021-03-05 01:11:17 +03:00
/// Creates a new AST from a given program tree.
2021-03-03 20:59:24 +03:00
pub fn new(program: Program) -> Self {
Self { ast: program }
}
2021-02-05 04:26:52 +03:00
/// Returns a reference to the inner program AST representation.
pub fn as_repr(&self) -> &Program {
&self.ast
}
2021-03-03 20:59:24 +03:00
pub fn into_repr(self) -> Program {
self.ast
}
2020-11-12 23:00:27 +03:00
/// Serializes the ast into a JSON string.
pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
2021-01-19 21:31:30 +03:00
serde_json::to_string_pretty(&self.ast)
}
2020-11-12 23:00:27 +03:00
/// Deserializes the JSON string into a ast.
pub fn from_json_string(json: &str) -> Result<Self, serde_json::Error> {
let ast: Program = serde_json::from_str(json)?;
Ok(Self { ast })
}
}
2021-03-03 20:59:24 +03:00
impl AsRef<Program> for Ast {
fn as_ref(&self) -> &Program {
&self.ast
}
}