add self and mut self to grammar module

This commit is contained in:
collin 2020-11-19 15:22:49 -08:00
parent 2cb44160ab
commit f8668303e3
6 changed files with 32 additions and 16 deletions

View File

@ -14,10 +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 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>),
}

View File

@ -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::*;

View File

@ -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::*;

View File

@ -14,18 +14,29 @@
// 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 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<Static>,
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)
}
}

View File

@ -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>),
}

View File

@ -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* ~ ")"}