whitequark / ast

A library for working with Abstract Syntax Trees.
MIT License
195 stars 25 forks source link

How to get the parent of a node? #2

Closed lrengan closed 10 years ago

lrengan commented 10 years ago

Hi,

I am using the AST class together with the Parser gem. I am looking for a way to traverse up the AST from a given node, i.e., given a node, I want to access its parent. Any suggestions on how I can do this?

Thank you, LN

mbj commented 10 years ago

I keep references to parent while walking the ast. But AFAIK its generally an antipattern and I try to avoid it as far as possible.

On Tue, Oct 15, 2013 at 01:48:04PM -0700, Lakshminarayanan Renganarayana wrote:

Hi,

I am using the AST class together with the Parser gem. I am looking for a way to traverse up the AST from a given node, i.e., given a node, I want to access its parent. Any suggestions on how I can do this?

Thank you, LN


Reply to this email directly or view it on GitHub: https://github.com/whitequark/ast/issues/2

Markus Schirp

Phone: +49 201 / 360 379 14 Fax: +49 201 / 360 379 16 Web: schirp-dso.com Email: mbj@schirp-dso.com Twitter: twitter.com/_m_bj OS-Code: github.com/mbj

Altendorferstrasse 44 D-45127 Essen

whitequark commented 10 years ago

It is not possible. Generally, you would need to either keep a reference to the parent, as @mbj said, or, preferably, handle the parent itself. I may be able to help you if you post a more specific example in whitequark/parser#111.