plutus-core
Safe HaskellNone
LanguageHaskell2010

PlutusCore.Parser

Description

Parsers for PLC terms in DefaultUni.

Synopsis

Documentation

name :: Parser Name Source #

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

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.

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.

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

Parser of constants whose type is in DefaultUni.

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 :: (MonadError ParserErrorBundle 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.

reachOffsetNoLine' Source #

Arguments

:: Stream s 
=> (Int -> s -> (Tokens s, s))

How to split input stream at given offset

-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)

How to fold over input stream

-> (Token s, Token s)

Newline token and tab token

-> (Token s -> Pos)

Offset to reach | Increment in column position for a token

-> Int 
-> PosState s

Initial PosState to use

-> PosState s

Updated PosState

getSourcePos' :: MonadParsec e Text m => m SourcePos Source #

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'

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.

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.

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

Parser for constants of the given type.

conArray :: DefaultUni (Esc a) -> Parser (Vector a) Source #

Parser for arrays.

conValue :: Parser Value Source #

Parser for values.

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

Parser for pairs.

parseProgram :: (MonadError ParserErrorBundle 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 :: (MonadError ParserErrorBundle 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 :: (MonadError ParserErrorBundle 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 
Instance details

Defined in Text.Megaparsec.Pos

type Rep SourcePos = D1 ('MetaData "SourcePos" "Text.Megaparsec.Pos" "megaparsec-9.7.0-D1TuX5dPgfP2v9mQdsSy5z" '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))))
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.7.0-D1TuX5dPgfP2v9mQdsSy5z" '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

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