| 1 |
{- |
| 2 |
- Language/Ding/Parser.hs - parser |
| 3 |
- |
| 4 |
- Copyright 2020-2022 Einhard Leichtfuß |
| 5 |
- |
| 6 |
- This file is part of ding2tei-haskell. |
| 7 |
- |
| 8 |
- ding2tei-haskell is free software: you can redistribute it and/or modify |
| 9 |
- it under the terms of the GNU Affero General Public License as published |
| 10 |
- by the Free Software Foundation, either version 3 of the License, or |
| 11 |
- (at your option) any later version. |
| 12 |
- |
| 13 |
- ding2tei-haskell is distributed in the hope that it will be useful, |
| 14 |
- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
- GNU Affero General Public License for more details. |
| 17 |
- |
| 18 |
- You should have received a copy of the GNU Affero General Public License |
| 19 |
- along with ding2tei-haskell. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
-} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
module Language.Ding.Parser (parse) where |
| 28 |
|
| 29 |
import Prelude hiding (log) |
| 30 |
import Control.Monad (guard) |
| 31 |
import Control.Monad.Writer (runWriter, Writer, mapWriterT) |
| 32 |
import Data.Functor.Identity (Identity(Identity)) |
| 33 |
import Data.Maybe (catMaybes) |
| 34 |
|
| 35 |
import Data.NatLang.Dictionary (Dictionary(Dictionary), Body(Body)) |
| 36 |
import Data.NatLang.Language (Language(German, English)) |
| 37 |
import Language.Ding.Parser.Header (parseHeader) |
| 38 |
import Language.Ding.Parser.Line (parseLine, FailWriter) |
| 39 |
import Language.Ding.Syntax (Ding, Line) |
| 40 |
import Language.Ding.Token (Token(..), Atom(..)) |
| 41 |
|
| 42 |
|
| 43 |
-- Note: |
| 44 |
-- * The parse log is completely separated from the returned value. |
| 45 |
-- * Consequence: Writing both in parallel (to stderr and, e.g., a file) is |
| 46 |
-- difficult. |
| 47 |
-- * This should not affect performance too much, however. The parse log is |
| 48 |
-- usually short and we cannot begin writing the data before most of the |
| 49 |
-- parsing is done, anyways. |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
parse :: [Token] -> (Maybe Ding, [Either String String]) |
| 57 |
parse ts = |
| 58 |
( do |
| 59 |
header <- either (const Nothing) Just eHeader |
| 60 |
guard $ not $ null bodyLines |
| 61 |
return $ Dictionary header German English (Body bodyLines) |
| 62 |
, concat |
| 63 |
[ either (pure . Left) (const []) eHeader |
| 64 |
, bodyLog |
| 65 |
-- Note: Appending a single value is not optimal. Prepending instead |
| 66 |
-- would yield an unusual order. |
| 67 |
, if null bodyLines |
| 68 |
-- The FreeDict XML schema requires at least one entry and no such can |
| 69 |
-- be generated from nothing. |
| 70 |
then pure $ Left "Input contains no valid dictionary entries." |
| 71 |
else [] |
| 72 |
] |
| 73 |
) |
| 74 |
where |
| 75 |
(headerLines, bodyToks) = separateHeaderLines ts |
| 76 |
|
| 77 |
eHeader = parseHeader headerLines |
| 78 |
(bodyLines, bodyLog) = runWriter $ parseBody bodyToks |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
parseBody :: [Token] -> ParseWriter [Line] |
| 83 |
parseBody = fmap catMaybes . mapM (mergeErrorToLog . parseLine) . tokLines |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
type ParseWriter = Writer [Either String String] |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
mergeErrorToLog :: FailWriter a -> ParseWriter (Maybe a) |
| 93 |
mergeErrorToLog = mapWriterT aux |
| 94 |
where |
| 95 |
aux :: Either String (a, [String]) |
| 96 |
-> Identity (Maybe a, [Either String String]) |
| 97 |
aux (Left e) = Identity (Nothing, pure $ Left e) |
| 98 |
aux (Right (x, log)) = Identity (Just x, map Right log) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
separateHeaderLines :: [Token] -> ([String], [Token]) |
| 104 |
separateHeaderLines (Token _ _ (HeaderLine hl) : Token _ _ NL : tls) = |
| 105 |
let (hls, tls') = separateHeaderLines tls |
| 106 |
in (hl : hls, tls') |
| 107 |
separateHeaderLines tls = ([], tls) |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
tokLines :: [Token] -> [[Token]] |
| 116 |
tokLines [] = [] |
| 117 |
tokLines ts = |
| 118 |
let (l, ts') = breakLine ts |
| 119 |
in l : tokLines ts' |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
breakLine :: [Token] -> ([Token], [Token]) |
| 125 |
breakLine (nl@(Token _ _ NL) : ts) = ([nl], ts) |
| 126 |
breakLine (t : ts) = |
| 127 |
let (l, ts') = breakLine ts |
| 128 |
in (t : l, ts') |
| 129 |
breakLine [] = |
| 130 |
-- TODO: Encapsulate error in the logging monad. |
| 131 |
error "Input is not newline-terminated." |
| 132 |
|
| 133 |
|
| 134 |
-- vi: ts=2 sw=2 et |