Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tokenTypeOf more precise #325

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions cborg/src/Codec/CBOR/FlatTerm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -562,25 +562,28 @@ fromFlatTerm decoder ft =
-- | Map a 'TermToken' to the underlying CBOR 'TokenType'
tokenTypeOf :: TermToken -> TokenType
tokenTypeOf (TkInt n)
| n >= 0 = TypeUInt
| otherwise = TypeNInt
tokenTypeOf TkInteger{} = TypeInteger
tokenTypeOf TkBytes{} = TypeBytes
tokenTypeOf TkBytesBegin{} = TypeBytesIndef
tokenTypeOf TkString{} = TypeString
tokenTypeOf TkStringBegin{} = TypeStringIndef
tokenTypeOf TkListLen{} = TypeListLen
tokenTypeOf TkListBegin{} = TypeListLenIndef
tokenTypeOf TkMapLen{} = TypeMapLen
tokenTypeOf TkMapBegin{} = TypeMapLenIndef
tokenTypeOf TkTag{} = TypeTag
tokenTypeOf TkBool{} = TypeBool
tokenTypeOf TkNull = TypeNull
tokenTypeOf TkBreak = TypeBreak
tokenTypeOf TkSimple{} = TypeSimple
tokenTypeOf TkFloat16{} = TypeFloat16
tokenTypeOf TkFloat32{} = TypeFloat32
tokenTypeOf TkFloat64{} = TypeFloat64
| n >= 0 = TypeUInt
| otherwise = TypeNInt
tokenTypeOf (TkInteger n) -- See https://github.com/well-typed/cborg/issues/324
| 0 <= n && n <= 0xffffffffffffffff = TypeUInt64
| -0xffffffffffffffff <= n && n < 0 = TypeNInt64
| otherwise = TypeInteger
tokenTypeOf TkBytes{} = TypeBytes
tokenTypeOf TkBytesBegin{} = TypeBytesIndef
tokenTypeOf TkString{} = TypeString
tokenTypeOf TkStringBegin{} = TypeStringIndef
tokenTypeOf TkListLen{} = TypeListLen
tokenTypeOf TkListBegin{} = TypeListLenIndef
tokenTypeOf TkMapLen{} = TypeMapLen
tokenTypeOf TkMapBegin{} = TypeMapLenIndef
tokenTypeOf TkTag{} = TypeTag
tokenTypeOf TkBool{} = TypeBool
tokenTypeOf TkNull = TypeNull
tokenTypeOf TkBreak = TypeBreak
tokenTypeOf TkSimple{} = TypeSimple
tokenTypeOf TkFloat16{} = TypeFloat16
tokenTypeOf TkFloat32{} = TypeFloat32
tokenTypeOf TkFloat64{} = TypeFloat64

--------------------------------------------------------------------------------

Expand Down