happy-arbitrary: Random programs for testing parsers made with Happy and Alex

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

Makes random programs from a parser's own Happy grammar and Alex lexer, of a given size in tokens, and checks their text with the language's lexer. For QuickCheck properties of parsers and pretty-printers.


[Skip to Readme]

Properties

Versions 0.0.1, 0.0.2, 0.0.3
Change log None available
Dependencies aeson, array, attoparsec, base (<5), bytestring, containers, data-fix, file-embed, mtl, QuickCheck, text, transformers-compat [details]
License GPL-3.0-only
Copyright Copyright (c) 2024-2026, Iphigenia Df
Author Iphigenia Df <iphydf@gmail.com>
Maintainer Iphigenia Df <iphydf@gmail.com>
Category Testing
Home page https://toktok.ltd/
Source repo head: git clone https://github.com/TokTok/hs-happy-arbitrary
Uploaded by iphydf at 2026-11-24T17:51:35Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for happy-arbitrary-0.0.3

[back to package description]

Haskell package: happy-arbitrary

Random programs for testing parsers made with Happy and Alex. It reads the parser's own grammar (.y) and lexer (.x), so the programs follow the grammar, and it asks the language's lexer whether their text lexes back to the tokens it generated.

Example

A property that printing a parsed program and parsing it again gives the same output:

import Language.Alex.Parser (parseAlex)
import Language.Happy (parseGrammar, runAlex)
import Language.Happy.Linker (Lexer, defaultLinkConfig, generateChecked)

-- | The token classes our lexer finds, named like in the %token patterns.
classes :: Lexer
classes = either (const Nothing) (Just . map (Text.pack . show . MyLexer.lexemeClass)) . MyLexer.scan

spec :: Spec
spec = do
    alex <- runIO $ either error id . parseAlex <$> Text.readFile "src/Lexer.x"
    happy <- runIO $ either error id . flip runAlex parseGrammar <$> LBS.readFile "src/Parser.y"
    let programs = generateChecked defaultLinkConfig alex classes "Program" happy

    it "prints the same after parsing what it printed" $
        forAll (programs `suchThatMap` (either (const Nothing) Just . MyParser.parse)) $ \ast ->
            let printed = MyPrinter.print ast
            in MyPrinter.print <$> MyParser.parse printed `shouldBe` Right printed

Programs have QuickCheck's size in tokens. The grammar doesn't know the checks in the parser's actions or in later passes, so the test drops the programs that the real parser rejects.

By default, a %token pattern like { L _ IdVar _ } names the token class IdVar, and so does a lexer action like { mkL IdVar }. A LinkConfig can find the class in other code.

Documentation

See doc/design.md for how it works, and doc/development.md for building and testing it.