{-# LANGUAGE OverloadedStrings #-}

module BNFC.Backend.Latex.Makefile where

import BNFC.Backend.CommonInterface.Backend

import BNFC.CF

import BNFC.Options.GlobalOptions

import BNFC.Backend.Latex.State

import BNFC.Prelude

import Control.Monad.State

import Data.String (fromString)

import System.FilePath (takeBaseName)

import Prettyprinter
import Prettyprinter.Render.String


latexmakefile :: LBNF -> State LatexBackendState Result
latexmakefile :: LBNF -> State LatexBackendState Result
latexmakefile LBNF
_ = do
  LatexBackendState
st <- StateT LatexBackendState Identity LatexBackendState
forall s (m :: * -> *). MonadState s m => m s
get
  let
    cfName :: String
cfName = String -> String
takeBaseName (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ GlobalOptions -> String
optInput (GlobalOptions -> String) -> GlobalOptions -> String
forall a b. (a -> b) -> a -> b
$ LatexBackendState -> GlobalOptions
globalOpt LatexBackendState
st
  Result -> State LatexBackendState Result
forall (m :: * -> *) a. Monad m => a -> m a
return [(String
"Makefile", String -> String
makefileString String
cfName)]

makefileString :: String -> String
makefileString :: String -> String
makefileString String
cfName =
  SimpleDocStream () -> String
forall ann. SimpleDocStream ann -> String
renderString (SimpleDocStream () -> String)
-> (Doc () -> SimpleDocStream ()) -> Doc () -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutOptions -> Doc () -> SimpleDocStream ()
forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutSmart LayoutOptions
defaultLayoutOptions (Doc () -> String) -> Doc () -> String
forall a b. (a -> b) -> a -> b
$
    String -> Doc ()
makefileDoc String
cfName

makefileDoc :: String -> Doc ()
makefileDoc :: String -> Doc ()
makefileDoc String
cfName = [Doc ()] -> Doc ()
forall ann. [Doc ann] -> Doc ann
vsep
  [ Doc ()
"## File generated by the BNF Converter."
  , Doc ()
forall ann. Doc ann
emptyDoc
  , Doc ()
"all :" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"pdf"
  , Doc ()
forall ann. Doc ann
emptyDoc
  , Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"pdf" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
forall ann. Doc ann
colon Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"tex"
  , Doc ()
"\tpdflatex" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"tex"
  , Doc ()
forall ann. Doc ann
emptyDoc
  , Doc ()
"clean :"
  , Doc ()
"\t-rm" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"pdf" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+>
               Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"aux" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+>
               Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"log"
  , Doc ()
forall ann. Doc ann
emptyDoc
  , Doc ()
"cleanall : clean"
  , Doc ()
"\t-rm" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
"Makefile" Doc () -> Doc () -> Doc ()
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
grammar Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
forall ann. Doc ann
dot Doc () -> Doc () -> Doc ()
forall a. Semigroup a => a -> a -> a
<> Doc ()
"tex"
  ]
  where
    grammar :: Doc ()
grammar = String -> Doc ()
forall a. IsString a => String -> a
fromString String
cfName