|
| 1 | +module Main (main) where |
| 2 | + |
| 3 | +import GlobalMain |
| 4 | +import Kore.Attribute.Symbol ( |
| 5 | + StepperAttributes, |
| 6 | + ) |
| 7 | +import Kore.BugReport |
| 8 | +import Kore.Exec (matchDisjunction) |
| 9 | +import Kore.IndexedModule.IndexedModule ( |
| 10 | + VerifiedModule, |
| 11 | + ) |
| 12 | +import Kore.Internal.Pattern (Pattern) |
| 13 | +import qualified Kore.Internal.Pattern as Pattern |
| 14 | +import Kore.Internal.TermLike ( |
| 15 | + pattern Or_, |
| 16 | + ) |
| 17 | +import Kore.Log ( |
| 18 | + KoreLogOptions, |
| 19 | + parseKoreLogOptions, |
| 20 | + runKoreLog, |
| 21 | + ) |
| 22 | +import Kore.Rewrite.RewritingVariable ( |
| 23 | + RewritingVariableName, |
| 24 | + mkRewritingPattern, |
| 25 | + ) |
| 26 | +import Kore.Syntax.Module ( |
| 27 | + ModuleName (..), |
| 28 | + ) |
| 29 | +import Kore.Unparser ( |
| 30 | + unparse, |
| 31 | + ) |
| 32 | +import Options.Applicative ( |
| 33 | + InfoMod, |
| 34 | + Parser, |
| 35 | + argument, |
| 36 | + fullDesc, |
| 37 | + header, |
| 38 | + help, |
| 39 | + long, |
| 40 | + metavar, |
| 41 | + progDesc, |
| 42 | + str, |
| 43 | + strOption, |
| 44 | + ) |
| 45 | +import Prelude.Kore |
| 46 | +import Pretty |
| 47 | +import System.Clock ( |
| 48 | + Clock (..), |
| 49 | + TimeSpec, |
| 50 | + getTime, |
| 51 | + ) |
| 52 | +import System.Exit ( |
| 53 | + exitWith, |
| 54 | + ) |
| 55 | +import System.IO ( |
| 56 | + IOMode (WriteMode), |
| 57 | + withFile, |
| 58 | + ) |
| 59 | + |
| 60 | +exeName :: ExeName |
| 61 | +exeName = ExeName "kore-match-disjunction" |
| 62 | + |
| 63 | +envName :: String |
| 64 | +envName = "KORE_MATCH_DISJUNCTION_OPTS" |
| 65 | + |
| 66 | +data KoreMatchDisjunctionOptions = KoreMatchDisjunctionOptions |
| 67 | + { -- | Name of file containing a definition to verify and use for execution |
| 68 | + definitionFileName :: !FilePath |
| 69 | + , -- | Name of file containing a disjunction to verify and use for matching |
| 70 | + disjunctionFileName :: !FilePath |
| 71 | + , -- | Name of file used to match with disjunction |
| 72 | + matchFileName :: !FilePath |
| 73 | + , -- | Name for file to contain the output pattern |
| 74 | + outputFileName :: !(Maybe FilePath) |
| 75 | + , -- | The name of the main module in the definition |
| 76 | + mainModuleName :: !ModuleName |
| 77 | + , bugReportOption :: !BugReportOption |
| 78 | + , koreLogOptions :: !KoreLogOptions |
| 79 | + } |
| 80 | + |
| 81 | +parseKoreMatchDisjunctionOptions :: TimeSpec -> Parser KoreMatchDisjunctionOptions |
| 82 | +parseKoreMatchDisjunctionOptions startTime = |
| 83 | + KoreMatchDisjunctionOptions |
| 84 | + <$> argument |
| 85 | + str |
| 86 | + ( metavar "DEFINITION_FILE" |
| 87 | + <> help "Kore definition file to verify and use for execution." |
| 88 | + ) |
| 89 | + <*> strOption |
| 90 | + ( metavar "DISJUNCTION_FILE" |
| 91 | + <> long "disjunction" |
| 92 | + <> help "File containing a disjunction of concrete terms." |
| 93 | + ) |
| 94 | + <*> strOption |
| 95 | + ( metavar "MATCH_FILE" |
| 96 | + <> long "match" |
| 97 | + <> help "Kore source file representing pattern to search for." |
| 98 | + ) |
| 99 | + <*> optional |
| 100 | + ( strOption |
| 101 | + ( metavar "PATTERN_OUTPUT_FILE" |
| 102 | + <> long "output" |
| 103 | + <> help "Output file to contain final Kore pattern." |
| 104 | + ) |
| 105 | + ) |
| 106 | + <*> parseMainModuleName |
| 107 | + <*> parseBugReportOption |
| 108 | + <*> parseKoreLogOptions exeName startTime |
| 109 | + where |
| 110 | + parseMainModuleName = |
| 111 | + GlobalMain.parseModuleName |
| 112 | + "MODULE" |
| 113 | + "module" |
| 114 | + "The name of the main module in the Kore definition." |
| 115 | + |
| 116 | +parserInfoModifiers :: InfoMod options |
| 117 | +parserInfoModifiers = |
| 118 | + fullDesc |
| 119 | + <> progDesc "Matches Kore pattern in MATCH_FILE with Kore pattern in DISJUNCTION_FILE" |
| 120 | + <> header "kore-match-disjunction - a tool for applying search patterns to disjunctions of configurations" |
| 121 | + |
| 122 | +main :: IO () |
| 123 | +main = do |
| 124 | + startTime <- getTime Monotonic |
| 125 | + options <- |
| 126 | + mainGlobal |
| 127 | + exeName |
| 128 | + (Just envName) |
| 129 | + (parseKoreMatchDisjunctionOptions startTime) |
| 130 | + parserInfoModifiers |
| 131 | + for_ (localOptions options) mainWithOptions |
| 132 | + |
| 133 | +mainWithOptions :: KoreMatchDisjunctionOptions -> IO () |
| 134 | +mainWithOptions options = do |
| 135 | + exitCode <- |
| 136 | + withBugReport exeName bugReportOption $ \tmpDir -> |
| 137 | + koreMatchDisjunction options |
| 138 | + & runKoreLog tmpDir koreLogOptions |
| 139 | + exitWith exitCode |
| 140 | + where |
| 141 | + KoreMatchDisjunctionOptions{bugReportOption} = options |
| 142 | + KoreMatchDisjunctionOptions{koreLogOptions} = options |
| 143 | + |
| 144 | +koreMatchDisjunction :: KoreMatchDisjunctionOptions -> Main ExitCode |
| 145 | +koreMatchDisjunction options = do |
| 146 | + definition <- loadDefinitions [definitionFileName] |
| 147 | + mainModule <- loadModule mainModuleName definition |
| 148 | + matchPattern <- mainParseMatchPattern mainModule matchFileName |
| 149 | + disjunctionPattern <- |
| 150 | + mainParseDisjunctionPattern mainModule disjunctionFileName |
| 151 | + final <- |
| 152 | + clockSomethingIO "Executing" $ |
| 153 | + matchDisjunction mainModule matchPattern disjunctionPattern |
| 154 | + lift $ |
| 155 | + renderResult |
| 156 | + options |
| 157 | + (unparse final) |
| 158 | + return ExitSuccess |
| 159 | + where |
| 160 | + mainParseMatchPattern mainModule fileName = |
| 161 | + mainParseSearchPattern mainModule fileName |
| 162 | + <&> mkRewritingPattern |
| 163 | + KoreMatchDisjunctionOptions |
| 164 | + { definitionFileName |
| 165 | + , disjunctionFileName |
| 166 | + , matchFileName |
| 167 | + , mainModuleName |
| 168 | + } = options |
| 169 | + |
| 170 | +mainParseDisjunctionPattern :: |
| 171 | + VerifiedModule StepperAttributes -> |
| 172 | + String -> |
| 173 | + Main [Pattern RewritingVariableName] |
| 174 | +mainParseDisjunctionPattern indexedModule patternFileName = do |
| 175 | + purePattern <- mainPatternParseAndVerify indexedModule patternFileName |
| 176 | + return $ parseDisjunction purePattern |
| 177 | + where |
| 178 | + parseDisjunction (Or_ _ term1 term2) = |
| 179 | + parseDisjunction term1 <> parseDisjunction term2 |
| 180 | + parseDisjunction term = |
| 181 | + let patt = |
| 182 | + mkRewritingPattern |
| 183 | + . Pattern.fromTermLike |
| 184 | + $ term |
| 185 | + in [patt] |
| 186 | + |
| 187 | +renderResult :: KoreMatchDisjunctionOptions -> Doc ann -> IO () |
| 188 | +renderResult KoreMatchDisjunctionOptions{outputFileName} doc = |
| 189 | + case outputFileName of |
| 190 | + Nothing -> putDoc doc |
| 191 | + Just outputFile -> |
| 192 | + withFile outputFile WriteMode (`hPutDoc` doc) |
0 commit comments