Haskell analyzer for AST

As an exercise to learn about Haskell (and torture), I am considering writing a custom Haskell code decoder.

It will support a configuration file written in JSON or YAML (or is something better?), Which sets such parameters as import sorting, sorting / grouping data and class statements, the number of lines between sections, etc.

I am looking for a parser for Haskell 98 that generates an abstract syntax tree (AST) and stores comments . Parsing the GHC with its language extensions will be a bonus.

In the absence of such a thing, I think I can write a recursive descent parser or one using Parsec or a parser generator. Perhaps my own growth will increase learning (and torture :-)).

Is there a complete Haskell-> AST analyzer available under one of the open source licenses? If I advance on this project, I will put it on github.

+6
source share
2 answers

The haskell-src-exts has a parser. Not only does the parser parse most GHC extensions; it also recognizes common extensions, such as XML syntax literals, etc. You must use the parseModuleWithComments function if you also want to access the comments.

Note, however, that comments are not saved in the actual syntax tree; they are saved as a separate comment list with location information. It would be rather trivial to include comments in the tree if you really need them there by merging the tree with the list using the linear merging algorithm (both sequences can be considered as β€œsorted”). Comments can be saved together with the corresponding AST nodes, because "annotated" ASTs can contain arbitrary metadata in each node (by default only SrcSpanInfo ). The reason why this was not done in the actual haskell-src-exts package is apparently because the AST parser was written before the comment parser.

+16
source

I wrote a super simple tool that automatically formats Haskell code. This is done through parsing and beautiful printing from haskell-src-exts . You can find it at https://github.com/djv/small/blob/master/tidy.hs . This could be the beginning for something more flexible and powerful.

+2
source

Source: https://habr.com/ru/post/925662/


All Articles