symparsec-1.1.1: Type level string parser combinators
Safe HaskellSafe-Inferred
LanguageGHC2021

Symparsec.Parser.Or

Synopsis

Documentation

type family pl :<|>: pr where ... infixl 3 Source #

Limited parser choice. Try left; if it fails, backtrack and try right. However, _the right choice must consume at least as much as the left choice._ If it doesn't, then even if the right parser succeeds, it will emit an error.

This behaviour is due to the parser runner not supporting backtracking. We can emulate it by storing a record of the characters parsed so far, and "replaying" these on the right parser if the left parser fails. If the right parser ends before we finish replaying, we will have consumed extra characters that we can't ask the runner to revert.

For example, Literal "abcd" :|: Literal "ab" is bad. An input of abcX will trigger the consumption error.

I can't think of another way to implement this with the current parser design. I think it's the best we have. A more complex parser design may permit changing internal running state, so we could save and load state (this would permit a Try p parser). But that's scary. And you're better off designing your type-level string schemas to permit non-backtracking parsing anyway...

Also problematic is that we never emit a left parser error, so errors can degrade. Perhaps your string was one character off a successful left parse; but if it fails, you won't see that error.

Equations

('PParser plCh plEnd s0l) :<|>: ('PParser prCh prEnd s0r) = Or' plCh plEnd s0l prCh prEnd s0r 

type Or' plCh plEnd s0l prCh prEnd s0r = 'PParser (OrChSym plCh prCh s0r) (OrEndSym plEnd prCh prEnd s0r) (Left '(s0l, '[])) Source #

type SOrS ssl ssr = SEither (STuple2 ssl (SList SChar)) ssr Source #

type OrS sl sr = Either (sl, [Char]) sr Source #

