<commit_before>{-
 Copyright (C) 2012 <NAME>, <NAME> <http://gsd.uwaterloo.ca>

 Permission is hereby granted, free of charge, to any person obtaining a copy of
 this software and associated documentation files (the "Software"), to deal in
 the Software without restriction, including without limitation the rights to
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 of the Software, and to permit persons to whom the Software is furnished to do
 so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
-}
module Language.Clafer.Generator.Python where

import Language.Clafer.Common
import Language.Clafer.Front.Absclafer
import Language.Clafer.Intermediate.Intclafer
import Data.Char

tag:: String -> String -> String
tag name exp' = concat ["<", name, ">", exp', "</", name, ">\n"]

--Maybe a -> (a -> [Char]) -> [Char]
--optTag elem f = maybe "" f elem

tagType :: String -> String -> String -> String
tagType name typename exp' = opening ++ rest
  where
  opening = concat ["<", name, " xsi:type=\"cl:", typename, "\""]
  rest
    | null exp' =" />"
    | otherwise = concat [">", exp', "</", name, ">"]

genPythonInteger :: Integer -> String
genPythonInteger n = concat ["IntegerLiteral.IntegerLiteral(", show n, ")" ] -- might need to include IntegerLiteral

isNull :: String -> String
isNull [] = "\"\""
isNull x = x

boolHelper:: String -> String
boolHelper (x:xs) = toUpper x : xs
boolHelper [] = []

genPythonBoolean :: String -> Bool -> String
genPythonBoolean label b = concat [label, "=", boolHelper $ toLowerS $ show b]

genPythonString :: String -> String
genPythonString str = concat [ "StringLiteral.StringLiteral(", show str, ")"] -- might need to include StringLiteral

genPythonIntPair :: (Integer, Integer) -> String
genPythonIntPair (x, y) = concat
  [ "(", genPythonInteger x 
  , ","
  , genPythonInteger y, ")"]

genPythonModule :: IModule -> Result
genPythonModule imodule = concat
  [ "from ast import Module\n"
  , "from ast import GCard\n"
  , "from ast import Supers\n"
  , "from ast import Clafer\n"
  , "from ast import Exp\n"
  , "from ast import Declaration\n"
  , "from ast import LocalDeclaration\n"
  , "from ast import IRConstraint\n"
  , "from ast import FunExp\n"
  , "from ast import ClaferId\n"
  , "from ast import DeclPExp\n"
  , "from ast import Goal\n\n"
  , "from ast import IntegerLiteral\n"
  , "from ast import DoubleLiteral\n"
  , "from ast import StringLiteral\n"
  , "def getModule():\n"
  , "\tstack = []\n"
  , "\tmodule = Module.Module(\"\")\n"
  , "\tstack.append(module)\n"
  , concatMap genPythonElement $ mDecls imodule
  , "\treturn module"
  ]


genPythonClafer :: IClafer -> Result
genPythonClafer x = case x of
  IClafer pos' abstract' gcard' id' uid' super' card' glcard' elements'  ->
    concat [ "\t", genPythonPosition pos', "\n" 
           , "\t", genPythonAbstract abstract', "\n"
           , "\t", maybe "" genPythonGCard gcard', "\n"
           , "\t", genPythonId id', "\n"
           , "\t", genPythonUid uid', "\n"
           , "\t", genPythonSuper super', "\n"
           , "\t", maybe "" genPythonCard card', "\n"
           , "\t", genPythonGlCard glcard', "\n"
           , "\tcurrClafer = Clafer.Clafer(pos=pos, isAbstract=isAbstract, gcard=groupCard, ident=id, uid=uid, my_supers=my_supers, card=card, glCard=globalCard)\n"
           , "\tstack[-1].addElement(currClafer)\n"
           , "\tstack.append(currClafer)\n"
           , concatMap genPythonElement elements'
           , "\tstack.pop()\n"]
  
genPythonAbstract :: Bool -> String
genPythonAbstract isAbstract' = concat [ genPythonBoolean "isAbstract" isAbstract']

genPythonGCard :: IGCard -> String
genPythonGCard (IGCard isKeyword' interval') = concat 
		[ "groupCard = GCard.GCard(", genPythonBoolean "isKeyword" isKeyword', ", " 
    	, "interval=" , genPythonInterval interval' , ")"]

genPythonInterval :: (Integer, Integer) -> String
genPythonInterval (nMin, nMax) = concat
  [ "(", genPythonInteger nMin 
  , ",", genPythonInteger nMax
  , ")"]

genPythonId :: String -> String
genPythonId ident' = concat[ "id=\"", ident', "\""]

