Parsec-3.1.0 with a custom token data type

parsec-3.1.0 ( http://hackage.haskell.org/package/parsec-3.1.0 ) works with any tick type. However, there are combinators such as Text.Parsec.Char.satisfy that are defined only for the Char data type. It seems that there is no more general analogue.

Should I define my own versions or am I missing something?

Haskell may have different parser libraries that allow you to:

  • custom token types
  • parser user state (I need to parse the stateful format - Wavefront OBJ)
+3
source share
1 answer

oneOf, noneOf anyChar satisfy, :

oneOfT :: (Eq t, Show t, Stream s m t) => [t] -> ParsecT s u m t
oneOfT ts = satisfyT (`elem` ts)

noneOfT :: (Eq t, Show t, Stream s m t) => [t] -> ParsecT s u m t
noneOfT ts = satisfyT (not . (`elem` ts))

anyT :: (Show t, Stream s m t) => ParsecT s u m t
anyT = satisfyT (const True)

satisfyT :: (Show t, Stream s m t) => (t -> Bool) -> ParsecT s u m t
satisfyT p = tokenPrim showTok nextPos testTok
    where
      showTok t     = show t
      testTok t     = if p t then Just t else Nothing
      nextPos p t s = -- however you update position for your token stream

, , , t, - . , Show Eq, , , Show, == elem.

, Char, , , , .

, , .

+4

Source: https://habr.com/ru/post/1737607/


All Articles