diff --git a/rust_compiler/libs/compiler/Cargo.toml b/rust_compiler/libs/compiler/Cargo.toml index 721bfcf..9d44bb4 100644 --- a/rust_compiler/libs/compiler/Cargo.toml +++ b/rust_compiler/libs/compiler/Cargo.toml @@ -13,6 +13,6 @@ lsp-types = { workspace = true } rust_decimal = { workspace = true } [dev-dependencies] -anyhow = { version = "1.0" } +anyhow = { workspace = true } indoc = { version = "2.0" } pretty_assertions = "1" diff --git a/rust_compiler/libs/parser/src/lib.rs b/rust_compiler/libs/parser/src/lib.rs index 4cb4618..362722a 100644 --- a/rust_compiler/libs/parser/src/lib.rs +++ b/rust_compiler/libs/parser/src/lib.rs @@ -308,7 +308,7 @@ impl<'a> Parser<'a> { Ok(Some(lhs)) } - /// Handles dot notation chains: x.y.z() + /// Handles dot notation chains (x.y.z()) and array indexing (x[0]) fn parse_postfix( &mut self, mut lhs: Spanned>, @@ -411,6 +411,9 @@ impl<'a> Parser<'a> { }), }; } + } else if self_matches_peek!(self, TokenType::Symbol(Symbol::LBracket)) { + // consume the `[` token + self.assign_next()?; } else { break; } diff --git a/rust_compiler/libs/parser/src/tree_node.rs b/rust_compiler/libs/parser/src/tree_node.rs index 2f21aef..be13c8b 100644 --- a/rust_compiler/libs/parser/src/tree_node.rs +++ b/rust_compiler/libs/parser/src/tree_node.rs @@ -174,6 +174,18 @@ impl<'a> std::fmt::Display for MemberAccessExpression<'a> { } } +#[derive(Debug, PartialEq, Eq)] +pub struct MemberIndexingExpression<'a> { + pub object: LiteralOrVariable<'a>, + pub index_of: Box>>, +} + +impl<'a> std::fmt::Display for MemberIndexingExpression<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}[{}]", self.object, self.index_of) + } +} + #[derive(Debug, PartialEq, Eq)] pub struct MethodCallExpression<'a> { pub object: Box>>,