genPythonUid :: String -> String
genPythonUid uid' = concat [ "uid=\"", uid', "\""]

genPythonSuper :: ISuper -> String
genPythonSuper x = case x of
  ISuper isOverlapping' pexps' -> concat
    [ "my_supers = Supers.Supers(", genPythonBoolean "isOverlapping" isOverlapping', ", elements=["
    , concatMap (genPythonPExp "Super") pexps' , "])"]

genPythonCard :: (Integer, Integer) -> String
genPythonCard interval' = concat [ "card=" , genPythonInterval interval']

genPythonGlCard :: (Integer, Integer) -> String
genPythonGlCard interval' = concat ["globalCard=", genPythonInterval interval']

genPythonElement :: IElement -> String
genPythonElement x = case x of
  IEClafer clafer'  -> concat ["##### clafer #####\n" ,genPythonClafer clafer']
  IEConstraint isHard' pexp'  -> concat
                         [ "##### constraint #####\n", "\tconstraint = IRConstraint.IRConstraint(" , genPythonBoolean "isHard" isHard' , " ,"
                         , " exp=", genPythonPExp "ParentExp" pexp' , ")\n"
                         , "\tstack[-1].addElement(constraint)\n"]
  IEGoal isMaximize' pexp' -> concat 
                         [ "##### goal #####\n" ,"\tgoal = Goal.Goal(" , genPythonBoolean "isMaximize" isMaximize'
                         , " exp=", genPythonPExp "ParentExp" pexp' , ")\n"
                         , "\tstack[-1].addElement(goal)\n"]
                         
                                                         
{-genPythonAnyOp ft f xs = concatMap
  (\(tname, texp) -> tagType tname (ft texp) $ f texp) xs -}


genPythonPExp :: String -> PExp -> String
genPythonPExp tagName (PExp iType' pid' pos' iexp') = concat<commit_msg>Added missing commma<commit_after>{-
 Copyright (C) 2012 <NAME>, <NAME> <http://gsd.uwaterloo.ca>

 Permission is hereby granted, free of charge, to any person obtaining a copy of
 this software and associated documentation files (the "Software"), to deal in
 the Software without restriction, including without limitation the rights to
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 of the Software, and to permit persons to whom the Software is furnished to do
 so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
-}
module Language.Clafer.Generator.Python where

import Language.Clafer.Common
import Language.Clafer.Front.Absclafer
import Language.Clafer.Intermediate.Intclafer
import Data.Char

tag:: String -> String -> String
tag name exp' = concat ["<", name, ">", exp', "</", name, ">\n"]

--Maybe a -> (a -> [Char]) -> [Char]
--optTag elem f = maybe "" f elem

tagType :: String -> String -> String -> String
tagType name typename exp' = opening ++ rest
  where
  opening = concat ["<", name, " xsi:type=\"cl:", typename, "\""]
  rest
    | null exp' =" />"
    | otherwise = concat [">", exp', "</", name, ">"]

genPythonInteger :: Integer -> String
genPythonInteger n = concat ["IntegerLiteral.IntegerLiteral(", show n, ")" ] -- might need to include IntegerLiteral

isNull :: String -> String
isNull [] = "\"\""
isNull x = x

boolHelper:: String -> String
boolHelper (x:xs) = toUpper x : xs
boolHelper [] = []

genPythonBoolean :: String -> Bool -> String
genPythonBoolean label b = concat [label, "=", boolHelper $ toLowerS $ show b]

genPythonString :: String -> String
genPythonString str = concat [ "StringLiteral.StringLiteral(", show str, ")"] -- might need to include StringLiteral

genPythonIntPair :: (Integer, Integer) -> String
genPythonIntPair (x, y) = concat
  [ "(", genPythonInteger x 
  , ","
  , genPythonInteger y, ")"]

genPythonModule :: IModule -> Result
genPythonModule imodule = concat
  [ "from ast import Module\n"
  , "from ast import GCard\n"
  , "from ast import Supers\n"
  , "from ast import Clafer\n"
  , "from ast import Exp\n"
  , "from ast import Declaration\n"
  , "from ast import LocalDeclaration\n"
  , "from ast import IRConstraint\n"
  , "from ast import FunExp\n"
  , "from ast import ClaferId\n"
  , "from ast import DeclPExp\n"
  , "from ast import Goal\n\n"
  , "from ast import IntegerLiteral\n"
  , "from ast import DoubleLiteral\n"
  , "from ast import StringLiteral\n"
  , "def getModule():\n"
  , "\tstack = []\n"
  , "\tmodule = Module.Module(\"\")\n"
  , "\tstack.append(module)\n"
  , concatMap genPythonElement $ mDecls imodule
  , "\treturn module"
  ]


genPythonClafer :: IClafer -> Result
genPythonClafer x = case x of
  IClafer pos' abstract' gcard' id' uid' super' card' glcard' elements'  ->
    concat [ "\t", genPythonPosition pos', "\n" 
           , "\t", genPythonAbstract abstract', "\n"
           , "\t", maybe "" genPythonGCard gcard', "\n"
           , "\t", genPythonId id', "\n"
           , "\t", genPythonUid uid', "\n"
           , "\t", genPythonSuper super', "\n"
           , "\t", maybe "" genPythonCard card', "\n"
           , "\t", genPythonGlCard glcard', "\n"
           , "\tcurrClafer = Clafer.Clafer(pos=pos, isAbstract=isAbstract, gcard=groupCard, ident=id, uid=uid, my_supers=my_supers, card=card, glCard=globalCard)\n"
           , "\tstack[-1].addElement(currClafer)\n"
           , "\tstack.append(currClafer)\n"
           , concatMap genPythonElement elements'
           , "\tstack.pop()\n"]
  
genPythonAbstract :: Bool -> String
genPythonAbstract isAbstract' = concat [ genPythonBoolean "isAbstract" isAbstract']

genPythonGCard :: IGCard -> String
genPythonGCard (IGCard isKeyword' interval') = concat 
		[ "groupCard = GCard.GCard(", genPythonBoolean "isKeyword" isKeyword', ", " 
    	, "interval=" , genPythonInterval interval' , ")"]

genPythonInterval :: (Integer, Integer) -> String
genPythonInterval (nMin, nMax) = concat
  [ "(", genPythonInteger nMin 
  , ",", genPythonInteger nMax
  , ")"]

genPythonId :: String -> String
genPythonId ident' = concat[ "id=\"", ident', "\""]

genPythonUid :: String -> String
genPythonUid uid' = concat [ "uid=\"", uid', "\""]

genPythonSuper :: ISuper -> String
genPythonSuper x = case x of
  ISuper isOverlapping' pexps' -> concat
    [ "my_supers = Supers.Supers(", genPythonBoolean "isOverlapping" isOverlapping', ", elements=["
    , concatMap (genPythonPExp "Super") pexps' , "])"]

genPythonCard :: (Integer, Integer) -> String
genPythonCard interval' = concat [ "card=" , genPythonInterval interval']

genPythonGlCard :: (Integer, Integer) -> String
genPythonGlCard interval' = concat ["globalCard=", genPythonInterval interval']

genPythonElement :: IElement -> String
genPythonElement x = case x of
  IEClafer clafer'  -> concat ["##### clafer #####\n" ,genPythonClafer clafer']
  IEConstraint isHard' pexp'  -> concat
                         [ "##### constraint #####\n", "\tconstraint = IRConstraint.IRConstraint(" , genPythonBoolean "isHard" isHard' , " ,"
                         , " exp=", genPythonPExp "ParentExp" pexp' , ")\n"
                         , "\tstack[-1].addElement(constraint)\n"]
  IEGoal isMaximize' pexp' -> concat 
                         [ "##### goal #####\n" ,"\tgoal = Goal.Goal(" , genPythonBoolean "isMaximize" isMaximize'
                         , ", exp=", genPythonPExp "ParentExp" pexp' , ")\n"
                         , "\tstack[-1].addElement(goal)\n"]
                         
                                                         
{-genPythonAnyOp ft f xs = concatMap
  (\(tname, texp) -> tagType tname (ft texp) $ f texp) xs -}


genPythonPExp :: String -> PExp -> String
genPythonPExp tagName (PExp iType' pid' pos' iexp') = concat
{'fraction_non_alphanumeric': 0.1399717070103741, 'fraction_numerical': 0.0011002829298962591, 'mean_word_length': 3.257276681164269, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757054', 'n_tokens_mistral': 4070, 'n_tokens_neox': 3819, 'n_words': 1227}