Skip to content

Commit 3249265

Browse files
committed
Fixed trailing comma parsing in arrays
1 parent 80a4398 commit 3249265

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/Codec/CBOR/Cuddle/Parser.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ pGroup = Group <$> NE.sepBy1 (space *> pGrpChoice <* space) (string "//")
124124
pGrpChoice :: Parser GrpChoice
125125
pGrpChoice =
126126
many
127-
( (space *> (noComment <$> pGrpEntry) <* space)
128-
<* optional (char ',')
129-
)
127+
(try $ (space *> (noComment <$> pGrpEntry) <* space) <* optional (char ','))
130128

131129
pGrpEntry :: Parser GroupEntry
132130
pGrpEntry =

test/Test/Codec/CBOR/Cuddle/CDDL/Parser.hs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,35 @@ type2Spec = describe "type2" $ do
182182
]
183183
)
184184
)
185+
it "Commas are optional" $
186+
parse pType2 "" "{ 1 => string, 2 => int 3 => bytes}"
187+
`shouldParse` T2Map
188+
( Group
189+
( [ WithComments
190+
( GEType
191+
Nothing
192+
(Just (MKType (Type1 (T2Value (VUInt 1)) Nothing)))
193+
(Type0 (Type1 (T2Name (Name "string") Nothing) Nothing NE.:| []))
194+
)
195+
Nothing
196+
, WithComments
197+
( GEType
198+
Nothing
199+
(Just (MKType (Type1 (T2Value (VUInt 2)) Nothing)))
200+
(Type0 (Type1 (T2Name (Name "int") Nothing) Nothing NE.:| []))
201+
)
202+
Nothing
203+
, WithComments
204+
( GEType
205+
Nothing
206+
(Just (MKType (Type1 (T2Value (VUInt 3)) Nothing)))
207+
(Type0 (Type1 (T2Name (Name "bytes") Nothing) Nothing NE.:| []))
208+
)
209+
Nothing
210+
]
211+
NE.:| []
212+
)
213+
)
185214
describe "Array" $ do
186215
it "Parses an array with an alternative" $
187216
parse pType2 "" "[int // string]"
@@ -236,6 +265,19 @@ type2Spec = describe "type2" $ do
236265
]
237266
)
238267
)
268+
it "Trailing commas permitted" $
269+
parse pType2 "" "[ 1 , ]"
270+
`shouldParse` T2Array
271+
( Group
272+
( [ noComment $
273+
GEType
274+
Nothing
275+
Nothing
276+
(Type0 ((NE.:| []) (Type1 (T2Value (VUInt 1)) Nothing)))
277+
]
278+
NE.:| []
279+
)
280+
)
239281

240282
grpEntrySpec :: SpecWith ()
241283
grpEntrySpec = describe "GroupEntry" $ do

0 commit comments

Comments
 (0)