<commit_before>{-# LANGUAGE OverloadedStrings #-}
module HLiquid.Parser where

import Text.Megaparsec
import Text.Megaparsec.Text

import Data.Text (pack)
import Data.Maybe (maybeToList)

import HLiquid.Lexer
import HLiquid.Syntax

import HLiquid.Parser.Variable
import HLiquid.Parser.Expression

fullFile = many liquid <* eof

liquid :: Parser Statement
liquid = assignTag
      <|> bodyText
      <|> breakTag
      <|> captureTag liquid
      <|> caseTag
      <|> commentTag
      <|> continueTag
      <|> cycleTag
      <|> decrementTag
      <|> formTag
      <|> forTag
      <|> ifTag
      <|> incrementTag
      <|> layoutTag
      <|> includeTag
      <|> sectionTag
      <|> paginateTag
      <|> tablerowTag
      <|> unlessTag
      <|> schemaTag
      <|> expressionTag

ifTag :: Parser Statement
ifTag = do
  b1 <- branch "if"

  branches <- many $ branch "elsif"

  e <- optional . try $ do
    braces $ symbol "else"
    b <- many liquid
    return $ Else b

  braces $ symbol "endif"
  return . If $ b1 : (branches ++ maybeToList e)

  where branch nm = do
                      cond <- try . braces $ symbol nm *> placeHolder
                      body <- many $ liquid
                      return $ Branch Comment body

unlessTag :: Parser Statement
unlessTag = do
  b1 <- branch "unless"

  branches <- many $ branch "elsif"

  e <- optional . try $ do
    braces $ symbol "else"
    b <- many liquid
    return $ Else b

  braces $ symbol "endunless"
  return . If $ b1 : (branches ++ maybeToList e)

  where branch nm = do
                      cond <- try . braces $ symbol nm *> placeHolder
                      body <- many $ liquid
                      return $ Branch Comment body

caseTag :: Parser Statement
caseTag = do
  try . braces $ symbol "case" <* placeHolder
  body <- some $ do
    try . braces $ symbol "when" *> placeHolder
    When <$> many (try liquid)

  e <- optional . try $ do
    braces $ symbol "else"
    b <- many liquid
    return $ Else b
  braces $ symbol "endcase"

  return $ Case body

forTag :: Parser Statement
forTag = do
  try . braces $ symbol "for" *> placeHolder
  b <- many $ liquid

  e <- optional . try $ do
    braces $ symbol "else"
    b <- many liquid
    return $ Else b

  braces $ symbol "endfor"
  return $ For Form b

breakTag :: Parser Statement
breakTag = simpleTag "break" (pure Break)

continueTag :: Parser Statement
continueTag = simpleTag "continue" (pure Continue)

cycleTag :: Parser Statement
cycleTag = try . braces $ symbol "cycle" *> placeHolder *> pure (Cycle [])

layoutTag :: Parser Statement
layoutTag = simpleTag "layout"
  (placeHolder *>  (pure $ Layout))

includeTag :: Parser Statement
includeTag = simpleTag "include"
  (placeHolder *>  (pure $ Include ""))

sectionTag :: Parser Statement
sectionTag = simpleTag "section"
  (placeHolder *>  (pure $ Section ))

tablerowTag :: Parser Statement
tablerowTag = simpleBlockTag "tablerow" head (many liquid)
  where head = placeHolder *> (pure . const $ Tablerow)
<commit_msg>Clean up more complex tags a little<commit_after>{-# LANGUAGE OverloadedStrings #-}
module HLiquid.Parser where

import Control.Monad (void)

import Text.Megaparsec
import Text.Megaparsec.Text

import Data.Text (pack)
import Data.Maybe (maybeToList)

import HLiquid.Lexer
import HLiquid.Syntax

import HLiquid.Parser.Variable
import HLiquid.Parser.Expression

fullFile = many liquid <* eof

liquid :: Parser Statement
liquid = assignTag
      <|> bodyText
      <|> breakTag
      <|> captureTag liquid
      <|> caseTag
      <|> commentTag
      <|> continueTag
      <|> cycleTag
      <|> decrementTag
      <|> formTag
      <|> forTag
      <|> ifTag
      <|> incrementTag
      <|> layoutTag
      <|> includeTag
      <|> sectionTag
      <|> paginateTag
      <|> tablerowTag
      <|> unlessTag
      <|> schemaTag
      <|> expressionTag

ifTag :: Parser Statement
ifTag = do
  b1 <- branch "if"

  branches <- many $ branch "elsif"

  e <- optional . try $ do
    braces $ symbol "else"
    b <- many liquid
    return $ Else b

  braces $ symbol "endif"
  return . If $ b1 : (branches ++ maybeToList e)

  where branch nm = do
          cond <- simpleTag nm placeHolder
          body <- many $ liquid
          return $ Branch Comment body

unlessTag :: Parser Statement
unlessTag = do
  b1 <- branch "unless"

  branches <- many $ branch "elsif"

  e <- optional . try $ do
    braces $ symbol "else"
    b <- many liquid
    return $ Else b

  simpleTag "endunless" (pure ())
  return . If $ b1 : (branches ++ maybeToList e)

  where branch nm = do
          cond <- simpleTag nm placeHolder
          body <- many $ liquid
          return $ Branch Comment body

caseTag :: Parser Statement
caseTag = tag "case" head body
  where head = placeHolder *> (pure Case)
        body f = do
          body <- some $ do
            simpleTag "when" (void placeHolder)
            When <$> many (try liquid)

          e <- optional . try $ do
            braces $ symbol "else"
            b <- many liquid
            return $ Else b
          simpleTag "endcase" (pure ())
          return $ f body

forTag :: Parser Statement
forTag = tag "for" head body
  where head = placeHolder *> (pure . uncurry $ For)
        body f = do
          b <- many $ liquid
          e <- optional . try $ do
            braces $ symbol "else"
            b <- many liquid
            return $ Else b
          braces $ symbol "endfor"
          return $ (curry f) Form b

breakTag :: Parser Statement
breakTag = simpleTag "break" (pure Break)

continueTag :: Parser Statement
continueTag = simpleTag "continue" (pure Continue)

cycleTag :: Parser Statement
cycleTag = try . braces $ symbol "cycle" *> placeHolder *> pure (Cycle [])

layoutTag :: Parser Statement
layoutTag = simpleTag "layout"
  (placeHolder *>  (pure $ Layout))

includeTag :: Parser Statement
includeTag = simpleTag "include"
  (placeHolder *>  (pure $ Include ""))

sectionTag :: Parser Statement
sectionTag = simpleTag "section"
  (placeHolder *>  (pure $ Section ))

tablerowTag :: Parser Statement
tablerowTag = simpleBlockTag "tablerow" head (many liquid)
  where head = placeHolder *> (pure . const $ Tablerow)

{'fraction_non_alphanumeric': 0.10450885668276973, 'fraction_numerical': 0.0012882447665056361, 'mean_word_length': 2.3995621237000546, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 63, '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': '3475616', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1860, 'n_words': 616}