haskell / happy

The Happy parser generator for Haskell
Other
288 stars 84 forks source link

Allow usage of qualified Prelude in templates #135

Closed Kellador closed 5 years ago

Kellador commented 5 years ago

Should resolve #131; allowing the usage of qualified Prelude in templates, as specified in the issue.

A set of all testcases modified to use NoImplicitPrelude and qualified Prelude is included in a seperate directory inside the tests directory.

simonmar commented 5 years ago

Thanks for doing this! Two suggestions:

Kellador commented 5 years ago

Thanks for the suggestions, injecting the import definitely sounds like a good idea, and I will look into how to best auto-generate the modified tests as well.

Kellador commented 5 years ago

import qualified Prelude as HappyPrelude is now injected and everything qualified with Prelude before is now qualified with HappyPrelude instead. This alone would however make all old grammars incompatible, since they would then have to import Prelude themselves without qualification, so I've added a new cli option: --noprelude https://github.com/Kellador/happy/blob/master/src/Main.lhs#L439-L440 Without this flag the default behaviour is injection of both: import qualified Prelude as HappyPrelude and import Prelude, whereas with the flag, the latter import is omitted; A side effect of this is that with the flag it is not necessary to declare {-# LANGUAGE NoImplicitPrelude #-}.

Both tests for HappyPrelude usage with the --noprelude flag and basic Prelude are now generated via the C preprocessor. For this all relevant tests are preprended with `#ifndef QUALIFIEDPRELUDE

define QUALIFIEDPRELUDE Prelude

endif`

and new targets were added to the tests Makefile (https://github.com/Kellador/happy/blob/master/tests/Makefile#L40-L126).

This in effect doubles up all tests, which might be why the Travis CI build is currently timing out.

simonmar commented 5 years ago

import qualified Prelude as HappyPrelude is now injected and everything qualified with Prelude before is now qualified with HappyPrelude instead. This alone would however make all old grammars incompatible, since they would then have to import Prelude themselves without qualification

I don't follow that - why wouldn't it work to just inject import qualified Preliude as HappyPrelude and then refer to all Prelude identifiers qualified by HappyPrelude.? We don't have to add {-# LANGUAGE NoImplicitPrelude #-} too, we can let the user choose whether to do that or not.

Kellador commented 5 years ago

I don't follow that - why wouldn't it work to just inject import qualified Preliude as HappyPrelude and then refer to all Prelude identifiers qualified by HappyPrelude.? We don't have to add {-# LANGUAGE NoImplicitPrelude #-} too, we can let the user choose whether to do that or not.

Because once Prelude is imported under a qualified name it is not implicitly imported anymore;

If the extra unqualified import Prelude is omitted all tests without the HappyPrelude qualifiers fail, even without {-# LANGUAGE NoImplicitPrelude #-}, which is not injected atm.

simonmar commented 5 years ago

Oh, I'd forgotten about that. How unfortunate.

Kellador commented 5 years ago

Giving Travis CI a little more time before timing out did the trick, so I pushed that along with the last few fixes to make it work for the older GHC versions.

Not sure about appveyor, as that looks to have been broken for a while now and fixing that would not be related to this pull request.

jvoigtlaender commented 5 years ago

Thanks @Kellador! The work done here was essential for getting https://github.com/simonmar/happy/pull/156 right.