ptera: A parser generator

[ library, parsing ] [ Propose Tags ]

Ptera is haskell libraries and toolchains for generating parser.


[Skip to Readme]

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

Candidates

Versions [RSS] 0.1.0.0, 0.2.0.0, 0.3.0.0, 0.4.0.0
Change log CHANGELOG.md
Dependencies base (>=4.14.0 && <5), containers (>=0.6.0 && <0.7), enummapset-th (>=0.6.0 && <0.7), membership (>=0.0.1 && <0.1), ptera-core (>=0.1.0 && <0.2), 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:32:53Z
Distributions NixOS:0.4.0.0
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 191 total (10 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-01-26 [all 1 reports]

Readme for ptera-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