add updated values to inputs

This commit is contained in:
collin 2020-08-05 14:36:37 -07:00
parent 1c1822b4ff
commit 648acb7fe3
17 changed files with 285 additions and 155 deletions

View File

@ -5,7 +5,7 @@ use crate::{
sections::Header, sections::Header,
tables::Table, tables::Table,
types::{DataType, Type}, types::{DataType, Type},
values::{NumberImplicitValue, NumberValue, Value}, values::{NumberValue, PositiveNumber, Value},
}; };
use pest::{ use pest::{
@ -42,10 +42,10 @@ impl InputParserError {
InputParserError::SyntaxError(InputSyntaxError::from(error)) InputParserError::SyntaxError(InputSyntaxError::from(error))
} }
pub fn implicit_type(data_type: DataType, implicit: NumberImplicitValue) -> Self { pub fn implicit_type(data_type: DataType, implicit: NumberValue) -> Self {
let message = format!("expected `{}`, found `{}`", data_type.to_string(), implicit.to_string()); let message = format!("expected `{}`, found `{}`", data_type.to_string(), implicit.to_string());
Self::new_from_span(message, implicit.span) Self::new_from_span(message, implicit.span().clone())
} }
pub fn data_type_mismatch(data_type: DataType, value: Value) -> Self { pub fn data_type_mismatch(data_type: DataType, value: Value) -> Self {
@ -73,7 +73,7 @@ impl InputParserError {
Self::new_from_span(message, span) Self::new_from_span(message, span)
} }
pub fn array_init_length(number: NumberValue, array: ArrayInitializerExpression) -> Self { pub fn array_init_length(number: PositiveNumber, array: ArrayInitializerExpression) -> Self {
let message = format!( let message = format!(
"expected an array with a fixed size of {} elements, found one with {} elements", "expected an array with a fixed size of {} elements, found one with {} elements",
number.to_string(), number.to_string(),

View File

@ -37,32 +37,40 @@ type_ = { type_array | type_data }
// Declared in types/integer_type.rs // Declared in types/integer_type.rs
type_integer = { type_integer = {
type_u8 type_integer_signed
| type_u16 | type_integer_unsigned
| type_u32
| type_u64
| type_u128
| type_i8
| type_i16
| type_i32
| type_i64
| type_i128
} }
// Declared in types/integer_type.rs // Declared in types/unsigned_integer_type.rs
type_u8 = { "u8" } type_u8 = { "u8" }
type_u16 = { "u16" } type_u16 = { "u16" }
type_u32 = { "u32" } type_u32 = { "u32" }
type_u64 = { "u64" } type_u64 = { "u64" }
type_u128 = { "u128" } type_u128 = { "u128" }
// Declared in types/integer_type.rs type_integer_unsigned = {
type_u8
| type_u16
| type_u32
| type_u64
| type_u128
}
// Declared in types/signed_integer_type.rs
type_i8 = { "i8" } type_i8 = { "i8" }
type_i16 = { "i16" } type_i16 = { "i16" }
type_i32 = { "i32" } type_i32 = { "i32" }
type_i64 = { "i64" } type_i64 = { "i64" }
type_i128 = { "i128" } type_i128 = { "i128" }
type_integer_signed = {
type_i8
| type_i16
| type_i32
| type_i64
| type_i128
}
// Declared in types/field_type.rs // Declared in types/field_type.rs
type_field = { "field" } type_field = { "field" }
@ -90,33 +98,44 @@ value = {
| value_field | value_field
| value_group | value_group
| value_integer | value_integer
| value_implicit // must be last as a catch all | value_number // must be last as a catch all
} }
// Declared in values/number_value.rs // Declared in values/number_value.rs
value_number = @{ (("-" ~ ASCII_NONZERO_DIGIT) | "0" | ASCII_NONZERO_DIGIT) ~ ASCII_DIGIT* } value_number = { number_negative | number_positive }
// Declared in values/number_negative.rs
number_negative = @{ "-" ~ ASCII_DIGIT+ }
// Declared in values/number_positive.rs
number_positive = @{ ASCII_DIGIT+ }
// Declared in values/number_implicit_value.rs
value_implicit = { value_number }
// Declared in values/integer_value.rs // Declared in values/integer_value.rs
value_integer = { value_number ~ type_integer } value_integer = { value_integer_signed | value_integer_unsigned}
// Declared in values/signed_integer_value.rs
value_integer_signed = @{ value_number ~ type_integer_signed }
// Declared in values/unsigned_integer_value.rs
value_integer_unsigned = @{ number_positive ~ type_integer_unsigned }
// Declared in values/boolean_value.rs // Declared in values/boolean_value.rs
value_boolean = { "true" | "false" } value_boolean = { "true" | "false" }
// Declared in values/field_value.rs // Declared in values/field_value.rs
value_field = { value_number ~ type_field } value_field = @{ value_number ~ type_field }
// Declared in values/group_value.rs // Declared in values/group_value.rs
value_group = { group_tuple ~ type_group } value_group = @{ group_single_or_tuple ~ type_group }
group_tuple = { "(" ~ NEWLINE* ~ value_number ~ "," ~ NEWLINE* ~ value_number ~ NEWLINE* ~")" } group_tuple = !{"(" ~ value_number ~ "," ~ value_number ~ ")"}
group_single_or_tuple = {value_number | group_tuple}
// Declared in values/address.rs // Declared in values/address.rs
address = @{ (LOWERCASE_LETTER | ASCII_DIGIT)* } address = @{ "aleo" ~ ASCII_DIGIT ~ (LOWERCASE_LETTER | ASCII_DIGIT){58} }
// Declared in values/address_value.rs // Declared in values/address_value.rs
value_address = { type_address ~ "(" ~ address ~ ")" } value_address = @{ type_address ~ "(" ~ address ~ ")" }
/// Expressions /// Expressions

View File

@ -1,19 +1,19 @@
use crate::{ast::Rule, types::DataType, values::NumberValue}; use crate::{ast::Rule, types::DataType, values::PositiveNumber};
use pest::Span; use pest::Span;
use pest_ast::FromPest; use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)] #[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_array))] #[pest_ast(rule(Rule::type_array))]
pub struct ArrayType<'ast> { pub struct ArrayType<'ast> {
pub _type: DataType, pub _type: DataType,
pub dimensions: Vec<NumberValue<'ast>>, pub dimensions: Vec<PositiveNumber<'ast>>,
#[pest_ast(outer())] #[pest_ast(outer())]
pub span: Span<'ast>, pub span: Span<'ast>,
} }
impl<'ast> ArrayType<'ast> { impl<'ast> ArrayType<'ast> {
pub fn next_dimension(&mut self) -> Option<NumberValue<'ast>> { pub fn next_dimension(&mut self) -> Option<PositiveNumber<'ast>> {
self.dimensions.pop() self.dimensions.pop()
} }
} }

