Added support of the binary notation for Nat and Int.

This commit is contained in:
SimaDovakin 2024-08-21 18:29:54 +03:00
parent e388786b48
commit 035e800a2a
4 changed files with 40 additions and 3 deletions

View File

@ -1427,6 +1427,18 @@ renderParseErrors s = \case
<> "after the"
<> Pr.group (style ErrorSite "0o" <> ".")
]
L.InvalidBinaryLiteral ->
Pr.lines
[ "This number isn't valid syntax: ",
"",
excerpt,
Pr.wrap $
"I was expecting only binary characters"
<> "(one of"
<> Pr.group (style Code "01" <> ")")
<> "after the"
<> Pr.group (style ErrorSite "0b" <> ".")
]
L.InvalidShortHash h ->
Pr.lines
[ "Invalid hash: " <> style ErrorSite (fromString h),

View File

@ -37,6 +37,10 @@ x = 0xoogabooga -- invalid hex chars
x = 0o987654321 -- 9 and 8 are not valid octal char
```
```unison:error
x = 0b3201 -- 3 and 2 are not valid binary chars
```
```unison:error
x = 0xsf -- odd number of hex chars in a bytes literal
```
@ -81,7 +85,7 @@ foo = cases
```unison:error
-- Missing a '->'
x = match Some a with
None ->
None ->
1
Some _
2

View File

@ -103,6 +103,22 @@ x = 0o987654321 -- 9 and 8 are not valid octal char
I was expecting only octal characters (one of 01234567) after
the 0o.
```
``` unison
x = 0b3201 -- 3 and 2 are not valid binary chars
```
``` ucm
Loading changes detected in scratch.u.
This number isn't valid syntax:
1 | x = 0b3201 -- 3 and 2 are not valid binary chars
I was expecting only binary characters (one of 01) after the
0b.
```
``` unison
x = 0xsf -- odd number of hex chars in a bytes literal
@ -245,7 +261,7 @@ foo = cases
``` unison
-- Missing a '->'
x = match Some a with
None ->
None ->
1
Some _
2

View File

@ -87,6 +87,7 @@ data Err
| InvalidBytesLiteral String
| InvalidHexLiteral
| InvalidOctalLiteral
| InvalidBinaryLiteral
| Both Err Err
| MissingFractional String -- ex `1.` rather than `1.04`
| MissingExponent String -- ex `1e` rather than `1e3`
@ -533,7 +534,7 @@ lexemes eof =
case Bytes.fromBase16 $ Bytes.fromWord8s (fromIntegral . ord <$> s) of
Left _ -> err start (InvalidBytesLiteral $ "0xs" <> s)
Right bs -> pure (Bytes bs)
otherbase = octal <|> hex
otherbase = octal <|> hex <|> binary
octal = do
start <- posP
commitAfter2 sign (lit "0o") $ \sign _ ->
@ -542,6 +543,10 @@ lexemes eof =
start <- posP
commitAfter2 sign (lit "0x") $ \sign _ ->
fmap (num sign) LP.hexadecimal <|> err start InvalidHexLiteral
binary = do
start <- posP
commitAfter2 sign (lit "0b") $ \sign _ ->
fmap (num sign) LP.binary <|> err start InvalidBinaryLiteral
num :: Maybe String -> Integer -> Lexeme
num sign n = Numeric (fromMaybe "" sign <> show n)