Merge pull request #326 from AleoHQ/refactor/array-initializer

Restrict pest grammar rule for array initializers
This commit is contained in:
Howard Wu 2020-09-02 13:40:55 -07:00 committed by GitHub
commit cb3fcf1d1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 4 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::{ast::Rule, common::SpreadOrExpression, values::PositiveNumber, SpanDef};
use crate::{ast::Rule, expressions::Expression, values::PositiveNumber, SpanDef};
use pest::Span;
use pest_ast::FromPest;
@ -23,7 +23,7 @@ use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::expression_array_initializer))]
pub struct ArrayInitializerExpression<'ast> {
pub expression: Box<SpreadOrExpression<'ast>>,
pub expression: Box<Expression<'ast>>,
pub count: PositiveNumber<'ast>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]

View File

@ -346,7 +346,7 @@ expression = { expression_term ~ (operation_binary ~ expression_term)* }
expression_tuple = { "(" ~ (expression ~ ("," ~ expression)*)? ~ ")" }
// Declared in expressions/array_initializer_expression.rs
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ number_positive ~ "]" }
expression_array_initializer = { "[" ~ expression ~ ";" ~ number_positive ~ "]" }
// Declared in expressions/array_inline_expression.rs
expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"}

View File

@ -15,7 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::Expression;
use leo_ast::common::SpreadOrExpression as AstSpreadOrExpression;
use leo_ast::{common::SpreadOrExpression as AstSpreadOrExpression, expressions::Expression as AstExpression};
use serde::{Deserialize, Serialize};
use std::fmt;
@ -38,6 +38,12 @@ impl<'ast> From<AstSpreadOrExpression<'ast>> for SpreadOrExpression {
}
}
impl<'ast> From<AstExpression<'ast>> for SpreadOrExpression {
fn from(expression: AstExpression<'ast>) -> Self {
SpreadOrExpression::Expression(Expression::from(expression))
}
}
impl fmt::Display for SpreadOrExpression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {