{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE DuplicateRecordFields #-}{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule XMIRSpec whereimport ASTimport Control.Monad (forM_, unless)import Data.Aesonimport Data.Char (isDigit)import Data.List (intercalate)import Data.Text qualified as Timport Data.Yaml qualified as Yamlimport GHC.Generics (Generic)import Misc (allPathsIn)import Parser (parseExpressionThrows, parseProgramThrows)import System.FilePath (makeRelative)import Test.Hspec (Spec, anyException, describe, expectationFailure, it, runIO, shouldBe, shouldThrow)import Text.XML (Document (..), Element (..))import Text.XML.Cursor qualified as Cimport XMIR (defaultXmirContext, parseXMIRThrows, printXMIR, programToXMIR, toName, xmirToPhi)data ParsePack = ParsePack { failure :: Maybe Bool , xmir :: String , phi :: String } deriving (Generic, Show, FromJSON)data PrintPack = PrintPack { phi :: String , xpaths :: [String] } deriving (Generic, Show, FromJSON)parsePack :: FilePath -> IO ParsePackparsePack = Yaml.decodeFileThrowprintPack :: FilePath -> IO PrintPackprintPack = Yaml.decodeFileThrow-- | An XPath predicate that filters cursors.data Predicate = AttrEquals String String | ChildText String String | ChildExists String [Predicate] | PositionIs Int | AndPred Predicate Predicate deriving (Show)-- | An XPath step with element name and predicates.data Step = Step String [Predicate] deriving (Show){- | Parse a simple XPath expression into steps.Supports: /element/element[@attr="val" and child="val" and child[N][@attr="val"]]-}xpath :: String -> [Step]xpath ('/' : rest) = steps restxpath _ = []steps :: String -> [Step]steps "" = []steps str = let (step, rest) = span (\c -> c /= '/' && c /= '[') str (preds, remaining) = parsePredicate rest in Step step preds : steps (dropWhile (== '/') remaining)parsePredicate :: String -> ([Predicate], String)parsePredicate ('[' : rest) = let (inner, after) = splitBracket rest pred' = parsePredicateInner inner (more, final) = parsePredicate after in (pred' : more, final)parsePredicate str = ([], str)splitBracket :: String -> (String, String)splitBracket = go (0 :: Int) "" where go :: Int -> String -> String -> (String, String) go _ acc "" = (reverse acc, "") go 0 acc (']' : rest) = (reverse acc, rest) go n acc ('[' : rest) = go (n + 1) ('[' : acc) rest go n acc (']' : rest) = go (n - 1) (']' : acc) rest go n acc (c : rest) = go n (c : acc) restparsePredicateInner :: String -> PredicateparsePredicateInner str | " and " `isInfixOf'` str = let parts = splitAnd str in foldr1 AndPred (map parsePredicateInner parts) | all isDigit str = PositionIs (read str) | '@' : rest <- str = parseAttrPred rest | otherwise = parseChildPred str where isInfixOf' needle haystack = needle `elem` tails haystack tails [] = [[]] tails s@(_ : xs) = s : tails xssplitAnd :: String -> [String]splitAnd = go (0 :: Int) "" where go :: Int -> String -> String -> [String] go _ acc "" = [reverse acc | not (null acc)] go n acc ('[' : rest) = go (n + 1) ('[' : acc) rest go n acc (']' : rest) = go (n - 1) (']' : acc) rest go 0 acc (' ' : 'a' : 'n' : 'd' : ' ' : rest) = reverse acc : go 0 "" rest go n acc (c : rest) = go n (c : acc) restparseAttrPred :: String -> PredicateparseAttrPred str = let (name, rest) = break (== '=') str val = extractQuoted (drop 1 rest) in AttrEquals name valparseChildPred :: String -> PredicateparseChildPred str | '[' `elem` str = let (name, rest) = break (== '[') str (preds, _) = parsePredicate rest in ChildExists name preds | '=' `elem` str = let (name, rest) = break (== '=') str val = extractQuoted (drop 1 rest) in ChildText name val | otherwise = ChildExists str []extractQuoted :: String -> StringextractQuoted (q : rest) | q == '"' || q == '\'' = takeWhile (/= q) restextractQuoted s = s{- | Evaluate an XPath expression on a document, returning matched cursors.Note: fromDocument returns cursor at root element, so first step must match root.-}evaluate :: Document -> [Step] -> [C.Cursor]evaluate doc [] = [C.fromDocument doc]evaluate doc (Step name preds : rest) = let root = C.fromDocument doc rootName = elementName (documentRoot doc) in if rootName == toName name then foldl applyStep (applyPredicates [root] preds) rest else []applyStep :: [C.Cursor] -> Step -> [C.Cursor]applyStep curs (Step name preds) = do cur <- curs child <- cur C.$/ C.element (toName name) applyPredicates [child] predsapplyPredicates :: [C.Cursor] -> [Predicate] -> [C.Cursor]applyPredicates = foldl applyPredicateapplyPredicate :: [C.Cursor] -> Predicate -> [C.Cursor]applyPredicate curs pred' = case pred' of AttrEquals name val -> filter (hasAttrValue name val) curs ChildText name val -> filter (hasChildText name val) curs ChildExists name nested -> filter (hasChild name nested) curs PositionIs n -> take 1 (drop (n - 1) curs) AndPred p1 p2 -> applyPredicate (applyPredicate curs p1) p2hasAttrValue :: String -> String -> C.Cursor -> BoolhasAttrValue name val cur = C.attribute (toName name) cur == [T.pack val]hasChildText :: String -> String -> C.Cursor -> BoolhasChildText name val cur = let children = cur C.$/ C.element (toName name) in any (hasTextContent val) childrenhasTextContent :: String -> C.Cursor -> BoolhasTextContent val cur = let txt = concatMap T.unpack (cur C.$/ C.content) in txt == valhasChild :: String -> [Predicate] -> C.Cursor -> BoolhasChild name nested cur = let children = cur C.$/ C.element (toName name) in not (null (applyPredicates children nested))-- | Check if an XPath expression matches anything in the document.matches :: Document -> String -> Boolmatches doc path = not (null (evaluate doc (xpath path)))spec :: Specspec = do describe "XMIR parsing packs" $ do let resources = "test-resources/xmir-parsing-packs" packs <- runIO (allPathsIn resources) forM_ packs ( \pth -> it (makeRelative resources pth) $ do pack <- parsePack pth let ParsePack{phi = phi'} = pack xmir' = do doc <- parseXMIRThrows (xmir pack) xmirToPhi doc case failure pack of Just True -> xmir' `shouldThrow` anyException _ -> do xmir'' <- xmir' phi'' <- parseProgramThrows phi' xmir'' `shouldBe` phi'' ) describe "prohibit to convert to XMIR" $ forM_ [ "[[ ]]" , "T" , "[[ x -> ? ]]" , "[[ ^ -> 5 ]]" , "Q.x.y.z" , "\"Hello\"" , "Q" , "$" ] ( \phi' -> it phi' $ do expr <- parseExpressionThrows phi' programToXMIR (Program expr) defaultXmirContext `shouldThrow` anyException ) describe "XMIR printing packs" $ do let resources = "test-resources/xmir-printing-packs" packs <- runIO (allPathsIn resources) forM_ packs ( \pth -> it (makeRelative resources pth) $ do pack <- printPack pth let PrintPack{phi = phi', xpaths = xpaths'} = pack prog <- parseProgramThrows phi' xmir' <- programToXMIR prog defaultXmirContext let failed = filter (not . matches xmir') xpaths' unless (null failed) (expectationFailure ("Failed xpaths:\n - " ++ intercalate "\n - " failed ++ "\nXMIR is:\n" ++ printXMIR xmir')) )