mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 07:07:07 +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/>.
|
||||
|
||||
use crate::{Circuit, Identifier, IntegerType, Type};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use num_bigint::BigInt;
|
||||
@ -314,7 +314,7 @@ impl ConstInt {
|
||||
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 {
|
||||
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))?),
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_ast::IntegerType;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -84,7 +84,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayAccessExpression> for ArrayAccessExpression<'
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ArrayAccessExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<ArrayAccessExpression<'a>, LeoError> {
|
||||
) -> Result<ArrayAccessExpression<'a>> {
|
||||
let array = <&Expression<'a>>::from_ast(
|
||||
scope,
|
||||
&*value.array,
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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;
|
||||
|
||||
@ -69,7 +69,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ArrayInitExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<ArrayInitExpression<'a>, LeoError> {
|
||||
) -> Result<ArrayInitExpression<'a>> {
|
||||
let (mut expected_item, expected_len) = match expected_type {
|
||||
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
|
||||
None => (None, None),
|
||||
@ -86,7 +86,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgError::parse_dimension_error(&value.span))?)
|
||||
})
|
||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
let len = *dimensions
|
||||
.get(0)
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_ast::SpreadOrExpression;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -104,7 +104,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInlineExpression> for ArrayInlineExpression<'
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ArrayInlineExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<ArrayInlineExpression<'a>, LeoError> {
|
||||
) -> Result<ArrayInlineExpression<'a>> {
|
||||
let (mut expected_item, expected_len) = match expected_type {
|
||||
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
|
||||
None => (None, None),
|
||||
@ -181,7 +181,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInlineExpression> for ArrayInlineExpression<'
|
||||
Ok((Cell::new(expr), true))
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>, LeoError>>()?,
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
};
|
||||
if let Some(expected_len) = expected_len {
|
||||
if len != expected_len {
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_ast::IntegerType;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -103,7 +103,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ArrayRangeAccessExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<ArrayRangeAccessExpression<'a>, LeoError> {
|
||||
) -> Result<ArrayRangeAccessExpression<'a>> {
|
||||
let (expected_array, expected_len) = match expected_type.clone() {
|
||||
Some(PartialType::Array(element, len)) => (Some(PartialType::Array(element, None)), len),
|
||||
None => (None, None),
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
pub use leo_ast::{BinaryOperation, BinaryOperationClass};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -117,7 +117,7 @@ impl<'a> FromAst<'a, leo_ast::BinaryExpression> for BinaryExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::BinaryExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<BinaryExpression<'a>, LeoError> {
|
||||
) -> Result<BinaryExpression<'a>> {
|
||||
let class = value.op.class();
|
||||
let expected_type = match class {
|
||||
BinaryOperationClass::Boolean => match expected_type {
|
||||
|
@ -28,7 +28,7 @@ use crate::{
|
||||
Type,
|
||||
};
|
||||
pub use leo_ast::{BinaryOperation, Node as AstNode};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -88,7 +88,7 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::CallExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<CallExpression<'a>, LeoError> {
|
||||
) -> Result<CallExpression<'a>> {
|
||||
let (target, function) = match &*value.function {
|
||||
leo_ast::Expression::Identifier(name) => (
|
||||
None,
|
||||
@ -199,7 +199,7 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
|
||||
}
|
||||
Ok(Cell::new(converted))
|
||||
})
|
||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
if function.is_test() {
|
||||
return Err(AsgError::call_test_function(&value.span).into());
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
pub use leo_ast::UnaryOperation;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -76,7 +76,7 @@ impl<'a> FromAst<'a, leo_ast::CastExpression> for CastExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::CastExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<CastExpression<'a>, LeoError> {
|
||||
) -> Result<CastExpression<'a>> {
|
||||
let target_type = scope.resolve_ast_type(&value.target_type, &value.span)?;
|
||||
if let Some(expected_type) = &expected_type {
|
||||
if !expected_type.matches(&target_type) {
|
||||
|
@ -28,7 +28,7 @@ use crate::{
|
||||
Type,
|
||||
};
|
||||
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -102,7 +102,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitMemberAccessExpression> for CircuitAccessEx
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::CircuitMemberAccessExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<CircuitAccessExpression<'a>, LeoError> {
|
||||
) -> Result<CircuitAccessExpression<'a>> {
|
||||
let target = <&'a Expression<'a>>::from_ast(scope, &*value.circuit, None)?;
|
||||
let circuit = match target.get_type() {
|
||||
Some(Type::Circuit(circuit)) => circuit,
|
||||
@ -171,7 +171,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitStaticFunctionAccessExpression> for Circuit
|
||||
scope: &Scope<'a>,
|
||||
value: &leo_ast::CircuitStaticFunctionAccessExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<CircuitAccessExpression<'a>, LeoError> {
|
||||
) -> Result<CircuitAccessExpression<'a>> {
|
||||
let circuit = match &*value.circuit {
|
||||
leo_ast::Expression::Identifier(name) => scope
|
||||
.resolve_circuit(&name.name)
|
||||
|
@ -28,7 +28,7 @@ use crate::{
|
||||
Type,
|
||||
};
|
||||
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use std::cell::Cell;
|
||||
@ -93,7 +93,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitInitExpression> for CircuitInitExpression<'
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::CircuitInitExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<CircuitInitExpression<'a>, LeoError> {
|
||||
) -> Result<CircuitInitExpression<'a>> {
|
||||
let circuit = scope
|
||||
.resolve_circuit(&value.name.name)
|
||||
.ok_or_else(|| AsgError::unresolved_circuit(&value.name.name, &value.name.span))?;
|
||||
|
@ -28,7 +28,7 @@ use crate::{
|
||||
Type,
|
||||
};
|
||||
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -78,7 +78,7 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
_scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ValueExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Constant<'a>, LeoError> {
|
||||
) -> Result<Constant<'a>> {
|
||||
use leo_ast::ValueExpression::*;
|
||||
Ok(match value {
|
||||
Address(value, span) => {
|
||||
|
@ -66,7 +66,7 @@ mod cast;
|
||||
pub use cast::*;
|
||||
|
||||
use crate::{ConstValue, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Expression<'a> {
|
||||
@ -285,7 +285,7 @@ impl<'a> FromAst<'a, leo_ast::Expression> for &'a Expression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::Expression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
use leo_ast::Expression::*;
|
||||
let expression = match value {
|
||||
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/>.
|
||||
|
||||
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;
|
||||
|
||||
@ -79,7 +79,7 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::TernaryExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<TernaryExpression<'a>, LeoError> {
|
||||
) -> Result<TernaryExpression<'a>> {
|
||||
let if_true = Cell::new(<&Expression<'a>>::from_ast(
|
||||
scope,
|
||||
&*value.if_true,
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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;
|
||||
|
||||
@ -75,7 +75,7 @@ impl<'a> FromAst<'a, leo_ast::TupleAccessExpression> for TupleAccessExpression<'
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::TupleAccessExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<TupleAccessExpression<'a>, LeoError> {
|
||||
) -> Result<TupleAccessExpression<'a>> {
|
||||
let index = value
|
||||
.index
|
||||
.value
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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;
|
||||
|
||||
@ -81,7 +81,7 @@ impl<'a> FromAst<'a, leo_ast::TupleInitExpression> for TupleInitExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::TupleInitExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<TupleInitExpression<'a>, LeoError> {
|
||||
) -> Result<TupleInitExpression<'a>> {
|
||||
let tuple_types = match expected_type {
|
||||
Some(PartialType::Tuple(sub_types)) => Some(sub_types),
|
||||
None => None,
|
||||
@ -120,7 +120,7 @@ impl<'a> FromAst<'a, leo_ast::TupleInitExpression> for TupleInitExpression<'a> {
|
||||
)
|
||||
.map(Cell::new)
|
||||
})
|
||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
Ok(TupleInitExpression {
|
||||
parent: Cell::new(None),
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
pub use leo_ast::UnaryOperation;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -90,7 +90,7 @@ impl<'a> FromAst<'a, leo_ast::UnaryExpression> for UnaryExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::UnaryExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<UnaryExpression<'a>, LeoError> {
|
||||
) -> Result<UnaryExpression<'a>> {
|
||||
let expected_type = match value.op {
|
||||
UnaryOperation::Not => match expected_type.map(|x| x.full()).flatten() {
|
||||
Some(Type::Boolean) | None => Some(Type::Boolean),
|
||||
|
@ -29,7 +29,7 @@ use crate::{
|
||||
Variable,
|
||||
};
|
||||
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -135,7 +135,7 @@ impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::Identifier,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<&'a Expression<'a>, LeoError> {
|
||||
) -> Result<&'a Expression<'a>> {
|
||||
let variable = if value.name.as_ref() == "input" {
|
||||
if let Some(input) = scope.resolve_input() {
|
||||
input.container
|
||||
|
@ -19,7 +19,7 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::{AsgContext, Program};
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
||||
@ -29,7 +29,7 @@ pub trait ImportResolver<'a> {
|
||||
context: AsgContext<'a>,
|
||||
package_segments: &[&str],
|
||||
span: &Span,
|
||||
) -> Result<Option<Program<'a>>, LeoError>;
|
||||
) -> Result<Option<Program<'a>>>;
|
||||
}
|
||||
|
||||
pub struct NullImportResolver;
|
||||
@ -40,7 +40,7 @@ impl<'a> ImportResolver<'a> for NullImportResolver {
|
||||
_context: AsgContext<'a>,
|
||||
_package_segments: &[&str],
|
||||
_span: &Span,
|
||||
) -> Result<Option<Program<'a>>, LeoError> {
|
||||
) -> Result<Option<Program<'a>>> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@ -65,7 +65,7 @@ impl<'a, 'b, T: ImportResolver<'b>> ImportResolver<'b> for CoreImportResolver<'a
|
||||
context: AsgContext<'b>,
|
||||
package_segments: &[&str],
|
||||
span: &Span,
|
||||
) -> Result<Option<Program<'b>>, LeoError> {
|
||||
) -> Result<Option<Program<'b>>> {
|
||||
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" {
|
||||
Ok(crate::resolve_core_module(context, &*package_segments[1..].join("."))?)
|
||||
} else {
|
||||
@ -84,7 +84,7 @@ impl<'a> ImportResolver<'a> for MockedImportResolver<'a> {
|
||||
_context: AsgContext<'a>,
|
||||
package_segments: &[&str],
|
||||
_span: &Span,
|
||||
) -> Result<Option<Program<'a>>, LeoError> {
|
||||
) -> Result<Option<Program<'a>>> {
|
||||
Ok(self.packages.get(&package_segments.join(".")).cloned())
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ pub mod context;
|
||||
pub use context::*;
|
||||
|
||||
pub use leo_ast::{Ast, Identifier};
|
||||
use leo_errors::LeoError;
|
||||
use leo_errors::Result;
|
||||
|
||||
/// The abstract semantic graph (ASG) for a Leo program.
|
||||
///
|
||||
@ -92,7 +92,7 @@ impl<'a> Asg<'a> {
|
||||
context: AsgContext<'a>,
|
||||
ast: Y,
|
||||
resolver: &mut T,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
context,
|
||||
asg: Program::new(context, ast.as_ref(), resolver)?,
|
||||
@ -125,7 +125,7 @@ pub fn load_asg<'a, T: ImportResolver<'a>>(
|
||||
context: AsgContext<'a>,
|
||||
content: &str,
|
||||
resolver: &mut T,
|
||||
) -> Result<Program<'a>, LeoError> {
|
||||
) -> Result<Program<'a>> {
|
||||
// Parses the Leo file and constructs a grammar ast.
|
||||
let ast = leo_parser::parse_ast("input.leo", content)?;
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
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.
|
||||
pub trait Node {
|
||||
@ -26,7 +26,7 @@ pub trait Node {
|
||||
pub(super) trait FromAst<'a, T: leo_ast::Node + 'static>: Sized {
|
||||
// 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)
|
||||
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> {
|
||||
|
@ -15,8 +15,8 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::Program;
|
||||
use leo_errors::LeoError;
|
||||
use leo_errors::Result;
|
||||
|
||||
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
|
||||
|
||||
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): 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 {
|
||||
"unstable.blake2s" => {
|
||||
let asg = crate::load_asg(
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Function, Identifier, Node, Scope, Type};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::cell::RefCell;
|
||||
@ -54,7 +54,7 @@ impl<'a> Node for 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 circuit = scope.context.alloc_circuit(Circuit {
|
||||
@ -85,7 +85,7 @@ impl<'a> Circuit<'a> {
|
||||
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 circuits = scope.circuits.borrow();
|
||||
|
||||
@ -118,7 +118,7 @@ impl<'a> Circuit<'a> {
|
||||
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() {
|
||||
match member {
|
||||
leo_ast::CircuitMember::CircuitVariable(..) => {}
|
||||
|
@ -29,7 +29,7 @@ use crate::{
|
||||
use indexmap::IndexMap;
|
||||
pub use leo_ast::Annotation;
|
||||
use leo_ast::FunctionInput;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
@ -67,7 +67,7 @@ impl<'a> PartialEq for Function<'a> {
|
||||
impl<'a> Eq for 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
|
||||
.output
|
||||
.as_ref()
|
||||
@ -126,7 +126,7 @@ impl<'a> Function<'a> {
|
||||
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 {
|
||||
let circuit = self.circuit.get();
|
||||
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 leo_ast::{Identifier, PackageAccess, PackageOrPackages};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::cell::{Cell, RefCell};
|
||||
@ -137,7 +137,7 @@ impl<'a> Program<'a> {
|
||||
context: AsgContext<'a>,
|
||||
program: &leo_ast::Program,
|
||||
import_resolver: &mut T,
|
||||
) -> Result<Program<'a>, LeoError> {
|
||||
) -> Result<Program<'a>> {
|
||||
// Recursively extract imported symbols.
|
||||
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
|
||||
for import in program.imports.iter() {
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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 std::cell::{Cell, RefCell};
|
||||
@ -174,7 +174,7 @@ impl<'a> Scope<'a> {
|
||||
///
|
||||
/// 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::*;
|
||||
Ok(match type_ {
|
||||
Address => Type::Address,
|
||||
@ -198,7 +198,7 @@ impl<'a> Scope<'a> {
|
||||
sub_types
|
||||
.iter()
|
||||
.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(
|
||||
self.resolve_circuit_self()
|
||||
|
@ -32,7 +32,7 @@ use crate::{
|
||||
};
|
||||
pub use leo_ast::AssignOperation;
|
||||
use leo_ast::AssigneeAccess as AstAssigneeAccess;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -65,7 +65,7 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::AssignStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
let (name, span) = (
|
||||
&statement.assignee.identifier.name.clone(),
|
||||
&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 left = left
|
||||
.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())
|
||||
})
|
||||
.transpose()?;
|
||||
let right = right
|
||||
.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)
|
||||
})
|
||||
.transpose()?;
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{FromAst, Node, PartialType, Scope, Statement};
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -38,7 +38,7 @@ impl<'a> FromAst<'a, leo_ast::Block> for BlockStatement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::Block,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
let new_scope = scope.make_subscope();
|
||||
|
||||
let mut output = vec![];
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{BlockStatement, Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -39,7 +39,7 @@ impl<'a> FromAst<'a, leo_ast::ConditionalStatement> for ConditionalStatement<'a>
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::ConditionalStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
let condition = <&Expression<'a>>::from_ast(scope, &statement.condition, Some(Type::Boolean.into()))?;
|
||||
let result = scope.context.alloc_statement(Statement::Block(BlockStatement::from_ast(
|
||||
scope,
|
||||
@ -49,7 +49,7 @@ impl<'a> FromAst<'a, leo_ast::ConditionalStatement> for ConditionalStatement<'a>
|
||||
let next = statement
|
||||
.next
|
||||
.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()?;
|
||||
|
||||
Ok(ConditionalStatement {
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{CharValue, Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||
use leo_ast::ConsoleFunction as AstConsoleFunction;
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -53,7 +53,7 @@ impl<'a> FromAst<'a, leo_ast::ConsoleArgs> for ConsoleArgs<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ConsoleArgs,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
let mut parameters = vec![];
|
||||
for parameter in value.parameters.iter() {
|
||||
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>,
|
||||
statement: &leo_ast::ConsoleStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
Ok(ConsoleStatement {
|
||||
parent: Cell::new(None),
|
||||
span: Some(statement.span.clone()),
|
||||
|
@ -15,7 +15,7 @@
|
||||
// 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 leo_errors::{AsgError, LeoError, Span};
|
||||
use leo_errors::{AsgError, Result, Span};
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
@ -54,7 +54,7 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::DefinitionStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
let type_ = statement
|
||||
.type_
|
||||
.as_ref()
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, FromAst, Node, PartialType, Scope, Statement};
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -37,7 +37,7 @@ impl<'a> FromAst<'a, leo_ast::ExpressionStatement> for ExpressionStatement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::ExpressionStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
let expression = <&Expression<'a>>::from_ast(scope, &statement.expression, None)?;
|
||||
|
||||
Ok(ExpressionStatement {
|
||||
|
@ -17,7 +17,7 @@
|
||||
use leo_ast::IntegerType;
|
||||
|
||||
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};
|
||||
|
||||
@ -43,7 +43,7 @@ impl<'a> FromAst<'a, leo_ast::IterationStatement> for &'a Statement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::IterationStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
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 stop = <&Expression<'a>>::from_ast(scope, &statement.stop, expected_index_type)?;
|
||||
|
@ -43,7 +43,7 @@ mod return_;
|
||||
pub use return_::*;
|
||||
|
||||
use crate::{FromAst, Node, PartialType, Scope};
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Statement<'a> {
|
||||
@ -80,7 +80,7 @@ impl<'a> FromAst<'a, leo_ast::Statement> for &'a Statement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::Statement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<&'a Statement<'a>, LeoError> {
|
||||
) -> Result<&'a Statement<'a>> {
|
||||
use leo_ast::Statement::*;
|
||||
Ok(match value {
|
||||
Return(statement) => scope
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
#[derive(Clone)]
|
||||
@ -36,7 +36,7 @@ impl<'a> FromAst<'a, leo_ast::ReturnStatement> for ReturnStatement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::ReturnStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, LeoError> {
|
||||
) -> Result<Self> {
|
||||
let return_type: Option<Type> = scope
|
||||
.resolve_current_function()
|
||||
.map(|x| x.output.clone())
|
||||
|
@ -62,7 +62,7 @@ pub use self::types::*;
|
||||
mod node;
|
||||
pub use node::*;
|
||||
|
||||
use leo_errors::{AstError, LeoError};
|
||||
use leo_errors::{AstError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
|
||||
@ -84,7 +84,7 @@ impl Ast {
|
||||
}
|
||||
|
||||
/// 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())?;
|
||||
Ok(())
|
||||
}
|
||||
@ -99,13 +99,13 @@ impl Ast {
|
||||
}
|
||||
|
||||
/// 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)
|
||||
.map_err(|e| AstError::failed_to_convert_ast_to_json_string(&e, Backtrace::new()))?)
|
||||
}
|
||||
|
||||
/// 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);
|
||||
let file = std::fs::File::create(&path)
|
||||
.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.
|
||||
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)
|
||||
.map_err(|e| AstError::failed_to_read_json_string_to_ast(&e, Backtrace::new()))?;
|
||||
Ok(Self { ast })
|
||||
}
|
||||
|
||||
/// 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)
|
||||
.map_err(|e| AstError::failed_to_read_json_file(&path, &e, Backtrace::new()))?;
|
||||
Self::from_json_string(&data)
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::*;
|
||||
use leo_errors::{AstError, LeoError, Span};
|
||||
use leo_errors::{AstError, Result, Span};
|
||||
|
||||
/// Replace Self when it is in a enclosing circuit type.
|
||||
/// Error when Self is outside an enclosing circuit type.
|
||||
@ -44,7 +44,7 @@ impl Canonicalizer {
|
||||
start: Expression,
|
||||
accesses: &[AssigneeAccess],
|
||||
span: &Span,
|
||||
) -> Result<Box<Expression>, LeoError> {
|
||||
) -> Result<Box<Expression>> {
|
||||
let mut left = Box::new(start);
|
||||
|
||||
for access in accesses.iter() {
|
||||
@ -84,7 +84,7 @@ impl Canonicalizer {
|
||||
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 {
|
||||
AssignOperation::Assign => unreachable!(),
|
||||
AssignOperation::Add => Ok(BinaryOperation::Add),
|
||||
@ -465,7 +465,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
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 {
|
||||
Type::Array(type_, mut dimensions) => {
|
||||
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() {
|
||||
return Err(AstError::empty_string(span).into());
|
||||
}
|
||||
@ -550,7 +550,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
&mut self,
|
||||
array_init: &ArrayInitExpression,
|
||||
element: Expression,
|
||||
) -> Result<ArrayInitExpression, LeoError> {
|
||||
) -> Result<ArrayInitExpression> {
|
||||
if array_init.dimensions.is_zero() {
|
||||
return Err(AstError::invalid_array_dimension_size(&array_init.span).into());
|
||||
}
|
||||
@ -599,7 +599,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
assign: &AssignStatement,
|
||||
assignee: Assignee,
|
||||
value: Expression,
|
||||
) -> Result<AssignStatement, LeoError> {
|
||||
) -> Result<AssignStatement> {
|
||||
match value {
|
||||
value if assign.operation != AssignOperation::Assign => {
|
||||
let left = self.canonicalize_accesses(
|
||||
@ -641,7 +641,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
input: Vec<FunctionInput>,
|
||||
output: Option<Type>,
|
||||
block: Block,
|
||||
) -> Result<Function, LeoError> {
|
||||
) -> Result<Function> {
|
||||
let new_output = match output {
|
||||
None => Some(Type::Tuple(vec![])),
|
||||
_ => output,
|
||||
@ -662,7 +662,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
_circuit: &Circuit,
|
||||
circuit_name: Identifier,
|
||||
members: Vec<CircuitMember>,
|
||||
) -> Result<Circuit, LeoError> {
|
||||
) -> Result<Circuit> {
|
||||
self.circuit_name = Some(circuit_name.clone());
|
||||
let circ = Circuit {
|
||||
circuit_name,
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
use crate::*;
|
||||
use indexmap::IndexMap;
|
||||
use leo_errors::{AstError, LeoError, Span};
|
||||
use leo_errors::{AstError, Result, Span};
|
||||
|
||||
pub struct ReconstructingDirector<R: ReconstructingReducer> {
|
||||
reducer: R,
|
||||
@ -30,7 +30,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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_ {
|
||||
Type::Array(type_, dimensions) => Type::Array(Box::new(self.reduce_type(type_, span)?), dimensions.clone()),
|
||||
Type::Tuple(types) => {
|
||||
@ -49,7 +49,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
Expression::Identifier(identifier) => Expression::Identifier(self.reduce_identifier(identifier)?),
|
||||
Expression::Value(value) => self.reduce_value(value)?,
|
||||
@ -82,15 +82,15 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
GroupValue::Tuple(group_tuple) => GroupValue::Tuple(Box::new(self.reduce_group_tuple(group_tuple)?)),
|
||||
_ => group_value.clone(),
|
||||
@ -99,11 +99,11 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
ValueExpression::Group(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)
|
||||
}
|
||||
|
||||
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 right = self.reduce_expression(&binary.right)?;
|
||||
|
||||
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)?;
|
||||
|
||||
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 if_true = self.reduce_expression(&ternary.if_true)?;
|
||||
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)
|
||||
}
|
||||
|
||||
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 target_type = self.reduce_type(&cast.target_type, &cast.span)?;
|
||||
|
||||
self.reducer.reduce_cast(cast, inner, target_type)
|
||||
}
|
||||
|
||||
pub fn reduce_array_inline(
|
||||
&mut self,
|
||||
array_inline: &ArrayInlineExpression,
|
||||
) -> Result<ArrayInlineExpression, LeoError> {
|
||||
pub fn reduce_array_inline(&mut self, array_inline: &ArrayInlineExpression) -> Result<ArrayInlineExpression> {
|
||||
let mut elements = vec![];
|
||||
for element in array_inline.elements.iter() {
|
||||
let reduced_element = match element {
|
||||
@ -164,16 +161,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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)?;
|
||||
|
||||
self.reducer.reduce_array_init(array_init, element)
|
||||
}
|
||||
|
||||
pub fn reduce_array_access(
|
||||
&mut self,
|
||||
array_access: &ArrayAccessExpression,
|
||||
) -> Result<ArrayAccessExpression, LeoError> {
|
||||
pub fn reduce_array_access(&mut self, array_access: &ArrayAccessExpression) -> Result<ArrayAccessExpression> {
|
||||
let array = self.reduce_expression(&array_access.array)?;
|
||||
let index = self.reduce_expression(&array_access.index)?;
|
||||
|
||||
@ -183,7 +177,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_array_range_access(
|
||||
&mut self,
|
||||
array_range_access: &ArrayRangeAccessExpression,
|
||||
) -> Result<ArrayRangeAccessExpression, LeoError> {
|
||||
) -> Result<ArrayRangeAccessExpression> {
|
||||
let array = self.reduce_expression(&array_range_access.array)?;
|
||||
let left = array_range_access
|
||||
.left
|
||||
@ -200,7 +194,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
.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![];
|
||||
for element in tuple_init.elements.iter() {
|
||||
elements.push(self.reduce_expression(element)?);
|
||||
@ -209,10 +203,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_tuple_init(tuple_init, elements)
|
||||
}
|
||||
|
||||
pub fn reduce_tuple_access(
|
||||
&mut self,
|
||||
tuple_access: &TupleAccessExpression,
|
||||
) -> Result<TupleAccessExpression, LeoError> {
|
||||
pub fn reduce_tuple_access(&mut self, tuple_access: &TupleAccessExpression) -> Result<TupleAccessExpression> {
|
||||
let tuple = self.reduce_expression(&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(
|
||||
&mut self,
|
||||
variable: &CircuitImpliedVariableDefinition,
|
||||
) -> Result<CircuitImpliedVariableDefinition, LeoError> {
|
||||
) -> Result<CircuitImpliedVariableDefinition> {
|
||||
let identifier = self.reduce_identifier(&variable.identifier)?;
|
||||
let expression = variable
|
||||
.expression
|
||||
@ -233,10 +224,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
.reduce_circuit_implied_variable_definition(variable, identifier, expression)
|
||||
}
|
||||
|
||||
pub fn reduce_circuit_init(
|
||||
&mut self,
|
||||
circuit_init: &CircuitInitExpression,
|
||||
) -> Result<CircuitInitExpression, LeoError> {
|
||||
pub fn reduce_circuit_init(&mut self, circuit_init: &CircuitInitExpression) -> Result<CircuitInitExpression> {
|
||||
let name = self.reduce_identifier(&circuit_init.name)?;
|
||||
|
||||
let mut members = vec![];
|
||||
@ -250,7 +238,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_circuit_member_access(
|
||||
&mut self,
|
||||
circuit_member_access: &CircuitMemberAccessExpression,
|
||||
) -> Result<CircuitMemberAccessExpression, LeoError> {
|
||||
) -> Result<CircuitMemberAccessExpression> {
|
||||
let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
|
||||
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(
|
||||
&mut self,
|
||||
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
|
||||
) -> Result<CircuitStaticFunctionAccessExpression, LeoError> {
|
||||
) -> Result<CircuitStaticFunctionAccessExpression> {
|
||||
let circuit = self.reduce_expression(&circuit_static_fn_access.circuit)?;
|
||||
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)
|
||||
}
|
||||
|
||||
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 mut arguments = vec![];
|
||||
@ -281,7 +269,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
Statement::Return(return_statement) => Statement::Return(self.reduce_return(return_statement)?),
|
||||
Statement::Definition(definition) => Statement::Definition(self.reduce_definition(definition)?),
|
||||
@ -296,19 +284,19 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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)?;
|
||||
|
||||
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)?;
|
||||
|
||||
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![];
|
||||
for variable_name in definition.variable_names.iter() {
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
AssigneeAccess::ArrayRange(left, right) => {
|
||||
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)
|
||||
}
|
||||
|
||||
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 mut accesses = vec![];
|
||||
@ -352,14 +340,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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 value = self.reduce_expression(&assign.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 block = self.reduce_block(&conditional.block)?;
|
||||
let next = conditional
|
||||
@ -371,7 +359,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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 start = self.reduce_expression(&iteration.start)?;
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
ConsoleFunction::Assert(expression) => ConsoleFunction::Assert(self.reduce_expression(expression)?),
|
||||
ConsoleFunction::Error(args) | ConsoleFunction::Log(args) => {
|
||||
@ -406,15 +394,12 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_console(console_function_call, function)
|
||||
}
|
||||
|
||||
pub fn reduce_expression_statement(
|
||||
&mut self,
|
||||
expression: &ExpressionStatement,
|
||||
) -> Result<ExpressionStatement, LeoError> {
|
||||
pub fn reduce_expression_statement(&mut self, expression: &ExpressionStatement) -> Result<ExpressionStatement> {
|
||||
let inner_expression = self.reduce_expression(&expression.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![];
|
||||
for statement in block.statements.iter() {
|
||||
statements.push(self.reduce_statement(statement)?);
|
||||
@ -424,7 +409,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
}
|
||||
|
||||
// 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![];
|
||||
for input in program.expected_input.iter() {
|
||||
inputs.push(self.reduce_function_input(input)?);
|
||||
@ -459,14 +444,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_function_input_variable(
|
||||
&mut self,
|
||||
variable: &FunctionInputVariable,
|
||||
) -> Result<FunctionInputVariable, LeoError> {
|
||||
) -> Result<FunctionInputVariable> {
|
||||
let identifier = self.reduce_identifier(&variable.identifier)?;
|
||||
let type_ = self.reduce_type(&variable.type_, &variable.span)?;
|
||||
|
||||
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 {
|
||||
FunctionInput::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)
|
||||
}
|
||||
|
||||
pub fn reduce_package_or_packages(
|
||||
&mut self,
|
||||
package_or_packages: &PackageOrPackages,
|
||||
) -> Result<PackageOrPackages, LeoError> {
|
||||
pub fn reduce_package_or_packages(&mut self, package_or_packages: &PackageOrPackages) -> Result<PackageOrPackages> {
|
||||
let new = match package_or_packages {
|
||||
PackageOrPackages::Package(package) => PackageOrPackages::Package(Package {
|
||||
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)
|
||||
}
|
||||
|
||||
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)?;
|
||||
|
||||
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 {
|
||||
CircuitMember::CircuitVariable(identifier, type_) => CircuitMember::CircuitVariable(
|
||||
self.reduce_identifier(identifier)?,
|
||||
@ -517,7 +499,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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 mut members = vec![];
|
||||
@ -528,13 +510,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
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)?;
|
||||
|
||||
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 mut annotations = vec![];
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::*;
|
||||
use indexmap::IndexMap;
|
||||
use leo_errors::{LeoError, Span};
|
||||
use leo_errors::{Result, Span};
|
||||
|
||||
// Needed to fix clippy bug.
|
||||
#[allow(clippy::redundant_closure)]
|
||||
@ -24,23 +24,23 @@ pub trait ReconstructingReducer {
|
||||
fn in_circuit(&self) -> bool;
|
||||
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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, LeoError> {
|
||||
fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier> {
|
||||
Ok(Identifier {
|
||||
name: identifier.name.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 {
|
||||
x: group_tuple.x.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)
|
||||
}
|
||||
|
||||
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(
|
||||
string.to_vec(),
|
||||
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)
|
||||
}
|
||||
|
||||
@ -69,7 +69,7 @@ pub trait ReconstructingReducer {
|
||||
left: Expression,
|
||||
right: Expression,
|
||||
op: BinaryOperation,
|
||||
) -> Result<BinaryExpression, LeoError> {
|
||||
) -> Result<BinaryExpression> {
|
||||
Ok(BinaryExpression {
|
||||
left: Box::new(left),
|
||||
right: Box::new(right),
|
||||
@ -83,7 +83,7 @@ pub trait ReconstructingReducer {
|
||||
unary: &UnaryExpression,
|
||||
inner: Expression,
|
||||
op: UnaryOperation,
|
||||
) -> Result<UnaryExpression, LeoError> {
|
||||
) -> Result<UnaryExpression> {
|
||||
Ok(UnaryExpression {
|
||||
inner: Box::new(inner),
|
||||
op,
|
||||
@ -97,7 +97,7 @@ pub trait ReconstructingReducer {
|
||||
condition: Expression,
|
||||
if_true: Expression,
|
||||
if_false: Expression,
|
||||
) -> Result<TernaryExpression, LeoError> {
|
||||
) -> Result<TernaryExpression> {
|
||||
Ok(TernaryExpression {
|
||||
condition: Box::new(condition),
|
||||
if_true: Box::new(if_true),
|
||||
@ -106,12 +106,7 @@ pub trait ReconstructingReducer {
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_cast(
|
||||
&mut self,
|
||||
cast: &CastExpression,
|
||||
inner: Expression,
|
||||
target_type: Type,
|
||||
) -> Result<CastExpression, LeoError> {
|
||||
fn reduce_cast(&mut self, cast: &CastExpression, inner: Expression, target_type: Type) -> Result<CastExpression> {
|
||||
Ok(CastExpression {
|
||||
inner: Box::new(inner),
|
||||
target_type,
|
||||
@ -123,7 +118,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
array_inline: &ArrayInlineExpression,
|
||||
elements: Vec<SpreadOrExpression>,
|
||||
) -> Result<ArrayInlineExpression, LeoError> {
|
||||
) -> Result<ArrayInlineExpression> {
|
||||
Ok(ArrayInlineExpression {
|
||||
elements,
|
||||
span: array_inline.span.clone(),
|
||||
@ -134,7 +129,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
array_init: &ArrayInitExpression,
|
||||
element: Expression,
|
||||
) -> Result<ArrayInitExpression, LeoError> {
|
||||
) -> Result<ArrayInitExpression> {
|
||||
Ok(ArrayInitExpression {
|
||||
element: Box::new(element),
|
||||
dimensions: array_init.dimensions.clone(),
|
||||
@ -147,7 +142,7 @@ pub trait ReconstructingReducer {
|
||||
array_access: &ArrayAccessExpression,
|
||||
array: Expression,
|
||||
index: Expression,
|
||||
) -> Result<ArrayAccessExpression, LeoError> {
|
||||
) -> Result<ArrayAccessExpression> {
|
||||
Ok(ArrayAccessExpression {
|
||||
array: Box::new(array),
|
||||
index: Box::new(index),
|
||||
@ -161,7 +156,7 @@ pub trait ReconstructingReducer {
|
||||
array: Expression,
|
||||
left: Option<Expression>,
|
||||
right: Option<Expression>,
|
||||
) -> Result<ArrayRangeAccessExpression, LeoError> {
|
||||
) -> Result<ArrayRangeAccessExpression> {
|
||||
Ok(ArrayRangeAccessExpression {
|
||||
array: Box::new(array),
|
||||
left: left.map(|expr| Box::new(expr)),
|
||||
@ -174,7 +169,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
tuple_init: &TupleInitExpression,
|
||||
elements: Vec<Expression>,
|
||||
) -> Result<TupleInitExpression, LeoError> {
|
||||
) -> Result<TupleInitExpression> {
|
||||
Ok(TupleInitExpression {
|
||||
elements,
|
||||
span: tuple_init.span.clone(),
|
||||
@ -185,7 +180,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
tuple_access: &TupleAccessExpression,
|
||||
tuple: Expression,
|
||||
) -> Result<TupleAccessExpression, LeoError> {
|
||||
) -> Result<TupleAccessExpression> {
|
||||
Ok(TupleAccessExpression {
|
||||
tuple: Box::new(tuple),
|
||||
index: tuple_access.index.clone(),
|
||||
@ -198,7 +193,7 @@ pub trait ReconstructingReducer {
|
||||
_variable: &CircuitImpliedVariableDefinition,
|
||||
identifier: Identifier,
|
||||
expression: Option<Expression>,
|
||||
) -> Result<CircuitImpliedVariableDefinition, LeoError> {
|
||||
) -> Result<CircuitImpliedVariableDefinition> {
|
||||
Ok(CircuitImpliedVariableDefinition { identifier, expression })
|
||||
}
|
||||
|
||||
@ -207,7 +202,7 @@ pub trait ReconstructingReducer {
|
||||
circuit_init: &CircuitInitExpression,
|
||||
name: Identifier,
|
||||
members: Vec<CircuitImpliedVariableDefinition>,
|
||||
) -> Result<CircuitInitExpression, LeoError> {
|
||||
) -> Result<CircuitInitExpression> {
|
||||
Ok(CircuitInitExpression {
|
||||
name,
|
||||
members,
|
||||
@ -220,7 +215,7 @@ pub trait ReconstructingReducer {
|
||||
circuit_member_access: &CircuitMemberAccessExpression,
|
||||
circuit: Expression,
|
||||
name: Identifier,
|
||||
) -> Result<CircuitMemberAccessExpression, LeoError> {
|
||||
) -> Result<CircuitMemberAccessExpression> {
|
||||
Ok(CircuitMemberAccessExpression {
|
||||
circuit: Box::new(circuit),
|
||||
name,
|
||||
@ -233,7 +228,7 @@ pub trait ReconstructingReducer {
|
||||
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
|
||||
circuit: Expression,
|
||||
name: Identifier,
|
||||
) -> Result<CircuitStaticFunctionAccessExpression, LeoError> {
|
||||
) -> Result<CircuitStaticFunctionAccessExpression> {
|
||||
Ok(CircuitStaticFunctionAccessExpression {
|
||||
circuit: Box::new(circuit),
|
||||
name,
|
||||
@ -246,7 +241,7 @@ pub trait ReconstructingReducer {
|
||||
call: &CallExpression,
|
||||
function: Expression,
|
||||
arguments: Vec<Expression>,
|
||||
) -> Result<CallExpression, LeoError> {
|
||||
) -> Result<CallExpression> {
|
||||
Ok(CallExpression {
|
||||
function: Box::new(function),
|
||||
arguments,
|
||||
@ -255,26 +250,18 @@ pub trait ReconstructingReducer {
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
fn reduce_return(
|
||||
&mut self,
|
||||
return_statement: &ReturnStatement,
|
||||
expression: Expression,
|
||||
) -> Result<ReturnStatement, LeoError> {
|
||||
fn reduce_return(&mut self, return_statement: &ReturnStatement, expression: Expression) -> Result<ReturnStatement> {
|
||||
Ok(ReturnStatement {
|
||||
expression,
|
||||
span: return_statement.span.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_variable_name(
|
||||
&mut self,
|
||||
variable_name: &VariableName,
|
||||
identifier: Identifier,
|
||||
) -> Result<VariableName, LeoError> {
|
||||
fn reduce_variable_name(&mut self, variable_name: &VariableName, identifier: Identifier) -> Result<VariableName> {
|
||||
Ok(VariableName {
|
||||
mutable: variable_name.mutable,
|
||||
identifier,
|
||||
@ -288,7 +275,7 @@ pub trait ReconstructingReducer {
|
||||
variable_names: Vec<VariableName>,
|
||||
type_: Option<Type>,
|
||||
value: Expression,
|
||||
) -> Result<DefinitionStatement, LeoError> {
|
||||
) -> Result<DefinitionStatement> {
|
||||
Ok(DefinitionStatement {
|
||||
declaration_type: definition.declaration_type.clone(),
|
||||
variable_names,
|
||||
@ -298,11 +285,7 @@ pub trait ReconstructingReducer {
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_assignee_access(
|
||||
&mut self,
|
||||
_access: &AssigneeAccess,
|
||||
new: AssigneeAccess,
|
||||
) -> Result<AssigneeAccess, LeoError> {
|
||||
fn reduce_assignee_access(&mut self, _access: &AssigneeAccess, new: AssigneeAccess) -> Result<AssigneeAccess> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -311,7 +294,7 @@ pub trait ReconstructingReducer {
|
||||
assignee: &Assignee,
|
||||
identifier: Identifier,
|
||||
accesses: Vec<AssigneeAccess>,
|
||||
) -> Result<Assignee, LeoError> {
|
||||
) -> Result<Assignee> {
|
||||
Ok(Assignee {
|
||||
identifier,
|
||||
accesses,
|
||||
@ -324,7 +307,7 @@ pub trait ReconstructingReducer {
|
||||
assign: &AssignStatement,
|
||||
assignee: Assignee,
|
||||
value: Expression,
|
||||
) -> Result<AssignStatement, LeoError> {
|
||||
) -> Result<AssignStatement> {
|
||||
Ok(AssignStatement {
|
||||
operation: assign.operation,
|
||||
assignee,
|
||||
@ -339,7 +322,7 @@ pub trait ReconstructingReducer {
|
||||
condition: Expression,
|
||||
block: Block,
|
||||
statement: Option<Statement>,
|
||||
) -> Result<ConditionalStatement, LeoError> {
|
||||
) -> Result<ConditionalStatement> {
|
||||
Ok(ConditionalStatement {
|
||||
condition,
|
||||
block,
|
||||
@ -355,7 +338,7 @@ pub trait ReconstructingReducer {
|
||||
start: Expression,
|
||||
stop: Expression,
|
||||
block: Block,
|
||||
) -> Result<IterationStatement, LeoError> {
|
||||
) -> Result<IterationStatement> {
|
||||
Ok(IterationStatement {
|
||||
variable,
|
||||
start,
|
||||
@ -366,11 +349,7 @@ pub trait ReconstructingReducer {
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_console(
|
||||
&mut self,
|
||||
console: &ConsoleStatement,
|
||||
function: ConsoleFunction,
|
||||
) -> Result<ConsoleStatement, LeoError> {
|
||||
fn reduce_console(&mut self, console: &ConsoleStatement, function: ConsoleFunction) -> Result<ConsoleStatement> {
|
||||
Ok(ConsoleStatement {
|
||||
function,
|
||||
span: console.span.clone(),
|
||||
@ -381,14 +360,14 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
expression_statement: &ExpressionStatement,
|
||||
expression: Expression,
|
||||
) -> Result<ExpressionStatement, LeoError> {
|
||||
) -> Result<ExpressionStatement> {
|
||||
Ok(ExpressionStatement {
|
||||
expression,
|
||||
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 {
|
||||
statements,
|
||||
span: block.span.clone(),
|
||||
@ -404,7 +383,7 @@ pub trait ReconstructingReducer {
|
||||
circuits: IndexMap<Identifier, Circuit>,
|
||||
functions: IndexMap<Identifier, Function>,
|
||||
global_consts: IndexMap<String, DefinitionStatement>,
|
||||
) -> Result<Program, LeoError> {
|
||||
) -> Result<Program> {
|
||||
Ok(Program {
|
||||
name: program.name.clone(),
|
||||
expected_input,
|
||||
@ -420,7 +399,7 @@ pub trait ReconstructingReducer {
|
||||
variable: &FunctionInputVariable,
|
||||
identifier: Identifier,
|
||||
type_: Type,
|
||||
) -> Result<FunctionInputVariable, LeoError> {
|
||||
) -> Result<FunctionInputVariable> {
|
||||
Ok(FunctionInputVariable {
|
||||
identifier,
|
||||
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)
|
||||
}
|
||||
|
||||
@ -438,7 +417,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
_package_or_packages: &PackageOrPackages,
|
||||
new: PackageOrPackages,
|
||||
) -> Result<PackageOrPackages, LeoError> {
|
||||
) -> Result<PackageOrPackages> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -446,18 +425,14 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
import: &ImportStatement,
|
||||
package_or_packages: PackageOrPackages,
|
||||
) -> Result<ImportStatement, LeoError> {
|
||||
) -> Result<ImportStatement> {
|
||||
Ok(ImportStatement {
|
||||
package_or_packages,
|
||||
span: import.span.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_circuit_member(
|
||||
&mut self,
|
||||
_circuit_member: &CircuitMember,
|
||||
new: CircuitMember,
|
||||
) -> Result<CircuitMember, LeoError> {
|
||||
fn reduce_circuit_member(&mut self, _circuit_member: &CircuitMember, new: CircuitMember) -> Result<CircuitMember> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -466,11 +441,11 @@ pub trait ReconstructingReducer {
|
||||
_circuit: &Circuit,
|
||||
circuit_name: Identifier,
|
||||
members: Vec<CircuitMember>,
|
||||
) -> Result<Circuit, LeoError> {
|
||||
) -> Result<Circuit> {
|
||||
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 {
|
||||
span: annotation.span.clone(),
|
||||
name,
|
||||
@ -487,7 +462,7 @@ pub trait ReconstructingReducer {
|
||||
input: Vec<FunctionInput>,
|
||||
output: Option<Type>,
|
||||
block: Block,
|
||||
) -> Result<Function, LeoError> {
|
||||
) -> Result<Function> {
|
||||
Ok(Function {
|
||||
identifier,
|
||||
annotations,
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_asg::{AsgContext, ImportResolver, Program};
|
||||
use leo_errors::{ImportError, LeoError, Span};
|
||||
use leo_errors::{ImportError, LeoError, Result, Span};
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use std::path::PathBuf;
|
||||
@ -47,7 +47,7 @@ impl<'a> ImportResolver<'a> for ImportParser<'a> {
|
||||
context: AsgContext<'a>,
|
||||
package_segments: &[&str],
|
||||
span: &Span,
|
||||
) -> Result<Option<Program<'a>>, LeoError> {
|
||||
) -> Result<Option<Program<'a>>> {
|
||||
let full_path = package_segments.join(".");
|
||||
if self.partial_imports.contains(&full_path) {
|
||||
return Err(ImportError::recursive_imports(full_path, span).into());
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::ImportParser;
|
||||
use leo_asg::{AsgContext, Program};
|
||||
use leo_errors::{ImportError, LeoError, Span};
|
||||
use leo_errors::{ImportError, Result, Span};
|
||||
|
||||
use std::{fs, fs::DirEntry, path::PathBuf};
|
||||
|
||||
@ -31,7 +31,7 @@ impl<'a> ImportParser<'a> {
|
||||
package: &DirEntry,
|
||||
remaining_segments: &[&str],
|
||||
span: &Span,
|
||||
) -> Result<Program<'a>, LeoError> {
|
||||
) -> Result<Program<'a>> {
|
||||
if !remaining_segments.is_empty() {
|
||||
return self.parse_package(context, package.path(), remaining_segments, span);
|
||||
}
|
||||
@ -52,7 +52,7 @@ impl<'a> ImportParser<'a> {
|
||||
mut path: PathBuf,
|
||||
segments: &[&str],
|
||||
span: &Span,
|
||||
) -> Result<Program<'a>, LeoError> {
|
||||
) -> Result<Program<'a>> {
|
||||
let error_path = path.clone();
|
||||
let package_name = segments[0];
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::ImportParser;
|
||||
use leo_ast::Program;
|
||||
use leo_errors::{ImportError, LeoError, Span};
|
||||
use leo_errors::{ImportError, Result, Span};
|
||||
|
||||
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.
|
||||
///
|
||||
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.
|
||||
let file_type = package
|
||||
.file_type()
|
||||
|
@ -145,7 +145,7 @@ impl Command for Build {
|
||||
|
||||
// Compile the main.leo file along with constraints
|
||||
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
|
||||
|
@ -44,7 +44,7 @@ impl Command for Init {
|
||||
|
||||
// Check that the current package directory 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.
|
||||
@ -54,7 +54,7 @@ impl Command for Init {
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
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();
|
||||
|
@ -46,7 +46,7 @@ impl Command for New {
|
||||
// Check that the given package name is valid.
|
||||
let package_name = self.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();
|
||||
@ -57,7 +57,7 @@ impl Command for New {
|
||||
|
||||
// Verify the package directory path does not exist yet.
|
||||
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
|
||||
|
@ -64,12 +64,12 @@ impl Add {
|
||||
if v.len() == 2 {
|
||||
Ok((v[0].to_string(), v[1].to_string()))
|
||||
} 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) {
|
||||
Ok((author.clone(), package.clone()))
|
||||
} 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 {
|
||||
Ok((v[0].to_string(), v[1].to_string()))
|
||||
} 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) {
|
||||
Ok((author.clone(), package.clone()))
|
||||
} 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");
|
||||
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)
|
||||
@ -92,7 +92,7 @@ impl Command for Login {
|
||||
|
||||
match api.run_route(ProfileRoute {})? {
|
||||
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)
|
||||
} else {
|
||||
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();
|
||||
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();
|
||||
@ -59,9 +59,9 @@ impl Command for Publish {
|
||||
manifest.get_package_license(),
|
||||
manifest.get_package_remote(),
|
||||
) {
|
||||
(None, _, _) => return Err(CliError::no_package_description(new_backtrace()))?,
|
||||
(_, None, _) => return Err(CliError::missing_package_license(new_backtrace()))?,
|
||||
(_, _, None) => return Err(CliError::missing_package_remote(new_backtrace()))?,
|
||||
(None, _, _) => return Err(CliError::no_package_description(new_backtrace()).into()),
|
||||
(_, None, _) => return Err(CliError::missing_package_license(new_backtrace()).into()),
|
||||
(_, _, 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.
|
||||
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.
|
||||
|
@ -80,10 +80,7 @@ impl Command for Test {
|
||||
|
||||
// when no main file and no files marked - error
|
||||
} else {
|
||||
return Err(CliError::program_file_does_not_exist(
|
||||
package_path.to_string_lossy(),
|
||||
new_backtrace(),
|
||||
))?;
|
||||
return Err(CliError::program_file_does_not_exist(package_path.to_string_lossy(), new_backtrace()).into());
|
||||
}
|
||||
|
||||
// Construct the path to the output directory;
|
||||
|
@ -189,10 +189,10 @@ pub fn read_username() -> Result<String> {
|
||||
|
||||
pub fn remove_token_and_username() -> Result<()> {
|
||||
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()) {
|
||||
Err(CliError::remove_token_and_username(err, new_backtrace()))?
|
||||
return Err(CliError::remove_token_and_username(err, new_backtrace()).into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -87,11 +87,7 @@ impl Updater {
|
||||
{
|
||||
Ok(latest_release.version)
|
||||
} else {
|
||||
Err(CliError::old_release_version(
|
||||
current_version,
|
||||
latest_release.version,
|
||||
new_backtrace(),
|
||||
))?
|
||||
Err(CliError::old_release_version(current_version, latest_release.version, new_backtrace()).into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use std::{borrow::Cow, fs, path::Path};
|
||||
|
||||
@ -26,7 +26,7 @@ pub struct ImportsDirectory;
|
||||
|
||||
impl ImportsDirectory {
|
||||
/// 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);
|
||||
if path.is_dir() && !path.ends_with(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.
|
||||
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);
|
||||
if path.is_dir() && !path.ends_with(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
|
||||
// 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,
|
||||
@ -31,7 +31,7 @@ pub struct InputsDirectory;
|
||||
|
||||
impl InputsDirectory {
|
||||
/// 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);
|
||||
if path.is_dir() && !path.ends_with(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.
|
||||
pub fn files(path: &Path) -> Result<Vec<PathBuf>, LeoError> {
|
||||
pub fn files(path: &Path) -> Result<Vec<PathBuf>> {
|
||||
let mut path = path.to_owned();
|
||||
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() {
|
||||
let file_entry = file_entry.map_err(|e| PackageError::failed_to_get_input_file_entry(e, Backtrace::new()))?;
|
||||
let file_path = file_entry.path();
|
||||
|
@ -18,7 +18,7 @@
|
||||
|
||||
use crate::inputs::INPUTS_DIRECTORY_NAME;
|
||||
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -56,7 +56,7 @@ impl InputFile {
|
||||
}
|
||||
|
||||
/// 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 input = fs::read_to_string(&path)
|
||||
@ -65,7 +65,7 @@ impl InputFile {
|
||||
}
|
||||
|
||||
/// 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 mut file = File::create(&path).map_err(|e| PackageError::io_error_input_file(e, Backtrace::new()))?;
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
//! The `program.state` file.
|
||||
|
||||
use crate::inputs::INPUTS_DIRECTORY_NAME;
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -55,7 +55,7 @@ impl StateFile {
|
||||
}
|
||||
|
||||
/// 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 input = fs::read_to_string(&path)
|
||||
@ -64,7 +64,7 @@ impl StateFile {
|
||||
}
|
||||
|
||||
/// 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 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 leo_errors::LeoError;
|
||||
use leo_errors::Result;
|
||||
|
||||
pub struct LeoPackage;
|
||||
|
||||
impl LeoPackage {
|
||||
/// 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)
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ impl LeoPackage {
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@
|
||||
//! The build checksum file.
|
||||
|
||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -51,7 +51,7 @@ impl ChecksumFile {
|
||||
}
|
||||
|
||||
/// 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 string = fs::read_to_string(&path)
|
||||
@ -60,7 +60,7 @@ impl ChecksumFile {
|
||||
}
|
||||
|
||||
/// 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 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,
|
||||
/// `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);
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
|
@ -17,7 +17,7 @@
|
||||
//! The serialized circuit output file.
|
||||
|
||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -51,7 +51,7 @@ impl CircuitFile {
|
||||
}
|
||||
|
||||
/// 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 string = fs::read_to_string(&path)
|
||||
@ -60,7 +60,7 @@ impl CircuitFile {
|
||||
}
|
||||
|
||||
/// 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 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,
|
||||
/// `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);
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use std::{borrow::Cow, fs, path::Path};
|
||||
@ -25,7 +25,7 @@ pub struct OutputsDirectory;
|
||||
|
||||
impl OutputsDirectory {
|
||||
/// 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);
|
||||
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
@ -36,7 +36,7 @@ impl OutputsDirectory {
|
||||
}
|
||||
|
||||
/// 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);
|
||||
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
|
@ -17,7 +17,7 @@
|
||||
//! The proof file.
|
||||
|
||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -51,7 +51,7 @@ impl ProofFile {
|
||||
}
|
||||
|
||||
/// 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 string = fs::read_to_string(&path)
|
||||
@ -60,7 +60,7 @@ impl ProofFile {
|
||||
}
|
||||
|
||||
/// 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 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,
|
||||
/// `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);
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
|
@ -17,7 +17,7 @@
|
||||
//! The proving key file.
|
||||
|
||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -55,7 +55,7 @@ impl ProvingKeyFile {
|
||||
}
|
||||
|
||||
/// 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 bytes =
|
||||
@ -64,7 +64,7 @@ impl ProvingKeyFile {
|
||||
}
|
||||
|
||||
/// 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 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,
|
||||
/// `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);
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
|
@ -17,7 +17,7 @@
|
||||
//! The verification key file.
|
||||
|
||||
use crate::outputs::OUTPUTS_DIRECTORY_NAME;
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -55,7 +55,7 @@ impl VerificationKeyFile {
|
||||
}
|
||||
|
||||
/// 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 bytes = fs::read(&path)
|
||||
@ -64,7 +64,7 @@ impl VerificationKeyFile {
|
||||
}
|
||||
|
||||
/// 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 mut file =
|
||||
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,
|
||||
/// `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);
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
|
@ -21,7 +21,7 @@ use crate::{
|
||||
source::{MainFile, SourceDirectory},
|
||||
};
|
||||
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -36,7 +36,7 @@ pub struct 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.
|
||||
if !Self::is_package_name_valid(package_name) {
|
||||
return Err(PackageError::invalid_package_name(package_name, Backtrace::new()).into());
|
||||
@ -184,7 +184,7 @@ impl Package {
|
||||
}
|
||||
|
||||
/// 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.
|
||||
{
|
||||
if !Self::can_initialize(package_name, path) {
|
||||
@ -244,7 +244,7 @@ impl Package {
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
//! The `.gitignore` file.
|
||||
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -40,7 +40,7 @@ impl Gitignore {
|
||||
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);
|
||||
if path.is_dir() {
|
||||
path.to_mut().push(GITIGNORE_FILENAME);
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::package::Package;
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -42,7 +42,7 @@ pub struct 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 {
|
||||
project: Package::new(package_name)?,
|
||||
remote: author.map(|author| Remote { author }),
|
||||
@ -81,7 +81,7 @@ impl Manifest {
|
||||
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);
|
||||
if path.is_dir() {
|
||||
path.to_mut().push(MANIFEST_FILENAME);
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
//! The `README.md` file.
|
||||
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -49,7 +49,7 @@ impl README {
|
||||
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);
|
||||
if path.is_dir() {
|
||||
path.to_mut().push(README_FILENAME);
|
||||
|
@ -30,7 +30,7 @@ use crate::{
|
||||
root::{MANIFEST_FILENAME, README_FILENAME},
|
||||
source::{SOURCE_DIRECTORY_NAME, SOURCE_FILE_EXTENSION},
|
||||
};
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
|
||||
@ -71,14 +71,14 @@ impl ZipFile {
|
||||
}
|
||||
|
||||
// /// 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);
|
||||
//
|
||||
// Ok(fs::read(&path).map_err(|_| PackageError::FileReadError(path.clone()))?)
|
||||
// }
|
||||
|
||||
/// 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
|
||||
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,
|
||||
/// `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);
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
@ -32,7 +32,7 @@ pub struct SourceDirectory;
|
||||
|
||||
impl SourceDirectory {
|
||||
/// 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);
|
||||
if path.is_dir() && !path.ends_with(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.
|
||||
pub fn files(path: &Path) -> Result<Vec<PathBuf>, LeoError> {
|
||||
pub fn files(path: &Path) -> Result<Vec<PathBuf>> {
|
||||
let mut path = Cow::from(path);
|
||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
//! The `main.leo` file.
|
||||
|
||||
use crate::source::directory::SOURCE_DIRECTORY_NAME;
|
||||
use leo_errors::{LeoError, PackageError};
|
||||
use leo_errors::{PackageError, Result};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use serde::Deserialize;
|
||||
@ -52,7 +52,7 @@ impl MainFile {
|
||||
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);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
|
@ -15,10 +15,10 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_ast::Ast;
|
||||
use leo_errors::LeoError;
|
||||
use leo_errors::Result;
|
||||
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.
|
||||
let program_filepath = filepath.to_path_buf();
|
||||
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)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), LeoError> {
|
||||
fn main() -> Result<()> {
|
||||
// Parse the command-line arguments as strings.
|
||||
let cli_arguments = env::args().collect::<Vec<String>>();
|
||||
|
||||
|
@ -29,11 +29,12 @@ pub mod parser;
|
||||
pub use parser::*;
|
||||
|
||||
use leo_ast::Ast;
|
||||
use leo_errors::Result;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
/// 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())?))
|
||||
}
|
||||
|
@ -16,9 +16,9 @@
|
||||
|
||||
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_errors::{LeoError, ParserError, Span};
|
||||
use leo_errors::{LeoError, ParserError, Result, Span};
|
||||
use tendril::format_tendril;
|
||||
|
||||
/// 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.
|
||||
///
|
||||
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())
|
||||
}
|
||||
|
||||
///
|
||||
/// 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())
|
||||
}
|
||||
|
||||
@ -89,7 +89,7 @@ impl ParserContext {
|
||||
.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 token.iter().any(|x| x == &spanned_token.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,
|
||||
/// 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();
|
||||
if i < 1 {
|
||||
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.
|
||||
///
|
||||
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 &token == inner {
|
||||
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.
|
||||
///
|
||||
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 token.iter().any(|x| x == inner) {
|
||||
Ok(self.tokens.pop().unwrap())
|
||||
@ -331,7 +331,7 @@ impl ParserContext {
|
||||
/// Returns the [`Identifier`] of the next token if it is a keyword,
|
||||
/// [`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) {
|
||||
return Ok(Identifier {
|
||||
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.
|
||||
///
|
||||
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 Token::Ident(_) = inner {
|
||||
let token = self.tokens.pop().unwrap();
|
||||
@ -371,7 +371,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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() {
|
||||
Ok(x)
|
||||
} else {
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use tendril::format_tendril;
|
||||
|
||||
use leo_errors::ParserError;
|
||||
use leo_errors::{ParserError, Result};
|
||||
|
||||
use super::*;
|
||||
|
||||
@ -40,7 +40,7 @@ impl ParserContext {
|
||||
/// Returns an [`Expression`] AST node if the next token is an expression.
|
||||
/// Includes circuit init expressions.
|
||||
///
|
||||
pub fn parse_expression(&mut self) -> SyntaxResult<Expression> {
|
||||
pub fn parse_expression(&mut self) -> Result<Expression> {
|
||||
// Store current parser 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`].
|
||||
///
|
||||
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.
|
||||
let mut expr = self.parse_disjunctive_expression()?;
|
||||
|
||||
@ -87,7 +87,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
while self.eat(Token::Or).is_some() {
|
||||
let right = self.parse_conjunctive_expression()?;
|
||||
@ -107,7 +107,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
while self.eat(Token::And).is_some() {
|
||||
let right = self.parse_equality_expression()?;
|
||||
@ -127,7 +127,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
// while self.eat(Token::BitOr).is_some() {
|
||||
// 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`].
|
||||
///
|
||||
// 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()?;
|
||||
// while self.eat(Token::BitXor).is_some() {
|
||||
// let right = self.parse_bit_and_expression()?;
|
||||
@ -167,7 +167,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
// while self.eat(Token::BitAnd).is_some() {
|
||||
// let right = self.parse_equality_expression()?;
|
||||
@ -187,7 +187,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
if let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Eq, Token::NotEq]) {
|
||||
let right = self.parse_ordering_expression()?;
|
||||
@ -211,7 +211,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
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`].
|
||||
///
|
||||
// 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()?;
|
||||
// while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Shl, Token::Shr, Token::ShrSigned]) {
|
||||
// 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`].
|
||||
///
|
||||
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()?;
|
||||
while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Add, Token::Minus]) {
|
||||
let right = self.parse_multiplicative_expression()?;
|
||||
@ -287,7 +287,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
while let Some(SpannedToken { token: op, .. }) = self.eat_any(&[Token::Mul, Token::Div]) {
|
||||
let right = self.parse_exponential_expression()?;
|
||||
@ -312,7 +312,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?];
|
||||
while self.eat(Token::Exp).is_some() {
|
||||
exprs.push(self.parse_cast_expression()?);
|
||||
@ -336,7 +336,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
while self.eat(Token::As).is_some() {
|
||||
let (type_, type_span) = self.parse_type()?;
|
||||
@ -355,7 +355,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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();
|
||||
while let Some(token) = self.eat_any(&[Token::Not, Token::Minus]) {
|
||||
ops.push(token);
|
||||
@ -400,7 +400,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
while let Some(token) = self.eat_any(&[Token::LeftSquare, Token::Dot, Token::LeftParen, Token::DoubleColon]) {
|
||||
match token.token {
|
||||
@ -505,7 +505,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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() {
|
||||
SpreadOrExpression::Spread(self.parse_expression()?)
|
||||
} else {
|
||||
@ -517,7 +517,7 @@ impl ParserContext {
|
||||
/// Returns an [`Expression`] AST node if the next tokens represent an
|
||||
/// 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)?;
|
||||
let mut members = Vec::new();
|
||||
let end_span;
|
||||
@ -555,7 +555,7 @@ impl ParserContext {
|
||||
/// Returns an [`Expression`] AST node if the next tokens represent an
|
||||
/// 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()? {
|
||||
return Ok(Expression::Value(ValueExpression::Group(Box::new(GroupValue::Tuple(
|
||||
Box::new(GroupTuple {
|
||||
@ -594,7 +594,7 @@ impl ParserContext {
|
||||
/// Returns an [`Expression`] AST node if the next tokens represent an
|
||||
/// 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) {
|
||||
return Ok(Expression::ArrayInline(ArrayInlineExpression {
|
||||
elements: Vec::new(),
|
||||
@ -654,7 +654,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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()?;
|
||||
Ok(match token {
|
||||
Token::Int(value) => {
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use tendril::format_tendril;
|
||||
|
||||
use leo_errors::ParserError;
|
||||
use leo_errors::{ParserError, Result};
|
||||
|
||||
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.
|
||||
///
|
||||
pub fn parse_program(&mut self) -> SyntaxResult<Program> {
|
||||
pub fn parse_program(&mut self) -> Result<Program> {
|
||||
let mut imports = Vec::new();
|
||||
let mut circuits = 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.
|
||||
///
|
||||
pub fn parse_annotation(&mut self) -> SyntaxResult<Annotation> {
|
||||
pub fn parse_annotation(&mut self) -> Result<Annotation> {
|
||||
let start = self.expect(Token::At)?;
|
||||
let name = self.expect_ident()?;
|
||||
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
|
||||
/// 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();
|
||||
self.expect(Token::LeftParen)?;
|
||||
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
|
||||
/// 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) {
|
||||
Ok(PackageAccess::Star { span })
|
||||
} else {
|
||||
@ -211,7 +211,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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).
|
||||
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
|
||||
/// 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()?;
|
||||
self.expect(Token::Dot)?;
|
||||
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.
|
||||
///
|
||||
pub fn parse_import(&mut self) -> SyntaxResult<ImportStatement> {
|
||||
pub fn parse_import(&mut self) -> Result<ImportStatement> {
|
||||
self.expect(Token::Import)?;
|
||||
let package_or_packages = self.parse_package_path()?;
|
||||
self.expect(Token::Semicolon)?;
|
||||
@ -303,7 +303,7 @@ impl ParserContext {
|
||||
/// Returns a [`CircuitMember`] AST node if the next tokens represent a circuit member variable
|
||||
/// 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 peeked = &self.peek()?.token;
|
||||
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.
|
||||
///
|
||||
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()?;
|
||||
self.expect(Token::Colon)?;
|
||||
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.
|
||||
///
|
||||
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();
|
||||
if peeked.token == Token::Function || peeked.token == Token::At {
|
||||
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
|
||||
/// 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)?;
|
||||
let name = self.expect_ident()?;
|
||||
self.expect(Token::LeftCurly)?;
|
||||
@ -405,7 +405,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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 mutable = self.eat(Token::Mut);
|
||||
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
|
||||
/// 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();
|
||||
while self.peek_token().as_ref() == &Token::At {
|
||||
annotations.push(self.parse_annotation()?);
|
||||
@ -490,7 +490,7 @@ impl ParserContext {
|
||||
/// Returns an [`(String, DefinitionStatement)`] AST node if the next tokens represent a global
|
||||
/// 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 variable_names = statement
|
||||
.variable_names
|
||||
|
@ -32,11 +32,9 @@ use std::unimplemented;
|
||||
use crate::{tokenizer::*, Token};
|
||||
use indexmap::IndexMap;
|
||||
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) -> SyntaxResult<()> {
|
||||
pub(crate) fn assert_no_whitespace(left_span: &Span, right_span: &Span, left: &str, right: &str) -> Result<()> {
|
||||
if left_span.col_stop != right_span.col_start {
|
||||
let mut error_span = left_span + right_span;
|
||||
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.
|
||||
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())?);
|
||||
|
||||
tokens.parse_program()
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
use leo_errors::ParserError;
|
||||
use leo_errors::{ParserError, Result};
|
||||
|
||||
const ASSIGN_TOKENS: &[Token] = &[
|
||||
Token::Assign,
|
||||
@ -41,7 +41,7 @@ impl ParserContext {
|
||||
/// Returns an [`Identifier`] AST node if the given [`Expression`] AST node evaluates to an
|
||||
/// 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;
|
||||
match expr {
|
||||
Expression::CircuitMemberAccess(expr) => {
|
||||
@ -72,7 +72,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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 mut accesses = Vec::new();
|
||||
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.
|
||||
///
|
||||
pub fn parse_statement(&mut self) -> SyntaxResult<Statement> {
|
||||
pub fn parse_statement(&mut self) -> Result<Statement> {
|
||||
match &self.peek()?.token {
|
||||
Token::Return => Ok(Statement::Return(self.parse_return_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.
|
||||
///
|
||||
pub fn parse_assign_statement(&mut self) -> SyntaxResult<Statement> {
|
||||
pub fn parse_assign_statement(&mut self) -> Result<Statement> {
|
||||
let expr = self.parse_expression()?;
|
||||
|
||||
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.
|
||||
///
|
||||
pub fn parse_block(&mut self) -> SyntaxResult<Block> {
|
||||
pub fn parse_block(&mut self) -> Result<Block> {
|
||||
let start = self.expect(Token::LeftCurly)?;
|
||||
|
||||
let mut statements = Vec::new();
|
||||
@ -166,7 +166,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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 expr = self.parse_expression()?;
|
||||
self.expect(Token::Semicolon)?;
|
||||
@ -180,7 +180,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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)?;
|
||||
self.fuzzy_struct_state = true;
|
||||
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.
|
||||
///
|
||||
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 ident = self.expect_ident()?;
|
||||
self.expect(Token::In)?;
|
||||
@ -234,7 +234,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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 string = match self.expect_any()? {
|
||||
SpannedToken {
|
||||
@ -267,7 +267,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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)?;
|
||||
self.expect(Token::Dot)?;
|
||||
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
|
||||
/// 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);
|
||||
if let Some(mutable) = &mutable {
|
||||
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.
|
||||
///
|
||||
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 mut variable_names = Vec::new();
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use super::*;
|
||||
use leo_errors::ParserError;
|
||||
use leo_errors::{ParserError, Result};
|
||||
|
||||
const TYPE_TOKENS: &[Token] = &[
|
||||
Token::I8,
|
||||
@ -58,7 +58,7 @@ impl ParserContext {
|
||||
///
|
||||
/// 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() {
|
||||
ArrayDimensions(vec![int])
|
||||
} else {
|
||||
@ -84,7 +84,7 @@ impl ParserContext {
|
||||
/// Returns a [`(Type, Span)`] tuple of AST nodes if the next token represents a type. Also
|
||||
/// 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) {
|
||||
(Type::SelfType, token.span)
|
||||
} else if let Some(ident) = self.eat_identifier() {
|
||||
|
@ -17,11 +17,11 @@
|
||||
use leo_ast::Ast;
|
||||
#[cfg(not(feature = "ci_skip"))]
|
||||
use leo_ast::Program;
|
||||
use leo_errors::LeoError;
|
||||
use leo_errors::{LeoError, Result};
|
||||
|
||||
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");
|
||||
|
||||
// Parses the Leo file and constructs a leo ast.
|
||||
|
Loading…
Reference in New Issue
Block a user