use positive_integer rule for array type and array initializer size

This commit is contained in:
collin 2020-08-03 14:50:18 -07:00
parent e7e84e6441
commit febdf51ca5
7 changed files with 57 additions and 23 deletions

View File

@ -1,4 +1,4 @@
use crate::{ast::Rule, common::SpreadOrExpression, values::Value, SpanDef};
use crate::{ast::Rule, common::SpreadOrExpression, values::PositiveNumber, SpanDef};
use pest::Span;
use pest_ast::FromPest;
@ -8,7 +8,7 @@ use serde::Serialize;
#[pest_ast(rule(Rule::expression_array_initializer))]
pub struct ArrayInitializerExpression<'ast> {
pub expression: Box<SpreadOrExpression<'ast>>,
pub count: Value<'ast>,
pub count: PositiveNumber<'ast>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,

View File

@ -169,7 +169,7 @@ type_self = { "Self" }
type_circuit = { identifier }
// Declared in types/array_type.rs
type_array = { type_data ~ ("[" ~ value ~ "]")+ }
type_array = { type_data ~ ("[" ~ positive_number ~ "]")+ }
type_list = _{ (type_ ~ ("," ~ type_)*)? }
@ -188,6 +188,9 @@ value = {
// Declared in values/number_value.rs
value_number = @{ (("-" ~ ASCII_NONZERO_DIGIT) | "0" | ASCII_NONZERO_DIGIT) ~ ASCII_DIGIT* }
// Declared in values/number_positive.rs
positive_number = @{ ("0" | ASCII_NONZERO_DIGIT) ~ ASCII_DIGIT*}
// Declared in values/number_implicit_value.rs
value_implicit = { value_number }
@ -271,7 +274,7 @@ expression_tuple = _{ (expression ~ ("," ~ expression)*)? }
expression = { expression_term ~ (operation_binary ~ expression_term)* }
// Declared in expressions/array_initializer_expression.rs
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ value ~ "]" }
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ positive_number ~ "]" }
// Declared in expressions/array_inline_expression.rs
expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"}

View File

@ -1,4 +1,4 @@
use crate::{ast::Rule, types::DataType, values::Value, SpanDef};
use crate::{ast::Rule, types::DataType, values::PositiveNumber, SpanDef};
use pest::Span;
use pest_ast::FromPest;
@ -8,7 +8,7 @@ use serde::Serialize;
#[pest_ast(rule(Rule::type_array))]
pub struct ArrayType<'ast> {
pub _type: DataType,
pub dimensions: Vec<Value<'ast>>,
pub dimensions: Vec<PositiveNumber<'ast>>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,

View File

@ -22,5 +22,8 @@ pub use number_implicit_value::*;
pub mod number_value;
pub use number_value::*;
pub mod positive_number;
pub use positive_number::*;
pub mod value;
pub use value::*;

View File

@ -0,0 +1,25 @@
use crate::{
ast::{span_into_string, Rule},
span::SpanDef,
};
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::positive_number))]
pub struct PositiveNumber<'ast> {
#[pest_ast(outer(with(span_into_string)))]
pub value: String,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for PositiveNumber<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}

View File

@ -13,11 +13,19 @@ use leo_ast::{
TernaryExpression,
},
operations::BinaryOperation,
values::{BooleanValue, FieldValue, GroupValue, IntegerValue, NumberImplicitValue, Value},
values::{
AddressValue,
BooleanValue,
FieldValue,
GroupValue,
IntegerValue,
NumberImplicitValue,
PositiveNumber as LeoPositiveNumber,
Value,
},
};
use leo_ast::values::AddressValue;
use leo_input::values::NumberValue;
use serde::{Deserialize, Serialize};
use std::fmt;
@ -104,21 +112,16 @@ impl Expression {
}
impl<'ast> Expression {
pub(crate) fn get_count_from_value(count: Value<'ast>) -> usize {
match count {
Value::Integer(integer) => integer
.number
.value
.parse::<usize>()
.expect("Unable to read array size"),
Value::Implicit(number) => number.number.value.parse::<usize>().expect("Unable to read array size"),
size => unimplemented!("Array size should be an integer {}", size),
}
}
pub(crate) fn get_count_from_number(number: NumberValue<'ast>) -> usize {
number.value.parse::<usize>().expect("Unable to read array size")
}
pub(crate) fn get_count_from_positive_number(number: LeoPositiveNumber<'ast>) -> usize {
number
.value
.parse::<usize>()
.expect("Array size should be a positive number")
}
}
impl<'ast> fmt::Display for Expression {
@ -403,7 +406,7 @@ impl<'ast> From<ArrayInlineExpression<'ast>> for Expression {
impl<'ast> From<ArrayInitializerExpression<'ast>> for Expression {
fn from(array: ArrayInitializerExpression<'ast>) -> Self {
let count = Expression::get_count_from_value(array.count);
let count = Expression::get_count_from_positive_number(array.count);
let expression = Box::new(SpreadOrExpression::from(*array.expression));
Expression::Array(vec![expression; count], Span::from(array.span))

View File

@ -101,7 +101,7 @@ impl<'ast> From<ArrayType<'ast>> for Type {
let dimensions = array_type
.dimensions
.into_iter()
.map(|row| Expression::get_count_from_value(row))
.map(|row| Expression::get_count_from_positive_number(row))
.collect();
Type::Array(element_type, dimensions)