From 09e9aeb537be964d245c4cdbc4cf94b530739759 Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu Date: Wed, 24 Aug 2022 15:05:16 -0700 Subject: [PATCH] Add support for mappings in the parsers --- compiler/parser/src/parser/file.rs | 27 ++++++++++++++++++- compiler/parser/src/tokenizer/lexer.rs | 2 +- compiler/parser/src/tokenizer/mod.rs | 3 ++- compiler/parser/src/tokenizer/token.rs | 6 +++++ .../rename_program.rs | 1 + compiler/span/src/symbol.rs | 1 + 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/compiler/parser/src/parser/file.rs b/compiler/parser/src/parser/file.rs index ce60d4fd1f..ef601abc29 100644 --- a/compiler/parser/src/parser/file.rs +++ b/compiler/parser/src/parser/file.rs @@ -29,6 +29,7 @@ impl ParserContext<'_> { let mut imports = IndexMap::new(); let mut functions = IndexMap::new(); let mut circuits = IndexMap::new(); + let mut mappings = IndexMap::new(); // TODO: Condense logic while self.has_next() { @@ -41,6 +42,10 @@ impl ParserContext<'_> { let (id, circuit) = self.parse_circuit()?; circuits.insert(id, circuit); } + Token::Mapping => { + let (id, mapping) = self.parse_mapping()?; + mappings.insert(id, mapping); + } Token::At => { let (id, function) = self.parse_function()?; functions.insert(id, function); @@ -64,6 +69,7 @@ impl ParserContext<'_> { imports, functions, circuits, + mappings, }) } @@ -213,7 +219,7 @@ impl ParserContext<'_> { } } - /// Parses a circuit or record definition, e.g., `circit Foo { ... }` or `record Foo { ... }`. + /// Parses a circuit or record definition, e.g., `circuit Foo { ... }` or `record Foo { ... }`. pub(super) fn parse_circuit(&mut self) -> Result<(Identifier, Circuit)> { let is_record = matches!(&self.token.token, Token::Record); let start = self.expect_any(&[Token::Circuit, Token::Record])?; @@ -233,6 +239,25 @@ impl ParserContext<'_> { )) } + /// Parses a mapping declaration, e.g. `mapping balances: address => u128`. + pub(super) fn parse_mapping(&mut self) -> Result<(Identifier, Mapping)> { + let start = self.expect(&Token::Mapping)?; + let identifier = self.expect_identifier()?; + self.expect(&Token::Colon)?; + let (key_type, _) = self.parse_type()?; + self.expect(&Token::Arrow)?; + let (value_type, value_span) = self.parse_type()?; + Ok(( + identifier, + Mapping { + identifier, + key_type, + value_type, + span: start + value_span, + }, + )) + } + /// Returns a [`ParamMode`] AST node if the next tokens represent a function parameter mode. pub(super) fn parse_function_parameter_mode(&mut self) -> Result { // TODO: Allow explicit "private" mode. diff --git a/compiler/parser/src/tokenizer/lexer.rs b/compiler/parser/src/tokenizer/lexer.rs index 663bdbb7f4..cf119f51d3 100644 --- a/compiler/parser/src/tokenizer/lexer.rs +++ b/compiler/parser/src/tokenizer/lexer.rs @@ -366,7 +366,7 @@ impl Token { Token::ShrAssign, ) } - '=' => return match_two(&mut input, Token::Assign, '=', Token::Eq), + '=' => return match_three(&mut input, Token::Assign, '=', Token::Eq, '>', Token::BigArrow), '[' => return match_one(&mut input, Token::LeftSquare), ']' => return match_one(&mut input, Token::RightSquare), '{' => return match_one(&mut input, Token::LeftCurly), diff --git a/compiler/parser/src/tokenizer/mod.rs b/compiler/parser/src/tokenizer/mod.rs index e39381fd52..8d66d4d08e 100644 --- a/compiler/parser/src/tokenizer/mod.rs +++ b/compiler/parser/src/tokenizer/mod.rs @@ -123,6 +123,7 @@ mod tests { , - -> + => _ . .. @@ -154,7 +155,7 @@ mod tests { assert_eq!( output, - r#""test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" 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 + r#""test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" 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 46e46d3edc..c688abed67 100644 --- a/compiler/parser/src/tokenizer/token.rs +++ b/compiler/parser/src/tokenizer/token.rs @@ -77,6 +77,7 @@ pub enum Token { DoubleColon, Question, Arrow, + BigArrow, Shl, ShlAssign, Shr, @@ -120,6 +121,7 @@ pub enum Token { Import, In, Let, + Mapping, // For public inputs. Public, Return, @@ -156,6 +158,7 @@ pub const KEYWORD_TOKENS: &[Token] = &[ Token::Import, Token::In, Token::Let, + Token::Mapping, Token::Public, Token::Record, Token::Return, @@ -201,6 +204,7 @@ impl Token { Token::In => sym::In, Token::Import => sym::import, Token::Let => sym::Let, + Token::Mapping => sym::mapping, Token::Public => sym::Public, Token::Record => sym::record, Token::Return => sym::Return, @@ -275,6 +279,7 @@ impl fmt::Display for Token { DoubleColon => write!(f, "::"), Question => write!(f, "?"), Arrow => write!(f, "->"), + BigArrow => write!(f, "=>"), Shl => write!(f, "<<"), ShlAssign => write!(f, "<<="), Shr => write!(f, ">>"), @@ -313,6 +318,7 @@ impl fmt::Display for Token { Import => write!(f, "import"), In => write!(f, "in"), Let => write!(f, "let"), + Mapping => write!(f, "mapping"), SelfLower => write!(f, "self"), Public => write!(f, "public"), Return => write!(f, "return"), diff --git a/compiler/passes/src/static_single_assignment/rename_program.rs b/compiler/passes/src/static_single_assignment/rename_program.rs index e244ecc52f..32d831cfd1 100644 --- a/compiler/passes/src/static_single_assignment/rename_program.rs +++ b/compiler/passes/src/static_single_assignment/rename_program.rs @@ -167,6 +167,7 @@ impl ProgramConsumer for StaticSingleAssigner<'_> { .map(|(i, f)| (i, self.consume_function(f))) .collect(), circuits: input.circuits, + mappings: input.mappings, } } } diff --git a/compiler/span/src/symbol.rs b/compiler/span/src/symbol.rs index ae6b30f295..ff6340158f 100644 --- a/compiler/span/src/symbol.rs +++ b/compiler/span/src/symbol.rs @@ -198,6 +198,7 @@ symbols! { assert_eq, assert_neq, main, + mapping, Mut: "mut", prelude, Public,