mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
Migrate access
This commit is contained in:
parent
507a084d7f
commit
720dfeefca
12
ast/src/access/access.rs
Normal file
12
ast/src/access/access.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::{access::*, ast::Rule};
|
||||
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access))]
|
||||
pub enum Access<'ast> {
|
||||
Array(ArrayAccess<'ast>),
|
||||
Call(CallAccess<'ast>),
|
||||
Object(MemberAccess<'ast>),
|
||||
StaticObject(StaticMemberAccess<'ast>),
|
||||
}
|
12
ast/src/access/array_access.rs
Normal file
12
ast/src/access/array_access.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::ast::{RangeOrExpression, Rule};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_array))]
|
||||
pub struct ArrayAccess<'ast> {
|
||||
pub expression: RangeOrExpression<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
20
ast/src/access/assignee_access.rs
Normal file
20
ast/src/access/assignee_access.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use crate::{access::{ArrayAccess, MemberAccess}, ast::Rule};
|
||||
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_assignee))]
|
||||
pub enum AssigneeAccess<'ast> {
|
||||
Array(ArrayAccess<'ast>),
|
||||
Member(MemberAccess<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for AssigneeAccess<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
AssigneeAccess::Array(ref array) => write!(f, "[{}]", array.expression),
|
||||
AssigneeAccess::Member(ref member) => write!(f, ".{}", member.identifier),
|
||||
}
|
||||
}
|
||||
}
|
12
ast/src/access/call_access.rs
Normal file
12
ast/src/access/call_access.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::{ast::Rule, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_call))]
|
||||
pub struct CallAccess<'ast> {
|
||||
pub expressions: Vec<Expression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
12
ast/src/access/member_access.rs
Normal file
12
ast/src/access/member_access.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::{ast::Rule, common::Identifier};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_member))]
|
||||
pub struct MemberAccess<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
17
ast/src/access/mod.rs
Normal file
17
ast/src/access/mod.rs
Normal file
@ -0,0 +1,17 @@
|
||||
pub mod access;
|
||||
pub use access::*;
|
||||
|
||||
pub mod array_access;
|
||||
pub use array_access::*;
|
||||
|
||||
pub mod assignee_access;
|
||||
pub use assignee_access::*;
|
||||
|
||||
pub mod call_access;
|
||||
pub use call_access::*;
|
||||
|
||||
pub mod member_access;
|
||||
pub use member_access::*;
|
||||
|
||||
pub mod static_member_access;
|
||||
pub use static_member_access::*;
|
12
ast/src/access/static_member_access.rs
Normal file
12
ast/src/access/static_member_access.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::{ast::Rule, common::Identifier};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_static_member))]
|
||||
pub struct StaticMemberAccess<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
//! Abstract syntax tree (ast) representation from leo.pest.
|
||||
use crate::{
|
||||
access::AssigneeAccess,
|
||||
circuits::Circuit,
|
||||
common::{
|
||||
Identifier,
|
||||
@ -99,64 +100,6 @@ impl<'ast> fmt::Display for RangeOrExpression<'ast> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_call))]
|
||||
pub struct CallAccess<'ast> {
|
||||
pub expressions: Vec<Expression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_array))]
|
||||
pub struct ArrayAccess<'ast> {
|
||||
pub expression: RangeOrExpression<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_member))]
|
||||
pub struct MemberAccess<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access_static_member))]
|
||||
pub struct StaticMemberAccess<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::access))]
|
||||
pub enum Access<'ast> {
|
||||
Array(ArrayAccess<'ast>),
|
||||
Call(CallAccess<'ast>),
|
||||
Object(MemberAccess<'ast>),
|
||||
StaticObject(StaticMemberAccess<'ast>),
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::assignee_access))]
|
||||
pub enum AssigneeAccess<'ast> {
|
||||
Array(ArrayAccess<'ast>),
|
||||
Member(MemberAccess<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for AssigneeAccess<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
AssigneeAccess::Array(ref array) => write!(f, "[{}]", array.expression),
|
||||
AssigneeAccess::Member(ref member) => write!(f, ".{}", member.identifier),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::assignee))]
|
||||
pub struct Assignee<'ast> {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, Access}, common::Identifier};
|
||||
use crate::{access::Access, ast::Rule, common::Identifier};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -129,20 +129,30 @@ group_single_or_tuple = {value_number | group_tuple}
|
||||
|
||||
/// Access
|
||||
|
||||
// Declared in access/access.rs
|
||||
access = { access_array | access_call | access_member | access_static_member}
|
||||
|
||||
// Declared in access/array_access.rs
|
||||
access_array = { "[" ~ range_or_expression ~ "]" }
|
||||
|
||||
// Declared in access/assignee_access.rs
|
||||
access_assignee = { access_array | access_member }
|
||||
|
||||
// Declared in access/call_access.rs
|
||||
access_call = { "(" ~ expression_tuple ~ ")" }
|
||||
|
||||
// Declared in access/member_access.rs
|
||||
access_member = { "." ~ identifier }
|
||||
|
||||
// Declared in access/static_member_access.rs
|
||||
access_static_member = { "::" ~ identifier }
|
||||
|
||||
range_or_expression = { range | expression }
|
||||
range = { from_expression? ~ ".." ~ to_expression }
|
||||
from_expression = { expression }
|
||||
to_expression = { expression }
|
||||
|
||||
range = { from_expression? ~ ".." ~ to_expression }
|
||||
range_or_expression = { range | expression }
|
||||
|
||||
access_array = { "[" ~ range_or_expression ~ "]" }
|
||||
access_call = { "(" ~ expression_tuple ~ ")" }
|
||||
access_member = { "." ~ identifier }
|
||||
access_static_member = { "::" ~ identifier }
|
||||
access = { access_array | access_call | access_member | access_static_member}
|
||||
|
||||
assignee_access = { access_array | access_member }
|
||||
assignee = { identifier ~ assignee_access* }
|
||||
assignee = { identifier ~ access_assignee* }
|
||||
|
||||
spread = { "..." ~ expression }
|
||||
spread_or_expression = { spread | expression }
|
||||
|
@ -5,6 +5,9 @@ extern crate pest_derive;
|
||||
#[macro_use]
|
||||
extern crate thiserror;
|
||||
|
||||
pub mod access;
|
||||
pub use access::*;
|
||||
|
||||
pub mod ast;
|
||||
pub use ast::*;
|
||||
|
||||
|
@ -2,6 +2,10 @@
|
||||
|
||||
use crate::{types, Import, ImportSymbol};
|
||||
use leo_ast::{
|
||||
access::{
|
||||
Access,
|
||||
AssigneeAccess,
|
||||
},
|
||||
ast,
|
||||
circuits::{
|
||||
Circuit,
|
||||
@ -363,13 +367,13 @@ impl<'ast> From<PostfixExpression<'ast>> for types::Expression {
|
||||
.into_iter()
|
||||
.fold(variable, |acc, access| match access {
|
||||
// Handle array accesses
|
||||
ast::Access::Array(array) => types::Expression::ArrayAccess(
|
||||
Access::Array(array) => types::Expression::ArrayAccess(
|
||||
Box::new(acc),
|
||||
Box::new(types::RangeOrExpression::from(array.expression)),
|
||||
),
|
||||
|
||||
// Handle function calls
|
||||
ast::Access::Call(function) => types::Expression::FunctionCall(
|
||||
Access::Call(function) => types::Expression::FunctionCall(
|
||||
Box::new(acc),
|
||||
function
|
||||
.expressions
|
||||
@ -379,11 +383,11 @@ impl<'ast> From<PostfixExpression<'ast>> for types::Expression {
|
||||
),
|
||||
|
||||
// Handle circuit member accesses
|
||||
ast::Access::Object(circuit_object) => types::Expression::CircuitMemberAccess(
|
||||
Access::Object(circuit_object) => types::Expression::CircuitMemberAccess(
|
||||
Box::new(acc),
|
||||
types::Identifier::from(circuit_object.identifier),
|
||||
),
|
||||
ast::Access::StaticObject(circuit_object) => {
|
||||
Access::StaticObject(circuit_object) => {
|
||||
types::Expression::CircuitStaticFunctionAccess(
|
||||
Box::new(acc),
|
||||
types::Identifier::from(circuit_object.identifier),
|
||||
@ -437,13 +441,13 @@ impl<'ast> From<ast::Assignee<'ast>> for types::Expression {
|
||||
.accesses
|
||||
.into_iter()
|
||||
.fold(variable, |acc, access| match access {
|
||||
ast::AssigneeAccess::Member(circuit_member) => {
|
||||
AssigneeAccess::Member(circuit_member) => {
|
||||
types::Expression::CircuitMemberAccess(
|
||||
Box::new(acc),
|
||||
types::Identifier::from(circuit_member.identifier),
|
||||
)
|
||||
}
|
||||
ast::AssigneeAccess::Array(array) => types::Expression::ArrayAccess(
|
||||
AssigneeAccess::Array(array) => types::Expression::ArrayAccess(
|
||||
Box::new(acc),
|
||||
Box::new(types::RangeOrExpression::from(array.expression)),
|
||||
),
|
||||
@ -468,11 +472,11 @@ impl<'ast> From<ast::Assignee<'ast>> for types::Assignee {
|
||||
.accesses
|
||||
.into_iter()
|
||||
.fold(variable, |acc, access| match access {
|
||||
ast::AssigneeAccess::Array(array) => types::Assignee::Array(
|
||||
AssigneeAccess::Array(array) => types::Assignee::Array(
|
||||
Box::new(acc),
|
||||
types::RangeOrExpression::from(array.expression),
|
||||
),
|
||||
ast::AssigneeAccess::Member(circuit_field) => types::Assignee::CircuitField(
|
||||
AssigneeAccess::Member(circuit_field) => types::Assignee::CircuitField(
|
||||
Box::new(acc),
|
||||
types::Identifier::from(circuit_field.identifier),
|
||||
),
|
||||
|
Loading…
Reference in New Issue
Block a user