| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
PlutusCore.Parser
Description
Parsers for PLC terms in DefaultUni.
Synopsis
- name :: Parser Name
- parse :: (MonadError ParserErrorBundle m, MonadQuote m) => Parser a -> String -> Text -> m a
- type Parser = ParsecT ParserError Text (StateT ParserState (ReaderT (Maybe Version) Quote))
- lexeme :: Parser a -> Parser a
- symbol :: Text -> Parser Text
- version :: Parser Version
- inBraces :: Parser a -> Parser a
- defaultUni :: Parser (SomeTypeIn (Kinded DefaultUni))
- cachedBuiltin :: Map Text DefaultFun
- type PType = Type TyName DefaultUni SrcSpan
- initial :: ParserState
- constant :: Parser (Some (ValueOf DefaultUni))
- kind :: Parser (Kind SrcSpan)
- tyName :: Parser TyName
- newtype ParserState = ParserState {
- identifiers :: Map Text Unique
- getVersion :: Parser (Maybe Version)
- withVersion :: Version -> Parser a -> Parser a
- whenVersion :: (Version -> Bool) -> Parser () -> Parser ()
- toErrorB :: Either (ParseErrorBundle Text ParserError) a -> Either ParserErrorBundle a
- parseGen :: (MonadError ParserErrorBundle m, MonadQuote m) => Parser a -> Text -> m a
- whitespace :: Parser ()
- leadingWhitespace :: Parser a -> Parser a
- trailingWhitespace :: Parser a -> Parser a
- reachOffsetNoLine' :: Stream s => (Int -> s -> (Tokens s, s)) -> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b) -> (Token s, Token s) -> (Token s -> Pos) -> Int -> PosState s -> PosState s
- getSourcePos' :: MonadParsec e Text m => m SourcePos
- withSpan' :: (SrcSpan -> Parser a) -> Parser a
- toSrcSpan :: SourcePos -> SourcePos -> SrcSpan
- withSpan :: (SrcSpan -> Parser a) -> Parser a
- inParens :: Parser a -> Parser a
- inBrackets :: Parser a -> Parser a
- varType :: Parser PType
- funType :: Parser PType
- pType :: Parser PType
- allType :: Parser PType
- lamType :: Parser PType
- ifixType :: Parser PType
- builtinType :: Parser PType
- sopType :: Parser PType
- appType :: Parser PType
- defaultUniApplication :: Parser (SomeTypeIn (Kinded DefaultUni))
- builtinFunction :: Parser DefaultFun
- conInteger :: Parser Integer
- hexByte :: Parser Word8
- conBS :: Parser ByteString
- conText :: Parser Text
- conUnit :: Parser ()
- conBool :: Parser Bool
- conList :: DefaultUni (Esc a) -> Parser [a]
- constantOf :: ExpectParens -> DefaultUni (Esc a) -> Parser a
- data ExpectParens
- conArray :: DefaultUni (Esc a) -> Parser (Vector a)
- conValue :: Parser Value
- conPair :: DefaultUni (Esc a) -> DefaultUni (Esc b) -> Parser (a, b)
- conDataNoParens :: Parser Data
- conData :: ExpectParens -> Parser Data
- con0xBS :: Parser ByteString
- conBLS12_381_G1_Element :: Parser Element
- conBLS12_381_G2_Element :: Parser Element
- program :: Parser (Program TyName Name DefaultUni DefaultFun SrcSpan)
- parseProgram :: (MonadError ParserErrorBundle m, MonadQuote m) => Text -> m (Program TyName Name DefaultUni DefaultFun SrcSpan)
- parseTerm :: (MonadError ParserErrorBundle m, MonadQuote m) => Text -> m (Term TyName Name DefaultUni DefaultFun SrcSpan)
- parseType :: (MonadError ParserErrorBundle m, MonadQuote m) => Text -> m (Type TyName DefaultUni SrcSpan)
- data SourcePos
- data ParserError
Documentation
parse :: (MonadError ParserErrorBundle m, MonadQuote m) => Parser a -> String -> Text -> m a Source #
type Parser = ParsecT ParserError Text (StateT ParserState (ReaderT (Maybe Version) Quote)) Source #
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 | |
Fields
| |
Instances
| Show ParserState Source # | |
Defined in PlutusCore.Parser.ParserCommon | |
getVersion :: Parser (Maybe Version) Source #
Get the version of the program being parsed, if we know it.
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.
toErrorB :: Either (ParseErrorBundle Text ParserError) a -> Either ParserErrorBundle a Source #
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.
leadingWhitespace :: Parser a -> Parser a Source #
trailingWhitespace :: Parser a -> Parser a 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 s | Updated |
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'
inBrackets :: Parser a -> Parser a Source #
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.
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.
constantOf :: ExpectParens -> DefaultUni (Esc a) -> Parser a Source #
Parser for constants of the given type.
data ExpectParens Source #
Constructors
| ExpectParensYes | |
| ExpectParensNo |
conPair :: DefaultUni (Esc a) -> DefaultUni (Esc b) -> Parser (a, b) Source #
Parser for pairs.
program :: Parser (Program TyName Name DefaultUni DefaultFun SrcSpan) Source #
Parser for PLC programs.
parseProgram :: (MonadError ParserErrorBundle m, MonadQuote m) => Text -> m (Program TyName Name DefaultUni DefaultFun SrcSpan) Source #
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.
Instances
data ParserError Source #
An error encountered during parsing.
Constructors
| BuiltinTypeNotAStar !Text !SourcePos | |
| UnknownBuiltinFunction !Text !SourcePos ![Text] | |
| InvalidBuiltinConstant !Text !Text !SourcePos |
Instances
| Generic ParserError Source # | |||||
Defined in PlutusCore.Error Associated Types
Methods from :: ParserError -> Rep ParserError x Source # to :: Rep ParserError x -> ParserError Source # | |||||
| Show ParserError Source # | |||||
Defined in PlutusCore.Error | |||||
| NFData ParserError Source # | |||||
Defined in PlutusCore.Error Methods rnf :: ParserError -> () Source # | |||||
| Eq ParserError Source # | |||||
Defined in PlutusCore.Error Methods (==) :: ParserError -> ParserError -> Bool Source # (/=) :: ParserError -> ParserError -> Bool Source # | |||||
| Ord ParserError Source # | |||||
Defined in PlutusCore.Error Methods compare :: ParserError -> ParserError -> Ordering Source # (<) :: ParserError -> ParserError -> Bool Source # (<=) :: ParserError -> ParserError -> Bool Source # (>) :: ParserError -> ParserError -> Bool Source # (>=) :: ParserError -> ParserError -> Bool Source # max :: ParserError -> ParserError -> ParserError Source # min :: ParserError -> ParserError -> ParserError Source # | |||||
| ShowErrorComponent ParserError Source # | |||||
Defined in PlutusCore.Error | |||||
| Pretty ParserError Source # | |||||
Defined in PlutusCore.Error | |||||
| type Rep ParserError Source # | |||||
Defined in PlutusCore.Error type Rep ParserError = D1 ('MetaData "ParserError" "PlutusCore.Error" "plutus-core-1.60.0.0-LXFqBsoUlXsJIEqLytHte7" 'False) (C1 ('MetaCons "BuiltinTypeNotAStar" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 SourcePos)) :+: (C1 ('MetaCons "UnknownBuiltinFunction" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 SourcePos) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [Text]))) :+: C1 ('MetaCons "InvalidBuiltinConstant" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 SourcePos))))) | |||||