{- Copyright 2016, Dominic Orchard, Andrew Rice, Mistral Contrastin, Matthew Danish Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -} {-# LANGUAGE FlexibleInstances, UndecidableInstances, ImplicitParams #-} {-# LANGUAGE MultiParamTypeClasses, FlexibleContexts, KindSignatures, ScopedTypeVariables, DeriveGeneric, DeriveDataTypeable #-} module Output where import Helpers import Traverse import Analysis.Annotations import Analysis.Syntax import Language.Fortran as Fortran import Language.Fortran.Pretty import Transformation.Syntax import Data.Text hiding (foldl,map,concatMap,take,drop,length,last,head,tail,replicate,concat) import qualified Data.Text as Text import Data.Map.Lazy hiding (map, foldl) import Data.Functor.Identity import Data.Generics import GHC.Generics import Data.List import Data.Generics.Uniplate.Data import Generics.Deriving.Copoint import Data.Char import Data.Generics.Zipper import Data.Maybe import Debug.Trace import Control.Monad.Trans.State.Lazy import Text.Printf -- Define new pretty printing version for HTML output data HTMLPP = HTMLPP instance PPVersion HTMLPP keyword = map pack ["end","subroutine","function","program","module","data", "common", "namelist", "external", "interface", "type", "include", "format", "len", "kind", "dimension", "allocatable", "parameter", "external", "intent", "intrinsic", "optional", "pointer", "save", "target", "volatile", "public", "private", "sequence", "operator", "assignment", "procedure", "do", "if", "else", "then", "allocate", "backspace", "call", "open", "close", "continue", "cycle", "deallocate", "endfile", "exit", "forall", "goto", "nullify", "inquire", "rewind", "stop", "where", "write", "rerun", "print", "read", "write", "implicit", "use"] outputHTML :: forall p . (Data p, Typeable p, PrintSlave p HTMLPP, PrintIndSlave (Fortran p) HTMLPP, Indentor (Decl p), Indentor (Fortran p)) => Fortran.ProgUnit p -> String outputHTML prog = unpack html where t :: SubName p -> SubName p t (SubName p n) = SubName p (addColor blue n) t x = x purple = "#800080" green = "#008000" blue = "#000080" toColor c t k = replace k (Text.concat [pack (""), k, pack ""]) t addColor c k = "" ++ k ++ "" pre l = Text.concat [pack "
", l, pack ""] types = map pack ["real", "integer", "character", "type", "logical"] html = let ?variant = HTMLPP in (Text.append (pack $ "" ++ "")) . (\t -> replace (pack "newline") (pack "\n") t) . (Text.concat . (map pre) . Text.lines) . (\t -> foldl (toColor green) t types) . (\t -> foldl (toColor purple) t keyword) . (pack . printMaster) -- . (pack . output) -- . (pack . paraBi (\p -> \ss -> (showPara p) ++ ss) "") -- . (pack . (para (\p -> \ss -> showPara p ++ (Prelude.concat ss)))) . (transformBi t) $ prog {- | Pretty printer for HTML, specialised to the analysis of CamFort, which mostly uses the default master behaviour, but with a few special cases -} instance PrintSlave Bool HTMLPP where printSlave = show instance PrintSlave SrcLoc HTMLPP where printSlave _ = "" -- not sure if I want this to shown instance (PrintIndSlave (Fortran p) HTMLPP, PrintSlave p HTMLPP, Indentor (Decl p), Indentor (Fortran p)) => PrintSlave (ProgUnit p) HTMLPP where printSlave = printMaster instance PrintSlave (DataForm p) HTMLPP where printSlave = printMaster instance (PrintSlave (DataForm p) HTMLPP) => PrintSlave (SubName p) HTMLPP where printSlave = printMaster instance PrintSlave (Implicit p) HTMLPP where printSlave = printMaster instance (Indentor (Decl p), PrintSlave (DataForm p) HTMLPP) => PrintSlave (Decl p) HTMLPP where printSlave = printMaster instance PrintSlave (Type p) HTMLPP where printSlave = printMaster instance PrintSlave (VarName p) HTMLPP where printSlave = printMaster instance (PrintSlave (DataForm p) HTMLPP) => PrintSlave (Expr p) HTMLPP where printSlave = printMaster instance PrintSlave (UnaryOp p) HTMLPP where printSlave = printMaster instance PrintSlave (BinOp p) HTMLPP where printSlave = printMaster instance PrintSlave (ArgList p) HTMLPP where printSlave = printMaster instance PrintSlave (BaseType p) HTMLPP where printSlave = printMaster instance (Indentor (Decl p)) => PrintSlave (InterfaceSpec p) HTMLPP where printSlave = printMaster instance PrintSlave (Arg p) HTMLPP where printSlave = printMaster instance PrintSlave (ArgName p) HTMLPP where printSlave = printMaster instance PrintSlave (GSpec p) HTMLPP where printSlave = printMaster instance PrintSlave (Attr p) HTMLPP where printSlave = printMaster instance PrintSlave (Fraction p) HTMLPP where printSlave = printMaster instance PrintSlave (MeasureUnitSpec p) HTMLPP where printSlave = printMaster instance (PrintSlave (DataForm p) HTMLPP, PrintIndSlave (Fortran p) HTMLPP, PrintSlave p HTMLPP, Indentor (Fortran p), Indentor (Decl p)) => PrintSlave (Block p) HTMLPP where printSlave = printMaster instance PrintSlave (Uses p) HTMLPP where printSlave u = showUse' u showUse' :: Uses p -> String showUse' (UseNil _) = "" showUse' (Uses _ (Use n []) us _) = ("use "++n++"\n") ++ (showUse' us) showUse' (Uses _ (Use n renames) us _) = ("use "++n++", " ++ (Prelude.concat $ Data.List.intersperse ", " (map (\(a, b) -> a ++ " => " ++ b) renames)) ++ "\n") ++ (showUse' us) instance (PrintIndSlave (Fortran p) HTMLPP, PrintSlave p HTMLPP, Indentor (Fortran p)) => PrintSlave (Fortran p) HTMLPP where printSlave (For p _ v e e' e'' f) = "do"++" "++printSlave v++" = "++printSlave e++", "++ printSlave e'++", "++printSlave e''++"\n"++ ""++"{"++printSlave p++"}\n" ++ (printIndSlave 1 f)++"\n"++(ind 1)++"end do" printSlave t = printMaster t instance PrintSlave (Spec p) HTMLPP where printSlave = printMaster instance Indentor (Fortran Bool) where indR t i = if (tag t) then let (s, SrcLoc f l c) = srcSpan t in Prelude.take c (repeat ' ') else ind i instance PrintIndSlave (Fortran A1) HTMLPP where printIndSlave = printIndMaster instance PrintIndSlave (Fortran Annotation) HTMLPP where printIndSlave i t@(For p _ v e e' e'' f) = (outputAnn p False i (show t)) ++ annotationMark i t ((ind i) ++ "do"++" "++printSlave v++" = "++ printSlave e++", "++ printSlave e'++", "++printSlave e''++"\n"++ (printIndSlave (i+1) f)++"\n"++(ind i)++"end do") -- printIndSlave i t@(FSeq p f1 f2) = (outputAnn p False i) ++ printIndSlave i f1 ++ printIndSlave i f2 printIndSlave i t = "