Add support for mappings in the parsers

This commit is contained in:
Pranav Gaddamadugu 2022-08-24 15:05:16 -07:00
parent dffb788913
commit 09e9aeb537
6 changed files with 37 additions and 3 deletions

View File

@ -29,6 +29,7 @@ impl ParserContext<'_> {
let mut imports = IndexMap::new(); let mut imports = IndexMap::new();
let mut functions = IndexMap::new(); let mut functions = IndexMap::new();
let mut circuits = IndexMap::new(); let mut circuits = IndexMap::new();
let mut mappings = IndexMap::new();
// TODO: Condense logic // TODO: Condense logic
while self.has_next() { while self.has_next() {
@ -41,6 +42,10 @@ impl ParserContext<'_> {
let (id, circuit) = self.parse_circuit()?; let (id, circuit) = self.parse_circuit()?;
circuits.insert(id, circuit); circuits.insert(id, circuit);
} }
Token::Mapping => {
let (id, mapping) = self.parse_mapping()?;
mappings.insert(id, mapping);
}
Token::At => { Token::At => {
let (id, function) = self.parse_function()?; let (id, function) = self.parse_function()?;
functions.insert(id, function); functions.insert(id, function);
@ -64,6 +69,7 @@ impl ParserContext<'_> {
imports, imports,
functions, functions,
circuits, 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)> { pub(super) fn parse_circuit(&mut self) -> Result<(Identifier, Circuit)> {
let is_record = matches!(&self.token.token, Token::Record); let is_record = matches!(&self.token.token, Token::Record);
let start = self.expect_any(&[Token::Circuit, 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. /// Returns a [`ParamMode`] AST node if the next tokens represent a function parameter mode.
pub(super) fn parse_function_parameter_mode(&mut self) -> Result<ParamMode> { pub(super) fn parse_function_parameter_mode(&mut self) -> Result<ParamMode> {
// TODO: Allow explicit "private" mode. // TODO: Allow explicit "private" mode.

View File

@ -366,7 +366,7 @@ impl Token {
Token::ShrAssign, 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::LeftSquare),
']' => return match_one(&mut input, Token::RightSquare), ']' => return match_one(&mut input, Token::RightSquare),
'{' => return match_one(&mut input, Token::LeftCurly), '{' => return match_one(&mut input, Token::LeftCurly),

View File

@ -123,6 +123,7 @@ mod tests {
, ,
- -
-> ->
=>
_ _
. .
.. ..
@ -154,7 +155,7 @@ mod tests {
assert_eq!( assert_eq!(
output, 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 */ // "# /* test */ // "#
); );
}); });

View File

@ -77,6 +77,7 @@ pub enum Token {
DoubleColon, DoubleColon,
Question, Question,
Arrow, Arrow,
BigArrow,
Shl, Shl,
ShlAssign, ShlAssign,
Shr, Shr,
@ -120,6 +121,7 @@ pub enum Token {
Import, Import,
In, In,
Let, Let,
Mapping,
// For public inputs. // For public inputs.
Public, Public,
Return, Return,
@ -156,6 +158,7 @@ pub const KEYWORD_TOKENS: &[Token] = &[
Token::Import, Token::Import,
Token::In, Token::In,
Token::Let, Token::Let,
Token::Mapping,
Token::Public, Token::Public,
Token::Record, Token::Record,
Token::Return, Token::Return,
@ -201,6 +204,7 @@ impl Token {
Token::In => sym::In, Token::In => sym::In,
Token::Import => sym::import, Token::Import => sym::import,
Token::Let => sym::Let, Token::Let => sym::Let,
Token::Mapping => sym::mapping,
Token::Public => sym::Public, Token::Public => sym::Public,
Token::Record => sym::record, Token::Record => sym::record,
Token::Return => sym::Return, Token::Return => sym::Return,
@ -275,6 +279,7 @@ impl fmt::Display for Token {
DoubleColon => write!(f, "::"), DoubleColon => write!(f, "::"),
Question => write!(f, "?"), Question => write!(f, "?"),
Arrow => write!(f, "->"), Arrow => write!(f, "->"),
BigArrow => write!(f, "=>"),
Shl => write!(f, "<<"), Shl => write!(f, "<<"),
ShlAssign => write!(f, "<<="), ShlAssign => write!(f, "<<="),
Shr => write!(f, ">>"), Shr => write!(f, ">>"),
@ -313,6 +318,7 @@ impl fmt::Display for Token {
Import => write!(f, "import"), Import => write!(f, "import"),
In => write!(f, "in"), In => write!(f, "in"),
Let => write!(f, "let"), Let => write!(f, "let"),
Mapping => write!(f, "mapping"),
SelfLower => write!(f, "self"), SelfLower => write!(f, "self"),
Public => write!(f, "public"), Public => write!(f, "public"),
Return => write!(f, "return"), Return => write!(f, "return"),

View File

@ -167,6 +167,7 @@ impl ProgramConsumer for StaticSingleAssigner<'_> {
.map(|(i, f)| (i, self.consume_function(f))) .map(|(i, f)| (i, self.consume_function(f)))
.collect(), .collect(),
circuits: input.circuits, circuits: input.circuits,
mappings: input.mappings,
} }
} }
} }

View File

@ -198,6 +198,7 @@ symbols! {
assert_eq, assert_eq,
assert_neq, assert_neq,
main, main,
mapping,
Mut: "mut", Mut: "mut",
prelude, prelude,
Public, Public,