diff --git a/leo-inputs/input.leo b/leo-inputs/input.leo index af741c284d..fce77b60c5 100644 --- a/leo-inputs/input.leo +++ b/leo-inputs/input.leo @@ -1,4 +1,4 @@ [main] -a: private u32 = 5 -b: public fe = 1fe -c: private bool = true \ No newline at end of file +a: private u32 = 5; +b: public field = 1field; +c: private bool = true; \ No newline at end of file diff --git a/leo-inputs/src/inputs_ast.rs b/leo-inputs/src/inputs_ast.rs index 21e4690cb4..9738652480 100644 --- a/leo-inputs/src/inputs_ast.rs +++ b/leo-inputs/src/inputs_ast.rs @@ -34,39 +34,31 @@ pub enum Visibility { // Types -#[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::type_u32))] -pub struct U32Type<'ast> { - #[pest_ast(outer())] - pub span: Span<'ast>, -} - #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::type_field))] -pub struct FieldType<'ast> { - #[pest_ast(outer())] - pub span: Span<'ast>, +pub struct FieldType {} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_group))] +pub struct GroupType {} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_boolean))] +pub struct BooleanType {} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_data))] +pub enum DataType { + Integer(IntegerType), + Field(FieldType), + Group(GroupType), + Boolean(BooleanType), } #[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::type_bool))] -pub struct BooleanType<'ast> { - #[pest_ast(outer())] - pub span: Span<'ast>, -} - -#[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::type_basic))] -pub enum BasicType<'ast> { - U32(U32Type<'ast>), - Field(FieldType<'ast>), - Boolean(BooleanType<'ast>), -} - -#[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::type_struct))] -pub struct StructType<'ast> { - pub variable: Variable<'ast>, +#[pest_ast(rule(Rule::type_circuit))] +pub struct CircuitType<'ast> { + pub variable: Identifier<'ast>, #[pest_ast(outer())] pub span: Span<'ast>, } @@ -74,8 +66,8 @@ pub struct StructType<'ast> { #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::type_array))] pub struct ArrayType<'ast> { - pub _type: BasicType<'ast>, - pub count: Value<'ast>, + pub _type: DataType, + pub dimensions: Vec>, #[pest_ast(outer())] pub span: Span<'ast>, } @@ -83,62 +75,125 @@ pub struct ArrayType<'ast> { #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::_type))] pub enum Type<'ast> { - Basic(BasicType<'ast>), + Data(DataType), Array(ArrayType<'ast>), - Struct(StructType<'ast>), + Circuit(CircuitType<'ast>), } // Values #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::value_number))] -pub struct Number<'ast> { +pub struct NumberValue<'ast> { #[pest_ast(outer(with(span_into_string)))] pub value: String, #[pest_ast(outer())] pub span: Span<'ast>, } -#[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::value_u32))] -pub struct U32<'ast> { - pub number: Number<'ast>, - pub _type: Option>, - #[pest_ast(outer())] - pub span: Span<'ast>, -} - #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::value_field))] -pub struct Field<'ast> { - pub number: Number<'ast>, - pub _type: FieldType<'ast>, +pub struct FieldValue<'ast> { + pub number: NumberValue<'ast>, + pub _type: FieldType, + #[pest_ast(outer())] + pub span: Span<'ast>, +} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::value_group))] +pub struct GroupValue<'ast> { + pub value: GroupRepresentation<'ast>, + pub _type: GroupType, + #[pest_ast(outer())] + pub span: Span<'ast>, +} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::group_single_or_tuple))] +pub enum GroupRepresentation<'ast> { + Single(NumberValue<'ast>), + Tuple(GroupTuple<'ast>), +} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::group_tuple))] +pub struct GroupTuple<'ast> { + pub x: NumberValue<'ast>, + pub y: NumberValue<'ast>, #[pest_ast(outer())] pub span: Span<'ast>, } #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::value_boolean))] -pub struct Boolean<'ast> { +pub struct BooleanValue<'ast> { #[pest_ast(outer(with(span_into_string)))] pub value: String, #[pest_ast(outer())] pub span: Span<'ast>, } +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_integer))] +pub enum IntegerType { + U8Type(U8Type), + U16Type(U16Type), + U32Type(U32Type), + U64Type(U64Type), + U128Type(U128Type), +} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_u8))] +pub struct U8Type {} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_u16))] +pub struct U16Type {} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_u32))] +pub struct U32Type {} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_u64))] +pub struct U64Type {} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::type_u128))] +pub struct U128Type {} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::value_integer))] +pub struct IntegerValue<'ast> { + pub number: NumberValue<'ast>, + pub _type: IntegerType, + #[pest_ast(outer())] + pub span: Span<'ast>, +} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::value_implicit))] +pub struct NumberImplicitValue<'ast> { + pub number: NumberValue<'ast>, + #[pest_ast(outer())] + pub span: Span<'ast>, +} #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::value))] pub enum Value<'ast> { - Field(Field<'ast>), - Boolean(Boolean<'ast>), - U32(U32<'ast>), + Integer(IntegerValue<'ast>), + Field(FieldValue<'ast>), + Group(GroupValue<'ast>), + Boolean(BooleanValue<'ast>), + Implicit(NumberImplicitValue<'ast>), } - -// Variables +// Identifier #[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::variable))] -pub struct Variable<'ast> { +#[pest_ast(rule(Rule::identifier))] +pub struct Identifier<'ast> { #[pest_ast(outer(with(span_into_string)))] pub value: String, #[pest_ast(outer())] @@ -164,22 +219,22 @@ pub struct ArrayInitializerExpression<'ast> { pub span: Span<'ast>, } -// Structs +// Circuits #[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::inline_struct_member))] -pub struct InlineStructMember<'ast> { - pub variable: Variable<'ast>, +#[pest_ast(rule(Rule::circuit_field))] +pub struct CircuitField<'ast> { + pub variable: Identifier<'ast>, pub expression: Expression<'ast>, #[pest_ast(outer())] pub span: Span<'ast>, } #[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::expression_inline_struct))] -pub struct StructInlineExpression<'ast> { - pub variable: Variable<'ast>, - pub members: Vec>, +#[pest_ast(rule(Rule::expression_circuit_inline))] +pub struct CircuitInlineExpression<'ast> { + pub variable: Identifier<'ast>, + pub members: Vec>, #[pest_ast(outer())] pub span: Span<'ast>, } @@ -189,53 +244,27 @@ pub struct StructInlineExpression<'ast> { #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::expression))] pub enum Expression<'ast> { - StructInline(StructInlineExpression<'ast>), + CircuitInline(CircuitInlineExpression<'ast>), ArrayInline(ArrayInlineExpression<'ast>), ArrayInitializer(ArrayInitializerExpression<'ast>), Value(Value<'ast>), - Variable(Variable<'ast>), + Variable(Identifier<'ast>), } -// Functions +// Parameters #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::parameter))] pub struct Parameter<'ast> { - pub variable: Variable<'ast>, + pub variable: Identifier<'ast>, pub visibility: Option, pub _type: Type<'ast>, #[pest_ast(outer())] pub span: Span<'ast>, } -#[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::function_name))] -pub struct FunctionName<'ast> { - #[pest_ast(outer(with(span_into_string)))] - pub value: String, - #[pest_ast(outer())] - pub span: Span<'ast>, -} - // Sections -#[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::header))] -pub struct Header<'ast> { - pub function_name: FunctionName<'ast>, - #[pest_ast(outer())] - pub span: Span<'ast>, -} - -#[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::assignment))] -pub struct Assignment<'ast> { - pub parameter: Parameter<'ast>, - pub expression: Expression<'ast>, - #[pest_ast(outer())] - pub span: Span<'ast>, -} - #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::section))] pub struct Section<'ast> { @@ -245,12 +274,34 @@ pub struct Section<'ast> { pub span: Span<'ast>, } +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::header))] +pub struct Header<'ast> { + pub name: Identifier<'ast>, + #[pest_ast(outer())] + pub span: Span<'ast>, +} + +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::assignment))] +pub struct Assignment<'ast> { + pub parameter: Parameter<'ast>, + pub expression: Expression<'ast>, + pub line_end: LineEnd, + #[pest_ast(outer())] + pub span: Span<'ast>, +} + // Utilities #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::EOI))] pub struct EOI; +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::LINE_END))] +pub struct LineEnd; + // File #[derive(Clone, Debug, FromPest, PartialEq)] diff --git a/leo-inputs/src/inputs_display.rs b/leo-inputs/src/inputs_display.rs deleted file mode 100644 index 25a12d1474..0000000000 --- a/leo-inputs/src/inputs_display.rs +++ /dev/null @@ -1,204 +0,0 @@ -//! Display implementations for a Leo inputs file - -use crate::inputs_ast::{ - ArrayInitializerExpression, ArrayInlineExpression, ArrayType, Assignment, BasicType, Boolean, - BooleanType, Expression, Field, FieldType, File, Number, Parameter, Private, Section, - StructInlineExpression, StructType, Type, U32Type, Value, Variable, Visibility, U32, -}; - -use std::fmt; - -// Visibility - -impl fmt::Display for Visibility { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Visibility::Private(_private) => write!(f, "private"), - Visibility::Public(_public) => write!(f, "public"), - } - } -} - -// Types - -impl<'ast> fmt::Display for BooleanType<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "bool") - } -} - -impl<'ast> fmt::Display for FieldType<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "fe") - } -} - -impl<'ast> fmt::Display for U32Type<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "u32") - } -} - -impl<'ast> fmt::Display for BasicType<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - BasicType::Boolean(bool) => write!(f, "{}", bool), - BasicType::Field(field) => write!(f, "{}", field), - BasicType::U32(u32) => write!(f, "{}", u32), - } - } -} - -impl<'ast> fmt::Display for ArrayType<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "[{}; {}]", self._type, self.count) - } -} - -impl<'ast> fmt::Display for StructType<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.variable) - } -} - -impl<'ast> fmt::Display for Type<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Type::Basic(ref _type) => write!(f, "{}", _type), - Type::Array(ref _type) => write!(f, "{}", _type), - Type::Struct(ref _type) => write!(f, "{}", _type), - } - } -} - -// Values - -impl<'ast> fmt::Display for Number<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.value) - } -} - -impl<'ast> fmt::Display for U32<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.number) - } -} - -impl<'ast> fmt::Display for Field<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.number) - } -} - -impl<'ast> fmt::Display for Boolean<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.value) - } -} - -impl<'ast> fmt::Display for Value<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Value::U32(ref value) => write!(f, "{}", value), - Value::Field(ref value) => write!(f, "{}", value), - Value::Boolean(ref value) => write!(f, "{}", value), - } - } -} -// Variables - -impl<'ast> fmt::Display for Variable<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.value) - } -} - -// Expressions - -impl<'ast> fmt::Display for StructInlineExpression<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} {{", self.variable)?; - for (i, member) in self.members.iter().enumerate() { - write!(f, "{}: {}", member.variable, member.expression)?; - if i < self.members.len() - 1 { - write!(f, ", ")?; - } - } - write!(f, "}}") - } -} - -impl<'ast> fmt::Display for ArrayInlineExpression<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "[")?; - for (i, expression) in self.expressions.iter().enumerate() { - write!(f, "{}", expression)?; - if i < self.expressions.len() - 1 { - write!(f, ", ")?; - } - } - write!(f, "]") - } -} - -impl<'ast> fmt::Display for ArrayInitializerExpression<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "[{} ; {}]", self.expression, self.count) - } -} - -impl<'ast> fmt::Display for Expression<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Expression::StructInline(_struct) => write!(f, "{}", _struct), - Expression::ArrayInline(array) => write!(f, "{}", array), - Expression::ArrayInitializer(array) => write!(f, "{}", array), - Expression::Value(value) => write!(f, "{}", value), - Expression::Variable(variable) => write!(f, "{}", variable), - } - } -} - -// Sections - -impl<'ast> fmt::Display for Parameter<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}: {} {}", - self.variable, - self.visibility - .as_ref() - .unwrap_or(&Visibility::Private(Private {})), // private by default - self._type - ) - } -} - -impl<'ast> fmt::Display for Assignment<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} = {}", self.parameter, self.expression) - } -} - -impl<'ast> fmt::Display for Section<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "[{}]\n", self.header.function_name.value)?; - for assignment in self.assignments.iter() { - write!(f, "\t{}\n", assignment)?; - } - write!(f, "") - } -} - -// File - -impl<'ast> fmt::Display for File<'ast> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for value in self.sections.iter() { - write!(f, "{}", value)?; - } - write!(f, "") - } -} diff --git a/leo-inputs/src/leo-inputs.pest b/leo-inputs/src/leo-inputs.pest index a31780fb36..90f76c96e8 100644 --- a/leo-inputs/src/leo-inputs.pest +++ b/leo-inputs/src/leo-inputs.pest @@ -1,73 +1,97 @@ -/// Visibility +/// Common +file = { SOI ~ NEWLINE* ~ section* ~ NEWLINE* ~ EOI } + +identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* } +protected_name = { visibility | "let" | "for"| "if" | "else" | "as" | "return" } + + +LINE_END = { ";" ~ NEWLINE* } + +visibility = { visibility_public | visibility_private } visibility_public = { "public" } visibility_private = { "private" } -visibility = { visibility_public | visibility_private } /// Types -type_u32 = {"u32"} -type_field = {"fe"} -type_bool = {"bool"} -type_basic = { type_u32 | type_field | type_bool } -type_struct = { variable } -type_array = {type_basic ~ ("[" ~ value ~ "]")+ } -_type = { type_array | type_basic | type_struct } +_type = { type_array | type_data | type_circuit } +type_integer = { + type_u8 + | type_u16 + | type_u32 + | type_u64 + | type_u128 +} +type_u8 = { "u8" } +type_u16 = { "u16" } +type_u32 = { "u32" } +type_u64 = { "u64" } +type_u128 = { "u128" } + +type_field = { "field" } + +type_group = { "group" } + +type_boolean = { "bool" } + +type_data = { type_field | type_group | type_boolean | type_integer } + +type_circuit = { identifier } + +type_array = { type_data ~ ("[" ~ value ~ "]")+ } /// Values +value = { value_field | value_group | value_boolean | value_integer | value_implicit } + value_number = @{ "0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* } -value_u32 = { value_number ~ type_u32? } -value_field = { value_number ~ type_field } + +value_implicit = { value_number } + +value_integer = { value_number ~ type_integer } + value_boolean = { "true" | "false" } -value = { value_field | value_boolean | value_u32 } -/// Variables +value_field = { value_number ~ type_field } -protected_name = { visibility | "return" } -variable = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* } - -/// Arrays - -inline_array_inner = _{(expression ~ ("," ~ NEWLINE* ~ expression)*)?} -expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"} -expression_array_initializer = { "[" ~ expression ~ ";" ~ value ~ "]" } - -/// Structs - -inline_struct_member = { variable ~ ":" ~ expression } -inline_struct_member_list = _{(inline_struct_member ~ ("," ~ NEWLINE* ~ inline_struct_member)*)? ~ ","? } -expression_inline_struct = { variable ~ "{" ~ NEWLINE* ~ inline_struct_member_list ~ NEWLINE* ~ "}" } +value_group = { group_single_or_tuple ~ type_group } +group_tuple = { "(" ~ NEWLINE* ~ value_number ~ "," ~ NEWLINE* ~ value_number ~ NEWLINE* ~")" } +group_single_or_tuple = { value_number | group_tuple } /// Expressions +expression_array_initializer = { "[" ~ expression ~ ";" ~ value ~ "]" } + +expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"} +inline_array_inner = _{ (expression ~ ("," ~ NEWLINE* ~ expression)*)? } + +expression_circuit_inline = { identifier ~ "{" ~ NEWLINE* ~ circuit_field_list ~ NEWLINE* ~ "}" } +circuit_field = { identifier ~ ":" ~ expression } +circuit_field_list = _{ (circuit_field ~ ("," ~ NEWLINE* ~ circuit_field)*)? ~ ","? } + expression = { - expression_inline_struct + expression_circuit_inline | expression_array_inline | expression_array_initializer | value - | variable + | identifier } -/// Functions +/// Parameters -parameter = { variable ~ ":" ~ visibility? ~ _type } - -function_name = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* } +parameter = { identifier ~ ":" ~ visibility? ~ _type } /// Section -header = { "[" ~ function_name ~ "]" } -assignment = { parameter ~ "=" ~ expression } -// assignment = { parameter ~ "=" } - section = { header ~ NEWLINE+ ~ (assignment ~ NEWLINE*)* } +header = { "[" ~ identifier ~ "]" } + +assignment = { parameter ~ "=" ~ NEWLINE* ~ expression ~ LINE_END } + /// Utilities COMMENT = _{ ("/*" ~ (!"*/" ~ ANY)* ~ "*/") | ("//" ~ (!NEWLINE ~ ANY)*) } WHITESPACE = _{ " " | "\t" ~ (NEWLINE)* } -/// Program File -file = { SOI ~ NEWLINE* ~ section* ~ NEWLINE* ~ EOI } diff --git a/leo-inputs/src/lib.rs b/leo-inputs/src/lib.rs index 48c17d24d3..0f23c65930 100644 --- a/leo-inputs/src/lib.rs +++ b/leo-inputs/src/lib.rs @@ -6,5 +6,3 @@ extern crate pest_ast; extern crate pest_derive; pub mod inputs_ast; - -pub(crate) mod inputs_display; diff --git a/leo-inputs/src/main.rs b/leo-inputs/src/main.rs index c697e9de61..7efee39083 100644 --- a/leo-inputs/src/main.rs +++ b/leo-inputs/src/main.rs @@ -13,5 +13,5 @@ fn main() { // Build the abstract syntax tree let syntax_tree = inputs_ast::File::from_pest(&mut file).expect("infallible"); - println!("tree: {}", syntax_tree); + println!("tree: {:?}", syntax_tree); }