Skip to content

Commit f9b93a1

Browse files
committed
Parser: ref: more Text, use takeWhile{,1}P
megaparsec texts promise takeWile{,1}P to give huge (magnitude) of speedup for according operation.
1 parent 02b33ff commit f9b93a1

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/Nix/Parser.hs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import Data.Data ( Data(..) )
6363
import Data.Fix ( Fix(..) )
6464
import qualified Data.HashSet as HashSet
6565
import qualified Data.Map as Map
66-
import Data.Text ( cons )
66+
import qualified Data.Text as Text
6767
import Nix.Utils
6868
import Nix.Expr.Types
6969
import Nix.Expr.Shorthands hiding ( ($>) )
@@ -251,19 +251,17 @@ nixUri =
251251
do
252252
start <- letterChar
253253
protocol <-
254-
many $
255-
satisfy $
256-
\ x ->
257-
isAlphanumeric x
258-
|| (`elem` ("+-." :: String)) x
259-
_ <- chunk ":"
254+
takeWhileP Nothing $
255+
\ x ->
256+
isAlphanumeric x
257+
|| (`elem` ("+-." :: String)) x
258+
_ <- single ':'
260259
address <-
261-
some $
262-
satisfy $
260+
takeWhile1P Nothing $
263261
\ x ->
264262
isAlphanumeric x
265263
|| (`elem` ("%/?:@&=+$,-_.!~*'" :: String)) x
266-
pure $ NStr $ DoubleQuoted $ one $ Plain $ toText $ one start <> protocol <> ":" <> address
264+
pure $ NStr $ DoubleQuoted $ one $ Plain $ start `Text.cons` protocol <> ":" <> address
267265

268266

269267
-- ** Strings
@@ -343,7 +341,7 @@ identifier =
343341
try $
344342
do
345343
(coerce -> iD) <-
346-
liftA2 cons
344+
liftA2 Text.cons
347345
(satisfy (\x -> isAlpha x || x == '_'))
348346
(takeWhileP mempty identLetter)
349347
guard $ not $ iD `HashSet.member` reservedNames
@@ -422,14 +420,14 @@ slash =
422420

423421
pathStr :: Parser Path
424422
pathStr =
425-
lexeme . coerce $
423+
lexeme $ coerce . toString <$>
426424
liftA2 (<>)
427-
(many $ satisfy pathChar)
428-
(concat <$>
425+
(takeWhileP Nothing pathChar)
426+
(Text.concat <$>
429427
some
430-
(liftA2 (:)
428+
(liftA2 Text.cons
431429
slash
432-
(some $ satisfy pathChar)
430+
(takeWhile1P Nothing pathChar)
433431
)
434432
)
435433

0 commit comments

Comments
 (0)