View File

@ -1,81 +1,23 @@
use crate::ast::Rule; use crate::{
ast::Rule,
types::{SignedIntegerType, UnsignedIntegerType},
};
use pest_ast::FromPest; use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)] #[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_integer))] #[pest_ast(rule(Rule::type_integer))]
pub enum IntegerType { pub enum IntegerType {
U8Type(U8Type), Signed(UnsignedIntegerType),
U16Type(U16Type), Unsigned(SignedIntegerType),
U32Type(U32Type),
U64Type(U64Type),
U128Type(U128Type),
I8Type(I8Type),
I16Type(I16Type),
I32Type(I32Type),
I64Type(I64Type),
I128Type(I128Type),
} }
// Unsigned impl fmt::Display for IntegerType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u8))]
pub struct U8Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u16))]
pub struct U16Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u32))]
pub struct U32Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u64))]
pub struct U64Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u128))]
pub struct U128Type {}
// Signed
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i8))]
pub struct I8Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i16))]
pub struct I16Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i32))]
pub struct I32Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i64))]
pub struct I64Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i128))]
pub struct I128Type {}
impl std::fmt::Display for IntegerType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
IntegerType::U8Type(_) => write!(f, "u8"), IntegerType::Signed(integer) => write!(f, "{}", integer),
IntegerType::U16Type(_) => write!(f, "u16"), IntegerType::Unsigned(integer) => write!(f, "{}", integer),
IntegerType::U32Type(_) => write!(f, "u32"),
IntegerType::U64Type(_) => write!(f, "u64"),
IntegerType::U128Type(_) => write!(f, "u128"),
IntegerType::I8Type(_) => write!(f, "i8"),
IntegerType::I16Type(_) => write!(f, "i16"),
IntegerType::I32Type(_) => write!(f, "i32"),
IntegerType::I64Type(_) => write!(f, "i64"),
IntegerType::I128Type(_) => write!(f, "i128"),
} }
} }
} }

View File

