Skip to content

Commit 74b989c

Browse files
committed
Improved prettyprinting, allow manual ordering in Huddle
Generalized tabular prettyprinting Added prettyprinter tests collectFrom now takes HuddleItems
1 parent 57c3af4 commit 74b989c

File tree

24 files changed

+1711
-879
lines changed

24 files changed

+1711
-879
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272

7373
- name: Install fourmolu
7474
run: |
75-
FOURMOLU_VERSION="0.16.2.0"
75+
FOURMOLU_VERSION="0.14.0.0"
7676
mkdir -p "$HOME/.local/bin"
7777
curl -sL "https://github.com/fourmolu/fourmolu/releases/download/v${FOURMOLU_VERSION}/fourmolu-${FOURMOLU_VERSION}-linux-x86_64" -o "$HOME/.local/bin/fourmolu"
7878
chmod a+x "$HOME/.local/bin/fourmolu"

bin/Main.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ run (Opts cmd cddlFile) = do
167167
Left err -> putStrLnErr (show err) >> exitFailure
168168
Right mt -> do
169169
stdGen <- getStdGen
170-
let term = generateCBORTerm mt (Name $ itemName gOpts) stdGen
170+
let term = generateCBORTerm mt (Name (itemName gOpts) mempty) stdGen
171171
in case outputFormat gOpts of
172172
AsTerm -> print term
173173
AsFlatTerm -> print $ toFlatTerm (encodeTerm term)

cuddle.cabal

+9
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,23 @@ library
5050
Codec.CBOR.Cuddle.CDDL.Postlude
5151
Codec.CBOR.Cuddle.CDDL.Prelude
5252
Codec.CBOR.Cuddle.CDDL.Resolve
53+
Codec.CBOR.Cuddle.Comments
5354
Codec.CBOR.Cuddle.Huddle
5455
Codec.CBOR.Cuddle.Huddle.HuddleM
5556
Codec.CBOR.Cuddle.Huddle.Optics
5657
Codec.CBOR.Cuddle.Parser
5758
Codec.CBOR.Cuddle.Parser.Lexer
5859
Codec.CBOR.Cuddle.Pretty
60+
Codec.CBOR.Cuddle.Pretty.Columnar
61+
Codec.CBOR.Cuddle.Pretty.Utils
5962

6063
other-modules:
6164

6265
-- other-extensions:
6366
build-depends:
6467
, base >=4.14.0.0
6568
, base16-bytestring
69+
, boxes
6670
, bytestring
6771
, capability
6872
, cborg
@@ -128,6 +132,7 @@ test-suite cuddle-test
128132
Test.Codec.CBOR.Cuddle.CDDL.Examples
129133
Test.Codec.CBOR.Cuddle.CDDL.Gen
130134
Test.Codec.CBOR.Cuddle.CDDL.Parser
135+
Test.Codec.CBOR.Cuddle.CDDL.Pretty
131136
Test.Codec.CBOR.Cuddle.Huddle
132137

133138
-- other-extensions:
@@ -136,11 +141,15 @@ test-suite cuddle-test
136141
main-is: Main.hs
137142
build-depends:
138143
, base >=4.14.0.0
144+
, bytestring
139145
, cuddle
146+
, data-default-class
140147
, hspec
141148
, hspec-megaparsec
149+
, HUnit
142150
, megaparsec
143151
, prettyprinter
152+
, string-qq
144153
, QuickCheck
145154
, text
146155
, tree-diff

example/Conway.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import GHC.Show (Show (show))
2020
default (Integer, Double)
2121

2222
conway :: Huddle
23-
conway = collectFrom [block]
23+
conway = collectFrom [HIRule block]
2424

2525
block :: Rule
2626
block =

example/Main.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ main = do
6161
case buildMonoCTree =<< buildResolvedCTree (buildRefCTree (asMap res)) of
6262
Left nre -> error $ show nre
6363
Right mt ->
64-
let term = generateCBORTerm mt (Name $ T.pack name) stdGen
64+
let term = generateCBORTerm mt (Name (T.pack name) mempty) stdGen
6565
in print term
6666
[] -> do
6767
let cw = toCDDL conway

example/Monad.hs

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
module Monad where
99

10+
import Codec.CBOR.Cuddle.Comments ((//-))
1011
import Codec.CBOR.Cuddle.Huddle qualified as Huddle
1112
import Codec.CBOR.Cuddle.Huddle.HuddleM
1213
import Data.Word (Word64)
@@ -43,9 +44,9 @@ spec2 =
4344
_transaction <-
4445
"transaction"
4546
=:= mp
46-
[ comment "Transaction inputs" $ idx 0 ==> set txIn
47-
, comment "Transaction outputs" $ idx 1 ==> set txOut
48-
, comment "Metadata" $ idx 2 ==> metadata
47+
[ idx 0 ==> set txIn //- "Transaction inputs"
48+
, idx 1 ==> set txOut //- "Transaction outputs"
49+
, idx 2 ==> metadata //- "Metadata"
4950
]
5051
metadata <- "metadata" =:= VBytes
5152
_value <- "value" =:= mp ["token" ==> VText, "quantity" ==> VUInt]

flake.lock

+22-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nickel.lock.ncl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
organist = import "/nix/store/2r3m9zdvydnrlmh3mvk15m3xddxschgy-source/lib/organist.ncl",
2+
organist = import "/nix/store/7zrf2b1ysrgrx7613qlmbz71cfyxgyfb-source/lib/organist.ncl",
33
}

project.ncl

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ let shellFor = fun ghcver =>
1717
} in
1818

1919
{
20-
shells = organist.shells.Bash,
20+
config = {
21+
shells = organist.shells.Bash,
2122

22-
shells.build = {
23-
packages = {},
24-
},
25-
26-
shells.dev = shellFor "ghc964",
23+
shells.build = {
24+
packages = {},
25+
},
2726

27+
shells.dev = shellFor "ghc964",
28+
}
2829
}
2930
| organist.OrganistExpression

src/Codec/CBOR/Cuddle/CBOR/Gen.hs

+18-15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Codec.CBOR.Cuddle.CDDL (
2121
Name (..),
2222
OccurrenceIndicator (..),
2323
Value (..),
24+
ValueVariant (..),
2425
)
2526
import Codec.CBOR.Cuddle.CDDL.CTree (CTree, CTreeRoot' (..))
2627
import Codec.CBOR.Cuddle.CDDL.CTree qualified as CTree
@@ -38,7 +39,6 @@ import Data.ByteString (ByteString)
3839
import Data.ByteString.Base16 qualified as Base16
3940
import Data.Functor ((<&>))
4041
import Data.Functor.Identity (Identity (runIdentity))
41-
import Data.List (foldl')
4242
import Data.List.NonEmpty qualified as NE
4343
import Data.Map.Strict qualified as Map
4444
import Data.Maybe (fromMaybe)
@@ -155,7 +155,7 @@ genDepthBiasedRM bounds = do
155155
genDepthBiasedBool :: forall g. RandomGen g => M g Bool
156156
genDepthBiasedBool = do
157157
d <- get @"depth"
158-
foldl' (&&) True <$> replicateM d genRandomM
158+
and <$> replicateM d genRandomM
159159

160160
genRandomM :: forall g a. (Random a, RandomGen g) => M g a
161161
genRandomM = asksM @"fakeSeed" randomM
@@ -304,15 +304,15 @@ genForCTree (CTree.Control op target controller) = do
304304
tt <- resolveIfRef target
305305
ct <- resolveIfRef controller
306306
case (op, ct) of
307-
(CtlOp.Le, CTree.Literal (VUInt n)) -> case tt of
307+
(CtlOp.Le, CTree.Literal (Value (VUInt n) _)) -> case tt of
308308
CTree.Postlude PTUInt -> S . TInteger <$> genUniformRM (0, fromIntegral n)
309309
_ -> error "Cannot apply le operator to target"
310310
(CtlOp.Le, _) -> error $ "Invalid controller for .le operator: " <> show controller
311-
(CtlOp.Lt, CTree.Literal (VUInt n)) -> case tt of
311+
(CtlOp.Lt, CTree.Literal (Value (VUInt n) _)) -> case tt of
312312
CTree.Postlude PTUInt -> S . TInteger <$> genUniformRM (0, fromIntegral n - 1)
313313
_ -> error "Cannot apply lt operator to target"
314314
(CtlOp.Lt, _) -> error $ "Invalid controller for .lt operator: " <> show controller
315-
(CtlOp.Size, CTree.Literal (VUInt n)) -> case tt of
315+
(CtlOp.Size, CTree.Literal (Value (VUInt n) _)) -> case tt of
316316
CTree.Postlude PTText -> S . TString <$> genText (fromIntegral n)
317317
CTree.Postlude PTBytes -> S . TBytes <$> genBytes (fromIntegral n)
318318
CTree.Postlude PTUInt -> S . TInteger <$> genUniformRM (0, 2 ^ n - 1)
@@ -321,7 +321,7 @@ genForCTree (CTree.Control op target controller) = do
321321
f <- resolveIfRef from
322322
t <- resolveIfRef to
323323
case (f, t) of
324-
(CTree.Literal (VUInt f1), CTree.Literal (VUInt t1)) -> case tt of
324+
(CTree.Literal (Value (VUInt f1) _), CTree.Literal (Value (VUInt t1) _)) -> case tt of
325325
CTree.Postlude PTText ->
326326
genUniformRM (fromIntegral f1, fromIntegral t1)
327327
>>= (fmap (S . TString) . genText)
@@ -418,17 +418,20 @@ applyOccurenceIndicator (OIBounded mlb mub) oldGen =
418418
>>= \i -> G <$> replicateM (fromIntegral i) oldGen
419419

420420
genValue :: RandomGen g => Value -> M g Term
421-
genValue (VUInt i) = pure . TInt $ fromIntegral i
422-
genValue (VNInt i) = pure . TInt $ fromIntegral (-i)
423-
genValue (VBignum i) = pure $ TInteger i
424-
genValue (VFloat16 i) = pure . THalf $ i
425-
genValue (VFloat32 i) = pure . TFloat $ i
426-
genValue (VFloat64 i) = pure . TDouble $ i
427-
genValue (VText t) = pure $ TString t
428-
genValue (VBytes b) = case Base16.decode b of
421+
genValue (Value x _) = genValueVariant x
422+
423+
genValueVariant :: RandomGen g => ValueVariant -> M g Term
424+
genValueVariant (VUInt i) = pure . TInt $ fromIntegral i
425+
genValueVariant (VNInt i) = pure . TInt $ fromIntegral (-i)
426+
genValueVariant (VBignum i) = pure $ TInteger i
427+
genValueVariant (VFloat16 i) = pure . THalf $ i
428+
genValueVariant (VFloat32 i) = pure . TFloat $ i
429+
genValueVariant (VFloat64 i) = pure . TDouble $ i
430+
genValueVariant (VText t) = pure $ TString t
431+
genValueVariant (VBytes b) = case Base16.decode b of
429432
Right bHex -> pure $ TBytes bHex
430433
Left err -> error $ "Unable to parse hex encoded bytestring: " <> err
431-
genValue (VBool b) = pure $ TBool b
434+
genValueVariant (VBool b) = pure $ TBool b
432435

433436
--------------------------------------------------------------------------------
434437
-- Generator functions

0 commit comments

Comments
 (0)