transparent keywords

This commit is contained in:
Protryon 2021-03-12 04:25:25 -08:00
parent 6d378c37e6
commit 344f6a2d8e
5 changed files with 32 additions and 24 deletions

View File

@ -14,7 +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::{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;
}
}

View File

@ -14,15 +14,16 @@
// 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::{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;
}
}

View File

@ -14,7 +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::{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;
}
}

View File

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

View File

@ -309,11 +309,16 @@ impl ParserContext {
///
pub fn parse_function_input(&mut self) -> SyntaxResult<FunctionInput> {
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;