From 035e800a2a41be5416f855069fec69fb388d3cc1 Mon Sep 17 00:00:00 2001 From: SimaDovakin Date: Wed, 21 Aug 2024 18:29:54 +0300 Subject: [PATCH 1/6] Added support of the binary notation for Nat and Int. --- parser-typechecker/src/Unison/PrintError.hs | 12 ++++++++++++ unison-src/transcripts/error-messages.md | 6 +++++- .../transcripts/error-messages.output.md | 18 +++++++++++++++++- .../src/Unison/Syntax/Lexer/Unison.hs | 7 ++++++- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/parser-typechecker/src/Unison/PrintError.hs b/parser-typechecker/src/Unison/PrintError.hs index 691d7cd3ef..bbc5381c7d 100644 --- a/parser-typechecker/src/Unison/PrintError.hs +++ b/parser-typechecker/src/Unison/PrintError.hs @@ -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), diff --git a/unison-src/transcripts/error-messages.md b/unison-src/transcripts/error-messages.md index 8490e491a2..2157f9f502 100644 --- a/unison-src/transcripts/error-messages.md +++ b/unison-src/transcripts/error-messages.md @@ -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 diff --git a/unison-src/transcripts/error-messages.output.md b/unison-src/transcripts/error-messages.output.md index 03e7e652ac..baa8cb54e5 100644 --- a/unison-src/transcripts/error-messages.output.md +++ b/unison-src/transcripts/error-messages.output.md @@ -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 diff --git a/unison-syntax/src/Unison/Syntax/Lexer/Unison.hs b/unison-syntax/src/Unison/Syntax/Lexer/Unison.hs index 9c50e2731f..2f3ff41d00 100644 --- a/unison-syntax/src/Unison/Syntax/Lexer/Unison.hs +++ b/unison-syntax/src/Unison/Syntax/Lexer/Unison.hs @@ -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) From d474cf83a50e4ee9dab04c975ca405142f29baa1 Mon Sep 17 00:00:00 2001 From: SimaDovakin Date: Thu, 22 Aug 2024 16:19:12 +0300 Subject: [PATCH 2/6] Edited heading in transcripts for testing syntax error of the invalid binary chars. --- unison-src/transcripts/error-messages.md | 2 +- unison-src/transcripts/error-messages.output.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/unison-src/transcripts/error-messages.md b/unison-src/transcripts/error-messages.md index 2157f9f502..f3b0353806 100644 --- a/unison-src/transcripts/error-messages.md +++ b/unison-src/transcripts/error-messages.md @@ -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 diff --git a/unison-src/transcripts/error-messages.output.md b/unison-src/transcripts/error-messages.output.md index baa8cb54e5..714b3c5845 100644 --- a/unison-src/transcripts/error-messages.output.md +++ b/unison-src/transcripts/error-messages.output.md @@ -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 From df14641265c71d52023a75a542c626287f45fbab Mon Sep 17 00:00:00 2001 From: SimaDovakin Date: Thu, 22 Aug 2024 16:24:21 +0300 Subject: [PATCH 3/6] Added vim syntax highlight for the numbers' binary notation. --- editor-support/vim/syntax/unison.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/editor-support/vim/syntax/unison.vim b/editor-support/vim/syntax/unison.vim index ec193723a7..45ceda9f87 100644 --- a/editor-support/vim/syntax/unison.vim +++ b/editor-support/vim/syntax/unison.vim @@ -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]\+\>|\<0b[01]\+\>" syn match uFloat "\<[0-9]\+\.[0-9]\+\([eE][-+]\=[0-9]\+\)\=\>" " Keyword definitions. These must be patterns instead of keywords @@ -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 == + @@ -101,7 +101,7 @@ if version >= 508 || !exists("did_u_syntax_inits") else command -nargs=+ HiLink hi def link endif - + HiLink uWatch Debug HiLink uDocMono Delimiter HiLink unisonDocDirective Import From 3ce567525e4c9217a25e318c93cfdfec734835b4 Mon Sep 17 00:00:00 2001 From: SimaDovakin Date: Thu, 22 Aug 2024 16:30:42 +0300 Subject: [PATCH 4/6] Fixed vim syntax highlight for numbers' binary notation. --- editor-support/vim/syntax/unison.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor-support/vim/syntax/unison.vim b/editor-support/vim/syntax/unison.vim index 45ceda9f87..bbbfa8b915 100644 --- a/editor-support/vim/syntax/unison.vim +++ b/editor-support/vim/syntax/unison.vim @@ -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]\+\>|\<0b[01]\+\>" +syn match uNumber "\<[0-9]\+\>\|\<0[xX][0-9a-fA-F]\+\>\|\<0[oO][0-7]\+\>\|\<0b[01]\+\>" syn match uFloat "\<[0-9]\+\.[0-9]\+\([eE][-+]\=[0-9]\+\)\=\>" " Keyword definitions. These must be patterns instead of keywords From 46672dfe0d1dfd6977926b50799ff145aa027c2d Mon Sep 17 00:00:00 2001 From: SimaDovakin Date: Thu, 22 Aug 2024 17:12:11 +0300 Subject: [PATCH 5/6] Added symbol class for consistency. --- editor-support/vim/syntax/unison.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor-support/vim/syntax/unison.vim b/editor-support/vim/syntax/unison.vim index bbbfa8b915..3bd3b2ef68 100644 --- a/editor-support/vim/syntax/unison.vim +++ b/editor-support/vim/syntax/unison.vim @@ -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]\+\>\|\<0b[01]\+\>" +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 From 79c877b341d05c37371ec9f1a54d60c567cf70aa Mon Sep 17 00:00:00 2001 From: SimaDovakin Date: Tue, 27 Aug 2024 10:50:24 +0300 Subject: [PATCH 6/6] Added new contributor. --- CONTRIBUTORS.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.markdown b/CONTRIBUTORS.markdown index 5649b15dd0..6ed2e9ca35 100644 --- a/CONTRIBUTORS.markdown +++ b/CONTRIBUTORS.markdown @@ -87,3 +87,4 @@ The format for this list: name, GitHub handle * Dan Doel (@dolio) * Eric Torreborre (@etorreborre) * Eduard Nicodei (@neduard) +* Ruslan Simchuk (@SimaDovakin)