Skip to content

Commit

Permalink
ValueParser: prefix internal functions that return a Parser with 'parse'
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Nov 6, 2024
1 parent 122be9f commit 9eec32e
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions cardano-api/internal/Cardano/Api/ValueParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,32 @@ data ValueExpr

parseValueExpr :: Parser ValueExpr
parseValueExpr =
buildExpressionParser operatorTable valueExprTerm
buildExpressionParser operatorTable parseValueExprTerm
<?> "multi-asset value expression"
where
operatorTable =
[ [Prefix negateOp]
, [Infix plusOp AssocLeft]
[ [Prefix parseNegateOp]
, [Infix parsePlusOp AssocLeft]
]

-- | Parse either a 'ValueExprLovelace' or 'ValueExprMultiAsset'.
valueExprTerm :: Parser ValueExpr
valueExprTerm = do
q <- try quantity <?> "quantity (word64)"
aId <- try assetIdUnspecified <|> assetIdSpecified <?> "asset id"
parseValueExprTerm :: Parser ValueExpr
parseValueExprTerm = do
q <- try parseQuantity <?> "quantity (word64)"
aId <- try parseAssetIdUnspecified <|> parseAssetIdSpecified <?> "asset id"
_ <- spaces
pure $ case aId of
AdaAssetId -> ValueExprLovelace q
AssetId polId aName -> ValueExprMultiAsset polId aName q
where
-- Parse an asset ID which must be lead by one or more whitespace
-- characters and may be trailed by whitespace characters.
assetIdSpecified :: Parser AssetId
assetIdSpecified = some space *> assetId
parseAssetIdSpecified :: Parser AssetId
parseAssetIdSpecified = some space *> parseAssetId

-- Default for if an asset ID is not specified.
assetIdUnspecified :: Parser AssetId
assetIdUnspecified =
parseAssetIdUnspecified :: Parser AssetId
parseAssetIdUnspecified =
spaces
*> notFollowedBy alphaNum
$> AdaAssetId
Expand All @@ -128,28 +128,28 @@ valueExprTerm = do
-- Primitive parsers
------------------------------------------------------------------------------

plusOp :: Parser (ValueExpr -> ValueExpr -> ValueExpr)
plusOp = (char '+' *> spaces) $> ValueExprAdd
parsePlusOp :: Parser (ValueExpr -> ValueExpr -> ValueExpr)
parsePlusOp = (char '+' *> spaces) $> ValueExprAdd

negateOp :: Parser (ValueExpr -> ValueExpr)
negateOp = (char '-' *> spaces) $> ValueExprNegate
parseNegateOp :: Parser (ValueExpr -> ValueExpr)
parseNegateOp = (char '-' *> spaces) $> ValueExprNegate

-- | Period (\".\") parser.
period :: Parser ()
period = void $ char '.'
parsePeriod :: Parser ()
parsePeriod = void $ char '.'

-- | Word64 parser.
word64 :: Parser Integer
word64 = do
i <- decimal
parseWord64 :: Parser Integer
parseWord64 = do
i <- parseDecimal
if i > fromIntegral (maxBound :: Word64)
then
fail $
"expecting word64, but the number exceeds the max bound: " <> show i
else return i

decimal :: Parser Integer
decimal = do
parseDecimal :: Parser Integer
parseDecimal = do
digits <- many1 digit
return $! List.foldl' (\x d -> 10 * x + toInteger (Char.digitToInt d)) 0 digits

Expand Down Expand Up @@ -183,34 +183,34 @@ parsePolicyId = do
. Text.pack

-- | Asset ID parser.
assetId :: Parser AssetId
assetId =
try adaAssetId
<|> nonAdaAssetId
parseAssetId :: Parser AssetId
parseAssetId =
try parseAdaAssetId
<|> parseNonAdaAssetId
<?> "asset ID"
where
-- Parse the ADA asset ID.
adaAssetId :: Parser AssetId
adaAssetId = string "lovelace" $> AdaAssetId
parseAdaAssetId :: Parser AssetId
parseAdaAssetId = string "lovelace" $> AdaAssetId

-- Parse a multi-asset ID.
nonAdaAssetId :: Parser AssetId
nonAdaAssetId = do
parseNonAdaAssetId :: Parser AssetId
parseNonAdaAssetId = do
polId <- parsePolicyId
fullAssetId polId <|> assetIdNoAssetName polId
parseFullAssetId polId <|> parseAssetIdNoAssetName polId

-- Parse a fully specified multi-asset ID with both a policy ID and asset
-- name.
fullAssetId :: PolicyId -> Parser AssetId
fullAssetId polId = do
_ <- period
parseFullAssetId :: PolicyId -> Parser AssetId
parseFullAssetId polId = do
_ <- parsePeriod
aName <- parseAssetName <?> "hexadecimal asset name"
pure (AssetId polId aName)

-- Parse a multi-asset ID that specifies a policy ID, but no asset name.
assetIdNoAssetName :: PolicyId -> Parser AssetId
assetIdNoAssetName polId = pure (AssetId polId "")
parseAssetIdNoAssetName :: PolicyId -> Parser AssetId
parseAssetIdNoAssetName polId = pure (AssetId polId "")

-- | Quantity (word64) parser. Only accepts positive quantities.
quantity :: Parser Quantity
quantity = fmap Quantity word64
parseQuantity :: Parser Quantity
parseQuantity = fmap Quantity parseWord64

0 comments on commit 9eec32e

Please sign in to comment.