Skip to content

Commit 95d155c

Browse files
committed
chore: init
0 parents  commit 95d155c

File tree

11 files changed

+399
-0
lines changed

11 files changed

+399
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.stack-work/
2+
*~
3+
dist-newstyle/

CHANGELOG.md

Whitespace-only changes.

README.org

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* megaparsec-utils
2+
Utilities to use on top of the [[https://hackage.haskell.org/package/megaparsec][Megaparsec]] library.

megaparsec-utils.cabal

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
cabal-version: 1.12
2+
3+
-- This file has been generated from package.yaml by hpack version 0.36.0.
4+
--
5+
-- see: https://github.com/sol/hpack
6+
7+
name: megaparsec-utils
8+
version: 0.1.0.0
9+
description: Utilities for the Megaparsec library.
10+
author: drlkf
11+
maintainer: [email protected]
12+
copyright: 2024 drlkf
13+
license: GPL-3
14+
build-type: Simple
15+
extra-source-files:
16+
README.org
17+
CHANGELOG.md
18+
19+
library
20+
exposed-modules:
21+
Lib
22+
Text.Megaparsec.Utils
23+
other-modules:
24+
Paths_megaparsec_utils
25+
hs-source-dirs:
26+
src
27+
ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
28+
build-depends:
29+
base >=4.7 && <5
30+
, megaparsec
31+
, parser-combinators
32+
, uuid
33+
default-language: Haskell2010
34+
35+
test-suite megaparsec-utils-test
36+
type: exitcode-stdio-1.0
37+
main-is: Spec.hs
38+
other-modules:
39+
Text.Megaparsec.UtilsSpec
40+
Paths_megaparsec_utils
41+
hs-source-dirs:
42+
test
43+
ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
44+
build-depends:
45+
QuickCheck
46+
, base >=4.7 && <5
47+
, hspec
48+
, megaparsec
49+
, megaparsec-utils
50+
, parser-combinators
51+
, uuid
52+
default-language: Haskell2010

package.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: megaparsec-utils
2+
version: 0.1.0.0
3+
license: GPL-3
4+
author: drlkf
5+
maintainer: [email protected]
6+
copyright: 2024 drlkf
7+
8+
extra-source-files:
9+
- README.org
10+
- CHANGELOG.md
11+
12+
description: Utilities for the Megaparsec library.
13+
14+
dependencies:
15+
- base >= 4.7 && < 5
16+
- megaparsec
17+
- parser-combinators
18+
- uuid
19+
20+
ghc-options:
21+
- -Wall
22+
- -Werror
23+
- -Wcompat
24+
- -Widentities
25+
- -Wincomplete-record-updates
26+
- -Wincomplete-uni-patterns
27+
- -Wmissing-export-lists
28+
- -Wmissing-home-modules
29+
- -Wpartial-fields
30+
- -Wredundant-constraints
31+
32+
library:
33+
source-dirs: src
34+
35+
tests:
36+
megaparsec-utils-test:
37+
main: Spec.hs
38+
source-dirs: test
39+
ghc-options:
40+
- -threaded
41+
- -rtsopts
42+
- -with-rtsopts=-N
43+
dependencies:
44+
- megaparsec-utils
45+
- hspec
46+
- QuickCheck

src/Lib.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Lib
2+
( someFunc
3+
) where
4+
5+
someFunc :: IO ()
6+
someFunc = putStrLn "someFunc"

src/Text/Megaparsec/Utils.hs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{-# LANGUAGE TypeFamilies #-}
2+
3+
module Text.Megaparsec.Utils
4+
( boolParser
5+
, boundedEnumShowParser
6+
, commaSeparated
7+
, numParser
8+
, occurrence
9+
, occurrences
10+
, parsecToReadsPrec
11+
, posNumParser
12+
, uuidParser
13+
) where
14+
15+
import Control.Applicative (many, some, (<|>))
16+
import Control.Applicative.Combinators (choice)
17+
import Control.Monad (replicateM, void)
18+
import Data.Functor (($>))
19+
import Data.List (intercalate, sortOn)
20+
import Data.List.NonEmpty (NonEmpty ((:|)))
21+
import Data.Maybe (fromJust)
22+
import Data.UUID (UUID)
23+
import qualified Data.UUID as U (fromString)
24+
import Data.Void (Void)
25+
import Text.Megaparsec (Parsec, anySingle, runParser,
26+
try)
27+
import Text.Megaparsec.Char (char, digitChar, hexDigitChar,
28+
string')
29+
30+
-- | Parse a case-insensitive human-readable boolean, including C-style numbers
31+
-- and English yes-no.
32+
boolParser :: Parsec Void String Bool
33+
boolParser = true <|> false
34+
where true = True <$ choice (map string' ["true", "y", "yes", "1"])
35+
false = False <$ choice (map string' ["false", "n", "no", "0"])
36+
37+
-- | Parse a 'Bounded' 'Enum' type that has a 'Show' instance, trying all
38+
-- possibilities, case-insensitive, in the 'Enum' order.
39+
boundedEnumShowParser
40+
:: Bounded a
41+
=> Enum a
42+
=> Show a
43+
=> Parsec Void String a
44+
boundedEnumShowParser =
45+
choice . map parseShow $ sortOn (negate . length . show) [minBound ..]
46+
where parseShow a = string' (show a) $> a
47+
48+
-- | Parse a comma-separated list of items.
49+
commaSeparated :: Parsec Void String a -> Parsec Void String (NonEmpty a)
50+
commaSeparated p = (:|) <$> p <*> many (char ',' >> p)
51+
52+
-- | Parse any occurrence of a given parser. Consumes any input before occurence.
53+
occurrence :: Parsec Void String a -> Parsec Void String a
54+
occurrence p = go
55+
where go = p <|> (anySingle >> go)
56+
57+
-- | Parse all occurrences of a given parser.
58+
occurrences :: Parsec Void String a -> Parsec Void String [a]
59+
occurrences = some . try . occurrence
60+
61+
-- | Parse a positive integer.
62+
posNumParser :: Read a => Parsec Void String a
63+
posNumParser = read <$> some digitChar
64+
65+
-- | Parse an integer, without any spaces between minus sign and digits.
66+
numParser :: Parsec Void String Int
67+
numParser = (char '-' >> negate <$> posNumParser) <|> posNumParser
68+
69+
-- | Convert a 'Parsec' parser into a 'ReadS' parser. Useful for defining 'Read'
70+
-- instances with 'Megaparsec'.
71+
parsecToReadsPrec :: Parsec Void String a -> ReadS a
72+
parsecToReadsPrec p = either (const []) (\x -> [(x, "")]) . runParser p "string"
73+
74+
-- | Parse a RFC4122-compliant UUID.
75+
uuidParser :: Parsec Void String UUID
76+
uuidParser = do
77+
part1 <- replicateM 8 hexDigitChar
78+
void $ char '-'
79+
part2 <- replicateM 4 hexDigitChar
80+
void $ char '-'
81+
part3 <- replicateM 4 hexDigitChar
82+
void $ char '-'
83+
part4 <- replicateM 4 hexDigitChar
84+
void $ char '-'
85+
part5 <- replicateM 12 hexDigitChar
86+
87+
pure . fromJust . U.fromString $ intercalate "-" [part1, part2, part3, part4, part5]

stack.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resolver:
2+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/25.yaml
3+
4+
packages:
5+
- .

stack.yaml.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This file was autogenerated by Stack.
2+
# You should not edit this file by hand.
3+
# For more information, please see the documentation at:
4+
# https://docs.haskellstack.org/en/stable/lock_files
5+
6+
packages: []
7+
snapshots:
8+
- completed:
9+
sha256: a81fb3877c4f9031e1325eb3935122e608d80715dc16b586eb11ddbff8671ecd
10+
size: 640086
11+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/25.yaml
12+
original:
13+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/25.yaml

test/Spec.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{-# OPTIONS_GHC -Wno-missing-export-lists #-}
2+
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

0 commit comments

Comments
 (0)