Merge pull request #774 from AleoHQ/transparent-keywords

Transparent keywords
This commit is contained in:
Howard Wu 2021-03-18 15:47:06 -07:00 committed by GitHub
commit 19e40a89a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 27 deletions

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // 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/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{Node, Span}; use crate::{Identifier, Node, Span};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
@ -22,8 +22,9 @@ use std::fmt;
/// The `input` keyword can view program register, record, and state values. /// The `input` keyword can view program register, record, and state values.
/// Values cannot be modified. The `input` keyword cannot be made mutable. /// Values cannot be modified. The `input` keyword cannot be made mutable.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(transparent)]
pub struct InputKeyword { pub struct InputKeyword {
pub span: Span, pub identifier: Identifier,
} }
impl fmt::Display for InputKeyword { impl fmt::Display for InputKeyword {
@ -34,10 +35,10 @@ impl fmt::Display for InputKeyword {
impl Node for InputKeyword { impl Node for InputKeyword {
fn span(&self) -> &Span { fn span(&self) -> &Span {
&self.span &self.identifier.span
} }
fn set_span(&mut self, span: Span) { fn set_span(&mut self, span: Span) {
self.span = span; self.identifier.span = span;
} }
} }

View File

@ -14,15 +14,16 @@
// You should have received a copy of the GNU General Public License // 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/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{Node, Span}; use crate::{Identifier, Node, Span};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
/// The `mut self` keyword can view and modify circuit values inside of a circuit function. /// The `mut self` keyword can view and modify circuit values inside of a circuit function.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(transparent)]
pub struct MutSelfKeyword { pub struct MutSelfKeyword {
pub span: Span, pub identifier: Identifier,
} }
impl fmt::Display for MutSelfKeyword { impl fmt::Display for MutSelfKeyword {
@ -33,10 +34,10 @@ impl fmt::Display for MutSelfKeyword {
impl Node for MutSelfKeyword { impl Node for MutSelfKeyword {
fn span(&self) -> &Span { fn span(&self) -> &Span {
&self.span &self.identifier.span
} }
fn set_span(&mut self, span: Span) { fn set_span(&mut self, span: Span) {
self.span = span; self.identifier.span = span;
} }
} }

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // 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/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{Node, Span}; use crate::{Identifier, Node, Span};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
@ -22,8 +22,9 @@ use std::fmt;
/// The `self` keyword can view circuit values inside of a circuit function. /// 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]. /// Circuit values cannot be modified. To modify values use the `mut self` [MutSelfKeyword].
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(transparent)]
pub struct SelfKeyword { pub struct SelfKeyword {
pub span: Span, pub identifier: Identifier,
} }
impl fmt::Display for SelfKeyword { impl fmt::Display for SelfKeyword {
@ -34,10 +35,10 @@ impl fmt::Display for SelfKeyword {
impl Node for SelfKeyword { impl Node for SelfKeyword {
fn span(&self) -> &Span { fn span(&self) -> &Span {
&self.span &self.identifier.span
} }
fn set_span(&mut self, span: Span) { fn set_span(&mut self, span: Span) {
self.span = span; self.identifier.span = span;
} }
} }

View File

@ -96,9 +96,9 @@ impl Node for FunctionInput {
fn span(&self) -> &Span { fn span(&self) -> &Span {
use FunctionInput::*; use FunctionInput::*;
match self { match self {
InputKeyword(keyword) => &keyword.span, InputKeyword(keyword) => &keyword.identifier.span,
SelfKeyword(keyword) => &keyword.span, SelfKeyword(keyword) => &keyword.identifier.span,
MutSelfKeyword(keyword) => &keyword.span, MutSelfKeyword(keyword) => &keyword.identifier.span,
Variable(variable) => &variable.span, Variable(variable) => &variable.span,
} }
} }
@ -106,9 +106,9 @@ impl Node for FunctionInput {
fn set_span(&mut self, span: Span) { fn set_span(&mut self, span: Span) {
use FunctionInput::*; use FunctionInput::*;
match self { match self {
InputKeyword(keyword) => keyword.span = span, InputKeyword(keyword) => keyword.identifier.span = span,
SelfKeyword(keyword) => keyword.span = span, SelfKeyword(keyword) => keyword.identifier.span = span,
MutSelfKeyword(keyword) => keyword.span = span, MutSelfKeyword(keyword) => keyword.identifier.span = span,
Variable(variable) => variable.span = span, Variable(variable) => variable.span = span,
} }
} }

View File

@ -507,7 +507,7 @@ impl ParserContext {
/// Returns an [`Expression`] AST node if the next tokens represent an /// Returns an [`Expression`] AST node if the next tokens represent an
/// circuit initialization expression. /// circuit initialization expression.
/// ///
pub fn parse_circuit_init(&mut self, ident: Identifier) -> SyntaxResult<Expression> { pub fn parse_circuit_init(&mut self, identifier: Identifier) -> SyntaxResult<Expression> {
self.expect(Token::LeftCurly)?; self.expect(Token::LeftCurly)?;
let mut members = vec![]; let mut members = vec![];
let end_span; let end_span;
@ -535,8 +535,8 @@ impl ParserContext {
} }
} }
Ok(Expression::CircuitInit(CircuitInitExpression { Ok(Expression::CircuitInit(CircuitInitExpression {
span: &ident.span + &end_span, span: &identifier.span + &end_span,
name: ident, name: identifier,
members, members,
})) }))
} }

View File

@ -309,11 +309,16 @@ impl ParserContext {
/// ///
pub fn parse_function_input(&mut self) -> SyntaxResult<FunctionInput> { pub fn parse_function_input(&mut self) -> SyntaxResult<FunctionInput> {
if let Some(token) = self.eat(Token::Input) { if let Some(token) = self.eat(Token::Input) {
return Ok(FunctionInput::InputKeyword(InputKeyword { span: token.span })); return Ok(FunctionInput::InputKeyword(InputKeyword {
identifier: Identifier {
name: token.token.to_string(),
span: token.span,
},
}));
} }
let const_ = self.eat(Token::Const); let const_ = self.eat(Token::Const);
let mutable = self.eat(Token::Mut); 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 { Identifier {
name: token.token.to_string(), name: token.token.to_string(),
span: token.span, span: token.span,
@ -326,11 +331,11 @@ impl ParserContext {
//error //error
} }
if let Some(mutable) = &mutable { if let Some(mutable) = &mutable {
return Ok(FunctionInput::MutSelfKeyword(MutSelfKeyword { name.span = &mutable.span + &name.span;
span: &mutable.span + &name.span, name.name = "mut self".to_string();
})); return Ok(FunctionInput::MutSelfKeyword(MutSelfKeyword { identifier: name }));
} }
return Ok(FunctionInput::SelfKeyword(SelfKeyword { span: name.span })); return Ok(FunctionInput::SelfKeyword(SelfKeyword { identifier: name }));
} }
self.expect(Token::Colon)?; self.expect(Token::Colon)?;
let type_ = self.parse_type()?.0; let type_ = self.parse_type()?.0;