{-
    BNF Converter: C++ Main file
    Copyright (C) 2004  Author:  Markus Forsberg, Michael Pellauer
-}

module BNFC.Backend.CPP.NoSTL (makeCppNoStl) where

import Data.Foldable (toList)
import qualified Data.Map as Map

import BNFC.Utils
import BNFC.CF
import BNFC.Options
import BNFC.Backend.Base
import BNFC.Backend.C            ( bufferH, bufferC, comment, testfileHeader )
import BNFC.Backend.C.CFtoBisonC ( cf2Bison )
import BNFC.Backend.C.CFtoFlexC  ( cf2flex, ParserMode(..) )
import BNFC.Backend.CPP.Common   ( commentWithEmacsModeHint )
import BNFC.Backend.CPP.Makefile
import BNFC.Backend.CPP.NoSTL.CFtoCPPAbs
import BNFC.Backend.CPP.STL.CFtoCVisitSkelSTL
import BNFC.Backend.CPP.PrettyPrinter
import qualified BNFC.Backend.Common.Makefile as Makefile

makeCppNoStl :: SharedOptions -> CF -> MkFiles ()
makeCppNoStl :: SharedOptions -> CF -> MkFiles ()
makeCppNoStl SharedOptions
opts CF
cf = do
    let (String
hfile, String
cfile) = String -> CF -> (String, String)
cf2CPPAbs String
name CF
cf
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Absyn.H" String
hfile
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Absyn.C" String
cfile
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Buffer.H" String
bufferH
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Buffer.C" (String -> MkFiles ()) -> String -> MkFiles ()
forall a b. (a -> b) -> a -> b
$ String -> String
bufferC String
"Buffer.H"
    let (String
flex, SymMap
env) = ParserMode -> CF -> (String, SymMap)
cf2flex ParserMode
parserMode CF
cf
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFileWithHint (String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
".l") String
flex
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFileWithHint (String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
".y") (String -> MkFiles ()) -> String -> MkFiles ()
forall a b. (a -> b) -> a -> b
$ RecordPositions -> ParserMode -> CF -> SymMap -> String
cf2Bison (SharedOptions -> RecordPositions
linenumbers SharedOptions
opts) ParserMode
parserMode CF
cf SymMap
env
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Parser.H" (String -> MkFiles ()) -> String -> MkFiles ()
forall a b. (a -> b) -> a -> b
$
      CF -> [Cat] -> [Cat] -> [String] -> String
forall {p} {p} {p}. p -> p -> [Cat] -> p -> String
mkHeaderFile CF
cf (CF -> [Cat]
forall f. CFG f -> [Cat]
allParserCats CF
cf) (NonEmpty Cat -> [Cat]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (NonEmpty Cat -> [Cat]) -> NonEmpty Cat -> [Cat]
forall a b. (a -> b) -> a -> b
$ CF -> NonEmpty Cat
forall f. CFG f -> NonEmpty Cat
allEntryPoints CF
cf) (SymMap -> [String]
forall k a. Map k a -> [a]
Map.elems SymMap
env)
    let (String
skelH, String
skelC) = Bool -> Maybe String -> CF -> (String, String)
cf2CVisitSkel Bool
False Maybe String
forall a. Maybe a
Nothing CF
cf
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Skeleton.H" String
skelH
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Skeleton.C" String
skelC
    let (String
prinH, String
prinC) = Bool -> Maybe String -> CF -> (String, String)
cf2CPPPrinter Bool
False Maybe String
forall a. Maybe a
Nothing CF
cf
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Printer.H" String
prinH
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Printer.C" String
prinC
    String -> String -> MkFiles ()
forall {c}. FileContent c => String -> c -> MkFiles ()
mkCppFile String
"Test.C" (CF -> String
cpptest CF
cf)
    SharedOptions -> (String -> Doc) -> MkFiles ()
Makefile.mkMakefile SharedOptions
opts ((String -> Doc) -> MkFiles ()) -> (String -> Doc) -> MkFiles ()
forall a b. (a -> b) -> a -> b
$ String -> String -> String -> Doc
makefile String
prefix String
name
  where
    name :: String
    name :: String
name = SharedOptions -> String
lang SharedOptions
opts
    -- The prefix is a string used by flex and bison
    -- that is prepended to generated function names.
    -- It should be a valid C identifier.
    prefix :: String
    prefix :: String
prefix = String -> String
snakeCase_ String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"_"
    parserMode :: ParserMode
    parserMode :: ParserMode
parserMode = Bool -> String -> ParserMode
CParser Bool
True String
prefix
    mkCppFile :: String -> c -> MkFiles ()
