Skip to content

Commit ca3b659

Browse files
Introduce purs-tidy formatter (#76)
* Add purs-tidy formatter * Run purs-tidy * review
1 parent e78554b commit ca3b659

File tree

12 files changed

+318
-248
lines changed

12 files changed

+318
-248
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515

1616
- name: Set up a PureScript toolchain
1717
uses: purescript-contrib/setup-purescript@main
18+
with:
19+
purs-tidy: "latest"
1820

1921
- name: Cache PureScript dependencies
2022
uses: actions/cache@v2
@@ -32,3 +34,6 @@ jobs:
3234

3335
- name: Run tests
3436
run: spago test --no-install
37+
38+
- name: Check formatting
39+
run: purs-tidy check src test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
!.gitignore
33
!.github
44
!.editorconfig
5+
!.tidyrc.json
56

67
output
78
generated-docs

.tidyrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"importSort": "source",
3+
"importWrap": "source",
4+
"indent": 2,
5+
"operatorsFile": null,
6+
"ribbon": 1,
7+
"typeArrowPlacement": "first",
8+
"unicode": "never",
9+
"width": null
10+
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ New features:
1111
Bugfixes:
1212

1313
Other improvements:
14+
- Added `purs-tidy` formatter (#76 by @thomashoneyman)
1415

1516
- Run slowest tests last and print status updates (#72)
1617

src/Text/Parsing/StringParser.purs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ instance applicativeParser :: Applicative Parser where
6464
instance altParser :: Alt Parser where
6565
alt (Parser p1) (Parser p2) = Parser \s ->
6666
case p1 s of
67-
Left { error, pos } | s.pos == pos -> p2 s
68-
| otherwise -> Left { error, pos }
67+
Left { error, pos }
68+
| s.pos == pos -> p2 s
69+
| otherwise -> Left { error, pos }
6970
right -> right
7071

7172
instance plusParser :: Plus Parser where
@@ -87,8 +88,8 @@ instance monadPlusParser :: MonadPlus Parser
8788
instance monadRecParser :: MonadRec Parser where
8889
tailRecM f a = Parser \str -> tailRecM (\st -> map split (unParser (f st.state) st.str)) { state: a, str }
8990
where
90-
split { result: Loop state, suffix: str } = Loop { state, str }
91-
split { result: Done b, suffix } = Done { result: b, suffix }
91+
split { result: Loop state, suffix: str } = Loop { state, str }
92+
split { result: Done b, suffix } = Done { result: b, suffix }
9293

9394
instance lazyParser :: Lazy (Parser a) where
9495
defer f = Parser $ \str -> unParser (f unit) str
@@ -101,7 +102,7 @@ fail error = Parser \{ pos } -> Left { pos, error }
101102
-- |
102103
-- | `try p` backtracks even if input was consumed.
103104
try :: forall a. Parser a -> Parser a
104-
try (Parser p) = Parser \(s@{ pos }) -> lmap (_ { pos = pos}) (p s)
105+
try (Parser p) = Parser \(s@{ pos }) -> lmap (_ { pos = pos }) (p s)
105106

106107
instance semigroupParser :: Semigroup a => Semigroup (Parser a) where
107108
append = lift2 append

src/Text/Parsing/StringParser/CodePoints.purs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ anyChar = Parser \{ str, pos } ->
5555
Nothing -> Left { pos, error: "CodePoint " <> show cp <> " is not a character" }
5656
Nothing -> Left { pos, error: "Unexpected EOF" }
5757
where
58-
toChar = fromCharCode <<< fromEnum
58+
toChar = fromCharCode <<< fromEnum
5959

6060
-- | Match any digit.
6161
anyDigit :: Parser Char
6262
anyDigit = try do
6363
c <- anyChar
64-
if c >= '0' && c <= '9'
65-
then pure c
66-
else fail $ "Character " <> show c <> " is not a digit"
64+
if c >= '0' && c <= '9' then pure c
65+
else fail $ "Character " <> show c <> " is not a digit"
6766

6867
-- | Match the specified string.
6968
string :: String -> Parser String
@@ -76,9 +75,8 @@ string nt = Parser \s ->
7675
satisfy :: (Char -> Boolean) -> Parser Char
7776
satisfy f = try do
7877
c <- anyChar
79-
if f c
80-
then pure c
81-
else fail $ "Character " <> show c <> " did not satisfy predicate"
78+
if f c then pure c
79+
else fail $ "Character " <> show c <> " did not satisfy predicate"
8280

8381
-- | Match the specified character.
8482
char :: Char -> Parser Char
@@ -87,7 +85,7 @@ char c = satisfy (_ == c) <?> "Could not match character " <> show c
8785
-- | Match many whitespace characters.
8886
whiteSpace :: Parser String
8987
whiteSpace = do
90-
cs <- many (satisfy \ c -> c == '\n' || c == '\r' || c == ' ' || c == '\t')
88+
cs <- many (satisfy \c -> c == '\n' || c == '\r' || c == ' ' || c == '\t')
9189
pure (foldMap singleton cs)
9290

9391
-- | Skip many whitespace characters.
@@ -106,17 +104,15 @@ noneOf = satisfy <<< flip notElem
106104
lowerCaseChar :: Parser Char
107105
lowerCaseChar = try do
108106
c <- anyChar
109-
if toCharCode c `elem` (97 .. 122)
110-
then pure c
111-
else fail $ "Expected a lower case character but found " <> show c
107+
if toCharCode c `elem` (97 .. 122) then pure c
108+
else fail $ "Expected a lower case character but found " <> show c
112109

113110
-- | Match any upper case character.
114111
upperCaseChar :: Parser Char
115112
upperCaseChar = try do
116113
c <- anyChar
117-
if toCharCode c `elem` (65 .. 90)
118-
then pure c
119-
else fail $ "Expected an upper case character but found " <> show c
114+
if toCharCode c `elem` (65 .. 90) then pure c
115+
else fail $ "Expected an upper case character but found " <> show c
120116

121117
-- | Match any letter.
122118
anyLetter :: Parser Char
@@ -135,21 +131,17 @@ regex pat =
135131
Right r ->
136132
matchRegex r
137133
where
138-
-- ensure the pattern only matches the current position in the parse
139-
pattern =
140-
case stripPrefix (Pattern "^") pat of
141-
Nothing ->
142-
"^" <> pat
143-
_ ->
144-
pat
145-
matchRegex :: Regex.Regex -> Parser String
146-
matchRegex r =
147-
Parser \{ str, pos } ->
148-
let
149-
remainder = drop pos str
150-
in
151-
case NEA.head <$> Regex.match r remainder of
152-
Just (Just matched) ->
153-
Right { result: matched, suffix: { str, pos: pos + length matched } }
154-
_ ->
155-
Left { pos, error: "no match" }
134+
-- ensure the pattern only matches the current position in the parse
135+
pattern =
136+
case stripPrefix (Pattern "^") pat of
137+
Nothing -> "^" <> pat
138+
_ -> pat
139+
140+
matchRegex :: Regex.Regex -> Parser String
141+
matchRegex r = Parser \{ str, pos } -> do
142+
let remainder = drop pos str
143+
case NEA.head <$> Regex.match r remainder of
144+
Just (Just matched) ->
145+
Right { result: matched, suffix: { str, pos: pos + length matched } }
146+
_ ->
147+
Left { pos, error: "no match" }

src/Text/Parsing/StringParser/CodeUnits.purs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ anyChar = Parser \{ str, pos } ->
5656
anyDigit :: Parser Char
5757
anyDigit = try do
5858
c <- anyChar
59-
if c >= '0' && c <= '9'
60-
then pure c
61-
else fail $ "Character " <> show c <> " is not a digit"
59+
if c >= '0' && c <= '9' then pure c
60+
else fail $ "Character " <> show c <> " is not a digit"
6261

6362
-- | Match the specified string.
6463
string :: String -> Parser String
@@ -71,9 +70,8 @@ string nt = Parser \s ->
7170
satisfy :: (Char -> Boolean) -> Parser Char
7271
satisfy f = try do
7372
c <- anyChar
74-
if f c
75-
then pure c
76-
else fail $ "Character " <> show c <> " did not satisfy predicate"
73+
if f c then pure c
74+
else fail $ "Character " <> show c <> " did not satisfy predicate"
7775

7876
-- | Match the specified character.
7977
char :: Char -> Parser Char
@@ -82,7 +80,7 @@ char c = satisfy (_ == c) <?> "Could not match character " <> show c
8280
-- | Match many whitespace characters.
8381
whiteSpace :: Parser String
8482
whiteSpace = do
85-
cs <- many (satisfy \ c -> c == '\n' || c == '\r' || c == ' ' || c == '\t')
83+
cs <- many (satisfy \c -> c == '\n' || c == '\r' || c == ' ' || c == '\t')
8684
pure (foldMap singleton cs)
8785

8886
-- | Skip many whitespace characters.
@@ -101,17 +99,15 @@ noneOf = satisfy <<< flip notElem
10199
lowerCaseChar :: Parser Char
102100
lowerCaseChar = try do
103101
c <- anyChar
104-
if toCharCode c `elem` (97 .. 122)
105-
then pure c
106-
else fail $ "Expected a lower case character but found " <> show c
102+
if toCharCode c `elem` (97 .. 122) then pure c
103+
else fail $ "Expected a lower case character but found " <> show c
107104

108105
-- | Match any upper case character.
109106
upperCaseChar :: Parser Char
110107
upperCaseChar = try do
111108
c <- anyChar
112-
if toCharCode c `elem` (65 .. 90)
113-
then pure c
114-
else fail $ "Expected an upper case character but found " <> show c
109+
if toCharCode c `elem` (65 .. 90) then pure c
110+
else fail $ "Expected an upper case character but found " <> show c
115111

116112
-- | Match any letter.
117113
anyLetter :: Parser Char
@@ -130,21 +126,17 @@ regex pat =
130126
Right r ->
131127
matchRegex r
132128
where
133-
-- ensure the pattern only matches the current position in the parse
134-
pattern =
135-
case SCU.stripPrefix (Pattern "^") pat of
136-
Nothing ->
137-
"^" <> pat
138-
_ ->
139-
pat
140-
matchRegex :: Regex.Regex -> Parser String
141-
matchRegex r =
142-
Parser \{ str, pos } ->
143-
let
144-
remainder = SCU.drop pos str
145-
in
146-
case NEA.head <$> Regex.match r remainder of
147-
Just (Just matched) ->
148-
Right { result: matched, suffix: { str, pos: pos + SCU.length matched } }
149-
_ ->
150-
Left { pos, error: "no match" }
129+
-- ensure the pattern only matches the current position in the parse
130+
pattern =
131+
case SCU.stripPrefix (Pattern "^") pat of
132+
Nothing -> "^" <> pat
133+
_ -> pat
134+
135+
matchRegex :: Regex.Regex -> Parser String
136+
matchRegex r = Parser \{ str, pos } -> do
137+
let remainder = SCU.drop pos str
138+
case NEA.head <$> Regex.match r remainder of
139+
Just (Just matched) ->
140+
Right { result: matched, suffix: { str, pos: pos + SCU.length matched } }
141+
_ ->
142+
Left { pos, error: "no match" }

src/Text/Parsing/StringParser/Combinators.purs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
-- | This module defines combinators for building string parsers.
2-
32
module Text.Parsing.StringParser.Combinators
43
( lookAhead
54
, many
65
, many1
7-
, withError, (<?>)
6+
, withError
7+
, (<?>)
88
, between
99
, option
1010
, optional
@@ -97,9 +97,11 @@ sepEndBy p sep = map NEL.toList (sepEndBy1 p sep) <|> pure Nil
9797
sepEndBy1 :: forall a sep. Parser a -> Parser sep -> Parser (NonEmptyList a)
9898
sepEndBy1 p sep = do
9999
a <- p
100-
(do _ <- sep
100+
( do
101+
_ <- sep
101102
as <- sepEndBy p sep
102-
pure (cons' a as)) <|> pure (NEL.singleton a)
103+
pure (cons' a as)
104+
) <|> pure (NEL.singleton a)
103105

104106
-- | Parse one or more separated values, ending with a separator.
105107
endBy1 :: forall a sep. Parser a -> Parser sep -> Parser (NonEmptyList a)
@@ -125,9 +127,12 @@ chainl1 p f = do
125127

126128
-- | Parse one or more values separated by a left-associative operator.
127129
chainl1' :: forall a. Parser a -> Parser (a -> a -> a) -> a -> Parser a
128-
chainl1' p f a = (do f' <- f
129-
a' <- p
130-
chainl1' p f (f' a a')) <|> pure a
130+
chainl1' p f a =
131+
( do
132+
f' <- f
133+
a' <- p
134+
chainl1' p f (f' a a')
135+
) <|> pure a
131136

132137
-- | Parse one or more values separated by a right-associative operator.
133138
chainr1 :: forall a. Parser a -> Parser (a -> a -> a) -> Parser a
@@ -137,9 +142,12 @@ chainr1 p f = do
137142

138143
-- | Parse one or more values separated by a right-associative operator.
139144
chainr1' :: forall a. Parser a -> Parser (a -> a -> a) -> a -> Parser a
140-
chainr1' p f a = (do f' <- f
141-
a' <- chainr1 p f
142-
pure $ f' a a') <|> pure a
145+
chainr1' p f a =
146+
( do
147+
f' <- f
148+
a' <- chainr1 p f
149+
pure $ f' a a'
150+
) <|> pure a
143151

144152
-- | Parse using any of a collection of parsers.
145153
choice :: forall f a. Foldable f => f (Parser a) -> Parser a
@@ -155,13 +163,13 @@ many1Till p end = do
155163
x <- p
156164
tailRecM inner (pure x)
157165
where
158-
ending acc = do
159-
_ <- end
160-
pure $ Done (NEL.reverse acc)
161-
continue acc = do
162-
c <- p
163-
pure $ Loop (NEL.cons c acc)
164-
inner acc = ending acc <|> continue acc
166+
ending acc = do
167+
_ <- end
168+
pure $ Done (NEL.reverse acc)
169+
continue acc = do
170+
c <- p
171+
pure $ Loop (NEL.cons c acc)
172+
inner acc = ending acc <|> continue acc
165173

166174
cons' :: forall a. a -> List a -> NonEmptyList a
167175
cons' h t = NonEmptyList (h :| t)

0 commit comments

Comments
 (0)