mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-27 12:17:35 +03:00
clippy, use leo result everywhere
This commit is contained in:
parent
6302bda18e
commit
2d7963771f
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{Circuit, Identifier, IntegerType, Type};
|
use crate::{Circuit, Identifier, IntegerType, Type};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
@ -314,7 +314,7 @@ impl ConstInt {
|
|||||||
Type::Integer(self.get_int_type())
|
Type::Integer(self.get_int_type())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(int_type: &IntegerType, value: &str, span: &Span) -> Result<ConstInt, LeoError> {
|
pub fn parse(int_type: &IntegerType, value: &str, span: &Span) -> Result<ConstInt> {
|
||||||
Ok(match int_type {
|
Ok(match int_type {
|
||||||
IntegerType::I8 => ConstInt::I8(value.parse().map_err(|_| AsgError::invalid_int(value, span))?),
|
IntegerType::I8 => ConstInt::I8(value.parse().map_err(|_| AsgError::invalid_int(value, span))?),
|
||||||
IntegerType::I16 => ConstInt::I16(value.parse().map_err(|_| AsgError::invalid_int(value, span))?),
|
IntegerType::I16 => ConstInt::I16(value.parse().map_err(|_| AsgError::invalid_int(value, span))?),
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
use leo_ast::IntegerType;
|
use leo_ast::IntegerType;
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayAccessExpression> for ArrayAccessExpression<'
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::ArrayAccessExpression,
|
value: &leo_ast::ArrayAccessExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<ArrayAccessExpression<'a>, LeoError> {
|
) -> Result<ArrayAccessExpression<'a>> {
|
||||||
let array = <&Expression<'a>>::from_ast(
|
let array = <&Expression<'a>>::from_ast(
|
||||||
scope,
|
scope,
|
||||||
&*value.array,
|
&*value.array,
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::ArrayInitExpression,
|
value: &leo_ast::ArrayInitExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<ArrayInitExpression<'a>, LeoError> {
|
) -> Result<ArrayInitExpression<'a>> {
|
||||||
let (mut expected_item, expected_len) = match expected_type {
|
let (mut expected_item, expected_len) = match expected_type {
|
||||||
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
|
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
|
||||||
None => (None, None),
|
None => (None, None),
|
||||||
@ -86,7 +86,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
|
|||||||
.parse::<usize>()
|
.parse::<usize>()
|
||||||
.map_err(|_| AsgError::parse_dimension_error(&value.span))?)
|
.map_err(|_| AsgError::parse_dimension_error(&value.span))?)
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
|
||||||
let len = *dimensions
|
let len = *dimensions
|
||||||
.get(0)
|
.get(0)
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
use leo_ast::SpreadOrExpression;
|
use leo_ast::SpreadOrExpression;
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInlineExpression> for ArrayInlineExpression<'
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::ArrayInlineExpression,
|
value: &leo_ast::ArrayInlineExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<ArrayInlineExpression<'a>, LeoError> {
|
) -> Result<ArrayInlineExpression<'a>> {
|
||||||
let (mut expected_item, expected_len) = match expected_type {
|
let (mut expected_item, expected_len) = match expected_type {
|
||||||
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
|
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
|
||||||
None => (None, None),
|
None => (None, None),
|
||||||
@ -181,7 +181,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInlineExpression> for ArrayInlineExpression<'
|
|||||||
Ok((Cell::new(expr), true))
|
Ok((Cell::new(expr), true))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, LeoError>>()?,
|
.collect::<Result<Vec<_>>>()?,
|
||||||
};
|
};
|
||||||
if let Some(expected_len) = expected_len {
|
if let Some(expected_len) = expected_len {
|
||||||
if len != expected_len {
|
if len != expected_len {
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
use leo_ast::IntegerType;
|
use leo_ast::IntegerType;
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::ArrayRangeAccessExpression,
|
value: &leo_ast::ArrayRangeAccessExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<ArrayRangeAccessExpression<'a>, LeoError> {
|
) -> Result<ArrayRangeAccessExpression<'a>> {
|
||||||
let (expected_array, expected_len) = match expected_type.clone() {
|
let (expected_array, expected_len) = match expected_type.clone() {
|
||||||
Some(PartialType::Array(element, len)) => (Some(PartialType::Array(element, None)), len),
|
Some(PartialType::Array(element, len)) => (Some(PartialType::Array(element, None)), len),
|
||||||
None => (None, None),
|
None => (None, None),
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
pub use leo_ast::{BinaryOperation, BinaryOperationClass};
|
pub use leo_ast::{BinaryOperation, BinaryOperationClass};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ impl<'a> FromAst<'a, leo_ast::BinaryExpression> for BinaryExpression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::BinaryExpression,
|
value: &leo_ast::BinaryExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<BinaryExpression<'a>, LeoError> {
|
) -> Result<BinaryExpression<'a>> {
|
||||||
let class = value.op.class();
|
let class = value.op.class();
|
||||||
let expected_type = match class {
|
let expected_type = match class {
|
||||||
BinaryOperationClass::Boolean => match expected_type {
|
BinaryOperationClass::Boolean => match expected_type {
|
||||||
|
@ -28,7 +28,7 @@ use crate::{
|
|||||||
Type,
|
Type,
|
||||||
};
|
};
|
||||||
pub use leo_ast::{BinaryOperation, Node as AstNode};
|
pub use leo_ast::{BinaryOperation, Node as AstNode};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::CallExpression,
|
value: &leo_ast::CallExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<CallExpression<'a>, LeoError> {
|
) -> Result<CallExpression<'a>> {
|
||||||
let (target, function) = match &*value.function {
|
let (target, function) = match &*value.function {
|
||||||
leo_ast::Expression::Identifier(name) => (
|
leo_ast::Expression::Identifier(name) => (
|
||||||
None,
|
None,
|
||||||
@ -199,7 +199,7 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
|
|||||||
}
|
}
|
||||||
Ok(Cell::new(converted))
|
Ok(Cell::new(converted))
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
|
||||||
if function.is_test() {
|
if function.is_test() {
|
||||||
return Err(AsgError::call_test_function(&value.span).into());
|
return Err(AsgError::call_test_function(&value.span).into());
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
pub use leo_ast::UnaryOperation;
|
pub use leo_ast::UnaryOperation;
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ impl<'a> FromAst<'a, leo_ast::CastExpression> for CastExpression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::CastExpression,
|
value: &leo_ast::CastExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<CastExpression<'a>, LeoError> {
|
) -> Result<CastExpression<'a>> {
|
||||||
let target_type = scope.resolve_ast_type(&value.target_type, &value.span)?;
|
let target_type = scope.resolve_ast_type(&value.target_type, &value.span)?;
|
||||||
if let Some(expected_type) = &expected_type {
|
if let Some(expected_type) = &expected_type {
|
||||||
if !expected_type.matches(&target_type) {
|
if !expected_type.matches(&target_type) {
|
||||||
|
@ -28,7 +28,7 @@ use crate::{
|
|||||||
Type,
|
Type,
|
||||||
};
|
};
|
||||||
|
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -102,7 +102,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitMemberAccessExpression> for CircuitAccessEx
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::CircuitMemberAccessExpression,
|
value: &leo_ast::CircuitMemberAccessExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<CircuitAccessExpression<'a>, LeoError> {
|
) -> Result<CircuitAccessExpression<'a>> {
|
||||||
let target = <&'a Expression<'a>>::from_ast(scope, &*value.circuit, None)?;
|
let target = <&'a Expression<'a>>::from_ast(scope, &*value.circuit, None)?;
|
||||||
let circuit = match target.get_type() {
|
let circuit = match target.get_type() {
|
||||||
Some(Type::Circuit(circuit)) => circuit,
|
Some(Type::Circuit(circuit)) => circuit,
|
||||||
@ -171,7 +171,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitStaticFunctionAccessExpression> for Circuit
|
|||||||
scope: &Scope<'a>,
|
scope: &Scope<'a>,
|
||||||
value: &leo_ast::CircuitStaticFunctionAccessExpression,
|
value: &leo_ast::CircuitStaticFunctionAccessExpression,
|
||||||
expected_type: Option<PartialType>,
|
expected_type: Option<PartialType>,
|
||||||
) -> Result<CircuitAccessExpression<'a>, LeoError> {
|
) -> Result<CircuitAccessExpression<'a>> {
|
||||||
let circuit = match &*value.circuit {
|
let circuit = match &*value.circuit {
|
||||||
leo_ast::Expression::Identifier(name) => scope
|
leo_ast::Expression::Identifier(name) => scope
|
||||||
.resolve_circuit(&name.name)
|
.resolve_circuit(&name.name)
|
||||||
|
@ -28,7 +28,7 @@ use crate::{
|
|||||||
Type,
|
Type,
|
||||||
};
|
};
|
||||||
|
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use indexmap::{IndexMap, IndexSet};
|
use indexmap::{IndexMap, IndexSet};
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
@ -93,7 +93,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitInitExpression> for CircuitInitExpression<'
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::CircuitInitExpression,
|
value: &leo_ast::CircuitInitExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<CircuitInitExpression<'a>, LeoError> {
|
) -> Result<CircuitInitExpression<'a>> {
|
||||||
let circuit = scope
|
let circuit = scope
|
||||||
.resolve_circuit(&value.name.name)
|
.resolve_circuit(&value.name.name)
|
||||||
.ok_or_else(|| AsgError::unresolved_circuit(&value.name.name, &value.name.span))?;
|
.ok_or_else(|| AsgError::unresolved_circuit(&value.name.name, &value.name.span))?;
|
||||||
|
@ -28,7 +28,7 @@ use crate::{
|
|||||||
Type,
|
Type,
|
||||||
};
|
};
|
||||||
|
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -78,7 +78,7 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
|||||||
_scope: &'a Scope<'a>,
|
_scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::ValueExpression,
|
value: &leo_ast::ValueExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Constant<'a>, LeoError> {
|
) -> Result<Constant<'a>> {
|
||||||
use leo_ast::ValueExpression::*;
|
use leo_ast::ValueExpression::*;
|
||||||
Ok(match value {
|
Ok(match value {
|
||||||
Address(value, span) => {
|
Address(value, span) => {
|
||||||
|
@ -66,7 +66,7 @@ mod cast;
|
|||||||
pub use cast::*;
|
pub use cast::*;
|
||||||
|
|
||||||
use crate::{ConstValue, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, FromAst, Node, PartialType, Scope, Type};
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum Expression<'a> {
|
pub enum Expression<'a> {
|
||||||
@ -285,7 +285,7 @@ impl<'a> FromAst<'a, leo_ast::Expression> for &'a Expression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::Expression,
|
value: &leo_ast::Expression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
use leo_ast::Expression::*;
|
use leo_ast::Expression::*;
|
||||||
let expression = match value {
|
let expression = match value {
|
||||||
Identifier(identifier) => Self::from_ast(scope, identifier, expected_type)?,
|
Identifier(identifier) => Self::from_ast(scope, identifier, expected_type)?,
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::TernaryExpression,
|
value: &leo_ast::TernaryExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<TernaryExpression<'a>, LeoError> {
|
) -> Result<TernaryExpression<'a>> {
|
||||||
let if_true = Cell::new(<&Expression<'a>>::from_ast(
|
let if_true = Cell::new(<&Expression<'a>>::from_ast(
|
||||||
scope,
|
scope,
|
||||||
&*value.if_true,
|
&*value.if_true,
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ impl<'a> FromAst<'a, leo_ast::TupleAccessExpression> for TupleAccessExpression<'
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::TupleAccessExpression,
|
value: &leo_ast::TupleAccessExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<TupleAccessExpression<'a>, LeoError> {
|
) -> Result<TupleAccessExpression<'a>> {
|
||||||
let index = value
|
let index = value
|
||||||
.index
|
.index
|
||||||
.value
|
.value
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ impl<'a> FromAst<'a, leo_ast::TupleInitExpression> for TupleInitExpression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::TupleInitExpression,
|
value: &leo_ast::TupleInitExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<TupleInitExpression<'a>, LeoError> {
|
) -> Result<TupleInitExpression<'a>> {
|
||||||
let tuple_types = match expected_type {
|
let tuple_types = match expected_type {
|
||||||
Some(PartialType::Tuple(sub_types)) => Some(sub_types),
|
Some(PartialType::Tuple(sub_types)) => Some(sub_types),
|
||||||
None => None,
|
None => None,
|
||||||
@ -120,7 +120,7 @@ impl<'a> FromAst<'a, leo_ast::TupleInitExpression> for TupleInitExpression<'a> {
|
|||||||
)
|
)
|
||||||
.map(Cell::new)
|
.map(Cell::new)
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
|
||||||
Ok(TupleInitExpression {
|
Ok(TupleInitExpression {
|
||||||
parent: Cell::new(None),
|
parent: Cell::new(None),
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||||
pub use leo_ast::UnaryOperation;
|
pub use leo_ast::UnaryOperation;
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ impl<'a> FromAst<'a, leo_ast::UnaryExpression> for UnaryExpression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::UnaryExpression,
|
value: &leo_ast::UnaryExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<UnaryExpression<'a>, LeoError> {
|
) -> Result<UnaryExpression<'a>> {
|
||||||
let expected_type = match value.op {
|
let expected_type = match value.op {
|
||||||
UnaryOperation::Not => match expected_type.map(|x| x.full()).flatten() {
|
UnaryOperation::Not => match expected_type.map(|x| x.full()).flatten() {
|
||||||
Some(Type::Boolean) | None => Some(Type::Boolean),
|
Some(Type::Boolean) | None => Some(Type::Boolean),
|
||||||
|
@ -29,7 +29,7 @@ use crate::{
|
|||||||
Variable,
|
Variable,
|
||||||
};
|
};
|
||||||
|
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::Identifier,
|
value: &leo_ast::Identifier,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<&'a Expression<'a>, LeoError> {
|
) -> Result<&'a Expression<'a>> {
|
||||||
let variable = if value.name.as_ref() == "input" {
|
let variable = if value.name.as_ref() == "input" {
|
||||||
if let Some(input) = scope.resolve_input() {
|
if let Some(input) = scope.resolve_input() {
|
||||||
input.container
|
input.container
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use crate::{AsgContext, Program};
|
use crate::{AsgContext, Program};
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ pub trait ImportResolver<'a> {
|
|||||||
context: AsgContext<'a>,
|
context: AsgContext<'a>,
|
||||||
package_segments: &[&str],
|
package_segments: &[&str],
|
||||||
span: &Span,
|
span: &Span,
|
||||||
) -> Result<Option<Program<'a>>, LeoError>;
|
) -> Result<Option<Program<'a>>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NullImportResolver;
|
pub struct NullImportResolver;
|
||||||
@ -40,7 +40,7 @@ impl<'a> ImportResolver<'a> for NullImportResolver {
|
|||||||
_context: AsgContext<'a>,
|
_context: AsgContext<'a>,
|
||||||
_package_segments: &[&str],
|
_package_segments: &[&str],
|
||||||
_span: &Span,
|
_span: &Span,
|
||||||
) -> Result<Option<Program<'a>>, LeoError> {
|
) -> Result<Option<Program<'a>>> {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ impl<'a, 'b, T: ImportResolver<'b>> ImportResolver<'b> for CoreImportResolver<'a
|
|||||||
context: AsgContext<'b>,
|
context: AsgContext<'b>,
|
||||||
package_segments: &[&str],
|
package_segments: &[&str],
|
||||||
span: &Span,
|
span: &Span,
|
||||||
) -> Result<Option<Program<'b>>, LeoError> {
|
) -> Result<Option<Program<'b>>> {
|
||||||
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" {
|
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" {
|
||||||
Ok(crate::resolve_core_module(context, &*package_segments[1..].join("."))?)
|
Ok(crate::resolve_core_module(context, &*package_segments[1..].join("."))?)
|
||||||
} else {
|
} else {
|
||||||
@ -84,7 +84,7 @@ impl<'a> ImportResolver<'a> for MockedImportResolver<'a> {
|
|||||||
_context: AsgContext<'a>,
|
_context: AsgContext<'a>,
|
||||||
package_segments: &[&str],
|
package_segments: &[&str],
|
||||||
_span: &Span,
|
_span: &Span,
|
||||||
) -> Result<Option<Program<'a>>, LeoError> {
|
) -> Result<Option<Program<'a>>> {
|
||||||
Ok(self.packages.get(&package_segments.join(".")).cloned())
|
Ok(self.packages.get(&package_segments.join(".")).cloned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ pub mod context;
|
|||||||
pub use context::*;
|
pub use context::*;
|
||||||
|
|
||||||
pub use leo_ast::{Ast, Identifier};
|
pub use leo_ast::{Ast, Identifier};
|
||||||
use leo_errors::LeoError;
|
use leo_errors::Result;
|
||||||
|
|
||||||
/// The abstract semantic graph (ASG) for a Leo program.
|
/// The abstract semantic graph (ASG) for a Leo program.
|
||||||
///
|
///
|
||||||
@ -92,7 +92,7 @@ impl<'a> Asg<'a> {
|
|||||||
context: AsgContext<'a>,
|
context: AsgContext<'a>,
|
||||||
ast: Y,
|
ast: Y,
|
||||||
resolver: &mut T,
|
resolver: &mut T,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
context,
|
context,
|
||||||
asg: Program::new(context, ast.as_ref(), resolver)?,
|
asg: Program::new(context, ast.as_ref(), resolver)?,
|
||||||
@ -125,7 +125,7 @@ pub fn load_asg<'a, T: ImportResolver<'a>>(
|
|||||||
context: AsgContext<'a>,
|
context: AsgContext<'a>,
|
||||||
content: &str,
|
content: &str,
|
||||||
resolver: &mut T,
|
resolver: &mut T,
|
||||||
) -> Result<Program<'a>, LeoError> {
|
) -> Result<Program<'a>> {
|
||||||
// Parses the Leo file and constructs a grammar ast.
|
// Parses the Leo file and constructs a grammar ast.
|
||||||
let ast = leo_parser::parse_ast("input.leo", content)?;
|
let ast = leo_parser::parse_ast("input.leo", content)?;
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{AsgContextInner, Circuit, Expression, Function, PartialType, Scope, Statement, Variable};
|
use crate::{AsgContextInner, Circuit, Expression, Function, PartialType, Scope, Statement, Variable};
|
||||||
|
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
/// A node in the abstract semantic graph.
|
/// A node in the abstract semantic graph.
|
||||||
pub trait Node {
|
pub trait Node {
|
||||||
@ -26,7 +26,7 @@ pub trait Node {
|
|||||||
pub(super) trait FromAst<'a, T: leo_ast::Node + 'static>: Sized {
|
pub(super) trait FromAst<'a, T: leo_ast::Node + 'static>: Sized {
|
||||||
// expected_type contract: if present, output expression must be of type expected_type.
|
// expected_type contract: if present, output expression must be of type expected_type.
|
||||||
// type of an element may NEVER be None unless it is functionally a non-expression. (static call targets, function ref call targets are not expressions)
|
// type of an element may NEVER be None unless it is functionally a non-expression. (static call targets, function ref call targets are not expressions)
|
||||||
fn from_ast(scope: &'a Scope<'a>, value: &T, expected_type: Option<PartialType<'a>>) -> Result<Self, LeoError>;
|
fn from_ast(scope: &'a Scope<'a>, value: &T, expected_type: Option<PartialType<'a>>) -> Result<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ArenaNode<'a> {
|
pub enum ArenaNode<'a> {
|
||||||
|
@ -15,8 +15,8 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::Program;
|
use crate::Program;
|
||||||
use leo_errors::LeoError;
|
use leo_errors::Result;
|
||||||
|
|
||||||
pub trait AsgPass<'a> {
|
pub trait AsgPass<'a> {
|
||||||
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, LeoError>;
|
fn do_pass(asg: Program<'a>) -> Result<Program<'a>>;
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,12 @@
|
|||||||
// TODO (protryon): We should merge this with core
|
// TODO (protryon): We should merge this with core
|
||||||
|
|
||||||
use crate::{AsgContext, Program};
|
use crate::{AsgContext, Program};
|
||||||
use leo_errors::LeoError;
|
use leo_errors::Result;
|
||||||
|
|
||||||
// TODO (protryon): Make asg deep copy so we can cache resolved core modules
|
// TODO (protryon): Make asg deep copy so we can cache resolved core modules
|
||||||
// TODO (protryon): Figure out how to do headers without bogus returns
|
// TODO (protryon): Figure out how to do headers without bogus returns
|
||||||
|
|
||||||
pub fn resolve_core_module<'a>(context: AsgContext<'a>, module: &str) -> Result<Option<Program<'a>>, LeoError> {
|
pub fn resolve_core_module<'a>(context: AsgContext<'a>, module: &str) -> Result<Option<Program<'a>>> {
|
||||||
match module {
|
match module {
|
||||||
"unstable.blake2s" => {
|
"unstable.blake2s" => {
|
||||||
let asg = crate::load_asg(
|
let asg = crate::load_asg(
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{Function, Identifier, Node, Scope, Type};
|
use crate::{Function, Identifier, Node, Scope, Type};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@ -54,7 +54,7 @@ impl<'a> Node for Circuit<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Circuit<'a> {
|
impl<'a> Circuit<'a> {
|
||||||
pub(super) fn init(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>, LeoError> {
|
pub(super) fn init(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>> {
|
||||||
let new_scope = scope.make_subscope();
|
let new_scope = scope.make_subscope();
|
||||||
|
|
||||||
let circuit = scope.context.alloc_circuit(Circuit {
|
let circuit = scope.context.alloc_circuit(Circuit {
|
||||||
@ -85,7 +85,7 @@ impl<'a> Circuit<'a> {
|
|||||||
Ok(circuit)
|
Ok(circuit)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn init_member(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>, LeoError> {
|
pub(super) fn init_member(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>> {
|
||||||
let new_scope = scope.make_subscope();
|
let new_scope = scope.make_subscope();
|
||||||
let circuits = scope.circuits.borrow();
|
let circuits = scope.circuits.borrow();
|
||||||
|
|
||||||
@ -118,7 +118,7 @@ impl<'a> Circuit<'a> {
|
|||||||
Ok(circuit)
|
Ok(circuit)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn fill_from_ast(self: &'a Circuit<'a>, value: &leo_ast::Circuit) -> Result<(), LeoError> {
|
pub(super) fn fill_from_ast(self: &'a Circuit<'a>, value: &leo_ast::Circuit) -> Result<()> {
|
||||||
for member in value.members.iter() {
|
for member in value.members.iter() {
|
||||||
match member {
|
match member {
|
||||||
leo_ast::CircuitMember::CircuitVariable(..) => {}
|
leo_ast::CircuitMember::CircuitVariable(..) => {}
|
||||||
|
@ -29,7 +29,7 @@ use crate::{
|
|||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
pub use leo_ast::Annotation;
|
pub use leo_ast::Annotation;
|
||||||
use leo_ast::FunctionInput;
|
use leo_ast::FunctionInput;
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ impl<'a> PartialEq for Function<'a> {
|
|||||||
impl<'a> Eq for Function<'a> {}
|
impl<'a> Eq for Function<'a> {}
|
||||||
|
|
||||||
impl<'a> Function<'a> {
|
impl<'a> Function<'a> {
|
||||||
pub(crate) fn init(scope: &'a Scope<'a>, value: &leo_ast::Function) -> Result<&'a Function<'a>, LeoError> {
|
pub(crate) fn init(scope: &'a Scope<'a>, value: &leo_ast::Function) -> Result<&'a Function<'a>> {
|
||||||
let output: Type<'a> = value
|
let output: Type<'a> = value
|
||||||
.output
|
.output
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@ -126,7 +126,7 @@ impl<'a> Function<'a> {
|
|||||||
Ok(function)
|
Ok(function)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn fill_from_ast(self: &'a Function<'a>, value: &leo_ast::Function) -> Result<(), LeoError> {
|
pub(super) fn fill_from_ast(self: &'a Function<'a>, value: &leo_ast::Function) -> Result<()> {
|
||||||
if self.qualifier != FunctionQualifier::Static {
|
if self.qualifier != FunctionQualifier::Static {
|
||||||
let circuit = self.circuit.get();
|
let circuit = self.circuit.get();
|
||||||
let self_variable = self.scope.context.alloc_variable(RefCell::new(crate::InnerVariable {
|
let self_variable = self.scope.context.alloc_variable(RefCell::new(crate::InnerVariable {
|
||||||
|
@ -26,7 +26,7 @@ pub use function::*;
|
|||||||
|
|
||||||
use crate::{node::FromAst, ArenaNode, AsgContext, DefinitionStatement, ImportResolver, Input, Scope, Statement};
|
use crate::{node::FromAst, ArenaNode, AsgContext, DefinitionStatement, ImportResolver, Input, Scope, Statement};
|
||||||
use leo_ast::{Identifier, PackageAccess, PackageOrPackages};
|
use leo_ast::{Identifier, PackageAccess, PackageOrPackages};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
@ -137,7 +137,7 @@ impl<'a> Program<'a> {
|
|||||||
context: AsgContext<'a>,
|
context: AsgContext<'a>,
|
||||||
program: &leo_ast::Program,
|
program: &leo_ast::Program,
|
||||||
import_resolver: &mut T,
|
import_resolver: &mut T,
|
||||||
) -> Result<Program<'a>, LeoError> {
|
) -> Result<Program<'a>> {
|
||||||
// Recursively extract imported symbols.
|
// Recursively extract imported symbols.
|
||||||
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
|
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
|
||||||
for import in program.imports.iter() {
|
for import in program.imports.iter() {
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{AsgContext, Circuit, DefinitionStatement, Function, Input, Type, Variable};
|
use crate::{AsgContext, Circuit, DefinitionStatement, Function, Input, Type, Variable};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
@ -174,7 +174,7 @@ impl<'a> Scope<'a> {
|
|||||||
///
|
///
|
||||||
/// Returns the type returned by the current scope.
|
/// Returns the type returned by the current scope.
|
||||||
///
|
///
|
||||||
pub fn resolve_ast_type(&self, type_: &leo_ast::Type, span: &Span) -> Result<Type<'a>, LeoError> {
|
pub fn resolve_ast_type(&self, type_: &leo_ast::Type, span: &Span) -> Result<Type<'a>> {
|
||||||
use leo_ast::Type::*;
|
use leo_ast::Type::*;
|
||||||
Ok(match type_ {
|
Ok(match type_ {
|
||||||
Address => Type::Address,
|
Address => Type::Address,
|
||||||
@ -198,7 +198,7 @@ impl<'a> Scope<'a> {
|
|||||||
sub_types
|
sub_types
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| self.resolve_ast_type(x, span))
|
.map(|x| self.resolve_ast_type(x, span))
|
||||||
.collect::<Result<Vec<_>, LeoError>>()?,
|
.collect::<Result<Vec<_>>>()?,
|
||||||
),
|
),
|
||||||
Circuit(name) if name.name.as_ref() == "Self" => Type::Circuit(
|
Circuit(name) if name.name.as_ref() == "Self" => Type::Circuit(
|
||||||
self.resolve_circuit_self()
|
self.resolve_circuit_self()
|
||||||
|
@ -32,7 +32,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
pub use leo_ast::AssignOperation;
|
pub use leo_ast::AssignOperation;
|
||||||
use leo_ast::AssigneeAccess as AstAssigneeAccess;
|
use leo_ast::AssigneeAccess as AstAssigneeAccess;
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
statement: &leo_ast::AssignStatement,
|
statement: &leo_ast::AssignStatement,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
let (name, span) = (
|
let (name, span) = (
|
||||||
&statement.assignee.identifier.name.clone(),
|
&statement.assignee.identifier.name.clone(),
|
||||||
&statement.assignee.identifier.span,
|
&statement.assignee.identifier.span,
|
||||||
@ -99,13 +99,13 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
|||||||
let index_type = Some(PartialType::Integer(None, Some(IntegerType::U32)));
|
let index_type = Some(PartialType::Integer(None, Some(IntegerType::U32)));
|
||||||
let left = left
|
let left = left
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|left: &leo_ast::Expression| -> Result<&'a Expression<'a>, LeoError> {
|
.map(|left: &leo_ast::Expression| -> Result<&'a Expression<'a>> {
|
||||||
<&Expression<'a>>::from_ast(scope, left, index_type.clone())
|
<&Expression<'a>>::from_ast(scope, left, index_type.clone())
|
||||||
})
|
})
|
||||||
.transpose()?;
|
.transpose()?;
|
||||||
let right = right
|
let right = right
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|right: &leo_ast::Expression| -> Result<&'a Expression<'a>, LeoError> {
|
.map(|right: &leo_ast::Expression| -> Result<&'a Expression<'a>> {
|
||||||
<&Expression<'a>>::from_ast(scope, right, index_type)
|
<&Expression<'a>>::from_ast(scope, right, index_type)
|
||||||
})
|
})
|
||||||
.transpose()?;
|
.transpose()?;
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{FromAst, Node, PartialType, Scope, Statement};
|
use crate::{FromAst, Node, PartialType, Scope, Statement};
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ impl<'a> FromAst<'a, leo_ast::Block> for BlockStatement<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
statement: &leo_ast::Block,
|
statement: &leo_ast::Block,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
let new_scope = scope.make_subscope();
|
let new_scope = scope.make_subscope();
|
||||||
|
|
||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{BlockStatement, Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
use crate::{BlockStatement, Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ impl<'a> FromAst<'a, leo_ast::ConditionalStatement> for ConditionalStatement<'a>
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
statement: &leo_ast::ConditionalStatement,
|
statement: &leo_ast::ConditionalStatement,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
let condition = <&Expression<'a>>::from_ast(scope, &statement.condition, Some(Type::Boolean.into()))?;
|
let condition = <&Expression<'a>>::from_ast(scope, &statement.condition, Some(Type::Boolean.into()))?;
|
||||||
let result = scope.context.alloc_statement(Statement::Block(BlockStatement::from_ast(
|
let result = scope.context.alloc_statement(Statement::Block(BlockStatement::from_ast(
|
||||||
scope,
|
scope,
|
||||||
@ -49,7 +49,7 @@ impl<'a> FromAst<'a, leo_ast::ConditionalStatement> for ConditionalStatement<'a>
|
|||||||
let next = statement
|
let next = statement
|
||||||
.next
|
.next
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.map(|next| -> Result<&'a Statement<'a>, LeoError> { <&'a Statement<'a>>::from_ast(scope, next, None) })
|
.map(|next| -> Result<&'a Statement<'a>> { <&'a Statement<'a>>::from_ast(scope, next, None) })
|
||||||
.transpose()?;
|
.transpose()?;
|
||||||
|
|
||||||
Ok(ConditionalStatement {
|
Ok(ConditionalStatement {
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{CharValue, Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
use crate::{CharValue, Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||||
use leo_ast::ConsoleFunction as AstConsoleFunction;
|
use leo_ast::ConsoleFunction as AstConsoleFunction;
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ impl<'a> FromAst<'a, leo_ast::ConsoleArgs> for ConsoleArgs<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::ConsoleArgs,
|
value: &leo_ast::ConsoleArgs,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
let mut parameters = vec![];
|
let mut parameters = vec![];
|
||||||
for parameter in value.parameters.iter() {
|
for parameter in value.parameters.iter() {
|
||||||
parameters.push(Cell::new(<&Expression<'a>>::from_ast(scope, parameter, None)?));
|
parameters.push(Cell::new(<&Expression<'a>>::from_ast(scope, parameter, None)?));
|
||||||
@ -81,7 +81,7 @@ impl<'a> FromAst<'a, leo_ast::ConsoleStatement> for ConsoleStatement<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
statement: &leo_ast::ConsoleStatement,
|
statement: &leo_ast::ConsoleStatement,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
Ok(ConsoleStatement {
|
Ok(ConsoleStatement {
|
||||||
parent: Cell::new(None),
|
parent: Cell::new(None),
|
||||||
span: Some(statement.span.clone()),
|
span: Some(statement.span.clone()),
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{Expression, ExpressionNode, FromAst, InnerVariable, Node, PartialType, Scope, Statement, Type, Variable};
|
use crate::{Expression, ExpressionNode, FromAst, InnerVariable, Node, PartialType, Scope, Statement, Type, Variable};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
statement: &leo_ast::DefinitionStatement,
|
statement: &leo_ast::DefinitionStatement,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
let type_ = statement
|
let type_ = statement
|
||||||
.type_
|
.type_
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{Expression, FromAst, Node, PartialType, Scope, Statement};
|
use crate::{Expression, FromAst, Node, PartialType, Scope, Statement};
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ impl<'a> FromAst<'a, leo_ast::ExpressionStatement> for ExpressionStatement<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
statement: &leo_ast::ExpressionStatement,
|
statement: &leo_ast::ExpressionStatement,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
let expression = <&Expression<'a>>::from_ast(scope, &statement.expression, None)?;
|
let expression = <&Expression<'a>>::from_ast(scope, &statement.expression, None)?;
|
||||||
|
|
||||||
Ok(ExpressionStatement {
|
Ok(ExpressionStatement {
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
use leo_ast::IntegerType;
|
use leo_ast::IntegerType;
|
||||||
|
|
||||||
use crate::{Expression, ExpressionNode, FromAst, InnerVariable, Node, PartialType, Scope, Statement, Variable};
|
use crate::{Expression, ExpressionNode, FromAst, InnerVariable, Node, PartialType, Scope, Statement, Variable};
|
||||||
use leo_errors::{AsgError, LeoError, Span};
|
use leo_errors::{AsgError, Result, Span};
|
||||||
|
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ impl<'a> FromAst<'a, leo_ast::IterationStatement> for &'a Statement<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
statement: &leo_ast::IterationStatement,
|
statement: &leo_ast::IterationStatement,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
let expected_index_type = Some(PartialType::Integer(Some(IntegerType::U32), None));
|
let expected_index_type = Some(PartialType::Integer(Some(IntegerType::U32), None));
|
||||||
let start = <&Expression<'a>>::from_ast(scope, &statement.start, expected_index_type.clone())?;
|
let start = <&Expression<'a>>::from_ast(scope, &statement.start, expected_index_type.clone())?;
|
||||||
let stop = <&Expression<'a>>::from_ast(scope, &statement.stop, expected_index_type)?;
|
let stop = <&Expression<'a>>::from_ast(scope, &statement.stop, expected_index_type)?;
|
||||||
|
@ -43,7 +43,7 @@ mod return_;
|
|||||||
pub use return_::*;
|
pub use return_::*;
|
||||||
|
|
||||||
use crate::{FromAst, Node, PartialType, Scope};
|
use crate::{FromAst, Node, PartialType, Scope};
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum Statement<'a> {
|
pub enum Statement<'a> {
|
||||||
@ -80,7 +80,7 @@ impl<'a> FromAst<'a, leo_ast::Statement> for &'a Statement<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
value: &leo_ast::Statement,
|
value: &leo_ast::Statement,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<&'a Statement<'a>, LeoError> {
|
) -> Result<&'a Statement<'a>> {
|
||||||
use leo_ast::Statement::*;
|
use leo_ast::Statement::*;
|
||||||
Ok(match value {
|
Ok(match value {
|
||||||
Return(statement) => scope
|
Return(statement) => scope
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
use crate::{Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -36,7 +36,7 @@ impl<'a> FromAst<'a, leo_ast::ReturnStatement> for ReturnStatement<'a> {
|
|||||||
scope: &'a Scope<'a>,
|
scope: &'a Scope<'a>,
|
||||||
statement: &leo_ast::ReturnStatement,
|
statement: &leo_ast::ReturnStatement,
|
||||||
_expected_type: Option<PartialType<'a>>,
|
_expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<Self, LeoError> {
|
) -> Result<Self> {
|
||||||
let return_type: Option<Type> = scope
|
let return_type: Option<Type> = scope
|
||||||
.resolve_current_function()
|
.resolve_current_function()
|
||||||
.map(|x| x.output.clone())
|
.map(|x| x.output.clone())
|
||||||
|
@ -62,7 +62,7 @@ pub use self::types::*;
|
|||||||
mod node;
|
mod node;
|
||||||
pub use node::*;
|
pub use node::*;
|
||||||
|
|
||||||
use leo_errors::{AstError, LeoError};
|
use leo_errors::{AstError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ impl Ast {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Mutates the program ast by preforming canonicalization on it.
|
/// Mutates the program ast by preforming canonicalization on it.
|
||||||
pub fn canonicalize(&mut self) -> Result<(), LeoError> {
|
pub fn canonicalize(&mut self) -> Result<()> {
|
||||||
self.ast = ReconstructingDirector::new(Canonicalizer::default()).reduce_program(self.as_repr())?;
|
self.ast = ReconstructingDirector::new(Canonicalizer::default()).reduce_program(self.as_repr())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -99,13 +99,13 @@ impl Ast {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes the ast into a JSON string.
|
/// Serializes the ast into a JSON string.
|
||||||
pub fn to_json_string(&self) -> Result<String, LeoError> {
|
pub fn to_json_string(&self) -> Result<String> {
|
||||||
Ok(serde_json::to_string_pretty(&self.ast)
|
Ok(serde_json::to_string_pretty(&self.ast)
|
||||||
.map_err(|e| AstError::failed_to_convert_ast_to_json_string(&e, Backtrace::new()))?)
|
.map_err(|e| AstError::failed_to_convert_ast_to_json_string(&e, Backtrace::new()))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes the ast into a JSON file.
|
/// Serializes the ast into a JSON file.
|
||||||
pub fn to_json_file(&self, mut path: std::path::PathBuf, file_name: &str) -> Result<(), LeoError> {
|
pub fn to_json_file(&self, mut path: std::path::PathBuf, file_name: &str) -> Result<()> {
|
||||||
path.push(file_name);
|
path.push(file_name);
|
||||||
let file = std::fs::File::create(&path)
|
let file = std::fs::File::create(&path)
|
||||||
.map_err(|e| AstError::failed_to_create_ast_json_file(&path, &e, Backtrace::new()))?;
|
.map_err(|e| AstError::failed_to_create_ast_json_file(&path, &e, Backtrace::new()))?;
|
||||||
@ -115,14 +115,14 @@ impl Ast {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes the JSON string into a ast.
|
/// Deserializes the JSON string into a ast.
|
||||||
pub fn from_json_string(json: &str) -> Result<Self, LeoError> {
|
pub fn from_json_string(json: &str) -> Result<Self> {
|
||||||
let ast: Program = serde_json::from_str(json)
|
let ast: Program = serde_json::from_str(json)
|
||||||
.map_err(|e| AstError::failed_to_read_json_string_to_ast(&e, Backtrace::new()))?;
|
.map_err(|e| AstError::failed_to_read_json_string_to_ast(&e, Backtrace::new()))?;
|
||||||
Ok(Self { ast })
|
Ok(Self { ast })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes the JSON string into a ast from a file.
|
/// Deserializes the JSON string into a ast from a file.
|
||||||
pub fn from_json_file(path: std::path::PathBuf) -> Result<Self, LeoError> {
|
pub fn from_json_file(path: std::path::PathBuf) -> Result<Self> {
|
||||||
let data = std::fs::read_to_string(&path)
|
let data = std::fs::read_to_string(&path)
|
||||||
.map_err(|e| AstError::failed_to_read_json_file(&path, &e, Backtrace::new()))?;
|
.map_err(|e| AstError::failed_to_read_json_file(&path, &e, Backtrace::new()))?;
|
||||||
Self::from_json_string(&data)
|
Self::from_json_string(&data)
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use leo_errors::{AstError, LeoError, Span};
|
use leo_errors::{AstError, Result, Span};
|
||||||
|
|
||||||
/// Replace Self when it is in a enclosing circuit type.
|
/// Replace Self when it is in a enclosing circuit type.
|
||||||
/// Error when Self is outside an enclosing circuit type.
|
/// Error when Self is outside an enclosing circuit type.
|
||||||
@ -44,7 +44,7 @@ impl Canonicalizer {
|
|||||||
start: Expression,
|
start: Expression,
|
||||||
accesses: &[AssigneeAccess],
|
accesses: &[AssigneeAccess],
|
||||||
span: &Span,
|
span: &Span,
|
||||||
) -> Result<Box<Expression>, LeoError> {
|
) -> Result<Box<Expression>> {
|
||||||
let mut left = Box::new(start);
|
let mut left = Box::new(start);
|
||||||
|
|
||||||
for access in accesses.iter() {
|
for access in accesses.iter() {
|
||||||
@ -84,7 +84,7 @@ impl Canonicalizer {
|
|||||||
Ok(left)
|
Ok(left)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compound_operation_converstion(&mut self, operation: &AssignOperation) -> Result<BinaryOperation, LeoError> {
|
pub fn compound_operation_converstion(&mut self, operation: &AssignOperation) -> Result<BinaryOperation> {
|
||||||
match operation {
|
match operation {
|
||||||
AssignOperation::Assign => unreachable!(),
|
AssignOperation::Assign => unreachable!(),
|
||||||
AssignOperation::Add => Ok(BinaryOperation::Add),
|
AssignOperation::Add => Ok(BinaryOperation::Add),
|
||||||
@ -465,7 +465,7 @@ impl ReconstructingReducer for Canonicalizer {
|
|||||||
self.in_circuit = !self.in_circuit;
|
self.in_circuit = !self.in_circuit;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_type(&mut self, _type_: &Type, new: Type, span: &Span) -> Result<Type, LeoError> {
|
fn reduce_type(&mut self, _type_: &Type, new: Type, span: &Span) -> Result<Type> {
|
||||||
match new {
|
match new {
|
||||||
Type::Array(type_, mut dimensions) => {
|
Type::Array(type_, mut dimensions) => {
|
||||||
if dimensions.is_zero() {
|
if dimensions.is_zero() {
|
||||||
@ -491,7 +491,7 @@ impl ReconstructingReducer for Canonicalizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, LeoError> {
|
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression> {
|
||||||
if string.is_empty() {
|
if string.is_empty() {
|
||||||
return Err(AstError::empty_string(span).into());
|
return Err(AstError::empty_string(span).into());
|
||||||
}
|
}
|
||||||
@ -550,7 +550,7 @@ impl ReconstructingReducer for Canonicalizer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
array_init: &ArrayInitExpression,
|
array_init: &ArrayInitExpression,
|
||||||
element: Expression,
|
element: Expression,
|
||||||
) -> Result<ArrayInitExpression, LeoError> {
|
) -> Result<ArrayInitExpression> {
|
||||||
if array_init.dimensions.is_zero() {
|
if array_init.dimensions.is_zero() {
|
||||||
return Err(AstError::invalid_array_dimension_size(&array_init.span).into());
|
return Err(AstError::invalid_array_dimension_size(&array_init.span).into());
|
||||||
}
|
}
|
||||||
@ -599,7 +599,7 @@ impl ReconstructingReducer for Canonicalizer {
|
|||||||
assign: &AssignStatement,
|
assign: &AssignStatement,
|
||||||
assignee: Assignee,
|
assignee: Assignee,
|
||||||
value: Expression,
|
value: Expression,
|
||||||
) -> Result<AssignStatement, LeoError> {
|
) -> Result<AssignStatement> {
|
||||||
match value {
|
match value {
|
||||||
value if assign.operation != AssignOperation::Assign => {
|
value if assign.operation != AssignOperation::Assign => {
|
||||||
let left = self.canonicalize_accesses(
|
let left = self.canonicalize_accesses(
|
||||||
@ -641,7 +641,7 @@ impl ReconstructingReducer for Canonicalizer {
|
|||||||
input: Vec<FunctionInput>,
|
input: Vec<FunctionInput>,
|
||||||
output: Option<Type>,
|
output: Option<Type>,
|
||||||
block: Block,
|
block: Block,
|
||||||
) -> Result<Function, LeoError> {
|
) -> Result<Function> {
|
||||||
let new_output = match output {
|
let new_output = match output {
|
||||||
None => Some(Type::Tuple(vec![])),
|
None => Some(Type::Tuple(vec![])),
|
||||||
_ => output,
|
_ => output,
|
||||||
@ -662,7 +662,7 @@ impl ReconstructingReducer for Canonicalizer {
|
|||||||
_circuit: &Circuit,
|
_circuit: &Circuit,
|
||||||
circuit_name: Identifier,
|
circuit_name: Identifier,
|
||||||
members: Vec<CircuitMember>,
|
members: Vec<CircuitMember>,
|
||||||
) -> Result<Circuit, LeoError> {
|
) -> Result<Circuit> {
|
||||||
self.circuit_name = Some(circuit_name.clone());
|
self.circuit_name = Some(circuit_name.clone());
|
||||||
let circ = Circuit {
|
let circ = Circuit {
|
||||||
circuit_name,
|
circuit_name,
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use leo_errors::{AstError, LeoError, Span};
|
use leo_errors::{AstError, Result, Span};
|
||||||
|
|
||||||
pub struct ReconstructingDirector<R: ReconstructingReducer> {
|
pub struct ReconstructingDirector<R: ReconstructingReducer> {
|
||||||
reducer: R,
|
reducer: R,
|
||||||
@ -30,7 +30,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
Self { reducer }
|
Self { reducer }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_type(&mut self, type_: &Type, span: &Span) -> Result<Type, LeoError> {
|
pub fn reduce_type(&mut self, type_: &Type, span: &Span) -> Result<Type> {
|
||||||
let new = match type_ {
|
let new = match type_ {
|
||||||
Type::Array(type_, dimensions) => Type::Array(Box::new(self.reduce_type(type_, span)?), dimensions.clone()),
|
Type::Array(type_, dimensions) => Type::Array(Box::new(self.reduce_type(type_, span)?), dimensions.clone()),
|
||||||
Type::Tuple(types) => {
|
Type::Tuple(types) => {
|
||||||
@ -49,7 +49,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Expressions
|
// Expressions
|
||||||
pub fn reduce_expression(&mut self, expression: &Expression) -> Result<Expression, LeoError> {
|
pub fn reduce_expression(&mut self, expression: &Expression) -> Result<Expression> {
|
||||||
let new = match expression {
|
let new = match expression {
|
||||||
Expression::Identifier(identifier) => Expression::Identifier(self.reduce_identifier(identifier)?),
|
Expression::Identifier(identifier) => Expression::Identifier(self.reduce_identifier(identifier)?),
|
||||||
Expression::Value(value) => self.reduce_value(value)?,
|
Expression::Value(value) => self.reduce_value(value)?,
|
||||||
@ -82,15 +82,15 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_expression(expression, new)
|
self.reducer.reduce_expression(expression, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, LeoError> {
|
pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier> {
|
||||||
self.reducer.reduce_identifier(identifier)
|
self.reducer.reduce_identifier(identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, LeoError> {
|
pub fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple> {
|
||||||
self.reducer.reduce_group_tuple(group_tuple)
|
self.reducer.reduce_group_tuple(group_tuple)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_group_value(&mut self, group_value: &GroupValue) -> Result<GroupValue, LeoError> {
|
pub fn reduce_group_value(&mut self, group_value: &GroupValue) -> Result<GroupValue> {
|
||||||
let new = match group_value {
|
let new = match group_value {
|
||||||
GroupValue::Tuple(group_tuple) => GroupValue::Tuple(Box::new(self.reduce_group_tuple(group_tuple)?)),
|
GroupValue::Tuple(group_tuple) => GroupValue::Tuple(Box::new(self.reduce_group_tuple(group_tuple)?)),
|
||||||
_ => group_value.clone(),
|
_ => group_value.clone(),
|
||||||
@ -99,11 +99,11 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_group_value(group_value, new)
|
self.reducer.reduce_group_value(group_value, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, LeoError> {
|
pub fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression> {
|
||||||
self.reducer.reduce_string(string, span)
|
self.reducer.reduce_string(string, span)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<Expression, LeoError> {
|
pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<Expression> {
|
||||||
let new = match value {
|
let new = match value {
|
||||||
ValueExpression::Group(group_value) => {
|
ValueExpression::Group(group_value) => {
|
||||||
Expression::Value(ValueExpression::Group(Box::new(self.reduce_group_value(group_value)?)))
|
Expression::Value(ValueExpression::Group(Box::new(self.reduce_group_value(group_value)?)))
|
||||||
@ -115,20 +115,20 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_value(value, new)
|
self.reducer.reduce_value(value, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_binary(&mut self, binary: &BinaryExpression) -> Result<BinaryExpression, LeoError> {
|
pub fn reduce_binary(&mut self, binary: &BinaryExpression) -> Result<BinaryExpression> {
|
||||||
let left = self.reduce_expression(&binary.left)?;
|
let left = self.reduce_expression(&binary.left)?;
|
||||||
let right = self.reduce_expression(&binary.right)?;
|
let right = self.reduce_expression(&binary.right)?;
|
||||||
|
|
||||||
self.reducer.reduce_binary(binary, left, right, binary.op.clone())
|
self.reducer.reduce_binary(binary, left, right, binary.op.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_unary(&mut self, unary: &UnaryExpression) -> Result<UnaryExpression, LeoError> {
|
pub fn reduce_unary(&mut self, unary: &UnaryExpression) -> Result<UnaryExpression> {
|
||||||
let inner = self.reduce_expression(&unary.inner)?;
|
let inner = self.reduce_expression(&unary.inner)?;
|
||||||
|
|
||||||
self.reducer.reduce_unary(unary, inner, unary.op.clone())
|
self.reducer.reduce_unary(unary, inner, unary.op.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_ternary(&mut self, ternary: &TernaryExpression) -> Result<TernaryExpression, LeoError> {
|
pub fn reduce_ternary(&mut self, ternary: &TernaryExpression) -> Result<TernaryExpression> {
|
||||||
let condition = self.reduce_expression(&ternary.condition)?;
|
let condition = self.reduce_expression(&ternary.condition)?;
|
||||||
let if_true = self.reduce_expression(&ternary.if_true)?;
|
let if_true = self.reduce_expression(&ternary.if_true)?;
|
||||||
let if_false = self.reduce_expression(&ternary.if_false)?;
|
let if_false = self.reduce_expression(&ternary.if_false)?;
|
||||||
@ -136,17 +136,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_ternary(ternary, condition, if_true, if_false)
|
self.reducer.reduce_ternary(ternary, condition, if_true, if_false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_cast(&mut self, cast: &CastExpression) -> Result<CastExpression, LeoError> {
|
pub fn reduce_cast(&mut self, cast: &CastExpression) -> Result<CastExpression> {
|
||||||
let inner = self.reduce_expression(&cast.inner)?;
|
let inner = self.reduce_expression(&cast.inner)?;
|
||||||
let target_type = self.reduce_type(&cast.target_type, &cast.span)?;
|
let target_type = self.reduce_type(&cast.target_type, &cast.span)?;
|
||||||
|
|
||||||
self.reducer.reduce_cast(cast, inner, target_type)
|
self.reducer.reduce_cast(cast, inner, target_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_array_inline(
|
pub fn reduce_array_inline(&mut self, array_inline: &ArrayInlineExpression) -> Result<ArrayInlineExpression> {
|
||||||
&mut self,
|
|
||||||
array_inline: &ArrayInlineExpression,
|
|
||||||
) -> Result<ArrayInlineExpression, LeoError> {
|
|
||||||
let mut elements = vec![];
|
let mut elements = vec![];
|
||||||
for element in array_inline.elements.iter() {
|
for element in array_inline.elements.iter() {
|
||||||
let reduced_element = match element {
|
let reduced_element = match element {
|
||||||
@ -164,16 +161,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_array_inline(array_inline, elements)
|
self.reducer.reduce_array_inline(array_inline, elements)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_array_init(&mut self, array_init: &ArrayInitExpression) -> Result<ArrayInitExpression, LeoError> {
|
pub fn reduce_array_init(&mut self, array_init: &ArrayInitExpression) -> Result<ArrayInitExpression> {
|
||||||
let element = self.reduce_expression(&array_init.element)?;
|
let element = self.reduce_expression(&array_init.element)?;
|
||||||
|
|
||||||
self.reducer.reduce_array_init(array_init, element)
|
self.reducer.reduce_array_init(array_init, element)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_array_access(
|
pub fn reduce_array_access(&mut self, array_access: &ArrayAccessExpression) -> Result<ArrayAccessExpression> {
|
||||||
&mut self,
|
|
||||||
array_access: &ArrayAccessExpression,
|
|
||||||
) -> Result<ArrayAccessExpression, LeoError> {
|
|
||||||
let array = self.reduce_expression(&array_access.array)?;
|
let array = self.reduce_expression(&array_access.array)?;
|
||||||
let index = self.reduce_expression(&array_access.index)?;
|
let index = self.reduce_expression(&array_access.index)?;
|
||||||
|
|
||||||
@ -183,7 +177,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
pub fn reduce_array_range_access(
|
pub fn reduce_array_range_access(
|
||||||
&mut self,
|
&mut self,
|
||||||
array_range_access: &ArrayRangeAccessExpression,
|
array_range_access: &ArrayRangeAccessExpression,
|
||||||
) -> Result<ArrayRangeAccessExpression, LeoError> {
|
) -> Result<ArrayRangeAccessExpression> {
|
||||||
let array = self.reduce_expression(&array_range_access.array)?;
|
let array = self.reduce_expression(&array_range_access.array)?;
|
||||||
let left = array_range_access
|
let left = array_range_access
|
||||||
.left
|
.left
|
||||||
@ -200,7 +194,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
.reduce_array_range_access(array_range_access, array, left, right)
|
.reduce_array_range_access(array_range_access, array, left, right)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_tuple_init(&mut self, tuple_init: &TupleInitExpression) -> Result<TupleInitExpression, LeoError> {
|
pub fn reduce_tuple_init(&mut self, tuple_init: &TupleInitExpression) -> Result<TupleInitExpression> {
|
||||||
let mut elements = vec![];
|
let mut elements = vec![];
|
||||||
for element in tuple_init.elements.iter() {
|
for element in tuple_init.elements.iter() {
|
||||||
elements.push(self.reduce_expression(element)?);
|
elements.push(self.reduce_expression(element)?);
|
||||||
@ -209,10 +203,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_tuple_init(tuple_init, elements)
|
self.reducer.reduce_tuple_init(tuple_init, elements)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_tuple_access(
|
pub fn reduce_tuple_access(&mut self, tuple_access: &TupleAccessExpression) -> Result<TupleAccessExpression> {
|
||||||
&mut self,
|
|
||||||
tuple_access: &TupleAccessExpression,
|
|
||||||
) -> Result<TupleAccessExpression, LeoError> {
|
|
||||||
let tuple = self.reduce_expression(&tuple_access.tuple)?;
|
let tuple = self.reduce_expression(&tuple_access.tuple)?;
|
||||||
|
|
||||||
self.reducer.reduce_tuple_access(tuple_access, tuple)
|
self.reducer.reduce_tuple_access(tuple_access, tuple)
|
||||||
@ -221,7 +212,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
pub fn reduce_circuit_implied_variable_definition(
|
pub fn reduce_circuit_implied_variable_definition(
|
||||||
&mut self,
|
&mut self,
|
||||||
variable: &CircuitImpliedVariableDefinition,
|
variable: &CircuitImpliedVariableDefinition,
|
||||||
) -> Result<CircuitImpliedVariableDefinition, LeoError> {
|
) -> Result<CircuitImpliedVariableDefinition> {
|
||||||
let identifier = self.reduce_identifier(&variable.identifier)?;
|
let identifier = self.reduce_identifier(&variable.identifier)?;
|
||||||
let expression = variable
|
let expression = variable
|
||||||
.expression
|
.expression
|
||||||
@ -233,10 +224,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
.reduce_circuit_implied_variable_definition(variable, identifier, expression)
|
.reduce_circuit_implied_variable_definition(variable, identifier, expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_circuit_init(
|
pub fn reduce_circuit_init(&mut self, circuit_init: &CircuitInitExpression) -> Result<CircuitInitExpression> {
|
||||||
&mut self,
|
|
||||||
circuit_init: &CircuitInitExpression,
|
|
||||||
) -> Result<CircuitInitExpression, LeoError> {
|
|
||||||
let name = self.reduce_identifier(&circuit_init.name)?;
|
let name = self.reduce_identifier(&circuit_init.name)?;
|
||||||
|
|
||||||
let mut members = vec![];
|
let mut members = vec![];
|
||||||
@ -250,7 +238,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
pub fn reduce_circuit_member_access(
|
pub fn reduce_circuit_member_access(
|
||||||
&mut self,
|
&mut self,
|
||||||
circuit_member_access: &CircuitMemberAccessExpression,
|
circuit_member_access: &CircuitMemberAccessExpression,
|
||||||
) -> Result<CircuitMemberAccessExpression, LeoError> {
|
) -> Result<CircuitMemberAccessExpression> {
|
||||||
let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
|
let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
|
||||||
let name = self.reduce_identifier(&circuit_member_access.name)?;
|
let name = self.reduce_identifier(&circuit_member_access.name)?;
|
||||||
|
|
||||||
@ -261,7 +249,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
pub fn reduce_circuit_static_fn_access(
|
pub fn reduce_circuit_static_fn_access(
|
||||||
&mut self,
|
&mut self,
|
||||||
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
|
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
|
||||||
) -> Result<CircuitStaticFunctionAccessExpression, LeoError> {
|
) -> Result<CircuitStaticFunctionAccessExpression> {
|
||||||
let circuit = self.reduce_expression(&circuit_static_fn_access.circuit)?;
|
let circuit = self.reduce_expression(&circuit_static_fn_access.circuit)?;
|
||||||
let name = self.reduce_identifier(&circuit_static_fn_access.name)?;
|
let name = self.reduce_identifier(&circuit_static_fn_access.name)?;
|
||||||
|
|
||||||
@ -269,7 +257,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
.reduce_circuit_static_fn_access(circuit_static_fn_access, circuit, name)
|
.reduce_circuit_static_fn_access(circuit_static_fn_access, circuit, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_call(&mut self, call: &CallExpression) -> Result<CallExpression, LeoError> {
|
pub fn reduce_call(&mut self, call: &CallExpression) -> Result<CallExpression> {
|
||||||
let function = self.reduce_expression(&call.function)?;
|
let function = self.reduce_expression(&call.function)?;
|
||||||
|
|
||||||
let mut arguments = vec![];
|
let mut arguments = vec![];
|
||||||
@ -281,7 +269,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Statements
|
// Statements
|
||||||
pub fn reduce_statement(&mut self, statement: &Statement) -> Result<Statement, LeoError> {
|
pub fn reduce_statement(&mut self, statement: &Statement) -> Result<Statement> {
|
||||||
let new = match statement {
|
let new = match statement {
|
||||||
Statement::Return(return_statement) => Statement::Return(self.reduce_return(return_statement)?),
|
Statement::Return(return_statement) => Statement::Return(self.reduce_return(return_statement)?),
|
||||||
Statement::Definition(definition) => Statement::Definition(self.reduce_definition(definition)?),
|
Statement::Definition(definition) => Statement::Definition(self.reduce_definition(definition)?),
|
||||||
@ -296,19 +284,19 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_statement(statement, new)
|
self.reducer.reduce_statement(statement, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_return(&mut self, return_statement: &ReturnStatement) -> Result<ReturnStatement, LeoError> {
|
pub fn reduce_return(&mut self, return_statement: &ReturnStatement) -> Result<ReturnStatement> {
|
||||||
let expression = self.reduce_expression(&return_statement.expression)?;
|
let expression = self.reduce_expression(&return_statement.expression)?;
|
||||||
|
|
||||||
self.reducer.reduce_return(return_statement, expression)
|
self.reducer.reduce_return(return_statement, expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_variable_name(&mut self, variable_name: &VariableName) -> Result<VariableName, LeoError> {
|
pub fn reduce_variable_name(&mut self, variable_name: &VariableName) -> Result<VariableName> {
|
||||||
let identifier = self.reduce_identifier(&variable_name.identifier)?;
|
let identifier = self.reduce_identifier(&variable_name.identifier)?;
|
||||||
|
|
||||||
self.reducer.reduce_variable_name(variable_name, identifier)
|
self.reducer.reduce_variable_name(variable_name, identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_definition(&mut self, definition: &DefinitionStatement) -> Result<DefinitionStatement, LeoError> {
|
pub fn reduce_definition(&mut self, definition: &DefinitionStatement) -> Result<DefinitionStatement> {
|
||||||
let mut variable_names = vec![];
|
let mut variable_names = vec![];
|
||||||
for variable_name in definition.variable_names.iter() {
|
for variable_name in definition.variable_names.iter() {
|
||||||
variable_names.push(self.reduce_variable_name(variable_name)?);
|
variable_names.push(self.reduce_variable_name(variable_name)?);
|
||||||
@ -325,7 +313,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_definition(definition, variable_names, type_, value)
|
self.reducer.reduce_definition(definition, variable_names, type_, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_assignee_access(&mut self, access: &AssigneeAccess) -> Result<AssigneeAccess, LeoError> {
|
pub fn reduce_assignee_access(&mut self, access: &AssigneeAccess) -> Result<AssigneeAccess> {
|
||||||
let new = match access {
|
let new = match access {
|
||||||
AssigneeAccess::ArrayRange(left, right) => {
|
AssigneeAccess::ArrayRange(left, right) => {
|
||||||
let left = left.as_ref().map(|left| self.reduce_expression(left)).transpose()?;
|
let left = left.as_ref().map(|left| self.reduce_expression(left)).transpose()?;
|
||||||
@ -341,7 +329,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_assignee_access(access, new)
|
self.reducer.reduce_assignee_access(access, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_assignee(&mut self, assignee: &Assignee) -> Result<Assignee, LeoError> {
|
pub fn reduce_assignee(&mut self, assignee: &Assignee) -> Result<Assignee> {
|
||||||
let identifier = self.reduce_identifier(&assignee.identifier)?;
|
let identifier = self.reduce_identifier(&assignee.identifier)?;
|
||||||
|
|
||||||
let mut accesses = vec![];
|
let mut accesses = vec![];
|
||||||
@ -352,14 +340,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_assignee(assignee, identifier, accesses)
|
self.reducer.reduce_assignee(assignee, identifier, accesses)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_assign(&mut self, assign: &AssignStatement) -> Result<AssignStatement, LeoError> {
|
pub fn reduce_assign(&mut self, assign: &AssignStatement) -> Result<AssignStatement> {
|
||||||
let assignee = self.reduce_assignee(&assign.assignee)?;
|
let assignee = self.reduce_assignee(&assign.assignee)?;
|
||||||
let value = self.reduce_expression(&assign.value)?;
|
let value = self.reduce_expression(&assign.value)?;
|
||||||
|
|
||||||
self.reducer.reduce_assign(assign, assignee, value)
|
self.reducer.reduce_assign(assign, assignee, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_conditional(&mut self, conditional: &ConditionalStatement) -> Result<ConditionalStatement, LeoError> {
|
pub fn reduce_conditional(&mut self, conditional: &ConditionalStatement) -> Result<ConditionalStatement> {
|
||||||
let condition = self.reduce_expression(&conditional.condition)?;
|
let condition = self.reduce_expression(&conditional.condition)?;
|
||||||
let block = self.reduce_block(&conditional.block)?;
|
let block = self.reduce_block(&conditional.block)?;
|
||||||
let next = conditional
|
let next = conditional
|
||||||
@ -371,7 +359,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_conditional(conditional, condition, block, next)
|
self.reducer.reduce_conditional(conditional, condition, block, next)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_iteration(&mut self, iteration: &IterationStatement) -> Result<IterationStatement, LeoError> {
|
pub fn reduce_iteration(&mut self, iteration: &IterationStatement) -> Result<IterationStatement> {
|
||||||
let variable = self.reduce_identifier(&iteration.variable)?;
|
let variable = self.reduce_identifier(&iteration.variable)?;
|
||||||
let start = self.reduce_expression(&iteration.start)?;
|
let start = self.reduce_expression(&iteration.start)?;
|
||||||
let stop = self.reduce_expression(&iteration.stop)?;
|
let stop = self.reduce_expression(&iteration.stop)?;
|
||||||
@ -380,7 +368,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_iteration(iteration, variable, start, stop, block)
|
self.reducer.reduce_iteration(iteration, variable, start, stop, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_console(&mut self, console_function_call: &ConsoleStatement) -> Result<ConsoleStatement, LeoError> {
|
pub fn reduce_console(&mut self, console_function_call: &ConsoleStatement) -> Result<ConsoleStatement> {
|
||||||
let function = match &console_function_call.function {
|
let function = match &console_function_call.function {
|
||||||
ConsoleFunction::Assert(expression) => ConsoleFunction::Assert(self.reduce_expression(expression)?),
|
ConsoleFunction::Assert(expression) => ConsoleFunction::Assert(self.reduce_expression(expression)?),
|
||||||
ConsoleFunction::Error(args) | ConsoleFunction::Log(args) => {
|
ConsoleFunction::Error(args) | ConsoleFunction::Log(args) => {
|
||||||
@ -406,15 +394,12 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_console(console_function_call, function)
|
self.reducer.reduce_console(console_function_call, function)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_expression_statement(
|
pub fn reduce_expression_statement(&mut self, expression: &ExpressionStatement) -> Result<ExpressionStatement> {
|
||||||
&mut self,
|
|
||||||
expression: &ExpressionStatement,
|
|
||||||
) -> Result<ExpressionStatement, LeoError> {
|
|
||||||
let inner_expression = self.reduce_expression(&expression.expression)?;
|
let inner_expression = self.reduce_expression(&expression.expression)?;
|
||||||
self.reducer.reduce_expression_statement(expression, inner_expression)
|
self.reducer.reduce_expression_statement(expression, inner_expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_block(&mut self, block: &Block) -> Result<Block, LeoError> {
|
pub fn reduce_block(&mut self, block: &Block) -> Result<Block> {
|
||||||
let mut statements = vec![];
|
let mut statements = vec![];
|
||||||
for statement in block.statements.iter() {
|
for statement in block.statements.iter() {
|
||||||
statements.push(self.reduce_statement(statement)?);
|
statements.push(self.reduce_statement(statement)?);
|
||||||
@ -424,7 +409,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Program
|
// Program
|
||||||
pub fn reduce_program(&mut self, program: &Program) -> Result<Program, LeoError> {
|
pub fn reduce_program(&mut self, program: &Program) -> Result<Program> {
|
||||||
let mut inputs = vec![];
|
let mut inputs = vec![];
|
||||||
for input in program.expected_input.iter() {
|
for input in program.expected_input.iter() {
|
||||||
inputs.push(self.reduce_function_input(input)?);
|
inputs.push(self.reduce_function_input(input)?);
|
||||||
@ -459,14 +444,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
pub fn reduce_function_input_variable(
|
pub fn reduce_function_input_variable(
|
||||||
&mut self,
|
&mut self,
|
||||||
variable: &FunctionInputVariable,
|
variable: &FunctionInputVariable,
|
||||||
) -> Result<FunctionInputVariable, LeoError> {
|
) -> Result<FunctionInputVariable> {
|
||||||
let identifier = self.reduce_identifier(&variable.identifier)?;
|
let identifier = self.reduce_identifier(&variable.identifier)?;
|
||||||
let type_ = self.reduce_type(&variable.type_, &variable.span)?;
|
let type_ = self.reduce_type(&variable.type_, &variable.span)?;
|
||||||
|
|
||||||
self.reducer.reduce_function_input_variable(variable, identifier, type_)
|
self.reducer.reduce_function_input_variable(variable, identifier, type_)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_function_input(&mut self, input: &FunctionInput) -> Result<FunctionInput, LeoError> {
|
pub fn reduce_function_input(&mut self, input: &FunctionInput) -> Result<FunctionInput> {
|
||||||
let new = match input {
|
let new = match input {
|
||||||
FunctionInput::Variable(function_input_variable) => {
|
FunctionInput::Variable(function_input_variable) => {
|
||||||
FunctionInput::Variable(Box::new(self.reduce_function_input_variable(function_input_variable)?))
|
FunctionInput::Variable(Box::new(self.reduce_function_input_variable(function_input_variable)?))
|
||||||
@ -477,10 +462,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_function_input(input, new)
|
self.reducer.reduce_function_input(input, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_package_or_packages(
|
pub fn reduce_package_or_packages(&mut self, package_or_packages: &PackageOrPackages) -> Result<PackageOrPackages> {
|
||||||
&mut self,
|
|
||||||
package_or_packages: &PackageOrPackages,
|
|
||||||
) -> Result<PackageOrPackages, LeoError> {
|
|
||||||
let new = match package_or_packages {
|
let new = match package_or_packages {
|
||||||
PackageOrPackages::Package(package) => PackageOrPackages::Package(Package {
|
PackageOrPackages::Package(package) => PackageOrPackages::Package(Package {
|
||||||
name: self.reduce_identifier(&package.name)?,
|
name: self.reduce_identifier(&package.name)?,
|
||||||
@ -497,13 +479,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_package_or_packages(package_or_packages, new)
|
self.reducer.reduce_package_or_packages(package_or_packages, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_import(&mut self, import: &ImportStatement) -> Result<ImportStatement, LeoError> {
|
pub fn reduce_import(&mut self, import: &ImportStatement) -> Result<ImportStatement> {
|
||||||
let package_or_packages = self.reduce_package_or_packages(&import.package_or_packages)?;
|
let package_or_packages = self.reduce_package_or_packages(&import.package_or_packages)?;
|
||||||
|
|
||||||
self.reducer.reduce_import(import, package_or_packages)
|
self.reducer.reduce_import(import, package_or_packages)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> Result<CircuitMember, LeoError> {
|
pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> Result<CircuitMember> {
|
||||||
let new = match circuit_member {
|
let new = match circuit_member {
|
||||||
CircuitMember::CircuitVariable(identifier, type_) => CircuitMember::CircuitVariable(
|
CircuitMember::CircuitVariable(identifier, type_) => CircuitMember::CircuitVariable(
|
||||||
self.reduce_identifier(identifier)?,
|
self.reduce_identifier(identifier)?,
|
||||||
@ -517,7 +499,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_circuit_member(circuit_member, new)
|
self.reducer.reduce_circuit_member(circuit_member, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_circuit(&mut self, circuit: &Circuit) -> Result<Circuit, LeoError> {
|
pub fn reduce_circuit(&mut self, circuit: &Circuit) -> Result<Circuit> {
|
||||||
let circuit_name = self.reduce_identifier(&circuit.circuit_name)?;
|
let circuit_name = self.reduce_identifier(&circuit.circuit_name)?;
|
||||||
|
|
||||||
let mut members = vec![];
|
let mut members = vec![];
|
||||||
@ -528,13 +510,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
|||||||
self.reducer.reduce_circuit(circuit, circuit_name, members)
|
self.reducer.reduce_circuit(circuit, circuit_name, members)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_annotation(&mut self, annotation: &Annotation) -> Result<Annotation, LeoError> {
|
fn reduce_annotation(&mut self, annotation: &Annotation) -> Result<Annotation> {
|
||||||
let name = self.reduce_identifier(&annotation.name)?;
|
let name = self.reduce_identifier(&annotation.name)?;
|
||||||
|
|
||||||
self.reducer.reduce_annotation(annotation, name)
|
self.reducer.reduce_annotation(annotation, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reduce_function(&mut self, function: &Function) -> Result<Function, LeoError> {
|
pub fn reduce_function(&mut self, function: &Function) -> Result<Function> {
|
||||||
let identifier = self.reduce_identifier(&function.identifier)?;
|
let identifier = self.reduce_identifier(&function.identifier)?;
|
||||||
|
|
||||||
let mut annotations = vec![];
|
let mut annotations = vec![];
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use leo_errors::{LeoError, Span};
|
use leo_errors::{Result, Span};
|
||||||
|
|
||||||
// Needed to fix clippy bug.
|
// Needed to fix clippy bug.
|
||||||
#[allow(clippy::redundant_closure)]
|
#[allow(clippy::redundant_closure)]
|
||||||
@ -24,23 +24,23 @@ pub trait ReconstructingReducer {
|
|||||||
fn in_circuit(&self) -> bool;
|
fn in_circuit(&self) -> bool;
|
||||||
fn swap_in_circuit(&mut self);
|
fn swap_in_circuit(&mut self);
|
||||||
|
|
||||||
fn reduce_type(&mut self, _type_: &Type, new: Type, _span: &Span) -> Result<Type, LeoError> {
|
fn reduce_type(&mut self, _type_: &Type, new: Type, _span: &Span) -> Result<Type> {
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expressions
|
// Expressions
|
||||||
fn reduce_expression(&mut self, _expression: &Expression, new: Expression) -> Result<Expression, LeoError> {
|
fn reduce_expression(&mut self, _expression: &Expression, new: Expression) -> Result<Expression> {
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, LeoError> {
|
fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier> {
|
||||||
Ok(Identifier {
|
Ok(Identifier {
|
||||||
name: identifier.name.clone(),
|
name: identifier.name.clone(),
|
||||||
span: identifier.span.clone(),
|
span: identifier.span.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, LeoError> {
|
fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple> {
|
||||||
Ok(GroupTuple {
|
Ok(GroupTuple {
|
||||||
x: group_tuple.x.clone(),
|
x: group_tuple.x.clone(),
|
||||||
y: group_tuple.y.clone(),
|
y: group_tuple.y.clone(),
|
||||||
@ -48,18 +48,18 @@ pub trait ReconstructingReducer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_group_value(&mut self, _group_value: &GroupValue, new: GroupValue) -> Result<GroupValue, LeoError> {
|
fn reduce_group_value(&mut self, _group_value: &GroupValue, new: GroupValue) -> Result<GroupValue> {
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, LeoError> {
|
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression> {
|
||||||
Ok(Expression::Value(ValueExpression::String(
|
Ok(Expression::Value(ValueExpression::String(
|
||||||
string.to_vec(),
|
string.to_vec(),
|
||||||
span.clone(),
|
span.clone(),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_value(&mut self, _value: &ValueExpression, new: Expression) -> Result<Expression, LeoError> {
|
fn reduce_value(&mut self, _value: &ValueExpression, new: Expression) -> Result<Expression> {
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ pub trait ReconstructingReducer {
|
|||||||
left: Expression,
|
left: Expression,
|
||||||
right: Expression,
|
right: Expression,
|
||||||
op: BinaryOperation,
|
op: BinaryOperation,
|
||||||
) -> Result<BinaryExpression, LeoError> {
|
) -> Result<BinaryExpression> {
|
||||||
Ok(BinaryExpression {
|
Ok(BinaryExpression {
|
||||||
left: Box::new(left),
|
left: Box::new(left),
|
||||||
right: Box::new(right),
|
right: Box::new(right),
|
||||||
@ -83,7 +83,7 @@ pub trait ReconstructingReducer {
|
|||||||
unary: &UnaryExpression,
|
unary: &UnaryExpression,
|
||||||
inner: Expression,
|
inner: Expression,
|
||||||
op: UnaryOperation,
|
op: UnaryOperation,
|
||||||
) -> Result<UnaryExpression, LeoError> {
|
) -> Result<UnaryExpression> {
|
||||||
Ok(UnaryExpression {
|
Ok(UnaryExpression {
|
||||||
inner: Box::new(inner),
|
inner: Box::new(inner),
|
||||||
op,
|
op,
|
||||||
@ -97,7 +97,7 @@ pub trait ReconstructingReducer {
|
|||||||
condition: Expression,
|
condition: Expression,
|
||||||
if_true: Expression,
|
if_true: Expression,
|
||||||
if_false: Expression,
|
if_false: Expression,
|
||||||
) -> Result<TernaryExpression, LeoError> {
|
) -> Result<TernaryExpression> {
|
||||||
Ok(TernaryExpression {
|
Ok(TernaryExpression {
|
||||||
condition: Box::new(condition),
|
condition: Box::new(condition),
|
||||||
if_true: Box::new(if_true),
|
if_true: Box::new(if_true),
|
||||||
@ -106,12 +106,7 @@ pub trait ReconstructingReducer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_cast(
|
fn reduce_cast(&mut self, cast: &CastExpression, inner: Expression, target_type: Type) -> Result<CastExpression> {
|
||||||
&mut self,
|
|
||||||
cast: &CastExpression,
|
|
||||||
inner: Expression,
|
|
||||||
target_type: Type,
|
|
||||||
) -> Result<CastExpression, LeoError> {
|
|
||||||
Ok(CastExpression {
|
Ok(CastExpression {
|
||||||
inner: Box::new(inner),
|
inner: Box::new(inner),
|
||||||
target_type,
|
target_type,
|
||||||
@ -123,7 +118,7 @@ pub trait ReconstructingReducer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
array_inline: &ArrayInlineExpression,
|
array_inline: &ArrayInlineExpression,
|
||||||
elements: Vec<SpreadOrExpression>,
|
elements: Vec<SpreadOrExpression>,
|
||||||
) -> Result<ArrayInlineExpression, LeoError> {
|
) -> Result<ArrayInlineExpression> {
|
||||||
Ok(ArrayInlineExpression {
|
Ok(ArrayInlineExpression {
|
||||||
elements,
|
elements,
|
||||||
span: array_inline.span.clone(),
|
span: array_inline.span.clone(),
|
||||||
@ -134,7 +129,7 @@ pub trait ReconstructingReducer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
array_init: &ArrayInitExpression,
|
array_init: &ArrayInitExpression,
|
||||||
element: Expression,
|
element: Expression,
|
||||||
) -> Result<ArrayInitExpression, LeoError> {
|
) -> Result<ArrayInitExpression> {
|
||||||
Ok(ArrayInitExpression {
|
Ok(ArrayInitExpression {
|
||||||
element: Box::new(element),
|
element: Box::new(element),
|
||||||
dimensions: array_init.dimensions.clone(),
|
dimensions: array_init.dimensions.clone(),
|
||||||
@ -147,7 +142,7 @@ pub trait ReconstructingReducer {
|
|||||||
array_access: &ArrayAccessExpression,
|
array_access: &ArrayAccessExpression,
|
||||||
array: Expression,
|
array: Expression,
|
||||||
index: Expression,
|
index: Expression,
|
||||||
) -> Result<ArrayAccessExpression, LeoError> {
|
) -> Result<ArrayAccessExpression> {
|
||||||
Ok(ArrayAccessExpression {
|
Ok(ArrayAccessExpression {
|
||||||
array: Box::new(array),
|
array: Box::new(array),
|
||||||
index: Box::new(index),
|
index: Box::new(index),
|
||||||
@ -161,7 +156,7 @@ pub trait ReconstructingReducer {
|
|||||||
array: Expression,
|
array: Expression,
|
||||||
left: Option<Expression>,
|
left: Option<Expression>,
|
||||||
right: Option<Expression>,
|
right: Option<Expression>,
|
||||||
) -> Result<ArrayRangeAccessExpression, LeoError> {
|
) -> Result<ArrayRangeAccessExpression> {
|
||||||
Ok(ArrayRangeAccessExpression {
|
Ok(ArrayRangeAccessExpression {
|
||||||
array: Box::new(array),
|
array: Box::new(array),
|
||||||
left: left.map(|expr| Box::new(expr)),
|
left: left.map(|expr| Box::new(expr)),
|
||||||
@ -174,7 +169,7 @@ pub trait ReconstructingReducer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
tuple_init: &TupleInitExpression,
|
tuple_init: &TupleInitExpression,
|
||||||
elements: Vec<Expression>,
|
elements: Vec<Expression>,
|
||||||
) -> Result<TupleInitExpression, LeoError> {
|
) -> Result<TupleInitExpression> {
|
||||||
Ok(TupleInitExpression {
|
Ok(TupleInitExpression {
|
||||||
elements,
|
elements,
|
||||||
span: tuple_init.span.clone(),
|
span: tuple_init.span.clone(),
|
||||||
@ -185,7 +180,7 @@ pub trait ReconstructingReducer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
tuple_access: &TupleAccessExpression,
|
tuple_access: &TupleAccessExpression,
|
||||||
tuple: Expression,
|
tuple: Expression,
|
||||||
) -> Result<TupleAccessExpression, LeoError> {
|
) -> Result<TupleAccessExpression> {
|
||||||
Ok(TupleAccessExpression {
|
Ok(TupleAccessExpression {
|
||||||
tuple: Box::new(tuple),
|
tuple: Box::new(tuple),
|
||||||
index: tuple_access.index.clone(),
|
index: tuple_access.index.clone(),
|
||||||
@ -198,7 +193,7 @@ pub trait ReconstructingReducer {
|
|||||||
_variable: &CircuitImpliedVariableDefinition,
|
_variable: &CircuitImpliedVariableDefinition,
|
||||||
identifier: Identifier,
|
identifier: Identifier,
|
||||||
expression: Option<Expression>,
|
expression: Option<Expression>,
|
||||||
) -> Result<CircuitImpliedVariableDefinition, LeoError> {
|
) -> Result<CircuitImpliedVariableDefinition> {
|
||||||
Ok(CircuitImpliedVariableDefinition { identifier, expression })
|
Ok(CircuitImpliedVariableDefinition { identifier, expression })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,7 +202,7 @@ pub trait ReconstructingReducer {
|
|||||||
circuit_init: &CircuitInitExpression,
|
circuit_init: &CircuitInitExpression,
|
||||||
name: Identifier,
|
name: Identifier,
|
||||||
members: Vec<CircuitImpliedVariableDefinition>,
|
members: Vec<CircuitImpliedVariableDefinition>,
|
||||||
) -> Result<CircuitInitExpression, LeoError> {
|
) -> Result<CircuitInitExpression> {
|
||||||
Ok(CircuitInitExpression {
|
Ok(CircuitInitExpression {
|
||||||
name,
|
name,
|
||||||
members,
|
members,
|
||||||
@ -220,7 +215,7 @@ pub trait ReconstructingReducer {
|
|||||||
circuit_member_access: &CircuitMemberAccessExpression,
|
circuit_member_access: &CircuitMemberAccessExpression,
|
||||||
circuit: Expression,
|
circuit: Expression,
|
||||||
name: Identifier,
|
name: Identifier,
|
||||||
) -> Result<CircuitMemberAccessExpression, LeoError> {
|
) -> Result<CircuitMemberAccessExpression> {
|
||||||
Ok(CircuitMemberAccessExpression {
|
Ok(CircuitMemberAccessExpression {
|
||||||
circuit: Box::new(circuit),
|
circuit: Box::new(circuit),
|
||||||
name,
|
name,
|
||||||
@ -233,7 +228,7 @@ pub trait ReconstructingReducer {
|
|||||||
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
|
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
|
||||||
circuit: Expression,
|
circuit: Expression,
|
||||||
name: Identifier,
|
name: Identifier,
|
||||||
) -> Result<CircuitStaticFunctionAccessExpression, LeoError> {
|
) -> Result<CircuitStaticFunctionAccessExpression> {
|
||||||
Ok(CircuitStaticFunctionAccessExpression {
|
Ok(CircuitStaticFunctionAccessExpression {
|
||||||
circuit: Box::new(circuit),
|
circuit: Box::new(circuit),
|
||||||
name,
|
name,
|
||||||
@ -246,7 +241,7 @@ pub trait ReconstructingReducer {
|
|||||||
call: &CallExpression,
|
call: &CallExpression,
|
||||||
function: Expression,
|
function: Expression,
|
||||||
arguments: Vec<Expression>,
|
arguments: Vec<Expression>,
|
||||||
) -> Result<CallExpression, LeoError> {
|
) -> Result<CallExpression> {
|
||||||
Ok(CallExpression {
|
Ok(CallExpression {
|
||||||
function: Box::new(function),
|
function: Box::new(function),
|
||||||
arguments,
|
arguments,
|
||||||
@ -255,26 +250,18 @@ pub trait ReconstructingReducer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Statements
|
// Statements
|
||||||
fn reduce_statement(&mut self, _statement: &Statement, new: Statement) -> Result<Statement, LeoError> {
|
fn reduce_statement(&mut self, _statement: &Statement, new: Statement) -> Result<Statement> {
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_return(
|
fn reduce_return(&mut self, return_statement: &ReturnStatement, expression: Expression) -> Result<ReturnStatement> {
|
||||||
&mut self,
|
|
||||||
return_statement: &ReturnStatement,
|
|
||||||
expression: Expression,
|
|
||||||
) -> Result<ReturnStatement, LeoError> {
|
|
||||||
Ok(ReturnStatement {
|
Ok(ReturnStatement {
|
||||||
expression,
|
expression,
|
||||||
span: return_statement.span.clone(),
|
span: return_statement.span.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_variable_name(
|
fn reduce_variable_name(&mut self, variable_name: &VariableName, identifier: Identifier) -> Result<VariableName> {
|
||||||
&mut self,
|
|
||||||
variable_name: &VariableName,
|
|
||||||
identifier: Identifier,
|
|
||||||
) -> Result<VariableName, LeoError> {
|
|
||||||
Ok(VariableName {
|
Ok(VariableName {
|
||||||
mutable: variable_name.mutable,
|
mutable: variable_name.mutable,
|
||||||
identifier,
|
identifier,
|
||||||
@ -288,7 +275,7 @@ pub trait ReconstructingReducer {
|
|||||||
variable_names: Vec<VariableName>,
|
variable_names: Vec<VariableName>,
|
||||||
type_: Option<Type>,
|
type_: Option<Type>,
|
||||||
value: Expression,
|
value: Expression,
|
||||||
) -> Result<DefinitionStatement, LeoError> {
|
) -> Result<DefinitionStatement> {
|
||||||
Ok(DefinitionStatement {
|
Ok(DefinitionStatement {
|
||||||
declaration_type: definition.declaration_type.clone(),
|
declaration_type: definition.declaration_type.clone(),
|
||||||
variable_names,
|
variable_names,
|
||||||
@ -298,11 +285,7 @@ pub trait ReconstructingReducer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_assignee_access(
|
fn reduce_assignee_access(&mut self, _access: &AssigneeAccess, new: AssigneeAccess) -> Result<AssigneeAccess> {
|
||||||
&mut self,
|
|
||||||
_access: &AssigneeAccess,
|
|
||||||
new: AssigneeAccess,
|
|
||||||
) -> Result<AssigneeAccess, LeoError> {
|
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,7 +294,7 @@ pub trait ReconstructingReducer {
|
|||||||
assignee: &Assignee,
|
assignee: &Assignee,
|
||||||
identifier: Identifier,
|
identifier: Identifier,
|
||||||
accesses: Vec<AssigneeAccess>,
|
accesses: Vec<AssigneeAccess>,
|
||||||
) -> Result<Assignee, LeoError> {
|
) -> Result<Assignee> {
|
||||||
Ok(Assignee {
|
Ok(Assignee {
|
||||||
identifier,
|
identifier,
|
||||||
accesses,
|
accesses,
|
||||||
@ -324,7 +307,7 @@ pub trait ReconstructingReducer {
|
|||||||
assign: &AssignStatement,
|
assign: &AssignStatement,
|
||||||
assignee: Assignee,
|
assignee: Assignee,
|
||||||
value: Expression,
|
value: Expression,
|
||||||
) -> Result<AssignStatement, LeoError> {
|
) -> Result<AssignStatement> {
|
||||||
Ok(AssignStatement {
|
Ok(AssignStatement {
|
||||||
operation: assign.operation,
|
operation: assign.operation,
|
||||||
assignee,
|
assignee,
|
||||||
@ -339,7 +322,7 @@ pub trait ReconstructingReducer {
|
|||||||
condition: Expression,
|
condition: Expression,
|
||||||
block: Block,
|
block: Block,
|
||||||
statement: Option<Statement>,
|
statement: Option<Statement>,
|
||||||
) -> Result<ConditionalStatement, LeoError> {
|
) -> Result<ConditionalStatement> {
|
||||||
Ok(ConditionalStatement {
|
Ok(ConditionalStatement {
|
||||||
condition,
|
condition,
|
||||||
block,
|
block,
|
||||||
@ -355,7 +338,7 @@ pub trait ReconstructingReducer {
|
|||||||
start: Expression,
|
start: Expression,
|
||||||
stop: Expression,
|
stop: Expression,
|
||||||
block: Block,
|
block: Block,
|
||||||
) -> Result<IterationStatement, LeoError> {
|
) -> Result<IterationStatement> {
|
||||||
Ok(IterationStatement {
|
Ok(IterationStatement {
|
||||||
variable,
|
variable,
|
||||||
start,
|
start,
|
||||||
@ -366,11 +349,7 @@ pub trait ReconstructingReducer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_console(
|
fn reduce_console(&mut self, console: &ConsoleStatement, function: ConsoleFunction) -> Result<ConsoleStatement> {
|
||||||
&mut self,
|
|
||||||
console: &ConsoleStatement,
|
|
||||||
function: ConsoleFunction,
|
|
||||||
) -> Result<ConsoleStatement, LeoError> {
|
|
||||||
Ok(ConsoleStatement {
|
Ok(ConsoleStatement {
|
||||||
function,
|
function,
|
||||||
span: console.span.clone(),
|
span: console.span.clone(),
|
||||||
@ -381,14 +360,14 @@ pub trait ReconstructingReducer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
expression_statement: &ExpressionStatement,
|
expression_statement: &ExpressionStatement,
|
||||||
expression: Expression,
|
expression: Expression,
|
||||||
) -> Result<ExpressionStatement, LeoError> {
|
) -> Result<ExpressionStatement> {
|
||||||
Ok(ExpressionStatement {
|
Ok(ExpressionStatement {
|
||||||
expression,
|
expression,
|
||||||
span: expression_statement.span.clone(),
|
span: expression_statement.span.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_block(&mut self, block: &Block, statements: Vec<Statement>) -> Result<Block, LeoError> {
|
fn reduce_block(&mut self, block: &Block, statements: Vec<Statement>) -> Result<Block> {
|
||||||
Ok(Block {
|
Ok(Block {
|
||||||
statements,
|
statements,
|
||||||
span: block.span.clone(),
|
span: block.span.clone(),
|
||||||
@ -404,7 +383,7 @@ pub trait ReconstructingReducer {
|
|||||||
circuits: IndexMap<Identifier, Circuit>,
|
circuits: IndexMap<Identifier, Circuit>,
|
||||||
functions: IndexMap<Identifier, Function>,
|
functions: IndexMap<Identifier, Function>,
|
||||||
global_consts: IndexMap<String, DefinitionStatement>,
|
global_consts: IndexMap<String, DefinitionStatement>,
|
||||||
) -> Result<Program, LeoError> {
|
) -> Result<Program> {
|
||||||
Ok(Program {
|
Ok(Program {
|
||||||
name: program.name.clone(),
|
name: program.name.clone(),
|
||||||
expected_input,
|
expected_input,
|
||||||
@ -420,7 +399,7 @@ pub trait ReconstructingReducer {
|
|||||||
variable: &FunctionInputVariable,
|
variable: &FunctionInputVariable,
|
||||||
identifier: Identifier,
|
identifier: Identifier,
|
||||||
type_: Type,
|
type_: Type,
|
||||||
) -> Result<FunctionInputVariable, LeoError> {
|
) -> Result<FunctionInputVariable> {
|
||||||
Ok(FunctionInputVariable {
|
Ok(FunctionInputVariable {
|
||||||
identifier,
|
identifier,
|
||||||
const_: variable.const_,
|
const_: variable.const_,
|
||||||
@ -430,7 +409,7 @@ pub trait ReconstructingReducer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_function_input(&mut self, _input: &FunctionInput, new: FunctionInput) -> Result<FunctionInput, LeoError> {
|
fn reduce_function_input(&mut self, _input: &FunctionInput, new: FunctionInput) -> Result<FunctionInput> {
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -438,7 +417,7 @@ pub trait ReconstructingReducer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
_package_or_packages: &PackageOrPackages,
|
_package_or_packages: &PackageOrPackages,
|
||||||
new: PackageOrPackages,
|
new: PackageOrPackages,
|
||||||
) -> Result<PackageOrPackages, LeoError> {
|
) -> Result<PackageOrPackages> {
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -446,18 +425,14 @@ pub trait ReconstructingReducer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
import: &ImportStatement,
|
import: &ImportStatement,
|
||||||
package_or_packages: PackageOrPackages,
|
package_or_packages: PackageOrPackages,
|
||||||
) -> Result<ImportStatement, LeoError> {
|
) -> Result<ImportStatement> {
|
||||||
Ok(ImportStatement {
|
Ok(ImportStatement {
|
||||||
package_or_packages,
|
package_or_packages,
|
||||||
span: import.span.clone(),
|
span: import.span.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_circuit_member(
|
fn reduce_circuit_member(&mut self, _circuit_member: &CircuitMember, new: CircuitMember) -> Result<CircuitMember> {
|
||||||
&mut self,
|
|
||||||
_circuit_member: &CircuitMember,
|
|
||||||
new: CircuitMember,
|
|
||||||
) -> Result<CircuitMember, LeoError> {
|
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -466,11 +441,11 @@ pub trait ReconstructingReducer {
|
|||||||
_circuit: &Circuit,
|
_circuit: &Circuit,
|
||||||
circuit_name: Identifier,
|
circuit_name: Identifier,
|
||||||
members: Vec<CircuitMember>,
|
members: Vec<CircuitMember>,
|
||||||
) -> Result<Circuit, LeoError> {
|
) -> Result<Circuit> {
|
||||||
Ok(Circuit { circuit_name, members })
|
Ok(Circuit { circuit_name, members })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_annotation(&mut self, annotation: &Annotation, name: Identifier) -> Result<Annotation, LeoError> {
|
fn reduce_annotation(&mut self, annotation: &Annotation, name: Identifier) -> Result<Annotation> {
|
||||||
Ok(Annotation {
|
Ok(Annotation {
|
||||||
span: annotation.span.clone(),
|
span: annotation.span.clone(),
|
||||||
name,
|
name,
|
||||||
@ -487,7 +462,7 @@ pub trait ReconstructingReducer {
|
|||||||
input: Vec<FunctionInput>,
|
input: Vec<FunctionInput>,
|
||||||
output: Option<Type>,
|
output: Option<Type>,
|
||||||
block: Block,
|
block: Block,
|
||||||
) -> Result<Function, LeoError> {
|
) -> Result<Function> {
|
||||||
Ok(Function {
|
Ok(Function {
|
||||||
identifier,
|
identifier,
|
||||||
annotations,
|
annotations,
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_asg::{AsgContext, ImportResolver, Program};
|
use leo_asg::{AsgContext, ImportResolver, Program};
|
||||||
use leo_errors::{ImportError, LeoError, Span};
|
use leo_errors::{ImportError, LeoError, Result, Span};
|
||||||
|
|
||||||
use indexmap::{IndexMap, IndexSet};
|
use indexmap::{IndexMap, IndexSet};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@ -47,7 +47,7 @@ impl<'a> ImportResolver<'a> for ImportParser<'a> {
|
|||||||
context: AsgContext<'a>,
|
context: AsgContext<'a>,
|
||||||
package_segments: &[&str],
|
package_segments: &[&str],
|
||||||
span: &Span,
|
span: &Span,
|
||||||
) -> Result<Option<Program<'a>>, LeoError> {
|
) -> Result<Option<Program<'a>>> {
|
||||||
let full_path = package_segments.join(".");
|
let full_path = package_segments.join(".");
|
||||||
if self.partial_imports.contains(&full_path) {
|
if self.partial_imports.contains(&full_path) {
|
||||||
return Err(ImportError::recursive_imports(full_path, span).into());
|
return Err(ImportError::recursive_imports(full_path, span).into());
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::ImportParser;
|
use crate::ImportParser;
|
||||||
use leo_asg::{AsgContext, Program};
|
use leo_asg::{AsgContext, Program};
|
||||||
use leo_errors::{ImportError, LeoError, Span};
|
use leo_errors::{ImportError, Result, Span};
|
||||||
|
|
||||||
use std::{fs, fs::DirEntry, path::PathBuf};
|
use std::{fs, fs::DirEntry, path::PathBuf};
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ impl<'a> ImportParser<'a> {
|
|||||||
package: &DirEntry,
|
package: &DirEntry,
|
||||||
remaining_segments: &[&str],
|
remaining_segments: &[&str],
|
||||||
span: &Span,
|
span: &Span,
|
||||||
) -> Result<Program<'a>, LeoError> {
|
) -> Result<Program<'a>> {
|
||||||
if !remaining_segments.is_empty() {
|
if !remaining_segments.is_empty() {
|
||||||
return self.parse_package(context, package.path(), remaining_segments, span);
|
return self.parse_package(context, package.path(), remaining_segments, span);
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ impl<'a> ImportParser<'a> {
|
|||||||
mut path: PathBuf,
|
mut path: PathBuf,
|
||||||
segments: &[&str],
|
segments: &[&str],
|
||||||
span: &Span,
|
span: &Span,
|
||||||
) -> Result<Program<'a>, LeoError> {
|
) -> Result<Program<'a>> {
|
||||||
let error_path = path.clone();
|
let error_path = path.clone();
|
||||||
let package_name = segments[0];
|
let package_name = segments[0];
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::ImportParser;
|
use crate::ImportParser;
|
||||||
use leo_ast::Program;
|
use leo_ast::Program;
|
||||||
use leo_errors::{ImportError, LeoError, Span};
|
use leo_errors::{ImportError, Result, Span};
|
||||||
|
|
||||||
use std::fs::DirEntry;
|
use std::fs::DirEntry;
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ impl<'a> ImportParser<'a> {
|
|||||||
///
|
///
|
||||||
/// Builds an abstract syntax tree from the given file and then builds the Leo syntax tree.
|
/// Builds an abstract syntax tree from the given file and then builds the Leo syntax tree.
|
||||||
///
|
///
|
||||||
pub(crate) fn parse_import_file(package: &DirEntry, span: &Span) -> Result<Program, LeoError> {
|
pub(crate) fn parse_import_file(package: &DirEntry, span: &Span) -> Result<Program> {
|
||||||
// Get the package file type.
|
// Get the package file type.
|
||||||
let file_type = package
|
let file_type = package
|
||||||
.file_type()
|
.file_type()
|
||||||
|
@ -145,7 +145,7 @@ impl Command for Build {
|
|||||||
|
|
||||||
// Compile the main.leo file along with constraints
|
// Compile the main.leo file along with constraints
|
||||||
if !MainFile::exists_at(&package_path) {
|
if !MainFile::exists_at(&package_path) {
|
||||||
return Err(CliError::package_main_file_not_found(new_backtrace()))?;
|
return Err(CliError::package_main_file_not_found(new_backtrace()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the output directory
|
// Create the output directory
|
||||||
|
@ -44,7 +44,7 @@ impl Command for Init {
|
|||||||
|
|
||||||
// Check that the current package directory path exists.
|
// Check that the current package directory path exists.
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
return Err(CliError::package_directory_does_not_exist(new_backtrace()))?;
|
return Err(CliError::package_directory_does_not_exist(new_backtrace()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the given package name is valid.
|
// Check that the given package name is valid.
|
||||||
@ -54,7 +54,7 @@ impl Command for Init {
|
|||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string();
|
.to_string();
|
||||||
if !LeoPackage::is_package_name_valid(&package_name) {
|
if !LeoPackage::is_package_name_valid(&package_name) {
|
||||||
return Err(CliError::invalid_package_name(&package_name, new_backtrace()))?;
|
return Err(CliError::invalid_package_name(&package_name, new_backtrace()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let username = read_username().ok();
|
let username = read_username().ok();
|
||||||
|
@ -46,7 +46,7 @@ impl Command for New {
|
|||||||
// Check that the given package name is valid.
|
// Check that the given package name is valid.
|
||||||
let package_name = self.name;
|
let package_name = self.name;
|
||||||
if !LeoPackage::is_package_name_valid(&package_name) {
|
if !LeoPackage::is_package_name_valid(&package_name) {
|
||||||
return Err(CliError::invalid_project_name(new_backtrace()))?;
|
return Err(CliError::invalid_project_name(new_backtrace()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let username = read_username().ok();
|
let username = read_username().ok();
|
||||||
@ -57,7 +57,7 @@ impl Command for New {
|
|||||||
|
|
||||||
// Verify the package directory path does not exist yet.
|
// Verify the package directory path does not exist yet.
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
return Err(CliError::package_directory_already_exists(&path, new_backtrace()))?;
|
return Err(CliError::package_directory_already_exists(&path, new_backtrace()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the package directory
|
// Create the package directory
|
||||||
|
@ -64,12 +64,12 @@ impl Add {
|
|||||||
if v.len() == 2 {
|
if v.len() == 2 {
|
||||||
Ok((v[0].to_string(), v[1].to_string()))
|
Ok((v[0].to_string(), v[1].to_string()))
|
||||||
} else {
|
} else {
|
||||||
Err(CliError::incorrect_command_argument(new_backtrace()))?
|
Err(CliError::incorrect_command_argument(new_backtrace()).into())
|
||||||
}
|
}
|
||||||
} else if let (Some(author), Some(package)) = (&self.author, &self.package) {
|
} else if let (Some(author), Some(package)) = (&self.author, &self.package) {
|
||||||
Ok((author.clone(), package.clone()))
|
Ok((author.clone(), package.clone()))
|
||||||
} else {
|
} else {
|
||||||
Err(CliError::incorrect_command_argument(new_backtrace()))?
|
Err(CliError::incorrect_command_argument(new_backtrace()).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,12 +68,12 @@ impl Clone {
|
|||||||
if v.len() == 2 {
|
if v.len() == 2 {
|
||||||
Ok((v[0].to_string(), v[1].to_string()))
|
Ok((v[0].to_string(), v[1].to_string()))
|
||||||
} else {
|
} else {
|
||||||
Err(CliError::incorrect_command_argument(new_backtrace()))?
|
Err(CliError::incorrect_command_argument(new_backtrace()).into())
|
||||||
}
|
}
|
||||||
} else if let (Some(author), Some(package)) = (&self.author, &self.package) {
|
} else if let (Some(author), Some(package)) = (&self.author, &self.package) {
|
||||||
Ok((author.clone(), package.clone()))
|
Ok((author.clone(), package.clone()))
|
||||||
} else {
|
} else {
|
||||||
Err(CliError::incorrect_command_argument(new_backtrace()))?
|
Err(CliError::incorrect_command_argument(new_backtrace()).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ impl Command for Login {
|
|||||||
|
|
||||||
let tok_opt = res.remove("token");
|
let tok_opt = res.remove("token");
|
||||||
if tok_opt.is_none() {
|
if tok_opt.is_none() {
|
||||||
return Err(CliError::unable_to_get_token(new_backtrace()))?;
|
return Err(CliError::unable_to_get_token(new_backtrace()).into());
|
||||||
};
|
};
|
||||||
|
|
||||||
(tok_opt.unwrap(), email_username)
|
(tok_opt.unwrap(), email_username)
|
||||||
@ -92,7 +92,7 @@ impl Command for Login {
|
|||||||
|
|
||||||
match api.run_route(ProfileRoute {})? {
|
match api.run_route(ProfileRoute {})? {
|
||||||
Some(username) => (token, username),
|
Some(username) => (token, username),
|
||||||
None => return Err(CliError::supplied_token_is_incorrect(new_backtrace()))?,
|
None => return Err(CliError::supplied_token_is_incorrect(new_backtrace()).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,10 +109,10 @@ impl Command for Login {
|
|||||||
(token, username)
|
(token, username)
|
||||||
} else {
|
} else {
|
||||||
remove_token_and_username()?;
|
remove_token_and_username()?;
|
||||||
return Err(CliError::stored_credentials_expired(new_backtrace()))?;
|
return Err(CliError::stored_credentials_expired(new_backtrace()).into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => return Err(CliError::no_credentials_provided(new_backtrace()))?,
|
None => return Err(CliError::no_credentials_provided(new_backtrace()).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -49,7 +49,7 @@ impl Command for Publish {
|
|||||||
|
|
||||||
let package_name = manifest.get_package_name();
|
let package_name = manifest.get_package_name();
|
||||||
if KEYWORD_TOKENS.iter().any(|keyword| keyword.to_string() == package_name) {
|
if KEYWORD_TOKENS.iter().any(|keyword| keyword.to_string() == package_name) {
|
||||||
return Err(CliError::package_cannot_be_named_after_a_keyword(new_backtrace()))?;
|
return Err(CliError::package_cannot_be_named_after_a_keyword(new_backtrace()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let package_version = manifest.get_package_version();
|
let package_version = manifest.get_package_version();
|
||||||
@ -59,9 +59,9 @@ impl Command for Publish {
|
|||||||
manifest.get_package_license(),
|
manifest.get_package_license(),
|
||||||
manifest.get_package_remote(),
|
manifest.get_package_remote(),
|
||||||
) {
|
) {
|
||||||
(None, _, _) => return Err(CliError::no_package_description(new_backtrace()))?,
|
(None, _, _) => return Err(CliError::no_package_description(new_backtrace()).into()),
|
||||||
(_, None, _) => return Err(CliError::missing_package_license(new_backtrace()))?,
|
(_, None, _) => return Err(CliError::missing_package_license(new_backtrace()).into()),
|
||||||
(_, _, None) => return Err(CliError::missing_package_remote(new_backtrace()))?,
|
(_, _, None) => return Err(CliError::missing_package_remote(new_backtrace()).into()),
|
||||||
(_, _, _) => (),
|
(_, _, _) => (),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ impl Command for Publish {
|
|||||||
|
|
||||||
// Prevent most common error before accessing API.
|
// Prevent most common error before accessing API.
|
||||||
if username == AUTHOR_PLACEHOLDER {
|
if username == AUTHOR_PLACEHOLDER {
|
||||||
return Err(CliError::package_author_is_not_set(new_backtrace()))?;
|
return Err(CliError::package_author_is_not_set(new_backtrace()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the output directory.
|
// Create the output directory.
|
||||||
|
@ -80,10 +80,7 @@ impl Command for Test {
|
|||||||
|
|
||||||
// when no main file and no files marked - error
|
// when no main file and no files marked - error
|
||||||
} else {
|
} else {
|
||||||
return Err(CliError::program_file_does_not_exist(
|
return Err(CliError::program_file_does_not_exist(package_path.to_string_lossy(), new_backtrace()).into());
|
||||||
package_path.to_string_lossy(),
|
|
||||||
new_backtrace(),
|
|
||||||
))?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct the path to the output directory;
|
// Construct the path to the output directory;
|
||||||
|
@ -189,10 +189,10 @@ pub fn read_username() -> Result<String> {
|
|||||||
|
|
||||||
pub fn remove_token_and_username() -> Result<()> {
|
pub fn remove_token_and_username() -> Result<()> {
|
||||||
if let Err(err) = fs::remove_file(&LEO_CREDENTIALS_PATH.to_path_buf()) {
|
if let Err(err) = fs::remove_file(&LEO_CREDENTIALS_PATH.to_path_buf()) {
|
||||||
Err(CliError::remove_token_and_username(err, new_backtrace()))?
|
return Err(CliError::remove_token_and_username(err, new_backtrace()).into());
|
||||||
}
|
}
|
||||||
if let Err(err) = fs::remove_file(&LEO_USERNAME_PATH.to_path_buf()) {
|
if let Err(err) = fs::remove_file(&LEO_USERNAME_PATH.to_path_buf()) {
|
||||||
Err(CliError::remove_token_and_username(err, new_backtrace()))?
|
return Err(CliError::remove_token_and_username(err, new_backtrace()).into());
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -87,11 +87,7 @@ impl Updater {
|
|||||||
{
|
{
|
||||||
Ok(latest_release.version)
|
Ok(latest_release.version)
|
||||||
} else {
|
} else {
|
||||||
Err(CliError::old_release_version(
|
Err(CliError::old_release_version(current_version, latest_release.version, new_backtrace()).into())
|
||||||
current_version,
|
|
||||||
latest_release.version,
|
|
||||||
new_backtrace(),
|
|
||||||
))?
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// 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/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use std::{borrow::Cow, fs, path::Path};
|
use std::{borrow::Cow, fs, path::Path};
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ pub struct ImportsDirectory;
|
|||||||
|
|
||||||
impl ImportsDirectory {
|
impl ImportsDirectory {
|
||||||
/// Creates a directory at the provided path with the default directory name.
|
/// Creates a directory at the provided path with the default directory name.
|
||||||
pub fn create(path: &Path) -> Result<(), LeoError> {
|
pub fn create(path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
|
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
|
||||||
path.to_mut().push(IMPORTS_DIRECTORY_NAME);
|
path.to_mut().push(IMPORTS_DIRECTORY_NAME);
|
||||||
@ -37,7 +37,7 @@ impl ImportsDirectory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Removes an imported package in the imports directory at the provided path.
|
/// Removes an imported package in the imports directory at the provided path.
|
||||||
pub fn remove_import(path: &Path, package_name: &str) -> Result<(), LeoError> {
|
pub fn remove_import(path: &Path, package_name: &str) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
|
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
|
||||||
path.to_mut().push(IMPORTS_DIRECTORY_NAME);
|
path.to_mut().push(IMPORTS_DIRECTORY_NAME);
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// 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/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
@ -31,7 +31,7 @@ pub struct InputsDirectory;
|
|||||||
|
|
||||||
impl InputsDirectory {
|
impl InputsDirectory {
|
||||||
/// Creates a directory at the provided path with the default directory name.
|
/// Creates a directory at the provided path with the default directory name.
|
||||||
pub fn create(path: &Path) -> Result<(), LeoError> {
|
pub fn create(path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() && !path.ends_with(INPUTS_DIRECTORY_NAME) {
|
if path.is_dir() && !path.ends_with(INPUTS_DIRECTORY_NAME) {
|
||||||
path.to_mut().push(INPUTS_DIRECTORY_NAME);
|
path.to_mut().push(INPUTS_DIRECTORY_NAME);
|
||||||
@ -42,7 +42,7 @@ impl InputsDirectory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a list of files in the input directory.
|
/// Returns a list of files in the input directory.
|
||||||
pub fn files(path: &Path) -> Result<Vec<PathBuf>, LeoError> {
|
pub fn files(path: &Path) -> Result<Vec<PathBuf>> {
|
||||||
let mut path = path.to_owned();
|
let mut path = path.to_owned();
|
||||||
path.push(INPUTS_DIRECTORY_NAME);
|
path.push(INPUTS_DIRECTORY_NAME);
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ impl InputsDirectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_file_paths(directory: ReadDir, file_paths: &mut Vec<PathBuf>) -> Result<(), LeoError> {
|
fn parse_file_paths(directory: ReadDir, file_paths: &mut Vec<PathBuf>) -> Result<()> {
|
||||||
for file_entry in directory.into_iter() {
|
for file_entry in directory.into_iter() {
|
||||||
let file_entry = file_entry.map_err(|e| PackageError::failed_to_get_input_file_entry(e, Backtrace::new()))?;
|
let file_entry = file_entry.map_err(|e| PackageError::failed_to_get_input_file_entry(e, Backtrace::new()))?;
|
||||||
let file_path = file_entry.path();
|
let file_path = file_entry.path();
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
use crate::inputs::INPUTS_DIRECTORY_NAME;
|
use crate::inputs::INPUTS_DIRECTORY_NAME;
|
||||||
|
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -56,7 +56,7 @@ impl InputFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the program input variables from the given file path if it exists.
|
/// Reads the program input variables from the given file path if it exists.
|
||||||
pub fn read_from<'a>(&self, path: &'a Path) -> Result<(String, Cow<'a, Path>), LeoError> {
|
pub fn read_from<'a>(&self, path: &'a Path) -> Result<(String, Cow<'a, Path>)> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let input = fs::read_to_string(&path)
|
let input = fs::read_to_string(&path)
|
||||||
@ -65,7 +65,7 @@ impl InputFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the standard input format to a file.
|
/// Writes the standard input format to a file.
|
||||||
pub fn write_to(self, path: &Path) -> Result<(), LeoError> {
|
pub fn write_to(self, path: &Path) -> Result<()> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
let mut file = File::create(&path).map_err(|e| PackageError::io_error_input_file(e, Backtrace::new()))?;
|
let mut file = File::create(&path).map_err(|e| PackageError::io_error_input_file(e, Backtrace::new()))?;
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! The `program.state` file.
|
//! The `program.state` file.
|
||||||
|
|
||||||
use crate::inputs::INPUTS_DIRECTORY_NAME;
|
use crate::inputs::INPUTS_DIRECTORY_NAME;
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -55,7 +55,7 @@ impl StateFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the state input variables from the given file path if it exists.
|
/// Reads the state input variables from the given file path if it exists.
|
||||||
pub fn read_from<'a>(&self, path: &'a Path) -> Result<(String, Cow<'a, Path>), LeoError> {
|
pub fn read_from<'a>(&self, path: &'a Path) -> Result<(String, Cow<'a, Path>)> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let input = fs::read_to_string(&path)
|
let input = fs::read_to_string(&path)
|
||||||
@ -64,7 +64,7 @@ impl StateFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the standard input format to a file.
|
/// Writes the standard input format to a file.
|
||||||
pub fn write_to(self, path: &Path) -> Result<(), LeoError> {
|
pub fn write_to(self, path: &Path) -> Result<()> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
let mut file = File::create(&path).map_err(|e| PackageError::io_error_state_file(e, Backtrace::new()))?;
|
let mut file = File::create(&path).map_err(|e| PackageError::io_error_state_file(e, Backtrace::new()))?;
|
||||||
|
|
||||||
|
@ -23,13 +23,13 @@ pub mod source;
|
|||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use leo_errors::LeoError;
|
use leo_errors::Result;
|
||||||
|
|
||||||
pub struct LeoPackage;
|
pub struct LeoPackage;
|
||||||
|
|
||||||
impl LeoPackage {
|
impl LeoPackage {
|
||||||
/// Initializes a Leo package at the given path.
|
/// Initializes a Leo package at the given path.
|
||||||
pub fn initialize(package_name: &str, path: &Path, author: Option<String>) -> Result<(), LeoError> {
|
pub fn initialize(package_name: &str, path: &Path, author: Option<String>) -> Result<()> {
|
||||||
package::Package::initialize(package_name, path, author)
|
package::Package::initialize(package_name, path, author)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ impl LeoPackage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Removes an imported Leo package
|
/// Removes an imported Leo package
|
||||||
pub fn remove_imported_package(package_name: &str, path: &Path) -> Result<(), LeoError> {
|
pub fn remove_imported_package(package_name: &str, path: &Path) -> Result<()> {
|
||||||
package::Package::remove_imported_package(package_name, path)
|
package::Package::remove_imported_package(package_name, path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! The build checksum file.
|
//! The build checksum file.
|
||||||
|
|
||||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -51,7 +51,7 @@ impl ChecksumFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the checksum from the given file path if it exists.
|
/// Reads the checksum from the given file path if it exists.
|
||||||
pub fn read_from(&self, path: &Path) -> Result<String, LeoError> {
|
pub fn read_from(&self, path: &Path) -> Result<String> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let string = fs::read_to_string(&path)
|
let string = fs::read_to_string(&path)
|
||||||
@ -60,7 +60,7 @@ impl ChecksumFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the given checksum to a file.
|
/// Writes the given checksum to a file.
|
||||||
pub fn write_to(&self, path: &Path, checksum: String) -> Result<(), LeoError> {
|
pub fn write_to(&self, path: &Path, checksum: String) -> Result<()> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
let mut file = File::create(&path).map_err(|e| PackageError::io_error_checksum_file(e, Backtrace::new()))?;
|
let mut file = File::create(&path).map_err(|e| PackageError::io_error_checksum_file(e, Backtrace::new()))?;
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ impl ChecksumFile {
|
|||||||
|
|
||||||
/// Removes the checksum at the given path if it exists. Returns `true` on success,
|
/// Removes the checksum at the given path if it exists. Returns `true` on success,
|
||||||
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
||||||
pub fn remove(&self, path: &Path) -> Result<bool, LeoError> {
|
pub fn remove(&self, path: &Path) -> Result<bool> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! The serialized circuit output file.
|
//! The serialized circuit output file.
|
||||||
|
|
||||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -51,7 +51,7 @@ impl CircuitFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the serialized circuit from the given file path if it exists.
|
/// Reads the serialized circuit from the given file path if it exists.
|
||||||
pub fn read_from(&self, path: &Path) -> Result<String, LeoError> {
|
pub fn read_from(&self, path: &Path) -> Result<String> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let string = fs::read_to_string(&path)
|
let string = fs::read_to_string(&path)
|
||||||
@ -60,7 +60,7 @@ impl CircuitFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the given serialized circuit to a file.
|
/// Writes the given serialized circuit to a file.
|
||||||
pub fn write_to(&self, path: &Path, circuit: String) -> Result<(), LeoError> {
|
pub fn write_to(&self, path: &Path, circuit: String) -> Result<()> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
let mut file = File::create(&path).map_err(|e| PackageError::io_error_circuit_file(e, Backtrace::new()))?;
|
let mut file = File::create(&path).map_err(|e| PackageError::io_error_circuit_file(e, Backtrace::new()))?;
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ impl CircuitFile {
|
|||||||
|
|
||||||
/// Removes the serialized circuit at the given path if it exists. Returns `true` on success,
|
/// Removes the serialized circuit at the given path if it exists. Returns `true` on success,
|
||||||
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
||||||
pub fn remove(&self, path: &Path) -> Result<bool, LeoError> {
|
pub fn remove(&self, path: &Path) -> Result<bool> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// 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/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use std::{borrow::Cow, fs, path::Path};
|
use std::{borrow::Cow, fs, path::Path};
|
||||||
@ -25,7 +25,7 @@ pub struct OutputsDirectory;
|
|||||||
|
|
||||||
impl OutputsDirectory {
|
impl OutputsDirectory {
|
||||||
/// Creates a directory at the provided path with the default directory name.
|
/// Creates a directory at the provided path with the default directory name.
|
||||||
pub fn create(path: &Path) -> Result<(), LeoError> {
|
pub fn create(path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||||
@ -36,7 +36,7 @@ impl OutputsDirectory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Removes the directory at the provided path.
|
/// Removes the directory at the provided path.
|
||||||
pub fn remove(path: &Path) -> Result<(), LeoError> {
|
pub fn remove(path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! The proof file.
|
//! The proof file.
|
||||||
|
|
||||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -51,7 +51,7 @@ impl ProofFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the proof from the given file path if it exists.
|
/// Reads the proof from the given file path if it exists.
|
||||||
pub fn read_from(&self, path: &Path) -> Result<String, LeoError> {
|
pub fn read_from(&self, path: &Path) -> Result<String> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let string = fs::read_to_string(&path)
|
let string = fs::read_to_string(&path)
|
||||||
@ -60,7 +60,7 @@ impl ProofFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the given proof to a file.
|
/// Writes the given proof to a file.
|
||||||
pub fn write_to(&self, path: &Path, proof: &[u8]) -> Result<(), LeoError> {
|
pub fn write_to(&self, path: &Path, proof: &[u8]) -> Result<()> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
let mut file = File::create(&path).map_err(|e| PackageError::io_error_proof_file(e, Backtrace::new()))?;
|
let mut file = File::create(&path).map_err(|e| PackageError::io_error_proof_file(e, Backtrace::new()))?;
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ impl ProofFile {
|
|||||||
|
|
||||||
/// Removes the proof at the given path if it exists. Returns `true` on success,
|
/// Removes the proof at the given path if it exists. Returns `true` on success,
|
||||||
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
||||||
pub fn remove(&self, path: &Path) -> Result<bool, LeoError> {
|
pub fn remove(&self, path: &Path) -> Result<bool> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! The proving key file.
|
//! The proving key file.
|
||||||
|
|
||||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -55,7 +55,7 @@ impl ProvingKeyFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the proving key from the given file path if it exists.
|
/// Reads the proving key from the given file path if it exists.
|
||||||
pub fn read_from(&self, path: &Path) -> Result<Vec<u8>, LeoError> {
|
pub fn read_from(&self, path: &Path) -> Result<Vec<u8>> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let bytes =
|
let bytes =
|
||||||
@ -64,7 +64,7 @@ impl ProvingKeyFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the given proving key to a file.
|
/// Writes the given proving key to a file.
|
||||||
pub fn write_to<'a>(&self, path: &'a Path, proving_key: &[u8]) -> Result<Cow<'a, Path>, LeoError> {
|
pub fn write_to<'a>(&self, path: &'a Path, proving_key: &[u8]) -> Result<Cow<'a, Path>> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
let mut file = File::create(&path).map_err(|e| PackageError::io_error_proving_key_file(e, Backtrace::new()))?;
|
let mut file = File::create(&path).map_err(|e| PackageError::io_error_proving_key_file(e, Backtrace::new()))?;
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ impl ProvingKeyFile {
|
|||||||
|
|
||||||
/// Removes the proving key at the given path if it exists. Returns `true` on success,
|
/// Removes the proving key at the given path if it exists. Returns `true` on success,
|
||||||
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
||||||
pub fn remove(&self, path: &Path) -> Result<bool, LeoError> {
|
pub fn remove(&self, path: &Path) -> Result<bool> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! The verification key file.
|
//! The verification key file.
|
||||||
|
|
||||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -55,7 +55,7 @@ impl VerificationKeyFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the verification key from the given file path if it exists.
|
/// Reads the verification key from the given file path if it exists.
|
||||||
pub fn read_from(&self, path: &Path) -> Result<Vec<u8>, LeoError> {
|
pub fn read_from(&self, path: &Path) -> Result<Vec<u8>> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let bytes = fs::read(&path)
|
let bytes = fs::read(&path)
|
||||||
@ -64,7 +64,7 @@ impl VerificationKeyFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the given verification key to a file.
|
/// Writes the given verification key to a file.
|
||||||
pub fn write_to<'a>(&self, path: &'a Path, verification_key: &[u8]) -> Result<Cow<'a, Path>, LeoError> {
|
pub fn write_to<'a>(&self, path: &'a Path, verification_key: &[u8]) -> Result<Cow<'a, Path>> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
let mut file =
|
let mut file =
|
||||||
File::create(&path).map_err(|e| PackageError::io_error_verification_key_file(e, Backtrace::new()))?;
|
File::create(&path).map_err(|e| PackageError::io_error_verification_key_file(e, Backtrace::new()))?;
|
||||||
@ -76,7 +76,7 @@ impl VerificationKeyFile {
|
|||||||
|
|
||||||
/// Removes the verification key at the given path if it exists. Returns `true` on success,
|
/// Removes the verification key at the given path if it exists. Returns `true` on success,
|
||||||
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
||||||
pub fn remove(&self, path: &Path) -> Result<bool, LeoError> {
|
pub fn remove(&self, path: &Path) -> Result<bool> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
|
@ -21,7 +21,7 @@ use crate::{
|
|||||||
source::{MainFile, SourceDirectory},
|
source::{MainFile, SourceDirectory},
|
||||||
};
|
};
|
||||||
|
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -36,7 +36,7 @@ pub struct Package {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Package {
|
impl Package {
|
||||||
pub fn new(package_name: &str) -> Result<Self, LeoError> {
|
pub fn new(package_name: &str) -> Result<Self> {
|
||||||
// Check that the package name is valid.
|
// Check that the package name is valid.
|
||||||
if !Self::is_package_name_valid(package_name) {
|
if !Self::is_package_name_valid(package_name) {
|
||||||
return Err(PackageError::invalid_package_name(package_name, Backtrace::new()).into());
|
return Err(PackageError::invalid_package_name(package_name, Backtrace::new()).into());
|
||||||
@ -184,7 +184,7 @@ impl Package {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a package at the given path
|
/// Creates a package at the given path
|
||||||
pub fn initialize(package_name: &str, path: &Path, author: Option<String>) -> Result<(), LeoError> {
|
pub fn initialize(package_name: &str, path: &Path, author: Option<String>) -> Result<()> {
|
||||||
// First, verify that this directory is not already initialized as a Leo package.
|
// First, verify that this directory is not already initialized as a Leo package.
|
||||||
{
|
{
|
||||||
if !Self::can_initialize(package_name, path) {
|
if !Self::can_initialize(package_name, path) {
|
||||||
@ -244,7 +244,7 @@ impl Package {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Removes the package at the given path
|
/// Removes the package at the given path
|
||||||
pub fn remove_imported_package(package_name: &str, path: &Path) -> Result<(), LeoError> {
|
pub fn remove_imported_package(package_name: &str, path: &Path) -> Result<()> {
|
||||||
ImportsDirectory::remove_import(path, package_name)
|
ImportsDirectory::remove_import(path, package_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
//! The `.gitignore` file.
|
//! The `.gitignore` file.
|
||||||
|
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -40,7 +40,7 @@ impl Gitignore {
|
|||||||
path.exists()
|
path.exists()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_to(self, path: &Path) -> Result<(), LeoError> {
|
pub fn write_to(self, path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
path.to_mut().push(GITIGNORE_FILENAME);
|
path.to_mut().push(GITIGNORE_FILENAME);
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::package::Package;
|
use crate::package::Package;
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -42,7 +42,7 @@ pub struct Manifest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Manifest {
|
impl Manifest {
|
||||||
pub fn new(package_name: &str, author: Option<String>) -> Result<Self, LeoError> {
|
pub fn new(package_name: &str, author: Option<String>) -> Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
project: Package::new(package_name)?,
|
project: Package::new(package_name)?,
|
||||||
remote: author.map(|author| Remote { author }),
|
remote: author.map(|author| Remote { author }),
|
||||||
@ -81,7 +81,7 @@ impl Manifest {
|
|||||||
self.remote.clone()
|
self.remote.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_to(self, path: &Path) -> Result<(), LeoError> {
|
pub fn write_to(self, path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
path.to_mut().push(MANIFEST_FILENAME);
|
path.to_mut().push(MANIFEST_FILENAME);
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
//! The `README.md` file.
|
//! The `README.md` file.
|
||||||
|
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -49,7 +49,7 @@ impl README {
|
|||||||
path.exists()
|
path.exists()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_to(self, path: &Path) -> Result<(), LeoError> {
|
pub fn write_to(self, path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
path.to_mut().push(README_FILENAME);
|
path.to_mut().push(README_FILENAME);
|
||||||
|
@ -30,7 +30,7 @@ use crate::{
|
|||||||
root::{MANIFEST_FILENAME, README_FILENAME},
|
root::{MANIFEST_FILENAME, README_FILENAME},
|
||||||
source::{SOURCE_DIRECTORY_NAME, SOURCE_FILE_EXTENSION},
|
source::{SOURCE_DIRECTORY_NAME, SOURCE_FILE_EXTENSION},
|
||||||
};
|
};
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
|
|
||||||
@ -71,14 +71,14 @@ impl ZipFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// /// Reads the program bytes from the given file path if it exists.
|
// /// Reads the program bytes from the given file path if it exists.
|
||||||
// pub fn read_from(&self, path: &Path) -> Result<Vec<u8>, LeoError> {
|
// pub fn read_from(&self, path: &Path) -> Result<Vec<u8>> {
|
||||||
// let path = self.setup_file_path(path);
|
// let path = self.setup_file_path(path);
|
||||||
//
|
//
|
||||||
// Ok(fs::read(&path).map_err(|_| PackageError::FileReadError(path.clone()))?)
|
// Ok(fs::read(&path).map_err(|_| PackageError::FileReadError(path.clone()))?)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
/// Writes the current package contents to a zip file.
|
/// Writes the current package contents to a zip file.
|
||||||
pub fn write(&self, src_dir: &Path) -> Result<(), LeoError> {
|
pub fn write(&self, src_dir: &Path) -> Result<()> {
|
||||||
// Build walkdir iterator from current package
|
// Build walkdir iterator from current package
|
||||||
let walkdir = WalkDir::new(src_dir);
|
let walkdir = WalkDir::new(src_dir);
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ impl ZipFile {
|
|||||||
|
|
||||||
/// Removes the zip file at the given path if it exists. Returns `true` on success,
|
/// Removes the zip file at the given path if it exists. Returns `true` on success,
|
||||||
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
||||||
pub fn remove(&self, path: &Path) -> Result<bool, LeoError> {
|
pub fn remove(&self, path: &Path) -> Result<bool> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// 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/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
@ -32,7 +32,7 @@ pub struct SourceDirectory;
|
|||||||
|
|
||||||
impl SourceDirectory {
|
impl SourceDirectory {
|
||||||
/// Creates a directory at the provided path with the default directory name.
|
/// Creates a directory at the provided path with the default directory name.
|
||||||
pub fn create(path: &Path) -> Result<(), LeoError> {
|
pub fn create(path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() && !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
if path.is_dir() && !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||||
@ -43,7 +43,7 @@ impl SourceDirectory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a list of files in the source directory.
|
/// Returns a list of files in the source directory.
|
||||||
pub fn files(path: &Path) -> Result<Vec<PathBuf>, LeoError> {
|
pub fn files(path: &Path) -> Result<Vec<PathBuf>> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! The `main.leo` file.
|
//! The `main.leo` file.
|
||||||
|
|
||||||
use crate::source::directory::SOURCE_DIRECTORY_NAME;
|
use crate::source::directory::SOURCE_DIRECTORY_NAME;
|
||||||
use leo_errors::{LeoError, PackageError};
|
use leo_errors::{PackageError, Result};
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -52,7 +52,7 @@ impl MainFile {
|
|||||||
path.exists()
|
path.exists()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_to(self, path: &Path) -> Result<(), LeoError> {
|
pub fn write_to(self, path: &Path) -> Result<()> {
|
||||||
let mut path = Cow::from(path);
|
let mut path = Cow::from(path);
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||||
|
@ -15,10 +15,10 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_ast::Ast;
|
use leo_ast::Ast;
|
||||||
use leo_errors::LeoError;
|
use leo_errors::Result;
|
||||||
use std::{env, fs, path::Path};
|
use std::{env, fs, path::Path};
|
||||||
|
|
||||||
fn to_leo_tree(filepath: &Path) -> Result<String, LeoError> {
|
fn to_leo_tree(filepath: &Path) -> Result<String> {
|
||||||
// Loads the Leo code as a string from the given file path.
|
// Loads the Leo code as a string from the given file path.
|
||||||
let program_filepath = filepath.to_path_buf();
|
let program_filepath = filepath.to_path_buf();
|
||||||
let program_string = fs::read_to_string(&program_filepath).expect("failed to open input file");
|
let program_string = fs::read_to_string(&program_filepath).expect("failed to open input file");
|
||||||
@ -31,7 +31,7 @@ fn to_leo_tree(filepath: &Path) -> Result<String, LeoError> {
|
|||||||
Ok(serialized_leo_ast)
|
Ok(serialized_leo_ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), LeoError> {
|
fn main() -> Result<()> {
|
||||||
// Parse the command-line arguments as strings.
|
// Parse the command-line arguments as strings.
|
||||||
let cli_arguments = env::args().collect::<Vec<String>>();
|
let cli_arguments = env::args().collect::<Vec<String>>();
|
||||||
|
|
||||||
|
@ -29,11 +29,12 @@ pub mod parser;
|
|||||||
pub use parser::*;
|
pub use parser::*;
|
||||||
|
|
||||||
use leo_ast::Ast;
|
use leo_ast::Ast;
|
||||||
|
use leo_errors::Result;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
/// Creates a new AST from a given file path and source code text.
|
/// Creates a new AST from a given file path and source code text.
|
||||||
pub fn parse_ast<T: AsRef<str>, Y: AsRef<str>>(path: T, source: Y) -> SyntaxResult<Ast> {
|
pub fn parse_ast<T: AsRef<str>, Y: AsRef<str>>(path: T, source: Y) -> Result<Ast> {
|
||||||
Ok(Ast::new(parser::parse(path.as_ref(), source.as_ref())?))
|
Ok(Ast::new(parser::parse(path.as_ref(), source.as_ref())?))
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
use std::{borrow::Cow, unimplemented};
|
use std::{borrow::Cow, unimplemented};
|
||||||
|
|
||||||
use crate::{assert_no_whitespace, tokenizer::*, SyntaxResult, Token, KEYWORD_TOKENS};
|
use crate::{assert_no_whitespace, tokenizer::*, Token, KEYWORD_TOKENS};
|
||||||
use leo_ast::*;
|
use leo_ast::*;
|
||||||
use leo_errors::{LeoError, ParserError, Span};
|
use leo_errors::{LeoError, ParserError, Result, Span};
|
||||||
use tendril::format_tendril;
|
use tendril::format_tendril;
|
||||||
|
|
||||||
/// Stores a program in tokenized format plus additional context.
|
/// Stores a program in tokenized format plus additional context.
|
||||||
@ -70,14 +70,14 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a reference to the next next token or error if it does not exist.
|
/// Returns a reference to the next next token or error if it does not exist.
|
||||||
///
|
///
|
||||||
pub fn peek_next(&self) -> SyntaxResult<&SpannedToken> {
|
pub fn peek_next(&self) -> Result<&SpannedToken> {
|
||||||
self.tokens.get(self.tokens.len() - 2).ok_or_else(|| self.eof())
|
self.tokens.get(self.tokens.len() - 2).ok_or_else(|| self.eof())
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Returns a reference to the next token or error if it does not exist.
|
/// Returns a reference to the next token or error if it does not exist.
|
||||||
///
|
///
|
||||||
pub fn peek(&self) -> SyntaxResult<&SpannedToken> {
|
pub fn peek(&self) -> Result<&SpannedToken> {
|
||||||
self.tokens.last().ok_or_else(|| self.eof())
|
self.tokens.last().ok_or_else(|| self.eof())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,7 +89,7 @@ impl ParserContext {
|
|||||||
.unwrap_or_else(|| Cow::Owned(Token::Eof))
|
.unwrap_or_else(|| Cow::Owned(Token::Eof))
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn peek_oneof(&self, token: &[Token]) -> SyntaxResult<&SpannedToken> {
|
// pub fn peek_oneof(&self, token: &[Token]) -> Result<&SpannedToken> {
|
||||||
// if let Some(spanned_token) = self.inner.last() {
|
// if let Some(spanned_token) = self.inner.last() {
|
||||||
// if token.iter().any(|x| x == &spanned_token.token) {
|
// if token.iter().any(|x| x == &spanned_token.token) {
|
||||||
// Ok(spanned_token)
|
// Ok(spanned_token)
|
||||||
@ -190,7 +190,7 @@ impl ParserContext {
|
|||||||
/// Removes the next two tokens if they are a pair of [`GroupCoordinate`] and returns them,
|
/// Removes the next two tokens if they are a pair of [`GroupCoordinate`] and returns them,
|
||||||
/// or [None] if the next token is not a [`GroupCoordinate`].
|
/// or [None] if the next token is not a [`GroupCoordinate`].
|
||||||
///
|
///
|
||||||
pub fn eat_group_partial(&mut self) -> Option<SyntaxResult<(GroupCoordinate, GroupCoordinate, Span)>> {
|
pub fn eat_group_partial(&mut self) -> Option<Result<(GroupCoordinate, GroupCoordinate, Span)>> {
|
||||||
let mut i = self.tokens.len();
|
let mut i = self.tokens.len();
|
||||||
if i < 1 {
|
if i < 1 {
|
||||||
return None;
|
return None;
|
||||||
@ -295,7 +295,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns the span of the next token if it is equal to the given [`Token`], or error.
|
/// Returns the span of the next token if it is equal to the given [`Token`], or error.
|
||||||
///
|
///
|
||||||
pub fn expect(&mut self, token: Token) -> SyntaxResult<Span> {
|
pub fn expect(&mut self, token: Token) -> Result<Span> {
|
||||||
if let Some(SpannedToken { token: inner, span }) = self.tokens.last() {
|
if let Some(SpannedToken { token: inner, span }) = self.tokens.last() {
|
||||||
if &token == inner {
|
if &token == inner {
|
||||||
Ok(self.tokens.pop().unwrap().span)
|
Ok(self.tokens.pop().unwrap().span)
|
||||||
@ -310,7 +310,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns the span of the next token if it is equal to one of the given [`Token`]s, or error.
|
/// Returns the span of the next token if it is equal to one of the given [`Token`]s, or error.
|
||||||
///
|
///
|
||||||
pub fn expect_oneof(&mut self, token: &[Token]) -> SyntaxResult<SpannedToken> {
|
pub fn expect_oneof(&mut self, token: &[Token]) -> Result<SpannedToken> {
|
||||||
if let Some(SpannedToken { token: inner, span }) = self.tokens.last() {
|
if let Some(SpannedToken { token: inner, span }) = self.tokens.last() {
|
||||||
if token.iter().any(|x| x == inner) {
|
if token.iter().any(|x| x == inner) {
|
||||||
Ok(self.tokens.pop().unwrap())
|
Ok(self.tokens.pop().unwrap())
|
||||||
@ -331,7 +331,7 @@ impl ParserContext {
|
|||||||
/// Returns the [`Identifier`] of the next token if it is a keyword,
|
/// Returns the [`Identifier`] of the next token if it is a keyword,
|
||||||
/// [`Token::Int(_)`], or an [`Identifier`], or error.
|
/// [`Token::Int(_)`], or an [`Identifier`], or error.
|
||||||
///
|
///
|
||||||
pub fn expect_loose_identifier(&mut self) -> SyntaxResult<Identifier> {
|
pub fn expect_loose_identifier(&mut self) -> Result<Identifier> {
|
||||||
if let Some(token) = self.eat_any(KEYWORD_TOKENS) {
|
if let Some(token) = self.eat_any(KEYWORD_TOKENS) {
|
||||||
return Ok(Identifier {
|
return Ok(Identifier {
|
||||||
name: token.token.to_string().into(),
|
name: token.token.to_string().into(),
|
||||||
@ -347,7 +347,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns the [`Identifier`] of the next token if it is an [`Identifier`], or error.
|
/// Returns the [`Identifier`] of the next token if it is an [`Identifier`], or error.
|
||||||
///
|
///
|
||||||
pub fn expect_ident(&mut self) -> SyntaxResult<Identifier> {
|
pub fn expect_ident(&mut self) -> Result<Identifier> {
|
||||||
if let Some(SpannedToken { token: inner, span }) = self.tokens.last() {
|
if let Some(SpannedToken { token: inner, span }) = self.tokens.last() {
|
||||||
if let Token::Ident(_) = inner {
|
if let Token::Ident(_) = inner {
|
||||||
let token = self.tokens.pop().unwrap();
|
let token = self.tokens.pop().unwrap();
|
||||||
@ -371,7 +371,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns the next token if it exists or return end of function.
|
/// Returns the next token if it exists or return end of function.
|
||||||
///
|
///
|
||||||
pub fn expect_any(&mut self) -> SyntaxResult<SpannedToken> {
|
pub fn expect_any(&mut self) -> Result<SpannedToken> {
|
||||||
if let Some(x) = self.tokens.pop() {
|
if let Some(x) = self.tokens.pop() {
|
||||||
Ok(x)
|
Ok(x)
|
||||||
} else {
|
} else {
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use tendril::format_tendril;
|
use tendril::format_tendril;
|
||||||
|
|
||||||
use leo_errors::ParserError;
|
use leo_errors::{ParserError, Result};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ impl ParserContext {
|
|||||||
/// Returns an [`Expression`] AST node if the next token is an expression.
|
/// Returns an [`Expression`] AST node if the next token is an expression.
|
||||||
/// Includes circuit init expressions.
|
/// Includes circuit init expressions.
|
||||||
///
|
///
|
||||||
pub fn parse_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_expression(&mut self) -> Result<Expression> {
|
||||||
// Store current parser state.
|
// Store current parser state.
|
||||||
let prior_fuzzy_state = self.fuzzy_struct_state;
|
let prior_fuzzy_state = self.fuzzy_struct_state;
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_disjunctive_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_disjunctive_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_conditional_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_conditional_expression(&mut self) -> Result<Expression> {
|
||||||
// Try to parse the next expression. Try BinaryOperation::Or.
|
// Try to parse the next expression. Try BinaryOperation::Or.
|
||||||
let mut expr = self.parse_disjunctive_expression()?;
|
let mut expr = self.parse_disjunctive_expression()?;
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_conjunctive_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_conjunctive_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_disjunctive_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_disjunctive_expression(&mut self) -> Result<Expression> {
|
||||||
let mut expr = self.parse_conjunctive_expression()?;
|
let mut expr = self.parse_conjunctive_expression()?;
|
||||||
while self.eat(Token::Or).is_some() {
|
while self.eat(Token::Or).is_some() {
|
||||||
let right = self.parse_conjunctive_expression()?;
|
let right = self.parse_conjunctive_expression()?;
|
||||||
@ -107,7 +107,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_bit_or_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_bit_or_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_conjunctive_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_conjunctive_expression(&mut self) -> Result<Expression> {
|
||||||
let mut expr = self.parse_equality_expression()?;
|
let mut expr = self.parse_equality_expression()?;
|
||||||
while self.eat(Token::And).is_some() {
|
while self.eat(Token::And).is_some() {
|
||||||
let right = self.parse_equality_expression()?;
|
let right = self.parse_equality_expression()?;
|
||||||
@ -127,7 +127,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_bit_xor_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_bit_xor_expression`].
|
||||||
///
|
///
|
||||||
// pub fn parse_bit_or_expression(&mut self) -> SyntaxResult<Expression> {
|
// pub fn parse_bit_or_expression(&mut self) -> Result<Expression> {
|
||||||
// let mut expr = self.parse_bit_xor_expression()?;
|
// let mut expr = self.parse_bit_xor_expression()?;
|
||||||
// while self.eat(Token::BitOr).is_some() {
|
// while self.eat(Token::BitOr).is_some() {
|
||||||
// let right = self.parse_bit_xor_expression()?;
|
// let right = self.parse_bit_xor_expression()?;
|
||||||
@ -147,7 +147,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_bit_and_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_bit_and_expression`].
|
||||||
///
|
///
|
||||||
// pub fn parse_bit_xor_expression(&mut self) -> SyntaxResult<Expression> {
|
// pub fn parse_bit_xor_expression(&mut self) -> Result<Expression> {
|
||||||
// let mut expr = self.parse_bit_and_expression()?;
|
// let mut expr = self.parse_bit_and_expression()?;
|
||||||
// while self.eat(Token::BitXor).is_some() {
|
// while self.eat(Token::BitXor).is_some() {
|
||||||
// let right = self.parse_bit_and_expression()?;
|
// let right = self.parse_bit_and_expression()?;
|
||||||
@ -167,7 +167,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_equality_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_equality_expression`].
|
||||||
///
|
///
|
||||||
// pub fn parse_bit_and_expression(&mut self) -> SyntaxResult<Expression> {
|
// pub fn parse_bit_and_expression(&mut self) -> Result<Expression> {
|
||||||
// let mut expr = self.parse_equality_expression()?;
|
// let mut expr = self.parse_equality_expression()?;
|
||||||
// while self.eat(Token::BitAnd).is_some() {
|
// while self.eat(Token::BitAnd).is_some() {
|
||||||
// let right = self.parse_equality_expression()?;
|
// let right = self.parse_equality_expression()?;
|
||||||
@ -187,7 +187,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_ordering_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_ordering_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_equality_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_equality_expression(&mut self) -> Result<Expression> {
|
||||||
let mut expr = self.parse_ordering_expression()?;
|
let mut expr = self.parse_ordering_expression()?;
|
||||||
if let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Eq, Token::NotEq]) {
|
if let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Eq, Token::NotEq]) {
|
||||||
let right = self.parse_ordering_expression()?;
|
let right = self.parse_ordering_expression()?;
|
||||||
@ -211,7 +211,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_shift_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_shift_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_ordering_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_ordering_expression(&mut self) -> Result<Expression> {
|
||||||
let mut expr = self.parse_additive_expression()?;
|
let mut expr = self.parse_additive_expression()?;
|
||||||
while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Lt, Token::LtEq, Token::Gt, Token::GtEq])
|
while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Lt, Token::LtEq, Token::Gt, Token::GtEq])
|
||||||
{
|
{
|
||||||
@ -238,7 +238,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_additive_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_additive_expression`].
|
||||||
///
|
///
|
||||||
// pub fn parse_shift_expression(&mut self) -> SyntaxResult<Expression> {
|
// pub fn parse_shift_expression(&mut self) -> Result<Expression> {
|
||||||
// let mut expr = self.parse_additive_expression()?;
|
// let mut expr = self.parse_additive_expression()?;
|
||||||
// while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Shl, Token::Shr, Token::ShrSigned]) {
|
// while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Shl, Token::Shr, Token::ShrSigned]) {
|
||||||
// let right = self.parse_additive_expression()?;
|
// let right = self.parse_additive_expression()?;
|
||||||
@ -263,7 +263,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_mul_div_pow_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_mul_div_pow_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_additive_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_additive_expression(&mut self) -> Result<Expression> {
|
||||||
let mut expr = self.parse_multiplicative_expression()?;
|
let mut expr = self.parse_multiplicative_expression()?;
|
||||||
while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Add, Token::Minus]) {
|
while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Add, Token::Minus]) {
|
||||||
let right = self.parse_multiplicative_expression()?;
|
let right = self.parse_multiplicative_expression()?;
|
||||||
@ -287,7 +287,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_exponential_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_exponential_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_multiplicative_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_multiplicative_expression(&mut self) -> Result<Expression> {
|
||||||
let mut expr = self.parse_exponential_expression()?;
|
let mut expr = self.parse_exponential_expression()?;
|
||||||
while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Mul, Token::Div]) {
|
while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Mul, Token::Div]) {
|
||||||
let right = self.parse_exponential_expression()?;
|
let right = self.parse_exponential_expression()?;
|
||||||
@ -312,7 +312,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_cast_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_cast_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_exponential_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_exponential_expression(&mut self) -> Result<Expression> {
|
||||||
let mut exprs = vec![self.parse_cast_expression()?];
|
let mut exprs = vec![self.parse_cast_expression()?];
|
||||||
while self.eat(Token::Exp).is_some() {
|
while self.eat(Token::Exp).is_some() {
|
||||||
exprs.push(self.parse_cast_expression()?);
|
exprs.push(self.parse_cast_expression()?);
|
||||||
@ -336,7 +336,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_unary_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_unary_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_cast_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_cast_expression(&mut self) -> Result<Expression> {
|
||||||
let mut expr = self.parse_unary_expression()?;
|
let mut expr = self.parse_unary_expression()?;
|
||||||
while self.eat(Token::As).is_some() {
|
while self.eat(Token::As).is_some() {
|
||||||
let (type_, type_span) = self.parse_type()?;
|
let (type_, type_span) = self.parse_type()?;
|
||||||
@ -355,7 +355,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_postfix_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_postfix_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_unary_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_unary_expression(&mut self) -> Result<Expression> {
|
||||||
let mut ops = Vec::new();
|
let mut ops = Vec::new();
|
||||||
while let Some(token) = self.eat_any(&[Token::Not, Token::Minus]) {
|
while let Some(token) = self.eat_any(&[Token::Not, Token::Minus]) {
|
||||||
ops.push(token);
|
ops.push(token);
|
||||||
@ -400,7 +400,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Otherwise, tries to parse the next token using [`parse_primary_expression`].
|
/// Otherwise, tries to parse the next token using [`parse_primary_expression`].
|
||||||
///
|
///
|
||||||
pub fn parse_postfix_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_postfix_expression(&mut self) -> Result<Expression> {
|
||||||
let mut expr = self.parse_primary_expression()?;
|
let mut expr = self.parse_primary_expression()?;
|
||||||
while let Some(token) = self.eat_any(&[Token::LeftSquare, Token::Dot, Token::LeftParen, Token::DoubleColon]) {
|
while let Some(token) = self.eat_any(&[Token::LeftSquare, Token::Dot, Token::LeftParen, Token::DoubleColon]) {
|
||||||
match token.token {
|
match token.token {
|
||||||
@ -505,7 +505,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// This method should only be called in the context of an array access expression.
|
/// This method should only be called in the context of an array access expression.
|
||||||
///
|
///
|
||||||
pub fn parse_spread_or_expression(&mut self) -> SyntaxResult<SpreadOrExpression> {
|
pub fn parse_spread_or_expression(&mut self) -> Result<SpreadOrExpression> {
|
||||||
Ok(if self.eat(Token::DotDotDot).is_some() {
|
Ok(if self.eat(Token::DotDotDot).is_some() {
|
||||||
SpreadOrExpression::Spread(self.parse_expression()?)
|
SpreadOrExpression::Spread(self.parse_expression()?)
|
||||||
} else {
|
} else {
|
||||||
@ -517,7 +517,7 @@ impl ParserContext {
|
|||||||
/// Returns an [`Expression`] AST node if the next tokens represent an
|
/// Returns an [`Expression`] AST node if the next tokens represent an
|
||||||
/// circuit initialization expression.
|
/// circuit initialization expression.
|
||||||
///
|
///
|
||||||
pub fn parse_circuit_expression(&mut self, identifier: Identifier) -> SyntaxResult<Expression> {
|
pub fn parse_circuit_expression(&mut self, identifier: Identifier) -> Result<Expression> {
|
||||||
self.expect(Token::LeftCurly)?;
|
self.expect(Token::LeftCurly)?;
|
||||||
let mut members = Vec::new();
|
let mut members = Vec::new();
|
||||||
let end_span;
|
let end_span;
|
||||||
@ -555,7 +555,7 @@ impl ParserContext {
|
|||||||
/// Returns an [`Expression`] AST node if the next tokens represent an
|
/// Returns an [`Expression`] AST node if the next tokens represent an
|
||||||
/// tuple initialization expression.
|
/// tuple initialization expression.
|
||||||
///
|
///
|
||||||
pub fn parse_tuple_expression(&mut self, span: &Span) -> SyntaxResult<Expression> {
|
pub fn parse_tuple_expression(&mut self, span: &Span) -> Result<Expression> {
|
||||||
if let Some((left, right, span)) = self.eat_group_partial().transpose()? {
|
if let Some((left, right, span)) = self.eat_group_partial().transpose()? {
|
||||||
return Ok(Expression::Value(ValueExpression::Group(Box::new(GroupValue::Tuple(
|
return Ok(Expression::Value(ValueExpression::Group(Box::new(GroupValue::Tuple(
|
||||||
Box::new(GroupTuple {
|
Box::new(GroupTuple {
|
||||||
@ -594,7 +594,7 @@ impl ParserContext {
|
|||||||
/// Returns an [`Expression`] AST node if the next tokens represent an
|
/// Returns an [`Expression`] AST node if the next tokens represent an
|
||||||
/// array initialization expression.
|
/// array initialization expression.
|
||||||
///
|
///
|
||||||
pub fn parse_array_expression(&mut self, span: &Span) -> SyntaxResult<Expression> {
|
pub fn parse_array_expression(&mut self, span: &Span) -> Result<Expression> {
|
||||||
if let Some(end) = self.eat(Token::RightSquare) {
|
if let Some(end) = self.eat(Token::RightSquare) {
|
||||||
return Ok(Expression::ArrayInline(ArrayInlineExpression {
|
return Ok(Expression::ArrayInline(ArrayInlineExpression {
|
||||||
elements: Vec::new(),
|
elements: Vec::new(),
|
||||||
@ -654,7 +654,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns an expression error if the token cannot be matched.
|
/// Returns an expression error if the token cannot be matched.
|
||||||
///
|
///
|
||||||
pub fn parse_primary_expression(&mut self) -> SyntaxResult<Expression> {
|
pub fn parse_primary_expression(&mut self) -> Result<Expression> {
|
||||||
let SpannedToken { token, span } = self.expect_any()?;
|
let SpannedToken { token, span } = self.expect_any()?;
|
||||||
Ok(match token {
|
Ok(match token {
|
||||||
Token::Int(value) => {
|
Token::Int(value) => {
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use tendril::format_tendril;
|
use tendril::format_tendril;
|
||||||
|
|
||||||
use leo_errors::ParserError;
|
use leo_errors::{ParserError, Result};
|
||||||
|
|
||||||
use crate::KEYWORD_TOKENS;
|
use crate::KEYWORD_TOKENS;
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`Program`] AST if all tokens can be consumed and represent a valid Leo program.
|
/// Returns a [`Program`] AST if all tokens can be consumed and represent a valid Leo program.
|
||||||
///
|
///
|
||||||
pub fn parse_program(&mut self) -> SyntaxResult<Program> {
|
pub fn parse_program(&mut self) -> Result<Program> {
|
||||||
let mut imports = Vec::new();
|
let mut imports = Vec::new();
|
||||||
let mut circuits = IndexMap::new();
|
let mut circuits = IndexMap::new();
|
||||||
let mut functions = IndexMap::new();
|
let mut functions = IndexMap::new();
|
||||||
@ -93,7 +93,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns an [`Annotation`] AST node if the next tokens represent a supported annotation.
|
/// Returns an [`Annotation`] AST node if the next tokens represent a supported annotation.
|
||||||
///
|
///
|
||||||
pub fn parse_annotation(&mut self) -> SyntaxResult<Annotation> {
|
pub fn parse_annotation(&mut self) -> Result<Annotation> {
|
||||||
let start = self.expect(Token::At)?;
|
let start = self.expect(Token::At)?;
|
||||||
let name = self.expect_ident()?;
|
let name = self.expect_ident()?;
|
||||||
if name.name.as_ref() == "context" {
|
if name.name.as_ref() == "context" {
|
||||||
@ -154,7 +154,7 @@ impl ParserContext {
|
|||||||
/// Returns a vector of [`PackageAccess`] AST nodes if the next tokens represent package access
|
/// Returns a vector of [`PackageAccess`] AST nodes if the next tokens represent package access
|
||||||
/// expressions within an import statement.
|
/// expressions within an import statement.
|
||||||
///
|
///
|
||||||
pub fn parse_package_accesses(&mut self, span: &Span) -> SyntaxResult<Vec<PackageAccess>> {
|
pub fn parse_package_accesses(&mut self, span: &Span) -> Result<Vec<PackageAccess>> {
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
self.expect(Token::LeftParen)?;
|
self.expect(Token::LeftParen)?;
|
||||||
while self.eat(Token::RightParen).is_none() {
|
while self.eat(Token::RightParen).is_none() {
|
||||||
@ -177,7 +177,7 @@ impl ParserContext {
|
|||||||
/// Returns a [`PackageAccess`] AST node if the next tokens represent a package access expression
|
/// Returns a [`PackageAccess`] AST node if the next tokens represent a package access expression
|
||||||
/// within an import statement.
|
/// within an import statement.
|
||||||
///
|
///
|
||||||
pub fn parse_package_access(&mut self) -> SyntaxResult<PackageAccess> {
|
pub fn parse_package_access(&mut self) -> Result<PackageAccess> {
|
||||||
if let Some(SpannedToken { span, .. }) = self.eat(Token::Mul) {
|
if let Some(SpannedToken { span, .. }) = self.eat(Token::Mul) {
|
||||||
Ok(PackageAccess::Star { span })
|
Ok(PackageAccess::Star { span })
|
||||||
} else {
|
} else {
|
||||||
@ -211,7 +211,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns an [`Identifier`] AST node if the next tokens represent a valid package name.
|
/// Returns an [`Identifier`] AST node if the next tokens represent a valid package name.
|
||||||
///
|
///
|
||||||
pub fn parse_package_name(&mut self) -> SyntaxResult<Identifier> {
|
pub fn parse_package_name(&mut self) -> Result<Identifier> {
|
||||||
// Build the package name, starting with valid characters up to a dash `-` (Token::Minus).
|
// Build the package name, starting with valid characters up to a dash `-` (Token::Minus).
|
||||||
let mut base = self.expect_loose_identifier()?;
|
let mut base = self.expect_loose_identifier()?;
|
||||||
|
|
||||||
@ -266,7 +266,7 @@ impl ParserContext {
|
|||||||
/// Returns a [`PackageOrPackages`] AST node if the next tokens represent a valid package import
|
/// Returns a [`PackageOrPackages`] AST node if the next tokens represent a valid package import
|
||||||
/// with accesses.
|
/// with accesses.
|
||||||
///
|
///
|
||||||
pub fn parse_package_path(&mut self) -> SyntaxResult<PackageOrPackages> {
|
pub fn parse_package_path(&mut self) -> Result<PackageOrPackages> {
|
||||||
let package_name = self.parse_package_name()?;
|
let package_name = self.parse_package_name()?;
|
||||||
self.expect(Token::Dot)?;
|
self.expect(Token::Dot)?;
|
||||||
if self.peek()?.token == Token::LeftParen {
|
if self.peek()?.token == Token::LeftParen {
|
||||||
@ -289,7 +289,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`ImportStatement`] AST node if the next tokens represent an import statement.
|
/// Returns a [`ImportStatement`] AST node if the next tokens represent an import statement.
|
||||||
///
|
///
|
||||||
pub fn parse_import(&mut self) -> SyntaxResult<ImportStatement> {
|
pub fn parse_import(&mut self) -> Result<ImportStatement> {
|
||||||
self.expect(Token::Import)?;
|
self.expect(Token::Import)?;
|
||||||
let package_or_packages = self.parse_package_path()?;
|
let package_or_packages = self.parse_package_path()?;
|
||||||
self.expect(Token::Semicolon)?;
|
self.expect(Token::Semicolon)?;
|
||||||
@ -303,7 +303,7 @@ impl ParserContext {
|
|||||||
/// Returns a [`CircuitMember`] AST node if the next tokens represent a circuit member variable
|
/// Returns a [`CircuitMember`] AST node if the next tokens represent a circuit member variable
|
||||||
/// or circuit member function.
|
/// or circuit member function.
|
||||||
///
|
///
|
||||||
pub fn parse_circuit_declaration(&mut self) -> SyntaxResult<Vec<CircuitMember>> {
|
pub fn parse_circuit_declaration(&mut self) -> Result<Vec<CircuitMember>> {
|
||||||
let mut members = Vec::new();
|
let mut members = Vec::new();
|
||||||
let peeked = &self.peek()?.token;
|
let peeked = &self.peek()?.token;
|
||||||
let mut last_variable = peeked == &Token::Function || peeked == &Token::At;
|
let mut last_variable = peeked == &Token::Function || peeked == &Token::At;
|
||||||
@ -346,7 +346,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`CircuitMember`] AST node if the next tokens represent a circuit member variable.
|
/// Returns a [`CircuitMember`] AST node if the next tokens represent a circuit member variable.
|
||||||
///
|
///
|
||||||
pub fn parse_member_variable_declaration(&mut self) -> SyntaxResult<(CircuitMember, bool)> {
|
pub fn parse_member_variable_declaration(&mut self) -> Result<(CircuitMember, bool)> {
|
||||||
let name = self.expect_ident()?;
|
let name = self.expect_ident()?;
|
||||||
self.expect(Token::Colon)?;
|
self.expect(Token::Colon)?;
|
||||||
let type_ = self.parse_type()?.0;
|
let type_ = self.parse_type()?.0;
|
||||||
@ -367,7 +367,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`CircuitMember`] AST node if the next tokens represent a circuit member function.
|
/// Returns a [`CircuitMember`] AST node if the next tokens represent a circuit member function.
|
||||||
///
|
///
|
||||||
pub fn parse_member_function_declaration(&mut self) -> SyntaxResult<CircuitMember> {
|
pub fn parse_member_function_declaration(&mut self) -> Result<CircuitMember> {
|
||||||
let peeked = self.peek()?.clone();
|
let peeked = self.peek()?.clone();
|
||||||
if peeked.token == Token::Function || peeked.token == Token::At {
|
if peeked.token == Token::Function || peeked.token == Token::At {
|
||||||
let function = self.parse_function_declaration()?;
|
let function = self.parse_function_declaration()?;
|
||||||
@ -390,7 +390,7 @@ impl ParserContext {
|
|||||||
/// Returns an [`(Identifier, Circuit)`] tuple of AST nodes if the next tokens represent a
|
/// Returns an [`(Identifier, Circuit)`] tuple of AST nodes if the next tokens represent a
|
||||||
/// circuit name and definition statement.
|
/// circuit name and definition statement.
|
||||||
///
|
///
|
||||||
pub fn parse_circuit(&mut self) -> SyntaxResult<(Identifier, Circuit)> {
|
pub fn parse_circuit(&mut self) -> Result<(Identifier, Circuit)> {
|
||||||
self.expect(Token::Circuit)?;
|
self.expect(Token::Circuit)?;
|
||||||
let name = self.expect_ident()?;
|
let name = self.expect_ident()?;
|
||||||
self.expect(Token::LeftCurly)?;
|
self.expect(Token::LeftCurly)?;
|
||||||
@ -405,7 +405,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`FunctionInput`] AST node if the next tokens represent a function parameter.
|
/// Returns a [`FunctionInput`] AST node if the next tokens represent a function parameter.
|
||||||
///
|
///
|
||||||
pub fn parse_function_parameters(&mut self) -> SyntaxResult<FunctionInput> {
|
pub fn parse_function_parameters(&mut self) -> Result<FunctionInput> {
|
||||||
let const_ = self.eat(Token::Const);
|
let const_ = self.eat(Token::Const);
|
||||||
let mutable = self.eat(Token::Mut);
|
let mutable = self.eat(Token::Mut);
|
||||||
let mut name = if let Some(token) = self.eat(Token::LittleSelf) {
|
let mut name = if let Some(token) = self.eat(Token::LittleSelf) {
|
||||||
@ -453,7 +453,7 @@ impl ParserContext {
|
|||||||
/// Returns an [`(Identifier, Function)`] AST node if the next tokens represent a function name
|
/// Returns an [`(Identifier, Function)`] AST node if the next tokens represent a function name
|
||||||
/// and function definition.
|
/// and function definition.
|
||||||
///
|
///
|
||||||
pub fn parse_function_declaration(&mut self) -> SyntaxResult<(Identifier, Function)> {
|
pub fn parse_function_declaration(&mut self) -> Result<(Identifier, Function)> {
|
||||||
let mut annotations = Vec::new();
|
let mut annotations = Vec::new();
|
||||||
while self.peek_token().as_ref() == &Token::At {
|
while self.peek_token().as_ref() == &Token::At {
|
||||||
annotations.push(self.parse_annotation()?);
|
annotations.push(self.parse_annotation()?);
|
||||||
@ -490,7 +490,7 @@ impl ParserContext {
|
|||||||
/// Returns an [`(String, DefinitionStatement)`] AST node if the next tokens represent a global
|
/// Returns an [`(String, DefinitionStatement)`] AST node if the next tokens represent a global
|
||||||
/// const definition statement and assignment.
|
/// const definition statement and assignment.
|
||||||
///
|
///
|
||||||
pub fn parse_global_const_declaration(&mut self) -> SyntaxResult<(String, DefinitionStatement)> {
|
pub fn parse_global_const_declaration(&mut self) -> Result<(String, DefinitionStatement)> {
|
||||||
let statement = self.parse_definition_statement()?;
|
let statement = self.parse_definition_statement()?;
|
||||||
let variable_names = statement
|
let variable_names = statement
|
||||||
.variable_names
|
.variable_names
|
||||||
|
@ -32,11 +32,9 @@ use std::unimplemented;
|
|||||||
use crate::{tokenizer::*, Token};
|
use crate::{tokenizer::*, Token};
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use leo_ast::*;
|
use leo_ast::*;
|
||||||
use leo_errors::{LeoError, ParserError, Span};
|
use leo_errors::{ParserError, Result, Span};
|
||||||
|
|
||||||
pub type SyntaxResult<T> = Result<T, LeoError>;
|
pub(crate) fn assert_no_whitespace(left_span: &Span, right_span: &Span, left: &str, right: &str) -> Result<()> {
|
||||||
|
|
||||||
pub(crate) fn assert_no_whitespace(left_span: &Span, right_span: &Span, left: &str, right: &str) -> SyntaxResult<()> {
|
|
||||||
if left_span.col_stop != right_span.col_start {
|
if left_span.col_stop != right_span.col_start {
|
||||||
let mut error_span = left_span + right_span;
|
let mut error_span = left_span + right_span;
|
||||||
error_span.col_start = left_span.col_stop - 1;
|
error_span.col_start = left_span.col_stop - 1;
|
||||||
@ -48,7 +46,7 @@ pub(crate) fn assert_no_whitespace(left_span: &Span, right_span: &Span, left: &s
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new program from a given file path and source code text.
|
/// Creates a new program from a given file path and source code text.
|
||||||
pub fn parse(path: &str, source: &str) -> SyntaxResult<Program> {
|
pub fn parse(path: &str, source: &str) -> Result<Program> {
|
||||||
let mut tokens = ParserContext::new(crate::tokenize(path, source.into())?);
|
let mut tokens = ParserContext::new(crate::tokenize(path, source.into())?);
|
||||||
|
|
||||||
tokens.parse_program()
|
tokens.parse_program()
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use leo_errors::ParserError;
|
use leo_errors::{ParserError, Result};
|
||||||
|
|
||||||
const ASSIGN_TOKENS: &[Token] = &[
|
const ASSIGN_TOKENS: &[Token] = &[
|
||||||
Token::Assign,
|
Token::Assign,
|
||||||
@ -41,7 +41,7 @@ impl ParserContext {
|
|||||||
/// Returns an [`Identifier`] AST node if the given [`Expression`] AST node evaluates to an
|
/// Returns an [`Identifier`] AST node if the given [`Expression`] AST node evaluates to an
|
||||||
/// identifier access. The access is stored in the given accesses.
|
/// identifier access. The access is stored in the given accesses.
|
||||||
///
|
///
|
||||||
pub fn construct_assignee_access(expr: Expression, accesses: &mut Vec<AssigneeAccess>) -> SyntaxResult<Identifier> {
|
pub fn construct_assignee_access(expr: Expression, accesses: &mut Vec<AssigneeAccess>) -> Result<Identifier> {
|
||||||
let identifier;
|
let identifier;
|
||||||
match expr {
|
match expr {
|
||||||
Expression::CircuitMemberAccess(expr) => {
|
Expression::CircuitMemberAccess(expr) => {
|
||||||
@ -72,7 +72,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns an [`Assignee`] AST node from the given [`Expression`] AST node with accesses.
|
/// Returns an [`Assignee`] AST node from the given [`Expression`] AST node with accesses.
|
||||||
///
|
///
|
||||||
pub fn construct_assignee(expr: Expression) -> SyntaxResult<Assignee> {
|
pub fn construct_assignee(expr: Expression) -> Result<Assignee> {
|
||||||
let expr_span = expr.span().clone();
|
let expr_span = expr.span().clone();
|
||||||
let mut accesses = Vec::new();
|
let mut accesses = Vec::new();
|
||||||
let identifier = Self::construct_assignee_access(expr, &mut accesses)?;
|
let identifier = Self::construct_assignee_access(expr, &mut accesses)?;
|
||||||
@ -87,7 +87,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`Statement`] AST node if the next tokens represent a statement.
|
/// Returns a [`Statement`] AST node if the next tokens represent a statement.
|
||||||
///
|
///
|
||||||
pub fn parse_statement(&mut self) -> SyntaxResult<Statement> {
|
pub fn parse_statement(&mut self) -> Result<Statement> {
|
||||||
match &self.peek()?.token {
|
match &self.peek()?.token {
|
||||||
Token::Return => Ok(Statement::Return(self.parse_return_statement()?)),
|
Token::Return => Ok(Statement::Return(self.parse_return_statement()?)),
|
||||||
Token::If => Ok(Statement::Conditional(self.parse_conditional_statement()?)),
|
Token::If => Ok(Statement::Conditional(self.parse_conditional_statement()?)),
|
||||||
@ -102,7 +102,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`Block`] AST node if the next tokens represent a assign, or expression statement.
|
/// Returns a [`Block`] AST node if the next tokens represent a assign, or expression statement.
|
||||||
///
|
///
|
||||||
pub fn parse_assign_statement(&mut self) -> SyntaxResult<Statement> {
|
pub fn parse_assign_statement(&mut self) -> Result<Statement> {
|
||||||
let expr = self.parse_expression()?;
|
let expr = self.parse_expression()?;
|
||||||
|
|
||||||
if let Some(operator) = self.eat_any(ASSIGN_TOKENS) {
|
if let Some(operator) = self.eat_any(ASSIGN_TOKENS) {
|
||||||
@ -144,7 +144,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`Block`] AST node if the next tokens represent a block of statements.
|
/// Returns a [`Block`] AST node if the next tokens represent a block of statements.
|
||||||
///
|
///
|
||||||
pub fn parse_block(&mut self) -> SyntaxResult<Block> {
|
pub fn parse_block(&mut self) -> Result<Block> {
|
||||||
let start = self.expect(Token::LeftCurly)?;
|
let start = self.expect(Token::LeftCurly)?;
|
||||||
|
|
||||||
let mut statements = Vec::new();
|
let mut statements = Vec::new();
|
||||||
@ -166,7 +166,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`ReturnStatement`] AST node if the next tokens represent a return statement.
|
/// Returns a [`ReturnStatement`] AST node if the next tokens represent a return statement.
|
||||||
///
|
///
|
||||||
pub fn parse_return_statement(&mut self) -> SyntaxResult<ReturnStatement> {
|
pub fn parse_return_statement(&mut self) -> Result<ReturnStatement> {
|
||||||
let start = self.expect(Token::Return)?;
|
let start = self.expect(Token::Return)?;
|
||||||
let expr = self.parse_expression()?;
|
let expr = self.parse_expression()?;
|
||||||
self.expect(Token::Semicolon)?;
|
self.expect(Token::Semicolon)?;
|
||||||
@ -180,7 +180,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`ConditionalStatement`] AST node if the next tokens represent a conditional statement.
|
/// Returns a [`ConditionalStatement`] AST node if the next tokens represent a conditional statement.
|
||||||
///
|
///
|
||||||
pub fn parse_conditional_statement(&mut self) -> SyntaxResult<ConditionalStatement> {
|
pub fn parse_conditional_statement(&mut self) -> Result<ConditionalStatement> {
|
||||||
let start = self.expect(Token::If)?;
|
let start = self.expect(Token::If)?;
|
||||||
self.fuzzy_struct_state = true;
|
self.fuzzy_struct_state = true;
|
||||||
let expr = self.parse_conditional_expression()?;
|
let expr = self.parse_conditional_expression()?;
|
||||||
@ -209,7 +209,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns an [`IterationStatement`] AST node if the next tokens represent an iteration statement.
|
/// Returns an [`IterationStatement`] AST node if the next tokens represent an iteration statement.
|
||||||
///
|
///
|
||||||
pub fn parse_loop_statement(&mut self) -> SyntaxResult<IterationStatement> {
|
pub fn parse_loop_statement(&mut self) -> Result<IterationStatement> {
|
||||||
let start_span = self.expect(Token::For)?;
|
let start_span = self.expect(Token::For)?;
|
||||||
let ident = self.expect_ident()?;
|
let ident = self.expect_ident()?;
|
||||||
self.expect(Token::In)?;
|
self.expect(Token::In)?;
|
||||||
@ -234,7 +234,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`ConsoleArgs`] AST node if the next tokens represent a formatted string.
|
/// Returns a [`ConsoleArgs`] AST node if the next tokens represent a formatted string.
|
||||||
///
|
///
|
||||||
pub fn parse_console_args(&mut self) -> SyntaxResult<ConsoleArgs> {
|
pub fn parse_console_args(&mut self) -> Result<ConsoleArgs> {
|
||||||
let start_span;
|
let start_span;
|
||||||
let string = match self.expect_any()? {
|
let string = match self.expect_any()? {
|
||||||
SpannedToken {
|
SpannedToken {
|
||||||
@ -267,7 +267,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`ConsoleStatement`] AST node if the next tokens represent a console statement.
|
/// Returns a [`ConsoleStatement`] AST node if the next tokens represent a console statement.
|
||||||
///
|
///
|
||||||
pub fn parse_console_statement(&mut self) -> SyntaxResult<ConsoleStatement> {
|
pub fn parse_console_statement(&mut self) -> Result<ConsoleStatement> {
|
||||||
let keyword = self.expect(Token::Console)?;
|
let keyword = self.expect(Token::Console)?;
|
||||||
self.expect(Token::Dot)?;
|
self.expect(Token::Dot)?;
|
||||||
let function = self.expect_ident()?;
|
let function = self.expect_ident()?;
|
||||||
@ -296,7 +296,7 @@ impl ParserContext {
|
|||||||
/// Returns a [`VariableName`] AST node if the next tokens represent a variable name with
|
/// Returns a [`VariableName`] AST node if the next tokens represent a variable name with
|
||||||
/// valid keywords.
|
/// valid keywords.
|
||||||
///
|
///
|
||||||
pub fn parse_variable_name(&mut self, span: &SpannedToken) -> SyntaxResult<VariableName> {
|
pub fn parse_variable_name(&mut self, span: &SpannedToken) -> Result<VariableName> {
|
||||||
let mutable = self.eat(Token::Mut);
|
let mutable = self.eat(Token::Mut);
|
||||||
if let Some(mutable) = &mutable {
|
if let Some(mutable) = &mutable {
|
||||||
return Err(ParserError::let_mut_statement(&(&mutable.span + &span.span)).into());
|
return Err(ParserError::let_mut_statement(&(&mutable.span + &span.span)).into());
|
||||||
@ -313,7 +313,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns a [`DefinitionStatement`] AST node if the next tokens represent a definition statement.
|
/// Returns a [`DefinitionStatement`] AST node if the next tokens represent a definition statement.
|
||||||
///
|
///
|
||||||
pub fn parse_definition_statement(&mut self) -> SyntaxResult<DefinitionStatement> {
|
pub fn parse_definition_statement(&mut self) -> Result<DefinitionStatement> {
|
||||||
let declare = self.expect_oneof(&[Token::Let, Token::Const])?;
|
let declare = self.expect_oneof(&[Token::Let, Token::Const])?;
|
||||||
let mut variable_names = Vec::new();
|
let mut variable_names = Vec::new();
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use leo_errors::ParserError;
|
use leo_errors::{ParserError, Result};
|
||||||
|
|
||||||
const TYPE_TOKENS: &[Token] = &[
|
const TYPE_TOKENS: &[Token] = &[
|
||||||
Token::I8,
|
Token::I8,
|
||||||
@ -58,7 +58,7 @@ impl ParserContext {
|
|||||||
///
|
///
|
||||||
/// Returns an [`ArrayDimensions`] AST node if the next tokens represent dimensions for an array type.
|
/// Returns an [`ArrayDimensions`] AST node if the next tokens represent dimensions for an array type.
|
||||||
///
|
///
|
||||||
pub fn parse_array_dimensions(&mut self) -> SyntaxResult<ArrayDimensions> {
|
pub fn parse_array_dimensions(&mut self) -> Result<ArrayDimensions> {
|
||||||
Ok(if let Some((int, _)) = self.eat_int() {
|
Ok(if let Some((int, _)) = self.eat_int() {
|
||||||
ArrayDimensions(vec![int])
|
ArrayDimensions(vec![int])
|
||||||
} else {
|
} else {
|
||||||
@ -84,7 +84,7 @@ impl ParserContext {
|
|||||||
/// Returns a [`(Type, Span)`] tuple of AST nodes if the next token represents a type. Also
|
/// Returns a [`(Type, Span)`] tuple of AST nodes if the next token represents a type. Also
|
||||||
/// returns the span of the parsed token.
|
/// returns the span of the parsed token.
|
||||||
///
|
///
|
||||||
pub fn parse_type(&mut self) -> SyntaxResult<(Type, Span)> {
|
pub fn parse_type(&mut self) -> Result<(Type, Span)> {
|
||||||
Ok(if let Some(token) = self.eat(Token::BigSelf) {
|
Ok(if let Some(token) = self.eat(Token::BigSelf) {
|
||||||
(Type::SelfType, token.span)
|
(Type::SelfType, token.span)
|
||||||
} else if let Some(ident) = self.eat_identifier() {
|
} else if let Some(ident) = self.eat_identifier() {
|
||||||
|
@ -17,11 +17,11 @@
|
|||||||
use leo_ast::Ast;
|
use leo_ast::Ast;
|
||||||
#[cfg(not(feature = "ci_skip"))]
|
#[cfg(not(feature = "ci_skip"))]
|
||||||
use leo_ast::Program;
|
use leo_ast::Program;
|
||||||
use leo_errors::LeoError;
|
use leo_errors::{LeoError, Result};
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
fn to_ast(program_filepath: &Path) -> Result<Ast, LeoError> {
|
fn to_ast(program_filepath: &Path) -> Result<Ast> {
|
||||||
let program_string = std::fs::read_to_string(program_filepath).expect("failed to open test");
|
let program_string = std::fs::read_to_string(program_filepath).expect("failed to open test");
|
||||||
|
|
||||||
// Parses the Leo file and constructs a leo ast.
|
// Parses the Leo file and constructs a leo ast.
|
||||||
|
Loading…
Reference in New Issue
Block a user