packages feed

phino-0.0.153 / src / Condition.hs

{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE RecordWildCards #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Condition (parseCondition, parseConditionThrows) whereimport AST (Attribute (AtDelta, AtLambda))import Control.Exception (Exception)import Data.Void (Void)import Misc (orThrow)import Parser (PhiParser (..), phiParser)import Text.Megaparsecimport Text.Megaparsec.Charimport qualified Text.Megaparsec.Char.Lexer as Limport Text.Printf (printf)import qualified Yaml as Ynewtype ConditionException = CouldNotParseCondition {message :: String}  deriving (Exception)instance Show ConditionException where  show CouldNotParseCondition{..} = printf "Couldn't parse given condition, cause: %s" messagetype Parser = Parsec Void StringwhiteSpace :: Parser ()whiteSpace = L.space hspace1 empty emptylexeme :: Parser a -> Parser alexeme = L.lexeme whiteSpacesymbol :: String -> Parser Stringsymbol = L.symbol whiteSpacelparen :: Parser Stringlparen = symbol "("rparen :: Parser Stringrparen = symbol ")"comma :: Parser Stringcomma = symbol ","several :: Parser a -> Parser [a]several item = choice [try (between (symbol "[") (symbol "]") (item `sepBy1` comma)), pure <$> item]number :: Parser Y.Numbernumber =  choice    [ do        _ <- symbol "length" >> lparen        bd <- _binding phiParser        _ <- rparen        return (Y.Length bd)    , do        _ <- symbol "domain" >> lparen        bd <- _binding phiParser        _ <- rparen        return (Y.Domain bd)    , either Y.AnyIndex Y.MetaIndex <$> _index phiParser    , do        sign <- optional (choice [char '-', char '+'])        unsigned <- lexeme L.decimal        let signed = if sign == Just '-' then negate unsigned else unsigned :: Integer        if signed < toInteger (minBound :: Int) || signed > toInteger (maxBound :: Int)          then fail (printf "the literal %d does not fit into Int" signed)          else return (Y.Literal (fromInteger signed))    ]comparable :: Parser Y.Comparablecomparable =  choice    [ try $ Y.CmpNum <$> number    , try $ Y.CmpAttr <$> _attribute phiParser    , Y.CmpExpr <$> _expression phiParser    ]condition :: Parser Y.Conditioncondition =  choice    [ do        _ <- symbol "and" >> lparen        args <- condition `sepBy1` comma        _ <- rparen        return (Y.And args)    , do        _ <- symbol "or" >> lparen        args <- condition `sepBy1` comma        _ <- rparen        return (Y.Or args)    , do        _ <- symbol "in" >> lparen        attrs <- several (choice [AtLambda <$ symbol "λ", AtDelta <$ symbol "Δ", _attribute phiParser])        _ <- comma        bds <- several (_binding phiParser)        _ <- rparen        return (Y.In attrs bds)    , do        _ <- symbol "not" >> lparen        cond <- condition        _ <- rparen        return (Y.Not cond)    , do        _ <- symbol "eq" >> lparen        left <- comparable        _ <- comma        right <- comparable        _ <- rparen        return (Y.Eq left right)    , do        _ <- symbol "gt" >> lparen        left <- comparable        _ <- comma        right <- comparable        _ <- rparen        return (Y.Gt left right)    , do        _ <- symbol "nf" >> lparen        expr <- _expression phiParser        _ <- rparen        return (Y.NF expr)    , do        _ <- symbol "absolute" >> lparen        expr <- _expression phiParser        _ <- rparen        return (Y.Absolute expr)    , do        _ <- symbol "matches" >> lparen        ptn <- _string phiParser        _ <- comma        expr <- _expression phiParser        _ <- rparen        return (Y.Matches ptn expr)    , do        _ <- symbol "part-of" >> lparen        expr <- _expression phiParser        _ <- comma        bd <- _binding phiParser        _ <- rparen        return (Y.PartOf expr bd)    , do        _ <- symbol "formation" >> lparen        expr <- _expression phiParser        _ <- rparen        return (Y.IsFormation expr)    ]parseCondition :: String -> Either String Y.ConditionparseCondition input = do  let parsed =        runParser          ( do              _ <- whiteSpace              p <- condition              _ <- eof              return p          )          "condition"          input  case parsed of    Right parsed' -> Right parsed'    Left err -> Left (errorBundlePretty err)parseConditionThrows :: String -> IO Y.ConditionparseConditionThrows cnd = orThrow CouldNotParseCondition (parseCondition cnd)