add scalar type to parser

This commit is contained in:
collin 2022-05-10 15:20:04 -07:00
parent 521c04b4d6
commit 9fe4a7281b
6 changed files with 33 additions and 23 deletions

View File

@ -294,6 +294,7 @@ impl ParserContext<'_> {
assert_no_whitespace("group")?; assert_no_whitespace("group")?;
Expression::Value(ValueExpression::Group(Box::new(GroupValue::Single(value, full_span)))) 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`. // Literal followed by other type suffix, e.g., `42u8`.
Some(suffix) => { Some(suffix) => {
assert_no_whitespace(&suffix.to_string())?; assert_no_whitespace(&suffix.to_string())?;

View File

@ -18,6 +18,10 @@ use super::*;
use leo_errors::Result; use leo_errors::Result;
pub(crate) const TYPE_TOKENS: &[Token] = &[ pub(crate) const TYPE_TOKENS: &[Token] = &[
Token::Address,
Token::Bool,
Token::Field,
Token::Group,
Token::I8, Token::I8,
Token::I16, Token::I16,
Token::I32, Token::I32,
@ -28,11 +32,8 @@ pub(crate) const TYPE_TOKENS: &[Token] = &[
Token::U32, Token::U32,
Token::U64, Token::U64,
Token::U128, Token::U128,
Token::Field,
Token::Group,
Token::Address,
Token::Bool,
Token::Char, Token::Char,
Token::Scalar,
]; ];
impl ParserContext<'_> { impl ParserContext<'_> {
@ -59,11 +60,12 @@ impl ParserContext<'_> {
let span = self.expect_any(TYPE_TOKENS)?; let span = self.expect_any(TYPE_TOKENS)?;
Ok(( Ok((
match &self.prev_token.token { match &self.prev_token.token {
Token::Field => Type::Field,
Token::Group => Type::Group,
Token::Address => Type::Address, Token::Address => Type::Address,
Token::Bool => Type::Boolean, Token::Bool => Type::Boolean,
Token::Field => Type::Field,
Token::Group => Type::Group,
Token::Char => Type::Char, Token::Char => Type::Char,
// todo @collin parse scalar type here.
x => Type::IntegerType(Self::token_to_int_type(x).expect("invalid int type")), x => Type::IntegerType(Self::token_to_int_type(x).expect("invalid int type")),
}, },
span, span,

View File

@ -426,6 +426,7 @@ impl Token {
"mut" => Token::Mut, "mut" => Token::Mut,
"public" => Token::Public, "public" => Token::Public,
"return" => Token::Return, "return" => Token::Return,
"scalar" => Token::Scalar,
"true" => Token::True, "true" => Token::True,
"type" => Token::Type, "type" => Token::Type,
"u8" => Token::U8, "u8" => Token::U8,

View File

@ -154,6 +154,7 @@ mod tests {
let let
mut mut
return return
scalar
string string
test test
true true
@ -204,7 +205,7 @@ mod tests {
assert_eq!( assert_eq!(
output, 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 */ // "# /* test */ // "#
); );
}); });

View File

@ -91,25 +91,26 @@ pub enum Token {
Arrow, Arrow,
Underscore, Underscore,
// Syntactic Grammr // Syntactic Grammar
// Types // Types
U8,
U16,
U32,
U64,
U128,
I8, I8,
I16, I16,
I32, I32,
I64, I64,
I128, I128,
Address,
Bool,
Field, Field,
Group, Group,
Bool, U8,
Address, U16,
U32,
U64,
U128,
Char, Char,
Scalar,
// primary expresion // primary expression
Input, Input,
// Regular Keywords // Regular Keywords
@ -159,6 +160,7 @@ pub const KEYWORD_TOKENS: &[Token] = &[
Token::Mut, Token::Mut,
Token::Public, Token::Public,
Token::Return, Token::Return,
Token::Scalar,
Token::True, Token::True,
Token::Type, Token::Type,
Token::U8, Token::U8,
@ -201,6 +203,7 @@ impl Token {
Token::Mut => sym::Mut, Token::Mut => sym::Mut,
Token::Public => sym::Public, Token::Public => sym::Public,
Token::Return => sym::Return, Token::Return => sym::Return,
Token::Scalar => sym::scalar,
Token::True => sym::True, Token::True => sym::True,
Token::Type => sym::Type, Token::Type => sym::Type,
Token::U8 => sym::u8, Token::U8 => sym::u8,
@ -264,21 +267,22 @@ impl fmt::Display for Token {
Arrow => write!(f, "->"), Arrow => write!(f, "->"),
Underscore => 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"), I8 => write!(f, "i8"),
I16 => write!(f, "i16"), I16 => write!(f, "i16"),
I32 => write!(f, "i32"), I32 => write!(f, "i32"),
I64 => write!(f, "i64"), I64 => write!(f, "i64"),
I128 => write!(f, "i128"), I128 => write!(f, "i128"),
Address => write!(f, "address"),
Bool => write!(f, "bool"),
Field => write!(f, "field"), Field => write!(f, "field"),
Group => write!(f, "group"), Group => write!(f, "group"),
Bool => write!(f, "bool"), U8 => write!(f, "u8"),
Address => write!(f, "address"), U16 => write!(f, "u16"),
U32 => write!(f, "u32"),
U64 => write!(f, "u64"),
U128 => write!(f, "u128"),
Char => write!(f, "char"), Char => write!(f, "char"),
Scalar => write!(f, "scalar"),
Input => write!(f, "input"), Input => write!(f, "input"),

View File

@ -134,6 +134,7 @@ symbols! {
prelude, prelude,
Public, Public,
Return: "return", Return: "return",
scalar,
Star: "*", Star: "*",
std, std,
Struct: "struct", Struct: "struct",