Add indexing to build.rs.

This commit is contained in:
Jesse Brault 2025-05-16 19:27:39 -05:00
parent 15abcc92d3
commit 17285e84eb

View File

@ -1173,26 +1173,45 @@ fn build_primary_expression(file_id: usize, primary_expression_pair: Pair<Rule>)
fn build_object_access( fn build_object_access(
file_id: usize, file_id: usize,
receiver: Expression, receiver: Expression,
navigations_pair: Pair<Rule>, object_access_pair: Pair<Rule>,
) -> ObjectAccess { ) -> ObjectAccess {
ObjectAccess { ObjectAccess {
receiver: Box::new(receiver), receiver: Box::new(receiver),
navigations: ObjectNavigations( navigations: ObjectNavigations(
navigations_pair object_access_pair
.into_inner() .into_inner()
.map(|identifier_pair| { .map(
ObjectNavigation::Identifier(Box::new(expect_and_use( |property_or_index_pair| match property_or_index_pair.as_rule() {
file_id, Rule::ObjectProperty => {
identifier_pair, build_object_property(file_id, property_or_index_pair)
Rule::Identifier, }
build_identifier, Rule::ObjectIndex => build_object_index(file_id, property_or_index_pair),
))) _ => unreachable!(),
}) },
)
.collect(), .collect(),
), ),
} }
} }
fn build_object_property(file_id: usize, inner_pair: Pair<Rule>) -> ObjectNavigation {
ObjectNavigation::Identifier(Box::new(expect_and_use(
file_id,
inner_pair.into_inner().next().unwrap(),
Rule::Identifier,
build_identifier,
)))
}
fn build_object_index(file_id: usize, inner_pair: Pair<Rule>) -> ObjectNavigation {
ObjectNavigation::Index(Box::new(expect_and_use(
file_id,
inner_pair.into_inner().next().unwrap(),
Rule::Expression,
build_expression,
)))
}
fn build_closure(file_id: usize, closure_pair: Pair<Rule>) -> Closure { fn build_closure(file_id: usize, closure_pair: Pair<Rule>) -> Closure {
todo!() todo!()
} }
@ -1242,3 +1261,37 @@ fn build_double_quote_string(file_id: usize, pair: Pair<Rule>) -> Literal {
fn build_backtick_string(file_id: usize, pair: Pair<Rule>) -> Literal { fn build_backtick_string(file_id: usize, pair: Pair<Rule>) -> Literal {
todo!() todo!()
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::DeimosParser;
use indoc::indoc;
fn assert_builds(src: &str) {
let parse_result = DeimosParser::parse(Rule::CompilationUnit, src);
if let Err(e) = parse_result {
panic!("Parsing failed.\n{}", e)
} else {
build_ast(0, parse_result.unwrap().next().unwrap());
}
}
#[test]
fn index_object() {
assert_builds(indoc! {"
fn main() {
let x = foo[a];
}
"})
}
#[test]
fn object_index_and_call() {
assert_builds(indoc! {"\
fn main() {
a[b]().c()
}
"})
}
}