Bump version + add telml-parse helpers
Getty Ritter
6 years ago
1 |
packages: |
|
1 | packages: | |
2 | telml/telml.cabal, | |
3 | telml-parse/telml-parse.cabal, | |
4 | telml-markup/telml-markup.cabal |
15 | 15 | exposed-modules: Data.TeLML |
16 | 16 | ghc-options: -Wall |
17 | 17 | other-modules: Data.TeLML.Parser, Data.TeLML.Type |
18 |
build-depends: base >=4.7 && < |
|
18 | build-depends: base >=4.7 && <5, | |
19 | 19 | deepseq >=1.4 && <2 |
20 | 20 | default-language: Haskell2010 |
21 | 21 |
1 | {-# LANGUAGE RecordWildCards #-} | |
2 | ||
3 | module Data.TeLML.Parse | |
4 | ( Fragment(..) | |
5 | , Document | |
6 | , Parse | |
7 | , decode | |
8 | , parse | |
9 | , select | |
10 | , field | |
11 | , text | |
12 | , document | |
13 | , arg | |
14 | , both | |
15 | ) where | |
16 | ||
17 | import Data.TeLML | |
18 | ||
19 | newtype Parse t a = Parse { runParse :: t -> Either String a } | |
20 | ||
21 | decode :: String -> Parse Document r -> Either String r | |
22 | decode str content = case parse str of | |
23 | Left err -> Left err | |
24 | Right x -> runParse content x | |
25 | ||
26 | instance Functor (Parse t) where | |
27 | fmap f (Parse g) = Parse (\ x -> fmap f (g x)) | |
28 | ||
29 | instance Applicative (Parse t) where | |
30 | pure x = Parse (\ _ -> Right x) | |
31 | f <*> x = | |
32 | f >>= \ f' -> | |
33 | x >>= \ x' -> | |
34 | pure (f' x') | |
35 | ||
36 | instance Monad (Parse t) where | |
37 | Parse x >>= f = Parse $ \ s -> | |
38 | case x s of | |
39 | Left err -> Left err | |
40 | Right v -> runParse (f v) s | |
41 | ||
42 | select :: String -> Parse [Document] t -> Parse Document [t] | |
43 | select name content = Parse $ \ s -> each s | |
44 | where | |
45 | each [] = return [] | |
46 | each (Tag t doc:xs) | |
47 | | t == name = (:) <$> runParse content doc <*> each xs | |
48 | each (_:xs) = each xs | |
49 | ||
50 | field :: String -> (Parse [Document] t) -> Parse Document t | |
51 | field name content = Parse $ \ s -> find s | |
52 | where | |
53 | find [] = Left ("Unable to find tag \\" ++ name) | |
54 | find (Tag t doc:_) | |
55 | | t == name = runParse content doc | |
56 | find (_:xs) = find xs | |
57 | ||
58 | arg :: Parse Document t -> Parse [Document] t | |
59 | arg f = Parse $ \ s -> | |
60 | case s of | |
61 | [x] -> runParse f x | |
62 | _ -> Left ("Wrong arity for `arg`: " ++ show (length s)) | |
63 | ||
64 | ||
65 | both :: Parse Document a -> Parse Document b -> Parse [Document] (a, b) | |
66 | both l r = Parse $ \ s -> | |
67 | case s of | |
68 | [a, b] -> (,) <$> runParse l a <*> runParse r b | |
69 | _ -> Left ("Wrong arity for `both`: " ++ show (length s)) | |
70 | ||
71 | text :: Parse Document String | |
72 | text = Parse (\ s -> concat <$> traverse go s) | |
73 | where go (Text str) = Right str | |
74 | go (Tag t _) = Left ("Expected Text fragment, found \\" ++ show t) | |
75 | ||
76 | document :: Parse Document Document | |
77 | document = Parse (\ s -> Right s) |
1 | Copyright (c) 2018, Getty Ritter | |
2 | All rights reserved. | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | |
5 | ||
6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | |
7 | ||
8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | |
9 | ||
10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | |
11 | ||
12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
1 | name: telml-parse | |
2 | version: 0.1.0.0 | |
3 | synopsis: A lightweight TeX-like markup format. | |
4 | description: A lightweight TeX-like markup format. | |
5 | license: BSD3 | |
6 | license-file: LICENSE | |
7 | author: Getty Ritter <gettylefou@gmail.com> | |
8 | maintainer: Getty Ritter <gettylefou@gmail.com> | |
9 | copyright: ©2018 Getty Ritter | |
10 | category: Data | |
11 | build-type: Simple | |
12 | cabal-version: >=1.10 | |
13 | ||
14 | library | |
15 | exposed-modules: Data.TeLML.Parse | |
16 | ghc-options: -Wall | |
17 | build-depends: base >=4.7 && <5, | |
18 | telml ==0.1.0.0 | |
19 | default-language: Haskell2010 | |
20 | ||
21 | source-repository head | |
22 | type: git | |
23 | location: https://github.com/aisamanra/telml |