Question about purescript-language-cst-parser traversals

I’m experimenting with the (fantastic!) purescript-language-cst-parser library but I’m struggling a little bit with its traversals.

I’m trying to extract all bound identifiers and their types from a module, but keeping the hierarchy information when the identifier is bound in a let...in, where, etc. clauses.

For instance (ignoring the fact I also need the types), traversing the module:

module MyModule where
import Prelude
f :: Int
f = a + b
 where
   a = 1
   b = 2
g :: String
g = ""

would give me a result similar to:

[ [ "f" ], [ "f", "a" ], [ "f", "b" ], [ "g" ] ]

or any other data structure, like a tree, that keeps this hierarchy.

I don’t have a whole lot of experience doing this kind of thing, but in the past I’ve solved this problem by writing a traversal function that allowed me to run separate functions for both before and after a certain node was visited (a mix of top-down and bottom-up?), and using a State monad to keep intermediate results. Probably a naive solution in the eyes of someone who’s experienced in writing parsers. :smile:

Any pointers on how I can accomplish this with language-cst-parser?

This is generally what the topDownWithContext variations take care of, where the new context is passed down to the next “level”, but you might run into issues with binding groups. There currently aren’t good traversals for dealing with scope (maybe write up a feature request ticket?), so you would likely have to piece together your own if the existing ones didn’t work for you.

1 Like