From 172431c2223531019d02105bd9639af065af48af Mon Sep 17 00:00:00 2001 From: collin Date: Sat, 15 Aug 2020 16:45:43 -0700 Subject: [PATCH] impl new group notation for inputs ast + typed --- ast/src/values/group_value.rs | 2 +- input/src/errors/parser.rs | 6 +++ input/src/leo-input.pest | 17 +++++++-- input/src/values/group_coordinate.rs | 57 ++++++++++++++++++++++++++++ input/src/values/group_value.rs | 24 ++---------- input/src/values/mod.rs | 3 ++ typed/src/groups/group_coordinate.rs | 45 ++++++++++++++++++++++ typed/src/groups/group_value.rs | 14 +++++++ typed/src/input/input_value.rs | 11 +++--- 9 files changed, 150 insertions(+), 29 deletions(-) create mode 100644 input/src/values/group_coordinate.rs diff --git a/ast/src/values/group_value.rs b/ast/src/values/group_value.rs index f406635172..ee9310de90 100644 --- a/ast/src/values/group_value.rs +++ b/ast/src/values/group_value.rs @@ -9,7 +9,7 @@ use std::fmt; #[pest_ast(rule(Rule::value_group))] pub struct GroupValue<'ast> { pub value: GroupTuple<'ast>, - pub _type: GroupType, + pub type_: GroupType, #[pest_ast(outer())] #[serde(with = "SpanDef")] pub span: Span<'ast>, diff --git a/input/src/errors/parser.rs b/input/src/errors/parser.rs index d09c6e9fe7..d5ae505ae8 100644 --- a/input/src/errors/parser.rs +++ b/input/src/errors/parser.rs @@ -48,6 +48,12 @@ impl InputParserError { Self::new_from_span(message, implicit.span().clone()) } + pub fn implicit_group(number: NumberValue) -> Self { + let message = format!("group coordinates should be in (x, y)group format, found `{}`", number); + + Self::new_from_span(message, number.span().clone()) + } + pub fn data_type_mismatch(data_type: DataType, value: Value) -> Self { let message = format!("expected `{}`, found `{}`", data_type.to_string(), value.to_string()); let span = value.span().to_owned(); diff --git a/input/src/leo-input.pest b/input/src/leo-input.pest index afafbf8ba3..2c9469317e 100644 --- a/input/src/leo-input.pest +++ b/input/src/leo-input.pest @@ -131,9 +131,20 @@ value_boolean = { "true" | "false" } value_field = ${ value_number ~ type_field } // Declared in values/group_value.rs -value_group = ${ group_single_or_tuple ~ type_group } -group_tuple = !{"(" ~ value_number ~ "," ~ value_number ~ ")"} -group_single_or_tuple = {value_number | group_tuple} +value_group = ${ group_tuple ~ type_group } +group_tuple = !{"(" ~ group_coordinate ~ "," ~ group_coordinate ~ ")"} + +// Declared in values/group_coordinate.rs +group_coordinate = { + value_number + | sign_high + | sign_low + | inferred +} + +sign_high = @{"+"} +sign_low = @{"-"} +inferred = @{"_"} // Declared in values/address.rs address = @{ "aleo" ~ ASCII_DIGIT ~ (LOWERCASE_LETTER | ASCII_DIGIT){58} } diff --git a/input/src/values/group_coordinate.rs b/input/src/values/group_coordinate.rs new file mode 100644 index 0000000000..1592ad9fd8 --- /dev/null +++ b/input/src/values/group_coordinate.rs @@ -0,0 +1,57 @@ +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::group_coordinate))] +pub enum GroupCoordinate<'ast> { + Number(NumberValue<'ast>), + SignHigh(SignHigh<'ast>), + SignLow(SignLow<'ast>), + Inferred(Inferred<'ast>), +} + +impl<'ast> GroupCoordinate<'ast> { + pub fn span(&self) -> &Span<'ast> { + match self { + GroupCoordinate::Number(number) => &number.span(), + GroupCoordinate::SignHigh(sign_high) => &sign_high.span, + GroupCoordinate::SignLow(sign_low) => &sign_low.span, + GroupCoordinate::Inferred(inferred) => &inferred.span, + } + } +} + +impl<'ast> fmt::Display for GroupCoordinate<'ast> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + GroupCoordinate::Number(number) => write!(f, "{}", number), + GroupCoordinate::SignHigh(_) => write!(f, "+"), + GroupCoordinate::SignLow(_) => write!(f, "-"), + GroupCoordinate::Inferred(_) => write!(f, "_"), + } + } +} + +#[derive(Clone, Debug, FromPest, PartialEq, Eq)] +#[pest_ast(rule(Rule::sign_high))] +pub struct SignHigh<'ast> { + #[pest_ast(outer())] + pub span: Span<'ast>, +} + +#[derive(Clone, Debug, FromPest, PartialEq, Eq)] +#[pest_ast(rule(Rule::sign_low))] +pub struct SignLow<'ast> { + #[pest_ast(outer())] + pub span: Span<'ast>, +} + +#[derive(Clone, Debug, FromPest, PartialEq, Eq)] +#[pest_ast(rule(Rule::inferred))] +pub struct Inferred<'ast> { + #[pest_ast(outer())] + pub span: Span<'ast>, +} diff --git a/input/src/values/group_value.rs b/input/src/values/group_value.rs index c31e070b49..c37de3ffd5 100644 --- a/input/src/values/group_value.rs +++ b/input/src/values/group_value.rs @@ -1,4 +1,4 @@ -use crate::{ast::Rule, types::GroupType, values::NumberValue}; +use crate::{ast::Rule, types::GroupType, values::GroupCoordinate}; use pest::Span; use pest_ast::FromPest; @@ -7,7 +7,7 @@ use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq, Eq)] #[pest_ast(rule(Rule::value_group))] pub struct GroupValue<'ast> { - pub value: GroupRepresentation<'ast>, + pub value: GroupTuple<'ast>, pub type_: GroupType, #[pest_ast(outer())] pub span: Span<'ast>, @@ -19,27 +19,11 @@ impl<'ast> fmt::Display for GroupValue<'ast> { } } -#[derive(Clone, Debug, FromPest, PartialEq, Eq)] -#[pest_ast(rule(Rule::group_single_or_tuple))] -pub enum GroupRepresentation<'ast> { - Single(NumberValue<'ast>), - Tuple(GroupTuple<'ast>), -} - -impl<'ast> fmt::Display for GroupRepresentation<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - GroupRepresentation::Single(number) => write!(f, "{}", number), - GroupRepresentation::Tuple(tuple) => write!(f, "{}", tuple), - } - } -} - #[derive(Clone, Debug, FromPest, PartialEq, Eq)] #[pest_ast(rule(Rule::group_tuple))] pub struct GroupTuple<'ast> { - pub x: NumberValue<'ast>, - pub y: NumberValue<'ast>, + pub x: GroupCoordinate<'ast>, + pub y: GroupCoordinate<'ast>, #[pest_ast(outer())] pub span: Span<'ast>, } diff --git a/input/src/values/mod.rs b/input/src/values/mod.rs index 089a9420ed..f8ded05f4e 100644 --- a/input/src/values/mod.rs +++ b/input/src/values/mod.rs @@ -13,6 +13,9 @@ pub use boolean_value::*; pub mod field_value; pub use field_value::*; +pub mod group_coordinate; +pub use group_coordinate::*; + pub mod group_value; pub use group_value::*; diff --git a/typed/src/groups/group_coordinate.rs b/typed/src/groups/group_coordinate.rs index a2806d0af7..9475a7c4eb 100644 --- a/typed/src/groups/group_coordinate.rs +++ b/typed/src/groups/group_coordinate.rs @@ -6,6 +6,13 @@ use leo_ast::values::{ SignHigh as AstSignHigh, SignLow as AstSignLow, }; +use leo_input::values::{ + GroupCoordinate as InputGroupCoordinate, + Inferred as InputInferred, + NumberValue as InputNumberValue, + SignHigh as InputSignHigh, + SignLow as InputSignLow, +}; use serde::{Deserialize, Serialize}; use std::fmt; @@ -29,6 +36,17 @@ impl<'ast> From> for GroupCoordinate { } } +impl<'ast> From> for GroupCoordinate { + fn from(coordinate: InputGroupCoordinate<'ast>) -> Self { + match coordinate { + InputGroupCoordinate::Number(number) => GroupCoordinate::from(number), + InputGroupCoordinate::SignHigh(sign_high) => GroupCoordinate::from(sign_high), + InputGroupCoordinate::SignLow(sign_low) => GroupCoordinate::from(sign_low), + InputGroupCoordinate::Inferred(inferred) => GroupCoordinate::from(inferred), + } + } +} + impl fmt::Display for GroupCoordinate { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { @@ -66,3 +84,30 @@ impl<'ast> From> for GroupCoordinate { GroupCoordinate::Inferred } } + +impl<'ast> From> for GroupCoordinate { + fn from(number: InputNumberValue<'ast>) -> Self { + let value = number.to_string(); + let span = Span::from(number.span().clone()); + + GroupCoordinate::Number(value, span) + } +} + +impl<'ast> From> for GroupCoordinate { + fn from(_sign: InputSignHigh<'ast>) -> Self { + GroupCoordinate::SignHigh + } +} + +impl<'ast> From> for GroupCoordinate { + fn from(_sign: InputSignLow<'ast>) -> Self { + GroupCoordinate::SignLow + } +} + +impl<'ast> From> for GroupCoordinate { + fn from(_sign: InputInferred<'ast>) -> Self { + GroupCoordinate::Inferred + } +} diff --git a/typed/src/groups/group_value.rs b/typed/src/groups/group_value.rs index 7ff16ad89c..8c54ef4939 100644 --- a/typed/src/groups/group_value.rs +++ b/typed/src/groups/group_value.rs @@ -1,5 +1,6 @@ use crate::{common::span::Span, groups::GroupCoordinate}; use leo_ast::values::GroupValue as AstGroupValue; +use leo_input::values::GroupValue as InputGroupValue; use serde::{Deserialize, Serialize}; use std::fmt; @@ -24,6 +25,19 @@ impl<'ast> From> for GroupValue { } } +impl<'ast> From> for GroupValue { + fn from(ast_group: InputGroupValue<'ast>) -> Self { + let ast_x = ast_group.value.x; + let ast_y = ast_group.value.y; + + Self { + x: GroupCoordinate::from(ast_x), + y: GroupCoordinate::from(ast_y), + span: Span::from(ast_group.span), + } + } +} + impl fmt::Display for GroupValue { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "({}, {})", self.x, self.y) diff --git a/typed/src/input/input_value.rs b/typed/src/input/input_value.rs index a17f0fd5e0..4d064e728c 100644 --- a/typed/src/input/input_value.rs +++ b/typed/src/input/input_value.rs @@ -1,8 +1,9 @@ +use crate::GroupValue; use leo_input::{ errors::InputParserError, expressions::{ArrayInitializerExpression, ArrayInlineExpression, Expression}, types::{ArrayType, DataType, IntegerType, Type}, - values::{BooleanValue, FieldValue, GroupValue, NumberValue, Value}, + values::{BooleanValue, FieldValue, GroupValue as InputGroupValue, NumberValue, Value}, }; use leo_input::{ @@ -17,7 +18,7 @@ pub enum InputValue { Address(String), Boolean(bool), Field(String), - Group(String), + Group(GroupValue), Integer(IntegerType, String), Array(Vec), Tuple(Vec), @@ -44,8 +45,8 @@ impl InputValue { Ok(InputValue::Integer(integer_type, number)) } - fn from_group(group: GroupValue) -> Self { - InputValue::Group(group.to_string()) + fn from_group(group: InputGroupValue) -> Self { + InputValue::Group(GroupValue::from(group)) } fn from_field(field: FieldValue) -> Self { @@ -57,7 +58,7 @@ impl InputValue { DataType::Address(_) => Err(InputParserError::implicit_type(data_type, implicit)), DataType::Boolean(_) => Err(InputParserError::implicit_type(data_type, implicit)), DataType::Integer(integer_type) => InputValue::from_number(integer_type, implicit.to_string()), - DataType::Group(_) => Ok(InputValue::Group(implicit.to_string())), + DataType::Group(_) => Err(InputParserError::implicit_group(implicit)), DataType::Field(_) => Ok(InputValue::Field(implicit.to_string())), } }