From 6306edab842d8ccb092ae795414e8d490946cce4 Mon Sep 17 00:00:00 2001 From: thepenguin Date: Wed, 26 Oct 2022 15:29:48 +0200 Subject: [PATCH] Start to use megaparsec instead of P_to_S --- README.org | 14 ++++++++++++++ app/Main.hs | 43 ++++++++++++++++++++++++----------------- otm.cabal | 3 ++- stack.yaml | 11 +++++++++-- stack.yaml.lock | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 100 insertions(+), 22 deletions(-) diff --git a/README.org b/README.org index 1f97f66..ae3cf84 100644 --- a/README.org +++ b/README.org @@ -3,3 +3,17 @@ Org to mafiauniverse is a parser which transforms org text into forum posts for the forum mafiauniverse. The name is pronounced like autumn. + +* Spec + +| org | forum | +|-----+-----------------| +| * | [TITLE][/TITLE] | +| ** | [SIZE=4][/SIZE] | +| *** | [SIZE=2][/SIZE] | +| | | +| | | +| | | +| | | +| | | +| | | diff --git a/app/Main.hs b/app/Main.hs index 708b333..6e1c30f 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,39 +1,46 @@ module Main where +import Control.Applicative import Control.Monad import Data.Functor import System.Environment import System.Exit +import Text.Megaparsec hiding (parse, satisfy) import Text.ParserCombinators.ReadP - - parse :: ReadP String -> String -> String -parse rules = fst . last .readP_to_S rules +parse rules = fst . last . readP_to_S rules + +parseFile :: String -> String +parseFile = unlines . map (parse fileParser) . lines + +fileParser :: ReadP String +fileParser = undefined main :: IO () -main = putStrLn =<< helper =<< getArgs - -helper :: [String] -> IO String -helper [] = return usage -helper (x:[]) = if (head x /= '-') - then readFile x - else return $ useArgs $ parse argParse x -helper (x:_:[]) = return $ useArgs $ parse argParse x -helper _ = fail "Too many arugemnts" +main = putStr =<< parseCli =<< getArgs +parseCli :: [String] -> IO String +parseCli [] = return usage +parseCli [x] = + if head x /= '-' + then parseFile <$> readFile x + else return $ useArgs $ parse argParse x +parseCli [x, _] = return $ useArgs $ parse argParse x +parseCli _ = fail "Too many arugemnts" argParse :: ReadP String argParse = - string "-" >> (many1 (satisfy (/= ' '))) + string "-" >> many1 (satisfy (/= ' ')) useArgs :: String -> String -useArgs [] = "" -useArgs ('h':_) = usage -useArgs ('v':_) = version -useArgs (_:xs) = useArgs xs +useArgs [] = "" +useArgs ('h' : _) = usage +useArgs ('v' : _) = version +useArgs (_ : xs) = useArgs xs usage :: String -usage = "Usage: otm [-hv] [file]" +usage = "Usage: otm [-hv] [file]" + version :: String version = "otm 0.1" diff --git a/otm.cabal b/otm.cabal index 4ba2393..56e4ffa 100644 --- a/otm.cabal +++ b/otm.cabal @@ -67,7 +67,8 @@ executable otm -- other-extensions: -- Other library packages from which modules are imported. - build-depends: base ^>=4.17.0.0 + build-depends: base ^>=4.16.3.0 + , megaparsec -- Directories containing source files. hs-source-dirs: app diff --git a/stack.yaml b/stack.yaml index d503c61..66020a5 100644 --- a/stack.yaml +++ b/stack.yaml @@ -17,7 +17,7 @@ # # resolver: ./custom-snapshot.yaml # resolver: https://example.com/snapshots/2018-01-01.yaml -resolver: ghc-9.4.2 +resolver: ghc-9.2.4 # User packages to be built. # Various formats can be used as shown in the example below. @@ -39,7 +39,14 @@ packages: # - git: https://github.com/commercialhaskell/stack.git # commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a # -extra-deps: [] +extra-deps: + - megaparsec-9.2.0@sha256:0248397bedfddbb94584925a09ba793cf2327362a3183624bd7bfaed529163cb,3347 + - case-insensitive-1.2.1.0@sha256:9dfd3171fc7698cf8d931727d3af3a7b389135b583e46b5adac1f9d2026fff61,2244 + - parser-combinators-1.3.0@sha256:edd54ba56cbae8fadbcceebcfef31b2c70a835e92e5eda41151b939c40647281,1570 + - scientific-0.3.7.0@sha256:76465a82beb2af6ea83ebd00684acc0ffe659e7da7066329931dc8f02fc97507,4826 + - hashable-1.4.0.2@sha256:0cddd0229d1aac305ea0404409c0bbfab81f075817bd74b8b2929eff58333e55,5005 + - integer-logarithms-1.0.3.1@sha256:b65e11ec6f4b29c5278716da0544b951a49ab5310608df0fc41eec29f15691d9,3229 + - primitive-0.7.3.0@sha256:6b28a1c0572f5ca50597ba8388aeade21515842969ae192cdc6bfca81367bf56,2951 # Override default flag values for local packages and extra-deps # flags: {} diff --git a/stack.yaml.lock b/stack.yaml.lock index 74c2087..b562c16 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -3,5 +3,54 @@ # For more information, please see the documentation at: # https://docs.haskellstack.org/en/stable/lock_files -packages: [] +packages: +- completed: + hackage: megaparsec-9.2.0@sha256:0248397bedfddbb94584925a09ba793cf2327362a3183624bd7bfaed529163cb,3347 + pantry-tree: + sha256: 71403f455a514f62cb0c62145b6f6ce19bec34b16ed150a53df02ea9d773d0be + size: 1518 + original: + hackage: megaparsec-9.2.0@sha256:0248397bedfddbb94584925a09ba793cf2327362a3183624bd7bfaed529163cb,3347 +- completed: + hackage: case-insensitive-1.2.1.0@sha256:9dfd3171fc7698cf8d931727d3af3a7b389135b583e46b5adac1f9d2026fff61,2244 + pantry-tree: + sha256: d6f175b17eacce002fc5336c17e119d989bd97f8ad9702446163c9e27c7c79b7 + size: 688 + original: + hackage: case-insensitive-1.2.1.0@sha256:9dfd3171fc7698cf8d931727d3af3a7b389135b583e46b5adac1f9d2026fff61,2244 +- completed: + hackage: parser-combinators-1.3.0@sha256:edd54ba56cbae8fadbcceebcfef31b2c70a835e92e5eda41151b939c40647281,1570 + pantry-tree: + sha256: 0754c8d3f213156877f8085da2ad74433167a6c30272b8b64119a2628bd7ade0 + size: 795 + original: + hackage: parser-combinators-1.3.0@sha256:edd54ba56cbae8fadbcceebcfef31b2c70a835e92e5eda41151b939c40647281,1570 +- completed: + hackage: scientific-0.3.7.0@sha256:76465a82beb2af6ea83ebd00684acc0ffe659e7da7066329931dc8f02fc97507,4826 + pantry-tree: + sha256: 2e5436cd8a2d2fa045aadc7619aa06cf18f4f77d01b70d8c517213b718b69a2b + size: 657 + original: + hackage: scientific-0.3.7.0@sha256:76465a82beb2af6ea83ebd00684acc0ffe659e7da7066329931dc8f02fc97507,4826 +- completed: + hackage: hashable-1.4.0.2@sha256:0cddd0229d1aac305ea0404409c0bbfab81f075817bd74b8b2929eff58333e55,5005 + pantry-tree: + sha256: 51dda0aa70849588d074125f324beba1c8736cfa486e7f4a90a798b1d0e6019d + size: 1248 + original: + hackage: hashable-1.4.0.2@sha256:0cddd0229d1aac305ea0404409c0bbfab81f075817bd74b8b2929eff58333e55,5005 +- completed: + hackage: integer-logarithms-1.0.3.1@sha256:b65e11ec6f4b29c5278716da0544b951a49ab5310608df0fc41eec29f15691d9,3229 + pantry-tree: + sha256: 804c8c0d77470f84640ee70c66155f770798e295c0a0572e41d071f69e24ca4e + size: 865 + original: + hackage: integer-logarithms-1.0.3.1@sha256:b65e11ec6f4b29c5278716da0544b951a49ab5310608df0fc41eec29f15691d9,3229 +- completed: + hackage: primitive-0.7.3.0@sha256:6b28a1c0572f5ca50597ba8388aeade21515842969ae192cdc6bfca81367bf56,2951 + pantry-tree: + sha256: 4dd118201091c04a138a25e262ab61e8e64e811832eadd8b6b39d56504dfe045 + size: 1655 + original: + hackage: primitive-0.7.3.0@sha256:6b28a1c0572f5ca50597ba8388aeade21515842969ae192cdc6bfca81367bf56,2951 snapshots: []