ptera-th: A parser generator

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]

Ptera is haskell libraries and toolchains for generating parser.


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0, 0.2.0.0, 0.3.0.0, 0.4.0.0, 0.5.0.0, 0.6.0.0, 0.6.1.0, 0.7.0.0
Change log CHANGELOG.md
Dependencies array (>=0.5.4 && <0.6), base (>=4.14.0 && <5), containers (>=0.6.0 && <0.7), enummapset-th (>=0.6.0 && <0.7), ghc-prim (>=0.6.1 && <0.7), membership (>=0.0.1 && <0.1), ptera (>=0.1.0 && <0.2), ptera-core (>=0.1.0 && <0.2), template-haskell (>=2.16.0 && <2.17), unordered-containers (>=0.2.0 && <0.3) [details]
License (Apache-2.0 OR MPL-2.0)
Copyright (c) 2021 Mizunashi Mana
Author Mizunashi Mana
Maintainer mizunashi-mana@noreply.git
Category Parsing
Home page https://github.com/mizunashi-mana/ptera
Bug tracker https://github.com/mizunashi-mana/ptera/issues
Source repo head: git clone https://github.com/mizunashi-mana/ptera.git
Uploaded by mizunashi_mana at 2022-01-26T14:33:26Z

Modules

[Index] [Quick Jump]

Flags

Manual Flags

NameDescriptionDefault
develop

Turn on some options for development

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for ptera-th-0.1.0.0

[back to package description]

Ptera: A Generator for Parsers

Hackage

Installation

Add dependencies on package.cabal:

build-depends:
    base,
    bytestring,
    ptera,          -- main
    ptera-th,       -- for outputing parser with Template Haskell
    charset,
    template-haskell,

Usage

Write parser rules:

data Terminal
    = Digit
    | SymPlus
    | SymMulti
    deriving (Eq, Show, Enum)

data NonTerminal
    | Expr
    | Sum
    | Product
    | Value
    deriving (Eq, Show, Enum)

type ParseRule = Rule Terminal NonTerminal


data Ast
    = GenValue
    | GenSum (NonEmpty Ast)
    | GenProduct Ast Ast

rExpr :: ParseRule Ast
rExpr = rule Expr rSum

rSum :: ParseRule Ast
rSum = rule Sum do
    (rProduct <,> manyP do token SymPlus *> rProduct) <&> \(e, es) -> GenSum do e :| es

rProduct :: ParseRule Ast
rProduct = rule Product do
    orP
        [
            (rValue <* token SymMulti <,> rProduct) <&> \(e1, e2) -> GenProduct e1 e2,
            rValue
        ]

rValue :: ParseRule Ast
rValue = rule Value do token Digit *> pure GenValue

Examples