mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-25 19:22:01 +03:00
Add support for mappings in the parsers
This commit is contained in:
parent
dffb788913
commit
09e9aeb537
@ -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<ParamMode> {
|
||||
// TODO: Allow explicit "private" mode.
|
||||
|
@ -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),
|
||||
|
@ -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 */ // "#
|
||||
);
|
||||
});
|
||||
|
@ -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"),
|
||||
|
@ -167,6 +167,7 @@ impl ProgramConsumer for StaticSingleAssigner<'_> {
|
||||
.map(|(i, f)| (i, self.consume_function(f)))
|
||||
.collect(),
|
||||
circuits: input.circuits,
|
||||
mappings: input.mappings,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -198,6 +198,7 @@ symbols! {
|
||||
assert_eq,
|
||||
assert_neq,
|
||||
main,
|
||||
mapping,
|
||||
Mut: "mut",
|
||||
prelude,
|
||||
Public,
|
||||
|
Loading…
Reference in New Issue
Block a user