From f8668303e3a05b0998124dac0dee20cef562d7fe Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 19 Nov 2020 15:22:49 -0800 Subject: [PATCH] add self and mut self to grammar module --- grammar/src/circuits/circuit_member.rs | 7 ++----- grammar/src/circuits/mod.rs | 4 ++-- grammar/src/common/mod.rs | 3 +++ .../mut_self_keyword.rs} | 21 ++++++++++++++----- grammar/src/functions/input/input.rs | 3 +++ grammar/src/leo.pest | 10 +++++---- 6 files changed, 32 insertions(+), 16 deletions(-) rename grammar/src/{circuits/circuit_function.rs => common/mut_self_keyword.rs} (70%) diff --git a/grammar/src/circuits/circuit_member.rs b/grammar/src/circuits/circuit_member.rs index d3d949e99b..32da1ca4d3 100644 --- a/grammar/src/circuits/circuit_member.rs +++ b/grammar/src/circuits/circuit_member.rs @@ -14,10 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - ast::Rule, - circuits::{CircuitFunction, CircuitVariableDefinition}, -}; +use crate::{ast::Rule, circuits::CircuitVariableDefinition, functions::Function}; use pest_ast::FromPest; use serde::Serialize; @@ -26,5 +23,5 @@ use serde::Serialize; #[pest_ast(rule(Rule::circuit_member))] pub enum CircuitMember<'ast> { CircuitVariableDefinition(CircuitVariableDefinition<'ast>), - CircuitFunction(CircuitFunction<'ast>), + CircuitFunction(Function<'ast>), } diff --git a/grammar/src/circuits/mod.rs b/grammar/src/circuits/mod.rs index 768a36fd7e..2f55b59400 100644 --- a/grammar/src/circuits/mod.rs +++ b/grammar/src/circuits/mod.rs @@ -23,8 +23,8 @@ pub use circuit_variable::*; pub mod circuit_variable_definition; pub use circuit_variable_definition::*; -pub mod circuit_function; -pub use circuit_function::*; +// pub mod circuit_function; +// pub use circuit_function::*; pub mod circuit_member; pub use circuit_member::*; diff --git a/grammar/src/common/mod.rs b/grammar/src/common/mod.rs index 119287f5cc..6a45652fc7 100644 --- a/grammar/src/common/mod.rs +++ b/grammar/src/common/mod.rs @@ -35,6 +35,9 @@ pub use line_end::*; pub mod mutable; pub use mutable::*; +pub mod mut_self_keyword; +pub use mut_self_keyword::*; + pub mod range; pub use range::*; diff --git a/grammar/src/circuits/circuit_function.rs b/grammar/src/common/mut_self_keyword.rs similarity index 70% rename from grammar/src/circuits/circuit_function.rs rename to grammar/src/common/mut_self_keyword.rs index 3fdeac1d02..12d269e191 100644 --- a/grammar/src/circuits/circuit_function.rs +++ b/grammar/src/common/mut_self_keyword.rs @@ -14,18 +14,29 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ast::Rule, common::Static, functions::Function, SpanDef}; +use crate::{ + ast::Rule, + common::{Mutable, SelfKeyword}, + SpanDef, +}; use pest::Span; use pest_ast::FromPest; use serde::Serialize; +use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] -#[pest_ast(rule(Rule::circuit_function))] -pub struct CircuitFunction<'ast> { - pub _static: Option, - pub function: Function<'ast>, +#[pest_ast(rule(Rule::mut_self_keyword))] +pub struct MutSelfKeyword<'ast> { + pub mutable: Mutable, + pub self_keyword: SelfKeyword<'ast>, #[pest_ast(outer())] #[serde(with = "SpanDef")] pub span: Span<'ast>, } + +impl<'ast> fmt::Display for MutSelfKeyword<'ast> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "mut {}", self.self_keyword) + } +} diff --git a/grammar/src/functions/input/input.rs b/grammar/src/functions/input/input.rs index d3cddee215..d6eb28250c 100644 --- a/grammar/src/functions/input/input.rs +++ b/grammar/src/functions/input/input.rs @@ -16,6 +16,7 @@ use crate::{ ast::Rule, + common::{MutSelfKeyword, SelfKeyword}, functions::{FunctionInput, InputKeyword}, }; @@ -26,5 +27,7 @@ use serde::Serialize; #[pest_ast(rule(Rule::input))] pub enum Input<'ast> { InputKeyword(InputKeyword<'ast>), + SelfKeyword(SelfKeyword<'ast>), + MutSelfKeyword(MutSelfKeyword<'ast>), FunctionInput(FunctionInput<'ast>), } diff --git a/grammar/src/leo.pest b/grammar/src/leo.pest index e695e3bb9f..b065834413 100644 --- a/grammar/src/leo.pest +++ b/grammar/src/leo.pest @@ -47,6 +47,9 @@ protected_name = { // Declared in common/self_keyword.rs self_keyword = { "self" } +// Declared in common/mut_self_keyword.rs +mut_self_keyword = { mutable ~ self_keyword } + // Declared in common/self_keyword_or_identifier.rs self_keyword_or_identifier = { self_keyword @@ -321,11 +324,8 @@ circuit_variable = { identifier ~ ":" ~ expression } // Declared in circuits/circuit_variable_definition.rs circuit_variable_definition = { mutable? ~ identifier ~ ":" ~ type_ ~ ","?} -// Declared in circuits/circuit_function.rs -circuit_function = { static_? ~ function } - // Declared in circuits/circuit_member.rs -circuit_member = { circuit_function | circuit_variable_definition ~ NEWLINE*} +circuit_member = { function | circuit_variable_definition ~ NEWLINE*} /// Conditionals @@ -429,6 +429,8 @@ input_keyword = { "input" } // Declared in functions/input/input.rs input = { input_keyword + | self_keyword + | mut_self_keyword | function_input } input_tuple = _{ "(" ~ NEWLINE* ~ (input ~ ("," ~ NEWLINE* ~ input)* ~ ","?)? ~ NEWLINE* ~ ")"}