plutus-core-1.60.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 #

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

pType :: Parser PType #

Parser for PType.

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

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)) #

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 #

Constructors

ParserState 

getVersion :: Parser (Maybe Version) #

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

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

Set the version of the program being parsed.

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

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.

parse :: (Text -> m a #

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

whitespace :: Parser () #

Space consumer.

reachOffsetNoLine' #

Arguments

:: forall s. Token s -> Pos)

Offset to reach | Increment in column position for a token

-> Text m => m SourcePos #

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

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 #

Like withSpan', but the result parser consumes whitespaces.

withSpan = (<* whitespace) . withSpan'

lexeme :: Parser a -> Parser a #

symbol :: Text #

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 () #

Parser for unit.

conBool :: Parser Text -> m (Program TyName Name DefaultUni DefaultFun SrcSpan) #

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 :: (Text -> m (Term TyName Name DefaultUni DefaultFun SrcSpan) #

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

parseType :: (Text -> m (Type TyName DefaultUni SrcSpan) #

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

data SourcePos #

The data type SourcePos represents source positions. It contains the name of the source file, a line number, and a column number. Source line and column positions change intensively during parsing, so we need to make them strict to avoid memory leaks.