-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParseUtil.hs
28 lines (25 loc) · 1.01 KB
/
ParseUtil.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{-# LANGUAGE OverloadedStrings #-}
module ParseUtil (
module Data.Attoparsec.Text,
isAllOf, isOneOf, identifier, skipHSpace, buffer, bufferH, bracket,
finishParse, consumedResult) where
import Data.Attoparsec.Text
import Data.Text as TS
import Data.Char
import Control.Applicative
isCombOf comb preds ch = comb $ preds <*> [ch]
isAllOf = isCombOf and
isOneOf = isCombOf or
isIdentChar = isOneOf [isAlpha, isDigit, ('_' ==)]
identifier = takeWhile1 isIdentChar
skipHSpace = skipWhile isHorizontalSpace
buffer body = skipSpace *> body <* skipSpace
bufferH body = skipHSpace *> body <* skipHSpace
bracket open close body = char open *> buffer body <* char close
-- sometimes you get another Partial after feeding empty
finishParse result@(Partial _) = finishParse $ feed result TS.empty
finishParse result = result
consumedResult done@(Done "" out) = done
consumedResult (Done text out) = Fail text [] "Unparsed trailing text"
consumedResult fail@(Fail _ _ _) = fail
consumedResult partial = consumedResult $ finishParse partial