Skip to content

Commit

Permalink
Merge pull request #17 from roc-lang/syntax-updates
Browse files Browse the repository at this point in the history
Syntax and formating updates
  • Loading branch information
lukewilliamboswell authored Aug 26, 2024
2 parents 2b6c68c + ea10b26 commit b121911
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Ignore the example binaries
examples/nrOfCodePoints
examples/getVisualWidth
example/splitGraphemes
examples/splitGraphemes

# Ignore the generated files
generated-docs
package/InternalGBPGen
package/GraphemeTestGen
package/InternalEmojiGen
package/InternalEAWGen
package/InternalEAWGen
5 changes: 3 additions & 2 deletions examples/getVisualWidth.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
unicode: "../package/main.roc", # use release URL (ends in tar.br) for local example, see github.com/roc/unicode/releases
}

import pf.Stdout
import pf.Task exposing [Task]
import unicode.CodePoint

word = "世界"
Expand All @@ -18,7 +19,7 @@ getVisualWidth = \str ->
|> Result.map List.sum

main =
when (getVisualWidth word) is
when getVisualWidth word is
Ok width -> Stdout.line "\n\nThe word $(word) will be displayed with the width of $(Num.toStr width) characters on most UIs.\n\n"
Err _ -> crash "ERROR: Unable to parse $(word)!"

Expand Down
5 changes: 2 additions & 3 deletions examples/nrOfCodePoints.roc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
unicode: "../package/main.roc", # use release URL (ends in tar.br) for local example, see github.com/roc/unicode/releases
}

import pf.Stdout
import pf.Task
import pf.Task exposing [Task]
import unicode.CodePoint exposing [Utf8ParseErr]

## Get the number of code points for a given Str
Expand All @@ -21,4 +21,3 @@ main =

Err _ ->
Task.err (Exit 1 "Failed to parse string $(word) as Utf8.")

14 changes: 7 additions & 7 deletions examples/splitGraphemes.roc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
unicode: "../package/main.roc", # use release URL (ends in tar.br) for local example, see github.com/roc/unicode/releases
}

import pf.Stdout
import pf.Task
import pf.Task exposing [Task]
import unicode.Grapheme

string = "🇦🇺🦘🪃"
Expand All @@ -13,8 +13,8 @@ expect Grapheme.split string == Ok ["🇦🇺", "🦘", "🪃"]

main =
string
|> Grapheme.split
|> Inspect.toStr
|> \splitted ->
Stdout.line! "\n\nThe string \"$(string)\" has following graphemes:"
Stdout.line! splitted
|> Grapheme.split
|> Inspect.toStr
|> \splitted ->
Stdout.line! "\n\nThe string \"$(string)\" has following graphemes:"
Stdout.line! splitted
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package/CodePoint.roc
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ parsePartialUtf8 = \bytes ->
# We always try to get the first byte, and if it fails with Err ListWasEmpty, then
# the whole function should return Err ListWasEmpty. This tells the caller "there's nothing
# else to parse" as opposed to "There are bytes here, but they're invalid UTF-8."
firstByte <- List.first bytes |> Result.try
firstByte = List.first? bytes

# Get the byte at the index, or return Err InvalidUtf8 if that's past the end of the list.
byteAt = \index ->
Expand All @@ -289,15 +289,15 @@ parsePartialUtf8 = \bytes ->
}
else if firstByte >= 0b1100_0000 && firstByte <= 0b1101_1111 then
# 2-byte code point
secondByte <- byteAt 1 |> Result.try
secondByte = byteAt? 1
bytesParsed = 2

parse2 firstByte secondByte
|> Result.map \u32 -> { codePoint: fromU32Unchecked u32, bytesParsed }
else if firstByte >= 0b1110_0000 && firstByte <= 0b1110_1111 then
# 3-byte code point
secondByte <- byteAt 1 |> Result.try
thirdByte <- byteAt 2 |> Result.try
secondByte = byteAt? 1
thirdByte = byteAt? 2
bytesParsed = 3

if Num.bitwiseAnd firstByte 0b11110000 == 0b11100000 then
Expand All @@ -307,9 +307,9 @@ parsePartialUtf8 = \bytes ->
Err InvalidUtf8
else if firstByte >= 0b1111_0000 && firstByte <= 0b1111_0111 then
# 4-byte code point
secondByte <- byteAt 1 |> Result.try
thirdByte <- byteAt 2 |> Result.try
fourthByte <- byteAt 3 |> Result.try
secondByte = byteAt? 1
thirdByte = byteAt? 2
fourthByte = byteAt? 3
bytesParsed = 4

if
Expand Down
6 changes: 4 additions & 2 deletions package/Grapheme.roc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ split = \str ->
# I'm not sure if we should return an error here or just crash.
# A Roc Str should be be valid utf8 and so in theory it should not be possible
# for split to have invalid utf8 in it. To be discussed.
codePoints <- str |> Str.toUtf8 |> CodePoint.parseUtf8 |> Result.map
codePoints = str |> Str.toUtf8 |> CodePoint.parseUtf8?

breakPoints = codePoints |> List.map InternalGBP.fromCP

splitHelp Next codePoints breakPoints [BR GB1] |> toListStr
Ok (splitHelp Next codePoints breakPoints [BR GB1] |> toListStr)

# Used internally to filter out the break/nobreak tokens and separate CPs into a List Str
toListStr : Tokens -> List Str
Expand All @@ -61,6 +61,8 @@ splitHelp = \state, codePoints, breakPoints, acc ->
nextBPs = List.dropFirst breakPoints 1

