MyNixOS website logo
Description

A parser generator.

Ptera is haskell libraries and toolchains for generating parser.

Ptera: A Parser Generator for PEGs

Hackage

Installation

Add dependencies on package.cabal:

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

Usage

Write parser rules:

module Parser.Rules where

import Language.Haskell.TH
import Language.Parser.Ptera.TH

data Token
    = TokDigit
    | TokSymPlus
    | TokSymMulti
    | TokEndOfInput
    deriving (Eq, Show, Enum)

data Ast
    = Value
    | Sum Ast Ast
    | Product Ast Ast

$(genGrammarToken (mkName "Tokens") [t|Token|]
    [ ("+", [p|TokSymPlus|])
    , ("*", [p|TokSymMulti|])
    , ("n", [p|TokDigit|])
    , ("^Z", [p|TokEndOfInput{}|])
    ])

$(Ptera.genRules
    do TH.mkName "RuleDefs"
    do GenRulesTypes
        { genRulesCtxTy = [t|()|]
        , genRulesTokensTy = [t|Tokens|]
        , genRulesTokenTy = [t|Token|]
        }
    [ (TH.mkName "ruleExprEos", "expr ^Z", [t|Ast|])
    , (TH.mkName "ruleExpr", "expr", [t|Ast|])
    , (TH.mkName "ruleSum", "sum", [t|Ast|])
    , (TH.mkName "ruleProduct", "product", [t|Ast|])
    , (TH.mkName "ruleValue", "value", [t|Ast|])
    ]
    )

$(Ptera.genParsePoints
    do TH.mkName "ParsePoints"
    do TH.mkName "RuleDefs"
    [ "expr ^Z"
    ]
    )

grammar :: Grammar RuleDefs Tokens Token ParsePoints
grammar = fixGrammar $ RuleDefs
    { ruleExprEos = rExprEos
    , ruleExpr = rExpr
    , ruleSum = rSum
    , ruleProduct = rProduct
    , ruleValue = rValue
    }

type Rule = RuleExpr RuleDefs Tokens Token


rExprEos :: Rule Ast
rExprEos = ruleExpr
    [ varA @"expr" <^> tokA @"^Z"
        <:> \(e :* _ :* HNil) -> e
    ]

rExpr :: Rule Ast
rExpr = ruleExpr
    [ varA @"sum"
        <:> \(e :* HNil) -> e
    ]

rSum :: Rule Ast
rSum = ruleExpr
    [ varA @"product" <^> tokA @"+" <^> varA @"sum"
        <:> \(e1 :* _ :* e2 :* HNil) -> [|| Sum $$(e1) $$(e2) ||]
    , varA @"product"
        <:> \(e :* HNil) -> e
    ]

rProduct :: Rule Ast
rProduct = ruleExpr
    [ varA @"value" <^> tokA @"*" <^> varA @"product"
        <:> \(e1 :* _ :* e2 :* HNil) -> [|| Product $$(e1) $$(e2) ||]
    , varA @"value"
        <:> \(e :* HNil) -> e
    ]

rValue :: Rule Ast
rValue = ruleExpr
    [ tokA @"n" <:> \(n :* HNil) ->
        [|| case $$(n) of
            TokDigit -> Value
            _        -> error "unreachable: expected digit token"
        ||]
    ]

And, generate parser:

module Parser where

import Parser.Rules
import Language.Parser.Ptera.TH
import Data.Proxy

$(genRunner
    (GenParam
        { startsTy = [t|ParsePoints|]
        , rulesTy  = [t|RuleDefs|]
        , tokensTy = [t|Tokens|]
        , tokenTy  = [t|Token|]
        , customCtxTy = defaultCustomCtxTy
        })
    grammar
    )

exprParser :: Scanner posMark Token m => m (Result posMark Ast)
exprParser = runParser (Proxy :: Proxy "expr EOS") pteraTHRunner

Examples

  • Small language: https://github.com/mizunashi-mana/ptera/tree/master/example/small-lang-th
  • Haskell2010: https://github.com/mizunashi-mana/ptera/tree/master/example/haskell2010
Metadata

Version

0.7.0.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows