module TextParser.Body (parseBody) where
import TextData.Data
import Text.Parsec
import Text.Parsec.String
import TextData.Keywords

{-FINAL body parser-}
parseBody :: Parser Body
parseBody = Body <$> (many parseBodyContent)
    where
      parseBodyContent :: Parser BodyContent
      parseBodyContent = Section_Body <$> parseSection

{-PARSE a single section-}
parseSection :: Parser Section
parseSection = parseAbstractSection section_tag Section


{-PARSE a single subSection-}
parseSubsection :: Parser SubSection
parseSubsection = parseAbstractSection subsection_tag SubSection

parseAbstractSection :: String -> (String -> [SectionContent] -> a) -> Parser a
parseAbstractSection s cons
          = do string s
               notFollowedBy $ char '-'
               t <- manyTill anyChar $ try $ string s
               newline
               sc <- many parseSectionContent
               return $ cons t sc

parseSectionContent :: Parser SectionContent
parseSectionContent = SubSection_Section  <$> parseSubsection
  --                 <|> Summation_Section  <$> parseSummation
                     <|> Text_Section       <$> parseText

{-PARSE a single summation-}
parseSummation :: Parser Summation
parseSummation = Summation <$> parseSummationContent
    where parseSummationContent = numberedContent <|> normalContent
          numberedContent = Numbered <$> (many1 $ (char plus) >> summationItem)
          normalContent   = Normal <$> (many1 $ (char star) >> summationItem)
          summationItem   = SummationItem <$> (many1 $ noneOf "\n")

parseText :: Parser Text
parseText =  many1 $ noneOf $ "-+*"
