Open
Description
It is common when you are doing trees (ASTs being one of them) manipulations to want to query the relative position between two nodes. Some common examples:
- Are these two nodes the same?
- Is this node a parent of mine? ----> is this my direct parent scope
- Is this node an ancesotor of mine? ----> Am I in the scope defined by this cursor
- Do we both have the same parent? ---> Am I in the same body as another cursor
- Is this node to my left/right among the children of out shared direct parent? ---> is this statement below or after me in the body of our direct parent scope
Ideally, we can define the Python comparision operators on cursors and give them some clear semantics. Some things to be careful about:
- Does equality of two cursors mean equality of the location in the tree or the equality of the subtrees each cursor points to? I think it should be the former and some other function to compare the content:
cursor1 == cursor2
checks if two cursors point to the same location.cursor1.is_same_ast(cursor2)
checks if the two ASTs are the same of cursors that could be at different locations. - If
cursor1
andcursor2
aren't in the same body, shouldcursor1 < cursro2
return false or raise an error?
The equality confusion might also suggest that maybe we should decouple the cursors API from the AST/introspection API (like @jrk suggested a while ago): cursors can only be a statement, expressions, block, and gap cursors, with the ability to get an introspectable AST object from statement and expression cursors.