Skip to content

feat: add support for ImportQualifiedPost extension #477

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ncaq
Copy link

@ncaq ncaq commented Apr 12, 2025

I add support for the ImportQualifiedPost extension
to fix parse errors that occur when processing code that uses this GHC extension.
The extension enables writing import statements with qualified keyword after the module name
(e.g., import Data.List qualified as L instead of the traditional import qualified Data.List as L).

Motivation

When tools like the sandwich test framework
process Haskell code that uses the ImportQualifiedPost extension,
haskell-src-exts was unable to parse these imports correctly, resulting in errors.
This implementation allows code using this extension
to be properly parsed and processed by tools that depend on haskell-src-exts.

Changes

  • Added ImportQualifiedPost to the list of known extensions in Extension.hs
  • Updated the parser grammar to support post-qualified imports by adding a new optqualified_post rule
  • Modified the ExactPrint module to correctly handle post-qualified imports by determining the position of the qualified keyword based on source locations
  • Added test cases with golden files to verify correct parsing and exact printing

Known Issue

The pretty printer still converts post-qualified imports back to pre-qualified form
(e.g., import qualified Data.List as L)
because the ImportDecl data structure only stores whether an import is qualified or not,
but not whether the qualification is pre or post. This is a limitation of the current API design.

Future improvements could involve modifying the ImportDecl data structure to explicitly track qualification style,
but this would require API changes and is out of scope for this implementation.

I add support for the `ImportQualifiedPost` extension
to fix parse errors that occur when processing code that uses this GHC extension.
The extension enables writing import statements with `qualified` keyword after the module name
(e.g., `import Data.List qualified as L` instead of the traditional `import qualified Data.List as L`).

Motivation
===

When tools like the [sandwich](https://github.com/codedownio/sandwich/) test framework
process Haskell code that uses the `ImportQualifiedPost` extension,
haskell-src-exts was unable to parse these imports correctly, resulting in errors.
This implementation allows code using this extension
to be properly parsed and processed by tools that depend on haskell-src-exts.

Changes
===

- Added `ImportQualifiedPost` to the list of known extensions in `Extension.hs`
- Updated the parser grammar to support post-qualified imports by adding a new `optqualified_post` rule
- Modified the `ExactPrint` module to correctly handle post-qualified imports by determining the position of the `qualified` keyword based on source locations
- Added test cases with golden files to verify correct parsing and exact printing

Known Issue
===

The pretty printer still converts post-qualified imports back to pre-qualified form
(e.g., `import qualified Data.List as L`)
because the `ImportDecl` data structure only stores whether an import is qualified or not,
but not whether the qualification is pre or post. This is a limitation of the current API design.

Future improvements could involve modifying the `ImportDecl` data structure to explicitly track qualification style,
but this would require API changes and is out of scope for this implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant