File tree 3 files changed +60
-35
lines changed
3 files changed +60
-35
lines changed Original file line number Diff line number Diff line change 1
- name : parser-combinators
2
- version : 0.1.0.0
3
- github : " githubuser/parser-combinators"
4
- license : BSD3
5
- author : " Author name here"
6
-
7
- copyright : " 2020 Author name here"
1
+ name : parser-combinators
2
+ version : 0.1.0.0
3
+ github : " githubuser/parser-combinators"
4
+ license : BSD3
5
+ author : " Author name here"
6
+
7
+ copyright : " 2020 Author name here"
8
8
9
9
extra-source-files :
10
- - README.md
11
- - ChangeLog.md
10
+ - README.md
11
+ - ChangeLog.md
12
12
13
13
# Metadata used when publishing your package
14
14
# synopsis: Short description of your package
@@ -17,32 +17,34 @@ extra-source-files:
17
17
# To avoid duplicated efforts in documentation and dealing with the
18
18
# complications of embedding Haddock markup inside cabal files, it is
19
19
# common to point users to the README.md file.
20
- description : Please see the README on GitHub at <https://github.com/githubuser/parser-combinators#readme>
20
+ description : Please see the README on GitHub at <https://github.com/githubuser/parser-combinators#readme>
21
21
22
22
dependencies :
23
- - base >= 4.7 && < 5
23
+ - base >= 4.7 && < 5
24
+ - text
25
+ - containers
24
26
25
27
library :
26
28
source-dirs : src
27
29
28
30
executables :
29
31
parser-combinators-exe :
30
- main : Main.hs
31
- source-dirs : app
32
+ main : Main.hs
33
+ source-dirs : app
32
34
ghc-options :
33
- - -threaded
34
- - -rtsopts
35
- - -with-rtsopts=-N
35
+ - -threaded
36
+ - -rtsopts
37
+ - -with-rtsopts=-N
36
38
dependencies :
37
- - parser-combinators
39
+ - parser-combinators
38
40
39
41
tests :
40
42
parser-combinators-test :
41
- main : Spec.hs
42
- source-dirs : test
43
+ main : Spec.hs
44
+ source-dirs : test
43
45
ghc-options :
44
- - -threaded
45
- - -rtsopts
46
- - -with-rtsopts=-N
46
+ - -threaded
47
+ - -rtsopts
48
+ - -with-rtsopts=-N
47
49
dependencies :
48
- - parser-combinators
50
+ - parser-combinators
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ cabal-version: 1.12
4
4
--
5
5
-- see: https://github.com/sol/hpack
6
6
--
7
- -- hash: 0330f6479464e5ffbbb1589d96011a9a3c47821ba74b14434ee6d16e3e138712
7
+ -- hash: 431ab4fb1b73226f52fcb1b718332fbbedb7a2139aaf244e7c3dbeb952b40bc8
8
8
9
9
name : parser-combinators
10
10
version : 0.1.0.0
@@ -35,6 +35,8 @@ library
35
35
src
36
36
build-depends :
37
37
base >= 4.7 && < 5
38
+ , containers
39
+ , text
38
40
default-language : Haskell2010
39
41
40
42
executable parser-combinators-exe
@@ -46,7 +48,9 @@ executable parser-combinators-exe
46
48
ghc-options : -threaded -rtsopts -with-rtsopts=-N
47
49
build-depends :
48
50
base >= 4.7 && < 5
51
+ , containers
49
52
, parser-combinators
53
+ , text
50
54
default-language : Haskell2010
51
55
52
56
test-suite parser-combinators-test
@@ -59,5 +63,7 @@ test-suite parser-combinators-test
59
63
ghc-options : -threaded -rtsopts -with-rtsopts=-N
60
64
build-depends :
61
65
base >= 4.7 && < 5
66
+ , containers
62
67
, parser-combinators
68
+ , text
63
69
default-language : Haskell2010
Original file line number Diff line number Diff line change
1
+ {-# LANGUAGE OverloadedStrings #-}
2
+
1
3
module Parser
2
4
(
3
5
)
4
6
where
5
7
8
+ import qualified Data.Map as M
9
+ import qualified Data.Text as T
6
10
import Text.Read (readMaybe )
7
11
8
- newtype Parser a = Parser { runParser :: String -> Maybe (a , String )}
12
+ data JsonVal
13
+ = -- Maybe indicates the fractional part of the number. This will obviously be `Nothing` if it's
14
+ -- just a plain integer.
15
+ JsonNumber Integer (Maybe Integer )
16
+ | JsonBool Bool
17
+ | JsonNull
18
+ | JsonString T. Text
19
+ | JsonArray [JsonVal ]
20
+ | JsonObj (M. Map T. Text JsonVal )
21
+ deriving (Eq , Show )
22
+
23
+ newtype Parser a = Parser { parse :: T. Text -> Maybe (a , T. Text )}
9
24
10
- parse :: Parser a -> String -> a
11
- parse m s =
12
- case runParser m s of
13
- Just (res, [] ) -> res
14
- Just (_, rest) -> error " Parser did not consume entire stream"
25
+ runParser :: Parser a -> T. Text -> a
26
+ runParser m s =
27
+ case parse m s of
28
+ Just (res, " " ) -> res
29
+ Just (_, rest) -> error " Parser did not consume the entire stream"
15
30
Nothing -> error " Parser did not manage to parse anything"
16
31
17
- item = Parser $ \ s ->
32
+ -- A test parser
33
+ digit :: Parser Int
34
+ digit = Parser $ \ s ->
18
35
case s of
19
- [] -> Nothing
20
- (c : cs) ->
21
- let rc = readMaybe [c] :: Maybe Int
22
- in case rc of
36
+ " " -> Nothing
37
+ t ->
38
+ let ch = T. head t; cs = T. tail t
39
+ in case readMaybe [ch] of
23
40
Just x -> Just (x, cs)
24
41
Nothing -> Nothing
You can’t perform that action at this time.
0 commit comments