Skip to content

Haskell: add --positions=range flag to include start and end position#546

Merged
Commelina merged 6 commits intoBNFC:masterfrom
Commelina:wip-end-pos
Mar 18, 2026
Merged

Haskell: add --positions=range flag to include start and end position#546
Commelina merged 6 commits intoBNFC:masterfrom
Commelina:wip-end-pos

Conversation

@Commelina
Copy link
Member

@Commelina Commelina commented Mar 11, 2026

Fixes #461 .

This PR added a --positions=range flag to the --haskell backend. The flag generates AST nodes with both start and end position:

type BNFC'Position = C.Maybe ((C.Int, C.Int), (C.Int, C.Int))

The position is also stored in generated Agda AST. However, it is currently not supported for layout grammars (the end position is always the same as the start one).

There should be absolutely NO change on ANY generated file if --positions=range is not given.

Some snippets from #463 are used.

TODO:

  1. Code cleanup (especially these with functor, withEndPos, hasFunctorOption, etc.)
  2. Detailed docs
  3. Add basic unit tests for --functor-v2
  4. Add end-to-end tests to ensure the generated files are not changed without the new flag (maybe build an old version and compare, anyway there should be a framework for such kind of tests)
  5. Support layout grammars

Item 1 and 2 are necessary before this PR can be merged.

@Commelina Commelina force-pushed the wip-end-pos branch 2 times, most recently from 627daa8 to ae13281 Compare March 12, 2026 00:57
@Commelina Commelina marked this pull request as ready for review March 12, 2026 00:58
@Commelina Commelina changed the title [DRAFT] Haskell: add --functor-v2 flag to include start and end position Haskell: add --functor-v2 flag to include start and end position Mar 12, 2026
@Commelina Commelina requested a review from andreasabel March 12, 2026 09:08
@Commelina Commelina force-pushed the wip-end-pos branch 2 times, most recently from e6f5cc5 to 371a2ca Compare March 12, 2026 09:19
@andreasabel
Copy link
Member

andreasabel commented Mar 12, 2026

How about organizing the option name like this: --positions=range|start|line. (Alternative for range: span but it seems that "range" is the more modern terminology.)

Legacy Meaning
--line-numbers --positions=line
--functor --positions=start

--posititions=range is the new one (this PR).

Defaults if just --positions is passed:

  • Haskell: range
  • C/C++/Java the current only option: line

Internally:

data Positions
  = None
  | Range
  | Start
  | Line

@Commelina Commelina changed the title Haskell: add --functor-v2 flag to include start and end position Haskell: add --positions=range flag to include start and end position Mar 12, 2026
@Commelina
Copy link
Member Author

Finished the first step unifying the position-related options, and --positions=start|range should work as expected for the Haskell backend (--functor is an alias to --positions=start now). line is present only internally.

aabounegm and others added 3 commits March 16, 2026 15:28
Co-authored-by: Nikolai Kudasov <fizruk@users.noreply.github.com>
Co-authored-by: Nikolai Kudasov <fizruk@users.noreply.github.com>
Copy link
Member

@andreasabel andreasabel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I tried this on one example for Haskell and Agda, and found no issues.

@Commelina Commelina merged commit 10274da into BNFC:master Mar 18, 2026
28 checks passed
@fizruk
Copy link
Contributor

fizruk commented Mar 19, 2026

So cool to have this merged! Thanks for picking this up @Commelina!

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.

Store tokens' position range instead of just start position

4 participants