@ -19,5 +19,11 @@ pub use group_type::*;
pub mod integer_type; pub mod integer_type;
pub use integer_type::*; pub use integer_type::*;
pub mod signed_integer_type;
pub use signed_integer_type::*;
pub mod type_; pub mod type_;
pub use type_::*; pub use type_::*;
pub mod unsigned_integer_type;
pub use unsigned_integer_type::*;

View File

@ -0,0 +1,46 @@
use crate::ast::Rule;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_integer_signed))]
pub enum SignedIntegerType {
I8Type(I8Type),
I16Type(I16Type),
I32Type(I32Type),
I64Type(I64Type),
I128Type(I128Type),
}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i8))]
pub struct I8Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i16))]
pub struct I16Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i32))]
pub struct I32Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i64))]
pub struct I64Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_i128))]
pub struct I128Type {}
impl fmt::Display for SignedIntegerType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SignedIntegerType::I8Type(_) => write!(f, "i8"),
SignedIntegerType::I16Type(_) => write!(f, "i16"),
SignedIntegerType::I32Type(_) => write!(f, "i32"),
SignedIntegerType::I64Type(_) => write!(f, "i64"),
SignedIntegerType::I128Type(_) => write!(f, "i128"),
}
}
}

View File

@ -0,0 +1,46 @@
use crate::ast::Rule;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_integer_unsigned))]
pub enum UnsignedIntegerType {
U8Type(U8Type),
U16Type(U16Type),
U32Type(U32Type),
U64Type(U64Type),
U128Type(U128Type),
}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u8))]
pub struct U8Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u16))]
pub struct U16Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u32))]
pub struct U32Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u64))]
pub struct U64Type {}
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::type_u128))]
pub struct U128Type {}
impl fmt::Display for UnsignedIntegerType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
UnsignedIntegerType::U8Type(_) => write!(f, "u8"),
UnsignedIntegerType::U16Type(_) => write!(f, "u16"),
UnsignedIntegerType::U32Type(_) => write!(f, "u32"),
UnsignedIntegerType::U64Type(_) => write!(f, "u64"),
UnsignedIntegerType::U128Type(_) => write!(f, "u128"),
}
}
}

View File

@ -1,6 +1,5 @@
use crate::{ast::Rule, types::FieldType, values::NumberValue}; use crate::{ast::Rule, types::FieldType, values::NumberValue};
use crate::values::NumberImplicitValue;
use pest::Span; use pest::Span;
use pest_ast::FromPest; use pest_ast::FromPest;
use std::fmt; use std::fmt;
@ -14,16 +13,6 @@ pub struct FieldValue<'ast> {
pub span: Span<'ast>, pub span: Span<'ast>,
} }
impl<'ast> FieldValue<'ast> {
pub fn from_implicit(number: NumberImplicitValue<'ast>, type_: FieldType) -> Self {
Self {
number: number.number,
type_,
span: number.span,
}
}
}
impl<'ast> fmt::Display for FieldValue<'ast> { impl<'ast> fmt::Display for FieldValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.number) write!(f, "{}", self.number)

View File

@ -1,7 +1,6 @@
use crate::{ use crate::{
ast::Rule, ast::Rule,
types::IntegerType, values::{SignedIntegerValue, UnsignedIntegerValue},
values::{NumberImplicitValue, NumberValue},
}; };
use pest::Span; use pest::Span;
@ -10,25 +9,25 @@ use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)] #[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::value_integer))] #[pest_ast(rule(Rule::value_integer))]
pub struct IntegerValue<'ast> { pub enum IntegerValue<'ast> {
pub number: NumberValue<'ast>, Signed(SignedIntegerValue<'ast>),
pub type_: IntegerType, Unsigned(UnsignedIntegerValue<'ast>),
#[pest_ast(outer())]
pub span: Span<'ast>,
} }
impl<'ast> IntegerValue<'ast> { impl<'ast> IntegerValue<'ast> {
pub fn from_implicit(number: NumberImplicitValue<'ast>, type_: IntegerType) -> Self { pub fn span(&self) -> &Span<'ast> {
Self { match self {
number: number.number, IntegerValue::Signed(integer) => &integer.span,
type_, IntegerValue::Unsigned(integer) => &integer.span,
span: number.span,
} }
} }
} }
impl<'ast> fmt::Display for IntegerValue<'ast> { impl<'ast> fmt::Display for IntegerValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.number, self.type_) match self {
IntegerValue::Signed(integer) => write!(f, "{}", integer),
IntegerValue::Unsigned(integer) => write!(f, "{}", integer),
}
} }
} }

