{-# LANGUAGE UndecidableInstances #-}
module Symparsec.Parser.Isolate ( type Isolate, type IsolateSym ) where
import Symparsec.Parser.Common
import Symparsec.Utils ( type IfNatLte )
type Isolate :: Natural -> PParser a -> PParser a
data Isolate n p s
type instance App (Isolate n p) s = Isolate' n p s
type family Isolate' n p s where
Isolate' n p ('State rem len idx) =
IfNatLte n len
(IsolateEnd len n (p @@ ('State rem n idx)))
('Reply (Err (Error1 (EStrInputTooShort n len))) ('State rem len idx))
type family IsolateEnd lenOrig n rep where
IsolateEnd lenOrig n ('Reply (OK a) ('State rem 0 idx)) =
'Reply (OK a) ('State rem (lenOrig-n) idx)
IsolateEnd lenOrig n ('Reply (Err e) ('State rem len idx)) =
'Reply (Err e) ('State rem (lenOrig-n+len) idx)
IsolateEnd lenOrig n ('Reply (OK _a) ('State rem len idx)) =
'Reply (Err (EIsolateIncomplete len)) ('State rem (lenOrig-n+len) idx)
type EIsolateIncomplete n = Error1
( "isolated parser completed without consuming all input ("
++ ShowNatDec n ++ " remaining)" )
type IsolateSym :: PParser a -> Natural ~> PParser a
data IsolateSym p x
type instance App (IsolateSym p) n = Isolate n p