-
I am interested in Ohm because of its ability to do incremental parsing. This is for a project where I want to provide real-time preview of a Markdown-like language. With incremental updates to the tree, I could incrementally update the DOM, rather than either replace all of it, or figure out what has changed by comparing the trees. I've tried to store weak references to the nodes in a WeakMap, but after incremental parsing it looks like the tree is entirely new, i.e. it's not reusing the nodes from the old tree. Is this by design (i.e. due to the parsing algorithm - I am not familiar with PEG grammars)? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
The documentation is somewhat vague about this1, but the arguments to semantic actions aren't actually the parse tree nodes themselves, but rather wrappers around the parse tree (i.e., CST) nodes. You can access the raw CST node via the While the wrappers will all be freshly created, the underlying CST nodes will be re-used where possible. So you should be able to use the raw CST nodes for the purposes you're describing here. An interesting thing about our use of wrappers is that it allows us to track dependencies within the semantic actions (i.e., we can see which operations/attributes of which nodes were used). So theoretically, it should be possible to implement incremental semantic actions as well. I'd love to implement that but haven't had the time to. Anyways, hope that helps! Footnotes
|
Beta Was this translation helpful? Give feedback.
-
Thanks, it works! Using the Incremental semantic actions would be nice indeed :-) |
Beta Was this translation helpful? Give feedback.
-
Personally, I would probably just use the WeakMap. It's unlikely to cause problems if you store properties on the node objects themselves, however, we do rely on being able to store arbitrary, name-mangled properties on nodes for the purposes of memoizing attributes. |
Beta Was this translation helpful? Give feedback.
The documentation is somewhat vague about this1, but the arguments to semantic actions aren't actually the parse tree nodes themselves, but rather wrappers around the parse tree (i.e., CST) nodes. You can access the raw CST node via the
_node
property of the wrapper.While the wrappers will all be freshly created, the underlying CST nodes will be re-used where possible. So you should be able to use the raw CST nodes for the purposes you're describing here.
An interesting thing about our use of wrappers is that it allows us to track dependencies within the semantic actions (i.e., we can see which operations/attributes of which nodes were used). So theoretically, it should be possible to im…