Retrieve / check the direct ancestor of a binding
morgante opened this issue · comments
Morgante Pell commented
Ideally, there would be an easy way to directly check the parent of a node against a pattern. Within is not quite sufficient, since it traverses upwards forever.
Use cases
- Confirming the direct parent of a statement is the program itself here
Possible approach: built-in parent
function
New parent($node)
built in that retrieves the parent of a node.
Reusing within
We could allow within
to take a modifier, determining how far it would traverse upwards.
Ex. this would make sure a const is directly within a specific parent:
`const $x;` as $match where $match <: within(1) statement_block()
This could be used to implement a parent
function in user space:
function parent($pattern) {
$pattern <: within(1) $parent,
return $parent
}