Skip to content

Commit c9b486e

Browse files
Introduce purs-tidy formatter (#126)
* Add purs-tidy formatter * Run purs-tidy * review * review 2 * missed a comma
1 parent a3e82c2 commit c9b486e

File tree

13 files changed

+1177
-1029
lines changed

13 files changed

+1177
-1029
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 -x spago-dev.dhall test --no-install
37+
38+
- name: Check formatting
39+
run: purs-tidy check src test bench

.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 (#126 by @thomashoneyman)
1415

1516
## [v7.0.1](https://github.com/purescript-contrib/purescript-parsing/releases/tag/v7.0.0) - 2021-11-17
1617

bench/Main.purs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,19 @@ import Text.Parsing.StringParser.CodeUnits as StringParser.CodeUnits
6262

6363
string23 :: String
6464
string23 = "23"
65+
6566
string23_2 :: String
6667
string23_2 = fold $ replicate 2 string23
68+
6769
string23_10000 :: String
6870
string23_10000 = fold $ replicate 10000 string23
6971

7072
stringSkidoo :: String
7173
stringSkidoo = "skidoo"
74+
7275
stringSkidoo_2 :: String
7376
stringSkidoo_2 = fold $ replicate 2 stringSkidoo
77+
7478
stringSkidoo_10000 :: String
7579
stringSkidoo_10000 = fold $ replicate 10000 stringSkidoo
7680

@@ -84,29 +88,31 @@ parse23Units :: StringParser.Parser (List Char)
8488
parse23Units = manyRec StringParser.CodeUnits.anyDigit
8589

8690
pattern23 :: Regex
87-
pattern23 = either (unsafePerformEffect <<< throw) identity $
88-
regex "\\d" $ RegexFlags
89-
{ dotAll: true
90-
, global: true
91-
, ignoreCase: false
92-
, multiline: true
93-
, sticky: false
94-
, unicode: true
95-
}
91+
pattern23 = either (unsafePerformEffect <<< throw) identity
92+
$ regex "\\d"
93+
$ RegexFlags
94+
{ dotAll: true
95+
, global: true
96+
, ignoreCase: false
97+
, multiline: true
98+
, sticky: false
99+
, unicode: true
100+
}
96101

97102
parseSkidoo :: Parser String (List String)
98103
parseSkidoo = manyRec $ string "skidoo"
99104

100105
patternSkidoo :: Regex
101-
patternSkidoo = either (unsafePerformEffect <<< throw) identity $
102-
regex "skidoo" $ RegexFlags
103-
{ dotAll: true
104-
, global: true
105-
, ignoreCase: false
106-
, multiline: true
107-
, sticky: false
108-
, unicode: true
109-
}
106+
patternSkidoo = either (unsafePerformEffect <<< throw) identity
107+
$ regex "skidoo"
108+
$ RegexFlags
109+
{ dotAll: true
110+
, global: true
111+
, ignoreCase: false
112+
, multiline: true
113+
, sticky: false
114+
, unicode: true
115+
}
110116

111117
main :: Effect Unit
112118
main = do

src/Text/Parsing/Parser.purs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ import Prelude
2020

2121
import Control.Alt (class Alt)
2222
import Control.Apply (lift2)
23-
import Control.Lazy (defer, class Lazy)
24-
import Control.Monad.Error.Class (class MonadThrow, throwError, catchError)
25-
import Control.Monad.Except (class MonadError, ExceptT(..), runExceptT, mapExceptT)
23+
import Control.Lazy (class Lazy, defer)
24+
import Control.Monad.Error.Class (class MonadThrow, catchError, throwError)
25+
import Control.Monad.Except (class MonadError, ExceptT(..), mapExceptT, runExceptT)
2626
import Control.Monad.Rec.Class (class MonadRec)
2727
import Control.Monad.State (class MonadState, StateT(..), evalStateT, gets, mapStateT, modify_, runStateT)
2828
import Control.Monad.Trans.Class (class MonadTrans, lift)
29-
import Control.MonadPlus (class Alternative, class MonadZero, class MonadPlus, class Plus)
29+
import Control.MonadPlus (class Alternative, class MonadPlus, class MonadZero, class Plus)
3030
import Data.Either (Either(..))
3131
import Data.Identity (Identity)
32-
import Data.Newtype (class Newtype, unwrap, over)
32+
import Data.Newtype (class Newtype, over, unwrap)
3333
import Data.Tuple (Tuple(..))
3434
import Text.Parsing.Parser.Pos (Position, initialPos)
3535

@@ -62,7 +62,8 @@ derive instance newtypeParserT :: Newtype (ParserT s m a) _
6262

6363
-- | Apply a parser, keeping only the parsed result.
6464
runParserT :: forall m s a. Monad m => s -> ParserT s m a -> m (Either ParseError a)
65-
runParserT s p = evalStateT (runExceptT (unwrap p)) initialState where
65+
runParserT s p = evalStateT (runExceptT (unwrap p)) initialState
66+
where
6667
initialState = ParseState s initialPos false
6768

6869
-- | The `Parser` monad is a synonym for the parser monad transformer applied to the `Identity` monad.
@@ -76,10 +77,13 @@ hoistParserT :: forall s m n a. (m ~> n) -> ParserT s m a -> ParserT s n a
7677
hoistParserT = mapParserT
7778

7879
-- | Change the underlying monad action and data type in a ParserT monad action.
79-
mapParserT :: forall b n s a m.
80-
( m (Tuple (Either ParseError a) (ParseState s))
81-
-> n (Tuple (Either ParseError b) (ParseState s))
82-
) -> ParserT s m a -> ParserT s n b
80+
mapParserT
81+
:: forall b n s a m
82+
. ( m (Tuple (Either ParseError a) (ParseState s))
83+
-> n (Tuple (Either ParseError b) (ParseState s))
84+
)
85+
-> ParserT s m a
86+
-> ParserT s n b
8387
mapParserT = over ParserT <<< mapExceptT <<< mapStateT
8488

8589
instance lazyParserT :: Lazy (ParserT s m a) where

src/Text/Parsing/Parser/Combinators.purs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Text.Parsing.Parser.Combinators where
2424

2525
import Prelude
2626

27-
import Control.Monad.Except (runExceptT, ExceptT(..))
27+
import Control.Monad.Except (ExceptT(..), runExceptT)
2828
import Control.Monad.State (StateT(..), runStateT)
2929
import Control.Plus (empty, (<|>))
3030
import Data.Either (Either(..))
@@ -35,7 +35,7 @@ import Data.List.NonEmpty as NEL
3535
import Data.Maybe (Maybe(..))
3636
import Data.Newtype (unwrap)
3737
import Data.Tuple (Tuple(..))
38-
import Text.Parsing.Parser (ParseState(..), ParserT(..), ParseError(..), fail)
38+
import Text.Parsing.Parser (ParseError(..), ParseState(..), ParserT(..), fail)
3939

4040
-- | Provide an error message in the case of failure.
4141
withErrorMessage :: forall m s a. Monad m => ParserT s m a -> String -> ParserT s m a
@@ -122,9 +122,11 @@ sepEndBy p sep = map NEL.toList (sepEndBy1 p sep) <|> pure Nil
122122
sepEndBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)
123123
sepEndBy1 p sep = do
124124
a <- p
125-
(do _ <- sep
125+
( do
126+
_ <- sep
126127
as <- sepEndBy p sep
127-
pure (NEL.cons' a as)) <|> pure (NEL.singleton a)
128+
pure (NEL.cons' a as)
129+
) <|> pure (NEL.singleton a)
128130

129131
-- | Parse phrases delimited and terminated by a separator, requiring at least one match.
130132
endBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)
@@ -155,9 +157,12 @@ chainl1 p f = do
155157
chainl1' p f a
156158

157159
chainl1' :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
158-
chainl1' p f a = (do f' <- f
159-
a' <- p
160-
chainl1' p f (f' a a')) <|> pure a
160+
chainl1' p f a =
161+
( do
162+
f' <- f
163+
a' <- p
164+
chainl1' p f (f' a a')
165+
) <|> pure a
161166

162167
-- | Parse phrases delimited by a right-associative operator, requiring at least one match.
163168
chainr1 :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a
@@ -166,9 +171,12 @@ chainr1 p f = do
166171
chainr1' p f a
167172

168173
chainr1' :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
169-
chainr1' p f a = (do f' <- f
170-
a' <- chainr1 p f
171-
pure $ f' a a') <|> pure a
174+
chainr1' p f a =
175+
( do
176+
f' <- f
177+
a' <- chainr1 p f
178+
pure $ f' a a'
179+
) <|> pure a
172180

173181
-- | Parse one of a set of alternatives.
174182
choice :: forall f m s a. Foldable f => Monad m => f (ParserT s m a) -> ParserT s m a
@@ -193,10 +201,10 @@ notFollowedBy p = try $ (try p *> fail "Negated parser succeeded") <|> pure unit
193201
manyTill :: forall s a m e. Monad m => ParserT s m a -> ParserT s m e -> ParserT s m (List a)
194202
manyTill p end = scan
195203
where
196-
scan = (end $> Nil)
197-
<|> do x <- p
198-
xs <- scan
199-
pure (x : xs)
204+
scan = (end $> Nil) <|> do
205+
x <- p
206+
xs <- scan
207+
pure (x : xs)
200208

201209
-- | Parse several phrases until the specified terminator matches, requiring at least one match.
202210
many1Till :: forall s a m e. Monad m => ParserT s m a -> ParserT s m e -> ParserT s m (NonEmptyList a)

src/Text/Parsing/Parser/Expr.purs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,27 @@ module Text.Parsing.Parser.Expr
88
import Prelude hiding (between)
99

1010
import Control.Alt ((<|>))
11-
import Data.Foldable (foldr, foldl)
11+
import Data.Foldable (foldl, foldr)
1212
import Data.List (List(..), (:))
1313
import Text.Parsing.Parser (ParserT)
1414
import Text.Parsing.Parser.Combinators (choice, (<?>))
1515

1616
data Assoc = AssocNone | AssocLeft | AssocRight
1717

18-
data Operator m s a = Infix (ParserT s m (a -> a -> a)) Assoc
19-
| Prefix (ParserT s m (a -> a))
20-
| Postfix (ParserT s m (a -> a))
18+
data Operator m s a
19+
= Infix (ParserT s m (a -> a -> a)) Assoc
20+
| Prefix (ParserT s m (a -> a))
21+
| Postfix (ParserT s m (a -> a))
2122

2223
type OperatorTable m s a = Array (Array (Operator m s a))
2324

24-
type SplitAccum m s a = { rassoc :: List (ParserT s m (a -> a -> a))
25-
, lassoc :: List (ParserT s m (a -> a -> a))
26-
, nassoc :: List (ParserT s m (a -> a -> a))
27-
, prefix :: List (ParserT s m (a -> a))
28-
, postfix :: List (ParserT s m (a -> a)) }
25+
type SplitAccum m s a =
26+
{ rassoc :: List (ParserT s m (a -> a -> a))
27+
, lassoc :: List (ParserT s m (a -> a -> a))
28+
, nassoc :: List (ParserT s m (a -> a -> a))
29+
, prefix :: List (ParserT s m (a -> a))
30+
, postfix :: List (ParserT s m (a -> a))
31+
}
2932

3033
-- | Build a parser from an `OperatorTable`.
3134
-- |
@@ -50,28 +53,30 @@ makeParser term ops = do
5053
<|> pure x
5154
<?> "operator"
5255
where
53-
accum = foldr splitOp { rassoc: Nil
54-
, lassoc: Nil
55-
, nassoc: Nil
56-
, prefix: Nil
57-
, postfix: Nil
58-
} ops
59-
60-
rassocOp = choice accum.rassoc
61-
lassocOp = choice accum.lassoc
62-
nassocOp = choice accum.nassoc
63-
prefixOp = choice accum.prefix <?> ""
56+
accum = foldr splitOp
57+
{ rassoc: Nil
58+
, lassoc: Nil
59+
, nassoc: Nil
60+
, prefix: Nil
61+
, postfix: Nil
62+
}
63+
ops
64+
65+
rassocOp = choice accum.rassoc
66+
lassocOp = choice accum.lassoc
67+
nassocOp = choice accum.nassoc
68+
prefixOp = choice accum.prefix <?> ""
6469
postfixOp = choice accum.postfix <?> ""
6570

6671
postfixP = postfixOp <|> pure identity
6772
prefixP = prefixOp <|> pure identity
6873

6974
splitOp :: forall m s a. Operator m s a -> SplitAccum m s a -> SplitAccum m s a
70-
splitOp (Infix op AssocNone) accum = accum { nassoc = op : accum.nassoc }
71-
splitOp (Infix op AssocLeft) accum = accum { lassoc = op : accum.lassoc }
72-
splitOp (Infix op AssocRight) accum = accum { rassoc = op : accum.rassoc }
73-
splitOp (Prefix op) accum = accum { prefix = op : accum.prefix }
74-
splitOp (Postfix op) accum = accum { postfix = op : accum.postfix }
75+
splitOp (Infix op AssocNone) accum = accum { nassoc = op : accum.nassoc }
76+
splitOp (Infix op AssocLeft) accum = accum { lassoc = op : accum.lassoc }
77+
splitOp (Infix op AssocRight) accum = accum { rassoc = op : accum.rassoc }
78+
splitOp (Prefix op) accum = accum { prefix = op : accum.prefix }
79+
splitOp (Postfix op) accum = accum { postfix = op : accum.postfix }
7580

7681
rassocP :: forall m a b c s. Monad m => a -> ParserT s m (a -> a -> a) -> ParserT s m (b -> c) -> ParserT s m b -> ParserT s m (c -> a) -> ParserT s m a
7782
rassocP x rassocOp prefixP term postfixP = do
@@ -101,7 +106,7 @@ nassocP x nassocOp prefixP term postfixP = do
101106

102107
termP :: forall m s a b c. Monad m => ParserT s m (a -> b) -> ParserT s m a -> ParserT s m (b -> c) -> ParserT s m c
103108
termP prefixP term postfixP = do
104-
pre <- prefixP
105-
x <- term
106-
post <- postfixP
109+
pre <- prefixP
110+
x <- term
111+
post <- postfixP
107112
pure (post (pre x))

0 commit comments

Comments
 (0)