<commit_before>module Parse.Module (moduleDecl, elmModule) where

import Text.Parsec hiding (newline, spaces)

import Parse.Helpers
import Parse.Declaration as Decl
import qualified AST.Declaration
import qualified AST.Module as Module
import qualified AST.Module.Name as ModuleName
import qualified AST.Variable as Var
import AST.V0_16


elmModule :: IParser Module.Module
elmModule =
  do  preModule <- option [] freshLine
      h <- header
      decls <- declarations
      trailingComments <-
          (++)
              <$> option [] freshLine
              <*> option [] spaces
      eof

      return $ Module.Module preModule h (decls ++ (map AST.Declaration.BodyComment trailingComments))


declarations :: IParser [AST.Declaration.Decl]
declarations =
  (++) <$> ((\x -> [x]) <$> Decl.declaration) -- TODO: can there be comments before this?
      <*> (concat <$> many freshDef)


freshDef :: IParser [AST.Declaration.Decl]
freshDef =
    commitIf (freshLine >> (letter <|> char '_')) $
      do  comments <- freshLine
          decl <- Decl.declaration
          return $ (map AST.Declaration.BodyComment comments) ++ [decl]


header :: IParser Module.Header
header =
  do  ((isPortModule, names, exports, postExports), preDocsComments) <-
          option
            ((False, Commented [] ["Main"] [], Var.OpenListing (Commented [] () []), []), [])
            ((,) <$> moduleDecl <*> freshLine)
      (docs, postDocsComments) <-
        choice
          [ (,) <$> addLocation (Just <$> docComment) <*> freshLine
          , (,) <$> addLocation (return Nothing) <*> return []
          ]
      imports' <- imports
      return $
        Module.Header
          isPortModule
          names
          exports
          postExports
          docs
          ((fmap Module.ImportComment (preDocsComments ++ postDocsComments)) ++ imports')


moduleDecl :: IParser (Bool, Commented ModuleName.Raw, Var.Listing Var.Value, Comments)
moduleDecl =
  choice
    [ try moduleDecl_0_16
    , moduleDecl_0_17
    ]


moduleDecl_0_16 :: IParser (Bool, Commented ModuleName.Raw, Var.Listing Var.Value, Comments)
moduleDecl_0_16 =
  expecting "a module declaration" $
  do  try (reserved "module")
      (_, preName) <- whitespace
      names <- dotSep1 capVar <?> "the name of this module"
      (_, postName1) <- whitespace
      (postName2, exports) <- option ([], Var.OpenListing (Commented [] () [])) (listing value)
      (_, preWhere) <- whitespace
      reserved "where"
      return (False, Commented preName names (postName1 ++ postName2), exports, preWhere)


moduleDecl_0_17 :: IParser (Bool, Commented ModuleName.Raw, Var.Listing Var.Value, Comments)
moduleDecl_0_17 =
  expecting "a module declaration" $
  do
      (isPortModule, postPortComments) <- option (False, []) ((,) True <$> (reserved "port" *> (snd <$> whitespace)))
      try (reserved "module")
      (_, preName) <- whitespace
      names <- dotSep1 capVar <?> "the name of this module"
      (_, postName1) <- whitespace
      reserved "exposing"
      (postName2, exports) <- option ([], Var.OpenListing (Commented [] () [])) (listing value)
      return (isPortModule, Commented (postPortComments ++ preName) names postName1, exports, postName2)


imports :: IParser [Module.UserImport]
imports =
  concat <$> many ((:) <$> import' <*> (fmap Module.ImportComment <$> freshLine))


import' :: IParser Module.UserImport<commit_msg>Refactor Parse.Module.header<commit_after>module Parse.Module (moduleDecl, elmModule) where

import Text.Parsec hiding (newline, spaces)

import Parse.Helpers
import Parse.Declaration as Decl
import qualified AST.Declaration
import qualified AST.Module as Module
import qualified AST.Module.Name as ModuleName
import qualified AST.Variable as Var
import qualified Reporting.Annotation as A
import AST.V0_16


elmModule :: IParser Module.Module
elmModule =
  do  preModule <- option [] freshLine
      h <- header
      decls <- declarations
      trailingComments <-
          (++)
              <$> option [] freshLine
              <*> option [] spaces
      eof

      return $ Module.Module preModule h (decls ++ (map AST.Declaration.BodyComment trailingComments))


declarations :: IParser [AST.Declaration.Decl]
declarations =
  (++) <$> ((\x -> [x]) <$> Decl.declaration) -- TODO: can there be comments before this?
      <*> (concat <$> many freshDef)


freshDef :: IParser [AST.Declaration.Decl]
freshDef =
    commitIf (freshLine >> (letter <|> char '_')) $
      do  comments <- freshLine
          decl <- Decl.declaration
          return $ (map AST.Declaration.BodyComment comments) ++ [decl]


header :: IParser Module.Header
header =
  do  (h, preDocsComments) <-
        option
          (Module.Header
            False
            (Commented [] ["Main"] [])
            (Var.OpenListing (Commented [] () []))
            [], [])
          ((,) <$> moduleDecl <*> freshLine)
      (docs, postDocsComments) <-
        choice
          [ (,) <$> addLocation (Just <$> docComment) <*> freshLine
          , (,) <$> addLocation (return Nothing) <*> return []
          ]
      imports' <- imports
      return $
        h
          docs
          ((fmap Module.ImportComment (preDocsComments ++ postDocsComments)) ++ imports')


moduleDecl :: IParser (A.Located (Maybe String) -> [Module.UserImport] -> Module.Header)
moduleDecl =
  choice
    [ try moduleDecl_0_16
    , moduleDecl_0_17
    ]


moduleDecl_0_16 :: IParser (A.Located (Maybe String) -> [Module.UserImport] -> Module.Header)
moduleDecl_0_16 =
  expecting "a module declaration" $
  do  try (reserved "module")
      (_, preName) <- whitespace
      names <- dotSep1 capVar <?> "the name of this module"
      (_, postName1) <- whitespace
      (postName2, exports) <- option ([], Var.OpenListing (Commented [] () [])) (listing value)
      (_, preWhere) <- whitespace
      reserved "where"
      return $
        Module.Header
          False
          (Commented preName names (postName1 ++ postName2))
          exports
          preWhere


moduleDecl_0_17 :: IParser (A.Located (Maybe String) -> [Module.UserImport] -> Module.Header)
moduleDecl_0_17 =
  expecting "a module declaration" $
  do
      (isPortModule, postPortComments) <- option (False, []) ((,) True <$> (reserved "port" *> (snd <$> whitespace)))
      try (reserved "module")
      (_, preName) <- whitespace
      names <- dotSep1 capVar <?> "the name of this module"
      (_, postName1) <- whitespace
      reserved "exposing"
      (postName2, exports) <- option ([], Var.OpenListing (Commented [] () [])) (listing value)
      return $
        Module.Header
          isPortModule
          (Commented (postPortComments ++ preName) names postName1)
          exports
          postName2


imports :: IParser [Module.UserImport]
imports =
  concat <$> many ((:) <$> import' <*> (fmap Module.ImportComment <$> freshLine))


import' :: IParser Module.UserImport
{'fraction_non_alphanumeric': 0.13094377800260154, 'fraction_numerical': 0.008960832490244255, 'mean_word_length': 2.795940756993966, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6170455', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2010, 'n_words': 540}