3
votes

I want to use haskell-src-exts to parse haskell source module. But it requires fixities of all the operators to be specified. So I need to have a list of all imported modules to extract fixities for all the operators in scope.

So, I need to parse module to get list of imported modules; and I need the list of imported modules to parse the module :(

The question: how to parse haskell source module without parsing it? Is there any other parser I can use? How hlint solves the issue?

haskell-src-exts provides function to extract top level pragmas. I need something similar, but for imported modules.

1
The haskell-src-exts parser returns a Module, whose single constructor appears to take a [ImportDecl] as one of its arguments; is this not exactly what you want? - Daniel Wagner
@DanielWagner Sorry, I was not clear. I want to avoid parsing it twice. Without fixities Module will contain wrong AST for module body. - Yuras
I don't think hlint resolves the operator fixities, it just looks at a single file. Also see community.haskell.org/~ndm/darcs/hlint/hlint.htm ("Why doesn't HLint know the fixity for my custom !@%$ operator?") - Niklas B.
@Yuras If this is something you care about, you may have to write your own code for it. But I bet the maintainer of haskell-src-exts would gladly accept patches to parse certain frequently-useful prefixes of a module. - Daniel Wagner
Another approach would be to parse without fixities parseFileWithMode defaultParseMode{ fixities =Nothing }, and then once you've looked at the imports use Language.Haskell.Exts.Fixity.applyFixities to make the AST follow the right fixities. Who knows if that plays nicely with fixity decls you can put in let. - aavogt

1 Answers

2
votes

Another approach would be to parse without fixities parseFileWithMode defaultParseMode{ fixities =Nothing }, and then once you've looked at the imports use Language.Haskell.Exts.Fixity.applyFixities to make the AST follow the right fixities. Who knows if that plays nicely with fixity decls you can put in let