Start to use megaparsec instead of P_to_S

This commit is contained in:
thepenguin 2022-10-26 15:29:48 +02:00
parent 93509d0ac8
commit 6306edab84
No known key found for this signature in database
GPG Key ID: F258C8C10D060D5E
5 changed files with 100 additions and 22 deletions

View File

@ -3,3 +3,17 @@
Org to mafiauniverse is a parser which transforms org text into forum posts for the forum mafiauniverse. Org to mafiauniverse is a parser which transforms org text into forum posts for the forum mafiauniverse.
The name is pronounced like autumn. The name is pronounced like autumn.
* Spec
| org | forum |
|-----+-----------------|
| * | [TITLE][/TITLE] |
| ** | [SIZE=4][/SIZE] |
| *** | [SIZE=2][/SIZE] |
| | |
| | |
| | |
| | |
| | |
| | |

View File

@ -1,39 +1,46 @@
module Main where module Main where
import Control.Applicative
import Control.Monad import Control.Monad
import Data.Functor import Data.Functor
import System.Environment import System.Environment
import System.Exit import System.Exit
import Text.Megaparsec hiding (parse, satisfy)
import Text.ParserCombinators.ReadP import Text.ParserCombinators.ReadP
parse :: ReadP String -> String -> String 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 :: IO ()
main = putStrLn =<< helper =<< getArgs main = putStr =<< parseCli =<< 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"
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 :: ReadP String
argParse = argParse =
string "-" >> (many1 (satisfy (/= ' '))) string "-" >> many1 (satisfy (/= ' '))
useArgs :: String -> String useArgs :: String -> String
useArgs [] = "" useArgs [] = ""
useArgs ('h':_) = usage useArgs ('h' : _) = usage
useArgs ('v':_) = version useArgs ('v' : _) = version
useArgs (_:xs) = useArgs xs useArgs (_ : xs) = useArgs xs
usage :: String usage :: String
usage = "Usage: otm [-hv] [file]" usage = "Usage: otm [-hv] [file]"
version :: String version :: String
version = "otm 0.1" version = "otm 0.1"

View File

@ -67,7 +67,8 @@ executable otm
-- other-extensions: -- other-extensions:
-- Other library packages from which modules are imported. -- 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. -- Directories containing source files.
hs-source-dirs: app hs-source-dirs: app

View File

@ -17,7 +17,7 @@
# #
# resolver: ./custom-snapshot.yaml # resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.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. # User packages to be built.
# Various formats can be used as shown in the example below. # Various formats can be used as shown in the example below.
@ -39,7 +39,14 @@ packages:
# - git: https://github.com/commercialhaskell/stack.git # - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a # 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 # Override default flag values for local packages and extra-deps
# flags: {} # flags: {}

View File

@ -3,5 +3,54 @@
# For more information, please see the documentation at: # For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files # 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: [] snapshots: []