Advertisement
Guest User

Untitled

a guest
Sep 11th, 2020
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE DeriveDataTypeable#-}
  2.  
  3. {- Extract stuff from Crusader King 3's 00_landed_titles.txt.
  4.  
  5.    You'll need parsec, syb (and containers). And of course GHC.
  6.    Super dirty code intended to put Perl to shame.
  7.    Boost licensed in lieu of public domain.
  8.  
  9. Permission is hereby granted, free of charge, to any person or organization
  10. obtaining a copy of the software and accompanying documentation covered by
  11. this license (the "Software") to use, reproduce, display, distribute,
  12. execute, and transmit the Software, and to prepare derivative works of the
  13. Software, and to permit third-parties to whom the Software is furnished to
  14. do so, all subject to the following:
  15.  
  16. The copyright notices in the Software and this entire statement, including
  17. the above license grant, this restriction and the following disclaimer,
  18. must be included in all copies of the Software, in whole or in part, and
  19. all derivative works of the Software, unless such copies or derivative
  20. works are solely in the form of machine-executable object code generated by
  21. a source language processor.
  22.  
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  26. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  27. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  28. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. DEALINGS IN THE SOFTWARE.
  30.  
  31. -}
  32. module Main where
  33. import Text.Parsec.String
  34. import Text.Parsec.Combinator
  35. import Text.Parsec.Char
  36. import Text.Parsec.Prim
  37.  
  38. import Text.Parsec.Language (javaStyle)
  39. import qualified Text.Parsec.Token as Tok
  40.  
  41. import Data.Generics
  42. import Data.Set as Set
  43. import Data.Map as Map
  44. import Data.List (isPrefixOf)
  45. import Control.Monad
  46.  
  47.  
  48.  
  49. main :: IO ()
  50. main    = do{ result <- parseFromFile (optional bom *> titles <* eof) "00_landed_titles.txt"
  51.              ; case result of
  52.                  Left err  -> print err
  53.                  Right ast  -> do
  54.                    let x = everything (Map.unionWith Set.union) (mkQ Map.empty foo) ast
  55.  
  56.                    forM_ (Map.toDescList x) $ \(n, xs) -> do
  57.                      putStr "##### " >> putStr (show $ Set.size xs)
  58.                      putStr " counties with " >> putStr (show n) >> putStrLn " baronies: "
  59.                      putStrLn . unwords $ Set.toAscList xs
  60.                      putStrLn ""
  61.              }
  62.  
  63. foo (Def (Ident n) x)  | "c_" `isPrefixOf` n = Map.singleton (everything (+) (mkQ 0 bar) x) (Set.singleton n)
  64. foo _ = Map.empty
  65.  
  66. bar (Def (Ident n) x) | "b_" `isPrefixOf` n = 1
  67. bar _ = 0
  68.  
  69.  
  70.  
  71.  
  72. lexer = Tok.makeTokenParser javaStyle { Tok.commentLine = "#"
  73.                                       , Tok.identStart = letter <|> char '@'
  74.                                       , Tok.identLetter = alphaNum <|> oneOf ":_-'"
  75.                                       }
  76. natural = Tok.natural lexer
  77. white = Tok.whiteSpace lexer
  78.  
  79. bom :: Parser ()
  80. bom = void $ char '\65279'
  81.  
  82.  
  83. data Item
  84.   = Number Integer
  85.   | Def Ident Rhs
  86.   | Bare Ident
  87.   deriving (Show, Typeable,Data, Eq, Ord)
  88.  
  89. newtype Ident = Ident String
  90.   deriving (Show, Typeable,Data, Eq, Ord)
  91. data Rhs = Single Item | Multi [Item] | Hsv ()
  92.   deriving (Show, Typeable,Data, Eq, Ord)
  93.  
  94. symbol = Tok.symbol lexer
  95.  
  96. braces = between (symbol "{") (symbol "}")
  97.  
  98. titles :: Parser [Item]
  99. titles = many1 item
  100.  
  101.  
  102. item = Number <$> natural <|> foo
  103.   where
  104.     foo = do
  105.       i <- ident
  106.       o <- optionMaybe $ Tok.reservedOp lexer "="
  107.       case o of
  108.         Nothing -> return $ Bare i
  109.         Just _ -> Def <$> pure i <*> rhs
  110.  
  111.  
  112.  
  113. ident = Ident <$> Tok.identifier lexer
  114.  
  115. rhs =
  116.  
  117.   (  (try $ Hsv <$> (void $ symbol "hsv" *> braces (many1 $ Tok.naturalOrFloat lexer))) <?> "hsv") -- Don't tell me that wasn't hacked in after the fact
  118.   <|> (try  (Multi <$> braces titles) <?> "multi")
  119.   <|> ( (Single <$> item) <?> "bare")
  120.  
  121.  
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement