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

Symparsec.Parser

Description

Base definitions for Symparsec parsers.

Some types are useable both on term-level, and promoted on type-level e.g. Result. For ease of use, you can access the promoted version via type synonyms like PResult (for promoted X). (This pattern is copied from singletons.)

Some definitions I use:

  • "defun symbol": Short for "defunctionalization symbol". A method of passing type-level functions (type families) around, without applying them. We use phadej's defun library for the basic functionality.
  • "consuming [parser]": A parser which must consume its input. Symparsec parsers are always consuming, as it helps keep parser running simple. This is problematic, as you can define non-consuming parsers e.g. Take 0. We handle this by preprocessing initial parser state, to check for such cases.
Synopsis

Parser

data Parser str s r Source #

Constructors

Parser 

Fields

data PParser s r Source #

A type-level parser, containing defunctionalization symbols.

Only intended for promoted use. For singled term-level parsers, use SParser. (Symparsec doesn't provide "regular" term-level parsers.)

I would make this demotable, but the defun symbols get in the way.

Constructors

PParser 

data SParser ss sr p where Source #

A singled parser.

TODO consider swapping for STuple3...? this is much easier though

Constructors

SParser :: SParserChSym ss sr pCh -> SParserEndSym ss sr pEnd -> ss s0 -> SParser ss sr ('PParser pCh pEnd s0) 

type ParserCh str s r = Char -> s -> Result str s r Source #

Parse a Char with the given state.

The action is always consuming. For this reason, you may need to look ahead for the final case, so as to not consume an extra Char. This prevents many zero-length parsers. It's a bit weird. See Drop for an example.

type ParserChSym s r = Char ~> (s ~> PResult s r) Source #

A defunctionalization symbol for a ParserCh.

type ParserChSym1 s r = Char -> s ~> PResult s r Source #

A partially-applied defunctionalization symbol for a ParserCh.

type SParserChSym ss sr pCh = Lam2 SChar ss (SResult ss sr) pCh Source #

type SParserChSym1 ch ss sr pCh = SChar ch -> Lam ss (SResult ss sr) (pCh ch) Source #

type ParserEnd str s r = s -> ResultEnd str r Source #

What a parser should do at the end of a Symbol.

type ParserEndSym s r = s ~> PResultEnd r Source #

A defunctionalization symbol for a ParserEnd.

type SParserEndSym ss sr pEnd = Lam ss (SResultEnd sr) pEnd Source #

type ParserSInit s0 s = s0 -> PResultSInit s Source #

type SParserSInitSym ss0 ss sInit = Lam ss0 (SResultSInit ss) sInit Source #

data Result str s r Source #

The result of a single step of a parser.

Promotable. Instantiate with String for term-level, Symbol for type-level.

Note that a Done indicates the parser has not consumed the character. In the original design, it did consume it, and parsers did their own "lookahead" to handle this. The non-consuming behaviour simplifies permitting non-consuming parsers such as Take 0.

Constructors

Cont s

OK, consumed, continue with state s

Done r

OK, not consumed, parse succeeded with result r

Err (E str)

parse error

Instances

Instances details
type App TakeChSym (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Take

type App TakeChSym (f :: Char) = TakeChSym1 f
type App SkipChSym (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Skip

type App SkipChSym (f :: Char) = SkipChSym1 f
type App LiteralChSym (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Literal

type App (TakeChSym1 ch :: FunKind TakeS (PResult TakeS Symbol) -> Type) (s :: TakeS) Source # 
Instance details

Defined in Symparsec.Parser.Take

type App (TakeChSym1 ch :: FunKind TakeS (PResult TakeS Symbol) -> Type) (s :: TakeS) = TakeCh ch s
type App (SkipChSym1 ch :: FunKind Natural (PResult Natural ()) -> Type) (n :: Natural) Source # 
Instance details

Defined in Symparsec.Parser.Skip

type App (SkipChSym1 ch :: FunKind Natural (PResult Natural ()) -> Type) (n :: Natural) = SkipCh ch n
type App (LiteralChSym1 ch :: FunKind Symbol (PResult Symbol ()) -> Type) (s :: Symbol) Source # 
Instance details

Defined in Symparsec.Parser.Literal

type App (LiteralChSym1 ch :: FunKind Symbol (PResult Symbol ()) -> Type) (s :: Symbol) = LiteralCh ch s
type App (NatBaseChSym base parseDigit :: FunKind Char (Maybe Natural ~> PResult (Maybe Natural) Natural) -> Type) (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Natural

type App (NatBaseChSym base parseDigit :: FunKind Char (Maybe Natural ~> PResult (Maybe Natural) Natural) -> Type) (f :: Char) = NatBaseChSym1 base parseDigit f
type App (IsolateChSym pCh pEnd :: FunKind Char ((Natural, s) ~> PResult (Natural, s) r) -> Type) (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Isolate

type App (IsolateChSym pCh pEnd :: FunKind Char ((Natural, s) ~> PResult (Natural, s) r) -> Type) (f :: Char) = IsolateChSym1 pCh pEnd f
type App (FailChSym name e :: FunKind Char (s ~> PResult s r) -> Type) (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Common

type App (FailChSym name e :: FunKind Char (s ~> PResult s r) -> Type) (f :: Char)
type App (ThenChSym plCh prCh s0r :: FunKind Char (Either sl (rl, sr) ~> PResult (Either sl (rl, sr)) (rl, rr)) -> Type) (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Then

type App (ThenChSym plCh prCh s0r :: FunKind Char (Either sl (rl, sr) ~> PResult (Either sl (rl, sr)) (rl, rr)) -> Type) (f :: Char) = ThenChSym1 plCh prCh s0r f
type App (ThenVRChSym plCh prCh s0r :: FunKind Char (Either sl (rl, sr) ~> PResult (Either sl (rl, sr)) rl) -> Type) (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Then.VoidRight

type App (ThenVRChSym plCh prCh s0r :: FunKind Char (Either sl (rl, sr) ~> PResult (Either sl (rl, sr)) rl) -> Type) (f :: Char) = ThenVRChSym1 plCh prCh s0r f
type App (ThenVLChSym plCh prCh s0r :: FunKind Char (Either sl sr ~> PResult (Either sl sr) rr) -> Type) (f :: Char) Source # 
Instance details

Defined in Symparsec.Parser.Then.VoidLeft

type App (ThenVLChSym plCh prCh s0r :: FunKind Char (Either sl sr ~> PResult (Either sl sr) rr) -> Type) (f :: Char) = ThenVLChSym1 plCh prCh s0r f
type App (NatBaseChSym1 base parseDigit ch :: FunKind (Maybe Natural) (PResult (Maybe Natural) Natural) -> Type) (mn :: Maybe Natural) Source # 
Instance details

Defined in Symparsec.Parser.Natural

type App (NatBaseChSym1 base parseDigit ch :: FunKind (Maybe Natural) (PResult (Maybe Natural) Natural) -> Type) (mn :: Maybe Natural) = NatBaseCh base parseDigit ch mn
type App (IsolateChSym1 pCh pEnd ch :: FunKind (Natural, s1) (PResult (Natural, s1) r) -> Type) (s2 :: (Natural, s1)) Source # 
Instance details

Defined in Symparsec.Parser.Isolate

type App (IsolateChSym1 pCh pEnd ch :: FunKind (Natural, s1) (PResult (Natural, s1) r) -> Type) (s2 :: (Natural, s1)) = IsolateCh pCh pEnd ch s2
type App (ThenChSym1 plCh prCh s0r ch :: FunKind (Either sl (rl, sr)) (PResult (Either sl (rl, sr)) (rl, rr)) -> Type) (s :: Either sl (rl, sr)) Source # 
Instance details

Defined in Symparsec.Parser.Then

type App (ThenChSym1 plCh prCh s0r ch :: FunKind (Either sl (rl, sr)) (PResult (Either sl (rl, sr)) (rl, rr)) -> Type) (s :: Either sl (rl, sr)) = ThenCh plCh prCh s0r ch s
type App (ThenVRChSym1 plCh prCh s0r ch :: FunKind (Either sl (rl, sr)) (PResult (Either sl (rl, sr)) rl) -> Type) (s :: Either sl (rl, sr)) Source # 
Instance details

Defined in Symparsec.Parser.Then.VoidRight

type App (ThenVRChSym1 plCh prCh s0r ch :: FunKind (Either sl (rl, sr)) (PResult (Either sl (rl, sr)) rl) -> Type) (s :: Either sl (rl, sr)) = ThenVRCh plCh prCh s0r ch s
type App (ThenVLChSym1 plCh prCh s0r ch :: FunKind (Either sl sr) (PResult (Either sl sr) rr) -> Type) (s :: Either sl sr) Source # 
Instance details

Defined in Symparsec.Parser.Then.VoidLeft

type App (ThenVLChSym1 plCh prCh s0r ch :: FunKind (Either sl sr) (PResult (Either sl sr) rr) -> Type) (s :: Either sl sr) = ThenVLCh plCh prCh s0r ch s

data SResult (ss :: s -> Type) (sr :: r -> Type) (res :: PResult s r) where Source #

Constructors

SCont :: ss s -> SResult ss sr (Cont s) 
SDone :: sr r -> SResult ss sr (Done r) 
SErr :: SE e -> SResult ss sr (Err e) 

type PResultSInit s = Either (PE, s) s Source #

Error

data E str Source #

Parser error.

Promotable. Instantiate with String for term-level, Symbol for type-level.

Constructors

EBase

Base parser error.

Fields

  • str

    parser name

  • (Doc str)

    error message

EIn

Inner parser error inside combinator.

Fields

  • str

    combinator name

  • (E str)

    inner error

Instances

Instances details
Demotable SE Source # 
Instance details

Defined in Symparsec.Parser

Associated Types

type Demote SE #

Methods

demote :: forall (k1 :: k). SE k1 -> Demote SE #

SingParser End Source # 
Instance details

Defined in Symparsec.Parser.End

Associated Types

type PS End :: s -> Type Source #

type PR End :: r -> Type Source #

Show str => Show (E str) Source # 
Instance details

Defined in Symparsec.Parser

Methods

showsPrec :: Int -> E str -> ShowS #

show :: E str -> String #

showList :: [E str] -> ShowS #

type Demote SE Source # 
Instance details

Defined in Symparsec.Parser

type Demote SE = E String
type PR End Source # 
Instance details

Defined in Symparsec.Parser.End

type PR End = SUnit
type PS End Source # 
Instance details

Defined in Symparsec.Parser.End

type PS End = SUnit
type App TakeEndSym (s :: TakeS) Source # 
Instance details

Defined in Symparsec.Parser.Take

type App TakeEndSym (s :: TakeS) = TakeEnd s
type App SkipEndSym (n :: Natural) Source # 
Instance details

Defined in Symparsec.Parser.Skip

type App SkipEndSym (n :: Natural) = SkipEnd n
type App LiteralEndSym (s :: Symbol) Source # 
Instance details

Defined in Symparsec.Parser.Literal

type App (FailEndSym name e :: FunKind a (PResultEnd r) -> Type) (s :: a) Source # 
Instance details

Defined in Symparsec.Parser.Common

type App (FailEndSym name e :: FunKind a (PResultEnd r) -> Type) (s :: a) = 'Left ('EBase name e) :: Either (E Symbol) r
type App NatBaseEndSym (mn :: Maybe Natural) Source # 
Instance details

Defined in Symparsec.Parser.Natural

type App (IsolateEndSym pEnd :: FunKind (Natural, s1) (PResultEnd r) -> Type) (s2 :: (Natural, s1)) Source # 
Instance details

Defined in Symparsec.Parser.Isolate

type App (IsolateEndSym pEnd :: FunKind (Natural, s1) (PResultEnd r) -> Type) (s2 :: (Natural, s1)) = IsolateEnd pEnd s2
type App (ThenVREndSym plEnd prEnd s0r :: FunKind (Either a1 (b1, a2)) (PResultEnd b1) -> Type) (s :: Either a1 (b1, a2)) Source # 
Instance details

Defined in Symparsec.Parser.Then.VoidRight

type App (ThenVREndSym plEnd prEnd s0r :: FunKind (Either a1 (b1, a2)) (PResultEnd b1) -> Type) (s :: Either a1 (b1, a2)) = ThenVREnd plEnd prEnd s0r s
type App (ThenEndSym plEnd prEnd s0r :: FunKind (Either a1 (k1, a2)) (PResultEnd (k1, k2)) -> Type) (s :: Either a1 (k1, a2)) Source # 
Instance details

Defined in Symparsec.Parser.Then

type App (ThenEndSym plEnd prEnd s0r :: FunKind (Either a1 (k1, a2)) (PResultEnd (k1, k2)) -> Type) (s :: Either a1 (k1, a2)) = ThenEnd plEnd prEnd s0r s
type App (ThenVLEndSym plEnd prEnd s0r :: FunKind (Either a1 a2) (PResultEnd b2) -> Type) (s :: Either a1 a2) Source # 
Instance details

Defined in Symparsec.Parser.Then.VoidLeft

type App (ThenVLEndSym plEnd prEnd s0r :: FunKind (Either a1 a2) (PResultEnd b2) -> Type) (s :: Either a1 a2) = ThenVLEnd plEnd prEnd s0r s

type PE = E Symbol Source #

Promoted E.

data SE (e :: PE) where Source #

Constructors

SEBase :: SSymbol name -> SDoc doc -> SE (EBase name doc) 
SEIn :: SSymbol name -> SE e -> SE (EIn name e) 

Instances

Instances details
Demotable SE Source # 
Instance details

Defined in Symparsec.Parser

Associated Types

type Demote SE #

Methods

demote :: forall (k1 :: k). SE k1 -> Demote SE #

type Demote SE Source # 
Instance details

Defined in Symparsec.Parser

type Demote SE = E String

class SingE (e :: PE) where Source #

Methods

singE :: SE e Source #

Instances

Instances details
(KnownSymbol name, SingDoc doc) => SingE ('EBase name doc) Source # 
Instance details

Defined in Symparsec.Parser

Methods

singE :: SE ('EBase name doc) Source #

(KnownSymbol name, SingE e) => SingE ('EIn name e) Source # 
Instance details

Defined in Symparsec.Parser

Methods

singE :: SE ('EIn name e) Source #

withSingE :: forall e r. SE e -> (SingE e => r) -> r Source #

Singling

class SingParser (p :: PParser s r) where Source #

Promoted parsers with singled implementations.

Associated Types

type PS p :: s -> Type Source #

A singleton for the parser state.

type PR p :: r -> Type Source #

A singleton for the parser return type.

Methods

singParser' :: SParser (PS p) (PR p) p Source #

The singled parser.

Instances

Instances details
SingParser End Source # 
Instance details

Defined in Symparsec.Parser.End

Associated Types

type PS End :: s -> Type Source #

type PR End :: r -> Type Source #

KnownNat n => SingParser (Take n :: PParser TakeS Symbol) Source # 
Instance details

Defined in Symparsec.Parser.Take

Associated Types

type PS (Take n) :: s -> Type Source #

type PR (Take n) :: r -> Type Source #

Methods

singParser' :: SParser (PS (Take n)) (PR (Take n)) (Take n) Source #

KnownNat n => SingParser (Skip n :: PParser Natural ()) Source # 
Instance details

Defined in Symparsec.Parser.Skip

Associated Types

type PS (Skip n) :: s -> Type Source #

type PR (Skip n) :: r -> Type Source #

Methods

singParser' :: SParser (PS (Skip n)) (PR (Skip n)) (Skip n) Source #

KnownSymbol str => SingParser (Literal str :: PParser Symbol ()) Source # 
Instance details

Defined in Symparsec.Parser.Literal

Associated Types

type PS (Literal str) :: s -> Type Source #

type PR (Literal str) :: r -> Type Source #

Methods

singParser' :: SParser (PS (Literal str)) (PR (Literal str)) (Literal str) Source #

(KnownNat base, SingParseDigit parseDigit) => SingParser (NatBase base parseDigit :: PParser (Maybe Natural) Natural) Source # 
Instance details

Defined in Symparsec.Parser.Natural

Associated Types

type PS (NatBase base parseDigit) :: s -> Type Source #

type PR (NatBase base parseDigit) :: r -> Type Source #

Methods

singParser' :: SParser (PS (NatBase base parseDigit)) (PR (NatBase base parseDigit)) (NatBase base parseDigit) Source #

(p ~ 'PParser pCh pEnd s0, KnownNat n, SingParser p) => SingParser (Isolate' n pCh pEnd s0 :: PParser (Natural, s) r) Source # 
Instance details

Defined in Symparsec.Parser.Isolate

Associated Types

type PS (Isolate' n pCh pEnd s0) :: s -> Type Source #

type PR (Isolate' n pCh pEnd s0) :: r -> Type Source #

Methods

singParser' :: SParser (PS (Isolate' n pCh pEnd s0)) (PR (Isolate' n pCh pEnd s0)) (Isolate' n pCh pEnd s0) Source #

(pl ~ 'PParser plCh plEnd s0l, pr ~ 'PParser prCh prEnd s0r, SingParser pl, SingParser pr) => SingParser (ThenVR' plCh plEnd s0l prCh prEnd s0r :: PParser (Either sl (r, sr)) r) Source # 
Instance details

Defined in Symparsec.Parser.Then.VoidRight

Associated Types

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

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

Methods

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

(pl ~ 'PParser plCh plEnd s0l, pr ~ 'PParser prCh prEnd s0r, SingParser pl, SingParser pr) => SingParser (ThenVL' plCh plEnd s0l prCh prEnd s0r :: PParser (Either sl sr) r) Source # 
Instance details

Defined in Symparsec.Parser.Then.VoidLeft

Associated Types

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

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

Methods

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

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

Defined in Symparsec.Parser.Then

Associated Types

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

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

Methods

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

singParser :: forall {s} {r} (p :: PParser s r). SingParser p => SParser (PS p) (PR p) p Source #

Get the singled version of the requested parser.

singParser' with better type application ergonomics.