I’m working with Ripper sexp expressions, it looks like this:
[:program,
[[:class,
[:const_ref, [:@const, "A", [1, 6]]],
nil,
[:bodystmt,
[[:class,
[:const_ref, [:@const, "B", [1, 15]]],
nil,
[:bodystmt,
[[:def,
[:@ident, "test", [1, 22]],
[:params, nil, nil, nil, nil, nil],
[:bodystmt, [[:void_stmt]], nil, nil, nil]]],
nil,
nil,
nil]]],
nil,
nil,
nil]]]]
And i’m using array as path to get element in sexp expression. For example
path = [1,0,1,1] => sexp[1][0][1][1]
will get me
[:@const, "A", [1, 6]]
With this path I can get next, previous, parent elements and so on.
But I’m wondering is there a data structure which more suitable for this kind of tasks?
Besides the obvious trees, you may want to look at zippers:
http://en.wikipedia.org/wiki/Zipper_(data_structure)
Someone recently implemented form-zip for Clojure, if you want to look at an example:
https://github.com/GeorgeJahad/form-zip