Skip to content

Commit b121911

Browse files
Merge pull request #17 from roc-lang/syntax-updates
Syntax and formating updates
2 parents 2b6c68c + ea10b26 commit b121911

14 files changed

+88
-68
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# Ignore the example binaries
33
examples/nrOfCodePoints
44
examples/getVisualWidth
5-
example/splitGraphemes
5+
examples/splitGraphemes
66

77
# Ignore the generated files
88
generated-docs
99
package/InternalGBPGen
1010
package/GraphemeTestGen
1111
package/InternalEmojiGen
12-
package/InternalEAWGen
12+
package/InternalEAWGen

examples/getVisualWidth.roc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
app [main] {
2-
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
2+
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
33
unicode: "../package/main.roc", # use release URL (ends in tar.br) for local example, see github.com/roc/unicode/releases
44
}
55

66
import pf.Stdout
7+
import pf.Task exposing [Task]
78
import unicode.CodePoint
89

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

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

examples/nrOfCodePoints.roc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
app [main] {
2-
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
2+
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
33
unicode: "../package/main.roc", # use release URL (ends in tar.br) for local example, see github.com/roc/unicode/releases
44
}
55

66
import pf.Stdout
7-
import pf.Task
7+
import pf.Task exposing [Task]
88
import unicode.CodePoint exposing [Utf8ParseErr]
99

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

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

examples/splitGraphemes.roc

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

66
import pf.Stdout
7-
import pf.Task
7+
import pf.Task exposing [Task]
88
import unicode.Grapheme
99

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

1414
main =
1515
string
16-
|> Grapheme.split
17-
|> Inspect.toStr
18-
|> \splitted ->
19-
Stdout.line! "\n\nThe string \"$(string)\" has following graphemes:"
20-
Stdout.line! splitted
16+
|> Grapheme.split
17+
|> Inspect.toStr
18+
|> \splitted ->
19+
Stdout.line! "\n\nThe string \"$(string)\" has following graphemes:"
20+
Stdout.line! splitted

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/CodePoint.roc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ parsePartialUtf8 = \bytes ->
271271
# We always try to get the first byte, and if it fails with Err ListWasEmpty, then
272272
# the whole function should return Err ListWasEmpty. This tells the caller "there's nothing
273273
# else to parse" as opposed to "There are bytes here, but they're invalid UTF-8."
274-
firstByte <- List.first bytes |> Result.try
274+
firstByte = List.first? bytes
275275

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

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

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

315315
if

package/Grapheme.roc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ split = \str ->
3030
# I'm not sure if we should return an error here or just crash.
3131
# A Roc Str should be be valid utf8 and so in theory it should not be possible
3232
# for split to have invalid utf8 in it. To be discussed.
33-
codePoints <- str |> Str.toUtf8 |> CodePoint.parseUtf8 |> Result.map
33+
codePoints = str |> Str.toUtf8 |> CodePoint.parseUtf8?
3434

3535
breakPoints = codePoints |> List.map InternalGBP.fromCP
3636

37-
splitHelp Next codePoints breakPoints [BR GB1] |> toListStr
37+
Ok (splitHelp Next codePoints breakPoints [BR GB1] |> toListStr)
3838

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

6363
when (state, codePoints, breakPoints) is
64+
# Special handling for empty list
65+
(Next, [], _) -> acc
6466
# Special handling for last codepoint
6567
(Next, [cp], _) -> List.concat acc [CP cp, BR GB2]
6668
(AfterHangulL prev, [cp], [bp]) if bp == L || bp == V || bp == LV || bp == LVT -> List.concat acc [CP prev, NB GB6, CP cp, BR GB2]

package/GraphemeTest.roc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import InternalCP
77

88
toCodePointList : List Str -> List (List U32)
99
toCodePointList = \strings ->
10-
strings |> List.map \str ->
10+
strings
11+
|> List.map \str ->
1112
when str |> Str.toUtf8 |> CodePoint.parseUtf8 is
1213
Ok cps -> List.map cps CodePoint.toU32
1314
Err _ -> crash "expected valid utf8"
1415

15-
1616
# GraphemeBreakTest-15.1.0.txt:line 25
1717
# % 0020 % 0020 % # % [0.2] SPACE (Other) % [999.0] SPACE (Other) % [0.3]
1818
expect
@@ -13817,4 +13817,4 @@ expect
1381713817
|> Result.try Grapheme.split
1381813818
|> Result.map toCodePointList
1381913819

13820-
got == exp
13820+
got == exp

package/GraphemeTestGen.roc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
## This file will read the test data from `data/GraphemeBreakTest-15.1.0.txt`
44
## parse it and then generate the individual tests.
55
app [main] {
6-
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
7-
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.7.1/MvLlME9RxOBjl0QCxyn3LIaoG9pSlaNxCa-t3BfbPNc.tar.br",
6+
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
7+
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.7.2/1usTzOOACTpnkarBX0ED3gFESzR4ROdAlt1Llf4WFzo.tar.br",
88
}
99

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

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

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

218218
Ok (List.append next (BR rule))
219219

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

223223
Ok (List.append next (NB rule))
224224

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

228228
Ok (List.append next (CP cp))
229229

package/InternalEAWGen.roc

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

99
import pf.File
@@ -15,7 +15,7 @@ import Helpers
1515
EawRange : (Str, Str, Str)
1616

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

@@ -178,4 +178,4 @@ allTests =
178178
('𑌓', "N"),
179179
('𑪊', "N"),
180180
]
181-
List.map tests createTest |> Str.joinWith "\n\n"
181+
List.map tests createTest |> Str.joinWith "\n\n"

0 commit comments

Comments
 (0)