From 344f6a2d8ef2da81a58b7d6f6d1faf297a95ce14 Mon Sep 17 00:00:00 2001 From: Protryon Date: Fri, 12 Mar 2021 04:25:25 -0800 Subject: [PATCH 1/2] transparent keywords --- ast/src/common/input_keyword.rs | 9 +++++---- ast/src/common/mut_self_keyword.rs | 9 +++++---- ast/src/common/self_keyword.rs | 9 +++++---- ast/src/functions/input/input_variable.rs | 12 ++++++------ parser/src/parser/file.rs | 17 +++++++++++------ 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/ast/src/common/input_keyword.rs b/ast/src/common/input_keyword.rs index cc9d167861..e0cae2d4d5 100644 --- a/ast/src/common/input_keyword.rs +++ b/ast/src/common/input_keyword.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Node, Span}; +use crate::{Identifier, Node, Span}; use serde::{Deserialize, Serialize}; use std::fmt; @@ -22,8 +22,9 @@ use std::fmt; /// The `input` keyword can view program register, record, and state values. /// Values cannot be modified. The `input` keyword cannot be made mutable. #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] +#[serde(transparent)] pub struct InputKeyword { - pub span: Span, + pub ident: Identifier, } impl fmt::Display for InputKeyword { @@ -34,10 +35,10 @@ impl fmt::Display for InputKeyword { impl Node for InputKeyword { fn span(&self) -> &Span { - &self.span + &self.ident.span } fn set_span(&mut self, span: Span) { - self.span = span; + self.ident.span = span; } } diff --git a/ast/src/common/mut_self_keyword.rs b/ast/src/common/mut_self_keyword.rs index 34de7c9921..e5cb3f1d5b 100644 --- a/ast/src/common/mut_self_keyword.rs +++ b/ast/src/common/mut_self_keyword.rs @@ -14,15 +14,16 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Node, Span}; +use crate::{Identifier, Node, Span}; use serde::{Deserialize, Serialize}; use std::fmt; /// The `mut self` keyword can view and modify circuit values inside of a circuit function. #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] +#[serde(transparent)] pub struct MutSelfKeyword { - pub span: Span, + pub ident: Identifier, } impl fmt::Display for MutSelfKeyword { @@ -33,10 +34,10 @@ impl fmt::Display for MutSelfKeyword { impl Node for MutSelfKeyword { fn span(&self) -> &Span { - &self.span + &self.ident.span } fn set_span(&mut self, span: Span) { - self.span = span; + self.ident.span = span; } } diff --git a/ast/src/common/self_keyword.rs b/ast/src/common/self_keyword.rs index ba92db2160..5b80b97ac6 100644 --- a/ast/src/common/self_keyword.rs +++ b/ast/src/common/self_keyword.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Node, Span}; +use crate::{Identifier, Node, Span}; use serde::{Deserialize, Serialize}; use std::fmt; @@ -22,8 +22,9 @@ use std::fmt; /// The `self` keyword can view circuit values inside of a circuit function. /// Circuit values cannot be modified. To modify values use the `mut self` [MutSelfKeyword]. #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] +#[serde(transparent)] pub struct SelfKeyword { - pub span: Span, + pub ident: Identifier, } impl fmt::Display for SelfKeyword { @@ -34,10 +35,10 @@ impl fmt::Display for SelfKeyword { impl Node for SelfKeyword { fn span(&self) -> &Span { - &self.span + &self.ident.span } fn set_span(&mut self, span: Span) { - self.span = span; + self.ident.span = span; } } diff --git a/ast/src/functions/input/input_variable.rs b/ast/src/functions/input/input_variable.rs index f342cb3e1b..66116f4988 100644 --- a/ast/src/functions/input/input_variable.rs +++ b/ast/src/functions/input/input_variable.rs @@ -96,9 +96,9 @@ impl Node for FunctionInput { fn span(&self) -> &Span { use FunctionInput::*; match self { - InputKeyword(keyword) => &keyword.span, - SelfKeyword(keyword) => &keyword.span, - MutSelfKeyword(keyword) => &keyword.span, + InputKeyword(keyword) => &keyword.ident.span, + SelfKeyword(keyword) => &keyword.ident.span, + MutSelfKeyword(keyword) => &keyword.ident.span, Variable(variable) => &variable.span, } } @@ -106,9 +106,9 @@ impl Node for FunctionInput { fn set_span(&mut self, span: Span) { use FunctionInput::*; match self { - InputKeyword(keyword) => keyword.span = span, - SelfKeyword(keyword) => keyword.span = span, - MutSelfKeyword(keyword) => keyword.span = span, + InputKeyword(keyword) => keyword.ident.span = span, + SelfKeyword(keyword) => keyword.ident.span = span, + MutSelfKeyword(keyword) => keyword.ident.span = span, Variable(variable) => variable.span = span, } } diff --git a/parser/src/parser/file.rs b/parser/src/parser/file.rs index 36dca63852..4d19a782e8 100644 --- a/parser/src/parser/file.rs +++ b/parser/src/parser/file.rs @@ -309,11 +309,16 @@ impl ParserContext { /// pub fn parse_function_input(&mut self) -> SyntaxResult { if let Some(token) = self.eat(Token::Input) { - return Ok(FunctionInput::InputKeyword(InputKeyword { span: token.span })); + return Ok(FunctionInput::InputKeyword(InputKeyword { + ident: Identifier { + name: token.token.to_string(), + span: token.span, + }, + })); } let const_ = self.eat(Token::Const); let mutable = self.eat(Token::Mut); - let name = if let Some(token) = self.eat(Token::LittleSelf) { + let mut name = if let Some(token) = self.eat(Token::LittleSelf) { Identifier { name: token.token.to_string(), span: token.span, @@ -326,11 +331,11 @@ impl ParserContext { //error } if let Some(mutable) = &mutable { - return Ok(FunctionInput::MutSelfKeyword(MutSelfKeyword { - span: &mutable.span + &name.span, - })); + name.span = &mutable.span + &name.span; + name.name = "mut self".to_string(); + return Ok(FunctionInput::MutSelfKeyword(MutSelfKeyword { ident: name })); } - return Ok(FunctionInput::SelfKeyword(SelfKeyword { span: name.span })); + return Ok(FunctionInput::SelfKeyword(SelfKeyword { ident: name })); } self.expect(Token::Colon)?; let type_ = self.parse_type()?.0; From 0c72e3e26466dfcd0d0e182576e95b13b8b865be Mon Sep 17 00:00:00 2001 From: Protryon Date: Fri, 12 Mar 2021 10:40:05 -0800 Subject: [PATCH 2/2] ident -> identifier --- ast/src/common/input_keyword.rs | 6 +++--- ast/src/common/mut_self_keyword.rs | 6 +++--- ast/src/common/self_keyword.rs | 6 +++--- ast/src/functions/input/input_variable.rs | 12 ++++++------ parser/src/parser/expression.rs | 6 +++--- parser/src/parser/file.rs | 6 +++--- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ast/src/common/input_keyword.rs b/ast/src/common/input_keyword.rs index e0cae2d4d5..fecf89322e 100644 --- a/ast/src/common/input_keyword.rs +++ b/ast/src/common/input_keyword.rs @@ -24,7 +24,7 @@ use std::fmt; #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(transparent)] pub struct InputKeyword { - pub ident: Identifier, + pub identifier: Identifier, } impl fmt::Display for InputKeyword { @@ -35,10 +35,10 @@ impl fmt::Display for InputKeyword { impl Node for InputKeyword { fn span(&self) -> &Span { - &self.ident.span + &self.identifier.span } fn set_span(&mut self, span: Span) { - self.ident.span = span; + self.identifier.span = span; } } diff --git a/ast/src/common/mut_self_keyword.rs b/ast/src/common/mut_self_keyword.rs index e5cb3f1d5b..d28c501be8 100644 --- a/ast/src/common/mut_self_keyword.rs +++ b/ast/src/common/mut_self_keyword.rs @@ -23,7 +23,7 @@ use std::fmt; #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(transparent)] pub struct MutSelfKeyword { - pub ident: Identifier, + pub identifier: Identifier, } impl fmt::Display for MutSelfKeyword { @@ -34,10 +34,10 @@ impl fmt::Display for MutSelfKeyword { impl Node for MutSelfKeyword { fn span(&self) -> &Span { - &self.ident.span + &self.identifier.span } fn set_span(&mut self, span: Span) { - self.ident.span = span; + self.identifier.span = span; } } diff --git a/ast/src/common/self_keyword.rs b/ast/src/common/self_keyword.rs index 5b80b97ac6..06b6bb12a8 100644 --- a/ast/src/common/self_keyword.rs +++ b/ast/src/common/self_keyword.rs @@ -24,7 +24,7 @@ use std::fmt; #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(transparent)] pub struct SelfKeyword { - pub ident: Identifier, + pub identifier: Identifier, } impl fmt::Display for SelfKeyword { @@ -35,10 +35,10 @@ impl fmt::Display for SelfKeyword { impl Node for SelfKeyword { fn span(&self) -> &Span { - &self.ident.span + &self.identifier.span } fn set_span(&mut self, span: Span) { - self.ident.span = span; + self.identifier.span = span; } } diff --git a/ast/src/functions/input/input_variable.rs b/ast/src/functions/input/input_variable.rs index 66116f4988..5ec9306aeb 100644 --- a/ast/src/functions/input/input_variable.rs +++ b/ast/src/functions/input/input_variable.rs @@ -96,9 +96,9 @@ impl Node for FunctionInput { fn span(&self) -> &Span { use FunctionInput::*; match self { - InputKeyword(keyword) => &keyword.ident.span, - SelfKeyword(keyword) => &keyword.ident.span, - MutSelfKeyword(keyword) => &keyword.ident.span, + InputKeyword(keyword) => &keyword.identifier.span, + SelfKeyword(keyword) => &keyword.identifier.span, + MutSelfKeyword(keyword) => &keyword.identifier.span, Variable(variable) => &variable.span, } } @@ -106,9 +106,9 @@ impl Node for FunctionInput { fn set_span(&mut self, span: Span) { use FunctionInput::*; match self { - InputKeyword(keyword) => keyword.ident.span = span, - SelfKeyword(keyword) => keyword.ident.span = span, - MutSelfKeyword(keyword) => keyword.ident.span = span, + InputKeyword(keyword) => keyword.identifier.span = span, + SelfKeyword(keyword) => keyword.identifier.span = span, + MutSelfKeyword(keyword) => keyword.identifier.span = span, Variable(variable) => variable.span = span, } } diff --git a/parser/src/parser/expression.rs b/parser/src/parser/expression.rs index ec4df7c2cd..f8dbac1f0e 100644 --- a/parser/src/parser/expression.rs +++ b/parser/src/parser/expression.rs @@ -507,7 +507,7 @@ impl ParserContext { /// Returns an [`Expression`] AST node if the next tokens represent an /// circuit initialization expression. /// - pub fn parse_circuit_init(&mut self, ident: Identifier) -> SyntaxResult { + pub fn parse_circuit_init(&mut self, identifier: Identifier) -> SyntaxResult { self.expect(Token::LeftCurly)?; let mut members = vec![]; let end_span; @@ -535,8 +535,8 @@ impl ParserContext { } } Ok(Expression::CircuitInit(CircuitInitExpression { - span: &ident.span + &end_span, - name: ident, + span: &identifier.span + &end_span, + name: identifier, members, })) } diff --git a/parser/src/parser/file.rs b/parser/src/parser/file.rs index 4d19a782e8..f61f228976 100644 --- a/parser/src/parser/file.rs +++ b/parser/src/parser/file.rs @@ -310,7 +310,7 @@ impl ParserContext { pub fn parse_function_input(&mut self) -> SyntaxResult { if let Some(token) = self.eat(Token::Input) { return Ok(FunctionInput::InputKeyword(InputKeyword { - ident: Identifier { + identifier: Identifier { name: token.token.to_string(), span: token.span, }, @@ -333,9 +333,9 @@ impl ParserContext { if let Some(mutable) = &mutable { name.span = &mutable.span + &name.span; name.name = "mut self".to_string(); - return Ok(FunctionInput::MutSelfKeyword(MutSelfKeyword { ident: name })); + return Ok(FunctionInput::MutSelfKeyword(MutSelfKeyword { identifier: name })); } - return Ok(FunctionInput::SelfKeyword(SelfKeyword { ident: name })); + return Ok(FunctionInput::SelfKeyword(SelfKeyword { identifier: name })); } self.expect(Token::Colon)?; let type_ = self.parse_type()?.0;