Skip to content

Instantly share code, notes, and snippets.

@novakboskov
Created October 5, 2017 13:21
Show Gist options
  • Save novakboskov/9c9a34b56d998ecd6646e4c317b48279 to your computer and use it in GitHub Desktop.
Save novakboskov/9c9a34b56d998ecd6646e4c317b48279 to your computer and use it in GitHub Desktop.
def get_last_cursor_recursive(c):
if not any(True for _ in c.get_children()):
return c
last = None
for last in c.get_children():
pass
return get_last_cursor_recursive(last)
def get_last_cursor(c):
"""Return last cursor beneath cursor c.
Practically, if c is root of an AST, right most leaf is returned.
For this snippet:
int main() {
int a = 0;
for (int i = 0; i < 3; i++) {
a += 1;
}
}
It returns the cursor that represents integer literal 1.
In this example:
int main() {
int a = 0;
for (int = 0; i < 3; i++) {
a += 1;
}
}
As there is an error in for statement it returns the last one correct.
In this case, that is cursors that represents 0 in line 2 of the snippet.
"""
# while there is any children
while any(True for _ in c.get_children()):
# rebind c to last child
for c in c.get_children():
pass
return c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment