by densh · 9y ago · 0 comments
[1] https://github.com/dotnet/roslyn
[2] https://ericlippert.com/2012/06/08/red-green-trees/
[3] https://github.com/dotnet/roslyn/wiki/Roslyn-Overview#syntax...
I love reading tidbits like that from other teams' work.
Cool! Yes this is what I was getting at. I mentioned Clang and Microsoft's IDEs in the "related work" section. I haven't seen them describe their data structures very much.
Clang has some documentation, but it's not very clear. For example, this doc has a FIXME(describe design) on it.
http://clang.llvm.org/docs/LibFormat.html
I also think the Clang AST is absolutely enormous and thus hard to document.
e.g. For code snippet "class C(x: Int)" Scala compiler create a tree that roughly resembles the following code:
class C extends scala.AnyRef {
<paramaccessor> private[this] val x: Int = _;
def <init>(x: Int) = {
super.<init>();
()
}
}
This tree looks even more terryfing under the hood in terms of AST constructors. Instead of making people figure out how that works we support nice high level sugar instead: q"class C(x: Int)"
Where q is a magic string interpolator that is compiled to generate an equivalent AST. It can be used for both construction of new AST nodes based on the older ones (we use $name syntax to substitute thing in) and also deconstuction of existing ones into smaller pieces via pattern matching ($name extracts parts out.)[1] http://docs.scala-lang.org/overviews/quasiquotes/intro.html
http://docs.julialang.org/en/stable/manual/metaprogramming/
https://en.wikibooks.org/wiki/Introducing_Julia/Metaprogramm...
They use :(expr) or quote/end for quotation, and $var for interpolation. Elixir metaprogramming uses quote/end for quotation, and unquote for interpolation. (In fact the entire Elixir language appears to be done with AST metaprogramming, since it's on top of Erlang.)
I basically think of these schemes as "Lisp-like AST metaprogramming, but with Syntax". Thanks for the pointer on Scala.
I saw a video where people asked why Clang source tools generate textual changes rather than AST changes... and this is a good example. People for some reason think that ASTs are "cleaner" or more usable, but they can be a pain.
I've looked a tiny bit at clang-format, but it's large and uses the large Clang AST.
And dartfmt too:
http://journal.stuffwithstuff.com/2015/09/08/the-hardest-pro...
The difference between gofmt and dartfmt is that Go doesn't impose any line length. And the Dart async style leads to a lot of chaining, which means more line break possibilities to consider.
I want to write an auto-formatter and it will be somewhere in the middle. It will support a max line length, so it probably needs these optimization functions like TeX and dartfmt, but the language doesn't have this async chaining pattern.
And now I have a pointer to scalafmt. Any other pointers are welcome!
Have you considered implementing a parser generator instead or taking the higher-order route and constructing a parser from higher-order primitives? Both ways provide a lot of flexibility to produce different trees for different problems, save a bit of time and a lot of code. Languages are mostly sequences, alternations, repetitions and characters anyway.