Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to AST traversal #2944

Merged
merged 20 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compiler-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,15 @@ impl TypedDefinition {
pub fn find_node(&self, byte_index: u32) -> Option<Located<'_>> {
match self {
Definition::Function(function) => {
// Search for the corresponding node inside the function
// only if the index falls within the function's full location.
if !function.full_location().contains(byte_index) {
return None;
}

katsmil marked this conversation as resolved.
Show resolved Hide resolved
if let Some(found) = function.body.iter().find_map(|s| s.find_node(byte_index)) {
return Some(found);
};
}

if let Some(found_arg) = function
.arguments
Expand Down
36 changes: 29 additions & 7 deletions compiler-core/src/ast/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ impl TypedExpr {
}
}

// This could be optimised in places to exit early if the first of a series
// of expressions is after the byte index.
pub fn find_node(&self, byte_index: u32) -> Option<Located<'_>> {
match self {
Self::Var { .. }
Expand All @@ -185,20 +183,44 @@ impl TypedExpr {
.find_map(|e| e.find_node(byte_index))
.or_else(|| finally.find_node(byte_index)),

// Exit the search and return None if during iteration a statement
// is found with a start index beyond the index under search.
Self::Block { statements, .. } => {
statements.iter().find_map(|e| e.find_node(byte_index))
for statement in statements {
if statement.location().start > byte_index {
break;
}

if let Some(located) = statement.find_node(byte_index) {
return Some(located);
}
}

None
}

// Exit the search and return the encompassing type (e.g., list or tuple)
// if during iteration, an element is encountered with a start index
// beyond the index under search.
Self::Tuple {
elems: expressions, ..
}
| Self::List {
elements: expressions,
..
} => expressions
.iter()
.find_map(|e| e.find_node(byte_index))
.or_else(|| self.self_if_contains_location(byte_index)),
} => {
for expression in expressions {
if expression.location().start > byte_index {
break;
}

if let Some(located) = expression.find_node(byte_index) {
return Some(located);
}
}

self.self_if_contains_location(byte_index)
}

Self::NegateBool { value, .. } | Self::NegateInt { value, .. } => value
.find_node(byte_index)
Expand Down
Loading