mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-25 02:22:44 +03:00
impl new group notation for inputs ast + typed
This commit is contained in:
parent
37bedc8662
commit
172431c222
@ -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>,
|
||||
|
@ -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();
|
||||
|
@ -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} }
|
||||
|
57
input/src/values/group_coordinate.rs
Normal file
57
input/src/values/group_coordinate.rs
Normal file
@ -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>,
|
||||
}
|
@ -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>,
|
||||
}
|
||||
|
@ -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::*;
|
||||
|
||||
|
@ -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<AstGroupCoordinate<'ast>> for GroupCoordinate {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<InputGroupCoordinate<'ast>> 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<AstInferred<'ast>> for GroupCoordinate {
|
||||
GroupCoordinate::Inferred
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<InputNumberValue<'ast>> 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<InputSignHigh<'ast>> for GroupCoordinate {
|
||||
fn from(_sign: InputSignHigh<'ast>) -> Self {
|
||||
GroupCoordinate::SignHigh
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<InputSignLow<'ast>> for GroupCoordinate {
|
||||
fn from(_sign: InputSignLow<'ast>) -> Self {
|
||||
GroupCoordinate::SignLow
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<InputInferred<'ast>> for GroupCoordinate {
|
||||
fn from(_sign: InputInferred<'ast>) -> Self {
|
||||
GroupCoordinate::Inferred
|
||||
}
|
||||
}
|
||||
|
@ -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<AstGroupValue<'ast>> for GroupValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<InputGroupValue<'ast>> 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)
|
||||
|
@ -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<InputValue>),
|
||||
Tuple(Vec<InputValue>),
|
||||
@ -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())),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user