From 3708c543622af84df10471dfc9b5c22650aa9d4e Mon Sep 17 00:00:00 2001 From: collin Date: Wed, 16 Sep 2020 14:27:44 -0700 Subject: [PATCH] make assignee rule more strict during pest parsing --- ast/src/common/assignee.rs | 4 +- ast/src/common/keyword_or_identifier.rs | 13 ++----- ast/src/common/mod.rs | 3 ++ ast/src/common/self_keyword_or_identifier.rs | 40 ++++++++++++++++++++ ast/src/leo.pest | 23 ++++++----- typed/src/common/assignee.rs | 6 +-- typed/src/common/identifier.rs | 23 ++++++----- 7 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 ast/src/common/self_keyword_or_identifier.rs diff --git a/ast/src/common/assignee.rs b/ast/src/common/assignee.rs index a11b442f29..6136b05440 100644 --- a/ast/src/common/assignee.rs +++ b/ast/src/common/assignee.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::{access::AssigneeAccess, ast::Rule, common::KeywordOrIdentifier, SpanDef}; +use crate::{access::AssigneeAccess, ast::Rule, common::SelfKeywordOrIdentifier, SpanDef}; use pest::Span; use pest_ast::FromPest; @@ -24,7 +24,7 @@ use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[pest_ast(rule(Rule::assignee))] pub struct Assignee<'ast> { - pub name: KeywordOrIdentifier<'ast>, + pub name: SelfKeywordOrIdentifier<'ast>, pub accesses: Vec>, #[pest_ast(outer())] #[serde(with = "SpanDef")] diff --git a/ast/src/common/keyword_or_identifier.rs b/ast/src/common/keyword_or_identifier.rs index d5a3c4ac7a..525b64e99f 100644 --- a/ast/src/common/keyword_or_identifier.rs +++ b/ast/src/common/keyword_or_identifier.rs @@ -14,13 +14,8 @@ // 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::{Identifier, SelfKeyword}, - functions::InputKeyword, -}; +use crate::{ast::Rule, common::SelfKeywordOrIdentifier, functions::InputKeyword, types::SelfType}; -use crate::types::SelfType; use pest_ast::FromPest; use serde::Serialize; use std::fmt; @@ -28,19 +23,17 @@ use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[pest_ast(rule(Rule::keyword_or_identifier))] pub enum KeywordOrIdentifier<'ast> { - SelfKeyword(SelfKeyword<'ast>), SelfType(SelfType<'ast>), Input(InputKeyword<'ast>), - Identifier(Identifier<'ast>), + SelfKeywordOrIdentifier(SelfKeywordOrIdentifier<'ast>), } impl<'ast> fmt::Display for KeywordOrIdentifier<'ast> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - KeywordOrIdentifier::SelfKeyword(self_keyword) => write!(f, "{}", self_keyword), KeywordOrIdentifier::SelfType(self_type) => write!(f, "{}", self_type), KeywordOrIdentifier::Input(input_keyword) => write!(f, "{}", input_keyword), - KeywordOrIdentifier::Identifier(identifier) => write!(f, "{}", identifier), + KeywordOrIdentifier::SelfKeywordOrIdentifier(name) => write!(f, "{}", name), } } } diff --git a/ast/src/common/mod.rs b/ast/src/common/mod.rs index 7029423a3e..119287f5cc 100644 --- a/ast/src/common/mod.rs +++ b/ast/src/common/mod.rs @@ -44,6 +44,9 @@ pub use range_or_expression::*; pub mod self_keyword; pub use self_keyword::*; +pub mod self_keyword_or_identifier; +pub use self_keyword_or_identifier::*; + pub mod spread; pub use spread::*; diff --git a/ast/src/common/self_keyword_or_identifier.rs b/ast/src/common/self_keyword_or_identifier.rs new file mode 100644 index 0000000000..44818d7610 --- /dev/null +++ b/ast/src/common/self_keyword_or_identifier.rs @@ -0,0 +1,40 @@ +// Copyright (C) 2019-2020 Aleo Systems Inc. +// This file is part of the Leo library. + +// The Leo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Leo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// 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::{Identifier, SelfKeyword}, +}; + +use pest_ast::FromPest; +use serde::Serialize; +use std::fmt; + +#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] +#[pest_ast(rule(Rule::self_keyword_or_identifier))] +pub enum SelfKeywordOrIdentifier<'ast> { + SelfKeyword(SelfKeyword<'ast>), + Identifier(Identifier<'ast>), +} + +impl<'ast> fmt::Display for SelfKeywordOrIdentifier<'ast> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + SelfKeywordOrIdentifier::SelfKeyword(self_keyword) => write!(f, "{}", self_keyword), + SelfKeywordOrIdentifier::Identifier(identifier) => write!(f, "{}", identifier), + } + } +} diff --git a/ast/src/leo.pest b/ast/src/leo.pest index 5a88b56dcb..69a002f37d 100644 --- a/ast/src/leo.pest +++ b/ast/src/leo.pest @@ -1,7 +1,7 @@ /// Common // Declared in common/assignee.rs -assignee = { keyword_or_identifier ~ access_assignee* } +assignee = { self_keyword_or_identifier ~ access_assignee* } // Declared in files/file.rs file = { SOI ~ NEWLINE* ~ definition* ~ NEWLINE* ~ EOI } @@ -47,6 +47,19 @@ protected_name = { // Declared in common/self_keyword.rs self_keyword = { "self" } +// Declared in common/self_keyword_or_identifier.rs +self_keyword_or_identifier = { + self_keyword + | identifier +} + +// Declared in common/keyword_or_identifier.rs +keyword_or_identifier = { + input_keyword + | type_self + | self_keyword_or_identifier +} + // Declared in common/line_end.rs LINE_END = { ";" ~ NEWLINE* } @@ -365,14 +378,6 @@ expression_unary = { operation_unary ~ expression_term } // Declared in expressions/postfix_expression.rs expression_postfix = ${ keyword_or_identifier ~ access+ } -// Declared in expressions/postfix_expression.rs -keyword_or_identifier = { - input_keyword - | self_keyword - | type_self - | identifier -} - /// Statements // Declared in statements/statement.rs diff --git a/typed/src/common/assignee.rs b/typed/src/common/assignee.rs index b5e0a54920..afa7f1c460 100644 --- a/typed/src/common/assignee.rs +++ b/typed/src/common/assignee.rs @@ -17,7 +17,7 @@ use crate::{Expression, Identifier, RangeOrExpression}; use leo_ast::{ access::AssigneeAccess as AstAssigneeAccess, - common::{Assignee as AstAssignee, Identifier as AstIdentifier, KeywordOrIdentifier}, + common::{Assignee as AstAssignee, Identifier as AstIdentifier, SelfKeywordOrIdentifier}, }; use serde::{Deserialize, Serialize}; @@ -38,8 +38,8 @@ impl<'ast> From> for Assignee { } } -impl<'ast> From> for Assignee { - fn from(name: KeywordOrIdentifier<'ast>) -> Self { +impl<'ast> From> for Assignee { + fn from(name: SelfKeywordOrIdentifier<'ast>) -> Self { Assignee::Identifier(Identifier::from(name)) } } diff --git a/typed/src/common/identifier.rs b/typed/src/common/identifier.rs index 9d71fe5350..c10474e62c 100644 --- a/typed/src/common/identifier.rs +++ b/typed/src/common/identifier.rs @@ -17,17 +17,14 @@ use crate::Span; use leo_ast::{ annotations::AnnotationArgument, - common::Identifier as AstIdentifier, + common::{Identifier as AstIdentifier, KeywordOrIdentifier, SelfKeyword, SelfKeywordOrIdentifier}, + expressions::CircuitName, + functions::InputKeyword, imports::PackageName as AstPackageName, + types::SelfType, }; use leo_input::common::Identifier as InputAstIdentifier; -use leo_ast::{ - common::{KeywordOrIdentifier, SelfKeyword}, - expressions::CircuitName, - functions::InputKeyword, - types::SelfType, -}; use serde::{ de::{self, Visitor}, Deserialize, @@ -93,10 +90,18 @@ impl<'ast> From> for Identifier { impl<'ast> From> for Identifier { fn from(name: KeywordOrIdentifier<'ast>) -> Self { match name { - KeywordOrIdentifier::SelfKeyword(keyword) => Identifier::from(keyword), + KeywordOrIdentifier::SelfKeywordOrIdentifier(keyword) => Identifier::from(keyword), KeywordOrIdentifier::SelfType(self_type) => Identifier::from(self_type), KeywordOrIdentifier::Input(keyword) => Identifier::from(keyword), - KeywordOrIdentifier::Identifier(identifier) => Identifier::from(identifier), + } + } +} + +impl<'ast> From> for Identifier { + fn from(name: SelfKeywordOrIdentifier<'ast>) -> Self { + match name { + SelfKeywordOrIdentifier::Identifier(identifier) => Identifier::from(identifier), + SelfKeywordOrIdentifier::SelfKeyword(keyword) => Identifier::from(keyword), } } }