when (state, codePoints, breakPoints) is
# Special handling for empty list
(Next, [], _) -> acc
# Special handling for last codepoint
(Next, [cp], _) -> List.concat acc [CP cp, BR GB2]
(AfterHangulL prev, [cp], [bp]) if bp == L || bp == V || bp == LV || bp == LVT -> List.concat acc [CP prev, NB GB6, CP cp, BR GB2]
Expand Down
6 changes: 3 additions & 3 deletions package/GraphemeTest.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import InternalCP

toCodePointList : List Str -> List (List U32)
toCodePointList = \strings ->
strings |> List.map \str ->
strings
|> List.map \str ->
when str |> Str.toUtf8 |> CodePoint.parseUtf8 is
Ok cps -> List.map cps CodePoint.toU32
Err _ -> crash "expected valid utf8"


# GraphemeBreakTest-15.1.0.txt:line 25
# % 0020 % 0020 % # % [0.2] SPACE (Other) % [999.0] SPACE (Other) % [0.3]
expect
Expand Down Expand Up @@ -13817,4 +13817,4 @@ expect
|> Result.try Grapheme.split
|> Result.map toCodePointList

got == exp
got == exp
12 changes: 6 additions & 6 deletions package/GraphemeTestGen.roc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
## This file will read the test data from `data/GraphemeBreakTest-15.1.0.txt`
## parse it and then generate the individual tests.
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.7.1/MvLlME9RxOBjl0QCxyn3LIaoG9pSlaNxCa-t3BfbPNc.tar.br",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.7.2/1usTzOOACTpnkarBX0ED3gFESzR4ROdAlt1Llf4WFzo.tar.br",
}

import pf.Task exposing [Task]
Expand All @@ -21,7 +21,7 @@ Rule : [GB1, GB2, GB3, GB4, GB5, GB6, GB7, GB8, GB9, GB9a, GB9b, GB9c, GB11, GB1
TestTokens : List [BR Rule, NB Rule, CP CodePoint]

main =
when Arg.list! |> List.get 1 is
when Arg.list! {} |> List.get 1 is
Err _ -> Task.err (InvalidArguments "USAGE: roc run GraphemeTest.roc -- path/to/package/")
Ok arg -> File.writeUtf8 "$(Helpers.removeTrailingSlash arg)/GraphemeTest.roc" template

Expand Down Expand Up @@ -213,17 +213,17 @@ zip : List [BR, NB, CP CodePoint], List [BR Rule, NB Rule] -> Result (List [BR R
zip = \first, second ->
when (List.first first, List.first second) is
(Ok BR, Ok (BR rule)) ->
next <- zip (List.dropFirst first 1) (List.dropFirst second 1) |> Result.try
next = zip? (List.dropFirst first 1) (List.dropFirst second 1)

Ok (List.append next (BR rule))

(Ok NB, Ok (NB rule)) ->
next <- zip (List.dropFirst first 1) (List.dropFirst second 1) |> Result.try
next = zip? (List.dropFirst first 1) (List.dropFirst second 1)

Ok (List.append next (NB rule))

(Ok (CP cp), _) ->
next <- zip (List.dropFirst first 1) second |> Result.try
next = zip? (List.dropFirst first 1) second

Ok (List.append next (CP cp))

Expand Down
6 changes: 3 additions & 3 deletions package/InternalEAWGen.roc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## This file will read the test data from `data/EastAsianWidth-15.1.0.txt`
## parse it and then generate function to test the East Asian Width property of a code point.
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
}

import pf.File
Expand All @@ -15,7 +15,7 @@ import Helpers
EawRange : (Str, Str, Str)

main =
when Arg.list! |> List.get 1 is
when Arg.list! {} |> List.get 1 is
Err _ -> Task.err (InvalidArguments "USAGE: roc run InternalEAWGen.roc -- path/to/package/")
Ok arg -> File.writeUtf8 "$(Helpers.removeTrailingSlash arg)/InternalEAW.roc" template

Expand Down Expand Up @@ -178,4 +178,4 @@ allTests =
('𑌓', "N"),
('𑪊', "N"),
]
List.map tests createTest |> Str.joinWith "\n\n"
List.map tests createTest |> Str.joinWith "\n\n"
2 changes: 1 addition & 1 deletion package/InternalEmoji.roc
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ fromCP = \cp ->
Ok Component
else if isEmoji u32 then
Ok Emoji
else
else
Err NonEmojiCodePoint
6 changes: 3 additions & 3 deletions package/InternalEmojiGen.roc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
## This file will read the test data from `data/emoji-data.txt`
## parse it and then generate the implementation for each of the Emoji properties.
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.5.2/9VrPjwfQQ1QeSL3CfmWr2Pr9DESdDIXy97pwpuq84Ck.tar.br",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.7.2/1usTzOOACTpnkarBX0ED3gFESzR4ROdAlt1Llf4WFzo.tar.br",
}

import pf.Task exposing [Task]
Expand All @@ -14,7 +14,7 @@ import "data/emoji-data.txt" as file : Str
import Helpers exposing [CPMeta, PropertyMap]

main =
when Arg.list! |> List.get 1 is
when Arg.list! {} |> List.get 1 is
Err _ -> Task.err (InvalidArguments "USAGE: roc run InternalEmoji.roc -- path/to/package/")
Ok arg -> File.writeUtf8 "$(Helpers.removeTrailingSlash arg)/InternalEmoji.roc" template

Expand Down
Loading

0 comments on commit b121911

Please sign in to comment.