▲ | aleph_minus_one 4 days ago | |||||||||||||||||||||||||
Some (sometimes) desirable source code formatting cannot be deduced from the abstract syntax tree alone: Consider the following (pseudo-)code example:
Should this code formatted this way? Or should it be formatted
to emphasize that three assignments are done?Or should this code be formatted
to bring make the "depth" of the structure variables more tabular so that you can immediately see by the tabular shape which "depth" a member variable has?We can go even further like
which emphasizes that the author considers it to be very important that the reader can easily grasp the magnitudes of the numbers involved (which is why in Excel or LibreOffice Calc, numbers are right-aligned by default). Or combining this with making the depth "tabular":
Each of these formattings emphasizes different aspects of the code that the author wants to emphasize. This information cannot be deduced from some abstract syntax tree alone. Rather, this needs additional information by the programmer in which sense the structure behind the code intended by the programmer is to be "interpreted". | ||||||||||||||||||||||||||
▲ | kennywinker 4 days ago | parent | next [-] | |||||||||||||||||||||||||
I see what you’re saying, but I also haven’t ever used anything but the first two formats, and my goal was always readability not emphasis. Storing the AST instead of the text is a lossy encoding, but would we lose something more valuable than what we gain? If your example is the best thing we’d lose - i’d say it’s still net a massive win. and there are ways to emphasize different parts, that would survive the roundtrip to AST. E.g. one way to emphasize depth:
or to emphasize the data:
Or heck you could allow style overides if you really wanted to preserve this kind of styling:
| ||||||||||||||||||||||||||
▲ | Cthulhu_ 4 days ago | parent | prev | next [-] | |||||||||||||||||||||||||
But "desirable code formatting" is subjective; some people prefer 2, 4 or 8 spaces, some prefer columnar layout like you demonstrated, etc. You can't deduce formatting from an AST alone as an AST is not source code and does not have formatting information. | ||||||||||||||||||||||||||
▲ | gentooflux 4 days ago | parent | prev [-] | |||||||||||||||||||||||||
The second two lines of your example smell like LoD violations. It's not a formatting problem, it's a structural problem. | ||||||||||||||||||||||||||
|