| 1 |
{- |
| 2 |
- Language/Ding/Partial/PseudoUnit.hs - partial pseudo units |
| 3 |
- |
| 4 |
- Copyright 2020 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.Partial.PseudoUnit |
| 28 |
( PartialPseudoUnit |
| 29 |
, fromSuffixes |
| 30 |
, addToUnit |
| 31 |
, plusGramAnnot |
| 32 |
, plusUsageAnnot |
| 33 |
) where |
| 34 |
|
| 35 |
import Data.List.NonEmpty (NonEmpty) |
| 36 |
import qualified Data.List.NonEmpty as NEList |
| 37 |
|
| 38 |
import Data.NatLang.Grammar (GrammarInfo) |
| 39 |
import Data.NatLang.Usage (Usage) |
| 40 |
import Language.Ding.Syntax (Unit(..)) |
| 41 |
import Language.Ding.Token (Token, tokenToString) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
data PartialPseudoUnit = PartialPseudoUnit |
| 46 |
{ plainToks :: [Token] |
| 47 |
, suffixes :: [String] |
| 48 |
, gramAnnots :: [NonEmpty GrammarInfo] |
| 49 |
, usageAnnots :: [NonEmpty Usage] |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
empty :: PartialPseudoUnit |
| 54 |
empty = PartialPseudoUnit [] [] [] [] |
| 55 |
|
| 56 |
|
| 57 |
fromSuffixes :: [(String, Token)] -> PartialPseudoUnit |
| 58 |
fromSuffixes sufs = empty |
| 59 |
{ suffixes = map fst sufs |
| 60 |
, plainToks = map snd sufs |
| 61 |
} |
| 62 |
|
| 63 |
plusGramAnnot :: PartialPseudoUnit -> NonEmpty GrammarInfo -> PartialPseudoUnit |
| 64 |
plusGramAnnot psu as = psu { gramAnnots = as : gramAnnots psu } |
| 65 |
|
| 66 |
plusUsageAnnot :: PartialPseudoUnit -> NonEmpty Usage -> PartialPseudoUnit |
| 67 |
plusUsageAnnot psu as = psu { usageAnnots = as : usageAnnots psu } |
| 68 |
|
| 69 |
|
| 70 |
addToUnit :: PartialPseudoUnit -> Unit -> Unit |
| 71 |
|
| 72 |
-- Note: |
| 73 |
-- * Due to the use of (++), this is not very efficient (for larger lists). |
| 74 |
-- * This could be improved by applying to a PartialUnit instead, which has |
| 75 |
-- stored its elements in inverse order. |
| 76 |
-- * This would however likely render the HappyParser.y code less |
| 77 |
-- readable. |
| 78 |
addToUnit psu u = u |
| 79 |
{ unitPlain = |
| 80 |
unitPlain u ++ tokenToString (mconcat $ reverse $ plainToks psu) |
| 81 |
, unitSuffixes = unitSuffixes u ++ suffixes psu |
| 82 |
, unitGrammar = |
| 83 |
unitGrammar u ++ concat (reverse $ map NEList.toList $ gramAnnots psu) |
| 84 |
, unitUsages = |
| 85 |
unitUsages u ++ concat (reverse $ map NEList.toList $ usageAnnots psu) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
-- vi: ft=haskell ts=2 sw=2 et |