Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't parse an empty GADT with “where” #358

Open
neongreen opened this issue Apr 16, 2017 · 3 comments
Open

Can't parse an empty GADT with “where” #358

neongreen opened this issue Apr 16, 2017 · 3 comments
Labels

Comments

@neongreen
Copy link

This code is accepted by GHC but not by HSE:

{-# LANGUAGE GADTs #-}
data SomeGADT a where

Here's a test case:

> let gadts = defaultParseMode {extensions = [EnableExtension GADTs]}

> parseDeclWithMode gadts "data SomeGADT a where\n"
ParseFailed (SrcLoc "<unknown>.hs" 2 1) "Parse error: virtual }"

I'm using haskell-src-exts from master (commit 1a3a7b2).

@mpickering
Copy link
Collaborator

I spent a long time debugging this today so will write down my progress.

After lexing the where token, the next production that we want to match is the open production which pushes a layout context. However, we want this layout context to be pushed before we lex the newline character as otherwise we push a layout context on the next line which causes the lexer to get very confused.

Happily, I could simply refactor the grammar so that there was a separate rule for the the empty case which didn't have to deal with layout. Unhappily, this broke the special case where the deriving clause as at the same indent as the GADT decls. This causes problems as the lexer inserts a semicolon before deriving which is then consumed by the gadtdecl rule before it can error and trigger the close rule to finish the layout context.

@adamschoenemann
Copy link

This bug also applies to empty where clauses in general (as in where-bindings) but I can only manifest it with two empty where clauses.
A definition such as

foo :: Int -> Int
foo x = z + b where
  y = x where
  z = y where
  b = 1

parses fine by GHC but is a parse-error for haskell-src-exts

@Melvar
Copy link

Melvar commented Sep 1, 2021

This also applies to associated data declarations in an instance (which are different in the AST), as I just found out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants