Replies: 1 comment 1 reply
-
Ended up figuring out how to dump the SyntaxTree using pyslang, code snippet below. Note that I'm excluding a lot of information (though it's still really verbose), so this is not a reversable serialization, but it's good enough for my purposes probably. import pyslang
import json
def serialize(node):
if node.kind == pyslang.SyntaxKind.SyntaxList:
serialized_node = [serialize(member) for member in node]
else:
serialized_node = {}
for attr_name in dir(node):
attr = getattr(node, attr_name)
if attr_name != 'parent' and not callable(attr) and not attr_name.startswith('__'):
if isinstance(attr, pyslang.SyntaxNode):
serialized_node[attr_name] = serialize(attr)
elif isinstance(attr, pyslang.Token):
serialized_node[attr_name] = {'kind': str(attr.kind), 'text': attr.rawText}
elif isinstance(attr, pyslang.SourceRange):
serialized_node[attr_name] = f'({attr.start.buffer.id}, {attr.start.offset}), ({attr.end.buffer.id}, {attr.end.offset})'
else:
serialized_node[attr_name] = str(attr)
return serialized_node
tree = pyslang.SyntaxTree.fromFile('example.sv')
with open('example_syntax.json', 'w') as f:
json.dump(serialize(tree.root), f, indent=2) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I'm building a tool which applies a program transformation and I'm planning on using the SyntaxRewriter tool to do so. To that end, I'm trying to get a better understanding of the SyntaxTree structure by going through the SyntaxTree output of an example SV file. However, I'm having trouble figuring out how to do this.
I would Ideally like to be able to dump the SyntaxTree into a json similar to the AST, such that I get the kind of each node and a map from the attribute names to each child node. Is there some existing mechanism for doing this? Otherwise how would I write a program for doing this? I tried doing this with pyslang but wasn't able to get it working well. (note I'm a complete beginner at C++)
Beta Was this translation helpful? Give feedback.
All reactions