Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Created December 4, 2018 18:07
Show Gist options
  • Save Heimdell/641870a594c94a8e1d088f604c432a86 to your computer and use it in GitHub Desktop.
Save Heimdell/641870a594c94a8e1d088f604c432a86 to your computer and use it in GitHub Desktop.
{-# 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