rust-analyzer / rowan

Apache License 2.0
689 stars 57 forks source link

Add deserialization symmetrically to existing serialization #74

Open abdulla-tuychi opened 3 years ago

abdulla-tuychi commented 3 years ago

I found no deserialization in feature serde1. Could this be added, please?

CAD97 commented 3 years ago

Deserialization is hard. The tree is deduplicated, so deserialization either reduplicates the tree (the way it is in the serialized form) or needs to thread context through in a difficult manner.

Additionally, Rowan is a library for textual parse trees. You already have a nice serialized format -- the text you're parsing -- and a way to convert that format into the tree. The tree contains the full text that you're parsing, so the serialized tree is strictly larger and contains more redundant information than the text that produced it. Even with the additional redundant information, parsing the tree from a tree serialization is likely not measurably faster than a well-written parser for the original text.

The serde serialization support exists mostly as a convenient simple way to perform snapshot testing (or a similar functionality) on the parsed trees that's (potentially) more readable, diffable, and useful than the debug output.

That's not to say it couldn't be done, though! In the (still experimental) reimplementation of the green tree layer, sorbus, I've implemented proper deserialization for green trees. It's messy, but a fairly good example of how to write an efficient allocation-minimizing implementation of cached DeserializeSeed.

I'm in grad school now so have very little time to spend on open source work currently. But if I find time, I do want to work on getting sorbus integrated into rowan (as the green tree design is better and enables faster "syntax pointers"), at which point it will be possible to get deserialization by asking sorbus for it. (But likely not Rowan, as, again, there's little reason Rowan sees for deserialization of a tree.)