Skip to content

Commit 26b498b

Browse files
committed
feat: add AST for parsing JSON
1 parent 78e48a2 commit 26b498b

File tree

3 files changed

+60
-35
lines changed

3 files changed

+60
-35
lines changed

package.yaml

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
name: parser-combinators
2-
version: 0.1.0.0
3-
github: "githubuser/parser-combinators"
4-
license: BSD3
5-
author: "Author name here"
6-
maintainer: "[email protected]"
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+
maintainer: "[email protected]"
7+
copyright: "2020 Author name here"
88

99
extra-source-files:
10-
- README.md
11-
- ChangeLog.md
10+
- README.md
11+
- ChangeLog.md
1212

1313
# Metadata used when publishing your package
1414
# synopsis: Short description of your package
@@ -17,32 +17,34 @@ extra-source-files:
1717
# To avoid duplicated efforts in documentation and dealing with the
1818
# complications of embedding Haddock markup inside cabal files, it is
1919
# 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>
2121

2222
dependencies:
23-
- base >= 4.7 && < 5
23+
- base >= 4.7 && < 5
24+
- text
25+
- containers
2426

2527
library:
2628
source-dirs: src
2729

2830
executables:
2931
parser-combinators-exe:
30-
main: Main.hs
31-
source-dirs: app
32+
main: Main.hs
33+
source-dirs: app
3234
ghc-options:
33-
- -threaded
34-
- -rtsopts
35-
- -with-rtsopts=-N
35+
- -threaded
36+
- -rtsopts
37+
- -with-rtsopts=-N
3638
dependencies:
37-
- parser-combinators
39+
- parser-combinators
3840

3941
tests:
4042
parser-combinators-test:
41-
main: Spec.hs
42-
source-dirs: test
43+
main: Spec.hs
44+
source-dirs: test
4345
ghc-options:
44-
- -threaded
45-
- -rtsopts
46-
- -with-rtsopts=-N
46+
- -threaded
47+
- -rtsopts
48+
- -with-rtsopts=-N
4749
dependencies:
48-
- parser-combinators
50+
- parser-combinators

parser-combinators.cabal

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 0330f6479464e5ffbbb1589d96011a9a3c47821ba74b14434ee6d16e3e138712
7+
-- hash: 431ab4fb1b73226f52fcb1b718332fbbedb7a2139aaf244e7c3dbeb952b40bc8
88

99
name: parser-combinators
1010
version: 0.1.0.0
@@ -35,6 +35,8 @@ library
3535
src
3636
build-depends:
3737
base >=4.7 && <5
38+
, containers
39+
, text
3840
default-language: Haskell2010
3941

4042
executable parser-combinators-exe
@@ -46,7 +48,9 @@ executable parser-combinators-exe
4648
ghc-options: -threaded -rtsopts -with-rtsopts=-N
4749
build-depends:
4850
base >=4.7 && <5
51+
, containers
4952
, parser-combinators
53+
, text
5054
default-language: Haskell2010
5155

5256
test-suite parser-combinators-test
@@ -59,5 +63,7 @@ test-suite parser-combinators-test
5963
ghc-options: -threaded -rtsopts -with-rtsopts=-N
6064
build-depends:
6165
base >=4.7 && <5
66+
, containers
6267
, parser-combinators
68+
, text
6369
default-language: Haskell2010

src/Parser.hs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
13
module Parser
24
(
35
)
46
where
57

8+
import qualified Data.Map as M
9+
import qualified Data.Text as T
610
import Text.Read (readMaybe)
711

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)}
924

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"
1530
Nothing -> error "Parser did not manage to parse anything"
1631

17-
item = Parser $ \s ->
32+
-- A test parser
33+
digit :: Parser Int
34+
digit = Parser $ \s ->
1835
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
2340
Just x -> Just (x, cs)
2441
Nothing -> Nothing

0 commit comments

Comments
 (0)