Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@glguy
Last active August 19, 2018 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glguy/7e8f8474630e3498842c72c20e5d152c to your computer and use it in GitHub Desktop.
Save glguy/7e8f8474630e3498842c72c20e5d152c to your computer and use it in GitHub Desktop.
module Demo where
check :: String -> Bool
check = go []
where
go ('[':xs) (']':ys) = go xs ys
go ('{':xs) ('}':ys) = go xs ys
go ('(':xs) (')':ys) = go xs ys
go xs (y:ys)
| y `elem` "[{(" = go (y:xs) ys
| otherwise = y `notElem` "]})" && go xs ys
go xs [] = null xs
module Demo where
import Text.Megaparsec
import Text.Megaparsec.Char
type Parser = Parsec (ErrorItem Char) String
demo :: String -> IO ()
demo = parseTest parser
parser :: Parser ()
parser = parserN *> eof
-- | Parse many non-bracket characters and bracketed subexpressions.
parserN :: Parser ()
parserN = skipMany parser1
-- | Parse a single non-bracket character or a whole bracketed subexpression.
parser1 :: Parser ()
parser1 = aux '(' ')' <|>
aux '[' ']' <|>
aux '{' '}' <|>
() <$ noneOf ")]}"
where
aux x y = () <$ between (char x) (char y) parserN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment