Created
December 4, 2018 18:07
-
-
Save Heimdell/641870a594c94a8e1d088f604c432a86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# language PatternSynonyms #-} | |
import Text.ParserCombinators.Parsec hiding (token) | |
data SExpr info atom | |
= Atom info atom | |
| SExpr info [SExpr info atom] | |
sexpr_elim onAtom onSexpr = recure | |
where | |
recure sexpr = case sexpr of | |
Atom info atom -> onAtom info atom | |
SExpr info children -> onSexpr info (map recure children) | |
instance (Show info, Show atom) => Show (SExpr info atom) where | |
show = sexpr_elim (\i -> show) (\_ list -> "(" ++ unwords list ++ ")") | |
sexpr :: Parser info -> Parser atom -> Parser (SExpr info atom) | |
sexpr info atom = recure | |
where | |
recure | |
= info >>= \info -> | |
( pure (SExpr info) | |
<* token "(" | |
<*> many recure | |
<* token ")" | |
<|> pure (Atom info) | |
<*> atom | |
) | |
token s = try (string s) <* spaces |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment