<commit_before>{-# LANGUAGE NoMonomorphismRestriction, FlexibleContexts #-}
module PTS.Parser ( parseStmt, parseStmts, parseTerm, parseTermAtPos ) where

import Prelude hiding (abs, pi, elem, notElem, const)

import Control.Applicative hiding (many, Const, optional)
import Control.Monad
import Control.Monad.Reader

import Data.Char
import Data.Either
import Data.Eq
import Data.Foldable

import System.IO

import Text.ParserCombinators.Parsec hiding ((<|>))
import qualified Text.ParserCombinators.Parsec as Parsec

import Tools.Errors.Class
import Tools.Instances

import Parametric.Error
import Parametric.Parser
import Parametric.Parser.Error

import PTS.Algebra
import PTS.AST
import PTS.Instances
import PTS.Options
import PTS.Constants

     -----------------
    -- PTS PARSER --
     -----------------

natop n f x = mkNatOp n f <$> (keyword (show n) *> x) <*> (x <?> "second argument of '" ++ show n ++ "'")

expr = term simple rec mkPos "expression" where
  simple = withPos mkPos $ asum
    [ parens expr
    , brackets expr
    , abs mkLam lambda identOrMeta colon1 expr dot expr
    , abs mkPi  pi     identOrMeta colon1 expr dot expr
    , mkIfZero <$> (keyword "if0" *> expr)
             <*> (keyword "then" *> expr)
             <*> (keyword "else" *> expr)
    , natop (read "add") Add simple
    , natop (read "sub") Sub simple
    , natop (read "mul") Mul simple
    , natop (read "div") Div simple
    , mkConst <$> const
    , mkUnquote <$> unquote
    , var mkVar ident
    , mkNat <$> number ]

  rec = asum
    [ app mkApp simple
    , arr (\a b -> mkPi (freshvar b (read "unused")) a b) arrow (expr )]

unquote = char '$' *> asum
  [ var mkVar ident
  , parens expr]

stmt = withPos StmtPos $ asum
  [ try (Bind <$> ident <*> args <* colon1 <*> (Just <$> expr) <* assign <*> expr)
  , try (Bind <$> ident <*> args <* assign <*> pure Nothing <*> expr)
  , Term <$> expr]

stmts = (optional pragma *> stmt) `endBy` semi

args = many (parens argGroup)
<commit_msg>Refactor stmt to ease bugfix: reduce duplication<commit_after>{-# LANGUAGE NoMonomorphismRestriction, FlexibleContexts #-}
module PTS.Parser ( parseStmt, parseStmts, parseTerm, parseTermAtPos ) where

import Prelude hiding (abs, pi, elem, notElem, const)

import Control.Applicative hiding (many, Const, optional)
import Control.Monad
import Control.Monad.Reader

import Data.Char
import Data.Either
import Data.Eq
import Data.Foldable

import System.IO

import Text.ParserCombinators.Parsec hiding ((<|>))
import qualified Text.ParserCombinators.Parsec as Parsec

import Tools.Errors.Class
import Tools.Instances

import Parametric.Error
import Parametric.Parser
import Parametric.Parser.Error

import PTS.Algebra
import PTS.AST
import PTS.Instances
import PTS.Options
import PTS.Constants

     -----------------
    -- PTS PARSER --
     -----------------

natop n f x = mkNatOp n f <$> (keyword (show n) *> x) <*> (x <?> "second argument of '" ++ show n ++ "'")

expr = term simple rec mkPos "expression" where
  simple = withPos mkPos $ asum
    [ parens expr
    , brackets expr
    , abs mkLam lambda identOrMeta colon1 expr dot expr
    , abs mkPi  pi     identOrMeta colon1 expr dot expr
    , mkIfZero <$> (keyword "if0" *> expr)
             <*> (keyword "then" *> expr)
             <*> (keyword "else" *> expr)
    , natop (read "add") Add simple
    , natop (read "sub") Sub simple
    , natop (read "mul") Mul simple
    , natop (read "div") Div simple
    , mkConst <$> const
    , mkUnquote <$> unquote
    , var mkVar ident
    , mkNat <$> number ]

  rec = asum
    [ app mkApp simple
    , arr (\a b -> mkPi (freshvar b (read "unused")) a b) arrow (expr )]

unquote = char '$' *> asum
  [ var mkVar ident
  , parens expr]

stmt = withPos StmtPos $ asum
  [ try (Bind <$> ident <*> args <*> optionMaybe (colon1 *> expr) <* assign <*> expr)
  , Term <$> expr]

stmts = (optional pragma *> stmt) `endBy` semi

args = many (parens argGroup)

{'fraction_non_alphanumeric': 0.13659401926001014, 'fraction_numerical': 0.0020273694880892043, 'mean_word_length': 3.0606995884773665, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 53, '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': '6921165', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1314, 'n_words': 426}