type SPOr ssl srl ssr srr plCh plEnd s0l prCh prEnd s0r = SParser (SOrS ssl ssr) (SEither srl srr) (Or' plCh plEnd s0l prCh prEnd s0r) Source #

sOr :: SParser ssl srl ('PParser plCh plEnd s0l) -> SParser ssr srr ('PParser prCh prEnd s0r) -> SPOr ssl srl ssr srr plCh plEnd s0l prCh prEnd s0r Source #

type family OrCh plCh prCh sr ch s where ... Source #

Equations

OrCh plCh prCh s0r ch (Left '(sl, chs)) = OrChL prCh s0r ch chs ((plCh @@ ch) @@ sl) 
OrCh plCh prCh _ ch (Right sr) = OrChR ((prCh @@ ch) @@ sr) 

type family OrChL prCh s0r chLast chs resl where ... Source #

Equations

OrChL _ _ chLast chs (Cont sl) = Cont (Left '(sl, chLast : chs)) 
OrChL _ _ _ _ (Done rl) = Done (Left rl) 
OrChL prCh s0r chLast chs (Err _) = OrChLReplay prCh chLast (Reverse chs) (Cont s0r) 

type family OrChLReplay prCh chLast chs resr where ... Source #

Equations

OrChLReplay prCh chLast (ch : chs) (Cont sr) = OrChLReplay prCh chLast chs ((prCh @@ ch) @@ sr) 
OrChLReplay prCh chLast '[] (Cont sr) = OrChR ((prCh @@ chLast) @@ sr) 
OrChLReplay prCh chLast chs (Err er) = Err (EOrR er) 
OrChLReplay prCh chLast chs (Done rr) = Err EOrStillReplaying 

sOrChLReplay :: SParserChSym ssr srr prCh -> SChar chLast -> SList SChar chs -> SResult ssr srr resr -> SResult (SOrS ssl ssr) (SEither srl srr) (OrChLReplay prCh chLast chs resr) Source #

type EOrR er = EIn "Or(R)" er Source #

eOrR :: SE er -> SE (EOrR er) Source #

type EOrStillReplaying = EBase "Or" (Text "right parser much consume at least as much as the failed left parser") Source #

type family OrChR resr where ... Source #

Equations

OrChR (Cont sr) = Cont (Right sr) 
OrChR (Done rr) = Done (Right rr) 
OrChR (Err er) = Err (EOrR er) 

sOrChR :: SResult ssr srr resr -> SResult (SOrS ssl ssr) (SEither srl srr) (OrChR resr) Source #

type family OrEnd plEnd prCh prEnd sr res where ... Source #

Equations

OrEnd plEnd prCh prEnd s0r (Left '(sl, chs)) = OrEndL prCh prEnd s0r chs (plEnd @@ sl) 
OrEnd plEnd prCh prEnd _ (Right sr) = OrEndR (prEnd @@ sr) 

type family OrEndR resr where ... Source #

Equations

OrEndR (Right rr) = Right (Right rr) 
OrEndR (Left er) = Left (EOrR er) 

sOrEndR :: SResultEnd srr resr -> SResultEnd (SEither srl srr) (OrEndR resr) Source #

type family OrEndL prCh prEnd s0r chs resl where ... Source #

Equations

OrEndL prCh prEnd s0r chs (Right rl) = Right (Left rl) 
OrEndL prCh prEnd s0r chs (Left el) = OrEndLReplay prCh prEnd (Reverse chs) (Cont s0r) 

sOrEndL :: SParserChSym ssr srr prCh -> SParserEndSym ssr srr prEnd -> ssr s0r -> SList SChar chs -> SResultEnd srl resl -> SResultEnd (SEither srl srr) (OrEndL prCh prEnd s0r chs resl) Source #

type family OrEndLReplay prCh prEnd chs resr where ... Source #

Equations

OrEndLReplay prCh prEnd (ch : chs) (Cont sr) = OrEndLReplay prCh prEnd chs ((prCh @@ ch) @@ sr) 
OrEndLReplay prCh prEnd '[] resr = OrEndLReplay' prEnd resr 
OrEndLReplay prCh prEnd chs (Err er) = Left (EOrR er) 
OrEndLReplay prCh prEnd chs (Done rr) = Left EOrStillReplaying 

sOrEndLReplay :: SParserChSym ssr srr prCh -> SParserEndSym ssr srr prEnd -> SList SChar chs -> SResult ssr srr resr -> SResultEnd (SEither srl srr) (OrEndLReplay prCh prEnd chs resr) Source #

type family OrEndLReplay' prEnd resr where ... Source #

Equations

OrEndLReplay' prEnd (Cont sr) = OrEndR (prEnd @@ sr) 
OrEndLReplay' prEnd (Done rr) = Right (Right rr) 
OrEndLReplay' prEnd (Err er) = Left (EOrR er) 

data OrChSym plCh prCh s0r f Source #

Instances

Instances details
(pl ~ 'PParser plCh plEnd s0l, pr ~ 'PParser prCh prEnd s0r, SingParser pl, SingParser pr) => SingParser (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) Source # 
Instance details

Defined in Symparsec.Parser.Or

Associated Types

type PS (Or' plCh plEnd s0l prCh prEnd s0r) :: s -> Type Source #

type PR (Or' plCh plEnd s0l prCh prEnd s0r) :: r -> Type Source #

Methods

singParser' :: SParser (PS (Or' plCh plEnd s0l prCh prEnd s0r)) (PR (Or' plCh plEnd s0l prCh prEnd s0r)) (Or' plCh plEnd s0l prCh prEnd s0r) Source #

type App (OrChSym plCh prCh s0r :: FunKind Char (OrS sl sr ~> PResult (OrS sl sr) (Either rl rr)) -> Type) (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Or

type App (OrChSym plCh prCh s0r :: FunKind Char (OrS sl sr ~> PResult (OrS sl sr) (Either rl rr)) -> Type) (f :: Char) = OrChSym1 plCh prCh s0r f
type PR (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) Source # 
Instance details

Defined in Symparsec.Parser.Or

type PR (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) = SEither (PR ('PParser plCh plEnd s0l)) (PR ('PParser prCh prEnd s0r))
type PS (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) Source # 
Instance details

Defined in Symparsec.Parser.Or

type PS (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) = SOrS (PS ('PParser plCh plEnd s0l)) (PS ('PParser prCh prEnd s0r))

data OrChSym1 plCh prCh sr ch s Source #

Instances

Instances details
type App (OrChSym1 plCh prCh sr2 ch :: FunKind (OrS sl sr1) (PResult (OrS sl sr1) (Either rl rr)) -> Type) (s :: OrS sl sr1) Source # 
Instance details

Defined in Symparsec.Parser.Or

type App (OrChSym1 plCh prCh sr2 ch :: FunKind (OrS sl sr1) (PResult (OrS sl sr1) (Either rl rr)) -> Type) (s :: OrS sl sr1) = OrCh plCh prCh sr2 ch s

sOrChSym :: SParserChSym ssl srl plCh -> SParserChSym ssr srr prCh -> ssr s0r -> SParserChSym (SOrS ssl ssr) (SEither srl srr) (OrChSym plCh prCh s0r) Source #

sOrChL :: SParserChSym ssr srr prCh -> ssr s0r -> SChar chLast -> SList SChar chs -> SResult ssl srl resl -> SResult (SOrS ssl ssr) (SEither srl srr) (OrChL prCh s0r chLast chs resl) Source #

data OrEndSym plEnd prCh prEnd s0r s Source #

Instances

Instances details
(pl ~ 'PParser plCh plEnd s0l, pr ~ 'PParser prCh prEnd s0r, SingParser pl, SingParser pr) => SingParser (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) Source # 
Instance details

Defined in Symparsec.Parser.Or

Associated Types

type PS (Or' plCh plEnd s0l prCh prEnd s0r) :: s -> Type Source #

type PR (Or' plCh plEnd s0l prCh prEnd s0r) :: r -> Type Source #

Methods

singParser' :: SParser (PS (Or' plCh plEnd s0l prCh prEnd s0r)) (PR (Or' plCh plEnd s0l prCh prEnd s0r)) (Or' plCh plEnd s0l prCh prEnd s0r) Source #

type App (OrEndSym plEnd prCh prEnd s0r :: FunKind (Either (sl, [Char]) sr) (PResultEnd (Either rl rr)) -> Type) (s :: Either (sl, [Char]) sr) Source # 
Instance details

Defined in Symparsec.Parser.Or

type App (OrEndSym plEnd prCh prEnd s0r :: FunKind (Either (sl, [Char]) sr) (PResultEnd (Either rl rr)) -> Type) (s :: Either (sl, [Char]) sr) = OrEnd plEnd prCh prEnd s0r s
type PR (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) Source # 
Instance details

Defined in Symparsec.Parser.Or

type PR (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) = SEither (PR ('PParser plCh plEnd s0l)) (PR ('PParser prCh prEnd s0r))
type PS (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) Source # 
Instance details

Defined in Symparsec.Parser.Or

type PS (Or' plCh plEnd s0l prCh prEnd s0r :: PParser (OrS sl sr) (Either rl rr)) = SOrS (PS ('PParser plCh plEnd s0l)) (PS ('PParser prCh prEnd s0r))

sOrEndSym :: SParserEndSym ssl srl plEnd -> SParserChSym ssr srr prCh -> SParserEndSym ssr srr prEnd -> ssr s0r -> SParserEndSym (SOrS ssl ssr) (SEither srl srr) (OrEndSym plEnd prCh prEnd s0r) Source #