mkCppFile         String
x = String -> (String -> String) -> c -> MkFiles ()
forall c.
FileContent c =>
String -> (String -> String) -> c -> MkFiles ()
mkfile String
x String -> String
comment
    mkCppFileWithHint :: String -> c -> MkFiles ()
mkCppFileWithHint String
x = String -> (String -> String) -> c -> MkFiles ()
forall c.
FileContent c =>
String -> (String -> String) -> c -> MkFiles ()
mkfile String
x String -> String
commentWithEmacsModeHint

cpptest :: CF -> String
cpptest :: CF -> String
cpptest CF
cf = [String] -> String
unlines ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ [[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
  [ [String]
testfileHeader
  , [ String
"",
    String
"#include <stdio.h>",
    String
"#include <string.h>",
    String
"#include \"Parser.H\"",
    String
"#include \"Printer.H\"",
    String
"#include \"Absyn.H\"",
    String
"",
    String
"void usage() {",
    String
"  printf(\"usage: Call with one of the following argument " String -> String -> String
forall a. [a] -> [a] -> [a]
++
      String
"combinations:\\n\");",
    String
"  printf(\"\\t--help\\t\\tDisplay this help message.\\n\");",
    String
"  printf(\"\\t(no arguments)\\tParse stdin verbosely.\\n\");",
    String
"  printf(\"\\t(files)\\t\\tParse content of files verbosely.\\n\");",
    String
"  printf(\"\\t-s (files)\\tSilent mode. Parse content of files " String -> String -> String
forall a. [a] -> [a] -> [a]
++
      String
"silently.\\n\");",
    String
"}",
    String
"",
    String
"int main(int argc, char ** argv)",
    String
"{",
    String
"  FILE *input;",
    String
"  int quiet = 0;",
    String
"  char *filename = NULL;",
    String
"",
    String
"  if (argc > 1) {",
    String
"    if (strcmp(argv[1], \"-s\") == 0) {",
    String
"      quiet = 1;",
    String
"      if (argc > 2) {",
    String
"        filename = argv[2];",
    String
"      } else {",
    String
"        input = stdin;",
    String
"      }",
    String
"    } else {",
    String
"      filename = argv[1];",
    String
"    }",
    String
"  }",
    String
"",
    String
"  if (filename) {",
    String
"    input = fopen(filename, \"r\");",
    String
"    if (!input) {",
    String
"      usage();",
    String
"      exit(1);",
    String
"    }",
    String
"  } else input = stdin;",
    String
"  /* The default entry point is used. For other options see Parser.H */",
    String
"  " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
dat String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" *parse_tree = p" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
def String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(input);",
    String
"  if (parse_tree)",
    String
"  {",
    String
"    printf(\"\\nParse Successful!\\n\");",
    String
"    if (!quiet) {",
    String
"      printf(\"\\n[Abstract Syntax]\\n\");",
    String
"      ShowAbsyn *s = new ShowAbsyn();",
    String
"      printf(\"%s\\n\\n\", s->show(parse_tree));",
    String
"      printf(\"[Linearized Tree]\\n\");",
    String
"      PrintAbsyn *p = new PrintAbsyn();",
    String
"      printf(\"%s\\n\\n\", p->print(parse_tree));",
    String
"    }",
    String
"    delete(parse_tree);",
    String
"    return 0;",
    String
"  }",
    String
"  return 1;",
    String
"}",
    String
""
    ]
  ]
  where
   cat :: Cat
cat = CF -> Cat
firstEntry CF
cf
   dat :: String
dat = Cat -> String
identCat (Cat -> String) -> Cat -> String
forall a b. (a -> b) -> a -> b
$ Cat -> Cat
normCat Cat
cat
   def :: String
def = Cat -> String
identCat Cat
cat

mkHeaderFile :: p -> p -> [Cat] -> p -> String
mkHeaderFile p
_cf p
_cats [Cat]
eps p
_env = [String] -> String
unlines ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ [[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
  [ [ String
"#ifndef PARSER_HEADER_FILE"
    , String
"#define PARSER_HEADER_FILE"
    , String
""
    , String
"#include <stdio.h>"
    , String
"#include \"Absyn.H\""
    , String
""
    ]
  , (Cat -> String) -> [Cat] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Cat -> String
mkFunc [Cat]
eps
  , [ String
""
    , String
"#endif"
    ]
  ]
  where
  mkFunc :: Cat -> String
mkFunc Cat
s = Cat -> String
identCat (Cat -> Cat
normCat Cat
s) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"*" String -> String -> String
+++ String
"p" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Cat -> String
identCat Cat
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(FILE *inp);"