plutus-core-1.36.0.0: Language library for Plutus Core
Safe HaskellSafe-Inferred
LanguageHaskell2010

PlutusCore.Parser

Description

Parsers for PLC terms in DefaultUni.

Synopsis

Documentation

type PType = Type TyName DefaultUni SrcSpan Source #

A PLC Type to be parsed. ATM the parser only works for types in the DefaultUni with DefaultFun.

pType :: Parser PType Source #

Parser for PType.

defaultUniApplication :: Parser (SomeTypeIn (Kinded DefaultUni)) Source #

Parser for built-in type applications. The textual names here should match the ones in the PrettyBy instance for DefaultUni in PlutusCore.Default.Universe.

defaultUni :: Parser (SomeTypeIn (Kinded DefaultUni)) Source #

Parser for built-in types (the ones from DefaultUni specifically).

Kinded is needed for checking that a type function can be applied to its argument. I.e. we do Plutus kind checking of builtin type applications during parsing, which is unfortunate, but there's no way we could construct a DefaultUni otherwise.

In case of kind error no sensible message is shown, only an overly general one:

>>> :set -XTypeApplications
>>> :set -XOverloadedStrings
>>> import PlutusCore.Error
>>> import PlutusCore.Quote
>>> let runP = putStrLn . either display display . runQuoteT . parseGen @ParserErrorBundle defaultUni
>>> runP "(list integer)"
(list integer)
>>> runP "(bool integer)"
test:1:14:
  |
1 | (bool integer)
  |              ^
expecting "bool", "bytestring", "data", "integer", "list", "pair", "string", "unit", or '('

This is to be fixed.

One thing we could do to avoid doing kind checking during parsing is to parse into

data TextualUni a where TextualUni :: TextualUni (Esc (Tree Text))

i.e. parse into Tree Text and do the kind checking afterwards, but given that we'll still need to do the kind checking of builtins regardless (even for UPLC), we don't win much by deferring doing it.

newtype ParserState Source #

Constructors

ParserState 

Instances

Instances details
Show ParserState Source # 
Instance details

Defined in PlutusCore.Parser.ParserCommon

getVersion :: Parser (Maybe Version) Source #

Get the version of the program being parsed, if we know it.

withVersion :: Version -> Parser a -> Parser a Source #

Set the version of the program being parsed.

whenVersion :: (Version -> Bool) -> Parser () -> Parser () Source #

Run an action conditionally based on a predicate on the version. If we don't know the version then the predicate is assumed to be false, i.e. we act if we _know_ the predicate is satisfied.

parseGen :: (AsParserErrorBundle e, MonadError e m, MonadQuote m) => Parser a -> Text -> m a Source #

Generic parser function in which the file path is just "test".

whitespace :: Parser () Source #

Space consumer.

withSpan' :: (SrcSpan -> Parser a) -> Parser a Source #

Returns a parser for a by calling the supplied function on the starting and ending positions of a.

The supplied function should usually return a parser that does not consume trailing whitespaces. Otherwise, the end position will be the first character after the trailing whitespaces.

withSpan :: (SrcSpan -> Parser a) -> Parser a Source #

Like withSpan', but the result parser consumes whitespaces.

@withSpan = (<* whitespace) . withSpan'

name :: Parser Name Source #

Parses a Name. Does not consume leading or trailing whitespaces.

builtinFunction :: Parser DefaultFun Source #

Parser for builtin functions. Atm the parser can only parse DefaultFun.

conInteger :: Parser Integer Source #

Parser for integer constants.

hexByte :: Parser Word8 Source #

Parser for a pair of hex digits to a Word8.

conBS :: Parser ByteString Source #

Parser for bytestring constants. They start with "#".

conText :: Parser Text Source #

Parser for string constants (wrapped in double quotes). Note that Data.Text.pack "performs replacement on invalid scalar values", which means that Unicode surrogate code points (corresponding to integers in the range 0xD800-0xDFFF) are converted to the Unicode replacement character U+FFFD (decimal 65533). Thus `(con string "XxD800Z")` parses to a Text object whose second character is U+FFFD.

conUnit :: Parser () Source #

Parser for unit.

conBool :: Parser Bool Source #

Parser for bool.

