Skip to content

Commit

Permalink
Merge pull request #5294 from SimaDovakin/numbers-binary-notation
Browse files Browse the repository at this point in the history
Numbers binary notation
  • Loading branch information
aryairani committed Aug 29, 2024
2 parents e82b649 + 3b2a25e commit d3d3dbb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ The format for this list: name, GitHub handle
* Eric Torreborre (@etorreborre)
* Eduard Nicodei (@neduard)
* Brian McKenna (@puffnfresh)
* Ruslan Simchuk (@SimaDovakin)
6 changes: 3 additions & 3 deletions editor-support/vim/syntax/unison.vim
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ syn match uSpecialCharError contained "\\&\|'''\+"
syn region uString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=uSpecialChar
syn match uCharacter "[^a-zA-Z0-9_']'\([^\\]\|\\[^']\+\|\\'\)'"lc=1 contains=uSpecialChar,uSpecialCharError
syn match uCharacter "^'\([^\\]\|\\[^']\+\|\\'\)'" contains=uSpecialChar,uSpecialCharError
syn match uNumber "\<[0-9]\+\>\|\<0[xX][0-9a-fA-F]\+\>\|\<0[oO][0-7]\+\>"
syn match uNumber "\<[0-9]\+\>\|\<0[xX][0-9a-fA-F]\+\>\|\<0[oO][0-7]\+\>\|\<0[bB][01]\+\>"
syn match uFloat "\<[0-9]\+\.[0-9]\+\([eE][-+]\=[0-9]\+\)\=\>"

" Keyword definitions. These must be patterns instead of keywords
Expand Down Expand Up @@ -83,7 +83,7 @@ syn region uDocDirective contained matchgroup=unisonDocDirective start="\(@

syn match uDebug "\<\(todo\|bug\|Debug.trace\)\>"

" things like
" things like
" > my_func 1 3
" test> Function.tap.tests.t1 = check let
" use Nat == +
Expand All @@ -101,7 +101,7 @@ if version >= 508 || !exists("did_u_syntax_inits")
else
command -nargs=+ HiLink hi def link <args>
endif

HiLink uWatch Debug
HiLink uDocMono Delimiter
HiLink unisonDocDirective Import
Expand Down
12 changes: 12 additions & 0 deletions parser-typechecker/src/Unison/PrintError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,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),
Expand Down
8 changes: 6 additions & 2 deletions unison-src/transcripts/error-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ x = 1e- -- missing an exponent
x = 1E+ -- missing an exponent
```

### Hex, octal, and bytes literals
### Hex, octal, binary, and bytes literals

```unison:error
x = 0xoogabooga -- invalid hex chars
Expand All @@ -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
```
Expand Down Expand Up @@ -81,7 +85,7 @@ foo = cases
```unison:error
-- Missing a '->'
x = match Some a with
None ->
None ->
1
Some _
2
Expand Down
20 changes: 18 additions & 2 deletions unison-src/transcripts/error-messages.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ x = 1E+ -- missing an exponent
`1e+37`.
```
### Hex, octal, and bytes literals
### Hex, octal, binary, and bytes literals

``` unison
x = 0xoogabooga -- invalid hex chars
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -244,7 +260,7 @@ foo = cases
``` unison
-- Missing a '->'
x = match Some a with
None ->
None ->
1
Some _
2
Expand Down
7 changes: 6 additions & 1 deletion unison-syntax/src/Unison/Syntax/Lexer/Unison.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -535,7 +536,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 _ ->
Expand All @@ -544,6 +545,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)
Expand Down

0 comments on commit d3d3dbb

Please sign in to comment.