module Wrench.Translator.Parser (asmParser) where

import Data.Functor
import Relude hiding (many)
import Text.Megaparsec (choice, eof, many)
import Text.Megaparsec.Char (space1)
import Wrench.Translator.Parser.CodeSection
import Wrench.Translator.Parser.DataSection
import Wrench.Translator.Parser.Misc
import Wrench.Translator.Parser.Types
import Wrench.Translator.Types

asmParser ::
    forall isa w.
    (IsWord w, MnemonicParser isa) =>
    Parser [Section isa w Text]
asmParser :: forall isa w.
(IsWord w, MnemonicParser isa) =>
Parser [Section isa w Text]
asmParser =
    do
        let cstart :: String
cstart = forall m. CommentStart m => String
commentStart @isa
        secs <-
            [Maybe (Section isa w Text)] -> [Section isa w Text]
forall a. [Maybe a] -> [a]
catMaybes
                ([Maybe (Section isa w Text)] -> [Section isa w Text])
-> ParsecT Void String Identity [Maybe (Section isa w Text)]
-> ParsecT Void String Identity [Section isa w Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void String Identity (Maybe (Section isa w Text))
-> ParsecT Void String Identity [Maybe (Section isa w Text)]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many
                    ( [ParsecT Void String Identity (Maybe (Section isa w Text))]
-> ParsecT Void String Identity (Maybe (Section isa w Text))
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
                        [ ParsecT Void String Identity ()
-> ParsecT Void String Identity (Maybe (Section isa w Text))
forall (m :: * -> *) a b. Monad m => m a -> m (Maybe b)
nothing (ParsecT Void String Identity ()
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m ()
space1 ParsecT Void String Identity ()
-> ParsecT Void String Identity ()
-> ParsecT Void String Identity ()
forall a.
ParsecT Void String Identity a
-> ParsecT Void String Identity a -> ParsecT Void String Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> ParsecT Void String Identity ()
eol' String
cstart)
                        , String -> Parser (Section isa w Text)
forall w isa. IsWord w => String -> Parser (Section isa w Text)
dataSection String
cstart Parser (Section isa w Text)
-> (Section isa w Text -> Maybe (Section isa w Text))
-> ParsecT Void String Identity (Maybe (Section isa w Text))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Section isa w Text -> Maybe (Section isa w Text)
forall a. a -> Maybe a
Just
                        , String -> Parser (Section isa w Text)
forall isa w.
MnemonicParser isa =>
String -> Parser (Section isa w Text)
codeSection String
cstart Parser (Section isa w Text)
-> (Section isa w Text -> Maybe (Section isa w Text))
-> ParsecT Void String Identity (Maybe (Section isa w Text))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Section isa w Text -> Maybe (Section isa w Text)
forall a. a -> Maybe a
Just
                        ]
                    )
        eof
        return secs