conList :: DefaultUni (Esc a) -> Parser [a] Source #

Parser for lists.

conPair :: DefaultUni (Esc a) -> DefaultUni (Esc b) -> Parser (a, b) Source #

Parser for pairs.

constantOf :: ExpectParens -> DefaultUni (Esc a) -> Parser a Source #

Parser for constants of the given type.

constant :: Parser (Some (ValueOf DefaultUni)) Source #

Parser of constants whose type is in DefaultUni.

parseProgram :: (AsParserErrorBundle e, MonadError e m, MonadQuote m) => Text -> m (Program TyName Name DefaultUni DefaultFun SrcSpan) Source #

Parse a PLC program. The resulting program will have fresh names. The underlying monad must be capable of handling any parse errors. This passes "test" to the parser as the name of the input stream; to supply a name explicity, use `parse program name input`.

parseTerm :: (AsParserErrorBundle e, MonadError e m, MonadQuote m) => Text -> m (Term TyName Name DefaultUni DefaultFun SrcSpan) Source #

Parse a PLC term. The resulting program will have fresh names. The underlying monad must be capable of handling any parse errors.

parseType :: (AsParserErrorBundle e, MonadError e m, MonadQuote m) => Text -> m (Type TyName DefaultUni SrcSpan) Source #

Parse a PLC type. The resulting program will have fresh names. The underlying monad must be capable of handling any parse errors.

data SourcePos #

Instances

Instances details
Data SourcePos 
Instance details

Defined in Text.Megaparsec.Pos

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> SourcePos -> c SourcePos Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c SourcePos Source #

toConstr :: SourcePos -> Constr Source #

dataTypeOf :: SourcePos -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c SourcePos) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SourcePos) Source #

gmapT :: (forall b. Data b => b -> b) -> SourcePos -> SourcePos Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> SourcePos -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> SourcePos -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> SourcePos -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> SourcePos -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos Source #

Generic SourcePos 
Instance details

Defined in Text.Megaparsec.Pos

Associated Types

type Rep SourcePos :: Type -> Type Source #

Read SourcePos 
Instance details

Defined in Text.Megaparsec.Pos

Show SourcePos 
Instance details

Defined in Text.Megaparsec.Pos

NFData SourcePos 
Instance details

Defined in Text.Megaparsec.Pos

Methods

rnf :: SourcePos -> () Source #

Eq SourcePos 
Instance details

Defined in Text.Megaparsec.Pos

Ord SourcePos 
Instance details

Defined in Text.Megaparsec.Pos

Pretty SourcePos Source # 
Instance details

Defined in PlutusCore.Error

Methods

pretty :: SourcePos -> Doc ann #

prettyList :: [SourcePos] -> Doc ann #

type Rep SourcePos 
Instance details

Defined in Text.Megaparsec.Pos

type Rep SourcePos = D1 ('MetaData "SourcePos" "Text.Megaparsec.Pos" "megaparsec-9.6.1-2y4TSvMYdIdIGL4Xdwvkcx" 'False) (C1 ('MetaCons "SourcePos" 'PrefixI 'True) (S1 ('MetaSel ('Just "sourceName") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 FilePath) :*: (S1 ('MetaSel ('Just "sourceLine") 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 Pos) :*: S1 ('MetaSel ('Just "sourceColumn") 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 Pos))))

data ParserError Source #

An error encountered during parsing.

Instances

Instances details
Generic ParserError Source # 
Instance details

Defined in PlutusCore.Error

Associated Types

type Rep ParserError :: Type -> Type Source #

Show ParserError Source # 
Instance details

Defined in PlutusCore.Error

NFData ParserError Source # 
Instance details

Defined in PlutusCore.Error

Methods

rnf :: ParserError -> () Source #

Eq ParserError Source # 
Instance details

Defined in PlutusCore.Error

Ord ParserError Source # 
Instance details

Defined in PlutusCore.Error

ShowErrorComponent ParserError Source # 
Instance details

Defined in PlutusCore.Error

Pretty ParserError Source # 
Instance details

Defined in PlutusCore.Error

Methods

pretty :: ParserError -> Doc ann #

prettyList :: [ParserError] -> Doc ann #

type Rep ParserError Source # 
Instance details

Defined in PlutusCore.Error