1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE Rank2Types #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# OPTIONS_GHC -fno-warn-orphans #-}
5 module Hcompta.JCC.Lib.Parsec where
7 import Control.Monad (Monad(..))
8 import Data.Char (Char)
9 import qualified Data.Char as Char
10 import qualified Data.Foldable as Foldable
11 import Data.Function (($), (.))
12 import Data.String (String)
13 import Prelude (Integer, Integral(..), Num(..), seq)
14 import Text.Parsec (Stream, ParsecT, (<|>))
15 import qualified Text.Parsec as R
17 -- * Useful combinators
19 -- | Like 'R.choice' but with 'R.try' on each case.
20 choice_try :: Stream s m t => [ParsecT s u m a] -> ParsecT s u m a
21 choice_try = Foldable.foldr ((<|>) . R.try) R.parserZero
22 -- choice_try = R.choice . fmap R.try
24 -- | Like 'R.sepBy' but without parsing an ending separator.
30 many_separated p sep =
33 xs <- R.many (R.try (sep >> p))
36 -- | Like 'R.sepBy1' but without parsing an ending separator.
42 many1_separated p sep = do
44 xs <- R.many (R.try (sep >> p))
46 -- (:) <$> p <*> R.many (R.try (sep >> p))
48 -- | Make a 'R.ParsecT' also return its user state.
52 -> ParsecT s u m (a, u)
60 -- | Return the 'Integer' obtained by multiplying the given digits
61 -- with the power of the given base respective to their rank.
64 -> String -- ^ Digits (MUST be recognised by 'Char.digitToInt').
66 integer_of_digits base =
67 Foldable.foldl' (\x d ->
68 base*x + toInteger (Char.digitToInt d)) 0
70 decimal :: Stream s m Char => ParsecT s u m Integer
71 decimal = integer 10 R.digit
72 hexadecimal :: Stream s m Char => ParsecT s u m Integer
73 hexadecimal = R.oneOf "xX" >> integer 16 R.hexDigit
74 octal :: Stream s m Char => ParsecT s u m Integer
75 octal = R.oneOf "oO" >> integer 8 R.octDigit
77 -- | Parse an 'Integer'.
78 integer :: Stream s m t
81 -> ParsecT s u m Integer
82 integer base digit = do
83 digits <- R.many1 digit
84 let n = integer_of_digits base digits