@hackage ptera0.1.0.0
A parser generator
Categories
License
(Apache-2.0 OR MPL-2.0)
Maintainer
mizunashi-mana@noreply.git
Links
Versions
Installation
CustomDependencies (6)
- 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 Show all…
Dependents (1)
@hackage/ptera-th
Package Flags
develop
(off by default)
Turn on some options for development
Ptera: A Generator for Parsers
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