Skip to content

Commit

Permalink
restore the ability to parse from astjson output. not sure when we lo…
Browse files Browse the repository at this point in the history
…st this ability! added regression test case so it doesn't happen again. (#610)
  • Loading branch information
ozgurakgun committed Nov 12, 2023
1 parent 2ae9cd7 commit 2ffcff2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/Conjure/UI/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Conjure.UI.IO
, readModelPreambleFromFile
, readModelInfoFromFile
, readParamJSON
, readASTFromFile
, readParamOrSolutionFromFile
, writeModel, writeModels
, readModel
Expand All @@ -18,7 +17,7 @@ import Conjure.UI
import Conjure.Language
import qualified Conjure.Language.Parser as Parser
import qualified Conjure.Language.ParserC as ParserC
import Conjure.Language.Parser
import Conjure.Language.AST.Syntax (ProgramTree)

-- aeson
import qualified Data.Aeson ( eitherDecodeStrict )
Expand All @@ -34,31 +33,23 @@ import qualified Data.Text.Encoding as T ( encodeUtf8 )
-- bytestring
import qualified Data.ByteString as BS ( readFile, writeFile )
import qualified Data.ByteString.Char8 as BS ( putStrLn )
import Conjure.Language.AST.Syntax (ProgramTree)
import Conjure.Language.AST.ASTParser (parseProgram)
import Conjure.Language.Validator (runValidator, validateModel, ValidatorState (typeChecking), initialState, isError)

import Conjure.UI.ErrorDisplay (showDiagnosticsForConsole)

readASTFromFile ::
readASTJSONFromFile ::
MonadIO m =>
MonadFailDoc m =>
MonadUserError m =>
FilePath -> m ProgramTree
readASTFromFile fp = do
(_,contents) <- liftIO $ pairWithContents fp
v <-case lexAndParse parseProgram contents of
Left pe -> failDoc $ pretty $ show pe
Right pt -> return pt
case
runValidator
(validateModel v) (initialState v (Just $ T.pack fp)) {typeChecking = False}
of
(_, vds, _) | any isError vds -> pure v
(_,vds,_) -> failDoc $ "Cannot pretty print a model with errors" <+> pretty (showDiagnosticsForConsole vds (Just fp) contents)
-- MonadFailDoc m =>
-- MonadUserError m =>
FilePath -> m (Either String Model)
readASTJSONFromFile fp = do
(_, contents) <- liftIO $ pairWithContents fp
contents
|> T.encodeUtf8 -- convert Text to ByteString
|> Data.Aeson.eitherDecodeStrict
|> return
-- case rawJSON of
-- Left err -> userErr1 (pretty err)
-- Right m -> return m




readModelFromFile ::
MonadIO m =>
Expand All @@ -70,8 +61,12 @@ readModelFromFile fp = do
case Data.Serialize.decode con of
Right res -> return res
Left _ -> do
pair <- liftIO $ pairWithContents fp
readModel Parser.parseModel (Just id) pair
ast_ <- readASTJSONFromFile fp
case ast_ of
Right res -> return res
Left _ -> do
pair <- liftIO $ pairWithContents fp
readModel Parser.parseModel (Just id) pair


readModelFromStdin ::
Expand Down Expand Up @@ -134,6 +129,7 @@ readModelPreambleFromFile fp = do
pair <- liftIO $ pairWithContents fp
readModel Parser.parseModel (Just onlyPreamble) pair


readModelInfoFromFile ::
MonadIO m =>
MonadFailDoc m =>
Expand Down
10 changes: 10 additions & 0 deletions tests/custom/astjson-roundtrip/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rm -rf conjure-output *.solution sample.json
conjure pretty sample.essence --output-format=astjson > sample.json
conjure solve sample.json
echo "===== 1 ====="
cat sample.essence
echo "===== 2 ====="
conjure pretty sample.json
echo "===== 3 ====="
cat *.solution
rm -rf conjure-output *.solution sample.json
4 changes: 4 additions & 0 deletions tests/custom/astjson-roundtrip/sample.essence
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
find x,y,z : int(1..3)
such that x + y + z = 6
such that x < y

27 changes: 27 additions & 0 deletions tests/custom/astjson-roundtrip/stdout.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Generating models for sample.json
Generated models: model000001.eprime
Saved under: conjure-output
Savile Row: model000001.eprime
Running minion for domain filtering.
Running solver: minion
Copying solution to: sample.solution
===== 1 =====
find x,y,z : int(1..3)
such that x + y + z = 6
such that x < y

===== 2 =====
language Essence 1.3

find x: int(1..3)
find y: int(1..3)
find z: int(1..3)
such that x + y + z = 6
such that x < y

===== 3 =====
language Essence 1.3

letting x be 1
letting y be 2
letting z be 3

0 comments on commit 2ffcff2

Please sign in to comment.