View File

@ -16,11 +16,20 @@ pub use group_value::*;
pub mod integer_value; pub mod integer_value;
pub use integer_value::*; pub use integer_value::*;
pub mod number_implicit_value; pub mod negative_number;
pub use number_implicit_value::*; pub use negative_number::*;
pub mod number_value; pub mod number_value;
pub use number_value::*; pub use number_value::*;
pub mod positive_number;
pub use positive_number::*;
pub mod signed_integer_value;
pub use signed_integer_value::*;
pub mod value; pub mod value;
pub use value::*; pub use value::*;
pub mod unsigned_integer_value;
pub use unsigned_integer_value::*;

View File

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

View File

@ -1,19 +0,0 @@
use crate::{ast::Rule, values::NumberValue};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::value_implicit))]
pub struct NumberImplicitValue<'ast> {
pub number: NumberValue<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for NumberImplicitValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.number)
}
}

View File

@ -1,4 +1,7 @@
use crate::ast::{span_into_string, Rule}; use crate::{
ast::Rule,
values::{NegativeNumber, PositiveNumber},
};
use pest::Span; use pest::Span;
use pest_ast::FromPest; use pest_ast::FromPest;
@ -6,15 +9,25 @@ use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)] #[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::value_number))] #[pest_ast(rule(Rule::value_number))]
pub struct NumberValue<'ast> { pub enum NumberValue<'ast> {
#[pest_ast(outer(with(span_into_string)))] Negative(NegativeNumber<'ast>),
pub value: String, Positive(PositiveNumber<'ast>),
#[pest_ast(outer())] }
pub span: Span<'ast>,
impl<'ast> NumberValue<'ast> {
pub fn span(&self) -> &Span<'ast> {
match self {
NumberValue::Negative(number) => &number.span,
NumberValue::Positive(number) => &number.span,
}
}
} }
impl<'ast> fmt::Display for NumberValue<'ast> { impl<'ast> fmt::Display for NumberValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value) match self {
NumberValue::Negative(number) => write!(f, "{}", number),
NumberValue::Positive(number) => write!(f, "{}", number),
}
} }
} }

View File

@ -0,0 +1,20 @@
use crate::ast::{span_into_string, Rule};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::number_positive))]
pub struct PositiveNumber<'ast> {
#[pest_ast(outer(with(span_into_string)))]
pub value: String,
#[pest_ast(outer())]
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

@ -0,0 +1,20 @@
use crate::{ast::Rule, types::SignedIntegerType, values::NumberValue};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::value_integer_signed))]
pub struct SignedIntegerValue<'ast> {
pub number: NumberValue<'ast>,
pub type_: SignedIntegerType,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for SignedIntegerValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.number, self.type_)
}
}

View File

@ -0,0 +1,20 @@
use crate::{ast::Rule, types::UnsignedIntegerType, values::PositiveNumber};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
#[pest_ast(rule(Rule::value_integer_unsigned))]
pub struct UnsignedIntegerValue<'ast> {
pub number: PositiveNumber<'ast>,
pub type_: UnsignedIntegerType,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for UnsignedIntegerValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.number, self.type_)
}
}

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
ast::Rule, ast::Rule,
values::{BooleanValue, FieldValue, GroupValue, IntegerValue, NumberImplicitValue}, values::{BooleanValue, FieldValue, GroupValue, IntegerValue, NumberValue},
}; };
use crate::values::AddressValue; use crate::values::AddressValue;
@ -15,7 +15,7 @@ pub enum Value<'ast> {
Boolean(BooleanValue<'ast>), Boolean(BooleanValue<'ast>),
Field(FieldValue<'ast>), Field(FieldValue<'ast>),
Group(GroupValue<'ast>), Group(GroupValue<'ast>),
Implicit(NumberImplicitValue<'ast>), Implicit(NumberValue<'ast>),
Integer(IntegerValue<'ast>), Integer(IntegerValue<'ast>),
} }
@ -26,8 +26,8 @@ impl<'ast> Value<'ast> {
Value::Boolean(value) => &value.span, Value::Boolean(value) => &value.span,
Value::Field(value) => &value.span, Value::Field(value) => &value.span,
Value::Group(value) => &value.span, Value::Group(value) => &value.span,
Value::Implicit(value) => &value.span, Value::Implicit(value) => &value.span(),
Value::Integer(value) => &value.span, Value::Integer(value) => &value.span(),
} }
} }
} }