From 9fe4a7281b3628c1015a01dc37c815111c4985ee Mon Sep 17 00:00:00 2001 From: collin <16715212+collinc97@users.noreply.github.com> Date: Tue, 10 May 2022 15:20:04 -0700 Subject: [PATCH] add scalar type to parser --- compiler/parser/src/parser/expression.rs | 1 + compiler/parser/src/parser/type_.rs | 14 +++++---- compiler/parser/src/tokenizer/lexer.rs | 1 + compiler/parser/src/tokenizer/mod.rs | 3 +- compiler/parser/src/tokenizer/token.rs | 36 +++++++++++++----------- leo/span/src/symbol.rs | 1 + 6 files changed, 33 insertions(+), 23 deletions(-) diff --git a/compiler/parser/src/parser/expression.rs b/compiler/parser/src/parser/expression.rs index aec8ac86cf..b75d5685c8 100644 --- a/compiler/parser/src/parser/expression.rs +++ b/compiler/parser/src/parser/expression.rs @@ -294,6 +294,7 @@ impl ParserContext<'_> { assert_no_whitespace("group")?; Expression::Value(ValueExpression::Group(Box::new(GroupValue::Single(value, full_span)))) } + // todo @collinc97 parse scalar type here. // Literal followed by other type suffix, e.g., `42u8`. Some(suffix) => { assert_no_whitespace(&suffix.to_string())?; diff --git a/compiler/parser/src/parser/type_.rs b/compiler/parser/src/parser/type_.rs index 4cc8df88a9..e2e2fb59c9 100644 --- a/compiler/parser/src/parser/type_.rs +++ b/compiler/parser/src/parser/type_.rs @@ -18,6 +18,10 @@ use super::*; use leo_errors::Result; pub(crate) const TYPE_TOKENS: &[Token] = &[ + Token::Address, + Token::Bool, + Token::Field, + Token::Group, Token::I8, Token::I16, Token::I32, @@ -28,11 +32,8 @@ pub(crate) const TYPE_TOKENS: &[Token] = &[ Token::U32, Token::U64, Token::U128, - Token::Field, - Token::Group, - Token::Address, - Token::Bool, Token::Char, + Token::Scalar, ]; impl ParserContext<'_> { @@ -59,11 +60,12 @@ impl ParserContext<'_> { let span = self.expect_any(TYPE_TOKENS)?; Ok(( match &self.prev_token.token { - Token::Field => Type::Field, - Token::Group => Type::Group, Token::Address => Type::Address, Token::Bool => Type::Boolean, + Token::Field => Type::Field, + Token::Group => Type::Group, Token::Char => Type::Char, + // todo @collin parse scalar type here. x => Type::IntegerType(Self::token_to_int_type(x).expect("invalid int type")), }, span, diff --git a/compiler/parser/src/tokenizer/lexer.rs b/compiler/parser/src/tokenizer/lexer.rs index a8d820f815..9ecf2ddb7a 100644 --- a/compiler/parser/src/tokenizer/lexer.rs +++ b/compiler/parser/src/tokenizer/lexer.rs @@ -426,6 +426,7 @@ impl Token { "mut" => Token::Mut, "public" => Token::Public, "return" => Token::Return, + "scalar" => Token::Scalar, "true" => Token::True, "type" => Token::Type, "u8" => Token::U8, diff --git a/compiler/parser/src/tokenizer/mod.rs b/compiler/parser/src/tokenizer/mod.rs index 4829268689..1e79048c1a 100644 --- a/compiler/parser/src/tokenizer/mod.rs +++ b/compiler/parser/src/tokenizer/mod.rs @@ -154,6 +154,7 @@ mod tests { let mut return + scalar string test true @@ -204,7 +205,7 @@ mod tests { assert_eq!( output, - r#"'a' '😭' "test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8 test_ident 12345 address bool const else false field for function group i128 i64 i32 i16 i8 if in input let mut return string test true u128 u64 u32 u16 u8 console ! != && ( ) * ** + , - -> _ . .. / : ; < <= = == > >= [ ] { { } } || ? // test + r#"'a' '😭' "test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8 test_ident 12345 address bool const else false field for function group i128 i64 i32 i16 i8 if in input let mut return scalar string test true u128 u64 u32 u16 u8 console ! != && ( ) * ** + , - -> _ . .. / : ; < <= = == > >= [ ] { { } } || ? // test /* test */ // "# ); }); diff --git a/compiler/parser/src/tokenizer/token.rs b/compiler/parser/src/tokenizer/token.rs index 5a0d58aff6..bf56b37995 100644 --- a/compiler/parser/src/tokenizer/token.rs +++ b/compiler/parser/src/tokenizer/token.rs @@ -91,25 +91,26 @@ pub enum Token { Arrow, Underscore, - // Syntactic Grammr + // Syntactic Grammar // Types - U8, - U16, - U32, - U64, - U128, I8, I16, I32, I64, I128, + Address, + Bool, Field, Group, - Bool, - Address, + U8, + U16, + U32, + U64, + U128, Char, + Scalar, - // primary expresion + // primary expression Input, // Regular Keywords @@ -159,6 +160,7 @@ pub const KEYWORD_TOKENS: &[Token] = &[ Token::Mut, Token::Public, Token::Return, + Token::Scalar, Token::True, Token::Type, Token::U8, @@ -201,6 +203,7 @@ impl Token { Token::Mut => sym::Mut, Token::Public => sym::Public, Token::Return => sym::Return, + Token::Scalar => sym::scalar, Token::True => sym::True, Token::Type => sym::Type, Token::U8 => sym::u8, @@ -264,21 +267,22 @@ impl fmt::Display for Token { Arrow => write!(f, "->"), Underscore => write!(f, "_"), - U8 => write!(f, "u8"), - U16 => write!(f, "u16"), - U32 => write!(f, "u32"), - U64 => write!(f, "u64"), - U128 => write!(f, "u128"), I8 => write!(f, "i8"), I16 => write!(f, "i16"), I32 => write!(f, "i32"), I64 => write!(f, "i64"), I128 => write!(f, "i128"), + Address => write!(f, "address"), + Bool => write!(f, "bool"), Field => write!(f, "field"), Group => write!(f, "group"), - Bool => write!(f, "bool"), - Address => write!(f, "address"), + U8 => write!(f, "u8"), + U16 => write!(f, "u16"), + U32 => write!(f, "u32"), + U64 => write!(f, "u64"), + U128 => write!(f, "u128"), Char => write!(f, "char"), + Scalar => write!(f, "scalar"), Input => write!(f, "input"), diff --git a/leo/span/src/symbol.rs b/leo/span/src/symbol.rs index c9a30ed80b..2d367869d7 100644 --- a/leo/span/src/symbol.rs +++ b/leo/span/src/symbol.rs @@ -134,6 +134,7 @@ symbols! { prelude, Public, Return: "return", + scalar, Star: "*", std, Struct: "struct",